1/*
2 This file is part of the KContacts framework.
3 SPDX-FileCopyrightText: 2003 Tobias Koenig <tokoe@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "vcard_p.h"
9
10using namespace KContacts;
11
12VCard::VCard()
13{
14}
15
16VCard::VCard(const VCard &vcard)
17 : mLineMap(vcard.mLineMap)
18{
19}
20
21VCard::~VCard()
22{
23}
24
25VCard &VCard::operator=(const VCard &vcard)
26{
27 if (&vcard == this) {
28 return *this;
29 }
30
31 mLineMap = vcard.mLineMap;
32
33 return *this;
34}
35
36void VCard::clear()
37{
38 mLineMap.clear();
39}
40
41QStringList VCard::identifiers() const
42{
43 QStringList list;
44 list.reserve(asize: mLineMap.size());
45 for (const auto &[lineId, l] : mLineMap) {
46 list.append(t: lineId);
47 }
48 return list;
49}
50
51void VCard::addLine(const VCardLine &line)
52{
53 auto it = findByLineId(identifier: line.identifier());
54 if (it != mLineMap.end()) {
55 it->list.append(t: line);
56 } else {
57 const LineData newdata{.identifier: line.identifier(), .list: {line}};
58 auto beforeIt = std::lower_bound(first: mLineMap.begin(), last: mLineMap.end(), val: newdata);
59 mLineMap.insert(position: beforeIt, x: newdata);
60 }
61}
62
63VCardLine::List VCard::lines(const QString &identifier) const
64{
65 auto it = findByLineId(identifier);
66 return it != mLineMap.cend() ? it->list : VCardLine::List{};
67}
68
69VCardLine VCard::line(const QString &identifier) const
70{
71 auto it = findByLineId(identifier);
72 return it != mLineMap.cend() && !it->list.isEmpty() ? it->list.at(i: 0) : VCardLine{};
73}
74
75static const char s_verStr[] = "VERSION";
76
77void VCard::setVersion(Version version)
78{
79 VCardLine vcardLine;
80 vcardLine.setIdentifier(QLatin1String(s_verStr));
81 if (version == v2_1) {
82 vcardLine.setIdentifier(QStringLiteral("2.1"));
83 } else if (version == v3_0) {
84 vcardLine.setIdentifier(QStringLiteral("3.0"));
85 } else if (version == v4_0) {
86 vcardLine.setIdentifier(QStringLiteral("4.0"));
87 }
88
89 auto it = findByLineId(identifier: QLatin1String(s_verStr));
90 if (it != mLineMap.end()) {
91 it->list.append(t: vcardLine);
92 } else {
93 const LineData newData{.identifier: QLatin1String(s_verStr), .list: {vcardLine}};
94 auto beforeIt = std::lower_bound(first: mLineMap.begin(), last: mLineMap.end(), val: newData);
95 mLineMap.insert(position: beforeIt, x: newData);
96 }
97}
98
99VCard::Version VCard::version() const
100{
101 auto versionEntryIt = findByLineId(identifier: QLatin1String(s_verStr));
102 if (versionEntryIt == mLineMap.cend()) {
103 return v3_0;
104 }
105
106 const VCardLine vcardLine = versionEntryIt->list.at(i: 0);
107 if (vcardLine.value() == QLatin1String("2.1")) {
108 return v2_1;
109 }
110 if (vcardLine.value() == QLatin1String("3.0")) {
111 return v3_0;
112 }
113
114 return v4_0;
115}
116

source code of kcontacts/src/vcardparser/vcard.cpp