grantleecontactformatter.cpp 22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
  This file is part of KAddressBook.

  Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library 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 Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include "grantleecontactformatter.h"
#include "grantleetheme/grantleetheme.h"
#include "grantleecontactutils.h"
25
26
27
28
29
30
31
#include "../contactobject/contactgrantleeaddressobject.h"
#include "../contactobject/contactgrantleephoneobject.h"
#include "../contactobject/contactgrantleeimobject.h"
#include "../contactobject/contactgrantleegeoobject.h"
#include "../contactobject/contactgrantleecryptoobject.h"
#include "../contactobject/contactgrantleewebsite.h"
#include "../contactobject/contactgrantleeemail.h"
32

Laurent Montel's avatar
Laurent Montel committed
33
#include <KContacts/Addressee>
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <grantlee/context.h>
#include <grantlee/engine.h>
#include <grantlee/templateloader.h>

#include <AkonadiCore/Item>

#include <KColorScheme>

#include <KIconLoader>
#include <KStringHandler>
#include <KConfigGroup>
#include <KLocalizedString>

Laurent Montel's avatar
Laurent Montel committed
47
#include <contacteditor/improtocols.h>
48
49
50
51
52
53
54
55
56
57
58

#include <QSet>
#include <QLocale>
#include <QRegularExpression>

using namespace KAddressBookGrantlee;

class Q_DECL_HIDDEN GrantleeContactFormatter::Private
{
public:
    Private()
Laurent Montel's avatar
Laurent Montel committed
59
60
        : forceDisableQRCode(false)
        , showQRCode(true)
61
62
63
64
65
66
67
68
69
70
71
72
73
    {
        KConfig config(QStringLiteral("akonadi_contactrc"));
        KConfigGroup group(&config, QStringLiteral("View"));
        showQRCode = group.readEntry("QRCodes", true);

        mEngine = new Grantlee::Engine;

        mTemplateLoader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
    }

    ~Private()
    {
        delete mEngine;
Laurent Montel's avatar
Laurent Montel committed
74
        mTemplateLoader.clear();
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    }

    void changeGrantleePath(const QString &path)
    {
        mTemplateLoader->setTemplateDirs(QStringList() << path);
        mEngine->addTemplateLoader(mTemplateLoader);

        mSelfcontainedTemplate = mEngine->loadByName(QStringLiteral("contact.html"));
        if (mSelfcontainedTemplate->error()) {
            mErrorMessage += mSelfcontainedTemplate->errorString() + QStringLiteral("<br>");
        }

        mEmbeddableTemplate = mEngine->loadByName(QStringLiteral("contact_embedded.html"));
        if (mEmbeddableTemplate->error()) {
            mErrorMessage += mEmbeddableTemplate->errorString() + QStringLiteral("<br>");
        }
    }

    QVector<QObject *> mObjects;
Laurent Montel's avatar
Laurent Montel committed
94
    Grantlee::Engine *mEngine = nullptr;
95
96
97
98
    QSharedPointer<Grantlee::FileSystemTemplateLoader> mTemplateLoader;
    Grantlee::Template mSelfcontainedTemplate;
    Grantlee::Template mEmbeddableTemplate;
    QString mErrorMessage;
Laurent Montel's avatar
Laurent Montel committed
99
100
    bool forceDisableQRCode = false;
    bool showQRCode = false;
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
};

GrantleeContactFormatter::GrantleeContactFormatter()
    : d(new Private)
{
}

GrantleeContactFormatter::~GrantleeContactFormatter()
{
    delete d;
}

void GrantleeContactFormatter::setAbsoluteThemePath(const QString &path)
{
    d->changeGrantleePath(path);
}

void GrantleeContactFormatter::setGrantleeTheme(const GrantleeTheme::Theme &theme)
{
    d->changeGrantleePath(theme.absolutePath());
}

