Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

blokkalconfigbase.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 - 2008 by Martin Mueller                           *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "blokkalconfigbase.h"
00021 
00022 #include <QDomElement>
00023 
00024 class Blokkal::ConfigBase::Private
00025 {
00026 public:
00027         Private( QDomElement configNode ) :
00028         node( configNode ) {}
00029 
00030         QDomElement node;
00031 };
00032 
00033 
00034 Blokkal::ConfigBase::ConfigBase( void ) :
00035 d( new Private( QDomElement() ) )
00036 {
00037 }
00038 
00039 Blokkal::ConfigBase::ConfigBase( QDomElement configNode ) :
00040 d( new Private( configNode ) )
00041 {
00042 }
00043 
00044 Blokkal::ConfigBase::~ConfigBase( void )
00045 {
00046         delete d;
00047 }
00048 
00049 void Blokkal::ConfigBase::setNode( QDomElement configNode )
00050 {
00051         d->node = configNode;
00052 }
00053 
00054 QDomElement Blokkal::ConfigBase::node( void ) const
00055 {
00056         return d->node;
00057 }
00058 
00059 QDomElement Blokkal::ConfigBase::findPropertyNode( const QString & name ) const
00060 {
00061         for( QDomNode currentNode = node().firstChild();
00062              !currentNode.isNull();
00063              currentNode = currentNode.nextSibling()
00064            )
00065         {
00066                 if( currentNode.isElement()
00067                     && currentNode.nodeName() == "property"
00068                     && currentNode.toElement().attribute( "name" ) == name )
00069                 {
00070                         return currentNode.toElement();
00071                 }
00072         }
00073                 
00074         return QDomNode().toElement();
00075 }
00076 
00077 QDomElement Blokkal::ConfigBase::createPropertyNode( const QString & name )
00078 {
00079         QDomElement propertyNode = node().ownerDocument().createElement( "property" );
00080         node().appendChild( propertyNode );
00081         propertyNode.setAttribute( "name", name );
00082         return propertyNode;
00083 }
00084 
00085 void Blokkal::ConfigBase::remove( void )
00086 {
00087         node().parentNode().removeChild( node() );
00088 }
00089 
00090 void Blokkal::ConfigBase::writeEntry( const QString & name, const QString & value )
00091 {
00092         QDomElement propertyNode = findPropertyNode( name );
00093         if( propertyNode.isNull() ) {
00094                 propertyNode = createPropertyNode( name );
00095         }
00096 
00097         propertyNode.setAttribute( "value", value );
00098 }
00099 
00100 void Blokkal::ConfigBase::writeEntry( const QString & name, bool value )
00101 {
00102         writeEntry( name, value ? QString::fromLatin1( "true" ) : QString::fromLatin1( "false" ) );
00103 }
00104 
00105 QString Blokkal::ConfigBase::readEntry( const QString & name, const QString & defaultValue ) const
00106 {
00107         QDomElement propertyNode = findPropertyNode( name );
00108 
00109         if( propertyNode.isNull() ) {
00110                 return defaultValue;
00111         }
00112 
00113         return propertyNode.attribute( "value" );
00114 }
00115 
00116 bool Blokkal::ConfigBase::readBoolEntry( const QString & name, bool defaultValue ) const
00117 {
00118         return readEntry( name, defaultValue ? QString::fromLatin1( "true" ) : QString::fromLatin1( "false" ) ) == QString::fromLatin1( "true" );
00119 }
00120 
00121 void Blokkal::ConfigBase::writeEntry( const QString & name, int value )
00122 {
00123         writeEntry( name, QString::number( value ) );
00124 }
00125 
00126 int Blokkal::ConfigBase::readIntEntry( const QString & name, int defaultValue ) const
00127 {
00128         bool ok;
00129         int value = readEntry( name, QString::number( defaultValue ) ).toInt( &ok );
00130         if( ok ) {
00131                 return value;
00132         }
00133 
00134         return defaultValue;
00135 }
00136 
00137 void Blokkal::ConfigBase::writeEntry( const QString & name, unsigned int value )
00138 {
00139         writeEntry( name, QString::number( value ) );
00140 }
00141 
00142 unsigned int Blokkal::ConfigBase::readUIntEntry( const QString & name, unsigned int defaultValue ) const
00143 {
00144         bool ok;
00145         unsigned int value = readEntry( name, QString::number( defaultValue ) ).toUInt( &ok );
00146         if( ok ) {
00147                 return value;
00148         }
00149 
00150         return defaultValue;
00151 }
00152 
00153 unsigned long Blokkal::ConfigBase::readULongEntry( const QString & name, unsigned long defaultValue ) const
00154 {
00155         bool ok;
00156         unsigned long value = readEntry( name, QString::number( defaultValue ) ).toULong( &ok );
00157         if( ok ) {
00158                 return value;
00159         }
00160 
00161         return defaultValue;
00162 }
00163 
00164 void Blokkal::ConfigBase::writeEntry( const QString & name, unsigned long value )
00165 {
00166         writeEntry( name, QString::number( value ) );
00167 }
00168 
00169 void Blokkal::ConfigBase::writeComplex( const QString & name, const QString & value )
00170 {
00171         QDomElement complexNode = findComplexNode( name );
00172         if( complexNode.isNull() ) {
00173                 complexNode = createComplexNode( name );
00174         }
00175 
00176         if( !complexNode.hasChildNodes() ) {
00177                 complexNode.appendChild( node().ownerDocument().createTextNode( value ) );
00178         }
00179         else {
00180                 complexNode.firstChild().toText().setData( value );
00181         }
00182 }
00183 
00184 QString Blokkal::ConfigBase::readComplex( const QString & name, const QString & defaultValue )
00185 {
00186         QDomElement complexNode = findComplexNode( name );
00187 
00188         if( complexNode.isNull() ) {
00189                 return defaultValue;
00190         }
00191 
00192         return complexNode.firstChild().toText().data();
00193 }
00194 
00195 QDomElement Blokkal::ConfigBase::findComplexNode( const QString & name ) const
00196 {
00197         for( QDomNode currentNode = node().firstChild();
00198              !currentNode.isNull();
00199              currentNode = currentNode.nextSibling()
00200            )
00201         {
00202                 if( currentNode.isElement()
00203                     && currentNode.nodeName() == "complex"
00204                     && currentNode.toElement().attribute( "name" ) == name )
00205                 {
00206                         return currentNode.toElement();
00207                 }
00208         }
00209                 
00210         return QDomNode().toElement();  
00211 }
00212 
00213 QDomElement Blokkal::ConfigBase::createComplexNode( const QString & name )
00214 {
00215         QDomElement complexNode = node().ownerDocument().createElement( "complex" );
00216         node().appendChild( complexNode );
00217         complexNode.setAttribute( "name", name );
00218         return complexNode;     
00219 }