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

Aller à la documentation de ce fichier.
00001 /*
00002  *  Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.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 #include <kdebug.h>
00020 #include <qdom.h>
00021 #include <QString>
00022 
00023 #include "KoProperties.h"
00024 
00025 KoProperties::KoProperties(const KoProperties & rhs)
00026     : m_properties( rhs.m_properties )
00027 {
00028 }
00029 
00030 void KoProperties::load(const QString & s)
00031 {
00032     m_properties.clear();
00033 
00034     QDomDocument doc;
00035     doc.setContent( s );
00036     QDomElement e = doc.documentElement();
00037     QDomNode n = e.firstChild();
00038 
00039     while (!n.isNull()) {
00040         // We don't nest elements in filter configuration. For now...
00041         QDomElement e = n.toElement();
00042         if (!e.isNull()) {
00043             if (e.tagName() == "property") {
00044                 const QString name = e.attribute("name");
00045                 const QString type = e.attribute("type");
00046                 const QString value = e.text();
00047                 // XXX Convert the variant pro-actively to the right type?
00048                 m_properties[name] = QVariant(value);
00049             }
00050         }
00051         n = n.nextSibling();
00052     }
00053 }
00054 
00055 QString KoProperties::store()
00056 {
00057     QDomDocument doc = QDomDocument("filterconfig");
00058     QDomElement root = doc.createElement( "filterconfig" );
00059 
00060     doc.appendChild( root );
00061 
00062     QMap<QString, QVariant>::Iterator it;
00063     for ( it = m_properties.begin(); it != m_properties.end(); ++it ) {
00064         QDomElement e = doc.createElement( "property" );
00065         e.setAttribute( "name", QString(it.key().toLatin1()) );
00066         QVariant v = it.value();
00067         e.setAttribute( "type", v.typeName() );
00068         QString s = v.toString();
00069         QDomText text = doc.createCDATASection(v.toString() ); // XXX: Unittest this!
00070         e.appendChild(text);
00071         root.appendChild(e);
00072     }
00073 
00074     return doc.toString();
00075 }
00076 
00077 
00078 void KoProperties::setProperty(const QString & name, const QVariant & value)
00079 {
00080     // If there's an existing value for this name already, replace it.
00081     m_properties.insert( name, value );
00082 }
00083 
00084 bool KoProperties::getProperty(const QString & name, QVariant & value) const
00085 {
00086    QMap<QString, QVariant>::const_iterator it = m_properties.find( name ); 
00087    if ( it == m_properties.end() ) {
00088        return false;
00089    }
00090    else {
00091        value = *it;
00092        return true;
00093    }
00094 }
00095 
00096 QVariant KoProperties::getProperty(const QString & name) const
00097 {
00098     return m_properties.value( name, QVariant() );
00099 }
00100 
00101 
00102 int KoProperties::getInt(const QString & name, int def) const
00103 {
00104     const QVariant v = getProperty(name);
00105     if (v.isValid())
00106         return v.toInt();
00107     else
00108         return def;
00109 
00110 }
00111 
00112 double KoProperties::getDouble(const QString & name, double def) const
00113 {
00114     const QVariant v = getProperty(name);
00115     if (v.isValid())
00116         return v.toDouble();
00117     else
00118         return def;
00119 }
00120 
00121 bool KoProperties::getBool(const QString & name, bool def) const
00122 {
00123     const QVariant v = getProperty(name);
00124     if (v.isValid())
00125         return v.toBool();
00126     else
00127         return def;
00128 }
00129 
00130 QString KoProperties::getString(const QString & name, const QString & def) const
00131 {
00132     const QVariant v = getProperty(name);
00133     if (v.isValid())
00134         return v.toString();
00135     else
00136         return def;
00137 }
00138 
00139 void KoProperties::dump()
00140 {
00141     QMap<QString, QVariant>::Iterator it;
00142     for ( it = m_properties.begin(); it != m_properties.end(); ++it ) {
00143         // ### TODO finish
00144     }
00145 }

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