summaryrefslogtreecommitdiff
path: root/noncore/apps/tinykate/libkate/microkde/kconfig.cpp
blob: d88bda0a01fdb26ea23ae417007929a7ca8bca9b (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
#include <qfile.h>
#include <qtextstream.h>

#include "kdebug.h"

#include "kconfig.h"

QString KConfig::mGroup = "";
//QString KConfig::mGroup = "General";

KConfig::KConfig( const QString &fileName )
  : mFileName( fileName ), mDirty( false )
{
  kdDebug() << "KConfig::KConfig(): '" << fileName << "'" << endl;

  load();
}


KConfig::~KConfig()
{
  sync();
}

void KConfig::setGroup( const QString &group )
{
  return;
  
//  kdDebug() << "KConfig::setGroup(): '" << group << "'" << endl;

  mGroup = group;

  if ( mGroup.right( 1 ) != "/" ) mGroup += "/";
}


QValueList<int> KConfig::readIntListEntry( const QString & )
{
  QValueList<int> l;
  return l;
}

int KConfig::readNumEntry( const QString &, int def )
{
  return def;
}

QString KConfig::readEntry( const QString &key, const QString &def )
{
  QMap<QString,QString>::ConstIterator it = mStringMap.find( mGroup + key );
  
  if ( it == mStringMap.end() ) {
    return def;
  }
  
  return *it;
}

QStringList KConfig::readListEntry( const QString & )
{
  return QStringList();
}

bool KConfig::readBoolEntry( const QString &key, bool def )
{
  QMap<QString,bool>::ConstIterator it = mBoolMap.find( mGroup + key );
  
  if ( it == mBoolMap.end() ) {
    return def;
  }
  
  return *it;
}

QColor KConfig::readColorEntry( const QString &, QColor *def )
{
  if ( def ) return *def;
  return QColor();
}

QFont KConfig::readFontEntry( const QString &, QFont *def )
{
  if ( def ) return *def;
  return QFont();
}


void KConfig::writeEntry( const QString &, QValueList<int> )
{
}

void KConfig::writeEntry( const QString &, int )
{
}

void KConfig::writeEntry( const QString &key, const QString &value )
{
  mStringMap.insert( mGroup + key, value );

  mDirty = true;
}

void KConfig::writeEntry( const QString &, const QStringList & )
{
}

void KConfig::writeEntry( const QString &key, bool value)
{
  mBoolMap.insert( mGroup + key, value );

  mDirty = true;
}

void KConfig::writeEntry( const QString &, const QColor & )
{
}

void KConfig::writeEntry( const QString &, const QFont & )
{
}

void KConfig::load()
{
  mBoolMap.clear();
  mStringMap.clear();

  QFile f( mFileName );
  if ( !f.open( IO_ReadOnly ) ) {
    kdDebug() << "KConfig::load(): Can't open file '" << mFileName << "'"
              << endl;
    return;
  }

  
  QTextStream t( &f );
  
  QString line = t.readLine();

  while ( !line.isNull() ) {
    QStringList tokens = QStringList::split( ",", line );
    if ( tokens[0] == "bool" ) {
      bool value = false;
      if ( tokens[2] == "1" ) value = true;
      
      mBoolMap.insert( tokens[1], value );
    } else if ( tokens[0] == "QString" ) {
      QString value = tokens[2];
      mStringMap.insert( tokens[1], value );
    }
  
    line = t.readLine();
  }
}

void KConfig::sync()
{
  if ( !mDirty ) return;

  QFile f( mFileName );
  if ( !f.open( IO_WriteOnly ) ) {
    kdDebug() << "KConfig::sync(): Can't open file '" << mFileName << "'"
              << endl;
    return;
  }

  QTextStream t( &f );
  
  QMap<QString,bool>::ConstIterator itBool;
  for( itBool = mBoolMap.begin(); itBool != mBoolMap.end(); ++itBool ) {
    t << "bool," << itBool.key() << "," << (*itBool ) << endl;
  }

  QMap<QString,QString>::ConstIterator itString;
  for( itString = mStringMap.begin(); itString != mStringMap.end(); ++itString ) {
    t << "QString," << itString.key() << "," << (*itString ) << endl;
  }

  f.close();

  mDirty = false;
}