KCalendarCore

customproperties.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  SPDX-FileCopyrightText: 2002, 2006, 2010 David Jarvie <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 /**
9  @file
10  This file is part of the API for handling calendar data and
11  defines the CustomProperties class.
12 
13  @brief
14  A class to manage custom calendar properties.
15 
16  @author David Jarvie <[email protected]>
17 */
18 
19 #include "customproperties.h"
20 
21 #include "kcalendarcore_debug.h"
22 #include <QDataStream>
23 
24 using namespace KCalendarCore;
25 
26 //@cond PRIVATE
27 static bool checkName(const QByteArray &name);
28 
29 class Q_DECL_HIDDEN CustomProperties::Private
30 {
31 public:
32  bool operator==(const Private &other) const;
33  QMap<QByteArray, QString> mProperties; // custom calendar properties
34  QMap<QByteArray, QString> mPropertyParameters;
35 
36  // Volatile properties are not written back to the serialized format and are not compared in operator==
37  // They are only used for runtime purposes and are not part of the payload.
38  QMap<QByteArray, QString> mVolatileProperties;
39 
40  bool isVolatileProperty(const QString &name) const
41  {
42  return name.startsWith(QLatin1String("X-KDE-VOLATILE"));
43  }
44 };
45 
46 bool CustomProperties::Private::operator==(const CustomProperties::Private &other) const
47 {
48  if (mProperties.count() != other.mProperties.count()) {
49  // qCDebug(KCALCORE_LOG) << "Property count is different:" << mProperties << other.mProperties;
50  return false;
51  }
52  for (QMap<QByteArray, QString>::ConstIterator it = mProperties.begin(); it != mProperties.end(); ++it) {
53  QMap<QByteArray, QString>::ConstIterator itOther = other.mProperties.find(it.key());
54  if (itOther == other.mProperties.end() || itOther.value() != it.value()) {
55  return false;
56  }
57  }
58  for (QMap<QByteArray, QString>::ConstIterator it = mPropertyParameters.begin(); it != mPropertyParameters.end(); ++it) {
59  QMap<QByteArray, QString>::ConstIterator itOther = other.mPropertyParameters.find(it.key());
60  if (itOther == other.mPropertyParameters.end() || itOther.value() != it.value()) {
61  return false;
62  }
63  }
64  return true;
65 }
66 //@endcond
67 
69  : d(new Private)
70 {
71 }
72 
74  : d(new Private(*cp.d))
75 {
76 }
77 
79 {
80  // check for self assignment
81  if (&other == this) {
82  return *this;
83  }
84 
85  *d = *other.d;
86  return *this;
87 }
88 
90 {
91  delete d;
92 }
93 
95 {
96  return *d == *other.d;
97 }
98 
99 void CustomProperties::setCustomProperty(const QByteArray &app, const QByteArray &key, const QString &value)
100 {
101  if (value.isNull() || key.isEmpty() || app.isEmpty()) {
102  return;
103  }
104  QByteArray property = "X-KDE-" + app + '-' + key;
105  if (!checkName(property)) {
106  return;
107  }
109 
110  if (d->isVolatileProperty(QLatin1String(property))) {
111  d->mVolatileProperties[property] = value;
112  } else {
113  d->mProperties[property] = value;
114  }
115 
117 }
118 
120 {
121  removeNonKDECustomProperty(QByteArray("X-KDE-" + app + '-' + key));
122 }
123 
125 {
126  return nonKDECustomProperty(QByteArray("X-KDE-" + app + '-' + key));
127 }
128 
130 {
131  QByteArray property("X-KDE-" + app + '-' + key);
132  if (!checkName(property)) {
133  return QByteArray();
134  }
135  return property;
136 }
137 
138 void CustomProperties::setNonKDECustomProperty(const QByteArray &name, const QString &value, const QString &parameters)
139 {
140  if (value.isNull() || !checkName(name)) {
141  return;
142  }
144  if (d->isVolatileProperty(QLatin1String(name))) {
145  d->mVolatileProperties[name] = value;
146  } else {
147  d->mProperties[name] = value;
148  d->mPropertyParameters[name] = parameters;
149  }
151 }
153 {
154  if (d->mProperties.contains(name)) {
156  d->mProperties.remove(name);
157  d->mPropertyParameters.remove(name);
159  } else if (d->mVolatileProperties.contains(name)) {
161  d->mVolatileProperties.remove(name);
163  }
164 }
165 
167 {
168  return d->isVolatileProperty(QLatin1String(name)) ? d->mVolatileProperties.value(name) : d->mProperties.value(name);
169 }
170 
172 {
173  return d->mPropertyParameters.value(name);
174 }
175 
177 {
178  bool changed = false;
179  for (QMap<QByteArray, QString>::ConstIterator it = properties.begin(); it != properties.end(); ++it) {
180  // Validate the property name and convert any null string to empty string
181  if (checkName(it.key())) {
182  if (d->isVolatileProperty(QLatin1String(it.key()))) {
183  d->mVolatileProperties[it.key()] = it.value().isNull() ? QLatin1String("") : it.value();
184  } else {
185  d->mProperties[it.key()] = it.value().isNull() ? QLatin1String("") : it.value();
186  }
187  if (!changed) {
189  }
190  changed = true;
191  }
192  }
193  if (changed) {
195  }
196 }
197 
199 {
200  QMap<QByteArray, QString> result = d->mProperties;
201  for (auto it = d->mVolatileProperties.begin(), end = d->mVolatileProperties.end(); it != end; ++it) {
202  result.insert(it.key(), it.value());
203  }
204 
205  return result;
206 }
207 
209 {
210 }
211 
213 {
214 }
215 
216 //@cond PRIVATE
217 bool checkName(const QByteArray &name)
218 {
219  // Check that the property name starts with 'X-' and contains
220  // only the permitted characters
221  const char *n = name.constData();
222  int len = name.length();
223  if (len < 2 || n[0] != 'X' || n[1] != '-') {
224  return false;
225  }
226  for (int i = 2; i < len; ++i) {
227  char ch = n[i];
228  if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-') {
229  continue;
230  }
231  return false; // invalid character found
232  }
233  return true;
234 }
235 //@endcond
236 
238 {
239  return stream << properties.d->mProperties << properties.d->mPropertyParameters;
240 }
241 
243 {
244  properties.d->mVolatileProperties.clear();
245  return stream >> properties.d->mProperties >> properties.d->mPropertyParameters;
246 }
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:820
void setNonKDECustomProperty(const QByteArray &name, const QString &value, const QString &parameters=QString())
Create or modify a non-KDE or non-standard custom calendar property.
A class to manage custom calendar properties.
This file is part of the API for handling calendar data and defines the CustomProperties class...
virtual void customPropertyUpdate()
Called before a custom property will be changed.
CustomProperties()
Constructs an empty custom properties instance.
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition: alarm.cpp:833
bool isEmpty() const const
virtual void customPropertyUpdated()
Called when a custom property has been changed.
int length() const const
QString nonKDECustomProperty(const QByteArray &name) const
Return the value of a non-KDE or non-standard custom calendar property.
bool isNull() const const
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
void removeNonKDECustomProperty(const QByteArray &name)
Delete a non-KDE or non-standard custom calendar property.
const char * constData() const const
void removeCustomProperty(const QByteArray &app, const QByteArray &key)
Delete a custom calendar property.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QMap::iterator end()
QMap::iterator begin()
QString customProperty(const QByteArray &app, const QByteArray &key) const
Return the value of a custom calendar property.
QMap< QByteArray, QString > customProperties() const
Returns all custom calendar property key/value pairs.
void setCustomProperties(const QMap< QByteArray, QString > &properties)
Initialise the alarm&#39;s custom calendar properties to the specified key/value pairs.
QMap::iterator insert(const Key &key, const T &value)
bool operator==(const CustomProperties &properties) const
Compare this with properties for equality.
static QByteArray customPropertyName(const QByteArray &app, const QByteArray &key)
Validate and return the full name of a custom calendar property.
int count(const Key &key) const const
QMap::iterator find(const Key &key)
QString nonKDECustomPropertyParameters(const QByteArray &name) const
Return the parameters of a non-KDE or non-standard custom calendar property.
CustomProperties & operator=(const CustomProperties &other)
Assignment operator.
Namespace for all KCalendarCore types.
Definition: alarm.h:36
virtual ~CustomProperties()
Destructor.
const T value(const Key &key, const T &defaultValue) const const
void setCustomProperty(const QByteArray &app, const QByteArray &key, const QString &value)
Create or modify a custom calendar property.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Sep 26 2021 22:51:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.