lib

KoOasisSettings.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Laurent Montel <montel@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KoOasisSettings.h"
21#include "KoXmlNS.h"
22#include "KoDom.h"
23#include <kdebug.h>
24
25KoOasisSettings::KoOasisSettings( const TQDomDocument& doc )
26 : m_settingsElement( KoDom::namedItemNS( doc.documentElement(), KoXmlNS::office, "settings" ) ),
27 m_configNSURI( KoXmlNS::config )
28{
29 const TQDomElement contents = doc.documentElement();
30 if ( m_settingsElement.isNull() )
31 kdDebug() << " document doesn't have tag 'office:settings'\n";
32}
33
34KoOasisSettings::KoOasisSettings( const TQDomDocument& doc, const char* officeNSURI, const char* configNSURI )
35 : m_settingsElement( KoDom::namedItemNS( doc.documentElement(), officeNSURI, "settings" ) ),
36 m_configNSURI( configNSURI )
37{
38 const TQDomElement contents = doc.documentElement();
39 if ( m_settingsElement.isNull() )
40 kdDebug() << " document doesn't have tag 'office:settings'\n";
41}
42
43KoOasisSettings::Items KoOasisSettings::itemSet( const TQString& itemSetName ) const
44{
45 TQDomElement e;
46 forEachElement( e, m_settingsElement )
47 {
48 if ( e.localName() == "config-item-set" &&
49 e.namespaceURI() == m_configNSURI &&
50 e.attributeNS( m_configNSURI, "name", TQString() ) == itemSetName )
51 {
52 return Items( e, this );
53 }
54 }
55
56 return Items( TQDomElement(), this );
57}
58
59KoOasisSettings::IndexedMap KoOasisSettings::Items::indexedMap( const TQString& itemMapName ) const
60{
61 TQDomElement configItem;
62 forEachElement( configItem, m_element )
63 {
64 if ( configItem.localName() == "config-item-map-indexed" &&
65 configItem.namespaceURI() == m_settings->m_configNSURI &&
66 configItem.attributeNS( m_settings->m_configNSURI, "name", TQString() ) == itemMapName )
67 {
68 return IndexedMap( configItem, m_settings );
69 }
70 }
71 return IndexedMap( TQDomElement(), m_settings );
72}
73
74KoOasisSettings::NamedMap KoOasisSettings::Items::namedMap( const TQString& itemMapName ) const
75{
76 TQDomElement configItem;
77 forEachElement( configItem, m_element )
78 {
79 if ( configItem.localName() == "config-item-map-named" &&
80 configItem.namespaceURI() == m_settings->m_configNSURI &&
81 configItem.attributeNS( m_settings->m_configNSURI, "name", TQString() ) == itemMapName )
82 {
83 return NamedMap( configItem, m_settings );
84 }
85 }
86 return NamedMap( TQDomElement(), m_settings );
87}
88
89KoOasisSettings::Items KoOasisSettings::IndexedMap::entry( int entryIndex ) const
90{
91 int i = 0;
92 TQDomElement entry;
93 forEachElement( entry, m_element )
94 {
95 if ( entry.localName() == "config-item-map-entry" &&
96 entry.namespaceURI() == m_settings->m_configNSURI )
97 {
98 if ( i == entryIndex )
99 return Items( entry, m_settings );
100 else
101 ++i;
102 }
103 }
104 return Items( TQDomElement(), m_settings );
105}
106
107KoOasisSettings::Items KoOasisSettings::NamedMap::entry( const TQString& entryName ) const
108{
109 TQDomElement entry;
110 forEachElement( entry, m_element )
111 {
112 if ( entry.localName() == "config-item-map-entry" &&
113 entry.namespaceURI() == m_settings->m_configNSURI &&
114 entry.attributeNS( m_settings->m_configNSURI, "name", TQString() ) == entryName )
115 {
116 return Items( entry, m_settings );
117 }
118 }
119 return Items( TQDomElement(), m_settings );
120}
121
122// helper method
123TQString KoOasisSettings::Items::findConfigItem( const TQDomElement& element,
124 const TQString& item, bool* ok ) const
125{
126 TQDomElement it;
127 forEachElement( it, element )
128 {
129 if ( it.localName() == "config-item" &&
130 it.namespaceURI() == m_settings->m_configNSURI &&
131 it.attributeNS( m_settings->m_configNSURI, "name", TQString() ) == item )
132 {
133 *ok = true;
134 return it.text();
135 }
136 }
137 *ok = false;
138 return TQString();
139}
140
141
142TQString KoOasisSettings::Items::findConfigItem( const TQString& item, bool* ok ) const
143{
144 return findConfigItem( m_element, item, ok );
145}
146
147#if 0 // does anyone need this one? passing a default value does the job, too
148bool KoOasisSettings::Items::hasConfigItem( const TQString& configName ) const
149{
150 bool ok;
151 (void)findConfigItem( configName, &ok );
152 return ok;
153}
154#endif
155
156TQString KoOasisSettings::Items::parseConfigItemString( const TQString& configName, const TQString& defValue ) const
157{
158 bool ok;
159 const TQString str = findConfigItem( configName, &ok );
160 return ok ? str : defValue;
161}
162
163int KoOasisSettings::Items::parseConfigItemInt( const TQString& configName, int defValue ) const
164{
165 bool ok;
166 const TQString str = findConfigItem( configName, &ok );
167 int value;
168 if ( ok ) {
169 value = str.toInt( &ok );
170 if ( ok )
171 return value;
172 }
173 return defValue;
174}
175
176double KoOasisSettings::Items::parseConfigItemDouble( const TQString& configName, double defValue ) const
177{
178 bool ok;
179 const TQString str = findConfigItem( configName, &ok );
180 double value;
181 if ( ok ) {
182 value = str.toDouble( &ok );
183 if ( ok )
184 return value;
185 }
186 return defValue;
187}
188
189bool KoOasisSettings::Items::parseConfigItemBool( const TQString& configName, bool defValue ) const
190{
191 bool ok;
192 const TQString str = findConfigItem( configName, &ok );
193 if ( str == "true" )
194 return true;
195 else if ( str == "false" )
196 return false;
197 return defValue;
198}
199
200short KoOasisSettings::Items::parseConfigItemShort( const TQString& configName, short defValue ) const
201{
202 bool ok;
203 const TQString str = findConfigItem( configName, &ok );
204 short value;
205 if ( ok ) {
206 value = str.toShort( &ok );
207 if ( ok )
208 return value;
209 }
210 return defValue;
211}
212
213long KoOasisSettings::Items::parseConfigItemLong( const TQString& configName, long defValue ) const
214{
215 bool ok;
216 const TQString str = findConfigItem( configName, &ok );
217 long value;
218 if ( ok ) {
219 value = str.toLong( &ok );
220 if ( ok )
221 return value;
222 }
223 return defValue;
224}
Represents a collection of items (config-item or maps).
IndexedMap indexedMap(const TQString &itemMapName) const
Look for the config-item-map-indexed named itemMapName and return it.
NamedMap namedMap(const TQString &itemMapName) const
Look for the config-item-map-named named mapItemName and return it.
KoOasisSettings(const TQDomDocument &doc)
Normal KoOasisSettings constructor, for an OASIS settings.xml.
Items itemSet(const TQString &itemSetName) const
Returns the toplevel item-set named itemSetName.
Repository of XML namespaces used for OASIS documents.
Definition KoXmlNS.h:29
This namespace contains a few convenience functions to simplify code using TQDom (when loading OASIS ...
Definition KoDom.h:51