KContacts

vcard.cpp
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{
44 list.reserve(mLineMap.size());
45 for (const auto &[lineId, l] : mLineMap) {
46 list.append(lineId);
47 }
48 return list;
49}
50
51void VCard::addLine(const VCardLine &line)
52{
53 auto it = findByLineId(line.identifier());
54 if (it != mLineMap.end()) {
55 it->list.append(line);
56 } else {
57 const LineData newdata{line.identifier(), {line}};
58 auto beforeIt = std::lower_bound(mLineMap.begin(), mLineMap.end(), newdata);
59 mLineMap.insert(beforeIt, 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(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(QLatin1String(s_verStr));
91 if (it != mLineMap.end()) {
92 it->list.append(line);
93 } else {
94 const LineData newData{QLatin1String(s_verStr), {line}};
95 auto beforeIt = std::lower_bound(mLineMap.begin(), mLineMap.end(), newData);
96 mLineMap.insert(beforeIt, newData);
97 }
98}
99
100VCard::Version VCard::version() const
101{
102 auto versionEntryIt = findByLineId(QLatin1String(s_verStr));
103 if (versionEntryIt == mLineMap.cend()) {
104 return v3_0;
105 }
106
107 const VCardLine line = versionEntryIt->list.at(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}
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void append(QList< T > &&value)
void reserve(qsizetype size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.