Debian Codegraph @

Dekko/backend/mail/Message.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 "Message.h"
19 #include <qmailfolder.h>
20 #include <qmailmessagekey.h>
21 #include <qmailstore.h>
22 #include <Account.h>
23
24 MinimalMessage::MinimalMessage(QObject *parent) : QObject(parent),
25 m_from(0), m_checked(Qt::Unchecked)
26 {
27 }
28
29 MailAddress* MinimalMessage::from() const
30 {
31 return m_from;
32 }
33
34 QString MinimalMessage::subject() const
35 {
36 return QMailMessageMetaData(m_id).subject().simplified();
37 }
38
39 QString MinimalMessage::preview() const
40 {
41 return QMailMessageMetaData(m_id).preview().simplified();
42 }
43
44 bool MinimalMessage::hasAttachments() const
45 {
46 return (QMailMessageMetaData(m_id).status() & QMailMessageMetaData::HasAttachments);
47 }
48
49 bool MinimalMessage::isRead() const
50 {
51 return (QMailMessageMetaData(m_id).status() & QMailMessageMetaData::Read);
52 }
53
54 bool MinimalMessage::isFlagged() const
55 {
56 return (QMailMessageMetaData(m_id).status() & QMailMessageMetaData::Important);
57 }
58
59 bool MinimalMessage::isTodo() const
60 {
61 return (QMailMessageMetaData(m_id).status() & QMailMessageMetaData::Todo);
62 }
63
64 bool MinimalMessage::canBeRestored() const
65 {
66 return QMailMessageMetaData(m_id).restoreFolderId().isValid();
67 }
68
69 bool MinimalMessage::isDone() const
70 {
71 QString done = QMailMessageMetaData(m_id).customField("task-done");
72 if (done.isEmpty()) {
73 return false;
74 }
75 return done.toInt() != 0;
76 }
77
78 bool MinimalMessage::isJunk() const
79 {
80 return (QMailMessageMetaData(m_id).status() & QMailMessageMetaData::Junk);
81 }
82
83 bool MinimalMessage::isListPost() const
84 {
85 return !QMailMessageMetaData(m_id).listId().isEmpty();
86 }
87
88 bool MinimalMessage::isDraft() const
89 {
90 return (QMailMessageMetaData(m_id).status() & QMailMessageMetaData::Draft);
91 }
92
93 QString MinimalMessage::previousFolderName() const
94 {
95 return QMailFolder(QMailMessage(m_id).restoreFolderId()).displayName();
96 }
97
98 QDateTime MinimalMessage::date() const
99 {
100 return QMailMessageMetaData(m_id).date().toLocalTime();
101 }
102
103 QString MinimalMessage::prettyDate()
104 {
105 QDateTime timestamp = date();
106 if (!timestamp.isValid())
107 return QString();
108
109 if (timestamp.date() == QDate::currentDate())
110 return timestamp.toString(tr("hh:mm","time format as hours:minutes e.g. 12:45"));
111
112 int beforeDays = timestamp.date().daysTo(QDate::currentDate());
113 if (beforeDays <= 7) {
114 //:Timeformat as short day name hour:minute e.g. Mon. 12:45
115 return timestamp.toString(tr("ddd hh:mm","time/date format as short day name hours:minutes e.g. Mon. 12:45"));
116 } else if (beforeDays <= 31) {
117 return timestamp.toString(tr("dd MMM","date format as day name and month e.g. 02 May"));
118 } else {
119 return timestamp.toString(tr("dd MMM yy","date format as day month year e.g. 31 02 2020"));
120 }
121 }
122
123 QString MinimalMessage::prettyLongDate()
124 {
125 QDateTime t = date();
126 return QStringLiteral("%1, %2").arg(t.toString(tr("dddd dd","time format as long day name and day e.g. Monday 02")), t.toString(tr("hh:mm","time format as hours:minutes e.g. 12:45")));
127 }
128
129 Qt::CheckState MinimalMessage::checked() const
130 {
131 return m_checked;
132 }
133
134 /** @short returns a messagekey suitable for showing a filtered list of messages from this sender */
135 QVariant MinimalMessage::senderMsgKey() const
136 {
137 QMailMessageKey excludeKey = QMailMessageKey::status((QMailMessage::Removed | QMailMessage::Trash), QMailDataComparator::Excludes);
138 return QMailMessageKey::sender(m_from->address(), QMailDataComparator::Includes) & excludeKey;
139 }
140
141 void MinimalMessage::setMessageId(const quint64 &id)
142 {
143 setMessageId(QMailMessageId(id));
144 }
145
146 void MinimalMessage::setMessageId(const QMailMessageId &id)
147 {
148 m_id = id;
149 if (!m_id.isValid()) {
150 return;
151 }
152 QMailMessageMetaData msg(m_id);
153 // qDebug() << "MSG ID: " << messageId();
154 m_from = new MailAddress(this);
155 m_from->setAddress(msg.from());
156
157 emit minMessageChanged();
158 emit internalMessageChanged();
159 }
160
161 void MinimalMessage::setChecked(const Qt::CheckState &checked)
162 {
163 if (checked == m_checked) {
164 return;
165 }
166 m_checked = checked;
167 emit checkedChanged();
168 }
169
170 void MinimalMessage::setIsTodo(const bool todo)
171 {
172 QMailMessageMetaData mmd(m_id);
173 mmd.setStatus(QMailMessageMetaData::Todo, todo);
174 QMailStore::instance()->updateMessage(&mmd);
175 emit minMessageChanged();
176 }
177
178
179 Message::Message(QObject *parent) : MinimalMessage(parent),
180 m_to(0), m_cc(0), m_bcc(0), m_attachments(0), m_preferPlainText(false)
181 {
182 connect(this, &Message::internalMessageChanged, this, &Message::initMessage);
183 connect(QMailStore::instance(), SIGNAL(messagesUpdated(QMailMessageIdList)), this, SLOT(handleUpdatedMessages(QMailMessageIdList)));
184 m_to = new QQmlObjectListModel<MailAddress>(this);
185 m_cc = new QQmlObjectListModel<MailAddress>(this);
186 m_bcc = new QQmlObjectListModel<MailAddress>(this);
187 m_attachments = new Attachments(this);
188 }
189
190 QString Message::toRecipientsString()
191 {
192 QMailAccountId accId(QMailMessage(m_id).parentAccountId());
193 Account *account = new Account(this);
194 account->setId(accId.toULongLong());
195 AccountConfiguration *config = static_cast<AccountConfiguration *>(account->incoming());
196
197 auto listContainsMe = [](AccountConfiguration *config, const QStringList &recips) -> int {
198 int idx = -1;
199 for (int i = 0; i < recips.size(); ++i) {
200 QString recip = recips.at(i);
201 if (config->name() == recip || config->email() == recip) {
202 return i;
203 }
204 }
205 return idx;
206 };
207
208 if (!config) {
209 qDebug() << "Failed to create account configuration";
210 return QString("Failed");
211 }
212 QStringList addr; // list of addresses to compare
213 // create our list of TO recipients addresses
214 Q_FOREACH(auto a, m_to->toList()) {
215 addr.append(a->address());
216 }
217 int myIndex = listContainsMe(config, addr);
218 // we actually want the name here
219 addr.clear();
220 Q_FOREACH(auto n, m_to->toList()) {
221 addr.append(n->name());
222 }
223 // Now move "Me" to the start
224 if (myIndex != -1) {
225 addr.move(myIndex, 0);
226 // and rename to "you" so that is is displayed
227 // like "to you, Bob, Phil..."
228 addr.replace(0, tr("you"));
229 }
230 return tr("to %1").arg(addr.join(", "));
231 }
232
233 QUrl Message::body() const
234 {
235 return m_body;
236 }
237
238 QUrl Message::findInterestingBodyPart(const QMailMessageId &id, const bool preferPlainText)
239 {
240 if (!id.isValid()) {
241 return QUrl();
242 }
243 QMailMessage msg(id);
244 bool isPlainText = false;
245 QString msgIdString = QString::number(id.toULongLong());
246 QString location;
247 QUrl url;
248
249 if (msg.multipartType() == QMailMessage::MultipartNone && !msg.body().data().isEmpty()) {
250 isPlainText = (msg.body().contentType().content() == QByteArrayLiteral("text/plain"));
251 url.setScheme(QStringLiteral("dekko-msg"));
252 } else {
253 QMailMessagePart *part = 0;
254 if (!preferPlainText && msg.hasHtmlBody()) {
255 QMailMessagePartContainer *html = msg.findHtmlContainer();
256 if (html) {
257 part = static_cast<QMailMessagePart *>(html);
258 }
259 }
260 if (!part || preferPlainText) {
261 QMailMessagePartContainer *ptext = msg.findPlainTextContainer();
262 if (ptext) {
263 part = static_cast<QMailMessagePart *>(ptext);
264 isPlainText = true;
265 }
266 }
267 if (!part) {
268 qDebug() << __func__ << "Unable to find a displayable message part :-/";
269 return QUrl();
270 }
271 location = part->location().toString(true);
272 url.setScheme(QStringLiteral("dekko-part"));
273 }
274 url.setHost(QStringLiteral("msg"));
275 QUrlQuery query;
276 query.addQueryItem(QStringLiteral("messageId"), msgIdString);
277 if (isPlainText) {
278 query.addQueryItem(QStringLiteral("requestFormatting"), QStringLiteral("true"));
279 }
280 if (!location.isEmpty()) {
281 query.addQueryItem(QStringLiteral("location"), location);
282 }
283 url.setQuery(query);
284 // qDebug() << url;
285 return url;
286 }
287
288 bool Message::preferPlainText() const
289 {
290 return m_preferPlainText;
291 }
292
293 void Message::setPreferPlainText(const bool preferPlainText)
294 {
295 if (m_preferPlainText == preferPlainText)
296 return;
297
298 m_preferPlainText = preferPlainText;
299 emit plainTextChanged();
300 }
301
302 void Message::initMessage()
303 {
304 if (m_body.isValid()) {
305 return;
306 }
307 QUrl url = Message::findInterestingBodyPart(m_id, m_preferPlainText);
308 if (url.isValid()) {
309 m_body = url;
310 emit bodyChanged();
311 }
312
313 auto appendAddresses = [](QQmlObjectListModel<MailAddress> *model, const QMailAddressList &addresses) {
314 Q_FOREACH(auto address, addresses) {
315 model->append(new MailAddress(0, address));
316 }
317 };
318 QMailMessage msg(m_id);
319 appendAddresses(m_to, msg.to());
320 appendAddresses(m_cc, msg.cc());
321 appendAddresses(m_bcc, msg.bcc());
322 m_attachments->setMessageId(m_id);
323 emit messageChanged();
324 }
325
326 void Message::handleUpdatedMessages(const QMailMessageIdList &list)
327 {
328 if (list.contains(m_id)) {
329 // Something changed so signal changes
330 emit minMessageChanged();
331 emit messageChanged();
332 }
333 }