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 <QMap>
16 #include <QStandardPaths>
17 #include <QStringList>
18 #include <QUrl>
19 
20 using namespace KContacts;
21 
22 class Q_DECL_HIDDEN Impp::Private : public QSharedData
23 {
24 public:
25  Private() = default;
26  Private(const Private &other)
27  : QSharedData(other)
28  {
29  parameters = other.parameters;
30  }
31 
32  QMap<QString, QStringList> parameters;
33  QUrl address;
34 };
35 
36 Impp::Impp()
37  : d(new Private)
38 {
39 }
40 
41 Impp::Impp(const Impp &other)
42  : d(other.d)
43 {
44 }
45 
46 Impp::Impp(const QUrl &address)
47  : d(new Private)
48 {
49  d->address = address;
50 }
51 
52 Impp::~Impp()
53 {
54 }
55 
56 bool Impp::isValid() const
57 {
58  return !d->address.isEmpty() && !d->address.scheme().isEmpty();
59 }
60 
61 void Impp::setAddress(const QUrl &address)
62 {
63  d->address = address;
64 }
65 
66 QUrl Impp::address() const
67 {
68  return d->address;
69 }
70 
72 {
73  return d->address.scheme();
74 }
75 
77 {
78  return serviceLabel(serviceType());
79 }
80 
82 {
83  return serviceIcon(serviceType());
84 }
85 
86 bool Impp::isPreferred() const
87 {
88  const auto it = d->parameters.constFind(QLatin1String("pref"));
89  if (it != d->parameters.constEnd() && !it.value().isEmpty()) {
90  return it.value().at(0) == QLatin1Char('1');
91  }
92  return false;
93 }
94 
95 void Impp::setPreferred(bool preferred)
96 {
97  if (preferred) {
98  d->parameters.insert(QStringLiteral("pref"), {QStringLiteral("1")});
99  } else {
100  d->parameters.remove(QLatin1String("pref"));
101  }
102 }
103 
104 void Impp::setParameters(const QMap<QString, QStringList> &params)
105 {
106  d->parameters = params;
107 }
108 
109 QMap<QString, QStringList> Impp::parameters() const
110 {
111  return d->parameters;
112 }
113 
114 bool Impp::operator==(const Impp &other) const
115 {
116  return (d->parameters == other.parameters()) && (d->address == other.address());
117 }
118 
119 bool Impp::operator!=(const Impp &other) const
120 {
121  return !(other == *this);
122 }
123 
124 Impp &Impp::operator=(const Impp &other)
125 {
126  if (this != &other) {
127  d = other.d;
128  }
129 
130  return *this;
131 }
132 
133 QString Impp::toString() const
134 {
135  QString str = QLatin1String("Impp {\n");
136  str += QStringLiteral(" type: %1\n").arg(serviceType());
137  str += QStringLiteral(" address: %1\n").arg(d->address.url());
138  if (!d->parameters.isEmpty()) {
139  QString param;
140  QMap<QString, QStringList>::const_iterator it = d->parameters.constBegin();
141  const QMap<QString, QStringList>::const_iterator end = d->parameters.constEnd();
142  while (it != end) {
143  param += QStringLiteral("%1 %2").arg(it.key(), it.value().join(QLatin1Char(',')));
144  ++it;
145  }
146  str += QStringLiteral(" parameters: %1\n").arg(param);
147  }
148  str += QLatin1String("}\n");
149  return str;
150 }
151 
152 QDataStream &KContacts::operator<<(QDataStream &s, const Impp &impp)
153 {
154  return s << impp.d->parameters << impp.d->address << (uint32_t)(0);
155 }
156 
157 QDataStream &KContacts::operator>>(QDataStream &s, Impp &impp)
158 {
159  int i;
160  s >> impp.d->parameters >> impp.d->address >> i;
161  return s;
162 }
163 
164 static QString improtcolFile(const QString &serviceType)
165 {
166  const auto path = 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:95
QString serviceType() const
Returns the messaging service this address is for.
Class that holds a IMPP for a contact.
Definition: impp.h:26
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-2020 The KDE developers.
Generated on Wed Jul 8 2020 22:53:31 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.