void GrantleeContactFormatter::setForceDisableQRCode(bool b)
{
    if (d->forceDisableQRCode != b) {
        d->forceDisableQRCode = b;
    }
}

bool GrantleeContactFormatter::forceDisableQRCode() const
{
    return d->forceDisableQRCode;
}

void GrantleeContactFormatter::setShowQRCode(bool b)
{
    d->showQRCode = b;
}

inline static void setHashField(QVariantHash &hash, const QString &name, const QString &value)
{
    if (!value.isEmpty()) {
        hash.insert(name, value);
    }
}

static QVariantHash phoneNumberHash(const KContacts::PhoneNumber &phoneNumber, int counter)
{
    QVariantHash numberObject;

    setHashField(numberObject, QStringLiteral("type"), phoneNumber.typeLabel());
    setHashField(numberObject, QStringLiteral("number"), phoneNumber.number());

    if (!phoneNumber.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
155
156
157
158
        const QString url
            = QStringLiteral("<a href=\"phone:?index=%1\">%2</a>").
              arg(counter).
              arg(phoneNumber.number());
159
160
161
        numberObject.insert(QStringLiteral("numberLink"), url);

        if (phoneNumber.type() & KContacts::PhoneNumber::Cell) {
Laurent Montel's avatar
Laurent Montel committed
162
163
            const QString url
                = QStringLiteral("<a href=\"sms:?index=%1\"><img src=\"sms_icon\" align=\"top\"/></a>").arg(counter);
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
            numberObject.insert(QStringLiteral("smsLink"), url);
        }
    }

    return numberObject;
}

static QVariantHash imAddressHash(const QString &typeKey, const QString &imAddress)
{
    QVariantHash addressObject;

    const QString dispLabel = i18nc("@title:row label for an Instant Messaging address, %1 is I18Ned protocol name",
                                    "IM (%1)", IMProtocols::self()->name(typeKey));

    setHashField(addressObject, QStringLiteral("type"), dispLabel);
    setHashField(addressObject, QStringLiteral("imAddress"), imAddress);

    const QString iconUrl = QUrl::fromLocalFile(KIconLoader::global()->iconPath(IMProtocols::self()->icon(typeKey),
Laurent Montel's avatar
Laurent Montel committed
182
                                                                                -KIconLoader::SizeSmall)).url();
183
    const QString url = QStringLiteral("<img src=\"%1\" align=\"top\" height=\"%2\" width=\"%2\"/>").arg(iconUrl, QString::number(KIconLoader::SizeSmall));
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
    addressObject.insert(QStringLiteral("imIcon"), url);

    return addressObject;
}

static QVariantHash addressHash(const KContacts::Address &address, int counter)
{
    QVariantHash addressObject;

    setHashField(addressObject, QStringLiteral("type"),
                 KContacts::Address::typeLabel(address.type()));

    setHashField(addressObject, QStringLiteral("street"), address.street());

    setHashField(addressObject, QStringLiteral("postOfficeBox"), address.postOfficeBox());

    setHashField(addressObject, QStringLiteral("locality"), address.locality());

    setHashField(addressObject, QStringLiteral("region"), address.region());

    setHashField(addressObject, QStringLiteral("postalCode"), address.postalCode());

    setHashField(addressObject, QStringLiteral("country"), address.country());

    setHashField(addressObject, QStringLiteral("label"), address.label());

    setHashField(addressObject, QStringLiteral("formattedAddress"), address.formattedAddress());

    QString formattedAddress;

    if (address.label().isEmpty()) {
        formattedAddress = address.formattedAddress().trimmed();
    } else {
        formattedAddress = address.label();
    }

    if (!formattedAddress.isEmpty()) {
        //Replace all \n by only one <br/>
        formattedAddress = formattedAddress.replace(QRegularExpression(QStringLiteral("\n+")), QStringLiteral("<br/>"));

        const QString link = QStringLiteral("<a href=\"address:?index=%1\">%2</a>").
                             arg(counter);
        QString url = link.arg(formattedAddress);
        addressObject.insert(QStringLiteral("formattedAddressLink"), url);

        url = link.arg(QStringLiteral("<img src=\"map_icon\" align=\"top\"/>"));
        addressObject.insert(QStringLiteral("formattedAddressIcon"), url);
    }

    return addressObject;
}

