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

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:10 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003