Warning, file /office/calligra/libs/odf/KoOasisSettings.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Laurent Montel <montel@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoOasisSettings.h"
0021 #include "KoXmlNS.h"
0022 #include <OdfDebug.h>
0023 
0024 class Q_DECL_HIDDEN KoOasisSettings::Private
0025 {
0026 };
0027 
0028 KoOasisSettings::KoOasisSettings(const KoXmlDocument& doc)
0029         : m_settingsElement(KoXml::namedItemNS(doc.documentElement(), KoXmlNS::office, "settings")),
0030         m_configNsUri(KoXmlNS::config)
0031         , d(0)
0032 {
0033     const KoXmlElement contents = doc.documentElement();
0034     if (m_settingsElement.isNull())
0035         debugOdf << " document doesn't have tag 'office:settings'";
0036 }
0037 
0038 KoOasisSettings::KoOasisSettings(const KoXmlDocument& doc, const char* officeNSURI, const char* configNSURI)
0039         : m_settingsElement(KoXml::namedItemNS(doc.documentElement(), officeNSURI, "settings")),
0040         m_configNsUri(configNSURI)
0041         , d(0)
0042 {
0043     const KoXmlElement contents = doc.documentElement();
0044     if (m_settingsElement.isNull())
0045         debugOdf << " document doesn't have tag 'office:settings'";
0046 }
0047 
0048 KoOasisSettings::~KoOasisSettings()
0049 {
0050     delete d;
0051 }
0052 
0053 KoOasisSettings::Items KoOasisSettings::itemSet(const QString& itemSetName) const
0054 {
0055     KoXmlElement e;
0056     forEachElement(e, m_settingsElement) {
0057         if (e.localName() == "config-item-set" &&
0058                 e.namespaceURI() == m_configNsUri &&
0059                 e.attributeNS(m_configNsUri, "name", QString()) == itemSetName) {
0060             return Items(e, this);
0061         }
0062     }
0063 
0064     return Items(KoXmlElement(), this);
0065 }
0066 
0067 KoOasisSettings::IndexedMap KoOasisSettings::Items::indexedMap(const QString& itemMapName) const
0068 {
0069     KoXmlElement configItem;
0070     forEachElement(configItem, m_element) {
0071         if (configItem.localName() == "config-item-map-indexed" &&
0072                 configItem.namespaceURI() == m_settings->m_configNsUri &&
0073                 configItem.attributeNS(m_settings->m_configNsUri, "name", QString()) == itemMapName) {
0074             return IndexedMap(configItem, m_settings);
0075         }
0076     }
0077     return IndexedMap(KoXmlElement(), m_settings);
0078 }
0079 
0080 KoOasisSettings::NamedMap KoOasisSettings::Items::namedMap(const QString& itemMapName) const
0081 {
0082     KoXmlElement configItem;
0083     forEachElement(configItem, m_element) {
0084         if (configItem.localName() == "config-item-map-named" &&
0085                 configItem.namespaceURI() == m_settings->m_configNsUri &&
0086                 configItem.attributeNS(m_settings->m_configNsUri, "name", QString()) == itemMapName) {
0087             return NamedMap(configItem, m_settings);
0088         }
0089     }
0090     return NamedMap(KoXmlElement(), m_settings);
0091 }
0092 
0093 KoOasisSettings::Items KoOasisSettings::IndexedMap::entry(int entryIndex) const
0094 {
0095     int i = 0;
0096     KoXmlElement entry;
0097     forEachElement(entry, m_element) {
0098         if (entry.localName() == "config-item-map-entry" &&
0099                 entry.namespaceURI() == m_settings->m_configNsUri) {
0100             if (i == entryIndex)
0101                 return Items(entry, m_settings);
0102             else
0103                 ++i;
0104         }
0105     }
0106     return Items(KoXmlElement(), m_settings);
0107 }
0108 
0109 KoOasisSettings::Items KoOasisSettings::NamedMap::entry(const QString& entryName) const
0110 {
0111     KoXmlElement entry;
0112     forEachElement(entry, m_element) {
0113         if (entry.localName() == "config-item-map-entry" &&
0114                 entry.namespaceURI() == m_settings->m_configNsUri &&
0115                 entry.attributeNS(m_settings->m_configNsUri, "name", QString()) == entryName) {
0116             return Items(entry, m_settings);
0117         }
0118     }
0119     return Items(KoXmlElement(), m_settings);
0120 }
0121 
0122 // helper method
0123 QString KoOasisSettings::Items::findConfigItem(const KoXmlElement& element,
0124         const QString& item, bool* ok) const
0125 {
0126     KoXmlElement it;
0127     forEachElement(it, element) {
0128         if (it.localName() == "config-item" &&
0129                 it.namespaceURI() == m_settings->m_configNsUri &&
0130                 it.attributeNS(m_settings->m_configNsUri, "name", QString()) == item) {
0131             *ok = true;
0132             return it.text();
0133         }
0134     }
0135     *ok = false;
0136     return QString();
0137 }
0138 
0139 
0140 QString KoOasisSettings::Items::findConfigItem(const QString& item, bool* ok) const
0141 {
0142     return findConfigItem(m_element, item, ok);
0143 }
0144 
0145 #if 0 // does anyone need this one? passing a default value does the job, too
0146 bool KoOasisSettings::Items::hasConfigItem(const QString& configName) const
0147 {
0148     bool ok;
0149     (void)findConfigItem(configName, &ok);
0150     return ok;
0151 }
0152 #endif
0153 
0154 QString KoOasisSettings::Items::parseConfigItemString(const QString& configName, const QString& defValue) const
0155 {
0156     bool ok;
0157     const QString str = findConfigItem(configName, &ok);
0158     return ok ? str : defValue;
0159 }
0160 
0161 int KoOasisSettings::Items::parseConfigItemInt(const QString& configName, int defValue) const
0162 {
0163     bool ok;
0164     const QString str = findConfigItem(configName, &ok);
0165     int value;
0166     if (ok) {
0167         value = str.toInt(&ok);
0168         if (ok)
0169             return value;
0170     }
0171     return defValue;
0172 }
0173 
0174 qreal KoOasisSettings::Items::parseConfigItemDouble(const QString& configName, qreal defValue) const
0175 {
0176     bool ok;
0177     const QString str = findConfigItem(configName, &ok);
0178     qreal value;
0179     if (ok) {
0180         value = str.toDouble(&ok);
0181         if (ok)
0182             return value;
0183     }
0184     return defValue;
0185 }
0186 
0187 bool KoOasisSettings::Items::parseConfigItemBool(const QString& configName, bool defValue) const
0188 {
0189     bool ok;
0190     const QString str = findConfigItem(configName, &ok);
0191     if (! ok)
0192         return defValue;
0193     if (str == "true")
0194         return true;
0195     else if (str == "false")
0196         return false;
0197     return defValue;
0198 }
0199 
0200 short KoOasisSettings::Items::parseConfigItemShort(const QString& configName, short defValue) const
0201 {
0202     bool ok;
0203     const QString str = findConfigItem(configName, &ok);
0204     short value;
0205     if (ok) {
0206         value = str.toShort(&ok);
0207         if (ok)
0208             return value;
0209     }
0210     return defValue;
0211 }
0212 
0213 long KoOasisSettings::Items::parseConfigItemLong(const QString& configName, long defValue) const
0214 {
0215     bool ok;
0216     const QString str = findConfigItem(configName, &ok);
0217     long value;
0218     if (ok) {
0219         value = str.toLong(&ok);
0220         if (ok)
0221             return value;
0222     }
0223     return defValue;
0224 }