• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

KCal Library

customproperties.cpp

Go to the documentation of this file.
00001 /*
00002   This file is part of the kcal 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 #include <kdebug.h>
00034 #include <QtCore/QByteArray>
00035 
00036 using namespace KCal;
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 };
00047 
00048 bool CustomProperties::Private::operator==( const CustomProperties::Private &other ) const
00049 {
00050   if ( mProperties.count() != other.mProperties.count() ) {
00051     return false;
00052   }
00053   for ( QMap<QByteArray, QString>::ConstIterator it = mProperties.begin();
00054         it != mProperties.end(); ++it ) {
00055     QMap<QByteArray, QString>::ConstIterator itOther =
00056                other.mProperties.find( it.key() );
00057     if ( itOther == other.mProperties.end() || itOther.value() != it.value() ) {
00058       return false;
00059     }
00060   }
00061   return true;
00062 }
00063 //@endcond
00064 
00065 CustomProperties::CustomProperties()
00066   : d( new Private )
00067 {
00068 }
00069 
00070 CustomProperties::CustomProperties( const CustomProperties &cp )
00071   : d( new Private( *cp.d ) )
00072 {
00073 }
00074 
00075 CustomProperties &CustomProperties::operator=( const CustomProperties &other )
00076 {
00077   // check for self assignment
00078   if ( &other == this ) {
00079     return *this;
00080   }
00081 
00082   *d = *other.d;
00083   return *this;
00084 }
00085 
00086 CustomProperties::~CustomProperties()
00087 {
00088   delete d;
00089 }
00090 
00091 bool CustomProperties::operator==( const CustomProperties &other ) const
00092 {
00093   return *d == *other.d;
00094 }
00095 
00096 void CustomProperties::setCustomProperty( const QByteArray &app, const QByteArray &key,
00097                                           const QString &value )
00098 {
00099   if ( value.isNull() || key.isEmpty() || app.isEmpty() ) {
00100     return;
00101   }
00102   QByteArray property = "X-KDE-" + app + '-' + key;
00103   if ( !checkName( property ) ) {
00104     return;
00105   }
00106   d->mProperties[property] = value;
00107   customPropertyUpdated();
00108 }
00109 
00110 void CustomProperties::removeCustomProperty( const QByteArray &app, const QByteArray &key )
00111 {
00112   removeNonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
00113 }
00114 
00115 QString CustomProperties::customProperty( const QByteArray &app, const QByteArray &key ) const
00116 {
00117   return nonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
00118 }
00119 
00120 QByteArray CustomProperties::customPropertyName( const QByteArray &app, const QByteArray &key )
00121 {
00122   QByteArray property( "X-KDE-" + app + '-' + key );
00123   if ( !checkName( property ) ) {
00124     return QByteArray();
00125   }
00126   return property;
00127 }
00128 
00129 void CustomProperties::setNonKDECustomProperty( const QByteArray &name, const QString &value )
00130 {
00131   if ( value.isNull() || !checkName( name ) ) {
00132     return;
00133   }
00134   d->mProperties[name] = value;
00135   customPropertyUpdated();
00136 }
00137 
00138 void CustomProperties::removeNonKDECustomProperty( const QByteArray &name )
00139 {
00140   if ( d->mProperties.remove( name ) ) {
00141     customPropertyUpdated();
00142   }
00143 }
00144 
00145 QString CustomProperties::nonKDECustomProperty( const QByteArray &name ) const
00146 {
00147   return d->mProperties.value( name );
00148 }
00149 
00150 void CustomProperties::setCustomProperties( const QMap<QByteArray, QString> &properties )
00151 {
00152   bool changed = false;
00153   for ( QMap<QByteArray, QString>::ConstIterator it = properties.begin();
00154         it != properties.end();  ++it ) {
00155     // Validate the property name and convert any null string to empty string
00156     if ( checkName( it.key() ) ) {
00157       d->mProperties[it.key()] = it.value().isNull() ? QString( "" ) : it.value();
00158       changed = true;
00159     }
00160   }
00161   if ( changed ) {
00162     customPropertyUpdated();
00163   }
00164 }
00165 
00166 QMap<QByteArray, QString> CustomProperties::customProperties() const
00167 {
00168   return d->mProperties;
00169 }
00170 
00171 //@cond PRIVATE
00172 bool checkName( const QByteArray &name )
00173 {
00174   // Check that the property name starts with 'X-' and contains
00175   // only the permitted characters
00176   const char *n = name;
00177   int len = name.length();
00178   if ( len < 2 ||  n[0] != 'X' || n[1] != '-' ) {
00179     return false;
00180   }
00181   for ( int i = 2;  i < len;  ++i ) {
00182     char ch = n[i];
00183     if ( ( ch >= 'A' && ch <= 'Z' ) ||
00184          ( ch >= 'a' && ch <= 'z' ) ||
00185          ( ch >= '0' && ch <= '9' ) ||
00186          ch == '-' ) {
00187       continue;
00188     }
00189     return false;   // invalid character found
00190   }
00191   return true;
00192 }
00193 //@endcond

KCal Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • 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
Generated for KDE-PIM Libraries by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal