1/*
2 This file is part of libkabc.
3 SPDX-FileCopyrightText: 2015-2019 Laurent Montel <montel@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "impp.h"
9#include "kcontacts_debug.h"
10#include "parametermap_p.h"
11
12#include <KDesktopFile>
13
14#include <QDataStream>
15#include <QDirIterator>
16#include <QStandardPaths>
17#include <QStringList>
18#include <QUrl>
19
20using namespace KContacts;
21
22class Q_DECL_HIDDEN Impp::Private : public QSharedData
23{
24public:
25 Private() = default;
26 Private(const Private &other)
27 : QSharedData(other)
28 {
29 mParamMap = other.mParamMap;
30 }
31
32 ParameterMap mParamMap;
33 QUrl address;
34};
35
36Impp::Impp()
37 : d(new Private)
38{
39}
40
41Impp::Impp(const Impp &other)
42 : d(other.d)
43{
44}
45
46Impp::Impp(const QUrl &address)
47 : d(new Private)
48{
49 d->address = address;
50}
51
52Impp::~Impp() = default;
53
54bool Impp::isValid() const
55{
56 return !d->address.isEmpty() && !d->address.scheme().isEmpty();
57}
58
59void Impp::setAddress(const QUrl &address)
60{
61 d->address = address;
62}
63
64QUrl Impp::address() const
65{
66 return d->address;
67}
68
69QString Impp::serviceType() const
70{
71 return d->address.scheme();
72}
73
74QString Impp::serviceLabel() const
75{
76 return serviceLabel(serviceType: serviceType());
77}
78
79QString Impp::serviceIcon() const
80{
81 return serviceIcon(serviceType: serviceType());
82}
83
84bool Impp::isPreferred() const
85{
86 const auto it = d->mParamMap.findParam(param: QLatin1String("pref"));
87 if (it != d->mParamMap.cend()) {
88 return !it->paramValues.isEmpty() && it->paramValues.at(i: 0) == QLatin1Char('1');
89 }
90 return false;
91}
92
93void Impp::setPreferred(bool preferred)
94{
95 if (!preferred) {
96 auto paramIt = d->mParamMap.findParam(QStringLiteral("pref"));
97 if (paramIt != d->mParamMap.end()) {
98 d->mParamMap.erase(position: paramIt);
99 }
100 } else {
101 auto paramIt = d->mParamMap.findParam(QStringLiteral("pref"));
102 if (paramIt != d->mParamMap.end()) {
103 paramIt->paramValues = QStringList{QStringLiteral("1")};
104 } else {
105 d->mParamMap.insertParam(newdata: {QStringLiteral("pref"), .paramValues: {QStringLiteral("1")}});
106 }
107 }
108}
109
110void Impp::setParams(const ParameterMap &params)
111{
112 d->mParamMap = params;
113}
114
115ParameterMap Impp::params() const
116{
117 return d->mParamMap;
118}
119
120bool Impp::operator==(const Impp &other) const
121{
122 return (d->mParamMap == other.d->mParamMap) && (d->address == other.address());
123}
124
125bool Impp::operator!=(const Impp &other) const
126{
127 return !(other == *this);
128}
129
130Impp &Impp::operator=(const Impp &other)
131{
132 if (this != &other) {
133 d = other.d;
134 }
135
136 return *this;
137}
138
139QString Impp::toString() const
140{
141 QString str = QLatin1String("Impp {\n");
142 str += QStringLiteral(" type: %1\n").arg(a: serviceType());
143 str += QStringLiteral(" address: %1\n").arg(a: d->address.url());
144 str += d->mParamMap.toString();
145 str += QLatin1String("}\n");
146 return str;
147}
148
149QDataStream &KContacts::operator<<(QDataStream &s, const Impp &impp)
150{
151 return s << impp.d->mParamMap << impp.d->address << (uint32_t)(0);
152}
153
154QDataStream &KContacts::operator>>(QDataStream &s, Impp &impp)
155{
156 int i;
157 s >> impp.d->mParamMap >> impp.d->address >> i;
158 return s;
159}
160
161static QString improtcolFile(const QString &serviceType)
162{
163 const auto path =
164 QStandardPaths::locate(type: QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols/") + serviceType + QStringLiteral(".desktop"));
165 if (!path.isEmpty()) {
166 return path;
167 }
168 return QStringLiteral(":/org.kde.kcontacts/improtocols/") + serviceType + QStringLiteral(".desktop");
169}
170
171QString Impp::serviceLabel(const QString &serviceType)
172{
173 const auto path = improtcolFile(serviceType);
174 KDesktopFile df(path);
175 return df.readName();
176}
177
178QString Impp::serviceIcon(const QString &serviceType)
179{
180 const auto path = improtcolFile(serviceType);
181 KDesktopFile df(path);
182 return df.readIcon();
183}
184
185QList<QString> Impp::serviceTypes()
186{
187 QList<QString> types;
188 auto paths = QStandardPaths::locateAll(type: QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols"), options: QStandardPaths::LocateDirectory);
189 paths.push_back(QStringLiteral(":/org.kde.kcontacts/improtocols/"));
190 for (const auto &path : paths) {
191 QDirIterator it(path, QDir::Files);
192 while (it.hasNext()) {
193 it.next();
194 const auto fi = it.fileInfo();
195 if (fi.suffix() == QLatin1String("desktop")) {
196 types.push_back(t: fi.baseName());
197 }
198 }
199 }
200
201 std::sort(first: types.begin(), last: types.end());
202 types.erase(abegin: std::unique(first: types.begin(), last: types.end()), aend: types.end());
203 return types;
204}
205
206#include "moc_impp.cpp"
207

source code of kcontacts/src/impp.cpp