libkcal

customproperties.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2002,2006 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   customPropertyUpdated();
00068 }
00069 
00070 void CustomProperties::removeCustomProperty(const QCString &app, const QCString &key)
00071 {
00072   removeNonKDECustomProperty(QCString("X-KDE-" + app + "-" + key));
00073 }
00074 
00075 QString CustomProperties::customProperty(const QCString &app, const QCString &key) const
00076 {
00077   return nonKDECustomProperty(QCString("X-KDE-" + app + "-" + key));
00078 }
00079 
00080 void CustomProperties::setNonKDECustomProperty(const QCString &name, const QString &value)
00081 {
00082   if (value.isNull() || !checkName(name))
00083     return;
00084   mProperties[name] = value;
00085   customPropertyUpdated();
00086 }
00087 
00088 void CustomProperties::removeNonKDECustomProperty(const QCString &name)
00089 {
00090   QMap<QCString, QString>::Iterator it = mProperties.find(name);
00091   if (it != mProperties.end()) {
00092     mProperties.remove(it);
00093     customPropertyUpdated();
00094   }
00095 }
00096 
00097 QString CustomProperties::nonKDECustomProperty(const QCString &name) const
00098 {
00099   QMap<QCString, QString>::ConstIterator it = mProperties.find(name);
00100   if (it == mProperties.end())
00101     return QString::null;
00102   return it.data();
00103 }
00104 
00105 void CustomProperties::setCustomProperties(const QMap<QCString, QString> &properties)
00106 {
00107   bool changed = false;
00108   for (QMap<QCString, QString>::ConstIterator it = properties.begin();  it != properties.end();  ++it) {
00109     // Validate the property name and convert any null string to empty string
00110     if (checkName(it.key())) {
00111       mProperties[it.key()] = it.data().isNull() ? QString("") : it.data();
00112       changed = true;
00113     }
00114   }
00115   if (changed)
00116     customPropertyUpdated();
00117 }
00118 
00119 QMap<QCString, QString> CustomProperties::customProperties() const
00120 {
00121   return mProperties;
00122 }
00123 
00124 bool CustomProperties::checkName(const QCString &name)
00125 {
00126   // Check that the property name starts with 'X-' and contains
00127   // only the permitted characters
00128   const char* n = name;
00129   int len = name.length();
00130   if (len < 2 ||  n[0] != 'X' || n[1] != '-')
00131     return false;
00132   for (int i = 2;  i < len;  ++i) {
00133     char ch = n[i];
00134     if (ch >= 'A' && ch <= 'Z'
00135     ||  ch >= 'a' && ch <= 'z'
00136     ||  ch >= '0' && ch <= '9'
00137     ||  ch == '-')
00138       continue;
00139     return false;   // invalid character found
00140   }
00141   return true;
00142 }