Debian Codegraph @

Dekko/backend/network/MsgReply.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 "MsgReply.h"
19 #include <QTimer>
20 #include <Formatting.h>
21
22 MsgReply::MsgReply(MsgPartQNAM *parent, QMailMessageId &msgId):
23 QNetworkReply(parent), formattedBufferContent(0), m_id(msgId), m_msg(msgId), m_format(false)
24 {
25 init();
26 buffer.setBuffer(formattedBufferContent);
27 buffer.open(QIODevice::ReadOnly);
28 }
29
30 MsgReply::MsgReply(MsgPartQNAM *parent, QMailMessageId &msgId, const bool requiresFormatting) :
31 QNetworkReply(parent), formattedBufferContent(0), m_id(msgId), m_msg(msgId), m_format(requiresFormatting)
32 {
33 init();
34 buffer.setBuffer(formattedBufferContent);
35 buffer.open(QBuffer::ReadOnly);
36 }
37
38 MsgReply::~MsgReply()
39 {
40 qDebug() << "Deleting MsgReply";
41 }
42
43 void MsgReply::close()
44 {
45 if (formattedBufferContent) {
46 delete formattedBufferContent;
47 formattedBufferContent = 0;
48 }
49 buffer.close();
50 buffer.setBuffer(0);
51 }
52
53 qint64 MsgReply::bytesAvailable() const
54 {
55 return buffer.bytesAvailable() + QNetworkReply::bytesAvailable();
56 }
57
58 qint64 MsgReply::readData(char *data, qint64 maxlen)
59 {
60 return buffer.read(data, maxlen);
61 }
62
63 void MsgReply::failed(const QString &reason)
64 {
65 setError(TimeoutError, reason);
66 setFinished(true);
67 emit error(TimeoutError);
68 emit finished();
69 return;
70 }
71
72 void MsgReply::handleMsgsAvailable(const QMailMessageIdList &list)
73 {
74 if (!list.contains(m_id)) {
75 return;
76 }
77 messageReady();
78 }
79
80 void MsgReply::handleFetchFailed(const QMailMessageIdList &list)
81 {
82 if (!list.contains(m_id)) {
83 return;
84 }
85 failed(QStringLiteral("Failed downloading message"));
86 }
87
88 void MsgReply::messageReady()
89 {
90 // Woohoo
91 emit downloadProgress(2000, 2000);
92 QByteArray data = m_msg.body().data().toUtf8();
93 data.replace(QByteArray("charset="), QByteArray("char&zwnj;set="));
94 QMailMessageContentType ct = m_msg.body().contentType();
95 if (m_format) {
96 //do formatting
97 data = Formatting::markupPlainTextToHtml(m_msg.body().data()).toUtf8();
98 setHeader(QNetworkRequest::ContentTypeHeader, QString("text/html")); // Can't add charset :-(
99 } else {
100 // if (!ct.charset().isEmpty()) {
101 // setHeader(QNetworkRequest::ContentTypeHeader, QString("%1; charset=%2").arg(QString(ct.content()), QString(ct.charset())));
102 // } else {
103 // setHeader(QNetworkRequest::ContentTypeHeader, QString("%1").arg(QString(ct.content())));
104 // }
105 setHeader(QNetworkRequest::ContentTypeHeader, ct.content());
106 }
107 formattedBufferContent = new QByteArray(data.data());
108 setFinished(true);
109 QTimer::singleShot( 0, this, SIGNAL(readyRead()) );
110 QTimer::singleShot( 0, this, SIGNAL(finished()) );
111 }
112
113 void MsgReply::init()
114 {
115 QUrl url;
116 url.setScheme(QStringLiteral("dekko-msg"));
117 url.setHost("msg");
118 setUrl(url);
119
120 setOpenMode(QIODevice::ReadOnly | QIODevice::Unbuffered);
121
122 if (!m_msg.body().data().isEmpty()) {
123 qDebug() << "[MsgReply]" << "Part available";
124 messageReady();
125 } else {
126 qDebug() << "[MsgReply]" << "Part not available requesting now";
127 connect(Client::instance(), SIGNAL(messagesNowAvailable(QMailMessageIdList)),
128 this, SLOT(handleMsgsAvailable(QMailMessageIdList)));
129 connect(Client::instance(), SIGNAL(messageFetchFailed(QMailMessageIdList)),
130 this, SLOT(handleFetchFailed(QMailMessageIdList)));
131 // Request the download now
132 // If it's already fetched then "Client" will emit the messagePartNowAvailable immediately
133 Client::instance()->downloadMessage(m_id);
134 emit downloadProgress(1000, 2000);
135 }
136
137 }
138
139
140