F:/KPlato/koffice/libs/kofficecore/KoOasisSettings.cpp

Aller à la documentation de ce fichier.
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., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KoOasisSettings.h"
00021 #include "KoXmlNS.h"
00022 #include "KoDom.h"
00023 #include <kdebug.h>
00024 
00025 KoOasisSettings::KoOasisSettings( const KoXmlDocument& doc )
00026     : m_settingsElement( KoDom::namedItemNS( doc.documentElement(), KoXmlNS::office, "settings" ) ),
00027       m_configNSURI( KoXmlNS::config )
00028 {
00029     const KoXmlElement contents = doc.documentElement();
00030     if ( m_settingsElement.isNull() )
00031         kDebug() << " document doesn't have tag 'office:settings'\n";
00032 }
00033 
00034 KoOasisSettings::KoOasisSettings( const KoXmlDocument& doc, const char* officeNSURI, const char* configNSURI )
00035     : m_settingsElement( KoDom::namedItemNS( doc.documentElement(), officeNSURI, "settings" ) ),
00036       m_configNSURI( configNSURI )
00037 {
00038     const KoXmlElement contents = doc.documentElement();
00039     if ( m_settingsElement.isNull() )
00040         kDebug() << " document doesn't have tag 'office:settings'\n";
00041 }
00042 
00043 KoOasisSettings::Items KoOasisSettings::itemSet( const QString& itemSetName ) const
00044 {
00045     KoXmlElement 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( KoXmlElement(), this );
00057 }
00058 
00059 KoOasisSettings::IndexedMap KoOasisSettings::Items::indexedMap( const QString& itemMapName ) const
00060 {
00061     KoXmlElement 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( KoXmlElement(), m_settings );
00072 }
00073 
00074 KoOasisSettings::NamedMap KoOasisSettings::Items::namedMap( const QString& itemMapName ) const
00075 {
00076     KoXmlElement 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( KoXmlElement(), m_settings );
00087 }
00088 
00089 KoOasisSettings::Items KoOasisSettings::IndexedMap::entry( int entryIndex ) const
00090 {
00091     int i = 0;
00092     KoXmlElement 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( KoXmlElement(), m_settings );
00105 }
00106 
00107 KoOasisSettings::Items KoOasisSettings::NamedMap::entry( const QString& entryName ) const
00108 {
00109     KoXmlElement 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( KoXmlElement(), m_settings );
00120 }
00121 
00122 // helper method
00123 QString KoOasisSettings::Items::findConfigItem( const KoXmlElement& element,
00124                                                 const QString& item, bool* ok ) const
00125 {
00126     KoXmlElement 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();
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     }
00173     return defValue;
00174 }
00175 
00176 double KoOasisSettings::Items::parseConfigItemDouble( const QString& configName, double defValue ) const
00177 {
00178     bool ok;
00179     const QString str = findConfigItem( configName, &ok );
00180     double value;
00181     if ( ok ) {
00182         value = str.toDouble( &ok );
00183         if ( ok )
00184             return value;
00185     }
00186     return defValue;
00187 }
00188 
00189 bool KoOasisSettings::Items::parseConfigItemBool( const QString& configName, bool defValue ) const
00190 {
00191     bool ok;
00192     const QString str = findConfigItem( configName, &ok );
00193     if ( str == "true" )
00194         return true;
00195     else if ( str == "false" )
00196         return false;
00197     return defValue;
00198 }
00199 
00200 short KoOasisSettings::Items::parseConfigItemShort( const QString& configName, short defValue ) const
00201 {
00202     bool ok;
00203     const QString str = findConfigItem( configName, &ok );
00204     short value;
00205     if ( ok ) {
00206         value = str.toShort( &ok );
00207         if ( ok )
00208             return value;
00209     }
00210     return defValue;
00211 }
00212 
00213 long KoOasisSettings::Items::parseConfigItemLong( const QString& configName, long defValue ) const
00214 {
00215     bool ok;
00216     const QString str = findConfigItem( configName, &ok );
00217     long value;
00218     if ( ok ) {
00219         value = str.toLong( &ok );
00220         if ( ok )
00221             return value;
00222     }
00223     return defValue;
00224 }

Généré le Wed Nov 22 23:41:03 2006 pour KPlato par  doxygen 1.5.1-p1