summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mime/mimetreeparser/nodehelper.h
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/domain/mime/mimetreeparser/nodehelper.h')
-rw-r--r--framework/src/domain/mime/mimetreeparser/nodehelper.h200
1 files changed, 200 insertions, 0 deletions
diff --git a/framework/src/domain/mime/mimetreeparser/nodehelper.h b/framework/src/domain/mime/mimetreeparser/nodehelper.h
new file mode 100644
index 00000000..2891f3bd
--- /dev/null
+++ b/framework/src/domain/mime/mimetreeparser/nodehelper.h
@@ -0,0 +1,200 @@
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
32class QUrl;
33class QTextCodec;
34
35namespace MimeTreeParser
36{
37class AttachmentTemporaryFilesDirs;
38namespace Interface
39{
40}
41}
42
43namespace MimeTreeParser
44{
45
46/**
47 * @author Andras Mantia <andras@kdab.net>
48 */
49class NodeHelper: public QObject
50{
51 Q_OBJECT
52public:
53 NodeHelper();
54
55 ~NodeHelper();
56
57 void setNodeProcessed(KMime::Content *node, bool recurse);
58 void setNodeUnprocessed(KMime::Content *node, bool recurse);
59 bool nodeProcessed(KMime::Content *node) const;
60 void clear();
61 void forceCleanTempFiles();
62
63 void setPartMetaData(KMime::Content *node, const PartMetaData &metaData);
64 PartMetaData partMetaData(KMime::Content *node);
65
66 /**
67 * Set the 'Content-Type' by mime-magic from the contents of the body.
68 * If autoDecode is true the decoded body will be used for mime type
69 * determination (this does not change the body itself).
70 */
71 void magicSetType(KMime::Content *node, bool autoDecode = true);
72
73 /** Attach an extra node to an existing node */
74 void attachExtraContent(KMime::Content *topLevelNode, KMime::Content *content);
75
76 /** Get the extra nodes attached to the @param topLevelNode and all sub-nodes of @param topLevelNode */
77 QList<KMime::Content *> extraContents(KMime::Content *topLevelNode) const;
78
79 /** Return a modified message (node tree) starting from @param topLevelNode that has the original nodes and the extra nodes.
80 The caller has the responsibility to delete the new message.
81 */
82 KMime::Message *messageWithExtraContent(KMime::Content *topLevelNode);
83
84 /** Get a QTextCodec suitable for this message part */
85 const QTextCodec *codec(KMime::Content *node);
86
87 /** Set the charset the user selected for the message to display */
88 void setOverrideCodec(KMime::Content *node, const QTextCodec *codec);
89
90 /**
91 * Cleanup the attachment temp files
92 */
93 void removeTempFiles();
94
95 /**
96 * Add a file to the list of managed temporary files
97 */
98 void addTempFile(const QString &file);
99
100 // Get a href in the form attachment:<nodeId>?place=<place>, used by ObjectTreeParser and
101 // UrlHandlerManager.
102 QString asHREF(const KMime::Content *node, const QString &place) const;
103 KMime::Content *fromHREF(const KMime::Message::Ptr &mMessage, const QUrl &href) const;
104
105 /**
106 * @return true if this node is a child or an encapsulated message
107 */
108 static bool isInEncapsulatedMessage(KMime::Content *node);
109
110 /**
111 * Returns the charset for the given node. If no charset is specified
112 * for the node, the defaultCharset() is returned.
113 */
114 static QByteArray charset(KMime::Content *node);
115
116 /**
117 * Return a QTextCodec for the specified charset.
118 * This function is a bit more tolerant, than QTextCodec::codecForName
119 */
120 static const QTextCodec *codecForName(const QByteArray &_str);
121
122 /**
123 * Returns a usable filename for a node, that can be the filename from the
124 * content disposition header, or if that one is empty, the name from the
125 * content type header.
126 */
127 static QString fileName(const KMime::Content *node);
128
129 /**
130 * Fixes an encoding received by a KDE function and returns the proper,
131 * MIME-compilant encoding name instead.
132 * @see encodingForName
133 */
134 static QString fixEncoding(const QString &encoding); //TODO(Andras) move to a utility class?
135
136 /**
137 * Drop-in replacement for KCharsets::encodingForName(). The problem with
138 * the KCharsets function is that it returns "human-readable" encoding names
139 * like "ISO 8859-15" instead of valid encoding names like "ISO-8859-15".
140 * This function fixes this by replacing whitespace with a hyphen.
141 */
142 static QString encodingForName(const QString &descriptiveName); //TODO(Andras) move to a utility class?
143
144 /**
145 * Return a list of the supported encodings
146 * @param usAscii if true, US-Ascii encoding will be prepended to the list.
147 */
148 static QStringList supportedEncodings(bool usAscii); //TODO(Andras) move to a utility class?
149
150 QString fromAsString(KMime::Content *node) const;
151
152 /**
153 * Returns a list of attachments of attached extra content nodes.
154 * This is mainly useful is order to get attachments of encrypted messages.
155 * Note that this does not include attachments from the primary node tree.
156 * @see KMime::Content::attachments().
157 */
158 QVector<KMime::Content *> attachmentsOfExtraContents() const;
159
160Q_SIGNALS:
161 void update(MimeTreeParser::UpdateMode);
162
163private:
164 Q_DISABLE_COPY(NodeHelper)
165 bool unencryptedMessage_helper(KMime::Content *node, QByteArray &resultingData, bool addHeaders,
166 int recursionLevel = 1);
167
168 void mergeExtraNodes(KMime::Content *node);
169 void cleanFromExtraNodes(KMime::Content *node);
170
171 /** Creates a persistent index string that bridges the gap between the
172 permanent nodes and the temporary ones.
173
174 Used internally for robust indexing.
175 **/
176 QString persistentIndex(const KMime::Content *node) const;
177
178 /** Translates the persistentIndex into a node back
179
180 node: any node of the actually message to what the persistentIndex is interpreded
181 **/
182 KMime::Content *contentFromIndex(KMime::Content *node, const QString &persistentIndex) const;
183
184private:
185 QList<KMime::Content *> mProcessedNodes;
186 QList<KMime::Content *> mNodesUnderProcess;
187 QSet<KMime::Content *> mDisplayEmbeddedNodes;
188 QSet<KMime::Content *> mDisplayHiddenNodes;
189 QTextCodec *mLocalCodec;
190 QMap<KMime::Content *, const QTextCodec *> mOverrideCodecs;
191 QMap<KMime::Content *, PartMetaData> mPartMetaDatas;
192 QMap<KMime::Message::Content *, QList<KMime::Content *> > mExtraContents;
193 AttachmentTemporaryFilesDirs *mAttachmentFilesDir;
194
195 friend class NodeHelperTest;
196};
197
198}
199
200#endif