KCal Library
customproperties.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00032 #include "customproperties.h"
00033 #include <kdebug.h>
00034 #include <QtCore/QByteArray>
00035
00036 using namespace KCal;
00037
00038
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;
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
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 *d = *other.d;
00078 return *this;
00079 }
00080
00081 CustomProperties::~CustomProperties()
00082 {
00083 delete d;
00084 }
00085
00086 bool CustomProperties::operator==( const CustomProperties &other ) const
00087 {
00088 return *d == *other.d;
00089 }
00090
00091 void CustomProperties::setCustomProperty( const QByteArray &app, const QByteArray &key,
00092 const QString &value )
00093 {
00094 if ( value.isNull() || key.isEmpty() || app.isEmpty() ) {
00095 return;
00096 }
00097 QByteArray property = "X-KDE-" + app + '-' + key;
00098 if ( !checkName( property ) ) {
00099 return;
00100 }
00101 d->mProperties[property] = value;
00102 customPropertyUpdated();
00103 }
00104
00105 void CustomProperties::removeCustomProperty( const QByteArray &app, const QByteArray &key )
00106 {
00107 removeNonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
00108 }
00109
00110 QString CustomProperties::customProperty( const QByteArray &app, const QByteArray &key ) const
00111 {
00112 return nonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
00113 }
00114
00115 void CustomProperties::setNonKDECustomProperty( const QByteArray &name, const QString &value )
00116 {
00117 if ( value.isNull() || !checkName( name ) ) {
00118 return;
00119 }
00120 d->mProperties[name] = value;
00121 customPropertyUpdated();
00122 }
00123
00124 void CustomProperties::removeNonKDECustomProperty( const QByteArray &name )
00125 {
00126 QMap<QByteArray, QString>::Iterator it = d->mProperties.find( name );
00127 if ( it != d->mProperties.end() ) {
00128 d->mProperties.erase( it );
00129 customPropertyUpdated();
00130 }
00131 }
00132
00133 QString CustomProperties::nonKDECustomProperty( const QByteArray &name ) const
00134 {
00135 QMap<QByteArray, QString>::ConstIterator it = d->mProperties.constFind( name );
00136 if ( it == d->mProperties.constEnd() ) {
00137 return QString();
00138 }
00139 return it.value();
00140 }
00141
00142 void CustomProperties::setCustomProperties( const QMap<QByteArray, QString> &properties )
00143 {
00144 bool changed = false;
00145 for ( QMap<QByteArray, QString>::ConstIterator it = properties.begin();
00146 it != properties.end(); ++it ) {
00147
00148 if ( checkName( it.key() ) ) {
00149 d->mProperties[it.key()] = it.value().isNull() ? QString( "" ) : it.value();
00150 changed = true;
00151 }
00152 }
00153 if ( changed ) {
00154 customPropertyUpdated();
00155 }
00156 }
00157
00158 QMap<QByteArray, QString> CustomProperties::customProperties() const
00159 {
00160 return d->mProperties;
00161 }
00162
00163
00164 bool checkName( const QByteArray &name )
00165 {
00166
00167
00168 const char *n = name;
00169 int len = name.length();
00170 if ( len < 2 || n[0] != 'X' || n[1] != '-' ) {
00171 return false;
00172 }
00173 for ( int i = 2; i < len; ++i ) {
00174 char ch = n[i];
00175 if ( ( ch >= 'A' && ch <= 'Z' ) ||
00176 ( ch >= 'a' && ch <= 'z' ) ||
00177 ( ch >= '0' && ch <= '9' ) ||
00178 ch == '-' ) {
00179 continue;
00180 }
00181 return false;
00182 }
00183 return true;
00184 }
00185