summaryrefslogtreecommitdiff
path: root/mime/mimeutils.cpp
blob: 2ce647533eb88684d5d5eace7778b0238ec7cc82 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * 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/>.
 */

#include "mimeutils.h"
#include <quuid.h>
#include <QtCore/qfile.h>
#include <qdom.h>
#include <kdebug.h>
#include <kabc/addressee.h>
#include "kolabformat/kolabdefinitions.h"
#include "kolabformat/errorhandler.h"
#include "libkolab-version.h"

namespace Kolab {
    namespace Mime {

KMime::Content* findContentByType(const KMime::Message::Ptr &data, const QByteArray &type)
{
    if (type.isEmpty()) {
        Error() << "Empty type";
        return 0;
    }
    Q_ASSERT(!data->contents().isEmpty());
    Q_FOREACH(KMime::Content *c, data->contents()) {
//         qDebug() << c->contentType()->mimeType() << type;
        if (c->contentType()->mimeType() ==  type) {
            return c;
        }
    }
    return 0;
}

KMime::Content* findContentByName(const KMime::Message::Ptr &data, const QString &name, QByteArray &type)
{
    Q_ASSERT(!data->contents().isEmpty());
    Q_FOREACH(KMime::Content *c, data->contents()) {
//         qDebug() << "searching: " << c->contentType()->name().toUtf8();
        if ( c->contentType()->name() == name ) {
            type = c->contentType()->mimeType();
            return c;
        }
    }
    return 0;
}

KMime::Content* findContentById(const KMime::Message::Ptr &data, const QByteArray &id, QByteArray &type, QString &name)
{
    if (id.isEmpty()) {
        Error() << "looking for empty cid";
        return 0;
    }
    Q_ASSERT(!data->contents().isEmpty());
    Q_FOREACH(KMime::Content *c, data->contents()) {
//         kDebug() << "searching: " << c->contentID()->identifier();
        if ( c->contentID()->identifier() == id ) {
            type = c->contentType()->mimeType();
            name = c->contentType()->name();
            return c;
        }
    }
    return 0;
}

QList<QByteArray> getContentMimeTypeList(const KMime::Message::Ptr& data)
{
    QList<QByteArray> typeList;
    Q_ASSERT(!data->contents().isEmpty());
    Q_FOREACH(KMime::Content *c, data->contents()) {
        typeList.append(c->contentType()->mimeType());
    }
    return typeList;
}



QByteArray getXmlDocument(const KMime::Message::Ptr &data, const QByteArray &mimetype)
{
    if ( KMime::Content *xmlContent = findContentByType( data, mimetype ) ) {
        return xmlContent->decodedContent();
    }
    Error() << "document not found";
    return QByteArray();
}


QString fromCid(const QString &cid)
{
    if (cid.left(4) != QString::fromLatin1("cid:")) { //Don't set if not a cid, happens when serializing format v2
        return QString();
    }
    return cid.right(cid.size()-4);
}

KMime::Message::Ptr createMessage(const KCalCore::Incidence::Ptr &incidencePtr, const QString &mimetype, const QString &xKolabType, const QByteArray &xml, bool v3, const QString &productId)
{
    KMime::Message::Ptr message = createMessage( xKolabType, v3, productId );
    if (!incidencePtr) {
        Error() << "invalid incidence passed  in";
        message->assemble();
        return message;
    }
    if ( incidencePtr->organizer() && !incidencePtr->organizer()->email().isEmpty()) {
        message->from()->addAddress( incidencePtr->organizer()->email().toUtf8(), incidencePtr->organizer()->name() );
    }
    message->subject()->fromUnicodeString( incidencePtr->uid(), "utf-8" );
    
    KMime::Content *content = createMainPart( mimetype, xml );
    message->addContent( content );
    
    Q_FOREACH (KCalCore::Attachment::Ptr attachment, incidencePtr->attachments()) {
        //Serialize the attachment as attachment with uri, referencing the created mime-part
        if (v3 && !attachment->uri().contains("cid:")) {
            //onyl by url, skip
            continue;
        }
        message->addContent( createAttachmentPart(fromCid(attachment->uri()).toLatin1(), attachment->mimeType(), attachment->label(), attachment->decodedData() ) );
    }
    
    message->assemble();
    return message;
}

KMime::Message::Ptr createMessage(const KABC::Addressee &contact, const QString &mimetype, const QString &xKolabType, const QByteArray &xml, bool v3, const QString &prodid)
{
    KMime::Message::Ptr message = Mime::createMessage( xKolabType, v3, prodid );
    message->subject()->fromUnicodeString( contact.uid(), "utf-8" );
    message->from()->fromUnicodeString( contact.fullEmail(), "utf-8" );
    
    KMime::Content* content = Mime::createMainPart( mimetype, xml );
    message->addContent( content );

// TODO add pictures as separate mimeparts
//     if ( !contact.picture().isNull() ) {
//         QByteArray pic;
//         QBuffer buffer(&pic);
//         buffer.open(QIODevice::WriteOnly);
//         contact.picture().save(&buffer, "PNG");
//         buffer.close();
//         
//         content = Mime::createAttachmentPart(QByteArray(), "image/png", "kolab-picture.png", pic );
//         message->addContent(content);
//     }
//     
//     if ( !contact.logo().isNull() ) {
//         QByteArray pic;
//         QBuffer buffer(&pic);
//         buffer.open(QIODevice::WriteOnly);
//         contact.logo().save(&buffer, "PNG");
//         buffer.close();
//         
//         content = Mime::createAttachmentPart(QByteArray(), "image/png", "kolab-logo.png", pic );
//         message->addContent(content);
//     }
//     
//     if ( !contact.sound().isEmpty() ) {
//         content = Mime::createAttachmentPart(QByteArray(), "audio/unknown", "sound", contact.sound() );
//         message->addContent(content);
//     }
    
    message->assemble();
    return message;
}

KMime::Message::Ptr createMessage(const QString &subject, const QString &mimetype, const QString &xKolabType, const QByteArray &xml, bool v3, const QString &prodid)
{
    KMime::Message::Ptr message = createMessage( xKolabType, v3, prodid );
    if (!subject.isEmpty()) {
        message->subject()->fromUnicodeString( subject, "utf-8" );
    }
    
    KMime::Content *content = createMainPart( mimetype, xml );
    message->addContent( content );
    
    message->assemble();
    return message;
}

KMime::Content* createExplanationPart(bool v3)
{
    KMime::Content *content = new KMime::Content();
    content->contentType()->setMimeType( "text/plain" );
    content->contentType()->setCharset( "us-ascii" );
    content->contentTransferEncoding()->setEncoding( KMime::Headers::CE7Bit );
    if (v3) {
        content->setBody( "This is a Kolab Groupware object.\n"
        "To view this object you will need an email client that can understand the Kolab Groupware format.\n"
        "For a list of such email clients please visit\n"
        "http://www.kolab.org/get-kolab\n" );
    } else {
        content->setBody( "This is a Kolab Groupware object.\n"
        "To view this object you will need an email client that can understand the Kolab Groupware format.\n"
        "For a list of such email clients please visit\n"
        "http://www.kolab.org/get-kolab\n" );
    }
    return content;
}


KMime::Message::Ptr createMessage(const QString& xKolabType, bool v3, const QString &prodid)
{
    KMime::Message::Ptr message( new KMime::Message );
    message->date()->setDateTime( KDateTime::currentUtcDateTime() );
    KMime::Headers::Generic *h = new KMime::Headers::Generic( X_KOLAB_TYPE_HEADER, message.get(), xKolabType, "utf-8" );
    message->appendHeader( h );
    if (v3) {
        KMime::Headers::Generic *vh = new KMime::Headers::Generic( X_KOLAB_MIME_VERSION_HEADER, message.get(), KOLAB_VERSION_V3, "utf-8" );
        message->appendHeader( vh );
    }
    message->userAgent()->from7BitString( prodid.toLatin1() );
    message->contentType()->setMimeType( "multipart/mixed" );
    message->contentType()->setBoundary( KMime::multiPartBoundary() );
    
    message->addContent( createExplanationPart(v3) );
    return message;
}


KMime::Content* createMainPart(const QString& mimeType, const QByteArray& decodedContent)
{
    KMime::Content* content = new KMime::Content();
    content->contentType()->setMimeType( mimeType.toLatin1() );
    content->contentType()->setName( KOLAB_OBJECT_FILENAME, "us-ascii" );
    content->contentTransferEncoding()->setEncoding( KMime::Headers::CEquPr );
    content->contentDisposition()->setDisposition( KMime::Headers::CDattachment );
    content->contentDisposition()->setFilename( KOLAB_OBJECT_FILENAME );
    content->setBody( decodedContent );
    return content;
}

KMime::Content* createAttachmentPart(const QByteArray& cid, const QString& mimeType, const QString& fileName, const QByteArray& decodedContent)
{
    KMime::Content* content = new KMime::Content();
    if (!cid.isEmpty()) {
        content->contentID()->setIdentifier( cid );
    }
    content->contentType()->setMimeType( mimeType.toLatin1() );
    content->contentType()->setName( fileName, "utf-8" );
    content->contentTransferEncoding()->setEncoding( KMime::Headers::CEbase64 );
    content->contentDisposition()->setDisposition( KMime::Headers::CDattachment );
    content->contentDisposition()->setFilename( fileName );
    content->setBody( decodedContent );
    return content;
}

void getAttachments(KCalCore::Incidence::Ptr incidence, const QStringList &attachments, const KMime::Message::Ptr &mimeData)
{
    if (!incidence) {
        Error() << "Invalid incidence";
        return;
    }
//     kDebug() << "getting " << attachments.size() << "attachments";
//     kDebug() << mimeData->encodedContent();
    foreach (const QString &name, attachments) {
        QByteArray type;
        KMime::Content *content = findContentByName(mimeData, name, type);
        if (!content) { // guard against malformed events with non-existent attachments
            Warning() << "could not find attachment: "<< name.toUtf8() << type;
            continue;
        }
        const QByteArray c = content->decodedContent().toBase64();
//         Debug() << c;
        KCalCore::Attachment::Ptr attachment( new KCalCore::Attachment( c, QString::fromLatin1( type ) ) );
        attachment->setLabel( name );
        incidence->addAttachment(attachment);
        Debug() << "ATTACHMENT NAME" << name << type;
    }
}

void getAttachmentsById(KCalCore::Incidence::Ptr incidence, const KMime::Message::Ptr &mimeData)
{
    if (!incidence) {
        Error() << "Invalid incidence";
        return;
    }
//     kDebug() << "getting " << attachments.size() << "attachments";
//     kDebug() << mimeData->encodedContent();

    foreach(KCalCore::Attachment::Ptr attachment, incidence->attachments()) {
        Debug() << attachment->uri();
        if (!attachment->uri().contains("cid:")) {
            continue;
        }
        //It's a referenced attachmant, extract it
        QByteArray type;
        QString name;
        KMime::Content *content = findContentById(mimeData, fromCid(attachment->uri()).toLatin1(), type, name);
        if (!content) { // guard against malformed events with non-existent attachments
            Error() << "could not find attachment: "<< name << type;
            continue;
        }
        attachment->setUri(QString());
        attachment->setData(content->decodedContent().toBase64());
        attachment->setMimeType(type);
        attachment->setLabel(name);
    }
}



}; //Namespace
}; //Namespace