lib Library API Documentation

koOasisSettings.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Laurent Montel <montel@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "koOasisSettings.h"
00021 #include "koxmlns.h"
00022 #include "kodom.h"
00023 #include <kdebug.h>
00024 
00025 KoOasisSettings::KoOasisSettings( const QDomDocument& doc )
00026     : m_settingsElement( KoDom::namedItemNS( doc.documentElement(), KoXmlNS::office, "settings" ) ),
00027       m_configNSURI( KoXmlNS::config )
00028 {
00029     const QDomElement contents = doc.documentElement();
00030     if ( m_settingsElement.isNull() )
00031         kdDebug() << " document doesn't have tag 'office:settings'\n";
00032 }
00033 
00034 KoOasisSettings::KoOasisSettings( const QDomDocument& doc, const char* officeNSURI, const char* configNSURI )
00035     : m_settingsElement( KoDom::namedItemNS( doc.documentElement(), officeNSURI, "settings" ) ),
00036       m_configNSURI( configNSURI )
00037 {
00038     const QDomElement contents = doc.documentElement();
00039     if ( m_settingsElement.isNull() )
00040         kdDebug() << " document doesn't have tag 'office:settings'\n";
00041 }
00042 
00043 KoOasisSettings::Items KoOasisSettings::itemSet( const QString& itemSetName ) const
00044 {
00045     QDomElement e;
00046     forEachElement( e, m_settingsElement )
00047     {
00048         if ( e.localName() == "config-item-set" &&
00049              e.namespaceURI() == m_configNSURI &&
00050              e.attributeNS( m_configNSURI, "name", QString::null ) == itemSetName )
00051         {
00052             return Items( e, this );
00053         }
00054     }
00055 
00056     return Items( QDomElement(), this );
00057 }
00058 
00059 KoOasisSettings::IndexedMap KoOasisSettings::Items::indexedMap( const QString& itemMapName ) const
00060 {
00061     QDomElement configItem;
00062     forEachElement( configItem, m_element )
00063     {
00064         if ( configItem.localName() == "config-item-map-indexed" &&
00065              configItem.namespaceURI() == m_settings->m_configNSURI &&
00066              configItem.attributeNS( m_settings->m_configNSURI, "name", QString::null ) == itemMapName )
00067         {
00068             return IndexedMap( configItem, m_settings );
00069         }
00070     }
00071     return IndexedMap( QDomElement(), m_settings );
00072 }
00073 
00074 KoOasisSettings::NamedMap KoOasisSettings::Items::namedMap( const QString& itemMapName ) const
00075 {
00076     QDomElement configItem;
00077     forEachElement( configItem, m_element )
00078     {
00079         if ( configItem.localName() == "config-item-map-named" &&
00080              configItem.namespaceURI() == m_settings->m_configNSURI &&
00081              configItem.attributeNS( m_settings->m_configNSURI, "name", QString::null ) == itemMapName )
00082         {
00083             return NamedMap( configItem, m_settings );
00084         }
00085     }
00086     return NamedMap( QDomElement(), m_settings );
00087 }
00088 
00089 KoOasisSettings::Items KoOasisSettings::IndexedMap::entry( int entryIndex ) const
00090 {
00091     int i = 0;
00092     QDomElement entry;
00093     forEachElement( entry, m_element )
00094     {
00095         if ( entry.localName() == "config-item-map-entry" &&
00096              entry.namespaceURI() == m_settings->m_configNSURI )
00097         {
00098             if ( i == entryIndex )
00099                 return Items( entry, m_settings );
00100             else
00101                 ++i;
00102         }
00103     }
00104     return Items( QDomElement(), m_settings );
00105 }
00106 
00107 KoOasisSettings::Items KoOasisSettings::NamedMap::entry( const QString& entryName ) const
00108 {
00109     QDomElement entry;
00110     forEachElement( entry, m_element )
00111     {
00112         if ( entry.localName() == "config-item-map-entry" &&
00113              entry.namespaceURI() == m_settings->m_configNSURI &&
00114              entry.attributeNS( m_settings->m_configNSURI, "name", QString::null ) == entryName )
00115         {
00116             return Items( entry, m_settings );
00117         }
00118     }
00119     return Items( QDomElement(), m_settings );
00120 }
00121 
00122 // helper method
00123 QString KoOasisSettings::Items::findConfigItem( const QDomElement& element,
00124                                                 const QString& item, bool* ok ) const
00125 {
00126     QDomElement it;
00127     forEachElement( it, element )
00128     {
00129         if ( it.localName() == "config-item" &&
00130              it.namespaceURI() == m_settings->m_configNSURI &&
00131              it.attributeNS( m_settings->m_configNSURI, "name", QString::null ) == item )
00132         {
00133             *ok = true;
00134             return it.text();
00135         }
00136     }
00137     *ok = false;
00138     return QString::null;
00139 }
00140 
00141 
00142 QString KoOasisSettings::Items::findConfigItem( const QString& item, bool* ok ) const
00143 {
00144     return findConfigItem( m_element, item, ok );
00145 }
00146 
00147 #if 0 // does anyone need this one? passing a default value does the job, too
00148 bool KoOasisSettings::Items::hasConfigItem( const QString& configName ) const
00149 {
00150     bool ok;
00151     (void)findConfigItem( configName, &ok );
00152     return ok;
00153 }
00154 #endif
00155 
00156 QString KoOasisSettings::Items::parseConfigItemString( const QString& configName, const QString& defValue ) const
00157 {
00158     bool ok;
00159     const QString str = findConfigItem( configName, &ok );
00160     return ok ? str : defValue;
00161 }
00162 
00163 int KoOasisSettings::Items::parseConfigItemInt( const QString& configName, int defValue ) const
00164 {
00165     bool ok;
00166     const QString str = findConfigItem( configName, &ok );
00167     int value;
00168     if ( ok )
00169         value = str.toInt( &ok );
00170     if ( ok )
00171         return value;
00172     return defValue;
00173 }
00174 
00175 double KoOasisSettings::Items::parseConfigItemDouble( const QString& configName, double defValue ) const
00176 {
00177     bool ok;
00178     const QString str = findConfigItem( configName, &ok );
00179     double value;
00180     if ( ok )
00181         value = str.toDouble( &ok );
00182     if ( ok )
00183         return value;
00184     return defValue;
00185 }
00186 
00187 bool KoOasisSettings::Items::parseConfigItemBool( const QString& configName, bool defValue ) const
00188 {
00189     bool ok;
00190     const QString str = findConfigItem( configName, &ok );
00191     if ( str == "true" )
00192         return true;
00193     else if ( str == "false" )
00194         return false;
00195     return defValue;
00196 }
00197 
00198 short KoOasisSettings::Items::parseConfigItemShort( const QString& configName, short defValue ) const
00199 {
00200     bool ok;
00201     const QString str = findConfigItem( configName, &ok );
00202     short value;
00203     if ( ok )
00204         value = str.toShort( &ok );
00205     if ( ok )
00206         return value;
00207     return defValue;
00208 }
00209 
00210 long KoOasisSettings::Items::parseConfigItemLong( const QString& configName, long defValue ) const
00211 {
00212     bool ok;
00213     const QString str = findConfigItem( configName, &ok );
00214     long value;
00215     if ( ok )
00216         value = str.toLong( &ok );
00217     if ( ok )
00218         return value;
00219     return defValue;
00220 }
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:02 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003