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

KCalCore Library

customproperties.cpp
Go to the documentation of this file.
00001 /*
00002   This file is part of the kcalcore library.
00003 
00004   Copyright (c) 2002,2006,2010 David Jarvie <djarvie@kde.org>
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Library General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015 
00016   You should have received a copy of the GNU Library General Public License
00017   along with this library; see the file COPYING.LIB.  If not, write to
00018   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019   Boston, MA 02110-1301, USA.
00020 */
00032 #include "customproperties.h"
00033 
00034 #include <QDataStream>
00035 
00036 using namespace KCalCore;
00037 
00038 //@cond PRIVATE
00039 static bool checkName( const QByteArray &name );
00040 
00041 class CustomProperties::Private
00042 {
00043   public:
00044     bool operator==( const Private &other ) const;
00045     QMap<QByteArray, QString> mProperties;   // custom calendar properties
00046     QMap<QByteArray, QString> mPropertyParameters;
00047 };
00048 
00049 bool CustomProperties::Private::operator==( const CustomProperties::Private &other ) const
00050 {
00051   if ( mProperties.count() != other.mProperties.count() ) {
00052     return false;
00053   }
00054   for ( QMap<QByteArray, QString>::ConstIterator it = mProperties.begin();
00055         it != mProperties.end(); ++it ) {
00056     QMap<QByteArray, QString>::ConstIterator itOther =
00057                other.mProperties.find( it.key() );
00058     if ( itOther == other.mProperties.end() || itOther.value() != it.value() ) {
00059       return false;
00060     }
00061   }
00062   for ( QMap<QByteArray, QString>::ConstIterator it = mPropertyParameters.begin();
00063         it != mPropertyParameters.end(); ++it ) {
00064     QMap<QByteArray, QString>::ConstIterator itOther =
00065                other.mPropertyParameters.find( it.key() );
00066     if ( itOther == other.mPropertyParameters.end() || itOther.value() != it.value() ) {
00067       return false;
00068     }
00069   }
00070   return true;
00071 }
00072 //@endcond
00073 
00074 CustomProperties::CustomProperties()
00075   : d( new Private )
00076 {
00077 }
00078 
00079 CustomProperties::CustomProperties( const CustomProperties &cp )
00080   : d( new Private( *cp.d ) )
00081 {
00082 }
00083 
00084 CustomProperties &CustomProperties::operator=( const CustomProperties &other )
00085 {
00086   // check for self assignment
00087   if ( &other == this ) {
00088     return *this;
00089   }
00090 
00091   *d = *other.d;
00092   return *this;
00093 }
00094 
00095 CustomProperties::~CustomProperties()
00096 {
00097   delete d;
00098 }
00099 
00100 bool CustomProperties::operator==( const CustomProperties &other ) const
00101 {
00102   return *d == *other.d;
00103 }
00104 
00105 void CustomProperties::setCustomProperty( const QByteArray &app, const QByteArray &key,
00106                                           const QString &value )
00107 {
00108   if ( value.isNull() || key.isEmpty() || app.isEmpty() ) {
00109     return;
00110   }
00111   QByteArray property = "X-KDE-" + app + '-' + key;
00112   if ( !checkName( property ) ) {
00113     return;
00114   }
00115   customPropertyUpdate();
00116   d->mProperties[property] = value;
00117   customPropertyUpdated();
00118 }
00119 
00120 void CustomProperties::removeCustomProperty( const QByteArray &app, const QByteArray &key )
00121 {
00122   removeNonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
00123 }
00124 
00125 QString CustomProperties::customProperty( const QByteArray &app, const QByteArray &key ) const
00126 {
00127   return nonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
00128 }
00129 
00130 QByteArray CustomProperties::customPropertyName( const QByteArray &app, const QByteArray &key )
00131 {
00132   QByteArray property( "X-KDE-" + app + '-' + key );
00133   if ( !checkName( property ) ) {
00134     return QByteArray();
00135   }
00136   return property;
00137 }
00138 
00139 void CustomProperties::setNonKDECustomProperty( const QByteArray &name, const QString &value,
00140                                                 const QString &parameters )
00141 {
00142   if ( value.isNull() || !checkName( name ) ) {
00143     return;
00144   }
00145   customPropertyUpdate();
00146   d->mProperties[name] = value;
00147   d->mPropertyParameters[name] = parameters;
00148   customPropertyUpdated();
00149 }
00150 void CustomProperties::removeNonKDECustomProperty( const QByteArray &name )
00151 {
00152   if ( d->mProperties.contains( name ) ) {
00153     customPropertyUpdate();
00154     d->mProperties.remove( name );
00155     d->mPropertyParameters.remove( name );
00156     customPropertyUpdated();
00157   }
00158 }
00159 
00160 QString CustomProperties::nonKDECustomProperty( const QByteArray &name ) const
00161 {
00162   return d->mProperties.value( name );
00163 }
00164 
00165 QString CustomProperties::nonKDECustomPropertyParameters( const QByteArray &name ) const
00166 {
00167   return d->mPropertyParameters.value( name );
00168 }
00169 
00170 void CustomProperties::setCustomProperties( const QMap<QByteArray, QString> &properties )
00171 {
00172   bool changed = false;
00173   for ( QMap<QByteArray, QString>::ConstIterator it = properties.begin();
00174         it != properties.end();  ++it ) {
00175     // Validate the property name and convert any null string to empty string
00176     if ( checkName( it.key() ) ) {
00177       d->mProperties[it.key()] = it.value().isNull() ? QString( "" ) : it.value();
00178       if ( !changed ) {
00179         customPropertyUpdate();
00180       }
00181       changed = true;
00182     }
00183   }
00184   if ( changed ) {
00185     customPropertyUpdated();
00186   }
00187 }
00188 
00189 QMap<QByteArray, QString> CustomProperties::customProperties() const
00190 {
00191   return d->mProperties;
00192 }
00193 
00194 void CustomProperties::customPropertyUpdate()
00195 {
00196 }
00197 
00198 void CustomProperties::customPropertyUpdated()
00199 {
00200 }
00201 
00202 void CustomProperties::virtual_hook( int id, void *data )
00203 {
00204   Q_UNUSED( id );
00205   Q_UNUSED( data );
00206   Q_ASSERT( false );
00207 }
00208 
00209 //@cond PRIVATE
00210 bool checkName( const QByteArray &name )
00211 {
00212   // Check that the property name starts with 'X-' and contains
00213   // only the permitted characters
00214   const char *n = name;
00215   int len = name.length();
00216   if ( len < 2 ||  n[0] != 'X' || n[1] != '-' ) {
00217     return false;
00218   }
00219   for ( int i = 2;  i < len;  ++i ) {
00220     char ch = n[i];
00221     if ( ( ch >= 'A' && ch <= 'Z' ) ||
00222          ( ch >= 'a' && ch <= 'z' ) ||
00223          ( ch >= '0' && ch <= '9' ) ||
00224          ch == '-' ) {
00225       continue;
00226     }
00227     return false;   // invalid character found
00228   }
00229   return true;
00230 }
00231 //@endcond
00232 
00233 QDataStream &KCalCore::operator<<( QDataStream &stream,
00234                                    const KCalCore::CustomProperties &properties )
00235 {
00236   return stream << properties.d->mProperties
00237                 << properties.d->mPropertyParameters;
00238 }
00239 
00240 QDataStream &KCalCore::operator>>( QDataStream &stream,
00241                                    KCalCore::CustomProperties &properties )
00242 {
00243   return stream >> properties.d->mProperties
00244                 >> properties.d->mPropertyParameters;
00245 }
00246 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:35:38 by doxygen 1.7.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.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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