diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-05-24 12:59:23 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-05-24 12:59:23 +0200 |
commit | acf2a53fea8fdbe366627e45fffe87434e3c2e7e (patch) | |
tree | f241beb77b302df6967b34d7297f55f23eaf6dea /framework/src | |
parent | 9ed658053d5d41f4bf8c83072e6fa62990bd7ad8 (diff) | |
download | kube-acf2a53fea8fdbe366627e45fffe87434e3c2e7e.tar.gz kube-acf2a53fea8fdbe366627e45fffe87434e3c2e7e.zip |
Attachment open and download buttons
We'll need proper icons though.
Diffstat (limited to 'framework/src')
-rw-r--r-- | framework/src/domain/mime/attachmentmodel.cpp | 40 | ||||
-rw-r--r-- | framework/src/domain/mime/messageparser.h | 1 |
2 files changed, 29 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; |
diff --git a/framework/src/domain/mime/messageparser.h b/framework/src/domain/mime/messageparser.h index 1f44b296..de72a025 100644 --- a/framework/src/domain/mime/messageparser.h +++ b/framework/src/domain/mime/messageparser.h | |||
@@ -112,6 +112,7 @@ public: | |||
112 | int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; | 112 | int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; |
113 | 113 | ||
114 | Q_INVOKABLE bool saveAttachmentToDisk(const QModelIndex &parent); | 114 | Q_INVOKABLE bool saveAttachmentToDisk(const QModelIndex &parent); |
115 | Q_INVOKABLE bool openAttachment(const QModelIndex &index); | ||
115 | 116 | ||
116 | private: | 117 | private: |
117 | std::unique_ptr<AttachmentModelPrivate> d; | 118 | std::unique_ptr<AttachmentModelPrivate> d; |