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 <djarvie@kde.org>
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 <djarvie@kde.org>
17*/
18
19#include "customproperties.h"
20
21#include "kcalendarcore_debug.h"
22#include <QDataStream>
23
24using namespace KCalendarCore;
25
26//@cond PRIVATE
27static bool checkName(const QByteArray &name);
28
29class Q_DECL_HIDDEN CustomProperties::Private
30{
31public:
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
46bool 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 (auto it = mProperties.cbegin(); it != mProperties.cend(); ++it) {
53 auto itOther = other.mProperties.constFind(it.key());
54 if (itOther == other.mProperties.cend() || itOther.value() != it.value()) {
55 return false;
56 }
57 }
58 for (auto it = mPropertyParameters.cbegin(); it != mPropertyParameters.cend(); ++it) {
59 auto itOther = other.mPropertyParameters.constFind(it.key());
60 if (itOther == other.mPropertyParameters.cend() || 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
93
95{
96 return *d == *other.d;
97}
98
99void 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
138void 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 (auto it = properties.cbegin(); it != properties.cend(); ++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
211
215
216//@cond PRIVATE
217bool 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}
A class to manage custom calendar properties.
void removeCustomProperty(const QByteArray &app, const QByteArray &key)
Delete a custom calendar property.
virtual ~CustomProperties()
Destructor.
bool operator==(const CustomProperties &properties) const
Compare this with properties for equality.
void setNonKDECustomProperty(const QByteArray &name, const QString &value, const QString &parameters=QString())
Create or modify a non-KDE or non-standard custom calendar property.
void setCustomProperty(const QByteArray &app, const QByteArray &key, const QString &value)
Create or modify a custom calendar property.
void setCustomProperties(const QMap< QByteArray, QString > &properties)
Initialise the alarm's custom calendar properties to the specified key/value pairs.
CustomProperties & operator=(const CustomProperties &other)
Assignment operator.
QString nonKDECustomPropertyParameters(const QByteArray &name) const
Return the parameters of a non-KDE or non-standard custom calendar property.
void removeNonKDECustomProperty(const QByteArray &name)
Delete a non-KDE or non-standard custom calendar property.
virtual void customPropertyUpdate()
Called before a custom property will be changed.
QString customProperty(const QByteArray &app, const QByteArray &key) const
Return the value of a custom calendar property.
CustomProperties()
Constructs an empty custom properties instance.
QMap< QByteArray, QString > customProperties() const
Returns all custom calendar property key/value pairs.
static QByteArray customPropertyName(const QByteArray &app, const QByteArray &key)
Validate and return the full name of a custom calendar property.
virtual void customPropertyUpdated()
Called when a custom property has been changed.
QString nonKDECustomProperty(const QByteArray &name) const
Return the value of a non-KDE or non-standard custom calendar property.
This file is part of the API for handling calendar data and defines the CustomProperties class.
Namespace for all KCalendarCore types.
Definition alarm.h:37
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition alarm.cpp:833
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition alarm.cpp:820
QString name(StandardShortcut id)
bool isEmpty() const const
const_iterator cbegin() const const
const_iterator cend() const const
size_type count() const const
iterator insert(const Key &key, const T &value)
const QChar * constData() const const
bool isNull() const const
qsizetype length() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
bool operator==(const QGraphicsApiFilter &reference, const QGraphicsApiFilter &sample)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:47 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.