00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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 }