Changeset View
Changeset View
Standalone View
Standalone View
framework/domain/mimetreeparser/stringhtmlwriter.cpp
- This file was added.
1 | /* -*- c++ -*- | ||||
---|---|---|---|---|---|
2 | filehtmlwriter.cpp | ||||
3 | | ||||
4 | This file is part of KMail, the KDE mail client. | ||||
5 | Copyright (c) 2003 Marc Mutz <mutz@kde.org> | ||||
6 | | ||||
7 | KMail is free software; you can redistribute it and/or modify it | ||||
8 | under the terms of the GNU General Public License, version 2, as | ||||
9 | published by the Free Software Foundation. | ||||
10 | | ||||
11 | KMail is distributed in the hope that it will be useful, but | ||||
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
14 | General Public License for more details. | ||||
15 | | ||||
16 | You should have received a copy of the GNU General Public License | ||||
17 | along with this program; if not, write to the Free Software | ||||
18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
19 | | ||||
20 | In addition, as a special exception, the copyright holders give | ||||
21 | permission to link the code of this program with any edition of | ||||
22 | the Qt library by Trolltech AS, Norway (or with modified versions | ||||
23 | of Qt that use the same license as Qt), and distribute linked | ||||
24 | combinations including the two. You must obey the GNU General | ||||
25 | Public License in all respects for all of the code used other than | ||||
26 | Qt. If you modify this file, you may extend this exception to | ||||
27 | your version of the file, but you are not obligated to do so. If | ||||
28 | you do not wish to do so, delete this exception statement from | ||||
29 | your version. | ||||
30 | */ | ||||
31 | | ||||
32 | #include "stringhtmlwriter.h" | ||||
33 | | ||||
34 | #include <QDebug> | ||||
35 | #include <QTextStream> | ||||
36 | #include <QUrl> | ||||
37 | | ||||
38 | StringHtmlWriter::StringHtmlWriter() | ||||
39 | : MimeTreeParser::HtmlWriter() | ||||
40 | , mState(Ended) | ||||
41 | { | ||||
42 | } | ||||
43 | | ||||
44 | StringHtmlWriter::~StringHtmlWriter() | ||||
45 | { | ||||
46 | } | ||||
47 | | ||||
48 | void StringHtmlWriter::begin(const QString &css) | ||||
49 | { | ||||
50 | if (mState != Ended) { | ||||
51 | qWarning() << "begin() called on non-ended session!"; | ||||
52 | reset(); | ||||
53 | } | ||||
54 | | ||||
55 | mState = Begun; | ||||
56 | mExtraHead.clear(); | ||||
57 | mHtml.clear(); | ||||
58 | | ||||
59 | if (!css.isEmpty()) { | ||||
60 | write(QLatin1String("<!-- CSS Definitions \n") + css + QLatin1String("-->\n")); | ||||
61 | } | ||||
62 | } | ||||
63 | | ||||
64 | void StringHtmlWriter::end() | ||||
65 | { | ||||
66 | if (mState != Begun) { | ||||
67 | qWarning() << "Called on non-begun or queued session!"; | ||||
68 | } | ||||
69 | | ||||
70 | if (!mExtraHead.isEmpty()) { | ||||
71 | insertExtraHead(); | ||||
72 | mExtraHead.clear(); | ||||
73 | } | ||||
74 | resolveCidUrls(); | ||||
75 | mState = Ended; | ||||
76 | } | ||||
77 | | ||||
78 | void StringHtmlWriter::reset() | ||||
79 | { | ||||
80 | if (mState != Ended) { | ||||
81 | mHtml.clear(); | ||||
82 | mExtraHead.clear(); | ||||
83 | mState = Begun; // don't run into end()'s warning | ||||
84 | end(); | ||||
85 | mState = Ended; | ||||
86 | } | ||||
87 | } | ||||
88 | | ||||
89 | void StringHtmlWriter::write(const QString &str) | ||||
90 | { | ||||
91 | if (mState != Begun) { | ||||
92 | qWarning() << "Called in Ended or Queued state!"; | ||||
93 | } | ||||
94 | mHtml.append(str); | ||||
95 | } | ||||
96 | | ||||
97 | void StringHtmlWriter::queue(const QString &str) | ||||
98 | { | ||||
99 | write(str); | ||||
100 | } | ||||
101 | | ||||
102 | void StringHtmlWriter::flush() | ||||
103 | { | ||||
104 | mState = Begun; // don't run into end()'s warning | ||||
105 | end(); | ||||
106 | } | ||||
107 | | ||||
108 | void StringHtmlWriter::embedPart(const QByteArray &contentId, const QString &url) | ||||
109 | { | ||||
110 | write("<!-- embedPart(contentID=" + contentId + ", url=" + url + ") -->\n"); | ||||
111 | mEmbeddedPartMap.insert(contentId, url); | ||||
112 | } | ||||
113 | | ||||
114 | void StringHtmlWriter::resolveCidUrls() | ||||
115 | { | ||||
116 | for (const auto &cid : mEmbeddedPartMap.keys()) { | ||||
117 | mHtml.replace(QString("src=\"cid:%1\"").arg(QString(cid)), QString("src=\"%1\"").arg(mEmbeddedPartMap.value(cid).toString())); | ||||
118 | } | ||||
119 | } | ||||
120 | | ||||
121 | void StringHtmlWriter::extraHead(const QString &extraHead) | ||||
122 | { | ||||
123 | if (mState != Ended) { | ||||
124 | qWarning() << "Called on non-started session!"; | ||||
125 | } | ||||
126 | mExtraHead.append(extraHead); | ||||
127 | } | ||||
128 | | ||||
129 | | ||||
130 | void StringHtmlWriter::insertExtraHead() | ||||
131 | { | ||||
132 | const QString headTag(QStringLiteral("<head>")); | ||||
133 | const int index = mHtml.indexOf(headTag); | ||||
134 | if (index != -1) { | ||||
135 | mHtml.insert(index + headTag.length(), mExtraHead); | ||||
136 | } | ||||
137 | } | ||||
138 | | ||||
139 | QMap<QByteArray, QUrl> StringHtmlWriter::embeddedParts() const | ||||
140 | { | ||||
141 | return mEmbeddedPartMap; | ||||
142 | } | ||||
143 | | ||||
144 | QString StringHtmlWriter::html() const | ||||
145 | { | ||||
146 | if (mState != Ended) { | ||||
147 | qWarning() << "Called on non-ended session!"; | ||||
148 | } | ||||
149 | return mHtml; | ||||
150 | } |