static int contactAge(const QDate &date)
{
    QDate now = QDate::currentDate();
    int age = now.year() - date.year();
    if (date > now.addYears(-age)) {
        age--;
    }
    return age;
}

QString GrantleeContactFormatter::toHtml(HtmlForm form) const
{
    if (!d->mErrorMessage.isEmpty()) {
        return d->mErrorMessage;
    }

    KContacts::Addressee rawContact;
    const Akonadi::Item localItem = item();
    if (localItem.isValid() && localItem.hasPayload<KContacts::Addressee>()) {
        rawContact = localItem.payload<KContacts::Addressee>();
    } else {
        rawContact = contact();
    }

    if (rawContact.isEmpty()) {
        return QString();
    }

    QVariantHash contactObject;
    GrantleeContactUtils grantleeContactUtil;
    // Name parts
    setHashField(contactObject, QStringLiteral("name"), rawContact.realName());
    setHashField(contactObject, QStringLiteral("formattedName"), rawContact.formattedName());
    setHashField(contactObject, QStringLiteral("prefix"), rawContact.prefix());
    setHashField(contactObject, QStringLiteral("givenName"), rawContact.givenName());
    setHashField(contactObject, QStringLiteral("additionalName"), rawContact.additionalName());
    setHashField(contactObject, QStringLiteral("familyName"), rawContact.familyName());
    setHashField(contactObject, QStringLiteral("suffix"), rawContact.suffix());
    setHashField(contactObject, QStringLiteral("nickName"), rawContact.nickName());

    // Dates
    const QDate birthday = rawContact.birthday().date();
    if (birthday.isValid()) {
        grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("birthdayi18n"));

        const QString formattedBirthday = QLocale().toString(birthday);
        contactObject.insert(QStringLiteral("birthday"), formattedBirthday);

        const int years = contactAge(birthday);
        contactObject.insert(QStringLiteral("age"), QString::number(years));
Laurent Montel's avatar
Laurent Montel committed
286
287
288
        contactObject.insert(QStringLiteral("birthdayage"), QString(formattedBirthday
                                                                    +QStringLiteral("&nbsp;&nbsp;")
                                                                    +i18np("(One year old)", "(%1 years old)", years)));
289
290
    }

Laurent Montel's avatar
Laurent Montel committed
291
292
293
    const QDate anniversary
        = QDate::fromString(rawContact.custom(QStringLiteral("KADDRESSBOOK"),
                                              QStringLiteral("X-Anniversary")), Qt::ISODate);
294
295
296
297
298
299
300
301
    if (anniversary.isValid()) {
        contactObject.insert(QStringLiteral("anniversary"),
                             QLocale().toString(anniversary));
        grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("anniversaryi18n"));
    }

    // Emails
    QStringList emails;
Laurent Montel's avatar
Laurent Montel committed
302
303
    const QStringList emailsList = rawContact.emails();
    for (const QString &email : emailsList) {
304
305
306
307
308
309
310
311
312
313
314
315
        const QString fullEmail = QString::fromLatin1(QUrl::toPercentEncoding(rawContact.fullEmail(email)));

        const QString url = QStringLiteral("<a href=\"mailto:%1\">%2</a>")
                            .arg(fullEmail, email);
        emails << url;
    }
    grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("emailsi18n"));
    contactObject.insert(QStringLiteral("emails"), emails);

    // Phone numbers
    QVariantList phoneNumbers;
    int counter = 0;
Laurent Montel's avatar
Laurent Montel committed
316
    const KContacts::PhoneNumber::List lstPhone = rawContact.phoneNumbers();
