diff options
Diffstat (limited to 'framework/src/domain/mimetreeparser/otp/attachmentstrategy.cpp')
-rw-r--r-- | framework/src/domain/mimetreeparser/otp/attachmentstrategy.cpp | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/framework/src/domain/mimetreeparser/otp/attachmentstrategy.cpp b/framework/src/domain/mimetreeparser/otp/attachmentstrategy.cpp new file mode 100644 index 00000000..5ea21133 --- /dev/null +++ b/framework/src/domain/mimetreeparser/otp/attachmentstrategy.cpp | |||
@@ -0,0 +1,343 @@ | |||
1 | /* -*- c++ -*- | ||
2 | attachmentstrategy.cpp | ||
3 | |||
4 | This file is part of KMail, the KDE mail client. | ||
5 | Copyright (c) 2003 Marc Mutz <mutz@kde.org> | ||
6 | Copyright (C) 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net | ||
7 | Copyright (c) 2009 Andras Mantia <andras@kdab.net> | ||
8 | |||
9 | KMail is free software; you can redistribute it and/or modify it | ||
10 | under the terms of the GNU General Public License, version 2, as | ||
11 | published by the Free Software Foundation. | ||
12 | |||
13 | KMail is distributed in the hope that it will be useful, but | ||
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU General Public License | ||
19 | along with this program; if not, write to the Free Software | ||
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | |||
22 | In addition, as a special exception, the copyright holders give | ||
23 | permission to link the code of this program with any edition of | ||
24 | the Qt library by Trolltech AS, Norway (or with modified versions | ||
25 | of Qt that use the same license as Qt), and distribute linked | ||
26 | combinations including the two. You must obey the GNU General | ||
27 | Public License in all respects for all of the code used other than | ||
28 | Qt. If you modify this file, you may extend this exception to | ||
29 | your version of the file, but you are not obligated to do so. If | ||
30 | you do not wish to do so, delete this exception statement from | ||
31 | your version. | ||
32 | */ | ||
33 | |||
34 | #include "attachmentstrategy.h" | ||
35 | |||
36 | #include "nodehelper.h" | ||
37 | #include "util.h" | ||
38 | |||
39 | #include <KMime/Content> | ||
40 | |||
41 | #include <QIcon> | ||
42 | |||
43 | #include "mimetreeparser_debug.h" | ||
44 | |||
45 | using namespace MimeTreeParser; | ||
46 | |||
47 | static AttachmentStrategy::Display smartDisplay(KMime::Content *node) | ||
48 | { | ||
49 | const auto cd = node->contentDisposition(false); | ||
50 | |||
51 | if (cd && cd->disposition() == KMime::Headers::CDinline) | ||
52 | // explict "inline" disposition: | ||
53 | { | ||
54 | return AttachmentStrategy::Inline; | ||
55 | } | ||
56 | if (cd && cd->disposition() == KMime::Headers::CDattachment) | ||
57 | // explicit "attachment" disposition: | ||
58 | { | ||
59 | return AttachmentStrategy::AsIcon; | ||
60 | } | ||
61 | |||
62 | const auto ct = node->contentType(false); | ||
63 | if (ct && ct->isText() && ct->name().trimmed().isEmpty() && | ||
64 | (!cd || cd->filename().trimmed().isEmpty())) | ||
65 | // text/* w/o filename parameter: | ||
66 | { | ||
67 | return AttachmentStrategy::Inline; | ||
68 | } | ||
69 | return AttachmentStrategy::AsIcon; | ||
70 | } | ||
71 | |||
72 | // | ||
73 | // IconicAttachmentStrategy: | ||
74 | // show everything but the first text/plain body as icons | ||
75 | // | ||
76 | |||
77 | class IconicAttachmentStrategy : public AttachmentStrategy | ||
78 | { | ||
79 | friend class AttachmentStrategy; | ||
80 | protected: | ||
81 | IconicAttachmentStrategy() : AttachmentStrategy() {} | ||
82 | virtual ~IconicAttachmentStrategy() {} | ||
83 | |||
84 | public: | ||
85 | const char *name() const Q_DECL_OVERRIDE | ||
86 | { | ||
87 | return "iconic"; | ||
88 | } | ||
89 | |||
90 | bool inlineNestedMessages() const Q_DECL_OVERRIDE | ||
91 | { | ||
92 | return false; | ||
93 | } | ||
94 | Display defaultDisplay(KMime::Content *node) const Q_DECL_OVERRIDE | ||
95 | { | ||
96 | if (node->contentType()->isText() && | ||
97 | node->contentDisposition()->filename().trimmed().isEmpty() && | ||
98 | node->contentType()->name().trimmed().isEmpty()) | ||
99 | // text/* w/o filename parameter: | ||
100 | { | ||
101 | return Inline; | ||
102 | } | ||
103 | return AsIcon; | ||
104 | } | ||
105 | }; | ||
106 | |||
107 | // | ||
108 | // SmartAttachmentStrategy: | ||
109 | // in addition to Iconic, show all body parts | ||
110 | // with content-disposition == "inline" and | ||
111 | // all text parts without a filename or name parameter inline | ||
112 | // | ||
113 | |||
114 | class SmartAttachmentStrategy : public AttachmentStrategy | ||
115 | { | ||
116 | friend class AttachmentStrategy; | ||
117 | protected: | ||
118 | SmartAttachmentStrategy() : AttachmentStrategy() {} | ||
119 | virtual ~SmartAttachmentStrategy() {} | ||
120 | |||
121 | public: | ||
122 | const char *name() const Q_DECL_OVERRIDE | ||
123 | { | ||
124 | return "smart"; | ||
125 | } | ||
126 | |||
127 | bool inlineNestedMessages() const Q_DECL_OVERRIDE | ||
128 | { | ||
129 | return true; | ||
130 | } | ||
131 | Display defaultDisplay(KMime::Content *node) const Q_DECL_OVERRIDE | ||
132 | { | ||
133 | return smartDisplay(node); | ||
134 | } | ||
135 | }; | ||
136 | |||
137 | // | ||
138 | // InlinedAttachmentStrategy: | ||
139 | // show everything possible inline | ||
140 | // | ||
141 | |||
142 | class InlinedAttachmentStrategy : public AttachmentStrategy | ||
143 | { | ||
144 | friend class AttachmentStrategy; | ||
145 | protected: | ||
146 | InlinedAttachmentStrategy() : AttachmentStrategy() {} | ||
147 | virtual ~InlinedAttachmentStrategy() {} | ||
148 | |||
149 | public: | ||
150 | const char *name() const Q_DECL_OVERRIDE | ||
151 | { | ||
152 | return "inlined"; | ||
153 | } | ||
154 | |||
155 | bool inlineNestedMessages() const Q_DECL_OVERRIDE | ||
156 | { | ||
157 | return true; | ||
158 | } | ||
159 | Display defaultDisplay(KMime::Content *) const Q_DECL_OVERRIDE | ||
160 | { | ||
161 | return Inline; | ||
162 | } | ||
163 | }; | ||
164 | |||
165 | // | ||
166 | // HiddenAttachmentStrategy | ||
167 | // show nothing except the first text/plain body part _at all_ | ||
168 | // | ||
169 | |||
170 | class HiddenAttachmentStrategy : public AttachmentStrategy | ||
171 | { | ||
172 | friend class AttachmentStrategy; | ||
173 | protected: | ||
174 | HiddenAttachmentStrategy() : AttachmentStrategy() {} | ||
175 | virtual ~HiddenAttachmentStrategy() {} | ||
176 | |||
177 | public: | ||
178 | const char *name() const Q_DECL_OVERRIDE | ||
179 | { | ||
180 | return "hidden"; | ||
181 | } | ||
182 | |||
183 | bool inlineNestedMessages() const Q_DECL_OVERRIDE | ||
184 | { | ||
185 | return false; | ||
186 | } | ||
187 | Display defaultDisplay(KMime::Content *node) const Q_DECL_OVERRIDE | ||
188 | { | ||
189 | if (node->contentType()->isText() && | ||
190 | node->contentDisposition()->filename().trimmed().isEmpty() && | ||
191 | node->contentType()->name().trimmed().isEmpty()) | ||
192 | // text/* w/o filename parameter: | ||
193 | { | ||
194 | return Inline; | ||
195 | } | ||
196 | if (!node->parent()) { | ||
197 | return Inline; | ||
198 | } | ||
199 | |||
200 | if (node->parent() && node->parent()->contentType()->isMultipart() && | ||
201 | node->parent()->contentType()->subType() == "related") { | ||
202 | return Inline; | ||
203 | } | ||
204 | |||
205 | return None; | ||
206 | } | ||
207 | }; | ||
208 | |||
209 | class HeaderOnlyAttachmentStrategy : public AttachmentStrategy | ||
210 | { | ||
211 | friend class AttachmentStrategy; | ||
212 | protected: | ||
213 | HeaderOnlyAttachmentStrategy() : AttachmentStrategy() {} | ||
214 | virtual ~HeaderOnlyAttachmentStrategy() {} | ||
215 | |||
216 | public: | ||
217 | const char *name() const Q_DECL_OVERRIDE | ||
218 | { | ||
219 | return "headerOnly"; | ||
220 | } | ||
221 | |||
222 | bool inlineNestedMessages() const Q_DECL_OVERRIDE | ||
223 | { | ||
224 | return true; | ||
225 | } | ||
226 | |||
227 | Display defaultDisplay(KMime::Content *node) const Q_DECL_OVERRIDE | ||
228 | { | ||
229 | if (NodeHelper::isInEncapsulatedMessage(node)) { | ||
230 | return smartDisplay(node); | ||
231 | } | ||
232 | |||
233 | if (!Util::labelForContent(node).isEmpty() && QIcon::hasThemeIcon(Util::iconNameForContent(node)) && ! Util::isTypeBlacklisted(node)) { | ||
234 | return None; | ||
235 | } | ||
236 | return smartDisplay(node); | ||
237 | } | ||
238 | |||
239 | bool requiresAttachmentListInHeader() const Q_DECL_OVERRIDE | ||
240 | { | ||
241 | return true; | ||
242 | } | ||
243 | }; | ||
244 | |||
245 | // | ||
246 | // AttachmentStrategy abstract base: | ||
247 | // | ||
248 | |||
249 | AttachmentStrategy::AttachmentStrategy() | ||
250 | { | ||
251 | |||
252 | } | ||
253 | |||
254 | AttachmentStrategy::~AttachmentStrategy() | ||
255 | { | ||
256 | |||
257 | } | ||
258 | |||
259 | const AttachmentStrategy *AttachmentStrategy::create(Type type) | ||
260 | { | ||
261 | switch (type) { | ||
262 | case Iconic: return iconic(); | ||
263 | case Smart: return smart(); | ||
264 | case Inlined: return inlined(); | ||
265 | case Hidden: return hidden(); | ||
266 | case HeaderOnly: return headerOnly(); | ||
267 | } | ||
268 | qCCritical(MIMETREEPARSER_LOG) << "Unknown attachment startegy ( type ==" | ||
269 | << (int)type << ") requested!"; | ||
270 | return nullptr; // make compiler happy | ||
271 | } | ||
272 | |||
273 | const AttachmentStrategy *AttachmentStrategy::create(const QString &type) | ||
274 | { | ||
275 | const QString lowerType = type.toLower(); | ||
276 | if (lowerType == QLatin1String("iconic")) { | ||
277 | return iconic(); | ||
278 | } | ||
279 | //if ( lowerType == "smart" ) return smart(); // not needed, see below | ||
280 | if (lowerType == QLatin1String("inlined")) { | ||
281 | return inlined(); | ||
282 | } | ||
283 | if (lowerType == QLatin1String("hidden")) { | ||
284 | return hidden(); | ||
285 | } | ||
286 | if (lowerType == QLatin1String("headeronly")) { | ||
287 | return headerOnly(); | ||
288 | } | ||
289 | // don't kFatal here, b/c the strings are user-provided | ||
290 | // (KConfig), so fail gracefully to the default: | ||
291 | return smart(); | ||
292 | } | ||
293 | |||
294 | static const AttachmentStrategy *iconicStrategy = nullptr; | ||
295 | static const AttachmentStrategy *smartStrategy = nullptr; | ||
296 | static const AttachmentStrategy *inlinedStrategy = nullptr; | ||
297 | static const AttachmentStrategy *hiddenStrategy = nullptr; | ||
298 | static const AttachmentStrategy *headerOnlyStrategy = nullptr; | ||
299 | |||
300 | const AttachmentStrategy *AttachmentStrategy::iconic() | ||
301 | { | ||
302 | if (!iconicStrategy) { | ||
303 | iconicStrategy = new IconicAttachmentStrategy(); | ||
304 | } | ||
305 | return iconicStrategy; | ||
306 | } | ||
307 | |||
308 | const AttachmentStrategy *AttachmentStrategy::smart() | ||
309 | { | ||
310 | if (!smartStrategy) { | ||
311 | smartStrategy = new SmartAttachmentStrategy(); | ||
312 | } | ||
313 | return smartStrategy; | ||
314 | } | ||
315 | |||
316 | const AttachmentStrategy *AttachmentStrategy::inlined() | ||
317 | { | ||
318 | if (!inlinedStrategy) { | ||
319 | inlinedStrategy = new InlinedAttachmentStrategy(); | ||
320 | } | ||
321 | return inlinedStrategy; | ||
322 | } | ||
323 | |||
324 | const AttachmentStrategy *AttachmentStrategy::hidden() | ||
325 | { | ||
326 | if (!hiddenStrategy) { | ||
327 | hiddenStrategy = new HiddenAttachmentStrategy(); | ||
328 | } | ||
329 | return hiddenStrategy; | ||
330 | } | ||
331 | |||
332 | const AttachmentStrategy *AttachmentStrategy::headerOnly() | ||
333 | { | ||
334 | if (!headerOnlyStrategy) { | ||
335 | headerOnlyStrategy = new HeaderOnlyAttachmentStrategy(); | ||
336 | } | ||
337 | return headerOnlyStrategy; | ||
338 | } | ||
339 | |||
340 | bool AttachmentStrategy::requiresAttachmentListInHeader() const | ||
341 | { | ||
342 | return false; | ||
343 | } | ||