# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# krootprop.cpp

    00001 /* This file is part of the KDE libraries
    00002     Copyright (C) 1997 Mark Donohoe (donohoe@kde.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., 59 Temple Place - Suite 330,
    00017     Boston, MA 02111-1307, USA.
    00018 */
    00019
    00020 #include <qwidget.h>
    00021 #ifndef Q_WS_QWS
    00022 #include "krootprop.h"
    00023 #include "kglobal.h"
    00024 #include "klocale.h"
    00025 #include "kcharsets.h"
    00026 #include "kapplication.h"
    00027 #include <qtextstream.h>
    00028
    00029 #include <X11/Xlib.h>
    00030 #include <X11/Xatom.h>
    00031
    00032 KRootProp::KRootProp(const QString& rProp )
    00033 {
    00034   atom = 0;
    00035   dirty = FALSE;
    00036   setProp( rProp );
    00037 }
    00038
    00039 KRootProp::~KRootProp()
    00040 {
    00041   sync();
    00042   propDict.clear();
    00043 }
    00044
    00045 void KRootProp::sync()
    00046 {
    00047   if ( !dirty )
    00048       return;
    00049
    00050   QString propString;
    00051   if ( !propDict.isEmpty() )
    00052   {
    00053     QMap<QString,QString>::Iterator it = propDict.begin();
    00054     QString keyvalue;
    00055
    00056     while ( it != propDict.end() )
    00057     {
    00058       keyvalue = QString( "%1=%2\n").arg(it.key()).arg(it.data());
    00059       propString += keyvalue;
    00060       ++it;
    00061     }
    00062   }
    00063
    00064   XChangeProperty( qt_xdisplay(), qt_xrootwin(), atom,
    00065                   XA_STRING, 8, PropModeReplace,
    00066                   (const unsigned char *)propString.utf8().data(),
    00067                   propString.length());
    00068   XFlush( qt_xdisplay() );
    00069 }
    00070
    00071 void KRootProp::setProp( const QString& rProp )
    00072 {
    00073   Atom type;
    00074   int format;
    00075   unsigned long nitems;
    00076   unsigned long bytes_after;
    00077   long offset;
    00078   char *buf;
    00079
    00080   // If a property has already been opened write
    00081   // the dictionary back to the root window
    00082
    00083   if( atom )
    00084     sync();
    00085
    00086   property_ = rProp;
    00087   if( rProp.isEmpty() )
    00088     return;
    00089
    00090   atom = XInternAtom( qt_xdisplay(), rProp.utf8(), False);
    00091
    00092   QString s;
    00093   offset = 0; bytes_after = 1;
    00094   while (bytes_after != 0)
    00095   {
    00096     XGetWindowProperty( qt_xdisplay(), qt_xrootwin(), atom, offset, 256,
    00097                         False, XA_STRING, &type, &format, &nitems, &bytes_after,
    00098                         (unsigned char **)&buf);
    00099     s += QString::fromUtf8(buf);
    00100     offset += nitems/4;
    00101     if (buf)
    00102       XFree(buf);
    00103   }
    00104
    00105   // Parse through the property string stripping out key value pairs
    00106   // and putting them in the dictionary
    00107
    00108   QString keypair;
    00109   int i=0;
    00110   QString key;
    00111   QString value;
    00112
    00113   while(s.length() >0 )
    00114   {
    00115     // parse the string for first key-value pair separator '\n'
    00116
    00117     i = s.find("\n");
    00118     if(i == -1)
    00119       i = s.length();
    00120
    00121     // extract the key-values pair and remove from string
    00122
    00123     keypair = s.left(i);
    00124     s.remove(0,i+1);
    00125
    00126     // split key and value and add to dictionary
    00127
    00128     keypair.simplifyWhiteSpace();
    00129
    00130     i = keypair.find( "=" );
    00131     if( i != -1 )
    00132     {
    00133       key = keypair.left( i );
    00134       value = keypair.mid( i+1 );
    00135       propDict.insert( key, value );
    00136     }
    00137   }
    00138 }
    00139
    00140
    00141 QString KRootProp::prop() const
    00142 {
    00143     return property_;
    00144 }
    00145
    00146 void KRootProp::destroy()
    00147 {
    00148     dirty = FALSE;
    00149     propDict.clear();
    00150     if( atom ) {
    00151         XDeleteProperty( qt_xdisplay(), qt_xrootwin(), atom );
    00152         atom = 0;
    00153     }
    00154 }
    00155
    00156 QString KRootProp::readEntry( const QString& rKey,
    00157                             const QString& pDefault ) const
    00158 {
    00159   if( propDict.contains( rKey ) )
    00160       return propDict[ rKey ];
    00161   else
    00162       return pDefault;
    00163 }
    00164
    00165 int KRootProp::readNumEntry( const QString& rKey, int nDefault ) const
    00166 {
    00167
    00168   QString aValue = readEntry( rKey );
    00169   if( !aValue.isNull() )
    00170   {
    00171     bool ok;
    00172
    00173     int rc = aValue.toInt( &ok );
    00174     if (ok)
    00175       return rc;
    00176   }
    00177   return nDefault;
    00178 }
    00179
    00180
    00181 QFont KRootProp::readFontEntry( const QString& rKey,
    00182                                 const QFont* pDefault ) const
    00183 {
    00184   QFont aRetFont;
    00185   QFont aDefFont;
    00186
    00187   if (pDefault)
    00188     aDefFont = *pDefault;
    00189
    00190   QString aValue = readEntry( rKey );
    00191   if( aValue.isNull() )
    00192     return aDefFont; // Return default font
    00193
    00194   if ( !aRetFont.fromString( aValue ) && pDefault )
    00195     aRetFont = aDefFont;
    00196
    00197   return aRetFont;
    00198 }
    00199
    00200
    00201 QColor KRootProp::readColorEntry( const QString& rKey,
    00202                                                                 const QColor* pDefault ) const
    00203 {
    00204   QColor aRetColor;
    00205   int nRed = 0, nGreen = 0, nBlue = 0;
    00206
    00207   if( pDefault )
    00208     aRetColor = *pDefault;
    00209
    00210   QString aValue = readEntry( rKey );
    00211   if( aValue.isNull() )
    00212     return aRetColor;
    00213
    00214   // Support #ffffff style colour naming.
    00215   // Help ease transistion from legacy KDE setups
    00216   if( aValue.find("#") == 0 ) {
    00217     aRetColor.setNamedColor( aValue );
    00218     return aRetColor;
    00219   }
    00220
    00221   // Parse "red,green,blue"
    00222   // find first comma
    00223   int nIndex1 = aValue.find( ',' );
    00224   if( nIndex1 == -1 )
    00225     return aRetColor;
    00226   // find second comma
    00227   int nIndex2 = aValue.find( ',', nIndex1+1 );
    00228   if( nIndex2 == -1 )
    00229     return aRetColor;
    00230
    00231   bool bOK;
    00232   nRed = aValue.left( nIndex1 ).toInt( &bOK );
    00233   nGreen = aValue.mid( nIndex1+1,
    00234                        nIndex2-nIndex1-1 ).toInt( &bOK );
    00235   nBlue = aValue.mid( nIndex2+1 ).toInt( &bOK );
    00236
    00237   aRetColor.setRgb( nRed, nGreen, nBlue );
    00238
    00239   return aRetColor;
    00240 }
    00241
    00242 QString KRootProp::writeEntry( const QString& rKey, const QString& rValue )
    00243 {
    00244     dirty = TRUE;
    00245     if ( propDict.contains( rKey ) ) {
    00246         QString aValue = propDict[ rKey ];
    00247         propDict.replace( rKey, rValue );
    00248         return aValue;
    00249     }
    00250     else {
    00251         propDict.insert( rKey, rValue );
    00252         return QString::null;
    00253     }
    00254 }
    00255
    00256 QString KRootProp::writeEntry( const QString& rKey, int nValue )
    00257 {
    00258   QString aValue;
    00259
    00260   aValue.setNum( nValue );
    00261
    00262   return writeEntry( rKey, aValue );
    00263 }
    00264
    00265 QString KRootProp::writeEntry( const QString& rKey, const QFont& rFont )
    00266 {
    00267   return writeEntry( rKey, rFont.toString() );
    00268 }
    00269
    00270 QString KRootProp::writeEntry( const QString& rKey, const QColor& rColor )
    00271 {
    00272   QString aValue = QString( "%1,%2,%3").arg(rColor.red()).arg(rColor.green()).arg(rColor.blue() );
    00273
    00274   return writeEntry( rKey, aValue );
    00275 }
    00276
    00277 QString KRootProp::removeEntry(const QString& rKey)
    00278 {
    00279     if (propDict.contains(rKey)) {
    00280         dirty = TRUE;
    00281         QString aValue = propDict[rKey];
    00282         propDict.remove(rKey);
    00283         return aValue;
    00284     } else
    00285         return QString::null;
    00286 }
    00287
    00288 QStringList KRootProp::listEntries() const
    00289 {
    00290     QMap<QString,QString>::ConstIterator it;
    00291     QStringList list;
    00292
    00293     for (it=propDict.begin(); it!=propDict.end(); it++)
    00294         list += it.key();
    00295
    00296     return list;
    00297 }
    00298 #endif

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:50 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001