Laurent Montel's avatar
Laurent Montel committed
317
    phoneNumbers.reserve(lstPhone.count());
Laurent Montel's avatar
Laurent Montel committed
318
    for (const KContacts::PhoneNumber &phoneNumber : lstPhone) {
319
320
321
322
323
324
325
326
327
        phoneNumbers.append(phoneNumberHash(phoneNumber, counter));
        counter++;
    }

    contactObject.insert(QStringLiteral("phoneNumbers"), phoneNumbers);

    // IM
    QVariantList imAddresses;
    const QStringList customs = rawContact.customs();
Laurent Montel's avatar
Laurent Montel committed
328
    for (const QString &custom : customs) {
Laurent Montel's avatar
Laurent Montel committed
329
        if (custom.startsWith(QLatin1String("messaging/"))) {
Laurent Montel's avatar
Laurent Montel committed
330
331
332
333
334
335
            int pos = custom.indexOf(QLatin1Char(':'));
            QString key = custom.left(pos);
            key.remove(QStringLiteral("-All"));
            const QString value = custom.mid(pos + 1);

            imAddresses.append(imAddressHash(key, value));
336
337
338
339
340
341
        }
    }

    contactObject.insert(QStringLiteral("imAddresses"), imAddresses);

    // Homepage
Laurent Montel's avatar
Laurent Montel committed
342
    QVariantList websites;
Laurent Montel's avatar
Laurent Montel committed
343
344
    const KContacts::ResourceLocatorUrl::List extraUrlList = rawContact.extraUrlList();
    for (const KContacts::ResourceLocatorUrl &resourceLocator : extraUrlList) {
Laurent Montel's avatar
Laurent Montel committed
345
        QString url = resourceLocator.url().url();
Laurent Montel's avatar
Laurent Montel committed
346
347
        if (!url.startsWith(QLatin1String("http://"))
            && !url.startsWith(QLatin1String("https://"))) {
348
349
            url = QStringLiteral("http://") + url;
        }
Laurent Montel's avatar
Laurent Montel committed
350
351
352
353
        websites.append(KStringHandler::tagUrls(url));
    }
    if (!websites.isEmpty()) {
        contactObject.insert(QStringLiteral("websites"), websites);
354
355
356
357
        grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("websitei18n"));
    }

    // Blog Feed
Laurent Montel's avatar
Laurent Montel committed
358
359
    const QString blog
        = rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("BlogFeed"));
360
361
362
363
364
365
    if (!blog.isEmpty()) {
        contactObject.insert(QStringLiteral("blogUrl"), KStringHandler::tagUrls(blog));
        grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("blogUrli18n"));
    }

    // Address Book
Laurent Montel's avatar
Laurent Montel committed
366
367
    const QString addressBookName
        = rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("AddressBook"));
368
369
370
371
372
373
374
375
    if (!addressBookName.isEmpty()) {
        contactObject.insert(QStringLiteral("addressBookName"), addressBookName);
        grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("addressBookNamei18n"));
    }

    // Addresses
    QVariantList addresses;
    counter = 0;
Laurent Montel's avatar
Laurent Montel committed
376
    const KContacts::Address::List lstAddresses = rawContact.addresses();
Laurent Montel's avatar
Laurent Montel committed
377
    addresses.reserve(lstAddresses.count());
