KContacts

resourcelocatorurl.cpp
1 /*
2  This file is part of the KContacts framework.
3  SPDX-FileCopyrightText: 2015-2019 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "resourcelocatorurl.h"
9 #include <QMap>
10 #include <QDataStream>
11 #include <QStringList>
12 
13 using namespace KContacts;
14 
15 class Q_DECL_HIDDEN ResourceLocatorUrl::Private : public QSharedData
16 {
17 public:
18  Private()
19  {
20  }
21 
22  Private(const Private &other)
23  : QSharedData(other)
24  {
25  parameters = other.parameters;
26  url = other.url;
27  }
28 
29  QMap<QString, QStringList> parameters;
30  QUrl url;
31 };
32 
33 ResourceLocatorUrl::ResourceLocatorUrl()
34  : d(new Private)
35 {
36 }
37 
38 ResourceLocatorUrl::ResourceLocatorUrl(const ResourceLocatorUrl &other)
39  : d(other.d)
40 {
41 }
42 
43 ResourceLocatorUrl::~ResourceLocatorUrl()
44 {
45 }
46 
47 struct url_type_name
48 {
49  const char* name;
51 };
52 
53 static const url_type_name url_type_names[] = {
54  { "HOME", ResourceLocatorUrl::Home },
55  { "WORK", ResourceLocatorUrl::Work },
56  { "OTHER", ResourceLocatorUrl::Other },
57  { "PROFILE", ResourceLocatorUrl::Profile }
58 };
59 
61 {
62  const auto it = d->parameters.constFind(QLatin1String("type"));
63  if (it == d->parameters.end()) {
64  return Unknown;
65  }
66 
67  Type type = Unknown;
68  for (const auto &s : it.value()) {
69  const auto it = std::find_if(std::begin(url_type_names), std::end(url_type_names), [s](const url_type_name &t) {
70  return QLatin1String(t.name) == s;
71  });
72  if (it != std::end(url_type_names)) {
73  type |= (*it).type;
74  }
75  }
76  return type;
77 }
78 
80 {
81  const auto oldType = this->type();
82 
83  auto types = d->parameters.value(QLatin1String("type"));
84  for (const auto &t : url_type_names) {
85  if (((type ^ oldType) & t.type) == 0) {
86  continue; // no change
87  }
88 
89  if (type & t.type) {
90  types.push_back(QLatin1String(t.name));
91  } else {
92  types.removeAll(QLatin1String(t.name));
93  }
94  }
95 
96  d->parameters.insert(QLatin1String("type"), types);
97 }
98 
100 {
101  auto it = d->parameters.constFind(QLatin1String("pref"));
102  if (it != d->parameters.end() && !it.value().isEmpty()) {
103  return it.value().at(0) == QLatin1Char('1');
104  }
105 
106  it = d->parameters.constFind(QLatin1String("type"));
107  if (it != d->parameters.end()) {
108  return it.value().contains(QLatin1String("PREF"), Qt::CaseInsensitive);
109  }
110 
111  return false;
112 }
113 
115 {
116  if (preferred == isPreferred()) {
117  return;
118  }
119 
120  auto types = d->parameters.value(QLatin1String("type"));
121  if (!preferred) {
122  d->parameters.remove(QLatin1String("pref"));
123  types.removeAll(QLatin1String("PREF"));
124  } else {
125  types.push_back(QLatin1String("PREF"));
126  }
127  d->parameters.insert(QLatin1String("type"), types);
128 }
129 
130 QMap<QString, QStringList> ResourceLocatorUrl::parameters() const
131 {
132  return d->parameters;
133 }
134 
135 bool ResourceLocatorUrl::operator==(const ResourceLocatorUrl &other) const
136 {
137  return (d->parameters == other.parameters()) && (d->url == other.url());
138 }
139 
140 bool ResourceLocatorUrl::operator!=(const ResourceLocatorUrl &other) const
141 {
142  return !(other == *this);
143 }
144 
145 ResourceLocatorUrl &ResourceLocatorUrl::operator=(const ResourceLocatorUrl &other)
146 {
147  if (this != &other) {
148  d = other.d;
149  }
150 
151  return *this;
152 }
153 
154 QString ResourceLocatorUrl::toString() const
155 {
156  QString str = QLatin1String("ResourceLocatorUrl {\n");
157  str += QStringLiteral(" url: %1\n").arg(d->url.toString());
158  if (!d->parameters.isEmpty()) {
159  QString param;
161  const QMap<QString, QStringList>::const_iterator end = d->parameters.constEnd();
162  while (it != end) {
163  param += QStringLiteral("%1 %2").arg(it.key(), it.value().join(QLatin1Char(',')));
164  ++it;
165  }
166  str += QStringLiteral(" parameters: %1\n").arg(param);
167  }
168  str += QLatin1String("}\n");
169  return str;
170 }
171 
172 void ResourceLocatorUrl::setParameters(const QMap<QString, QStringList> &params)
173 {
174  d->parameters = params;
175 }
176 
177 bool ResourceLocatorUrl::isValid() const
178 {
179  return d->url.isValid();
180 }
181 
182 void ResourceLocatorUrl::setUrl(const QUrl &url)
183 {
184  d->url = url;
185 }
186 
187 QUrl ResourceLocatorUrl::url() const
188 {
189  return d->url;
190 }
191 
192 QDataStream &KContacts::operator<<(QDataStream &s, const ResourceLocatorUrl &calUrl)
193 {
194  return s << calUrl.d->parameters << calUrl.d->url;
195 }
196 
197 QDataStream &KContacts::operator>>(QDataStream &s, ResourceLocatorUrl &calUrl)
198 {
199  s >> calUrl.d->parameters >> calUrl.d->url;
200  return s;
201 }
QString url(QUrl::FormattingOptions options) const const
QString name(const QVariant &location)
Class that holds a Resource Locator.
QMap::const_iterator constBegin() const const
bool isPreferred() const
Returns whether this is the preferred website.
Type type(const QSqlDatabase &db)
CaseInsensitive
void setPreferred(bool preferred)
Sets that this is the preferred website.
void setType(Type type)
Sets the URL type.
const Key key(const T &value, const Key &defaultKey) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Type type() const
Returns the type of the URL.
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 2 2020 22:53:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.