diff options
author | ubbo <ubbo@34ebc366-c3a9-4b3c-9f84-69acf7962910> | 2010-08-19 22:19:26 +0000 |
---|---|---|
committer | ubbo <ubbo@34ebc366-c3a9-4b3c-9f84-69acf7962910> | 2010-08-19 22:19:26 +0000 |
commit | f52aacbc3db505dc1aa692327180e5765012bed1 (patch) | |
tree | c14210ac5e4fa568fa15b88cd8975be2e367fe0c | |
parent | decode quoted printable (diff) | |
download | gpg4usb-f52aacbc3db505dc1aa692327180e5765012bed1.tar.gz gpg4usb-f52aacbc3db505dc1aa692327180e5765012bed1.zip |
start implementing 'open file with' for attachments. very rough for now, needs more work
git-svn-id: http://cpunk.de/svn/src/gpg4usb/trunk@364 34ebc366-c3a9-4b3c-9f84-69acf7962910
-rw-r--r-- | attachments.cpp | 54 | ||||
-rw-r--r-- | attachments.h | 7 | ||||
-rw-r--r-- | gpgwin.cpp | 29 | ||||
-rwxr-xr-x | settingsdialog.cpp | 13 | ||||
-rwxr-xr-x | settingsdialog.h | 3 |
5 files changed, 89 insertions, 17 deletions
diff --git a/attachments.cpp b/attachments.cpp index 5c08bf1..f9d95aa 100644 --- a/attachments.cpp +++ b/attachments.cpp @@ -58,7 +58,6 @@ Attachments::Attachments(QString iconpath, QWidget *parent) tableView->horizontalHeader()->setStretchLastSection(true); QVBoxLayout *layout = new QVBoxLayout; - //layout->addWidget(mAttachmentTable); layout->addWidget(tableView); setLayout(layout); createActions(); @@ -69,6 +68,10 @@ void Attachments::contextMenuEvent(QContextMenuEvent *event) { QMenu menu(this); menu.addAction(saveFileAct); + // enable open with only if allowed by user + if(settings.value("mime/openAttachment").toBool()) + menu.addAction(openFileAct); + menu.exec(event->globalPos()); } @@ -79,6 +82,11 @@ void Attachments::createActions() saveFileAct->setIcon(QIcon(iconPath + "filesave.png")); connect(saveFileAct, SIGNAL(triggered()), this, SLOT(saveFile())); + openFileAct = new QAction(tr("Open File"), this); + openFileAct->setToolTip(tr("Open this file")); + openFileAct->setIcon(QIcon(iconPath + "fileopen.png")); + connect(openFileAct, SIGNAL(triggered()), this, SLOT(openFile())); + } void Attachments::saveFile() @@ -119,7 +127,51 @@ void Attachments::saveByteArrayToFile(QByteArray outBuffer, QString filename) out.writeRawData(outBuffer.data(), outBuffer.length()); } +/** + * WIP: TODO: + * - create attachments dir if not existing + * - ask for cleanup of dir on exit + * - remove code-duplication with saveByteArrayToFile + */ +void Attachments::openFile() { + + QString tmpPath = qApp->applicationDirPath() + "/attachments/"; + //QDir p = QDir(qApp->applicationDirPath() + "/attachments/"); + //if(!p.exists()) { + // qDebug() << "creating " << p; + // p.mkpath("/attachments/"); + //} + + QModelIndexList indexes = tableView->selectionModel()->selection().indexes(); + MimePart mp = table->getMimePart(indexes.at(0).row()); + + qDebug() << "mime: " << mp.header.getValue("Content-Type"); + + QString filename = mp.header.getParam("Content-Type", "name"); + // TODO: find out why filename is quoted + qDebug() << "file: " << filename; + filename.chop(1); + filename.remove(0, 1); + filename.prepend(tmpPath); + + qDebug() << "file: " << filename; + QByteArray outBuffer = QByteArray::fromBase64(mp.body); + + QFile outfile(filename); + if (!outfile.open(QFile::WriteOnly)) { + QMessageBox::warning(this, tr("File"), + tr("Cannot write file %1:\n%2.") + .arg(filename) + .arg(outfile.errorString())); + return; + } + + QDataStream out(&outfile); + out.writeRawData(outBuffer.data(), outBuffer.length()); + + QDesktopServices::openUrl(QUrl("file://"+filename, QUrl::TolerantMode)); +} void Attachments::addMimePart(MimePart *mp) { diff --git a/attachments.h b/attachments.h index 4a10a02..6fe1bae 100644 --- a/attachments.h +++ b/attachments.h @@ -33,6 +33,10 @@ #include "QMessageBox" #include "QContextMenuEvent" #include "QFileDialog" +#include "QUrl" +#include "QDesktopServices" +#include "QSettings" +#include "QApplication" class Attachments : public QWidget { @@ -40,6 +44,7 @@ class Attachments : public QWidget public slots: void saveFile(); + void openFile(); public: Attachments(QString iconpath, QWidget *parent = 0); @@ -49,9 +54,11 @@ private: void createActions(); void saveByteArrayToFile(QByteArray outBuffer, QString filename); QAction *saveFileAct; + QAction *openFileAct; QString iconPath; AttachmentTableModel *table; QTableView *tableView; + QSettings settings; protected: void contextMenuEvent(QContextMenuEvent *event); @@ -515,11 +515,11 @@ void GpgWin::encrypt() void GpgWin::decrypt() { - QByteArray *tmp = new QByteArray(); + QByteArray *decrypted = new QByteArray(); QByteArray text = edit->toPlainText().toAscii(); preventNoDataErr(&text); - mCtx->decrypt(text, tmp); - if (!tmp->isEmpty()) { + mCtx->decrypt(text, decrypted); + if (!decrypted->isEmpty()) { /** * 1) is it mime (content-type:) @@ -527,29 +527,30 @@ void GpgWin::decrypt() * 2) choose action depending on content-type */ - if(Mime::isMime(tmp)) { - Header header = Mime::getHeader(tmp); + if(Mime::isMime(decrypted)) { + Header header = Mime::getHeader(decrypted); // is it multipart, is multipart-parsing enabled if(header.getValue("Content-Type") == "multipart/mixed" && settings.value("mime/parseMime").toBool()) { - parseMime(tmp); + parseMime(decrypted); } else if(header.getValue("Content-Type") == "text/plain" && settings.value("mime/parseQP").toBool()){ if (header.getValue("Content-Transfer-Encoding") == "quoted-printable") { - QByteArray *decode = new QByteArray(); - Mime::quotedPrintableDecode(*tmp, *decode); + QByteArray *decoded = new QByteArray(); + Mime::quotedPrintableDecode(*decrypted, *decoded); //TODO: remove header - tmp = decode; + decrypted = decoded; } } } - edit->setPlainText(QString::fromUtf8(*tmp)); + edit->setPlainText(QString::fromUtf8(*decrypted)); + //edit->setPlainText(*decrypted); } } @@ -560,11 +561,11 @@ void GpgWin::decrypt() void GpgWin::parseMime(QByteArray *message) { - if (! Mime::isMultipart(message)) { + /*if (! Mime::isMultipart(message)) { qDebug() << "no multipart"; return; - } - qDebug() << "multipart"; + }*/ + //qDebug() << "multipart"; QString pText; bool showmadock = false; @@ -590,7 +591,7 @@ void GpgWin::parseMime(QByteArray *message) } } - *message = pText.toAscii(); + *message = pText.toUtf8(); if (showmadock) aDock->show(); } diff --git a/settingsdialog.cpp b/settingsdialog.cpp index ed71294..42c7d28 100755 --- a/settingsdialog.cpp +++ b/settingsdialog.cpp @@ -122,11 +122,18 @@ SettingsDialog::SettingsDialog(QWidget *parent) *****************************************/ mimeParseBox = new QGroupBox(tr("MIME-parsing (Experimental)")); - mimeParseBoxLayout = new QHBoxLayout(); + mimeParseBoxLayout = new QVBoxLayout(); mimeParseCheckBox = new QCheckBox(tr("Try to split attachments from PGP-MIME ecrypted messages."), this); mimeParseBoxLayout->addWidget(mimeParseCheckBox); mimeQPCheckBox = new QCheckBox(tr("Try to recognice quoted printable."), this); mimeParseBoxLayout->addWidget(mimeQPCheckBox); + mimeOpenAttachmentCheckBox = new QCheckBox(tr("Enable open with external app, saves file in tmp folder."), this); + //mimeOpenAttachmentCheckBox->setWhatsThis("Open attachments <b>with</b>... neeeds temp foder.."); + mimeOpenAttachmentCheckBox->setToolTip("Open attachments with Application for the filetype. <b>Needs saving the file to temporarly folder.</b> For now its your job to clean this folder up if you want."); + /* + * Here could be something like Qstring("?"), or an icon with an ?, with the action "show tooltip" + */ + mimeParseBoxLayout->addWidget(mimeOpenAttachmentCheckBox); mimeParseBox->setLayout(mimeParseBoxLayout); /***************************************** @@ -198,6 +205,9 @@ void SettingsDialog::setSettings() // Qouted Printable if (settings.value("mime/parseQP").toBool()) mimeQPCheckBox->setCheckState(Qt::Checked); + // Open Attachments with external app + if (settings.value("mime/openAttachment").toBool()) mimeOpenAttachmentCheckBox->setCheckState(Qt::Checked); + //Language setting QString langKey = settings.value("int/lang").toString(); QString langValue = lang.value(langKey); @@ -236,6 +246,7 @@ void SettingsDialog::applySettings() settings.setValue("mime/parsemime" , mimeParseCheckBox->isChecked()); settings.setValue("mime/parseQP" , mimeQPCheckBox->isChecked()); + settings.setValue("mime/openAttachment" , mimeOpenAttachmentCheckBox->isChecked()); settings.setValue("int/lang", lang.key(langSelectBox->currentText())); diff --git a/settingsdialog.h b/settingsdialog.h index a1480b8..ce558bf 100755 --- a/settingsdialog.h +++ b/settingsdialog.h @@ -62,6 +62,7 @@ private: QCheckBox *saveCheckedKeysCheckBox; QCheckBox *mimeParseCheckBox; QCheckBox *mimeQPCheckBox; + QCheckBox *mimeOpenAttachmentCheckBox; QComboBox *langSelectBox; QHash<QString, QString> lang; @@ -69,7 +70,7 @@ private: QHBoxLayout *iconStyleBoxLayout; QHBoxLayout *windowSizeBoxLayout; QHBoxLayout *saveCheckedKeysBoxLayout; - QHBoxLayout *mimeParseBoxLayout; + QVBoxLayout *mimeParseBoxLayout; QVBoxLayout *vbox; void setSettings(); |