KCalCore 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
00034 #include <QDataStream>
00035
00036 using namespace KCalCore;
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 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
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
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 ¶meters )
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
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
00210 bool checkName( const QByteArray &name )
00211 {
00212
00213
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;
00228 }
00229 return true;
00230 }
00231
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