1/*
2 This file is part of the KContacts framework.
3 SPDX-FileCopyrightText: 2002 Tobias Koenig <tokoe@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "vcardconverter.h"
9#include "vcardtool_p.h"
10
11using namespace KContacts;
12
13VCardConverter::VCardConverter()
14 : d(nullptr)
15{
16}
17
18VCardConverter::~VCardConverter()
19{
20}
21
22QByteArray VCardConverter::exportVCard(const Addressee &addr, Version version) const
23{
24 Addressee::List list;
25 list.append(t: addr);
26
27 return exportVCards(list, version);
28}
29
30QByteArray VCardConverter::exportVCards(const Addressee::List &list, Version version) const
31{
32 VCardTool tool;
33 QByteArray returnValue;
34 switch (version) {
35 case v2_1:
36 returnValue = tool.exportVCards(list, version: VCard::v2_1);
37 break;
38 case v3_0:
39 returnValue = tool.exportVCards(list, version: VCard::v3_0);
40 break;
41 case v4_0:
42 returnValue = tool.exportVCards(list, version: VCard::v4_0);
43 break;
44 }
45
46 return returnValue;
47}
48
49QByteArray VCardConverter::createVCard(const Addressee &addr, Version version) const
50{
51 Addressee::List list;
52 list.append(t: addr);
53
54 return createVCards(list, version);
55}
56
57QByteArray VCardConverter::createVCards(const Addressee::List &list, Version version) const
58{
59 VCardTool tool;
60 QByteArray returnValue;
61 switch (version) {
62 case v2_1:
63 returnValue = tool.createVCards(list, version: VCard::v2_1);
64 break;
65 case v3_0:
66 returnValue = tool.createVCards(list, version: VCard::v3_0);
67 break;
68 case v4_0:
69 returnValue = tool.createVCards(list, version: VCard::v4_0);
70 break;
71 }
72
73 return returnValue;
74}
75
76Addressee VCardConverter::parseVCard(const QByteArray &vcard) const
77{
78 Addressee::List list = parseVCards(vcard);
79
80 return list.isEmpty() ? Addressee() : list[0];
81}
82
83Addressee::List VCardConverter::parseVCards(const QByteArray &vcard) const
84{
85 VCardTool tool;
86
87 return tool.parseVCards(vcard);
88}
89
90/* Helper functions */
91
92QString KContacts::dateToVCardString(const QDateTime &dateTime)
93{
94 return dateTime.toString(QStringLiteral("yyyyMMddThhmmssZ"));
95}
96
97QString KContacts::dateToVCardString(QDate date)
98{
99 return date.toString(QStringLiteral("yyyyMMdd"));
100}
101
102QDateTime KContacts::VCardStringToDate(const QString &dateString)
103{
104 QDate date;
105 QTime time;
106 QString d(dateString);
107
108 d = d.remove(c: QLatin1Char('-')).remove(c: QLatin1Char(':'));
109
110 if (d.length() >= 8) {
111 const QStringView strView(d);
112 date = QDate(strView.mid(pos: 0, n: 4).toUInt(), strView.mid(pos: 4, n: 2).toUInt(), strView.mid(pos: 6, n: 2).toUInt());
113 }
114
115 if (d.length() > 9 && d[8].toUpper() == QLatin1Char('T')) {
116 const QStringView strView(d);
117 time = QTime(strView.mid(pos: 9, n: 2).toUInt(), strView.mid(pos: 11, n: 2).toUInt(), strView.mid(pos: 13, n: 2).toUInt());
118 }
119
120 return QDateTime(date, time);
121}
122
123void KContacts::adaptIMAttributes(QByteArray &data)
124{
125 data.replace(before: "X-messaging/aim-All", after: ("X-AIM"));
126 data.replace(before: "X-messaging/icq-All", after: ("X-ICQ"));
127 data.replace(before: "X-messaging/xmpp-All", after: ("X-JABBER"));
128 data.replace(before: "X-messaging/msn-All", after: ("X-MSN"));
129 data.replace(before: "X-messaging/yahoo-All", after: ("X-YAHOO"));
130 data.replace(before: "X-messaging/gadu-All", after: ("X-GADUGADU"));
131 data.replace(before: "X-messaging/skype-All", after: ("X-SKYPE"));
132 data.replace(before: "X-messaging/groupwise-All", after: ("X-GROUPWISE"));
133 data.replace(before: "X-messaging/sms-All", after: ("X-SMS"));
134 data.replace(before: "X-messaging/meanwhile-All", after: ("X-MEANWHILE"));
135 data.replace(before: "X-messaging/irc-All", after: ("X-IRC"));
136 data.replace(before: "X-messaging/googletalk-All", after: ("X-GTALK"));
137}
138

source code of kcontacts/src/converter/vcardconverter.cpp