File: kolabbase.cpp

package info (click to toggle)
kdepim-runtime 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,012 kB
  • sloc: cpp: 90,562; xml: 1,020; javascript: 60; sh: 58; makefile: 13
file content (487 lines) | stat: -rw-r--r-- 13,923 bytes parent folder | download
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
    This file is part of the kolab resource - the implementation of the
    Kolab storage format. See www.kolab.org for documentation on this.

    SPDX-FileCopyrightText: 2004 Bo Thorsen <bo@sonofthor.dk>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "kolabbase.h"
#include "pimkolab_debug.h"

#include <KContacts/Addressee>
#include <KContacts/ContactGroup>

using namespace KolabV2;

KolabBase::KolabBase(const QString &tz)
    : mCreationDate(QDateTime::currentDateTime())
    , mLastModified(QDateTime::currentDateTimeUtc())
    , mSensitivity(Public)
{
    // unlike the previously used KTimeZone here, QTimeZone defaults to local time zone if tz.isEmpty()
    // we therefore force it to invalid, which however is unsafe to use (unlike KTimeZone)
    // therefore all usage of mTimeZone must be preceded by a validity check
    mTimeZone = tz.isEmpty() ? QTimeZone() : QTimeZone(tz.toUtf8());
}

KolabBase::~KolabBase() = default;

void KolabBase::setFields(const KCalendarCore::Incidence::Ptr &incidence)
{
    // So far unhandled KCalendarCore::IncidenceBase fields:
    // mPilotID, mSyncStatus, mFloats

    setUid(incidence->uid());
    setBody(incidence->description());
    setCategories(incidence->categoriesStr());
    setCreationDate(localToUTC(incidence->created()));
    setLastModified(incidence->lastModified());
    setSensitivity(static_cast<Sensitivity>(incidence->secrecy()));
    // TODO: Attachments
}

void KolabBase::saveTo(const KCalendarCore::Incidence::Ptr &incidence) const
{
    incidence->setUid(uid());
    incidence->setDescription(body());
    incidence->setCategories(categories());
    incidence->setCreated(utcToLocal(creationDate()));
    incidence->setLastModified(lastModified());
    switch (sensitivity()) {
    case 1:
        incidence->setSecrecy(KCalendarCore::Incidence::SecrecyPrivate);
        break;
    case 2:
        incidence->setSecrecy(KCalendarCore::Incidence::SecrecyConfidential);
        break;
    default:
        incidence->setSecrecy(KCalendarCore::Incidence::SecrecyPublic);
        break;
    }

    // TODO: Attachments
}

void KolabBase::setFields(const KContacts::Addressee *addressee)
{
    // An addressee does not have a creation date, so somehow we should
    // make one, if this is a new entry

    setUid(addressee->uid());
    setBody(addressee->note());
    setCategories(addressee->categories().join(QLatin1Char(',')));

    // Set creation-time and last-modification-time
    const QString creationString = addressee->custom(QStringLiteral("KOLAB"), QStringLiteral("CreationDate"));
    qCDebug(PIMKOLAB_LOG) << "Creation time string:" << creationString;
    QDateTime creationDate;
    if (creationString.isEmpty() && mTimeZone.isValid()) {
        creationDate = QDateTime::currentDateTime().toTimeZone(mTimeZone);
        qCDebug(PIMKOLAB_LOG) << "Creation date set to current time" << mTimeZone;
    } else {
        creationDate = stringToDateTime(creationString);
        qCDebug(PIMKOLAB_LOG) << "Creation date loaded";
    }
    QDateTime modified;
    if (mTimeZone.isValid()) {
        modified = addressee->revision().toTimeZone(mTimeZone);
    }
    if (!modified.isValid()) {
        modified = QDateTime::currentDateTimeUtc();
    }
    setLastModified(modified);
    if (modified < creationDate) {
        // It's not possible that the modification date is earlier than creation
        creationDate = modified;
        qCDebug(PIMKOLAB_LOG) << "Creation date set to modification date";
    }
    setCreationDate(creationDate);
    const QString newCreationDate = dateTimeToString(creationDate);
    if (creationString != newCreationDate) {
        // We modified the creation date, so store it for future reference
        const_cast<KContacts::Addressee *>(addressee)->insertCustom(QStringLiteral("KOLAB"), QStringLiteral("CreationDate"), newCreationDate);
        qCDebug(PIMKOLAB_LOG) << "Creation date modified. New one:" << newCreationDate;
    }

    switch (addressee->secrecy().type()) {
    case KContacts::Secrecy::Private:
        setSensitivity(Private);
        break;
    case KContacts::Secrecy::Confidential:
        setSensitivity(Confidential);
        break;
    default:
        setSensitivity(Public);
    }

    // TODO: Attachments
}

