Debian Codegraph @

Dekko/backend/network/MsgPartReply.cpp

raw symbols SCIP
1 /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3
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
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "MsgPartReply.h"
19 #include <Formatting.h>
20 #include <QTimer>
21
22 MsgPartReply::MsgPartReply(MsgPartQNAM *parent, const QMailMessageId &id, const QString &cidUrl):
23 QNetworkReply(parent), formattedBufferContent(0), m_msgId(id), m_ptext(false), m_format(false),
24 m_part(0)
25 {
26 if (!cidToPart(QMailMessage(m_msgId), cidUrl)) {
27 failed(QStringLiteral("Failed to find cid part"));
28 return;
29 }
30 m_part = new Part(m_msgId, m_location);
31 if (m_part->partPtr()->contentID().isEmpty()) {
32 qDebug() << "MISSING CONTENT ID";
33 failed(QStringLiteral("Missing content id"));
34 return;
35 }
36 init();
37 buffer.setBuffer(formattedBufferContent);
38 buffer.open(QIODevice::ReadOnly);
39 }
40
41 MsgPartReply::MsgPartReply(MsgPartQNAM *parent, const QMailMessageId &id,
42 const QString &location, const bool plaintext,
43 const bool requiresFormatting) :
44 QNetworkReply(parent), formattedBufferContent(0), secondBuffer(Q_NULLPTR),
45 m_msgId(id), m_location(location), m_ptext(plaintext), m_format(requiresFormatting),
46 m_part(0)
47 {
48 m_part = new Part(m_msgId, m_location);
49 init();
50 buffer.setBuffer(formattedBufferContent);
51 buffer.open(QIODevice::ReadOnly);
52 }
53
54 MsgPartReply::~MsgPartReply() {
55 qDebug() << "Deleting MsgPartReply";
56 }
57
58 void MsgPartReply::init()
59 {
60 QUrl url;
61 url.setScheme(QStringLiteral("dekko-part"));
62 url.setHost("msg");
63 setUrl(url);
64
65 setOpenMode(QIODevice::ReadOnly | QIODevice::Unbuffered);
66 if (auto part = m_part->partPtr()) {
67 qDebug() << part->location().toString(true);
68 if (part->contentAvailable()) {
69 qDebug() << "[MsgPartReply]" << "Part available";
70 messageReady();
71 } else {
72 qDebug() << "[MsgPartReply]" << "Part not available requesting now";
73 connect(Client::instance(), SIGNAL(messagePartNowAvailable(quint64,QString)),
74 this, SLOT(handleMsgPartAvailable(quint64,QString)));
75 connect(Client::instance(), SIGNAL(messagePartFetchFailed(quint64,QString)),
76 this, SLOT(handlePartFetchFailed(quint64,QString)));
77 // Request the download now
78 // If it's already fetched then "Client" will emit the messagePartNowAvailable immediately
79 Client::instance()->downloadMessagePart(m_part->partPtr());
80 // just fudge download progress for now
81 emit downloadProgress(1000, 2000);
82 }
83 }
84 }
85
86 bool MsgPartReply::cidToPart(const QMailMessage &message, const QString &cidUrl) {
87
88 CidPartDetector d(cidUrl);
89 if (!message.foreachPart<CidPartDetector &>(d)) {
90 m_location = d.location;
91 return true;
92 } else {
93 return false;
94 }
95 }
96
97 void MsgPartReply::close()
98 {
99 if (formattedBufferContent) {
100 delete formattedBufferContent;
101 formattedBufferContent = 0;
102 }
103 buffer.close();
104 buffer.setBuffer(0);
105 }
106
107 qint64 MsgPartReply::bytesAvailable() const
108 {
109 qDebug() << "[MsgPartReply]" << __func__ << buffer.bytesAvailable();
110 return buffer.bytesAvailable() + QNetworkReply::bytesAvailable();
111 }
112
113 qint64 MsgPartReply::readData(char *data, qint64 maxlen)
114 {
115 qDebug() << "[MsgPartReply]" << __func__ << "Reading data. MaxLen: " << maxlen;
116 return buffer.read(data, maxlen);
117 }
118
119 void MsgPartReply::failed(const QString &reason)
120 {
121 setError(TimeoutError, reason);
122 setFinished(true);
123 emit error(TimeoutError);
124 emit finished();
125 return;
126 }
127
128 void MsgPartReply::handleMsgPartAvailable(const quint64 &id, const QString &location)
129 {
130 QMailMessageId receivedId(id);
131 if (receivedId.toULongLong() != m_part->partPtr()->location().containingMessageId().toULongLong()
132 || location != m_part->partPtr()->location().toString(true)) {
133 qDebug() << "[MsgPartReply]" << __func__ << "Part not available... waiting";
134 return;
135 }
136 qDebug() << "[MsgPartReply]" << __func__ << "Part now available...";
137 // refresh the pointer;
138 delete m_part;
139 m_part = 0;
140 m_part = new Part(m_msgId, m_location);
141 messageReady();
142 }
143
144 void MsgPartReply::handlePartFetchFailed(const quint64 &id, const QString &location)
145 {
146 QMailMessageId receivedId(id);
147 if (receivedId.toULongLong() != m_part->partPtr()->location().containingMessageId().toULongLong()
148 || location != m_part->partPtr()->location().toString(true)) {
149 qDebug() << "[MsgPartReply]" << __func__ << "Part not available... waiting";
150 return;
151 }
152 failed(QStringLiteral("Failed fetching part: %1").arg(location));
153 }
154
155 void MsgPartReply::messageReady()
156 {
157 // Woohoo
158 emit downloadProgress(2000, 2000);
159 const QMailMessagePart *part = m_part->partPtr();
160 QByteArray data;
161 QMailMessageContentType ct = part->contentType();
162 if (ct.type().toLower() == "text") {
163 data = part->body().data().toUtf8();
164 } else if (ct.type().toLower() == "image") {
165 data = part->body().data(QMailMessageBody::Decoded).trimmed();
166 }
167
168 if (m_format) {
169 data = Formatting::markupPlainTextToHtml(data).toUtf8();
170 setHeader(QNetworkRequest::ContentTypeHeader, QString("text/html")); // can't set charset=utf-8 :-(
171 } else {
172 // if (!ct.charset().isEmpty()) {
173 // setHeader(QNetworkRequest::ContentTypeHeader, QString("%1; charset=%2").arg(QString(ct.content()), QString(ct.charset())));
174 // } else {
175 // setHeader(QNetworkRequest::ContentTypeHeader, QString("%1").arg(QString(ct.content())));
176 // }
177 setHeader(QNetworkRequest::ContentTypeHeader, ct.content());
178 }
179 formattedBufferContent = new QByteArray(data);
180 data.replace(QByteArray("charset="), QByteArray("char&zwnj;set="));
181 buffer.close();
182 secondBuffer = new QByteArray(data);
183 buffer.setBuffer(secondBuffer);
184 buffer.open(QIODevice::ReadOnly);
185 setFinished(true);
186 QTimer::singleShot( 0, this, SIGNAL(readyRead()) );
187 QTimer::singleShot( 0, this, SIGNAL(finished()) );
188 }