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 */
19 #include "customproperties.h"
20 
21 #include <QDataStream>
22 #include "kcalendarcore_debug.h"
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();
53  it != mProperties.end(); ++it) {
55  other.mProperties.find(it.key());
56  if (itOther == other.mProperties.end() || itOther.value() != it.value()) {
57  return false;
58  }
59  }
60  for (QMap<QByteArray, QString>::ConstIterator it = mPropertyParameters.begin();
61  it != mPropertyParameters.end(); ++it) {
63  other.mPropertyParameters.find(it.key());
64  if (itOther == other.mPropertyParameters.end() || itOther.value() != it.value()) {
65  return false;
66  }
67  }
68  return true;
69 }
70 //@endcond
71 
73  : d(new Private)
74 {
75 }
76 
78  : d(new Private(*cp.d))
79 {
80 }
81 
83 {
84  // check for self assignment
85  if (&other == this) {
86  return *this;
87  }
88 
89  *d = *other.d;
90  return *this;
91 }
92 
94 {
95  delete d;
96 }
97 
99 {
100  return *d == *other.d;
101 }
102 
104  const QString &value)
105 {
106  if (value.isNull() || key.isEmpty() || app.isEmpty()) {
107  return;
108  }
109  QByteArray property = "X-KDE-" + app + '-' + key;
110  if (!checkName(property)) {
111  return;
112  }
114 
115  if (d->isVolatileProperty(QLatin1String(property))) {
116  d->mVolatileProperties[property] = value;
117  } else {
118  d->mProperties[property] = value;
119  }
120 
122 }
123 
125 {
126  removeNonKDECustomProperty(QByteArray("X-KDE-" + app + '-' + key));
127 }
128 
130 {
131  return nonKDECustomProperty(QByteArray("X-KDE-" + app + '-' + key));
132 }
133 
135 {
136  QByteArray property("X-KDE-" + app + '-' + key);
137  if (!checkName(property)) {
138  return QByteArray();
139  }
140  return property;
141 }
142 
144  const QString &parameters)
145 {
146  if (value.isNull() || !checkName(name)) {
147  return;
148  }
150  if (d->isVolatileProperty(QLatin1String(name))) {
151  d->mVolatileProperties[name] = value;
152  } else {
153  d->mProperties[name] = value;
154  d->mPropertyParameters[name] = parameters;
155  }
157 }
159 {
160  if (d->mProperties.contains(name)) {
162  d->mProperties.remove(name);
163  d->mPropertyParameters.remove(name);
165  } else if (d->mVolatileProperties.contains(name)) {
167  d->mVolatileProperties.remove(name);
169  }
170 }
171 
173 {
174  return d->isVolatileProperty(QLatin1String(name)) ? d->mVolatileProperties.value(name) : d->mProperties.value(name);
175 }
176 
178 {
179  return d->mPropertyParameters.value(name);
180 }
181 
183 {
184  bool changed = false;
185  for (QMap<QByteArray, QString>::ConstIterator it = properties.begin();
186  it != properties.end(); ++it) {
187  // Validate the property name and convert any null string to empty string
188  if (checkName(it.key())) {
189  if (d->isVolatileProperty(QLatin1String(it.key()))) {
190  d->mVolatileProperties[it.key()] = it.value().isNull() ? QLatin1String("") : it.value();
191  } else {
192  d->mProperties[it.key()] = it.value().isNull() ? QLatin1String("") : it.value();
193  }
194  if (!changed) {
196  }
197  changed = true;
198  }
199  }
200  if (changed) {
202  }
203 }
204 
206 {
207  QMap<QByteArray, QString> result = d->mProperties;
208  for (auto it = d->mVolatileProperties.begin(), end = d->mVolatileProperties.end(); it != end; ++it) {
209  result.insert(it.key(), it.value());
210  }
211 
212  return result;
213 }
214 
216 {
217 }
218 
220 {
221 }
222 
223 //@cond PRIVATE
224 bool checkName(const QByteArray &name)
225 {
226  // Check that the property name starts with 'X-' and contains
227  // only the permitted characters
228  const char *n = name.constData();
229  int len = name.length();
230  if (len < 2 || n[0] != 'X' || n[1] != '-') {
231  return false;
232  }
233  for (int i = 2; i < len; ++i) {
234  char ch = n[i];
235  if ((ch >= 'A' && ch <= 'Z') ||
236  (ch >= 'a' && ch <= 'z') ||
237  (ch >= '0' && ch <= '9') ||
238  ch == '-') {
239  continue;
240  }
241  return false; // invalid character found
242  }
243  return true;
244 }
245 //@endcond
246 
248  const KCalendarCore::CustomProperties &properties)
249 {
250  return stream << properties.d->mProperties
251  << properties.d->mPropertyParameters;
252 }
253 
256 {
257  properties.d->mVolatileProperties.clear();
258  return stream >> properties.d->mProperties
259  >> properties.d->mPropertyParameters;
260 }
261 
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:825
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:849
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-2020 The KDE developers.
Generated on Wed Sep 23 2020 22:51:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.