-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathewsfetchmaildetailjob.cpp
170 lines (140 loc) · 5.86 KB
/
ewsfetchmaildetailjob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* This file is part of Akonadi EWS Resource
Copyright (C) 2015-2016 Krzysztof Nowicki <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "ewsfetchmaildetailjob.h"
#include <KMime/Message>
#include <KMime/HeaderParsing>
#include <Akonadi/KMime/MessageFlags>
#include "ewsitemshape.h"
#include "ewsgetitemrequest.h"
#include "ewsmailbox.h"
#include "ewsmailhandler.h"
#include "ewsclient_debug.h"
using namespace Akonadi;
EwsFetchMailDetailJob::EwsFetchMailDetailJob(EwsClient &client, QObject *parent, const Akonadi::Collection &collection)
: EwsFetchItemDetailJob(client, parent, collection)
{
EwsItemShape shape(EwsShapeIdOnly);
shape << EwsPropertyField("item:Subject");
shape << EwsPropertyField("item:Importance");
shape << EwsPropertyField("message:From");
shape << EwsPropertyField("message:ToRecipients");
shape << EwsPropertyField("message:CcRecipients");
shape << EwsPropertyField("message:BccRecipients");
shape << EwsPropertyField("item:Categories");
shape << EwsPropertyField("item:DateTimeReceived");
shape << EwsPropertyField("item:InReplyTo");
shape << EwsPropertyField("message:References");
shape << EwsPropertyField("message:ReplyTo");
shape << EwsPropertyField("message:InternetMessageId");
shape << EwsPropertyField("item:Size");
Q_FOREACH(const EwsPropertyField &field, EwsMailHandler::flagsProperties()) {
shape << field;
}
Q_FOREACH(const EwsPropertyField &field, EwsItemHandler::tagsProperties()) {
shape << field;
}
mRequest->setItemShape(shape);
}
EwsFetchMailDetailJob::~EwsFetchMailDetailJob()
{
}
void EwsFetchMailDetailJob::processItems(const QList<EwsGetItemRequest::Response> &responses)
{
Item::List::iterator it = mChangedItems.begin();
Q_FOREACH(const EwsGetItemRequest::Response &resp, responses) {
Item &item = *it;
if (!resp.isSuccess()) {
qCWarningNC(EWSRES_LOG) << QStringLiteral("Failed to fetch item %1").arg(item.remoteId());
continue;
}
const EwsItem &ewsItem = resp.item();
KMime::Message::Ptr msg(new KMime::Message);
// Rebuild the message headers
QVariant v = ewsItem[EwsItemFieldSubject];
if (Q_LIKELY(v.isValid())) {
msg->subject()->fromUnicodeString(v.toString(), "utf-8");
}
v = ewsItem[EwsItemFieldFrom];
if (Q_LIKELY(v.isValid())) {
EwsMailbox mbox = v.value<EwsMailbox>();
msg->from()->addAddress(mbox);
}
v = ewsItem[EwsItemFieldToRecipients];
if (Q_LIKELY(v.isValid())) {
EwsMailbox::List mboxList = v.value<EwsMailbox::List>();
QStringList addrList;
Q_FOREACH(const EwsMailbox &mbox, mboxList) {
msg->to()->addAddress(mbox);
}
}
v = ewsItem[EwsItemFieldCcRecipients];
if (Q_LIKELY(v.isValid())) {
EwsMailbox::List mboxList = v.value<EwsMailbox::List>();
QStringList addrList;
Q_FOREACH(const EwsMailbox &mbox, mboxList) {
msg->cc()->addAddress(mbox);
}
}
v = ewsItem[EwsItemFieldBccRecipients];
if (v.isValid()) {
EwsMailbox::List mboxList = v.value<EwsMailbox::List>();
QStringList addrList;
Q_FOREACH(const EwsMailbox &mbox, mboxList) {
msg->bcc()->addAddress(mbox);
}
}
v = ewsItem[EwsItemFieldInternetMessageId];
if (v.isValid()) {
msg->messageID()->from7BitString(v.toString().toAscii());
}
v = ewsItem[EwsItemFieldInReplyTo];
if (v.isValid()) {
msg->inReplyTo()->from7BitString(v.toString().toAscii());
}
v = ewsItem[EwsItemFieldDateTimeReceived];
if (v.isValid()) {
msg->date()->setDateTime(v.toDateTime());
}
v = ewsItem[EwsItemFieldReferences];
if (v.isValid()) {
msg->references()->from7BitString(v.toString().toAscii());
}
v = ewsItem[EwsItemFieldReplyTo];
if (v.isValid()) {
EwsMailbox mbox = v.value<EwsMailbox>();
msg->replyTo()->addAddress(mbox);
}
msg->assemble();
item.setPayload(KMime::Message::Ptr(msg));
v = ewsItem[EwsItemFieldSize];
if (v.isValid()) {
item.setSize(v.toUInt());
}
// Workaround for Akonadi bug
// When setting flags, adding each of them separately vs. setting a list in one go makes
// a difference. In the former case the item treats this as an incremental change and
// records flags added and removed. In the latter it sets a flag indicating that flags were
// reset.
// For some strange reason Akonadi is not seeing the flags in the latter case.
Q_FOREACH(const QByteArray &flag, EwsMailHandler::readFlags(ewsItem)) {
item.setFlag(flag);
}
qCDebugNC(EWSRES_LOG) << "EwsFetchMailDetailJob::processItems:" << ewsHash(item.remoteId()) << item.flags();
it++;
}
qCDebugNC(EWSRES_LOG) << "EwsFetchMailDetailJob::processItems: done";
emitResult();
}