Debian Codegraph @

Dekko/backend/network/MsgPartReply.h

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 #ifndef MSGPARTREPLY_H
19 #define MSGPARTREPLY_H
20
21 #include <QObject>
22 #include <QBuffer>
23 #include <QNetworkReply>
24 #include <qmailstore.h>
25 #include <qmailmessage.h>
26 #include <MailServiceClient.h>
27 #include "MsgPartQNAM.h"
28
29 struct PartFinder {
30 QString location;
31 const QMailMessagePart *part;
32 PartFinder() {}
33 PartFinder(const QString &location) : location(location) {}
34
35 bool operator()(const QMailMessagePart &p) {
36 const bool isPart = (p.location().toString(true) == location);
37 if (isPart) {
38 part = const_cast<QMailMessagePart *>(&p);
39 }
40 return !isPart; // Has to be false to exit the foreachPart
41 }
42 };
43
44 struct CidPartDetector {
45 QString cid;
46 QString location;
47
48 CidPartDetector(const QString &cid) : cid(cid) {}
49
50 bool operator()(const QMailMessagePart &part) {
51 const bool isPart = (part.contentID() == QUrl(cid).path());
52 if (isPart) {
53 location = part.location().toString(true);
54 }
55 return !isPart; // Has to be false to exit the foreachPart
56 }
57 };
58
59 class MsgPartReply : public QNetworkReply
60 {
61 Q_OBJECT
62 public:
63 MsgPartReply(MsgPartQNAM *parent, const QMailMessageId &id, const QString &cidUrl);
64 MsgPartReply(MsgPartQNAM *parent, const QMailMessageId &id, const QString &location,
65 const bool plaintext, const bool requiresFormatting);
66 ~MsgPartReply();
67
68 // QIODevice interface
69 public:
70 virtual void close();
71 virtual qint64 bytesAvailable() const;
72
73 protected:
74 virtual qint64 readData(char *data, qint64 maxlen);
75 void failed(const QString &reason);
76 const QMailMessagePart *partPtr( const bool refresh = false);
77
78 // QNetworkReply interface
79 public slots:
80 virtual void abort() { close(); }
81
82 void handleMsgPartAvailable(const quint64 &id, const QString &location);
83 void handlePartFetchFailed(const quint64 &id, const QString &location);
84
85 void messageReady();
86 void init();
87 protected:
88 bool cidToPart(const QMailMessage &message, const QString &cidUrl);
89 private:
90 // Container to hold our msgpart pointer.
91 class Part {
92 public:
93 Part(const QMailMessageId &id, const QString &location) : m_finder(0), m_msg(id) {
94 m_finder = PartFinder(location);
95 m_msg.foreachPart<PartFinder &>(m_finder);
96 }
97 const QMailMessage parentMessage() {
98 return m_msg;
99 }
100
101 const QMailMessagePart *partPtr() {
102 return m_finder.part;
103 }
104 private:
105 PartFinder m_finder;
106 QMailMessage m_msg;
107 };
108
109 QByteArray *formattedBufferContent;
110 QByteArray *secondBuffer;
111 mutable QBuffer buffer;
112 QMailMessageId m_msgId;
113 QString m_location;
114 bool m_ptext;
115 bool m_format;
116 Part *m_part;
117 };
118
119 #endif // MSGPARTREPLY_H