• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

KCalCore Library

  • kcalcore
customproperties.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  Copyright (c) 2002,2006,2010 David Jarvie <djarvie@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
32 #include "customproperties.h"
33 
34 #include <QDataStream>
35 
36 using namespace KCalCore;
37 
38 //@cond PRIVATE
39 static bool checkName( const QByteArray &name );
40 
41 class CustomProperties::Private
42 {
43  public:
44  bool operator==( const Private &other ) const;
45  QMap<QByteArray, QString> mProperties; // custom calendar properties
46  QMap<QByteArray, QString> mPropertyParameters;
47 
48  // Volatile properties are not written back to the serialized format and are not compared in operator==
49  // They are only used for runtime purposes and are not part of the payload.
50  QMap<QByteArray, QString> mVolatileProperties;
51 
52 
53  bool isVolatileProperty( const QString &name ) const
54  {
55  return name.startsWith( "X-KDE-VOLATILE" );
56  }
57 };
58 
59 bool CustomProperties::Private::operator==( const CustomProperties::Private &other ) const
60 {
61  if ( mProperties.count() != other.mProperties.count() ) {
62  return false;
63  }
64  for ( QMap<QByteArray, QString>::ConstIterator it = mProperties.begin();
65  it != mProperties.end(); ++it ) {
66  QMap<QByteArray, QString>::ConstIterator itOther =
67  other.mProperties.find( it.key() );
68  if ( itOther == other.mProperties.end() || itOther.value() != it.value() ) {
69  return false;
70  }
71  }
72  for ( QMap<QByteArray, QString>::ConstIterator it = mPropertyParameters.begin();
73  it != mPropertyParameters.end(); ++it ) {
74  QMap<QByteArray, QString>::ConstIterator itOther =
75  other.mPropertyParameters.find( it.key() );
76  if ( itOther == other.mPropertyParameters.end() || itOther.value() != it.value() ) {
77  return false;
78  }
79  }
80  return true;
81 }
82 //@endcond
83 
84 CustomProperties::CustomProperties()
85  : d( new Private )
86 {
87 }
88 
89 CustomProperties::CustomProperties( const CustomProperties &cp )
90  : d( new Private( *cp.d ) )
91 {
92 }
93 
94 CustomProperties &CustomProperties::operator=( const CustomProperties &other )
95 {
96  // check for self assignment
97  if ( &other == this ) {
98  return *this;
99  }
100 
101  *d = *other.d;
102  return *this;
103 }
104 
105 CustomProperties::~CustomProperties()
106 {
107  delete d;
108 }
109 
110 bool CustomProperties::operator==( const CustomProperties &other ) const
111 {
112  return *d == *other.d;
113 }
114 
115 void CustomProperties::setCustomProperty( const QByteArray &app, const QByteArray &key,
116  const QString &value )
117 {
118  if ( value.isNull() || key.isEmpty() || app.isEmpty() ) {
119  return;
120  }
121  QByteArray property = "X-KDE-" + app + '-' + key;
122  if ( !checkName( property ) ) {
123  return;
124  }
125  customPropertyUpdate();
126 
127  if ( d->isVolatileProperty( property ) ) {
128  d->mVolatileProperties[property] = value;
129  } else {
130  d->mProperties[property] = value;
131  }
132 
133  customPropertyUpdated();
134 }
135 
136 void CustomProperties::removeCustomProperty( const QByteArray &app, const QByteArray &key )
137 {
138  removeNonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
139 }
140 
141 QString CustomProperties::customProperty( const QByteArray &app, const QByteArray &key ) const
142 {
143  return nonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
144 }
145 
146 QByteArray CustomProperties::customPropertyName( const QByteArray &app, const QByteArray &key )
147 {
148  QByteArray property( "X-KDE-" + app + '-' + key );
149  if ( !checkName( property ) ) {
150  return QByteArray();
151  }
152  return property;
153 }
154 
155 void CustomProperties::setNonKDECustomProperty( const QByteArray &name, const QString &value,
156  const QString &parameters )
157 {
158  if ( value.isNull() || !checkName( name ) ) {
159  return;
160  }
161  customPropertyUpdate();
162  d->mProperties[name] = value;
163  d->mPropertyParameters[name] = parameters;
164  customPropertyUpdated();
165 }
166 void CustomProperties::removeNonKDECustomProperty( const QByteArray &name )
167 {
168  if ( d->mProperties.contains( name ) ) {
169  customPropertyUpdate();
170  d->mProperties.remove( name );
171  d->mPropertyParameters.remove( name );
172  customPropertyUpdated();
173  } else if ( d->mVolatileProperties.contains( name ) ) {
174  customPropertyUpdate();
175  d->mVolatileProperties.remove( name );
176  customPropertyUpdated();
177  }
178 }
179 
180 QString CustomProperties::nonKDECustomProperty( const QByteArray &name ) const
181 {
182  return d->isVolatileProperty( name ) ? d->mVolatileProperties.value( name ) : d->mProperties.value( name );
183 }
184 
185 QString CustomProperties::nonKDECustomPropertyParameters( const QByteArray &name ) const
186 {
187  return d->mPropertyParameters.value( name );
188 }
189 
190 void CustomProperties::setCustomProperties( const QMap<QByteArray, QString> &properties )
191 {
192  bool changed = false;
193  for ( QMap<QByteArray, QString>::ConstIterator it = properties.begin();
194  it != properties.end(); ++it ) {
195  // Validate the property name and convert any null string to empty string
196  if ( checkName( it.key() ) ) {
197  if ( d->isVolatileProperty( it.key() ) ) {
198  d->mVolatileProperties[it.key()] = it.value().isNull() ? QString( "" ) : it.value();
199  } else {
200  d->mProperties[it.key()] = it.value().isNull() ? QString( "" ) : it.value();
201  }
202  if ( !changed ) {
203  customPropertyUpdate();
204  }
205  changed = true;
206  }
207  }
208  if ( changed ) {
209  customPropertyUpdated();
210  }
211 }
212 
213 QMap<QByteArray, QString> CustomProperties::customProperties() const
214 {
215  QMap<QByteArray, QString> result;
216  result.unite( d->mProperties );
217  result.unite( d->mVolatileProperties );
218 
219  return result;
220 }
221 
222 void CustomProperties::customPropertyUpdate()
223 {
224 }
225 
226 void CustomProperties::customPropertyUpdated()
227 {
228 }
229 
230 void CustomProperties::virtual_hook( int id, void *data )
231 {
232  Q_UNUSED( id );
233  Q_UNUSED( data );
234  Q_ASSERT( false );
235 }
236 
237 //@cond PRIVATE
238 bool checkName( const QByteArray &name )
239 {
240  // Check that the property name starts with 'X-' and contains
241  // only the permitted characters
242  const char *n = name;
243  int len = name.length();
244  if ( len < 2 || n[0] != 'X' || n[1] != '-' ) {
245  return false;
246  }
247  for ( int i = 2; i < len; ++i ) {
248  char ch = n[i];
249  if ( ( ch >= 'A' && ch <= 'Z' ) ||
250  ( ch >= 'a' && ch <= 'z' ) ||
251  ( ch >= '0' && ch <= '9' ) ||
252  ch == '-' ) {
253  continue;
254  }
255  return false; // invalid character found
256  }
257  return true;
258 }
259 //@endcond
260 
261 QDataStream &KCalCore::operator<<( QDataStream &stream,
262  const KCalCore::CustomProperties &properties )
263 {
264  return stream << properties.d->mProperties
265  << properties.d->mPropertyParameters;
266 }
267 
268 QDataStream &KCalCore::operator>>( QDataStream &stream,
269  KCalCore::CustomProperties &properties )
270 {
271  properties.d->mVolatileProperties.clear();
272  return stream >> properties.d->mProperties
273  >> properties.d->mPropertyParameters;
274 }
275 
customproperties.h
This file is part of the API for handling calendar data and defines the CustomProperties class...
KCalCore::CustomProperties::removeNonKDECustomProperty
void removeNonKDECustomProperty(const QByteArray &name)
Delete a non-KDE or non-standard custom calendar property.
Definition: customproperties.cpp:166
KCalCore::CustomProperties::customProperty
QString customProperty(const QByteArray &app, const QByteArray &key) const
Return the value of a custom calendar property.
Definition: customproperties.cpp:141
KCalCore::CustomProperties::operator=
CustomProperties & operator=(const CustomProperties &other)
Assignment operator.
Definition: customproperties.cpp:94
KCalCore::CustomProperties::nonKDECustomProperty
QString nonKDECustomProperty(const QByteArray &name) const
Return the value of a non-KDE or non-standard custom calendar property.
Definition: customproperties.cpp:180
KCalCore::CustomProperties::setNonKDECustomProperty
void setNonKDECustomProperty(const QByteArray &name, const QString &value, const QString &parameters=QString())
Create or modify a non-KDE or non-standard custom calendar property.
Definition: customproperties.cpp:155
KCalCore::CustomProperties::customPropertyUpdated
virtual void customPropertyUpdated()
Called when a custom property has been changed.
Definition: customproperties.cpp:226
KCalCore::CustomProperties::removeCustomProperty
void removeCustomProperty(const QByteArray &app, const QByteArray &key)
Delete a custom calendar property.
Definition: customproperties.cpp:136
KCalCore::CustomProperties::setCustomProperty
void setCustomProperty(const QByteArray &app, const QByteArray &key, const QString &value)
Create or modify a custom calendar property.
Definition: customproperties.cpp:115
KCalCore::CustomProperties::customPropertyUpdate
virtual void customPropertyUpdate()
Called before a custom property will be changed.
Definition: customproperties.cpp:222
KCalCore::CustomProperties::customPropertyName
static QByteArray customPropertyName(const QByteArray &app, const QByteArray &key)
Validate and return the full name of a custom calendar property.
Definition: customproperties.cpp:146
KCalCore::CustomProperties::operator==
bool operator==(const CustomProperties &properties) const
Compare this with properties for equality.
Definition: customproperties.cpp:110
KCalCore::CustomProperties::setCustomProperties
void setCustomProperties(const QMap< QByteArray, QString > &properties)
Initialise the alarm&#39;s custom calendar properties to the specified key/value pairs.
Definition: customproperties.cpp:190
KCalCore::operator<<
KCALCORE_EXPORT QDataStream & operator<<(QDataStream &stream, const KCalCore::Attendee::Ptr &attendee)
Serializes an Attendee object into a data stream.
Definition: attendee.cpp:186
KCalCore::CustomProperties
A class to manage custom calendar properties.
Definition: customproperties.h:51
KCalCore::CustomProperties::CustomProperties
CustomProperties()
Constructs an empty custom properties instance.
Definition: customproperties.cpp:84
KCalCore::CustomProperties::virtual_hook
virtual void virtual_hook(int id, void *data)
Definition: customproperties.cpp:230
KCalCore::CustomProperties::~CustomProperties
virtual ~CustomProperties()
Destructor.
Definition: customproperties.cpp:105
KCalCore::CustomProperties::nonKDECustomPropertyParameters
QString nonKDECustomPropertyParameters(const QByteArray &name) const
Return the parameters of a non-KDE or non-standard custom calendar property.
Definition: customproperties.cpp:185
KCalCore::operator>>
KCALCORE_EXPORT QDataStream & operator>>(QDataStream &stream, KCalCore::Attendee::Ptr &attendee)
Initializes an Attendee object from a data stream.
Definition: attendee.cpp:199
KCalCore::CustomProperties::customProperties
QMap< QByteArray, QString > customProperties() const
Returns all custom calendar property key/value pairs.
Definition: customproperties.cpp:213
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:02:04 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KCalCore Library

Skip menu "KCalCore Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal