~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to akonadi/resources/kolabproxy/addressbookhandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2009-07-10 06:34:50 UTC
  • mfrom: (1.1.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20090710063450-neojgew2fh0n3y0u
Tags: 4:4.2.96-0ubuntu1
* New upstream release
* Bump kde build-deps to 4.2.96

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright (c) 2009 Andras Mantia <amantia@kde.org>
3
 
    Copyright (c) 2009 Kevin Krammer <kevin.krammer@gmx.at>
4
 
 
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.
9
 
 
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.
14
 
 
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
18
 
    02110-1301, USA.
19
 
*/
20
 
 
21
 
#include "addressbookhandler.h"
22
 
#include "contact.h"
23
 
#include "distributionlist.h"
24
 
 
25
 
#include <kabc/addressee.h>
26
 
#include <kabc/contactgroup.h>
27
 
#include <kabc/stdaddressbook.h>
28
 
#include <kdebug.h>
29
 
#include <kmime/kmime_codecs.h>
30
 
 
31
 
#include <QBuffer>
32
 
 
33
 
 
34
 
AddressBookHandler::AddressBookHandler(): KolabHandler()
35
 
{
36
 
  m_mimeType = "application/x-vnd.kolab.contact";
37
 
 
38
 
  // TODO using old KABC API should really be avoided
39
 
  mAddressBook = KABC::StdAddressBook::self(true);
40
 
}
41
 
 
42
 
 
43
 
AddressBookHandler::~AddressBookHandler()
44
 
{
45
 
}
46
 
 
47
 
 
48
 
Akonadi::Item::List AddressBookHandler::translateItems(const Akonadi::Item::List & items)
49
 
{
50
 
  Akonadi::Item::List newItems;
51
 
  Q_FOREACH(const Akonadi::Item &item, items)
52
 
  {
53
 
//     kDebug() << item.id();
54
 
    if (!item.hasPayload<MessagePtr>()) {
55
 
      kWarning() << "Payload is not a MessagePtr!";
56
 
      continue;
57
 
    }
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);
65
 
      newItems << newItem;
66
 
    } else if (contactGroupFromKolab(payload, contactGroup)) {
67
 
      Akonadi::Item newItem(KABC::ContactGroup::mimeType());
68
 
      newItem.setRemoteId(QString::number(item.id()));
69
 
      newItem.setPayload(contactGroup);
70
 
      newItems << newItem;
71
 
    }
72
 
  }
73
 
 
74
 
  return newItems;
75
 
}
76
 
 
77
 
bool AddressBookHandler::addresseFromKolab(MessagePtr data, KABC::Addressee &addressee)
78
 
{
79
 
  KMime::Content *xmlContent  = findContentByType(data, m_mimeType);
80
 
  if (xmlContent) {
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()) {
86
 
      QByteArray type;
87
 
      KMime::Content *imgContent = findContentByName(data, "kolab-picture.png", type);
88
 
      if (imgContent) {
89
 
        QByteArray imgData = imgContent->decodedContent();
90
 
        QBuffer buffer(&imgData);
91
 
        buffer.open(QIODevice::ReadOnly);
92
 
        QImage image;
93
 
        image.load(&buffer, "PNG");
94
 
        contact.setPicture(image);
95
 
      }
96
 
    }
97
 
 
98
 
    QString logoAttachmentName = contact.logoAttachmentName();
99
 
    if (!logoAttachmentName.isEmpty()) {
100
 
      QByteArray type;
101
 
      KMime::Content *imgContent = findContentByName(data, "kolab-logo.png", type);
102
 
      if (imgContent) {
103
 
        QByteArray imgData = imgContent->decodedContent();
104
 
        QBuffer buffer(&imgData);
105
 
        buffer.open(QIODevice::ReadOnly);
106
 
        QImage image;
107
 
        image.load(&buffer, "PNG");
108
 
        contact.setLogo(image);
109
 
      }
110
 
    }
111
 
 
112
 
    QString soundAttachmentName = contact.soundAttachmentName();
113
 
    if (!soundAttachmentName.isEmpty()) {
114
 
      QByteArray type;
115
 
      KMime::Content *content = findContentByName(data, "sound", type);
116
 
      if (content) {
117
 
        QByteArray sData = content->decodedContent();
118
 
        contact.setSound(sData);
119
 
      }
120
 
    }
121
 
    contact.saveTo(&addressee);
122
 
    return true;
123
 
  }
124
 
  return false;
125
 
}
126
 
 
127
 
bool AddressBookHandler::contactGroupFromKolab(MessagePtr data, KABC::ContactGroup &contactGroup)
128
 
{
129
 
  KMime::Content *xmlContent  = findContentByType(data, m_mimeType + ".distlist");
130
 
  if (xmlContent) {
131
 
    QByteArray xmlData = xmlContent->decodedContent();
132
 
//     kDebug() << "xmlData " << xmlData;
133
 
    Kolab::DistributionList distList(QString::fromUtf8(xmlData));
134
 
    distList.saveTo(&contactGroup);
135
 
    return true;
136
 
  }
137
 
  return false;
138
 
}
139
 
 
140
 
void AddressBookHandler::toKolabFormat(const Akonadi::Item& item, Akonadi::Item &imapItem)
141
 
{
142
 
  if (item.hasPayload<KABC::Addressee>()) {
143
 
    KABC::Addressee addressee = item.payload<KABC::Addressee>();
144
 
    Kolab::Contact contact(&addressee, 0);
145
 
 
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);
150
 
 
151
 
    distListToKolabFormat(distList, imapItem);
152
 
  } else {
153
 
    kWarning() << "Payload is neither a KABC::Addressee nor KABC::ContactGroup!";
154
 
    return;
155
 
  }
156
 
}
157
 
 
158
 
void AddressBookHandler::contactToKolabFormat(const Kolab::Contact& contact, Akonadi::Item &imapItem)
159
 
{
160
 
  imapItem.setMimeType( "message/rfc822" );
161
 
 
162
 
  MessagePtr message(new KMime::Message);
163
 
  QString header;
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());
171
 
 
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);
180
 
 
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);
189
 
 
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());
196
 
    QByteArray pic;
197
 
    QBuffer buffer(&pic);
198
 
    buffer.open(QIODevice::WriteOnly);
199
 
    contact.picture().save(&buffer, "PNG");
200
 
    buffer.close();
201
 
    content->setBody(pic.toBase64());
202
 
    message->addContent(content);
203
 
  }
204
 
 
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());
211
 
    QByteArray pic;
212
 
    QBuffer buffer(&pic);
213
 
    buffer.open(QIODevice::WriteOnly);
214
 
    contact.logo().save(&buffer, "PNG");
215
 
    buffer.close();
216
 
    content->setBody(pic.toBase64());
217
 
    message->addContent(content);
218
 
  }
219
 
 
220
 
 
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);
229
 
  }
230
 
 
231
 
 
232
 
  imapItem.setPayload(message);
233
 
}
234
 
 
235
 
void AddressBookHandler::distListToKolabFormat(const Kolab::DistributionList& distList, Akonadi::Item &imapItem)
236
 
{
237
 
  imapItem.setMimeType( "message/rfc822" );
238
 
 
239
 
  MessagePtr message(new KMime::Message);
240
 
  QString header;
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());
248
 
 
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);
257
 
 
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);
266
 
 
267
 
  imapItem.setPayload(message);
268
 
}
269
 
 
270
 
QStringList AddressBookHandler::contentMimeTypes()
271
 
{
272
 
  return QStringList() << KABC::Addressee::mimeType()
273
 
                       << KABC::ContactGroup::mimeType();
274
 
}