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 /attachments.cpp | |
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
Diffstat (limited to 'attachments.cpp')
-rw-r--r-- | attachments.cpp | 54 |
1 files changed, 53 insertions, 1 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) { |