-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathewsmailhandler.cpp
188 lines (153 loc) · 6.09 KB
/
ewsmailhandler.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* 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 "ewsmailhandler.h"
#include <KMime/Message>
#include <Akonadi/KMime/MessageFlags>
#include <AkonadiCore/Item>
#include "ewsfetchmaildetailjob.h"
#include "ewsmodifymailjob.h"
#include "ewscreatemailjob.h"
using namespace Akonadi;
static const EwsPropertyField propPidFlagStatus(0x1090, EwsPropTypeInteger);
EwsMailHandler::EwsMailHandler()
{
}
EwsMailHandler::~EwsMailHandler()
{
}
EwsItemHandler *EwsMailHandler::factory()
{
return new EwsMailHandler();
}
EwsFetchItemDetailJob *EwsMailHandler::fetchItemDetailJob(EwsClient &client, QObject *parent,
const Akonadi::Collection &collection)
{
return new EwsFetchMailDetailJob(client, parent, collection);
}
void EwsMailHandler::setSeenFlag(Item &item, bool value)
{
if (value) {
item.setFlag(MessageFlags::Seen);
}
else {
item.clearFlag(MessageFlags::Seen);
}
}
QString EwsMailHandler::mimeType()
{
return KMime::Message::mimeType();
}
bool EwsMailHandler::setItemPayload(Akonadi::Item &item, const EwsItem &ewsItem)
{
qDebug() << "EwsMailHandler::setItemPayload";
QByteArray mimeContent = ewsItem[EwsItemFieldMimeContent].toByteArray();
if (mimeContent.isEmpty()) {
qWarning() << QStringLiteral("MIME content is empty!");
return false;
}
mimeContent.replace("\r\n", "\n");
KMime::Message::Ptr msg(new KMime::Message);
msg->setContent(mimeContent);
msg->parse();
qDebug() << msg->attachments().size() << "attachments";
// Some messages might just be empty (just headers). This results in the body being empty.
// The problem is that when Akonadi sees an empty body it will interpret this as "body not
// yet loaded" and will retry which will cause an endless loop. To work around this put a
// single newline so that it is not empty.
if (msg->body().isEmpty()) {
msg->setBody("\n");
}
Q_FOREACH(KMime::Content* c, msg->attachments()) {
KMime::Headers::ContentID *cid = c->contentID(false);
if (cid) {
qDebug() << cid->asUnicodeString();
}
}
item.setPayload<KMime::Message::Ptr>(msg);
return true;
}
EwsModifyItemJob *EwsMailHandler::modifyItemJob(EwsClient& client, const QVector<Akonadi::Item> &items,
const QSet<QByteArray> &parts, QObject *parent)
{
return new EwsModifyMailJob(client, items, parts, parent);
}
EwsCreateItemJob *EwsMailHandler::createItemJob(EwsClient& client, const Akonadi::Item &item,
const Akonadi::Collection &collection,
EwsTagStore *tagStore, EwsResource *parent)
{
return new EwsCreateMailJob(client, item, collection, tagStore, parent);
}
QHash<EwsPropertyField, QVariant> EwsMailHandler::writeFlags(const QSet<QByteArray> flags)
{
// Strip all the message flags that can be stored in dedicated Exchange fields and leave
// any remaining ones to be stored in a private property.
QSet<QByteArray> unknownFlags;
QHash<EwsPropertyField, QVariant> propertyHash;
bool isRead = false;
bool isFlagged = false;
Q_FOREACH(const QByteArray &flag, flags) {
if (flag == MessageFlags::Seen) {
isRead = true;
} else if (flag == MessageFlags::Flagged) {
isFlagged = true;
} else if (flag == MessageFlags::HasAttachment || flag == MessageFlags::HasInvitation ||
flag == MessageFlags::Signed || flag == MessageFlags::Encrypted) {
// These flags are read-only. Remove them from the unknown list but don't do anything with them.
} else {
unknownFlags.insert(flag);
}
}
propertyHash.insert(EwsPropertyField(QStringLiteral("message:IsRead")),
isRead ? QStringLiteral("true") : QStringLiteral("false"));
if (isFlagged) {
propertyHash.insert(propPidFlagStatus, QStringLiteral("2"));
}
else {
propertyHash.insert(propPidFlagStatus, QVariant());
}
propertyHash.unite(EwsItemHandler::writeFlags(unknownFlags));
return propertyHash;
}
QSet<QByteArray> EwsMailHandler::readFlags(const EwsItem &item)
{
QSet<QByteArray> flags = EwsItemHandler::readFlags(item);
QVariant v = item[EwsItemFieldIsRead];
if (v.isValid() && v.toBool()) {
flags.insert(MessageFlags::Seen);
}
v = item[EwsItemFieldHasAttachments];
if (v.isValid() && v.toBool()) {
flags.insert(MessageFlags::HasAttachment);
}
QVariant flagProp = item[propPidFlagStatus];
if (!flagProp.isNull() && (flagProp.toUInt() == 2)) {
flags.insert(MessageFlags::Flagged);
}
if (item.type() == EwsItemTypeMeetingRequest) {
flags.insert(MessageFlags::HasInvitation);
}
return flags;
}
QList<EwsPropertyField> EwsMailHandler::flagsProperties()
{
QList<EwsPropertyField> props = EwsItemHandler::flagsProperties();
props.append(propPidFlagStatus);
props.append(EwsPropertyField(QStringLiteral("message:IsRead")));
props.append(EwsPropertyField(QStringLiteral("item:HasAttachments")));
return props;
}
EWS_DECLARE_ITEM_HANDLER(EwsMailHandler, EwsItemTypeMessage)
EWS_DECLARE_ITEM_HANDLER(EwsMailHandler, EwsItemTypePostItem)