From acf2a53fea8fdbe366627e45fffe87434e3c2e7e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 24 May 2017 12:59:23 +0200 Subject: Attachment open and download buttons We'll need proper icons though. --- framework/src/domain/mime/attachmentmodel.cpp | 40 +++++++++++++++++++-------- 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'framework/src/domain/mime/attachmentmodel.cpp') diff --git a/framework/src/domain/mime/attachmentmodel.cpp b/framework/src/domain/mime/attachmentmodel.cpp index 714177e1..99745796 100644 --- a/framework/src/domain/mime/attachmentmodel.cpp +++ b/framework/src/domain/mime/attachmentmodel.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include QString sizeHuman(const Content::Ptr &content) @@ -129,34 +130,49 @@ QVariant AttachmentModel::data(const QModelIndex &index, int role) const return QVariant(); } -bool AttachmentModel::saveAttachmentToDisk(const QModelIndex &index) +static QString saveAttachmentToDisk(const QModelIndex &index, const QString &path) { if (index.internalPointer()) { const auto entry = static_cast(index.internalPointer()); const auto content = entry->content().at(0); auto filename = entry->mailMime()->filename(); auto data = content->mailMime()->decodedContent(); - - auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); - if (downloadDir.isEmpty()) { - downloadDir = "~"; - } - downloadDir += "/kube/"; - QDir{}.mkpath(downloadDir); - - auto fname = downloadDir + filename; - if (content->mailMime()->isText() && !data.isEmpty()) { // convert CRLF to LF before writing text attachments to disk data = KMime::CRLFtoLF(data); } + auto fname = path + filename; QFile f(fname); if (!f.open(QIODevice::ReadWrite)) { qWarning() << "Failed to write attachment to file:" << fname << " Error: " << f.errorString(); - return false; + return {}; } f.write(data); qInfo() << "Wrote attachment to file: " << fname; + return fname; + } + return {}; + +} + +bool AttachmentModel::saveAttachmentToDisk(const QModelIndex &index) +{ + auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); + if (downloadDir.isEmpty()) { + downloadDir = "~"; + } + downloadDir += "/kube/"; + QDir{}.mkpath(downloadDir); + return !::saveAttachmentToDisk(index, downloadDir).isEmpty(); +} + +bool AttachmentModel::openAttachment(const QModelIndex &index) +{ + auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation)+ "/kube/"; + QDir{}.mkpath(downloadDir); + const auto filePath = ::saveAttachmentToDisk(index, downloadDir); + if (!filePath.isEmpty()) { + QDesktopServices::openUrl(QUrl("file://" + filePath)); return true; } return false; -- cgit v1.2.3