summaryrefslogtreecommitdiffstats
path: root/tdecore/krootprop.cpp
blob: 72fd5ae78a440e8ba10a0cb4c32b80da1e7f59de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* This file is part of the KDE libraries
    Copyright (C) 1997 Mark Donohoe (donohoe@kde.org)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <tqwidget.h>

#include "config.h"
#ifdef TQ_WS_X11 // not needed anyway :-)

#include "krootprop.h"
#include "tdeglobal.h"
#include "tdelocale.h"
#include "kcharsets.h"
#include "tdeapplication.h"
#include <tqtextstream.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>

KRootProp::KRootProp(const TQString& rProp )
{
  atom = 0;
  dirty = false;
  setProp( rProp );
}

KRootProp::~KRootProp()
{
  sync();
  propDict.clear();
}

void KRootProp::sync()
{
  if ( !dirty )
      return;

  TQString propString;
  if ( !propDict.isEmpty() )
  {
    TQMap<TQString,TQString>::Iterator it = propDict.begin();
    TQString keyvalue;

    while ( it != propDict.end() )
    {
      keyvalue = TQString( "%1=%2\n").arg(it.key()).arg(it.data());
      propString += keyvalue;
      ++it;
    }
  }

  XChangeProperty( tqt_xdisplay(), tqt_xrootwin(), atom,
                  XA_STRING, 8, PropModeReplace,
                  (const unsigned char *)propString.utf8().data(),
                  propString.length());
  XFlush( tqt_xdisplay() );
}

void KRootProp::setProp( const TQString& rProp )
{
  Atom type;
  int format;
  unsigned long nitems;
  unsigned long bytes_after;
  long offset;

  // If a property has already been opened write
  // the dictionary back to the root window

  if( atom )
    sync();

  property_ = rProp;
  if( rProp.isEmpty() )
    return;

  atom = XInternAtom( tqt_xdisplay(), rProp.utf8(), False);

  TQString s;
  offset = 0; bytes_after = 1;
  while (bytes_after != 0)
  {
    unsigned char *buf = 0; 
    if (XGetWindowProperty( tqt_xdisplay(), tqt_xrootwin(), atom, offset, 256,
                        False, XA_STRING, &type, &format, &nitems, &bytes_after,
                        &buf) == Success && buf) 
    {
      s += TQString::fromUtf8((const char*)buf);
      offset += nitems/4;
      XFree(buf);
    }
  }

  // Parse through the property string stripping out key value pairs
  // and putting them in the dictionary

  TQString keypair;
  int i=0;
  TQString key;
  TQString value;

  while(s.length() >0 )
  {
    // parse the string for first key-value pair separator '\n'

    i = s.find("\n");
    if(i == -1)
      i = s.length();

    // extract the key-values pair and remove from string

    keypair = s.left(i);
    s.remove(0,i+1);

    // split key and value and add to dictionary

    keypair.simplifyWhiteSpace();

    i = keypair.find( "=" );
    if( i != -1 )
    {
      key = keypair.left( i );
      value = keypair.mid( i+1 );
      propDict.insert( key, value );
    }
  }
}


TQString KRootProp::prop() const
{
    return property_;
}

void KRootProp::destroy()
{
    dirty = false;
    propDict.clear();
    if( atom ) {
	XDeleteProperty( tqt_xdisplay(), tqt_xrootwin(), atom );
	atom = 0;
    }
}

TQString KRootProp::readEntry( const TQString& rKey,
			    const TQString& pDefault ) const
{
  if( propDict.contains( rKey ) )
      return propDict[ rKey ];
  else
      return pDefault;
}

int KRootProp::readNumEntry( const TQString& rKey, int nDefault ) const
{

  TQString aValue = readEntry( rKey );
  if( !aValue.isNull() )
  {
    bool ok;

    int rc = aValue.toInt( &ok );
    if (ok)
      return rc;
  }
  return nDefault;
}


TQFont KRootProp::readFontEntry( const TQString& rKey,
                                const TQFont* pDefault ) const
{
  TQFont aRetFont;
  TQFont aDefFont;

  if (pDefault)
    aDefFont = *pDefault;

  TQString aValue = readEntry( rKey );
  if( aValue.isNull() )
    return aDefFont; // Return default font

  if ( !aRetFont.fromString( aValue ) && pDefault )
    aRetFont = aDefFont;

  return aRetFont;
}


TQColor KRootProp::readColorEntry( const TQString& rKey,
								const TQColor* pDefault ) const
{
  TQColor aRetColor;
  int nRed = 0, nGreen = 0, nBlue = 0;

  if( pDefault )
    aRetColor = *pDefault;

  TQString aValue = readEntry( rKey );
  if( aValue.isNull() )
    return aRetColor;

  // Support #ffffff style color naming.
  // Help ease transistion from legacy KDE setups
  if( aValue.find("#") == 0 ) {
    aRetColor.setNamedColor( aValue );
    return aRetColor;
  }

  // Parse "red,green,blue"
  // find first comma
  int nIndex1 = aValue.find( ',' );
  if( nIndex1 == -1 )
    return aRetColor;
  // find second comma
  int nIndex2 = aValue.find( ',', nIndex1+1 );
  if( nIndex2 == -1 )
    return aRetColor;

  bool bOK;
  nRed = aValue.left( nIndex1 ).toInt( &bOK );
  nGreen = aValue.mid( nIndex1+1,
                       nIndex2-nIndex1-1 ).toInt( &bOK );
  nBlue = aValue.mid( nIndex2+1 ).toInt( &bOK );

  aRetColor.setRgb( nRed, nGreen, nBlue );

  return aRetColor;
}

TQString KRootProp::writeEntry( const TQString& rKey, const TQString& rValue )
{
    dirty = true;
    if ( propDict.contains( rKey ) ) {
	TQString aValue = propDict[ rKey ];
	propDict.replace( rKey, rValue );
	return aValue;
    }
    else {
	propDict.insert( rKey, rValue );
	return TQString::null;
    }
}

TQString KRootProp::writeEntry( const TQString& rKey, int nValue )
{
  TQString aValue;

  aValue.setNum( nValue );

  return writeEntry( rKey, aValue );
}

TQString KRootProp::writeEntry( const TQString& rKey, const TQFont& rFont )
{
  return writeEntry( rKey, TQString(rFont.toString()) );
}

TQString KRootProp::writeEntry( const TQString& rKey, const TQColor& rColor )
{
  TQString aValue = TQString( "%1,%2,%3").arg(rColor.red()).arg(rColor.green()).arg(rColor.blue() );

  return writeEntry( rKey, aValue );
}

TQString KRootProp::removeEntry(const TQString& rKey)
{
    if (propDict.contains(rKey)) {
	dirty = true;
	TQString aValue = propDict[rKey];
	propDict.remove(rKey);
	return aValue;
    } else
	return TQString::null;
}

TQStringList KRootProp::listEntries() const
{
    TQMap<TQString,TQString>::ConstIterator it;
    TQStringList list;

	TQMap<TQString,TQString>::ConstIterator end(propDict.end());
    for (it=propDict.begin(); it!=end; ++it)
	list += it.key();

    return list;
}
#endif