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 <QDataStream>
10 #include <QStringList>
11 
12 using namespace KContacts;
13 
14 class Q_DECL_HIDDEN ResourceLocatorUrl::Private : public QSharedData
15 {
16 public:
17  Private()
18  {
19  }
20 
21  Private(const Private &other)
22  : QSharedData(other)
23  {
24  parameters = other.parameters;
25  url = other.url;
26  }
27 
28  QMap<QString, QStringList> parameters;
29  QUrl url;
30 };
31 
32 ResourceLocatorUrl::ResourceLocatorUrl()
33  : d(new Private)
34 {
35 }
36 
37 ResourceLocatorUrl::ResourceLocatorUrl(const ResourceLocatorUrl &other)
38  : d(other.d)
39 {
40 }
41 
42 ResourceLocatorUrl::~ResourceLocatorUrl()
43 {
44 }
45 
46 struct url_type_name {
47  const char *name;
49 };
50 
51 static const url_type_name url_type_names[] = {
52  {"HOME", ResourceLocatorUrl::Home},
53  {"WORK", ResourceLocatorUrl::Work},
54  {"OTHER", ResourceLocatorUrl::Other},
55  {"PROFILE", ResourceLocatorUrl::Profile},
56 };
57 
59 {
60  const auto it = d->parameters.constFind(QLatin1String("type"));
61  if (it == d->parameters.end()) {
62  return Unknown;
63  }
64 
65  Type type = Unknown;
66  for (const auto &s : it.value()) {
67  const auto it = std::find_if(std::begin(url_type_names), std::end(url_type_names), [s](const url_type_name &t) {
68  return QLatin1String(t.name) == s;
69  });
70  if (it != std::end(url_type_names)) {
71  type |= (*it).type;
72  }
73  }
74  return type;
75 }
76 
78 {
79  const auto oldType = this->type();
80 
81  auto types = d->parameters.value(QLatin1String("type"));
82  for (const auto &t : url_type_names) {
83  if (((type ^ oldType) & t.type) == 0) {
84  continue; // no change
85  }
86 
87  if (type & t.type) {
88  types.push_back(QLatin1String(t.name));
89  } else {
90  types.removeAll(QLatin1String(t.name));
91  }
92  }
93 
94  d->parameters.insert(QLatin1String("type"), types);
95 }
96 
98 {
99  auto it = d->parameters.constFind(QLatin1String("pref"));
100  if (it != d->parameters.end() && !it.value().isEmpty()) {
101  return it.value().at(0) == QLatin1Char('1');
102  }
103 
104  it = d->parameters.constFind(QLatin1String("type"));
105  if (it != d->parameters.end()) {
106  return it.value().contains(QLatin1String("PREF"), Qt::CaseInsensitive);
107  }
108 
109  return false;
110 }
111 
113 {
114  if (preferred == isPreferred()) {
115  return;
116  }
117 
118  auto types = d->parameters.value(QLatin1String("type"));
119  if (!preferred) {
120  d->parameters.remove(QLatin1String("pref"));
121  types.removeAll(QLatin1String("PREF"));
122  } else {
123  types.push_back(QLatin1String("PREF"));
124  }
125  d->parameters.insert(QLatin1String("type"), types);
126 }
127 
128 QMap<QString, QStringList> ResourceLocatorUrl::parameters() const
129 {
130  return d->parameters;
131 }
132 
133 bool ResourceLocatorUrl::operator==(const ResourceLocatorUrl &other) const
134 {
135  return (d->parameters == other.parameters()) && (d->url == other.url());
136 }
137 
138 bool ResourceLocatorUrl::operator!=(const ResourceLocatorUrl &other) const
139 {
140  return !(other == *this);
141 }
142 
143 ResourceLocatorUrl &ResourceLocatorUrl::operator=(const ResourceLocatorUrl &other)
144 {
145  if (this != &other) {
146  d = other.d;
147  }
148 
149  return *this;
150 }
151 
152 QString ResourceLocatorUrl::toString() const
153 {
154  QString str = QLatin1String("ResourceLocatorUrl {\n");
155  str += QStringLiteral(" url: %1\n").arg(d->url.toString());
156  if (!d->parameters.isEmpty()) {
157  QString param;
159  const QMap<QString, QStringList>::const_iterator end = d->parameters.constEnd();
160  while (it != end) {
161  param += QStringLiteral("%1 %2").arg(it.key(), it.value().join(QLatin1Char(',')));
162  ++it;
163  }
164  str += QStringLiteral(" parameters: %1\n").arg(param);
165  }
166  str += QLatin1String("}\n");
167  return str;
168 }
169 
170 void ResourceLocatorUrl::setParameters(const QMap<QString, QStringList> &params)
171 {
172  d->parameters = params;
173 }
174 
175 bool ResourceLocatorUrl::isValid() const
176 {
177  return d->url.isValid();
178 }
179 
180 void ResourceLocatorUrl::setUrl(const QUrl &url)
181 {
182  d->url = url;
183 }
184 
185 QUrl ResourceLocatorUrl::url() const
186 {
187  return d->url;
188 }
189 
190 QDataStream &KContacts::operator<<(QDataStream &s, const ResourceLocatorUrl &calUrl)
191 {
192  return s << calUrl.d->parameters << calUrl.d->url;
193 }
194 
195 QDataStream &KContacts::operator>>(QDataStream &s, ResourceLocatorUrl &calUrl)
196 {
197  s >> calUrl.d->parameters >> calUrl.d->url;
198  return s;
199 }
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-2021 The KDE developers.
Generated on Sat Jun 19 2021 22:55:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.