void KolabBase::saveTo(KContacts::Addressee *addressee) const
{
    addressee->setUid(uid());
    addressee->setNote(body());
    addressee->setCategories(categories().split(QLatin1Char(','), Qt::SkipEmptyParts));
    if (mTimeZone.isValid()) {
        addressee->setRevision(lastModified().toTimeZone(mTimeZone));
    }
    addressee->insertCustom(QStringLiteral("KOLAB"), QStringLiteral("CreationDate"), dateTimeToString(creationDate()));

    switch (sensitivity()) {
    case Private:
        addressee->setSecrecy(KContacts::Secrecy(KContacts::Secrecy::Private));
        break;
    case Confidential:
        addressee->setSecrecy(KContacts::Secrecy(KContacts::Secrecy::Confidential));
        break;
    default:
        addressee->setSecrecy(KContacts::Secrecy(KContacts::Secrecy::Public));
        break;
    }
    // TODO: Attachments
}

void KolabBase::setFields(const KContacts::ContactGroup *contactGroup)
{
    // A contactgroup does not have a creation date, so somehow we should
    // make one, if this is a new entry

    setUid(contactGroup->id());

    // Set creation-time and last-modification-time
    QDateTime creationDate;
    if (mTimeZone.isValid()) {
        creationDate = QDateTime::currentDateTime().toTimeZone(mTimeZone);
    }
    qCDebug(PIMKOLAB_LOG) << "Creation date set to current time";

    QDateTime modified = QDateTime::currentDateTimeUtc();
    setLastModified(modified);
    if (modified < creationDate) {
        // It's not possible that the modification date is earlier than creation
        creationDate = modified;
        qCDebug(PIMKOLAB_LOG) << "Creation date set to modification date";
    }
    setCreationDate(creationDate);
}

void KolabBase::saveTo(KContacts::ContactGroup *contactGroup) const
{
    contactGroup->setId(uid());
}

void KolabBase::setUid(const QString &uid)
{
    mUid = uid;
}

QString KolabBase::uid() const
{
    return mUid;
}

void KolabBase::setBody(const QString &body)
{
    mBody = body;
}

QString KolabBase::body() const
{
    return mBody;
}

void KolabBase::setCategories(const QString &categories)
{
    mCategories = categories;
}

QString KolabBase::categories() const
{
    return mCategories;
}

void KolabBase::setCreationDate(const QDateTime &date)
{
    mCreationDate = date;
}

QDateTime KolabBase::creationDate() const
{
    return mCreationDate;
}

void KolabBase::setLastModified(const QDateTime &date)
{
    mLastModified = date;
}

QDateTime KolabBase::lastModified() const
{
    return mLastModified;
}

void KolabBase::setSensitivity(Sensitivity sensitivity)
{
    mSensitivity = sensitivity;
}

KolabBase::Sensitivity KolabBase::sensitivity() const
{
    return mSensitivity;
}

void KolabBase::setPilotSyncId(unsigned long id)
{
    mHasPilotSyncId = true;
    mPilotSyncId = id;
}

bool KolabBase::hasPilotSyncId() const
{
    return mHasPilotSyncId;
}

unsigned long KolabBase::pilotSyncId() const
{
    return mPilotSyncId;
}

void KolabBase::setPilotSyncStatus(int status)
{
    mHasPilotSyncStatus = true;
    mPilotSyncStatus = status;
}

bool KolabBase::hasPilotSyncStatus() const
{
    return mHasPilotSyncStatus;
}

int KolabBase::pilotSyncStatus() const
{
    return mPilotSyncStatus;
}

bool KolabBase::loadEmailAttribute(QDomElement &element, Email &email)
{
    for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) {
        if (n.isComment()) {
            continue;
        }
        if (n.isElement()) {
            QDomElement e = n.toElement();
            const QString tagName = e.tagName();

            if (tagName == QLatin1StringView("display-name")) {
                email.displayName = e.text();
            } else if (tagName == QLatin1StringView("smtp-address")) {
                email.smtpAddress = e.text();
            } else {
                // TODO: Unhandled tag - save for later storage
                qCDebug(PIMKOLAB_LOG) << "Warning: Unhandled tag" << e.tagName();
            }
        } else {
            qCDebug(PIMKOLAB_LOG) << "Node is not a comment or an element???";
        }
    }

    return true;
}

void KolabBase::saveEmailAttribute(QDomElement &element, const Email &email, const QString &tagName) const
{
    QDomElement e = element.ownerDocument().createElement(tagName);
    element.appendChild(e);
    writeString(e, QStringLiteral("display-name"), email.displayName);
    writeString(e, QStringLiteral("smtp-address"), email.smtpAddress);
}

