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
80 VCardLine line;
81 line.setIdentifier(QLatin1String(s_verStr));
82 if (version == v2_1) {
83 line.setIdentifier(QStringLiteral("2.1"));
84 } else if (version == v3_0) {
85 line.setIdentifier(QStringLiteral("3.0"));
86 } else if (version == v4_0) {
87 line.setIdentifier(QStringLiteral("4.0"));
88 }
89
90 auto it = findByLineId(identifier: QLatin1String(s_verStr));
91 if (it != mLineMap.end()) {
92 it->list.append(t: line);
93 } else {
94 const LineData newData{.identifier: QLatin1String(s_verStr), .list: {line}};
95 auto beforeIt = std::lower_bound(first: mLineMap.begin(), last: mLineMap.end(), val: newData);
96 mLineMap.insert(position: beforeIt, x: newData);
97 }
98}
99
100VCard::Version VCard::version() const
101{
102 auto versionEntryIt = findByLineId(identifier: QLatin1String(s_verStr));
103 if (versionEntryIt == mLineMap.cend()) {
104 return v3_0;
105 }
106
107 const VCardLine line = versionEntryIt->list.at(i: 0);
108 if (line.value() == QLatin1String("2.1")) {
109 return v2_1;
110 }
111 if (line.value() == QLatin1String("3.0")) {
112 return v3_0;
113 }
114
115 return v4_0;
116}
117

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