• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KDECore

krootprop.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, 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 
00082   // If a property has already been opened write
00083   // the dictionary back to the root window
00084 
00085   if( atom )
00086     sync();
00087 
00088   property_ = rProp;
00089   if( rProp.isEmpty() )
00090     return;
00091 
00092   atom = XInternAtom( qt_xdisplay(), rProp.utf8(), False);
00093 
00094   QString s;
00095   offset = 0; bytes_after = 1;
00096   while (bytes_after != 0)
00097   {
00098     unsigned char *buf = 0; 
00099     if (XGetWindowProperty( qt_xdisplay(), qt_xrootwin(), atom, offset, 256,
00100                         False, XA_STRING, &type, &format, &nitems, &bytes_after,
00101                         &buf) == Success && buf) 
00102     {
00103       s += QString::fromUtf8((const char*)buf);
00104       offset += nitems/4;
00105       XFree(buf);
00106     }
00107   }
00108 
00109   // Parse through the property string stripping out key value pairs
00110   // and putting them in the dictionary
00111 
00112   QString keypair;
00113   int i=0;
00114   QString key;
00115   QString value;
00116 
00117   while(s.length() >0 )
00118   {
00119     // parse the string for first key-value pair separator '\n'
00120 
00121     i = s.find("\n");
00122     if(i == -1)
00123       i = s.length();
00124 
00125     // extract the key-values pair and remove from string
00126 
00127     keypair = s.left(i);
00128     s.remove(0,i+1);
00129 
00130     // split key and value and add to dictionary
00131 
00132     keypair.simplifyWhiteSpace();
00133 
00134     i = keypair.find( "=" );
00135     if( i != -1 )
00136     {
00137       key = keypair.left( i );
00138       value = keypair.mid( i+1 );
00139       propDict.insert( key, value );
00140     }
00141   }
00142 }
00143 
00144 
00145 QString KRootProp::prop() const
00146 {
00147     return property_;
00148 }
00149 
00150 void KRootProp::destroy()
00151 {
00152     dirty = false;
00153     propDict.clear();
00154     if( atom ) {
00155     XDeleteProperty( qt_xdisplay(), qt_xrootwin(), atom );
00156     atom = 0;
00157     }
00158 }
00159 
00160 QString KRootProp::readEntry( const QString& rKey,
00161                 const QString& pDefault ) const
00162 {
00163   if( propDict.contains( rKey ) )
00164       return propDict[ rKey ];
00165   else
00166       return pDefault;
00167 }
00168 
00169 int KRootProp::readNumEntry( const QString& rKey, int nDefault ) const
00170 {
00171 
00172   QString aValue = readEntry( rKey );
00173   if( !aValue.isNull() )
00174   {
00175     bool ok;
00176 
00177     int rc = aValue.toInt( &ok );
00178     if (ok)
00179       return rc;
00180   }
00181   return nDefault;
00182 }
00183 
00184 
00185 QFont KRootProp::readFontEntry( const QString& rKey,
00186                                 const QFont* pDefault ) const
00187 {
00188   QFont aRetFont;
00189   QFont aDefFont;
00190 
00191   if (pDefault)
00192     aDefFont = *pDefault;
00193 
00194   QString aValue = readEntry( rKey );
00195   if( aValue.isNull() )
00196     return aDefFont; // Return default font
00197 
00198   if ( !aRetFont.fromString( aValue ) && pDefault )
00199     aRetFont = aDefFont;
00200 
00201   return aRetFont;
00202 }
00203 
00204 
00205 QColor KRootProp::readColorEntry( const QString& rKey,
00206                                 const QColor* pDefault ) const
00207 {
00208   QColor aRetColor;
00209   int nRed = 0, nGreen = 0, nBlue = 0;
00210 
00211   if( pDefault )
00212     aRetColor = *pDefault;
00213 
00214   QString aValue = readEntry( rKey );
00215   if( aValue.isNull() )
00216     return aRetColor;
00217 
00218   // Support #ffffff style color naming.
00219   // Help ease transistion from legacy KDE setups
00220   if( aValue.find("#") == 0 ) {
00221     aRetColor.setNamedColor( aValue );
00222     return aRetColor;
00223   }
00224 
00225   // Parse "red,green,blue"
00226   // find first comma
00227   int nIndex1 = aValue.find( ',' );
00228   if( nIndex1 == -1 )
00229     return aRetColor;
00230   // find second comma
00231   int nIndex2 = aValue.find( ',', nIndex1+1 );
00232   if( nIndex2 == -1 )
00233     return aRetColor;
00234 
00235   bool bOK;
00236   nRed = aValue.left( nIndex1 ).toInt( &bOK );
00237   nGreen = aValue.mid( nIndex1+1,
00238                        nIndex2-nIndex1-1 ).toInt( &bOK );
00239   nBlue = aValue.mid( nIndex2+1 ).toInt( &bOK );
00240 
00241   aRetColor.setRgb( nRed, nGreen, nBlue );
00242 
00243   return aRetColor;
00244 }
00245 
00246 QString KRootProp::writeEntry( const QString& rKey, const QString& rValue )
00247 {
00248     dirty = true;
00249     if ( propDict.contains( rKey ) ) {
00250     QString aValue = propDict[ rKey ];
00251     propDict.replace( rKey, rValue );
00252     return aValue;
00253     }
00254     else {
00255     propDict.insert( rKey, rValue );
00256     return QString::null;
00257     }
00258 }
00259 
00260 QString KRootProp::writeEntry( const QString& rKey, int nValue )
00261 {
00262   QString aValue;
00263 
00264   aValue.setNum( nValue );
00265 
00266   return writeEntry( rKey, aValue );
00267 }
00268 
00269 QString KRootProp::writeEntry( const QString& rKey, const QFont& rFont )
00270 {
00271   return writeEntry( rKey, rFont.toString() );
00272 }
00273 
00274 QString KRootProp::writeEntry( const QString& rKey, const QColor& rColor )
00275 {
00276   QString aValue = QString( "%1,%2,%3").arg(rColor.red()).arg(rColor.green()).arg(rColor.blue() );
00277 
00278   return writeEntry( rKey, aValue );
00279 }
00280 
00281 QString KRootProp::removeEntry(const QString& rKey)
00282 {
00283     if (propDict.contains(rKey)) {
00284     dirty = true;
00285     QString aValue = propDict[rKey];
00286     propDict.remove(rKey);
00287     return aValue;
00288     } else
00289     return QString::null;
00290 }
00291 
00292 QStringList KRootProp::listEntries() const
00293 {
00294     QMap<QString,QString>::ConstIterator it;
00295     QStringList list;
00296 
00297     QMap<QString,QString>::ConstIterator end(propDict.end());
00298     for (it=propDict.begin(); it!=end; ++it)
00299     list += it.key();
00300 
00301     return list;
00302 }
00303 #endif

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal