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()
00076 {
00077 delete d;
00078 }
00079
00080 bool CustomProperties::operator==( const CustomProperties &other ) const
00081 {
00082 return *d == *other.d;
00083 }
00084
00085 void CustomProperties::setCustomProperty( const QByteArray &app, const QByteArray &key,
00086 const QString &value )
00087 {
00088 if ( value.isNull() || key.isEmpty() || app.isEmpty() ) {
00089 return;
00090 }
00091 QByteArray property = "X-KDE-" + app + '-' + key;
00092 if ( !checkName( property ) ) {
00093 return;
00094 }
00095 d->mProperties[property] = value;
00096 customPropertyUpdated();
00097 }
00098
00099 void CustomProperties::removeCustomProperty( const QByteArray &app, const QByteArray &key )
00100 {
00101 removeNonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
00102 }
00103
00104 QString CustomProperties::customProperty( const QByteArray &app, const QByteArray &key ) const
00105 {
00106 return nonKDECustomProperty( QByteArray( "X-KDE-" + app + '-' + key ) );
00107 }
00108
00109 void CustomProperties::setNonKDECustomProperty( const QByteArray &name, const QString &value )
00110 {
00111 if ( value.isNull() || !checkName( name ) ) {
00112 return;
00113 }
00114 d->mProperties[name] = value;
00115 customPropertyUpdated();
00116 }
00117
00118 void CustomProperties::removeNonKDECustomProperty( const QByteArray &name )
00119 {
00120 QMap<QByteArray, QString>::Iterator it = d->mProperties.find( name );
00121 if ( it != d->mProperties.end() ) {
00122 d->mProperties.erase( it );
00123 customPropertyUpdated();
00124 }
00125 }
00126
00127 QString CustomProperties::nonKDECustomProperty( const QByteArray &name ) const
00128 {
00129 QMap<QByteArray, QString>::ConstIterator it = d->mProperties.find( name );
00130 if ( it == d->mProperties.end() ) {
00131 return QString();
00132 }
00133 return it.value();
00134 }
00135
00136 void CustomProperties::setCustomProperties( const QMap<QByteArray, QString> &properties )
00137 {
00138 bool changed = false;
00139 for ( QMap<QByteArray, QString>::ConstIterator it = properties.begin();
00140 it != properties.end(); ++it ) {
00141
00142 if ( checkName( it.key() ) ) {
00143 d->mProperties[it.key()] = it.value().isNull() ? QString( "" ) : it.value();
00144 changed = true;
00145 }
00146 }
00147 if ( changed ) {
00148 customPropertyUpdated();
00149 }
00150 }
00151
00152 QMap<QByteArray, QString> CustomProperties::customProperties() const
00153 {
00154 return d->mProperties;
00155 }
00156
00157
00158 bool checkName( const QByteArray &name )
00159 {
00160
00161
00162 const char *n = name;
00163 int len = name.length();
00164 if ( len < 2 || n[0] != 'X' || n[1] != '-' ) {
00165 return false;
00166 }
00167 for ( int i = 2; i < len; ++i ) {
00168 char ch = n[i];
00169 if ( ch >= 'A' && ch <= 'Z' ||
00170 ch >= 'a' && ch <= 'z' ||
00171 ch >= '0' && ch <= '9' ||
00172 ch == '-' ) {
00173 continue;
00174 }
00175 return false;
00176 }
00177 return true;
00178 }
00179