2
Copyright (c) 2009 Andras Mantia <amantia@kde.org>
3
Copyright (c) 2009 Kevin Krammer <kevin.krammer@gmx.at>
5
This library is free software; you can redistribute it and/or modify it
6
under the terms of the GNU Library General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or (at your
8
option) any later version.
10
This library is distributed in the hope that it will be useful, but WITHOUT
11
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13
License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to the
17
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
#include "addressbookhandler.h"
23
#include "distributionlist.h"
25
#include <kabc/addressee.h>
26
#include <kabc/contactgroup.h>
27
#include <kabc/stdaddressbook.h>
29
#include <kmime/kmime_codecs.h>
34
AddressBookHandler::AddressBookHandler(): KolabHandler()
36
m_mimeType = "application/x-vnd.kolab.contact";
38
// TODO using old KABC API should really be avoided
39
mAddressBook = KABC::StdAddressBook::self(true);
43
AddressBookHandler::~AddressBookHandler()
48
Akonadi::Item::List AddressBookHandler::translateItems(const Akonadi::Item::List & items)
50
Akonadi::Item::List newItems;
51
Q_FOREACH(const Akonadi::Item &item, items)
53
// kDebug() << item.id();
54
if (!item.hasPayload<MessagePtr>()) {
55
kWarning() << "Payload is not a MessagePtr!";
58
MessagePtr payload = item.payload<MessagePtr>();
59
KABC::Addressee addressee;
60
KABC::ContactGroup contactGroup;
61
if (addresseFromKolab(payload, addressee)) {
62
Akonadi::Item newItem(KABC::Addressee::mimeType());
63
newItem.setRemoteId(QString::number(item.id()));
64
newItem.setPayload(addressee);
66
} else if (contactGroupFromKolab(payload, contactGroup)) {
67
Akonadi::Item newItem(KABC::ContactGroup::mimeType());
68
newItem.setRemoteId(QString::number(item.id()));
69
newItem.setPayload(contactGroup);
77
bool AddressBookHandler::addresseFromKolab(MessagePtr data, KABC::Addressee &addressee)
79
KMime::Content *xmlContent = findContentByType(data, m_mimeType);
81
QByteArray xmlData = xmlContent->decodedContent();
82
// kDebug() << "xmlData " << xmlData;
83
Kolab::Contact contact(QString::fromUtf8(xmlData));
84
QString pictureAttachmentName = contact.pictureAttachmentName();
85
if (!pictureAttachmentName.isEmpty()) {
87
KMime::Content *imgContent = findContentByName(data, "kolab-picture.png", type);
89
QByteArray imgData = imgContent->decodedContent();
90
QBuffer buffer(&imgData);
91
buffer.open(QIODevice::ReadOnly);
93
image.load(&buffer, "PNG");
94
contact.setPicture(image);
98
QString logoAttachmentName = contact.logoAttachmentName();
99
if (!logoAttachmentName.isEmpty()) {
101
KMime::Content *imgContent = findContentByName(data, "kolab-logo.png", type);
103
QByteArray imgData = imgContent->decodedContent();
104
QBuffer buffer(&imgData);
105
buffer.open(QIODevice::ReadOnly);
107
image.load(&buffer, "PNG");
108
contact.setLogo(image);
112
QString soundAttachmentName = contact.soundAttachmentName();
113
if (!soundAttachmentName.isEmpty()) {
115
KMime::Content *content = findContentByName(data, "sound", type);
117
QByteArray sData = content->decodedContent();
118
contact.setSound(sData);
121
contact.saveTo(&addressee);
127
bool AddressBookHandler::contactGroupFromKolab(MessagePtr data, KABC::ContactGroup &contactGroup)
129
KMime::Content *xmlContent = findContentByType(data, m_mimeType + ".distlist");
131
QByteArray xmlData = xmlContent->decodedContent();
132
// kDebug() << "xmlData " << xmlData;
133
Kolab::DistributionList distList(QString::fromUtf8(xmlData));
134
distList.saveTo(&contactGroup);
140
void AddressBookHandler::toKolabFormat(const Akonadi::Item& item, Akonadi::Item &imapItem)
142
if (item.hasPayload<KABC::Addressee>()) {
143
KABC::Addressee addressee = item.payload<KABC::Addressee>();
144
Kolab::Contact contact(&addressee, 0);
146
contactToKolabFormat(contact, imapItem);
147
} else if (item.hasPayload<KABC::ContactGroup>()) {
148
KABC::ContactGroup contactGroup = item.payload<KABC::ContactGroup>();
149
Kolab::DistributionList distList(&contactGroup, mAddressBook);
151
distListToKolabFormat(distList, imapItem);
153
kWarning() << "Payload is neither a KABC::Addressee nor KABC::ContactGroup!";
158
void AddressBookHandler::contactToKolabFormat(const Kolab::Contact& contact, Akonadi::Item &imapItem)
160
imapItem.setMimeType( "message/rfc822" );
162
MessagePtr message(new KMime::Message);
164
header += "From: " + contact.fullEmail() + "\n";
165
header += "Subject: " + contact.uid() + "\n";
166
header += "Date: " + QDateTime::currentDateTime().toString(Qt::TextDate) + "\n";
167
header += "User-Agent: Akonadi Kolab Proxy Resource \n";
168
header += "MIME-Version: 1.0\n";
169
header += "X-Kolab-Type: " + m_mimeType + "\n\n\n";
170
message->setContent(header.toLatin1());
172
KMime::Content *content = new KMime::Content();
173
QByteArray contentData = QByteArray("Content-Type: text/plain; charset=\"us-ascii\"\nContent-Transfer-Encoding: 7bit\n\n") +
174
"This is a Kolab Groupware object.\n" +
175
"To view this object you will need an email client that can understand the Kolab Groupware format.\n" +
176
"For a list of such email clients please visit\n"
177
"http://www.kolab.org/kolab2-clients.html\n";
178
content->setContent(contentData);
179
message->addContent(content);
181
content = new KMime::Content();
182
header = "Content-Type: " + m_mimeType + "; name=\"kolab.xml\"\n";
183
header += "Content-Transfer-Encoding: quoted-printable\n";
184
header += "Content-Disposition: attachment; filename=\"kolab.xml\"";
185
content->setHead(header.toLatin1());
186
KMime::Codec *codec = KMime::Codec::codecForName( "quoted-printable" );
187
content->setBody(codec->encode(contact.saveXML().toUtf8()));
188
message->addContent(content);
190
if (!contact.pictureAttachmentName().isEmpty()) {
191
header = "Content-Type: image/png; name=\"kolab-picture.png\"\n";
192
header += "Content-Transfer-Encoding: base64\n";
193
header += "Content-Disposition: attachment; filename=\"kolab-picture.png\"";
194
content = new KMime::Content();
195
content->setHead(header.toLatin1());
197
QBuffer buffer(&pic);
198
buffer.open(QIODevice::WriteOnly);
199
contact.picture().save(&buffer, "PNG");
201
content->setBody(pic.toBase64());
202
message->addContent(content);
205
if (!contact.logoAttachmentName().isEmpty()) {
206
header = "Content-Type: image/png; name=\"kolab-logo.png\"\n";
207
header += "Content-Transfer-Encoding: base64\n";
208
header += "Content-Disposition: attachment; filename=\"kolab-logo.png\"";
209
content = new KMime::Content();
210
content->setHead(header.toLatin1());
212
QBuffer buffer(&pic);
213
buffer.open(QIODevice::WriteOnly);
214
contact.logo().save(&buffer, "PNG");
216
content->setBody(pic.toBase64());
217
message->addContent(content);
221
if (!contact.soundAttachmentName().isEmpty()) {
222
header = "Content-Type: audio/unknown; name=\"sound\"\n";
223
header += "Content-Transfer-Encoding: base64\n";
224
header += "Content-Disposition: attachment; filename=\"sound\"";
225
content = new KMime::Content();
226
content->setHead(header.toLatin1());
227
content->setBody(contact.sound().toBase64());
228
message->addContent(content);
232
imapItem.setPayload(message);
235
void AddressBookHandler::distListToKolabFormat(const Kolab::DistributionList& distList, Akonadi::Item &imapItem)
237
imapItem.setMimeType( "message/rfc822" );
239
MessagePtr message(new KMime::Message);
241
header += "From: " + distList.name() + "\n";
242
header += "Subject: " + distList.uid() + "\n";
243
header += "Date: " + QDateTime::currentDateTime().toString(Qt::TextDate) + "\n";
244
header += "User-Agent: Akonadi Kolab Proxy Resource \n";
245
header += "MIME-Version: 1.0\n";
246
header += "X-Kolab-Type: " + m_mimeType + ".distlist\n\n\n";
247
message->setContent(header.toLatin1());
249
KMime::Content *content = new KMime::Content();
250
QByteArray contentData = QByteArray("Content-Type: text/plain; charset=\"us-ascii\"\nContent-Transfer-Encoding: 7bit\n\n") +
251
"This is a Kolab Groupware object.\n" +
252
"To view this object you will need an email client that can understand the Kolab Groupware format.\n" +
253
"For a list of such email clients please visit\n"
254
"http://www.kolab.org/kolab2-clients.html\n";
255
content->setContent(contentData);
256
message->addContent(content);
258
content = new KMime::Content();
259
header = "Content-Type: " + m_mimeType + ".distlist; name=\"kolab.xml\"\n";
260
header += "Content-Transfer-Encoding: quoted-printable\n";
261
header += "Content-Disposition: attachment; filename=\"kolab.xml\"";
262
content->setHead(header.toLatin1());
263
KMime::Codec *codec = KMime::Codec::codecForName( "quoted-printable" );
264
content->setBody(codec->encode(distList.saveXML().toUtf8()));
265
message->addContent(content);
267
imapItem.setPayload(message);
270
QStringList AddressBookHandler::contentMimeTypes()
272
return QStringList() << KABC::Addressee::mimeType()
273
<< KABC::ContactGroup::mimeType();