libkcal

customproperties.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2002 David Jarvie <software@astrojar.org.uk>
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 */
00021 
00022 #include "customproperties.h"
00023 
00024 #include <kdebug.h>
00025 
00026 using namespace KCal;
00027 
00028 CustomProperties::CustomProperties()
00029 {
00030 }
00031 
00032 CustomProperties::CustomProperties(const CustomProperties &cp)
00033   : mProperties(cp.mProperties)
00034 {
00035 }
00036 
00037 CustomProperties::~CustomProperties()
00038 {
00039 }
00040 
00041 bool CustomProperties::operator==( const CustomProperties &other ) const
00042 {
00043   if ( mProperties.count() != other.mProperties.count() ) return false;
00044   QMap<QCString, QString>::ConstIterator it;
00045   for( it = mProperties.begin(); it != mProperties.end(); ++it ) {
00046     QMap<QCString, QString>::ConstIterator itOther =
00047       other.mProperties.find( it.key() );
00048 
00049     if ( itOther == other.mProperties.end() ) {
00050       return false;
00051     }
00052     if ( itOther.data() != it.data() ) return false;
00053   }
00054 
00055   return true;
00056 }
00057 
00058 void CustomProperties::setCustomProperty(const QCString &app, const QCString &key,
00059                                          const QString &value)
00060 {
00061   if (value.isNull() || key.isEmpty() || app.isEmpty())
00062     return;
00063   QCString property = "X-KDE-" + app + "-" + key;
00064   if (!checkName(property))
00065     return;
00066   mProperties[property] = value;
00067 }
00068 
00069 void CustomProperties::removeCustomProperty(const QCString &app, const QCString &key)
00070 {
00071   removeNonKDECustomProperty(QCString("X-KDE-" + app + "-" + key));
00072 }
00073 
00074 QString CustomProperties::customProperty(const QCString &app, const QCString &key) const
00075 {
00076   return nonKDECustomProperty(QCString("X-KDE-" + app + "-" + key));
00077 }
00078 
00079 void CustomProperties::setNonKDECustomProperty(const QCString &name, const QString &value)
00080 {
00081   if (value.isNull() || !checkName(name))
00082     return;
00083   mProperties[name] = value;
00084 }
00085 
00086 void CustomProperties::removeNonKDECustomProperty(const QCString &name)
00087 {
00088   QMap<QCString, QString>::Iterator it = mProperties.find(name);
00089   if (it != mProperties.end())
00090     mProperties.remove(it);
00091 }
00092 
00093 QString CustomProperties::nonKDECustomProperty(const QCString &name) const
00094 {
00095   QMap<QCString, QString>::ConstIterator it = mProperties.find(name);
00096   if (it == mProperties.end())
00097     return QString::null;
00098   return it.data();
00099 }
00100 
00101 void CustomProperties::setCustomProperties(const QMap<QCString, QString> &properties)
00102 {
00103   for (QMap<QCString, QString>::ConstIterator it = properties.begin();  it != properties.end();  ++it) {
00104     // Validate the property name and convert any null string to empty string
00105     if (checkName(it.key())) {
00106       mProperties[it.key()] = it.data().isNull() ? QString("") : it.data();
00107     }
00108   }
00109 }
00110 
00111 QMap<QCString, QString> CustomProperties::customProperties() const
00112 {
00113   return mProperties;
00114 }
00115 
00116 bool CustomProperties::checkName(const QCString &name)
00117 {
00118   // Check that the property name starts with 'X-' and contains
00119   // only the permitted characters
00120   const char* n = name;
00121   int len = name.length();
00122   if (len < 2 ||  n[0] != 'X' || n[1] != '-')
00123     return false;
00124   for (int i = 2;  i < len;  ++i) {
00125     char ch = n[i];
00126     if (ch >= 'A' && ch <= 'Z'
00127     ||  ch >= 'a' && ch <= 'z'
00128     ||  ch >= '0' && ch <= '9'
00129     ||  ch == '-')
00130       continue;
00131     return false;   // invalid character found
00132   }
00133   return true;
00134 }
KDE Home | KDE Accessibility Home | Description of Access Keys