summaryrefslogtreecommitdiff
path: root/kolabformat/v2helpers.h
blob: f93f9a132acd3a1bbada171479999c3654726386 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (C) 2012  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef V2HELPERS_H
#define V2HELPERS_H

#include "kolabdefinitions.h"

#include "kolabformatV2/kolabbase.h"
#include "kolabformatV2/journal.h"
#include "kolabformatV2/task.h"
#include "kolabformatV2/event.h"
#include "kolabformatV2/contact.h"
#include "kolabformatV2/distributionlist.h"
#include "kolabformatV2/note.h"
#include "mime/mimeutils.h"
#include "kolabformat/errorhandler.h"

#include <qdom.h>
#include <kdebug.h>
#include <qbuffer.h>
#include <akonadi/notes/noteutils.h>

namespace Kolab {


/*
 * Parse XML, create KCalCore container and extract attachments
 */
template <typename KCalPtr, typename Container>
static KCalPtr fromXML(const QByteArray &xmlData, QStringList &attachments)
{
    const QDomDocument xmlDoc = KolabV2::KolabBase::loadDocument( QString::fromUtf8(xmlData) ); //TODO extract function from V2 format
    Q_ASSERT ( !xmlDoc.isNull() );
    const KCalPtr i = Container::fromXml( xmlDoc, QString() ); //For parsing we don't need the timezone, so we don't set one
    Q_ASSERT ( i );
    QDomNodeList nodes = xmlDoc.elementsByTagName("inline-attachment");
    for (int i = 0; i < nodes.size(); i++ ) {
        attachments.append(nodes.at(i).toElement().text());
    }
    return i;
}

template <typename IncidencePtr, typename Converter>
static inline IncidencePtr incidenceFromKolabImpl( const KMime::Message::Ptr &data, const QByteArray &mimetype, const QString &timezoneId )
{
    KMime::Content *xmlContent = Mime::findContentByType( data, mimetype );
    if ( !xmlContent ) {
        Warning() << "couldn't find part";
        return IncidencePtr();
    }
    const QByteArray &xmlData = xmlContent->decodedContent();
    
    QStringList attachments;
    IncidencePtr ptr = fromXML<IncidencePtr, Converter>(xmlData, attachments); //TODO do we care about timezone?
    Mime::getAttachments(ptr, attachments, data);
    
    return ptr;
}

QImage getPicture(const QString &pictureAttachmentName, const KMime::Message::Ptr &data, QByteArray &type)
{
    KMime::Content *imgContent = Mime::findContentByName(data, pictureAttachmentName/*"kolab-picture.png"*/, type);
    if (!imgContent) {
        Warning() << "could not find picture: " << pictureAttachmentName;
    }
    QByteArray imgData = imgContent->decodedContent();
    QBuffer buffer(&imgData);
    buffer.open(QIODevice::ReadOnly);
    QImage image;
    bool success = false;
    if (type == "image/jpeg") {
        success = image.load(&buffer, "JPEG");
        //FIXME I tried getting the code to interpret the picture as PNG, but the VCard implementation writes it as JPEG anyways...
//         if (success) {
//             QByteArray pic;
//             QBuffer b(&pic);
//             b.open(QIODevice::ReadWrite);
//             Q_ASSERT(image.save(&b, "PNG"));
//             b.close();
//             Debug() << pic.toBase64();
//             QBuffer b2(&pic);
//             b2.open(QIODevice::ReadOnly);
//             success = image.load(&b2, "PNG");
//             b2.close();
//             Q_ASSERT(success);
//         }
    } else {
        type = "image/png";
        success = image.load(&buffer, "PNG");
    }
    buffer.close();
    if (!success) {
        Warning() << "failed to load picture";
    }
    return image;
}

KABC::Addressee addresseeFromKolab( const QByteArray &xmlData, const KMime::Message::Ptr &data)
{
    KABC::Addressee addressee;
//     Debug() << "xmlData " << xmlData;
    KolabV2::Contact contact(QString::fromUtf8(xmlData));
    QByteArray type;
    const QString &pictureAttachmentName = contact.pictureAttachmentName();
    if (!pictureAttachmentName.isEmpty()) {
        const QImage &img = getPicture(pictureAttachmentName, data, type);
        contact.setPicture(img, type);
    }
    
    const QString &logoAttachmentName = contact.logoAttachmentName();
    if (!logoAttachmentName.isEmpty()) {
        contact.setLogo(getPicture(logoAttachmentName, data, type), type);
    }
    
    const QString &soundAttachmentName = contact.soundAttachmentName();
    if (!soundAttachmentName.isEmpty()) {
        QByteArray type;
        KMime::Content *content = Mime::findContentByName(data, soundAttachmentName/*"sound"*/, type);
        if (content) {
            const QByteArray &sData = content->decodedContent();
            contact.setSound(sData);
        } else {
            Warning() << "could not find sound: " << soundAttachmentName;
        }
    }
    contact.saveTo(&addressee);
    return addressee;
}

QByteArray createPicture(const QImage &img, const QString &format, QString &type)
{
    QByteArray pic;
    QBuffer buffer(&pic);
    buffer.open(QIODevice::WriteOnly);
    type = "image/png";
    //FIXME it's not possible to save jpegs lossless, so we always use png. otherwise we would compress the image on every write.
//     if (format == "image/jpeg") {
//         type = "image/jpeg";
//         img.save(&buffer, "JPEG");
//     } else {
        img.save(&buffer, "PNG");
//     }
    buffer.close();
    return pic;
}

KMime::Message::Ptr contactToKolabFormat(const KolabV2::Contact& contact, const QString &productId)
{
    KMime::Message::Ptr message = Mime::createMessage( KOLAB_TYPE_CONTACT, false, productId );
    message->subject()->fromUnicodeString( contact.uid(), "utf-8" );
    message->from()->fromUnicodeString( contact.fullEmail(), "utf-8" );
    
    KMime::Content* content = Mime::createMainPart( KOLAB_TYPE_CONTACT, contact.saveXML().toUtf8() );
    message->addContent( content );
    
    if ( !contact.picture().isNull() ) {
        QString type;
        const QByteArray &pic = createPicture(contact.picture(), contact.pictureFormat(), type);
        content = Mime::createAttachmentPart(QByteArray(), type, /*"kolab-picture.png"*/contact.pictureAttachmentName(), pic );
        message->addContent(content);
    }
    
    if ( !contact.logo().isNull() ) {
        QString type;
        const QByteArray &pic = createPicture(contact.logo(), contact.logoFormat(), type);
        content = Mime::createAttachmentPart(QByteArray(), type, /*"kolab-logo.png"*/contact.logoAttachmentName(), pic );
        message->addContent(content);
    }
    
    if ( !contact.sound().isEmpty() ) {
        content = Mime::createAttachmentPart(QByteArray(), "audio/unknown", /*"sound"*/contact.soundAttachmentName(), contact.sound() );
        message->addContent(content);
    }
    
    message->assemble();
    return message;
}

KABC::ContactGroup contactGroupFromKolab(const QByteArray &xmlData)
{
    KABC::ContactGroup contactGroup;
    //     kDebug() << "xmlData " << xmlData;
    KolabV2::DistributionList distList(QString::fromUtf8(xmlData));
    distList.saveTo(&contactGroup);
    return contactGroup;
}

KMime::Message::Ptr distListToKolabFormat(const KolabV2::DistributionList& distList, const QString &productId)
{    
    KMime::Message::Ptr message = Mime::createMessage( KOLAB_TYPE_DISTLIST, false, productId );
    message->subject()->fromUnicodeString( distList.uid(), "utf-8" );
    message->from()->fromUnicodeString( distList.uid(), "utf-8" );
    
    KMime::Content* content = Mime::createMainPart( KOLAB_TYPE_DISTLIST, distList.saveXML().toUtf8() );
    message->addContent( content );
    
    message->assemble();
    return message;
}

KMime::Message::Ptr noteFromKolab(const QByteArray &xmlData, const KMime::Message::Ptr &data)
{
    KolabV2::Note j;
    if ( !j.load( xmlData ) ) {
        Warning() << "failed to read note";
        return KMime::Message::Ptr();
    }
    
    Akonadi::NoteUtils::NoteMessageWrapper note;
    note.setTitle(j.summary());
    note.setText(j.body().toUtf8());
    note.setFrom("kolab@kde4");
    note.setCreationDate(data->date()->dateTime());
    return note.message();
}

KMime::Message::Ptr noteToKolab(const KMime::Message::Ptr& msg, const QString &productId)
{
    Akonadi::NoteUtils::NoteMessageWrapper note(msg);
    KolabV2::Note j;
    j.setSummary( note.title() );
    j.setBody( note.text() );
    
    return Mime::createMessage(j.summary(), KOLAB_TYPE_NOTE, KOLAB_TYPE_NOTE, j.saveXML().toUtf8(), false, productId);
}

QStringList readLegacyDictionaryConfiguration(const QByteArray &xmlData, QString &language)
{
    QStringList dictionary;
    const QDomDocument xmlDoc = KolabV2::KolabBase::loadDocument( QString::fromUtf8(xmlData) ); //TODO extract function from V2 format
    Q_ASSERT ( !xmlDoc.isNull() );

    QDomElement top = xmlDoc.documentElement();

    if ( top.tagName() != "configuration" ) {
        qWarning( "XML error: Top tag was %s instead of the expected configuration",
                top.tagName().toAscii().data() );
        return QStringList();
    }

    for ( QDomNode n = top.firstChild(); !n.isNull(); n = n.nextSibling() ) {
        if ( n.isComment() || !n.isElement() )
            continue;
        QDomElement e = n.toElement();
        if (e.tagName() == "language") {
            language = e.text();
        } else if (e.tagName() == "e") {
            dictionary.append(e.text());
        }
    }
    return dictionary;
}

}

#endif