Back to index

kdeartwork  4.3.2
kxsxml.cpp
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 // KDE xscreensaver configuration dialog
00004 //
00005 // Copyright (c)  Martin R. Jones <mjones@kde.org> 2002
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public
00009 // License as published by the Free Software Foundation;
00010 // version 2 of the License.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; see the file COPYING.  If not, write to
00019 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020 // Boston, MA 02110-1301, USA.
00021 
00022 #include "kxsxml.h"
00023 #include "kxscontrol.h"
00024 #include <qobject.h>
00025 #include <qfile.h>
00026 #include <kvbox.h>
00027 #include <khbox.h>
00028 #include <stdio.h>
00029 
00030 KXSXml::KXSXml( QWidget *p )
00031     : parent(p), handler(0)
00032 {
00033 }
00034 
00035 bool KXSXml::parse( const QString &filename )
00036 {
00037     QFile file( filename );
00038     handler = new KXSXmlHandler( parent );
00039     QXmlInputSource source( &file );
00040     QXmlSimpleReader reader;
00041     reader.setContentHandler( handler );
00042     if ( !reader.parse( &source, false ) )
00043        return false;
00044 
00045     return true;
00046 }
00047 
00048 QList<KXSConfigItem*> KXSXml::items() const
00049 {
00050     if ( handler )
00051        return handler->items();
00052     return QList<KXSConfigItem*>();
00053 }
00054 
00055 QString KXSXml::description() const
00056 {
00057     if ( handler )
00058        return handler->description();
00059     return QString();
00060 }
00061 
00062 //===========================================================================
00063 
00064 KXSXmlHandler::KXSXmlHandler( QWidget *p )
00065     : QXmlDefaultHandler(), parent(p), selItem(0), inDesc(false)
00066 {
00067     mParentStack.push( p );
00068 }
00069 
00070 bool KXSXmlHandler::startDocument()
00071 {
00072     return true;
00073 }
00074 
00075 bool KXSXmlHandler::startElement( const QString&, const QString&, 
00076                                     const QString& qName, 
00077                                     const QXmlAttributes &atts )
00078 {
00079     KXSConfigItem *i = 0;
00080     QString id = atts.value("id");
00081     if ( qName == "number" ) {
00082        QString sLow = atts.value( "low" );
00083        QString sHigh = atts.value( "high" );
00084        if ( sLow.contains( '.' ) || sHigh.contains( '.' ) ) {
00085            if ( parent )
00086               i = new KXSDoubleRangeControl( parent, id, atts );
00087            else
00088               i = new KXSDoubleRangeItem( id, atts );
00089        } else {
00090            if ( parent )
00091               i = new KXSRangeControl( parent, id, atts );
00092            else
00093               i = new KXSRangeItem( id, atts );
00094        }
00095     } else if ( qName == "boolean" ) {
00096        if ( parent )
00097            i = new KXSCheckBoxControl( parent, id, atts );
00098        else
00099            i = new KXSBoolItem( id, atts );
00100     } else if ( qName == "string" ) {
00101        if ( parent )
00102            i = new KXSLineEditControl( parent, id, atts );
00103        else
00104            i = new KXSStringItem( id, atts );
00105     } else if ( qName == "file" ) {
00106        if ( parent )
00107            i = new KXSFileControl( parent, id, atts );
00108        else
00109            i = new KXSStringItem( id, atts );
00110     } else if ( qName == "_description" ) {
00111        inDesc = true;
00112     } else if ( qName == "select" ) {
00113        if ( parent )
00114            selItem = new KXSDropListControl( parent, id, atts );
00115        else
00116            selItem = new KXSSelectItem( id, atts );
00117        i = selItem;
00118     } else if ( qName == "option" && selItem ) {
00119        selItem->addOption( atts );
00120     } else if ( qName == "hgroup" && parent ) {
00121        KHBox *hb = new KHBox( parent );
00122        mParentStack.push( hb );
00123        parent = hb;
00124     } else if ( qName == "vgroup" && parent ) {
00125        KVBox *vb = new KVBox( parent );
00126        mParentStack.push( vb );
00127        parent = vb;
00128     }
00129 
00130     if ( i )
00131        mConfigItemList.append( i );
00132 
00133     return true;
00134 }
00135 
00136 bool KXSXmlHandler::endElement( const QString&, const QString&, const QString &qName )
00137 {
00138     if ( qName == "select" ) {
00139        selItem = 0;
00140     } else if ( qName == "_description" ) {
00141        inDesc = false;
00142     } else if ( (qName == "hgroup" || qName == "vgroup") && parent ) {
00143        if ( mParentStack.count() > 1 ) {
00144            mParentStack.pop();
00145            parent = mParentStack.top();
00146        }
00147     }
00148     return true;
00149 }
00150 
00151 bool KXSXmlHandler::characters( const QString &ch )
00152 {
00153     if ( inDesc )
00154        desc += ch;
00155     return true;
00156 }
00157