bool KolabBase::loadAttribute(QDomElement &element)
{
    const QString tagName = element.tagName();
    switch (tagName[0].toLatin1()) {
    case 'u':
        if (tagName == QLatin1StringView("uid")) {
            setUid(element.text());
            return true;
        }
        break;
    case 'b':
        if (tagName == QLatin1StringView("body")) {
            setBody(element.text());
            return true;
        }
        break;
    case 'c':
        if (tagName == QLatin1StringView("categories")) {
            setCategories(element.text());
            return true;
        }
        if (tagName == QLatin1StringView("creation-date")) {
            setCreationDate(stringToDateTime(element.text()));
            return true;
        }
        break;
    case 'l':
        if (tagName == QLatin1StringView("last-modification-date")) {
            setLastModified(stringToDateTime(element.text()));
            return true;
        }
        break;
    case 's':
        if (tagName == QLatin1StringView("sensitivity")) {
            setSensitivity(stringToSensitivity(element.text()));
            return true;
        }
        break;
    case 'p':
        if (tagName == QLatin1StringView("product-id")) {
            return true; // ignore this field
        }
        if (tagName == QLatin1StringView("pilot-sync-id")) {
            setPilotSyncId(element.text().toULong());
            return true;
        }
        if (tagName == QLatin1StringView("pilot-sync-status")) {
            setPilotSyncStatus(element.text().toInt());
            return true;
        }
        break;
    default:
        break;
    }
    return false;
}

bool KolabBase::saveAttributes(QDomElement &element) const
{
    writeString(element, QStringLiteral("product-id"), productID());
    writeString(element, QStringLiteral("uid"), uid());
    writeString(element, QStringLiteral("body"), body());
    writeString(element, QStringLiteral("categories"), categories());
    writeString(element, QStringLiteral("creation-date"), dateTimeToString(creationDate().toUTC()));
    writeString(element, QStringLiteral("last-modification-date"), dateTimeToString(lastModified().toUTC()));
    writeString(element, QStringLiteral("sensitivity"), sensitivityToString(sensitivity()));
    if (hasPilotSyncId()) {
        writeString(element, QStringLiteral("pilot-sync-id"), QString::number(pilotSyncId()));
    }
    if (hasPilotSyncStatus()) {
        writeString(element, QStringLiteral("pilot-sync-status"), QString::number(pilotSyncStatus()));
    }
    return true;
}

bool KolabBase::load(const QString &xml)
{
    const QDomDocument document = loadDocument(xml);
    if (document.isNull()) {
        return false;
    }
    // XML file loaded into tree. Now parse it
    return loadXML(document);
}

QDomDocument KolabBase::loadDocument(const QString &xmlData)
{
    QString errorMsg;
    int errorLine, errorColumn;
    QDomDocument document;
    bool ok = document.setContent(xmlData, &errorMsg, &errorLine, &errorColumn);

    if (!ok) {
        qWarning("Error loading document: %s, line %d, column %d", qPrintable(errorMsg), errorLine, errorColumn);
        return {};
    }

    return document;
}

QDomDocument KolabBase::domTree()
{
    QDomDocument document;

    const QString p = QStringLiteral("version=\"1.0\" encoding=\"UTF-8\"");
    document.appendChild(document.createProcessingInstruction(QStringLiteral("xml"), p));

    return document;
}

QString KolabBase::dateTimeToString(const QDateTime &time)
{
    return time.toString(Qt::ISODate);
}

QString KolabBase::dateToString(QDate date)
{
    return date.toString(Qt::ISODate);
}

QDateTime KolabBase::stringToDateTime(const QString &time)
{
    return QDateTime::fromString(time, Qt::ISODate);
}

QDate KolabBase::stringToDate(const QString &date)
{
    return QDate::fromString(date, Qt::ISODate);
}

QString KolabBase::sensitivityToString(Sensitivity s)
{
    switch (s) {
    case Private:
        return QStringLiteral("private");
    case Confidential:
        return QStringLiteral("confidential");
    case Public:
        return QStringLiteral("public");
    }

    return QStringLiteral("What what what???");
}

KolabBase::Sensitivity KolabBase::stringToSensitivity(const QString &s)
{
    if (s == QLatin1StringView("private")) {
        return Private;
    }
    if (s == QLatin1StringView("confidential")) {
        return Confidential;
    }
    return Public;
}

QString KolabBase::colorToString(const QColor &color)
{
    // Color is in the format "#RRGGBB"
    return color.name();
}

QColor KolabBase::stringToColor(const QString &s)
{
    return QColor(s);
}

void KolabBase::writeString(QDomElement &element, const QString &tag, const QString &tagString)
{
    if (!tagString.isEmpty()) {
        QDomElement e = element.ownerDocument().createElement(tag);
        QDomText t = element.ownerDocument().createTextNode(tagString);
        e.appendChild(t);
        element.appendChild(e);
    }
}

QDateTime KolabBase::localToUTC(const QDateTime &time) const
{
    return time.toUTC();
}

QDateTime KolabBase::utcToLocal(const QDateTime &time) const
{
    QDateTime dt = time;
    dt.setTimeZone(QTimeZone::utc());
    return dt;
}