From 5df2baeb48298d422000574f8755ea08b0c19a97 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 24 May 2017 08:01:12 +0200 Subject: Saving of attachments --- framework/src/domain/mime/attachmentmodel.cpp | 38 ++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (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 2871579c..714177e1 100644 --- a/framework/src/domain/mime/attachmentmodel.cpp +++ b/framework/src/domain/mime/attachmentmodel.cpp @@ -20,8 +20,11 @@ #include "messageparser.h" #include "mimetreeparser/interface.h" -#include #include +#include +#include +#include +#include QString sizeHuman(const Content::Ptr &content) { @@ -126,6 +129,39 @@ QVariant AttachmentModel::data(const QModelIndex &index, int role) const return QVariant(); } +bool AttachmentModel::saveAttachmentToDisk(const QModelIndex &index) +{ + 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); + } + QFile f(fname); + if (!f.open(QIODevice::ReadWrite)) { + qWarning() << "Failed to write attachment to file:" << fname << " Error: " << f.errorString(); + return false; + } + f.write(data); + qInfo() << "Wrote attachment to file: " << fname; + return true; + } + return false; +} + QModelIndex AttachmentModel::parent(const QModelIndex &index) const { return QModelIndex(); -- cgit v1.2.3