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 : mParamMap(line.mParamMap)
31 , mIdentifier(line.mIdentifier)
32 , mGroup(line.mGroup)
33 , mValue(line.mValue)
34{
35}
36
37VCardLine::~VCardLine()
38{
39}
40
41VCardLine &VCardLine::operator=(const VCardLine &line)
42{
43 if (&line == this) {
44 return *this;
45 }
46
47 mParamMap = line.mParamMap;
48 mValue = line.mValue;
49 mIdentifier = line.mIdentifier;
50 mGroup = line.mGroup;
51
52 return *this;
53}
54
55bool VCardLine::operator==(const VCardLine &other) const
56{
57 // clang-format off
58 return (mParamMap == other.parameterMap())
59 && (mValue == other.value())
60 && (mIdentifier == other.identifier())
61 && (mGroup == other.group());
62 // clang-format on
63}
64
65void VCardLine::setIdentifier(const QString &identifier)
66{
67 mIdentifier = identifier;
68}
69
70QString VCardLine::identifier() const
71{
72 return mIdentifier;
73}
74
75void VCardLine::setValue(const QVariant &value)
76{
77 mValue = value;
78}
79
80QVariant VCardLine::value() const
81{
82 return mValue;
83}
84
85void VCardLine::setGroup(const QString &group)
86{
87 mGroup = group;
88}
89
90QString VCardLine::group() const
91{
92 return mGroup;
93}
94
95bool VCardLine::hasGroup() const
96{
97 return !mGroup.isEmpty();
98}
99
100QStringList VCardLine::parameterList() const
101{
102 QStringList list;
103 list.reserve(asize: mParamMap.size());
104 for (const auto &[param, values] : mParamMap) {
105 list.append(t: param);
106 }
107
108 return list;
109}
110
111void VCardLine::addParameters(const ParameterMap &params)
112{
113 for (const auto &[param, list] : params) {
114 addParameter(param, value: list.join(sep: QLatin1Char(',')));
115 }
116}
117
118void VCardLine::addParameter(const QString &param, const QString &value)
119{
120 auto it = mParamMap.findParam(param);
121 if (it != mParamMap.end()) {
122 if (!it->paramValues.contains(str: value)) { // not included yet
123 it->paramValues.push_back(t: value);
124 }
125 } else {
126 mParamMap.insertParam(newdata: {.param: param, .paramValues: QStringList{value}});
127 }
128}
129
130QStringList VCardLine::parameters(const QString &param) const
131{
132 auto it = mParamMap.findParam(param);
133 return it != mParamMap.cend() ? it->paramValues : QStringList{};
134}
135
136QString VCardLine::parameter(const QString &param) const
137{
138 auto it = mParamMap.findParam(param);
139 return it != mParamMap.cend() && !it->paramValues.isEmpty() ? it->paramValues.at(i: 0) : QString{};
140}
141
142ParameterMap VCardLine::parameterMap() const
143{
144 return mParamMap;
145}
146

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