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()
53{
54}
55
56bool Impp::isValid() const
57{
58 return !d->address.isEmpty() && !d->address.scheme().isEmpty();
59}
60
61void Impp::setAddress(const QUrl &address)
62{
63 d->address = address;
64}
65
66QUrl Impp::address() const
67{
68 return d->address;
69}
70
71QString Impp::serviceType() const
72{
73 return d->address.scheme();
74}
75
76QString Impp::serviceLabel() const
77{
78 return serviceLabel(serviceType: serviceType());
79}
80
81QString Impp::serviceIcon() const
82{
83 return serviceIcon(serviceType: serviceType());
84}
85
86bool Impp::isPreferred() const
87{
88 const auto it = d->mParamMap.findParam(param: QLatin1String("pref"));
89 if (it != d->mParamMap.cend()) {
90 return !it->paramValues.isEmpty() && it->paramValues.at(i: 0) == QLatin1Char('1');
91 }
92 return false;
93}
94
95void Impp::setPreferred(bool preferred)
96{
97 if (!preferred) {
98 auto paramIt = d->mParamMap.findParam(QStringLiteral("pref"));
99 if (paramIt != d->mParamMap.end()) {
100 d->mParamMap.erase(position: paramIt);
101 }
102 } else {
103 auto paramIt = d->mParamMap.findParam(QStringLiteral("pref"));
104 if (paramIt != d->mParamMap.end()) {
105 paramIt->paramValues = QStringList{QStringLiteral("1")};
106 } else {
107 d->mParamMap.insertParam(newdata: {QStringLiteral("pref"), .paramValues: {QStringLiteral("1")}});
108 }
109 }
110}
111
112void Impp::setParams(const ParameterMap &params)
113{
114 d->mParamMap = params;
115}
116
117ParameterMap Impp::params() const
118{
119 return d->mParamMap;
120}
121
122bool Impp::operator==(const Impp &other) const
123{
124 return (d->mParamMap == other.d->mParamMap) && (d->address == other.address());
125}
126
127bool Impp::operator!=(const Impp &other) const
128{
129 return !(other == *this);
130}
131
132Impp &Impp::operator=(const Impp &other)
133{
134 if (this != &other) {
135 d = other.d;
136 }
137
138 return *this;
139}
140
141QString Impp::toString() const
142{
143 QString str = QLatin1String("Impp {\n");
144 str += QStringLiteral(" type: %1\n").arg(a: serviceType());
145 str += QStringLiteral(" address: %1\n").arg(a: d->address.url());
146 str += d->mParamMap.toString();
147 str += QLatin1String("}\n");
148 return str;
149}
150
151QDataStream &KContacts::operator<<(QDataStream &s, const Impp &impp)
152{
153 return s << impp.d->mParamMap << impp.d->address << (uint32_t)(0);
154}
155
156QDataStream &KContacts::operator>>(QDataStream &s, Impp &impp)
157{
158 int i;
159 s >> impp.d->mParamMap >> impp.d->address >> i;
160 return s;
161}
162
163static QString improtcolFile(const QString &serviceType)
164{
165 const auto path =
166 QStandardPaths::locate(type: QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols/") + serviceType + QStringLiteral(".desktop"));
167 if (!path.isEmpty()) {
168 return path;
169 }
170 return QStringLiteral(":/org.kde.kcontacts/improtocols/") + serviceType + QStringLiteral(".desktop");
171}
172
173QString Impp::serviceLabel(const QString &serviceType)
174{
175 const auto path = improtcolFile(serviceType);
176 KDesktopFile df(path);
177 return df.readName();
178}
179
180QString Impp::serviceIcon(const QString &serviceType)
181{
182 const auto path = improtcolFile(serviceType);
183 KDesktopFile df(path);
184 return df.readIcon();
185}
186
187QList<QString> Impp::serviceTypes()
188{
189 QList<QString> types;
190 auto paths = QStandardPaths::locateAll(type: QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols"), options: QStandardPaths::LocateDirectory);
191 paths.push_back(QStringLiteral(":/org.kde.kcontacts/improtocols/"));
192 for (const auto &path : paths) {
193 QDirIterator it(path, QDir::Files);
194 while (it.hasNext()) {
195 it.next();
196 const auto fi = it.fileInfo();
197 if (fi.suffix() == QLatin1String("desktop")) {
198 types.push_back(t: fi.baseName());
199 }
200 }
201 }
202
203 std::sort(first: types.begin(), last: types.end());
204 types.erase(abegin: std::unique(first: types.begin(), last: types.end()), aend: types.end());
205 return types;
206}
207
208#include "moc_impp.cpp"
209

source code of kcontacts/src/impp.cpp