00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <tqwidget.h>
00021
00022 #include "config.h"
00023 #ifdef Q_WS_X11 // not needed anyway :-)
00024
00025 #include "krootprop.h"
00026 #include "tdeglobal.h"
00027 #include "tdelocale.h"
00028 #include "kcharsets.h"
00029 #include "tdeapplication.h"
00030 #include <tqtextstream.h>
00031
00032 #include <X11/Xlib.h>
00033 #include <X11/Xatom.h>
00034
00035 KRootProp::KRootProp(const TQString& 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 TQString propString;
00054 if ( !propDict.isEmpty() )
00055 {
00056 TQMap<TQString,TQString>::Iterator it = propDict.begin();
00057 TQString keyvalue;
00058
00059 while ( it != propDict.end() )
00060 {
00061 keyvalue = TQString( "%1=%2\n").arg(it.key()).arg(it.data());
00062 propString += keyvalue;
00063 ++it;
00064 }
00065 }
00066
00067 XChangeProperty( tqt_xdisplay(), tqt_xrootwin(), atom,
00068 XA_STRING, 8, PropModeReplace,
00069 (const unsigned char *)propString.utf8().data(),
00070 propString.length());
00071 XFlush( tqt_xdisplay() );
00072 }
00073
00074 void KRootProp::setProp( const TQString& rProp )
00075 {
00076 Atom type;
00077 int format;
00078 unsigned long nitems;
00079 unsigned long bytes_after;
00080 long offset;
00081
00082
00083
00084
00085 if( atom )
00086 sync();
00087
00088 property_ = rProp;
00089 if( rProp.isEmpty() )
00090 return;
00091
00092 atom = XInternAtom( tqt_xdisplay(), rProp.utf8(), False);
00093
00094 TQString s;
00095 offset = 0; bytes_after = 1;
00096 while (bytes_after != 0)
00097 {
00098 unsigned char *buf = 0;
00099 if (XGetWindowProperty( tqt_xdisplay(), tqt_xrootwin(), atom, offset, 256,
00100 False, XA_STRING, &type, &format, &nitems, &bytes_after,
00101 &buf) == Success && buf)
00102 {
00103 s += TQString::fromUtf8((const char*)buf);
00104 offset += nitems/4;
00105 XFree(buf);
00106 }
00107 }
00108
00109
00110
00111
00112 TQString keypair;
00113 int i=0;
00114 TQString key;
00115 TQString value;
00116
00117 while(s.length() >0 )
00118 {
00119
00120
00121 i = s.find("\n");
00122 if(i == -1)
00123 i = s.length();
00124
00125
00126
00127 keypair = s.left(i);
00128 s.remove(0,i+1);
00129
00130
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 TQString 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( tqt_xdisplay(), tqt_xrootwin(), atom );
00156 atom = 0;
00157 }
00158 }
00159
00160 TQString KRootProp::readEntry( const TQString& rKey,
00161 const TQString& pDefault ) const
00162 {
00163 if( propDict.contains( rKey ) )
00164 return propDict[ rKey ];
00165 else
00166 return pDefault;
00167 }
00168
00169 int KRootProp::readNumEntry( const TQString& rKey, int nDefault ) const
00170 {
00171
00172 TQString 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 TQFont KRootProp::readFontEntry( const TQString& rKey,
00186 const TQFont* pDefault ) const
00187 {
00188 TQFont aRetFont;
00189 TQFont aDefFont;
00190
00191 if (pDefault)
00192 aDefFont = *pDefault;
00193
00194 TQString aValue = readEntry( rKey );
00195 if( aValue.isNull() )
00196 return aDefFont;
00197
00198 if ( !aRetFont.fromString( aValue ) && pDefault )
00199 aRetFont = aDefFont;
00200
00201 return aRetFont;
00202 }
00203
00204
00205 TQColor KRootProp::readColorEntry( const TQString& rKey,
00206 const TQColor* pDefault ) const
00207 {
00208 TQColor aRetColor;
00209 int nRed = 0, nGreen = 0, nBlue = 0;
00210
00211 if( pDefault )
00212 aRetColor = *pDefault;
00213
00214 TQString aValue = readEntry( rKey );
00215 if( aValue.isNull() )
00216 return aRetColor;
00217
00218
00219
00220 if( aValue.find("#") == 0 ) {
00221 aRetColor.setNamedColor( aValue );
00222 return aRetColor;
00223 }
00224
00225
00226
00227 int nIndex1 = aValue.find( ',' );
00228 if( nIndex1 == -1 )
00229 return aRetColor;
00230
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 TQString KRootProp::writeEntry( const TQString& rKey, const TQString& rValue )
00247 {
00248 dirty = true;
00249 if ( propDict.contains( rKey ) ) {
00250 TQString aValue = propDict[ rKey ];
00251 propDict.replace( rKey, rValue );
00252 return aValue;
00253 }
00254 else {
00255 propDict.insert( rKey, rValue );
00256 return TQString::null;
00257 }
00258 }
00259
00260 TQString KRootProp::writeEntry( const TQString& rKey, int nValue )
00261 {
00262 TQString aValue;
00263
00264 aValue.setNum( nValue );
00265
00266 return writeEntry( rKey, aValue );
00267 }
00268
00269 TQString KRootProp::writeEntry( const TQString& rKey, const TQFont& rFont )
00270 {
00271 return writeEntry( rKey, TQString(rFont.toString()) );
00272 }
00273
00274 TQString KRootProp::writeEntry( const TQString& rKey, const TQColor& rColor )
00275 {
00276 TQString aValue = TQString( "%1,%2,%3").arg(rColor.red()).arg(rColor.green()).arg(rColor.blue() );
00277
00278 return writeEntry( rKey, aValue );
00279 }
00280
00281 TQString KRootProp::removeEntry(const TQString& rKey)
00282 {
00283 if (propDict.contains(rKey)) {
00284 dirty = true;
00285 TQString aValue = propDict[rKey];
00286 propDict.remove(rKey);
00287 return aValue;
00288 } else
00289 return TQString::null;
00290 }
00291
00292 TQStringList KRootProp::listEntries() const
00293 {
00294 TQMap<TQString,TQString>::ConstIterator it;
00295 TQStringList list;
00296
00297 TQMap<TQString,TQString>::ConstIterator end(propDict.end());
00298 for (it=propDict.begin(); it!=end; ++it)
00299 list += it.key();
00300
00301 return list;
00302 }
00303 #endif