File indexing completed on 2024-07-21 03:39:42

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2002 Tobias Koenig <tokoe@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "vcardconverter.h"
0009 #include "vcardtool_p.h"
0010 
0011 using namespace KContacts;
0012 
0013 VCardConverter::VCardConverter()
0014     : d(nullptr)
0015 {
0016 }
0017 
0018 VCardConverter::~VCardConverter()
0019 {
0020 }
0021 
0022 QByteArray VCardConverter::exportVCard(const Addressee &addr, Version version) const
0023 {
0024     Addressee::List list;
0025     list.append(addr);
0026 
0027     return exportVCards(list, version);
0028 }
0029 
0030 QByteArray VCardConverter::exportVCards(const Addressee::List &list, Version version) const
0031 {
0032     VCardTool tool;
0033     QByteArray returnValue;
0034     switch (version) {
0035     case v2_1:
0036         returnValue = tool.exportVCards(list, VCard::v2_1);
0037         break;
0038     case v3_0:
0039         returnValue = tool.exportVCards(list, VCard::v3_0);
0040         break;
0041     case v4_0:
0042         returnValue = tool.exportVCards(list, VCard::v4_0);
0043         break;
0044     }
0045 
0046     return returnValue;
0047 }
0048 
0049 QByteArray VCardConverter::createVCard(const Addressee &addr, Version version) const
0050 {
0051     Addressee::List list;
0052     list.append(addr);
0053 
0054     return createVCards(list, version);
0055 }
0056 
0057 QByteArray VCardConverter::createVCards(const Addressee::List &list, Version version) const
0058 {
0059     VCardTool tool;
0060     QByteArray returnValue;
0061     switch (version) {
0062     case v2_1:
0063         returnValue = tool.createVCards(list, VCard::v2_1);
0064         break;
0065     case v3_0:
0066         returnValue = tool.createVCards(list, VCard::v3_0);
0067         break;
0068     case v4_0:
0069         returnValue = tool.createVCards(list, VCard::v4_0);
0070         break;
0071     }
0072 
0073     return returnValue;
0074 }
0075 
0076 Addressee VCardConverter::parseVCard(const QByteArray &vcard) const
0077 {
0078     Addressee::List list = parseVCards(vcard);
0079 
0080     return list.isEmpty() ? Addressee() : list[0];
0081 }
0082 
0083 Addressee::List VCardConverter::parseVCards(const QByteArray &vcard) const
0084 {
0085     VCardTool tool;
0086 
0087     return tool.parseVCards(vcard);
0088 }
0089 
0090 /* Helper functions */
0091 
0092 QString KContacts::dateToVCardString(const QDateTime &dateTime)
0093 {
0094     return dateTime.toString(QStringLiteral("yyyyMMddThhmmssZ"));
0095 }
0096 
0097 QString KContacts::dateToVCardString(QDate date)
0098 {
0099     return date.toString(QStringLiteral("yyyyMMdd"));
0100 }
0101 
0102 QDateTime KContacts::VCardStringToDate(const QString &dateString)
0103 {
0104     QDate date;
0105     QTime time;
0106     QString d(dateString);
0107 
0108     d = d.remove(QLatin1Char('-')).remove(QLatin1Char(':'));
0109 
0110     if (d.length() >= 8) {
0111         const QStringView strView(d);
0112         date = QDate(strView.mid(0, 4).toUInt(), strView.mid(4, 2).toUInt(), strView.mid(6, 2).toUInt());
0113     }
0114 
0115     if (d.length() > 9 && d[8].toUpper() == QLatin1Char('T')) {
0116         const QStringView strView(d);
0117         time = QTime(strView.mid(9, 2).toUInt(), strView.mid(11, 2).toUInt(), strView.mid(13, 2).toUInt());
0118     }
0119 
0120     return QDateTime(date, time);
0121 }
0122 
0123 void KContacts::adaptIMAttributes(QByteArray &data)
0124 {
0125     data.replace("X-messaging/aim-All", ("X-AIM"));
0126     data.replace("X-messaging/icq-All", ("X-ICQ"));
0127     data.replace("X-messaging/xmpp-All", ("X-JABBER"));
0128     data.replace("X-messaging/msn-All", ("X-MSN"));
0129     data.replace("X-messaging/yahoo-All", ("X-YAHOO"));
0130     data.replace("X-messaging/gadu-All", ("X-GADUGADU"));
0131     data.replace("X-messaging/skype-All", ("X-SKYPE"));
0132     data.replace("X-messaging/groupwise-All", ("X-GROUPWISE"));
0133     data.replace("X-messaging/sms-All", ("X-SMS"));
0134     data.replace("X-messaging/meanwhile-All", ("X-MEANWHILE"));
0135     data.replace("X-messaging/irc-All", ("X-IRC"));
0136     data.replace("X-messaging/googletalk-All", ("X-GTALK"));
0137 }