• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 
49 bool CustomProperties::Private::operator==( const CustomProperties::Private &other ) const
50 {
51  if ( mProperties.count() != other.mProperties.count() ) {
52  return false;
53  }
54  for ( QMap<QByteArray, QString>::ConstIterator it = mProperties.begin();
55  it != mProperties.end(); ++it ) {
56  QMap<QByteArray, QString>::ConstIterator itOther =
57  other.mProperties.find( it.key() );
58  if ( itOther == other.mProperties.end() || itOther.value() != it.value() ) {
59  return false;
60  }
61  }
62  for ( QMap<QByteArray, QString>::ConstIterator it = mPropertyParameters.begin();
63  it != mPropertyParameters.end(); ++it ) {
64  QMap<QByteArray, QString>::ConstIterator itOther =
65  other.mPropertyParameters.find( it.key() );
66  if ( itOther == other.mPropertyParameters.end() || itOther.value() != it.value() ) {
67  return false;
68  }
69  }
70  return true;
71 }
72 //@endcond
73 
74 CustomProperties::CustomProperties()
75  : d( new Private )
76 {
77 }
78 
79 CustomProperties::CustomProperties( const CustomProperties &cp )
80  : d( new Private( *cp.d ) )
81 {
82 }
83 
84 CustomProperties &CustomProperties::operator=( const CustomProperties &other )
85 {
86  // check for self assignment
87  if ( &other == this ) {
88  return *this;
89  }
90 
91  *d = *other.d;
92  return *this;
93 }
94 
95 CustomProperties::~CustomProperties()
96 {
97  delete d;
98 }
99 
100 bool CustomProperties::operator==( const CustomProperties &other ) const
101 {
102  return *d == *other.d;
103 }
104 
105 void CustomProperties::setCustomProperty( const QByteArray &app, const QByteArray &key,
106  const QString &value )
107 {
108  if ( value.isNull() || key.isEmpty() || app.isEmpty() ) {
109  return;
110  }
111  QByteArray property = "X-KDE-" + app + '-' + key;
112  if ( !checkName( property ) ) {
113  return;
114  }
115  customPropertyUpdate();
116  d->mProperties[property] = value;
117  customPropertyUpdated();
118 }
119 
120 void CustomProperties::removeCustomProperty( const QByteArray &app, const QByteArray &key )
121 {
122  removeNonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
123 }
124 
125 QString CustomProperties::customProperty( const QByteArray &app, const QByteArray &key ) const
126 {
127  return nonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
128 }
129 
130 QByteArray CustomProperties::customPropertyName( const QByteArray &app, const QByteArray &key )
131 {
132  QByteArray property( "X-KDE-" + app + '-' + key );
133  if ( !checkName( property ) ) {
134  return QByteArray();
135  }
136  return property;
137 }
138 
139 void CustomProperties::setNonKDECustomProperty( const QByteArray &name, const QString &value,
140  const QString &parameters )
141 {
142  if ( value.isNull() || !checkName( name ) ) {
143  return;
144  }
145  customPropertyUpdate();
146  d->mProperties[name] = value;
147  d->mPropertyParameters[name] = parameters;
148  customPropertyUpdated();
149 }
150 void CustomProperties::removeNonKDECustomProperty( const QByteArray &name )
151 {
152  if ( d->mProperties.contains( name ) ) {
153  customPropertyUpdate();
154  d->mProperties.remove( name );
155  d->mPropertyParameters.remove( name );
156  customPropertyUpdated();
157  }
158 }
159 
160 QString CustomProperties::nonKDECustomProperty( const QByteArray &name ) const
161 {
162  return d->mProperties.value( name );
163 }
164 
165 QString CustomProperties::nonKDECustomPropertyParameters( const QByteArray &name ) const
166 {
167  return d->mPropertyParameters.value( name );
168 }
169 
170 void CustomProperties::setCustomProperties( const QMap<QByteArray, QString> &properties )
171 {
172  bool changed = false;
173  for ( QMap<QByteArray, QString>::ConstIterator it = properties.begin();
174  it != properties.end(); ++it ) {
175  // Validate the property name and convert any null string to empty string
176  if ( checkName( it.key() ) ) {
177  d->mProperties[it.key()] = it.value().isNull() ? QString( "" ) : it.value();
178  if ( !changed ) {
179  customPropertyUpdate();
180  }
181  changed = true;
182  }
183  }
184  if ( changed ) {
185  customPropertyUpdated();
186  }
187 }
188 
189 QMap<QByteArray, QString> CustomProperties::customProperties() const
190 {
191  return d->mProperties;
192 }
193 
194 void CustomProperties::customPropertyUpdate()
195 {
196 }
197 
198 void CustomProperties::customPropertyUpdated()
199 {
200 }
201 
202 void CustomProperties::virtual_hook( int id, void *data )
203 {
204  Q_UNUSED( id );
205  Q_UNUSED( data );
206  Q_ASSERT( false );
207 }
208 
209 //@cond PRIVATE
210 bool checkName( const QByteArray &name )
211 {
212  // Check that the property name starts with 'X-' and contains
213  // only the permitted characters
214  const char *n = name;
215  int len = name.length();
216  if ( len < 2 || n[0] != 'X' || n[1] != '-' ) {
217  return false;
218  }
219  for ( int i = 2; i < len; ++i ) {
220  char ch = n[i];
221  if ( ( ch >= 'A' && ch <= 'Z' ) ||
222  ( ch >= 'a' && ch <= 'z' ) ||
223  ( ch >= '0' && ch <= '9' ) ||
224  ch == '-' ) {
225  continue;
226  }
227  return false; // invalid character found
228  }
229  return true;
230 }
231 //@endcond
232 
233 QDataStream &KCalCore::operator<<( QDataStream &stream,
234  const KCalCore::CustomProperties &properties )
235 {
236  return stream << properties.d->mProperties
237  << properties.d->mPropertyParameters;
238 }
239 
240 QDataStream &KCalCore::operator>>( QDataStream &stream,
241  KCalCore::CustomProperties &properties )
242 {
243  return stream >> properties.d->mProperties
244  >> properties.d->mPropertyParameters;
245 }
246 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:24:51 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 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