KContacts

impp.cpp
1 /*
2  This file is part of libkabc.
3  SPDX-FileCopyrightText: 2015-2019 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "impp.h"
9 #include "kcontacts_debug.h"
10 
11 #include <KDesktopFile>
12 
13 #include <QDataStream>
14 #include <QDirIterator>
15 #include <QStandardPaths>
16 #include <QStringList>
17 #include <QUrl>
18 
19 using namespace KContacts;
20 
21 class Q_DECL_HIDDEN Impp::Private : public QSharedData
22 {
23 public:
24  Private() = default;
25  Private(const Private &other)
26  : QSharedData(other)
27  {
28  parameters = other.parameters;
29  }
30 
31  QMap<QString, QStringList> parameters;
32  QUrl address;
33 };
34 
35 Impp::Impp()
36  : d(new Private)
37 {
38 }
39 
40 Impp::Impp(const Impp &other)
41  : d(other.d)
42 {
43 }
44 
45 Impp::Impp(const QUrl &address)
46  : d(new Private)
47 {
48  d->address = address;
49 }
50 
51 Impp::~Impp()
52 {
53 }
54 
55 bool Impp::isValid() const
56 {
57  return !d->address.isEmpty() && !d->address.scheme().isEmpty();
58 }
59 
60 void Impp::setAddress(const QUrl &address)
61 {
62  d->address = address;
63 }
64 
65 QUrl Impp::address() const
66 {
67  return d->address;
68 }
69 
71 {
72  return d->address.scheme();
73 }
74 
76 {
77  return serviceLabel(serviceType());
78 }
79 
81 {
82  return serviceIcon(serviceType());
83 }
84 
85 bool Impp::isPreferred() const
86 {
87  const auto it = d->parameters.constFind(QLatin1String("pref"));
88  if (it != d->parameters.constEnd() && !it.value().isEmpty()) {
89  return it.value().at(0) == QLatin1Char('1');
90  }
91  return false;
92 }
93 
94 void Impp::setPreferred(bool preferred)
95 {
96  if (preferred) {
97  d->parameters.insert(QStringLiteral("pref"), {QStringLiteral("1")});
98  } else {
99  d->parameters.remove(QLatin1String("pref"));
100  }
101 }
102 
103 void Impp::setParameters(const QMap<QString, QStringList> &params)
104 {
105  d->parameters = params;
106 }
107 
108 QMap<QString, QStringList> Impp::parameters() const
109 {
110  return d->parameters;
111 }
112 
113 bool Impp::operator==(const Impp &other) const
114 {
115  return (d->parameters == other.parameters()) && (d->address == other.address());
116 }
117 
118 bool Impp::operator!=(const Impp &other) const
119 {
120  return !(other == *this);
121 }
122 
123 Impp &Impp::operator=(const Impp &other)
124 {
125  if (this != &other) {
126  d = other.d;
127  }
128 
129  return *this;
130 }
131 
132 QString Impp::toString() const
133 {
134  QString str = QLatin1String("Impp {\n");
135  str += QStringLiteral(" type: %1\n").arg(serviceType());
136  str += QStringLiteral(" address: %1\n").arg(d->address.url());
137  if (!d->parameters.isEmpty()) {
138  QString param;
139  QMap<QString, QStringList>::const_iterator it = d->parameters.constBegin();
140  const QMap<QString, QStringList>::const_iterator end = d->parameters.constEnd();
141  while (it != end) {
142  param += QStringLiteral("%1 %2").arg(it.key(), it.value().join(QLatin1Char(',')));
143  ++it;
144  }
145  str += QStringLiteral(" parameters: %1\n").arg(param);
146  }
147  str += QLatin1String("}\n");
148  return str;
149 }
150 
151 QDataStream &KContacts::operator<<(QDataStream &s, const Impp &impp)
152 {
153  return s << impp.d->parameters << impp.d->address << (uint32_t)(0);
154 }
155 
156 QDataStream &KContacts::operator>>(QDataStream &s, Impp &impp)
157 {
158  int i;
159  s >> impp.d->parameters >> impp.d->address >> i;
160  return s;
161 }
162 
163 static QString improtcolFile(const QString &serviceType)
164 {
165  const auto path =
166  QStandardPaths::locate(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 
173 QString Impp::serviceLabel(const QString &serviceType)
174 {
175  const auto path = improtcolFile(serviceType);
176  KDesktopFile df(path);
177  return df.readName();
178 }
179 
180 QString Impp::serviceIcon(const QString &serviceType)
181 {
182  const auto path = improtcolFile(serviceType);
183  KDesktopFile df(path);
184  return df.readIcon();
185 }
186 
188 {
189  QVector<QString> types;
190  auto paths = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kf5/kcontacts/improtocols"), 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(fi.baseName());
199  }
200  }
201  }
202 
203  std::sort(types.begin(), types.end());
204  types.erase(std::unique(types.begin(), types.end()), types.end());
205  return types;
206 }
207 
208 #include "moc_impp.cpp"
QString next()
bool isPreferred() const
Returns whether this is the preferred messaging address.
QFileInfo fileInfo() const const
QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
QVector::iterator begin()
QVector::iterator erase(QVector::iterator begin, QVector::iterator end)
QString serviceLabel() const
Returns a translated label for the service type.
PostalAddress address(const QVariant &location)
static QVector< QString > serviceTypes()
List all known service types.
Definition: impp.cpp:187
void setPreferred(bool preferred)
Sets that this is the preferred messaging address.
Definition: impp.cpp:94
QString serviceType() const
Returns the messaging service this address is for.
Class that holds a IMPP for a contact.
Definition: impp.h:27
const Key key(const T &value, const Key &defaultKey) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString readIcon() const
void push_back(const T &value)
bool hasNext() const const
QVector::iterator end()
QString readName() const
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
const T value(const Key &key, const T &defaultValue) const const
QString serviceIcon() const
Returns the name of an icon representing the service type.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Jun 20 2021 22:54:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.