Laurent Montel's avatar
Laurent Montel committed
378
    for (const KContacts::Address &address : lstAddresses) {
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
        addresses.append(addressHash(address, counter));
        counter++;
    }
    // Note
    if (!rawContact.note().isEmpty()) {
        const QString notes = QStringLiteral("<a>%1</a>").arg(rawContact.note().replace(QLatin1Char('\n'), QStringLiteral("<br>")));
        contactObject.insert(QStringLiteral("note"), notes);
        grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("notei18n"));
    }

    contactObject.insert(QStringLiteral("addresses"), addresses);

    setHashField(contactObject, QStringLiteral("mailer"), rawContact.mailer());

    setHashField(contactObject, QStringLiteral("title"), rawContact.title());

    setHashField(contactObject, QStringLiteral("role"), rawContact.role());

    QString job = rawContact.title();
    if (job.isEmpty()) {
        job = rawContact.role();
    }
    if (job.isEmpty()) {
        job = rawContact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession"));
    }
    setHashField(contactObject, QStringLiteral("job"), job);

    setHashField(contactObject, QStringLiteral("organization"), rawContact.organization());

    setHashField(contactObject, QStringLiteral("department"), rawContact.department());
    grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("departmenti18n"));

    //setHashField( contactObject, QStringLiteral( "note" ), rawContact.note() );

    setHashField(contactObject, QStringLiteral("profession"),
                 rawContact.custom(QStringLiteral("KADDRESSBOOK"),
                                   QStringLiteral("X-Profession")));
    grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("Professioni18n"));
    setHashField(contactObject, QStringLiteral("office"),
                 rawContact.custom(QStringLiteral("KADDRESSBOOK"),
                                   QStringLiteral("X-Office")));
    grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("officei18n"));

    setHashField(contactObject, QStringLiteral("manager"),
                 rawContact.custom(QStringLiteral("KADDRESSBOOK"),
                                   QStringLiteral("X-ManagersName")));
    grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("manageri18n"));

    setHashField(contactObject, QStringLiteral("assistant"),
                 rawContact.custom(QStringLiteral("KADDRESSBOOK"),
                                   QStringLiteral("X-AssistantsName")));
    grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("assistanti18n"));

    setHashField(contactObject, QStringLiteral("spouse"),
                 rawContact.custom(QStringLiteral("KADDRESSBOOK"),
                                   QStringLiteral("X-SpousesName")));
    grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("spousei18n"));

    setHashField(contactObject, QStringLiteral("imAddress"),
                 rawContact.custom(QStringLiteral("KADDRESSBOOK"),
                                   QStringLiteral("X-IMAddress")));
    grantleeContactUtil.insertVariableToQVariantHash(contactObject, QStringLiteral("imAddressi18n"));

    // Custom fields

    QVariantList customFields;
    QVariantList customFieldsUrl;
    static QSet<QString> blacklistedKeys;
    if (blacklistedKeys.isEmpty()) {
        blacklistedKeys.insert(QStringLiteral("CRYPTOPROTOPREF"));
        blacklistedKeys.insert(QStringLiteral("OPENPGPFP"));
        blacklistedKeys.insert(QStringLiteral("SMIMEFP"));
        blacklistedKeys.insert(QStringLiteral("CRYPTOSIGNPREF"));
        blacklistedKeys.insert(QStringLiteral("CRYPTOENCRYPTPREF"));
        blacklistedKeys.insert(QStringLiteral("Anniversary"));
        blacklistedKeys.insert(QStringLiteral("BlogFeed"));
        blacklistedKeys.insert(QStringLiteral("Profession"));
        blacklistedKeys.insert(QStringLiteral("Office"));
        blacklistedKeys.insert(QStringLiteral("ManagersName"));
        blacklistedKeys.insert(QStringLiteral("AssistantsName"));
        blacklistedKeys.insert(QStringLiteral("SpousesName"));
        blacklistedKeys.insert(QStringLiteral("IMAddress"));
        blacklistedKeys.insert(QStringLiteral("AddressBook"));
        blacklistedKeys.insert(QStringLiteral("MailPreferedFormatting"));
        blacklistedKeys.insert(QStringLiteral("MailAllowToRemoteContent"));
Laurent Montel's avatar
Laurent Montel committed
464
465
        blacklistedKeys.insert(QStringLiteral("MAILPREFEREDFORMATTING"));
        blacklistedKeys.insert(QStringLiteral("MAILALLOWTOREMOTECONTENT"));
466
467
468
    }

    if (!customs.empty()) {
Laurent Montel's avatar
Laurent Montel committed
469
        for (QString custom : qAsConst(customs)) {   //krazy:exclude=foreach
Laurent Montel's avatar
Laurent Montel committed
470
            if (custom.startsWith(QLatin1String("KADDRESSBOOK-"))) {
471
472
473
474
475
476
477
478
479
480
481
482
483
                custom.remove(QStringLiteral("KADDRESSBOOK-X-"));
                custom.remove(QStringLiteral("KADDRESSBOOK-"));

                int pos = custom.indexOf(QLatin1Char(':'));
                QString key = custom.left(pos);
                QString value = custom.mid(pos + 1);

                if (blacklistedKeys.contains(key)) {
                    continue;
                }

                bool addUrl = false;
                // check whether it is a custom local field
Laurent Montel's avatar
Laurent Montel committed
484
485
                for (int i = 0; i < customFieldDescriptions().size(); ++i) {
                    const QVariantMap description = customFieldDescriptions().at(i);
486
487
488
489
490
491
492
493
494
                    if (description.value(QStringLiteral("key")).toString() == key) {
                        key = description.value(QStringLiteral("title")).toString();
                        const QString descriptionType = description.value(QStringLiteral("type")).toString();
                        if (descriptionType == QLatin1String("boolean")) {
                            if (value == QLatin1String("true")) {
                                value = i18nc("Boolean value", "yes");
                            } else {
                                value = i18nc("Boolean value", "no");
                            }
Laurent Montel's avatar
Laurent Montel committed
495
                        } else if (descriptionType == QLatin1String("date")) {
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
                            const QDate date = QDate::fromString(value, Qt::ISODate);
                            value = QLocale().toString(date, QLocale::ShortFormat);
                        } else if (descriptionType == QLatin1String("time")) {
                            const QTime time = QTime::fromString(value, Qt::ISODate);
                            value = QLocale::system().toString(time, QLocale::ShortFormat);
                        } else if (descriptionType == QLatin1String("datetime")) {
                            const QDateTime dateTime = QDateTime::fromString(value, Qt::ISODate);
                            value = QLocale().toString(dateTime, QLocale::ShortFormat);
                        } else if (descriptionType == QLatin1String("url")) {
                            value = KStringHandler::tagUrls(value.toHtmlEscaped());
                            addUrl = true;
                        }
                        break;
                    }
                }
                QVariantHash customFieldObject;
                customFieldObject.insert(QStringLiteral("title"), key);
                customFieldObject.insert(QStringLiteral("value"), value);

                if (addUrl) {
                    customFieldsUrl.append(customFieldObject);
                } else {
                    customFields.append(customFieldObject);
                }
            }
        }
    }

    contactObject.insert(QStringLiteral("customFields"), customFields);
    contactObject.insert(QStringLiteral("customFieldsUrl"), customFieldsUrl);

    if (!d->forceDisableQRCode) {
        if (d->showQRCode) {
            contactObject.insert(QStringLiteral("hasqrcode"), QStringLiteral("true"));
        }
    }

    QVariantHash colorsObject;

    colorsObject.insert(
        QStringLiteral("linkColor"),
        KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name());

    colorsObject.insert(
        QStringLiteral("textColor"),
        KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name());

    colorsObject.insert(
        QStringLiteral("backgroundColor"),
        KColorScheme(QPalette::Active, KColorScheme::View).background().color().name());

    QVariantHash mapping;
    mapping.insert(QStringLiteral("contact"), contactObject);
    mapping.insert(QStringLiteral("colors"), colorsObject);

    Grantlee::Context context(mapping);

    if (form == SelfcontainedForm) {
        return d->mSelfcontainedTemplate->render(&context);
    } else if (form == EmbeddableForm) {
        return d->mEmbeddableTemplate->render(&context);
    } else {
        return QString();
    }
}