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 "vcardline_p.h"
9
10#include "parametermap_p.h"
11
12using namespace KContacts;
13
14VCardLine::VCardLine()
15{
16}
17
18VCardLine::VCardLine(const QString &identifier)
19 : VCardLine(identifier, {})
20{
21}
22
23VCardLine::VCardLine(const QString &identifier, const QVariant &value)
24 : mIdentifier(identifier)
25 , mValue(value)
26{
27}
28
29VCardLine::VCardLine(const VCardLine &line)
30 : mBase64Value(line.mBase64Value)
31 , mParamMap(line.mParamMap)
32 , mIdentifier(line.mIdentifier)
33 , mGroup(line.mGroup)
34 , mValue(line.mValue)
35{
36}
37
38VCardLine::~VCardLine()
39{
40}
41
42VCardLine &VCardLine::operator=(const VCardLine &line)
43{
44 if (&line == this) {
45 return *this;
46 }
47
48 mParamMap = line.mParamMap;
49 mValue = line.mValue;
50 mIdentifier = line.mIdentifier;
51 mGroup = line.mGroup;
52 mBase64Value = line.mBase64Value;
53
54 return *this;
55}
56
57bool VCardLine::operator==(const VCardLine &other) const
58{
59 // clang-format off
60 return (mParamMap == other.parameterMap())
61 && (mValue == other.value())
62 && (mIdentifier == other.identifier())
63 && (mGroup == other.group())
64 && (mBase64Value == other.base64Value());
65 // clang-format on
66}
67
68void VCardLine::setIdentifier(const QString &identifier)
69{
70 mIdentifier = identifier;
71}
72
73QString VCardLine::identifier() const
74{
75 return mIdentifier;
76}
77
78void VCardLine::setValue(const QVariant &value)
79{
80 mValue = value;
81}
82
83QVariant VCardLine::value() const
84{
85 return mValue;
86}
87
88void VCardLine::setGroup(const QString &group)
89{
90 mGroup = group;
91}
92
93QString VCardLine::group() const
94{
95 return mGroup;
96}
97
98bool VCardLine::hasGroup() const
99{
100 return !mGroup.isEmpty();
101}
102
103QStringList VCardLine::parameterList() const
104{
105 QStringList list;
106 list.reserve(asize: mParamMap.size());
107 for (const auto &[param, values] : mParamMap) {
108 list.append(t: param);
109 }
110
111 return list;
112}
113
114void VCardLine::addParameters(const ParameterMap &params)
115{
116 for (const auto &[param, list] : params) {
117 addParameter(param, value: list.join(sep: QLatin1Char(',')));
118 }
119}
120
121void VCardLine::addParameter(const QString &param, const QString &value)
122{
123 auto it = mParamMap.findParam(param);
124 if (it != mParamMap.end()) {
125 if (!it->paramValues.contains(str: value)) { // not included yet
126 it->paramValues.push_back(t: value);
127 }
128 } else {
129 mParamMap.insertParam(newdata: {.param: param, .paramValues: QStringList{value}});
130 }
131}
132
133QStringList VCardLine::parameters(const QString &param) const
134{
135 auto it = mParamMap.findParam(param);
136 return it != mParamMap.cend() ? it->paramValues : QStringList{};
137}
138
139QString VCardLine::parameter(const QString &param) const
140{
141 auto it = mParamMap.findParam(param);
142 return it != mParamMap.cend() && !it->paramValues.isEmpty() ? it->paramValues.at(i: 0) : QString{};
143}
144
145ParameterMap VCardLine::parameterMap() const
146{
147 return mParamMap;
148}
149
150QByteArray VCardLine::base64Value() const
151{
152 return mBase64Value;
153}
154
155void VCardLine::setBase64Value(const QByteArray &newBase64Value)
156{
157 mBase64Value = newBase64Value;
158}
159
160QDebug operator<<(QDebug d, const KContacts::VCardLine &t)
161{
162 d.space() << "Identifier" << t.identifier();
163 d.space() << "Group" << t.group();
164 d.space() << "Value" << t.value();
165 d.space() << "Base64Value" << t.base64Value();
166 d.space() << "ParamMap" << t.parameterMap().toString();
167 return d;
168}
169

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