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 | |
12 | using namespace KContacts; |
13 | |
14 | VCardLine::VCardLine() |
15 | { |
16 | } |
17 | |
18 | VCardLine::VCardLine(const QString &identifier) |
19 | : VCardLine(identifier, {}) |
20 | { |
21 | } |
22 | |
23 | VCardLine::VCardLine(const QString &identifier, const QVariant &value) |
24 | : mIdentifier(identifier) |
25 | , mValue(value) |
26 | { |
27 | } |
28 | |
29 | VCardLine::VCardLine(const VCardLine &line) |
30 | : mParamMap(line.mParamMap) |
31 | , mIdentifier(line.mIdentifier) |
32 | , mGroup(line.mGroup) |
33 | , mValue(line.mValue) |
34 | { |
35 | } |
36 | |
37 | VCardLine::~VCardLine() |
38 | { |
39 | } |
40 | |
41 | VCardLine &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 | |
55 | bool 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 | |
65 | void VCardLine::setIdentifier(const QString &identifier) |
66 | { |
67 | mIdentifier = identifier; |
68 | } |
69 | |
70 | QString VCardLine::identifier() const |
71 | { |
72 | return mIdentifier; |
73 | } |
74 | |
75 | void VCardLine::setValue(const QVariant &value) |
76 | { |
77 | mValue = value; |
78 | } |
79 | |
80 | QVariant VCardLine::value() const |
81 | { |
82 | return mValue; |
83 | } |
84 | |
85 | void VCardLine::setGroup(const QString &group) |
86 | { |
87 | mGroup = group; |
88 | } |
89 | |
90 | QString VCardLine::group() const |
91 | { |
92 | return mGroup; |
93 | } |
94 | |
95 | bool VCardLine::hasGroup() const |
96 | { |
97 | return !mGroup.isEmpty(); |
98 | } |
99 | |
100 | QStringList 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 | |
111 | void VCardLine::addParameters(const ParameterMap ¶ms) |
112 | { |
113 | for (const auto &[param, list] : params) { |
114 | addParameter(param, value: list.join(sep: QLatin1Char(','))); |
115 | } |
116 | } |
117 | |
118 | void VCardLine::addParameter(const QString ¶m, 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 | |
130 | QStringList VCardLine::parameters(const QString ¶m) const |
131 | { |
132 | auto it = mParamMap.findParam(param); |
133 | return it != mParamMap.cend() ? it->paramValues : QStringList{}; |
134 | } |
135 | |
136 | QString VCardLine::parameter(const QString ¶m) const |
137 | { |
138 | auto it = mParamMap.findParam(param); |
139 | return it != mParamMap.cend() && !it->paramValues.isEmpty() ? it->paramValues.at(i: 0) : QString{}; |
140 | } |
141 | |
142 | ParameterMap VCardLine::parameterMap() const |
143 | { |
144 | return mParamMap; |
145 | } |
146 | |