diff options
Diffstat (limited to 'framework/src/domain/mimetreeparser/otp/nodehelper.h')
-rw-r--r-- | framework/src/domain/mimetreeparser/otp/nodehelper.h | 290 |
1 files changed, 290 insertions, 0 deletions
diff --git a/framework/src/domain/mimetreeparser/otp/nodehelper.h b/framework/src/domain/mimetreeparser/otp/nodehelper.h new file mode 100644 index 00000000..70253f21 --- /dev/null +++ b/framework/src/domain/mimetreeparser/otp/nodehelper.h | |||
@@ -0,0 +1,290 @@ | |||
1 | /* | ||
2 | Copyright (C) 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net | ||
3 | Copyright (c) 2009 Andras Mantia <andras@kdab.net> | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License along | ||
16 | with this program; if not, write to the Free Software Foundation, Inc., | ||
17 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #ifndef __MIMETREEPARSER_NODEHELPER_H__ | ||
21 | #define __MIMETREEPARSER_NODEHELPER_H__ | ||
22 | |||
23 | #include "partmetadata.h" | ||
24 | #include "enums.h" | ||
25 | |||
26 | #include <KMime/Message> | ||
27 | |||
28 | #include <QList> | ||
29 | #include <QMap> | ||
30 | #include <QSet> | ||
31 | |||
32 | class QUrl; | ||
33 | class QTextCodec; | ||
34 | |||
35 | namespace MimeTreeParser | ||
36 | { | ||
37 | class AttachmentTemporaryFilesDirs; | ||
38 | namespace Interface | ||
39 | { | ||
40 | class BodyPartMemento; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | namespace MimeTreeParser | ||
45 | { | ||
46 | |||
47 | /** | ||
48 | * @author Andras Mantia <andras@kdab.net> | ||
49 | */ | ||
50 | class NodeHelper: public QObject | ||
51 | { | ||
52 | Q_OBJECT | ||
53 | public: | ||
54 | NodeHelper(); | ||
55 | |||
56 | ~NodeHelper(); | ||
57 | |||
58 | void setNodeProcessed(KMime::Content *node, bool recurse); | ||
59 | void setNodeUnprocessed(KMime::Content *node, bool recurse); | ||
60 | bool nodeProcessed(KMime::Content *node) const; | ||
61 | void clear(); | ||
62 | void forceCleanTempFiles(); | ||
63 | |||
64 | void setEncryptionState(const KMime::Content *node, const KMMsgEncryptionState state); | ||
65 | KMMsgEncryptionState encryptionState(const KMime::Content *node) const; | ||
66 | |||
67 | void setSignatureState(const KMime::Content *node, const KMMsgSignatureState state); | ||
68 | KMMsgSignatureState signatureState(const KMime::Content *node) const; | ||
69 | |||
70 | KMMsgSignatureState overallSignatureState(KMime::Content *node) const; | ||
71 | KMMsgEncryptionState overallEncryptionState(KMime::Content *node) const; | ||
72 | |||
73 | void setPartMetaData(KMime::Content *node, const PartMetaData &metaData); | ||
74 | PartMetaData partMetaData(KMime::Content *node); | ||
75 | |||
76 | /** | ||
77 | * Set the 'Content-Type' by mime-magic from the contents of the body. | ||
78 | * If autoDecode is true the decoded body will be used for mime type | ||
79 | * determination (this does not change the body itself). | ||
80 | */ | ||
81 | void magicSetType(KMime::Content *node, bool autoDecode = true); | ||
82 | |||
83 | /** | ||
84 | * Return this mails subject, with all "forward" and "reply" | ||
85 | * prefixes removed | ||
86 | */ | ||
87 | static QString cleanSubject(KMime::Message *message); | ||
88 | |||
89 | /** Attach an extra node to an existing node */ | ||
90 | void attachExtraContent(KMime::Content *topLevelNode, KMime::Content *content); | ||
91 | |||
92 | /** Get the extra nodes attached to the @param topLevelNode and all sub-nodes of @param topLevelNode */ | ||
93 | QList<KMime::Content *> extraContents(KMime::Content *topLevelNode) const; | ||
94 | |||
95 | /** Return a modified message (node tree) starting from @param topLevelNode that has the original nodes and the extra nodes. | ||
96 | The caller has the responsibility to delete the new message. | ||
97 | */ | ||
98 | KMime::Message *messageWithExtraContent(KMime::Content *topLevelNode); | ||
99 | |||
100 | /** Get a QTextCodec suitable for this message part */ | ||
101 | const QTextCodec *codec(KMime::Content *node); | ||
102 | |||
103 | /** Set the charset the user selected for the message to display */ | ||
104 | void setOverrideCodec(KMime::Content *node, const QTextCodec *codec); | ||
105 | |||
106 | Interface::BodyPartMemento *bodyPartMemento(KMime::Content *node, const QByteArray &which) const; | ||
107 | |||
108 | void setBodyPartMemento(KMime::Content *node, const QByteArray &which, | ||
109 | Interface::BodyPartMemento *memento); | ||
110 | |||
111 | // A flag to remember if the node was embedded. This is useful for attachment nodes, the reader | ||
112 | // needs to know if they were displayed inline or not. | ||
113 | bool isNodeDisplayedEmbedded(KMime::Content *node) const; | ||
114 | void setNodeDisplayedEmbedded(KMime::Content *node, bool displayedEmbedded); | ||
115 | |||
116 | // Same as above, but this time determines if the node was hidden or not | ||
117 | bool isNodeDisplayedHidden(KMime::Content *node) const; | ||
118 | void setNodeDisplayedHidden(KMime::Content *node, bool displayedHidden); | ||
119 | |||
120 | /** | ||
121 | * Writes the given message part to a temporary file and returns the | ||
122 | * name of this file or QString() if writing failed. | ||
123 | */ | ||
124 | QString writeNodeToTempFile(KMime::Content *node); | ||
125 | |||
126 | /** | ||
127 | * Returns the temporary file path and name where this node was saved, or an empty url | ||
128 | * if it wasn't saved yet with writeNodeToTempFile() | ||
129 | */ | ||
130 | QUrl tempFileUrlFromNode(const KMime::Content *node); | ||
131 | |||
132 | /** | ||
133 | * Creates a temporary dir for saving attachments, etc. | ||
134 | * Will be automatically deleted when another message is viewed. | ||
135 | * @param param Optional part of the directory name. | ||
136 | */ | ||
137 | QString createTempDir(const QString ¶m = QString()); | ||
138 | |||
139 | /** | ||
140 | * Cleanup the attachment temp files | ||
141 | */ | ||
142 | void removeTempFiles(); | ||
143 | |||
144 | /** | ||
145 | * Add a file to the list of managed temporary files | ||
146 | */ | ||
147 | void addTempFile(const QString &file); | ||
148 | |||
149 | // Get a href in the form attachment:<nodeId>?place=<place>, used by ObjectTreeParser and | ||
150 | // UrlHandlerManager. | ||
151 | QString asHREF(const KMime::Content *node, const QString &place) const; | ||
152 | KMime::Content *fromHREF(const KMime::Message::Ptr &mMessage, const QUrl &href) const; | ||
153 | |||
154 | /** | ||
155 | * @return true if this node is a child or an encapsulated message | ||
156 | */ | ||
157 | static bool isInEncapsulatedMessage(KMime::Content *node); | ||
158 | |||
159 | /** | ||
160 | * Returns the charset for the given node. If no charset is specified | ||
161 | * for the node, the defaultCharset() is returned. | ||
162 | */ | ||
163 | static QByteArray charset(KMime::Content *node); | ||
164 | |||
165 | /** | ||
166 | * Check for prefixes @p prefixRegExps in @p str. If none | ||
167 | * is found, @p newPrefix + ' ' is prepended to @p str and the | ||
168 | * resulting string is returned. If @p replace is true, any | ||
169 | * sequence of whitespace-delimited prefixes at the beginning of | ||
170 | * @p str is replaced by @p newPrefix. | ||
171 | */ | ||
172 | static QString replacePrefixes(const QString &str, | ||
173 | const QStringList &prefixRegExps, | ||
174 | bool replace, | ||
175 | const QString &newPrefix); | ||
176 | |||
177 | /** | ||
178 | * Return a QTextCodec for the specified charset. | ||
179 | * This function is a bit more tolerant, than QTextCodec::codecForName | ||
180 | */ | ||
181 | static const QTextCodec *codecForName(const QByteArray &_str); | ||
182 | |||
183 | /** | ||
184 | * Returns a usable filename for a node, that can be the filename from the | ||
185 | * content disposition header, or if that one is empty, the name from the | ||
186 | * content type header. | ||
187 | */ | ||
188 | static QString fileName(const KMime::Content *node); | ||
189 | |||
190 | /** | ||
191 | * Fixes an encoding received by a KDE function and returns the proper, | ||
192 | * MIME-compilant encoding name instead. | ||
193 | * @see encodingForName | ||
194 | */ | ||
195 | static QString fixEncoding(const QString &encoding); //TODO(Andras) move to a utility class? | ||
196 | |||
197 | /** | ||
198 | * Drop-in replacement for KCharsets::encodingForName(). The problem with | ||
199 | * the KCharsets function is that it returns "human-readable" encoding names | ||
200 | * like "ISO 8859-15" instead of valid encoding names like "ISO-8859-15". | ||
201 | * This function fixes this by replacing whitespace with a hyphen. | ||
202 | */ | ||
203 | static QString encodingForName(const QString &descriptiveName); //TODO(Andras) move to a utility class? | ||
204 | |||
205 | /** | ||
206 | * Return a list of the supported encodings | ||
207 | * @param usAscii if true, US-Ascii encoding will be prepended to the list. | ||
208 | */ | ||
209 | static QStringList supportedEncodings(bool usAscii); //TODO(Andras) move to a utility class? | ||
210 | |||
211 | QString fromAsString(KMime::Content *node) const; | ||
212 | |||
213 | KMime::Content *decryptedNodeForContent(KMime::Content *content) const; | ||
214 | |||
215 | /** | ||
216 | * This function returns the unencrypted message that is based on @p originalMessage. | ||
217 | * All encrypted MIME parts are removed and replaced by their decrypted plain-text versions. | ||
218 | * Encrypted parts that are within signed parts are not replaced, since that would invalidate | ||
219 | * the signature. | ||
220 | * | ||
221 | * This only works if the message was run through ObjectTreeParser::parseObjectTree() with the | ||
222 | * currrent NodeHelper before, because parseObjectTree() actually decrypts the message and stores | ||
223 | * the decrypted nodes by calling attachExtraContent(). | ||
224 | * | ||
225 | * @return the unencrypted message or an invalid pointer if the original message didn't contain | ||
226 | * a part that needed to be modified. | ||
227 | */ | ||
228 | KMime::Message::Ptr unencryptedMessage(const KMime::Message::Ptr &originalMessage); | ||
229 | |||
230 | /** | ||
231 | * Returns a list of attachments of attached extra content nodes. | ||
232 | * This is mainly useful is order to get attachments of encrypted messages. | ||
233 | * Note that this does not include attachments from the primary node tree. | ||
234 | * @see KMime::Content::attachments(). | ||
235 | */ | ||
236 | QVector<KMime::Content *> attachmentsOfExtraContents() const; | ||
237 | |||
238 | Q_SIGNALS: | ||
239 | void update(MimeTreeParser::UpdateMode); | ||
240 | |||
241 | private: | ||
242 | Q_DISABLE_COPY(NodeHelper) | ||
243 | bool unencryptedMessage_helper(KMime::Content *node, QByteArray &resultingData, bool addHeaders, | ||
244 | int recursionLevel = 1); | ||
245 | |||
246 | /** Check for prefixes @p prefixRegExps in #subject(). If none | ||
247 | is found, @p newPrefix + ' ' is prepended to the subject and the | ||
248 | resulting string is returned. If @p replace is true, any | ||
249 | sequence of whitespace-delimited prefixes at the beginning of | ||
250 | #subject() is replaced by @p newPrefix | ||
251 | **/ | ||
252 | static QString cleanSubject(KMime::Message *message, const QStringList &prefixRegExps, | ||
253 | bool replace, const QString &newPrefix); | ||
254 | |||
255 | void mergeExtraNodes(KMime::Content *node); | ||
256 | void cleanFromExtraNodes(KMime::Content *node); | ||
257 | |||
258 | /** Creates a persistent index string that bridges the gap between the | ||
259 | permanent nodes and the temporary ones. | ||
260 | |||
261 | Used internally for robust indexing. | ||
262 | **/ | ||
263 | QString persistentIndex(const KMime::Content *node) const; | ||
264 | |||
265 | /** Translates the persistentIndex into a node back | ||
266 | |||
267 | node: any node of the actually message to what the persistentIndex is interpreded | ||
268 | **/ | ||
269 | KMime::Content *contentFromIndex(KMime::Content *node, const QString &persistentIndex) const; | ||
270 | |||
271 | private: | ||
272 | QList<KMime::Content *> mProcessedNodes; | ||
273 | QList<KMime::Content *> mNodesUnderProcess; | ||
274 | QMap<const KMime::Content *, KMMsgEncryptionState> mEncryptionState; | ||
275 | QMap<const KMime::Content *, KMMsgSignatureState> mSignatureState; | ||
276 | QSet<KMime::Content *> mDisplayEmbeddedNodes; | ||
277 | QSet<KMime::Content *> mDisplayHiddenNodes; | ||
278 | QTextCodec *mLocalCodec; | ||
279 | QMap<KMime::Content *, const QTextCodec *> mOverrideCodecs; | ||
280 | QMap<QString, QMap<QByteArray, Interface::BodyPartMemento *> > mBodyPartMementoMap; | ||
281 | QMap<KMime::Content *, PartMetaData> mPartMetaDatas; | ||
282 | QMap<KMime::Message::Content *, QList<KMime::Content *> > mExtraContents; | ||
283 | AttachmentTemporaryFilesDirs *mAttachmentFilesDir; | ||
284 | |||
285 | friend class NodeHelperTest; | ||
286 | }; | ||
287 | |||
288 | } | ||
289 | |||
290 | #endif | ||