summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mime/attachmentmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/domain/mime/attachmentmodel.cpp')
-rw-r--r--framework/src/domain/mime/attachmentmodel.cpp38
1 files changed, 37 insertions, 1 deletions
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 @@
20#include "messageparser.h" 20#include "messageparser.h"
21#include "mimetreeparser/interface.h" 21#include "mimetreeparser/interface.h"
22 22
23#include <QIcon>
24#include <QDebug> 23#include <QDebug>
24#include <KMime/Content>
25#include <QFile>
26#include <QStandardPaths>
27#include <QDir>
25 28
26QString sizeHuman(const Content::Ptr &content) 29QString sizeHuman(const Content::Ptr &content)
27{ 30{
@@ -126,6 +129,39 @@ QVariant AttachmentModel::data(const QModelIndex &index, int role) const
126 return QVariant(); 129 return QVariant();
127} 130}
128 131
132bool AttachmentModel::saveAttachmentToDisk(const QModelIndex &index)
133{
134 if (index.internalPointer()) {
135 const auto entry = static_cast<Part *>(index.internalPointer());
136 const auto content = entry->content().at(0);
137 auto filename = entry->mailMime()->filename();
138 auto data = content->mailMime()->decodedContent();
139
140 auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
141 if (downloadDir.isEmpty()) {
142 downloadDir = "~";
143 }
144 downloadDir += "/kube/";
145 QDir{}.mkpath(downloadDir);
146
147 auto fname = downloadDir + filename;
148
149 if (content->mailMime()->isText() && !data.isEmpty()) {
150 // convert CRLF to LF before writing text attachments to disk
151 data = KMime::CRLFtoLF(data);
152 }
153 QFile f(fname);
154 if (!f.open(QIODevice::ReadWrite)) {
155 qWarning() << "Failed to write attachment to file:" << fname << " Error: " << f.errorString();
156 return false;
157 }
158 f.write(data);
159 qInfo() << "Wrote attachment to file: " << fname;
160 return true;
161 }
162 return false;
163}
164
129QModelIndex AttachmentModel::parent(const QModelIndex &index) const 165QModelIndex AttachmentModel::parent(const QModelIndex &index) const
130{ 166{
131 return QModelIndex(); 167 return QModelIndex();