diff options
Diffstat (limited to 'framework/src/domain/mime/attachmentmodel.cpp')
-rw-r--r-- | framework/src/domain/mime/attachmentmodel.cpp | 40 |
1 files changed, 28 insertions, 12 deletions
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 @@ | |||
24 | #include <KMime/Content> | 24 | #include <KMime/Content> |
25 | #include <QFile> | 25 | #include <QFile> |
26 | #include <QStandardPaths> | 26 | #include <QStandardPaths> |
27 | #include <QDesktopServices> | ||
27 | #include <QDir> | 28 | #include <QDir> |
28 | 29 | ||
29 | QString sizeHuman(const Content::Ptr &content) | 30 | QString sizeHuman(const Content::Ptr &content) |
@@ -129,34 +130,49 @@ QVariant AttachmentModel::data(const QModelIndex &index, int role) const | |||
129 | return QVariant(); | 130 | return QVariant(); |
130 | } | 131 | } |
131 | 132 | ||
132 | bool AttachmentModel::saveAttachmentToDisk(const QModelIndex &index) | 133 | static QString saveAttachmentToDisk(const QModelIndex &index, const QString &path) |
133 | { | 134 | { |
134 | if (index.internalPointer()) { | 135 | if (index.internalPointer()) { |
135 | const auto entry = static_cast<Part *>(index.internalPointer()); | 136 | const auto entry = static_cast<Part *>(index.internalPointer()); |
136 | const auto content = entry->content().at(0); | 137 | const auto content = entry->content().at(0); |
137 | auto filename = entry->mailMime()->filename(); | 138 | auto filename = entry->mailMime()->filename(); |
138 | auto data = content->mailMime()->decodedContent(); | 139 | 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()) { | 140 | if (content->mailMime()->isText() && !data.isEmpty()) { |
150 | // convert CRLF to LF before writing text attachments to disk | 141 | // convert CRLF to LF before writing text attachments to disk |
151 | data = KMime::CRLFtoLF(data); | 142 | data = KMime::CRLFtoLF(data); |
152 | } | 143 | } |
144 | auto fname = path + filename; | ||
153 | QFile f(fname); | 145 | QFile f(fname); |
154 | if (!f.open(QIODevice::ReadWrite)) { | 146 | if (!f.open(QIODevice::ReadWrite)) { |
155 | qWarning() << "Failed to write attachment to file:" << fname << " Error: " << f.errorString(); | 147 | qWarning() << "Failed to write attachment to file:" << fname << " Error: " << f.errorString(); |
156 | return false; | 148 | return {}; |
157 | } | 149 | } |
158 | f.write(data); | 150 | f.write(data); |
159 | qInfo() << "Wrote attachment to file: " << fname; | 151 | qInfo() << "Wrote attachment to file: " << fname; |
152 | return fname; | ||
153 | } | ||
154 | return {}; | ||
155 | |||
156 | } | ||
157 | |||
158 | bool AttachmentModel::saveAttachmentToDisk(const QModelIndex &index) | ||
159 | { | ||
160 | auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); | ||
161 | if (downloadDir.isEmpty()) { | ||
162 | downloadDir = "~"; | ||
163 | } | ||
164 | downloadDir += "/kube/"; | ||
165 | QDir{}.mkpath(downloadDir); | ||
166 | return !::saveAttachmentToDisk(index, downloadDir).isEmpty(); | ||
167 | } | ||
168 | |||
169 | bool AttachmentModel::openAttachment(const QModelIndex &index) | ||
170 | { | ||
171 | auto downloadDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation)+ "/kube/"; | ||
172 | QDir{}.mkpath(downloadDir); | ||
173 | const auto filePath = ::saveAttachmentToDisk(index, downloadDir); | ||
174 | if (!filePath.isEmpty()) { | ||
175 | QDesktopServices::openUrl(QUrl("file://" + filePath)); | ||
160 | return true; | 176 | return true; |
161 | } | 177 | } |
162 | return false; | 178 | return false; |