summaryrefslogtreecommitdiff
authorzecke <zecke>2003-05-14 12:08:54 (UTC)
committer zecke <zecke>2003-05-14 12:08:54 (UTC)
commitc480b91a5afe1f259287c3c4173ec02f2b4854cb (patch) (unidiff)
treeffe59a2fc544f6067fd96065d94a877188f1ef88
parent415a300230e10e29660b2af26a23798fd0b12e03 (diff)
downloadopie-c480b91a5afe1f259287c3c4173ec02f2b4854cb.zip
opie-c480b91a5afe1f259287c3c4173ec02f2b4854cb.tar.gz
opie-c480b91a5afe1f259287c3c4173ec02f2b4854cb.tar.bz2
patch by Scott Bronson
kill the duplicated config.cpp and config.h file add proper default values so that kpacman actually works out of the box Config wanted to read kpacman.conf but this file did only exist globally
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/kpacman/config.cpp394
-rw-r--r--noncore/games/kpacman/config.h74
-rw-r--r--noncore/games/kpacman/keys.cpp4
-rw-r--r--noncore/games/kpacman/kpacman.cpp18
-rw-r--r--noncore/games/kpacman/kpacman.pro2
-rw-r--r--noncore/games/kpacman/kpacmanwidget.cpp2
-rw-r--r--noncore/games/kpacman/painter.cpp2
-rw-r--r--noncore/games/kpacman/referee.cpp2
-rw-r--r--noncore/games/kpacman/status.cpp4
9 files changed, 19 insertions, 483 deletions
diff --git a/noncore/games/kpacman/config.cpp b/noncore/games/kpacman/config.cpp
deleted file mode 100644
index b9e3607..0000000
--- a/noncore/games/kpacman/config.cpp
+++ b/dev/null
@@ -1,394 +0,0 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qt Palmtop Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19** $Id$
20**
21**********************************************************************/
22
23#include "config.h"
24
25#include <qfile.h>
26#include <qdir.h>
27#include <qfileinfo.h>
28#include <qtextstream.h>
29#if QT_VERSION <= 230 && defined(QT_NO_CODECS)
30#include <qtextcodec.h>
31#endif
32#include <stdlib.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <fcntl.h>
36#include <unistd.h>
37
38/*!
39 \internal
40*/
41QString Config::configFilename(const QString& name, Domain d)
42{
43 switch (d) {
44 case File:
45 return name;
46 case User: {
47 QDir dir = (QString(getenv("HOME")) + "/Settings");
48 if ( !dir.exists() )
49 mkdir(dir.path().local8Bit(),0700);
50 return dir.path() + "/" + name + ".conf";
51 }
52 }
53 return name;
54}
55
56/*!
57 \class Config config.h
58 \brief The Config class provides for saving application cofniguration state.
59
60 You should keep a Config in existence only while you do not want others
61 to be able to change the state. There is no locking currently, but there
62 may be in the future.
63*/
64
65/*!
66 \enum Config::ConfigGroup
67 \internal
68*/
69
70/*!
71 \enum Config::Domain
72
73 \value File
74 \value User
75
76 See Config for details.
77*/
78
79/*!
80 Constructs a config that will load or create a configuration with the
81 given \a name in the given \a domain.
82
83 You must call setGroup() before doing much else with the Config.
84
85 In the default Domain, \e User,
86 the configuration is user-specific. \a name should not contain "/" in
87 this case, and in general should be the name of the C++ class that is
88 primarily responsible for maintaining the configuration.
89
90 In the File Domain, \a name is an absolute filename.
91*/
92Config::Config( const QString &name, Domain domain )
93 : filename( configFilename(name,domain) )
94{
95 git = groups.end();
96 read();
97
98 lang = getenv("LANG");
99 int i = lang.find(".");
100 if ( i > 0 )
101 lang = lang.left( i );
102 i = lang.find( "_" );
103 if ( i > 0 )
104 glang = lang.left(i);
105}
106
107/*!
108 Writes any changes to disk and destroys the in-memory object.
109*/
110Config::~Config()
111{
112 if ( changed )
113 write();
114}
115
116/*!
117 Returns whether the current group has an entry called \a key.
118*/
119bool Config::hasKey( const QString &key ) const
120{
121 if ( groups.end() == git )
122 return FALSE;
123 ConfigGroup::ConstIterator it = ( *git ).find( key );
124 return it != ( *git ).end();
125}
126
127/*!
128 Sets the current group for subsequent reading and writing of
129 entries to \a gname. Grouping allows the application to partition the namespace.
130
131 This function must be called prior to any reading or writing
132 of entries.
133
134 The \a gname must not be empty.
135*/
136void Config::setGroup( const QString &gname )
137{
138 QMap< QString, ConfigGroup>::Iterator it = groups.find( gname );
139 if ( it == groups.end() ) {
140 ConfigGroup *grp = new ConfigGroup;
141 git = groups.insert( gname, *grp );
142 changed = TRUE;
143 return;
144 }
145 git = it;
146}
147
148/*!
149 Writes a (\a key, \a value) entry to the current group.
150
151 \sa readEntry()
152*/
153void Config::writeEntry( const QString &key, const QString &value )
154{
155 if ( git == groups.end() ) {
156 qWarning( "no group set" );
157 return;
158 }
159 if ( (*git)[key] != value ) {
160 ( *git ).insert( key, value );
161 changed = TRUE;
162 }
163}
164
165/*!
166 Writes a (\a key, \a num) entry to the current group.
167
168 \sa readNumEntry()
169*/
170void Config::writeEntry( const QString &key, int num )
171{
172 QString s;
173 s.setNum( num );
174 writeEntry( key, s );
175}
176
177#ifdef Q_HAS_BOOL_TYPE
178/*!
179 Writes a (\a key, \a b) entry to the current group.
180
181 \sa readBoolEntry()
182*/
183void Config::writeEntry( const QString &key, bool b )
184{
185 QString s;
186 s.setNum( ( int )b );
187 writeEntry( key, s );
188}
189#endif
190
191/*!
192 Writes a (\a key, \a lst) entry to the current group. The list
193 is separated by \a sep, so the strings must not contain that character.
194
195 \sa readListEntry()
196*/
197void Config::writeEntry( const QString &key, const QStringList &lst, const QChar &sep )
198{
199 QString s;
200 QStringList::ConstIterator it = lst.begin();
201 for ( ; it != lst.end(); ++it )
202 s += *it + sep;
203 writeEntry( key, s );
204}
205
206
207
208/*!
209 Reads a string entry stored with \a key, defaulting to \a deflt if there is no entry.
210*/
211QString Config::readEntry( const QString &key, const QString &deflt )
212{
213 QString res = readEntryDirect( key+"["+lang+"]" );
214 if ( !res.isNull() )
215 return res;
216 if ( !glang.isEmpty() ) {
217 res = readEntryDirect( key+"["+glang+"]" );
218 if ( !res.isNull() )
219 return res;
220 }
221 return readEntryDirect( key, deflt );
222}
223
224/*!
225 \internal
226*/
227QString Config::readEntryDirect( const QString &key, const QString &deflt )
228{
229 if ( git == groups.end() ) {
230 //qWarning( "no group set" );
231 return deflt;
232 }
233 ConfigGroup::Iterator it = ( *git ).find( key );
234 if ( it != ( *git ).end() )
235 return *it;
236 else
237 return deflt;
238}
239
240/*!
241 Reads a numeric entry stored with \a key, defaulting to \a deflt if there is no entry.
242*/
243int Config::readNumEntry( const QString &key, int deflt )
244{
245 QString s = readEntry( key );
246 if ( s.isEmpty() )
247 return deflt;
248 else
249 return s.toInt();
250}
251
252/*!
253 Reads a bool entry stored with \a key, defaulting to \a deflt if there is no entry.
254*/
255bool Config::readBoolEntry( const QString &key, bool deflt )
256{
257 QString s = readEntry( key );
258 if ( s.isEmpty() )
259 return deflt;
260 else
261 return (bool)s.toInt();
262}
263
264/*!
265 Reads a string list entry stored with \a key, and with \a sep as the separator.
266*/
267QStringList Config::readListEntry( const QString &key, const QChar &sep )
268{
269 QString s = readEntry( key );
270 if ( s.isEmpty() )
271 return QStringList();
272 else
273 return QStringList::split( sep, s );
274}
275
276/*!
277 Removes all entries from the current group.
278*/
279void Config::clearGroup()
280{
281 if ( git == groups.end() ) {
282 qWarning( "no group set" );
283 return;
284 }
285 if ( !(*git).isEmpty() ) {
286 ( *git ).clear();
287 changed = TRUE;
288 }
289}
290
291/*!
292 \internal
293*/
294void Config::write( const QString &fn )
295{
296 if ( !fn.isEmpty() )
297 filename = fn;
298
299 QFile f( filename );
300 if ( !f.open( IO_WriteOnly ) ) {
301 qWarning( "could not open for writing `%s'", filename.latin1() );
302 git = groups.end();
303 return;
304 }
305
306 QTextStream s( &f );
307#if QT_VERSION <= 230 && defined(QT_NO_CODECS)
308 // The below should work, but doesn't in Qt 2.3.0
309 s.setCodec( QTextCodec::codecForMib( 106 ) );
310#else
311 s.setEncoding( QTextStream::UnicodeUTF8 );
312#endif
313 QMap< QString, ConfigGroup >::Iterator g_it = groups.begin();
314 for ( ; g_it != groups.end(); ++g_it ) {
315 s << "[" << g_it.key() << "]" << "\n";
316 ConfigGroup::Iterator e_it = ( *g_it ).begin();
317 for ( ; e_it != ( *g_it ).end(); ++e_it )
318 s << e_it.key() << " = " << *e_it << "\n";
319 }
320
321 f.close();
322}
323
324/*!
325 Returns whether the Config is in a valid state.
326*/
327bool Config::isValid() const
328{
329 return groups.end() != git;
330}
331
332/*!
333 \internal
334*/
335void Config::read()
336{
337 changed = FALSE;
338
339 if ( !QFileInfo( filename ).exists() ) {
340 git = groups.end();
341 return;
342 }
343
344 QFile f( filename );
345 if ( !f.open( IO_ReadOnly ) ) {
346 git = groups.end();
347 return;
348 }
349
350 QTextStream s( &f );
351#if QT_VERSION <= 230 && defined(QT_NO_CODECS)
352 // The below should work, but doesn't in Qt 2.3.0
353 s.setCodec( QTextCodec::codecForMib( 106 ) );
354#else
355 s.setEncoding( QTextStream::UnicodeUTF8 );
356#endif
357
358 QString line;
359 while ( !s.atEnd() ) {
360 line = s.readLine();
361 if ( !parse( line ) ) {
362 git = groups.end();
363 return;
364 }
365 }
366
367 f.close();
368}
369
370/*!
371 \internal
372*/
373bool Config::parse( const QString &l )
374{
375 QString line = l.stripWhiteSpace();
376 if ( line[ 0 ] == QChar( '[' ) ) {
377 QString gname = line;
378 gname = gname.remove( 0, 1 );
379 if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) )
380 gname = gname.remove( gname.length() - 1, 1 );
381 ConfigGroup *grp = new ConfigGroup;
382 git = groups.insert( gname, *grp );
383 } else if ( !line.isEmpty() ) {
384 if ( git == groups.end() )
385 return FALSE;
386 int eq = line.find( '=' );
387 if ( eq == -1 )
388 return FALSE;
389 QString key = line.left(eq).stripWhiteSpace();
390 QString value = line.mid(eq+1).stripWhiteSpace();
391 ( *git ).insert( key, value );
392 }
393 return TRUE;
394}
diff --git a/noncore/games/kpacman/config.h b/noncore/games/kpacman/config.h
deleted file mode 100644
index 3c26b5d..0000000
--- a/noncore/games/kpacman/config.h
+++ b/dev/null
@@ -1,74 +0,0 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qt Designer.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#ifndef CONFIG_H
22#define CONFIG_H
23
24// ##### could use QSettings with Qt 3.0
25
26#include <qmap.h>
27#include <qstringlist.h>
28
29class ConfigPrivate;
30class Config
31{
32public:
33 typedef QMap< QString, QString > ConfigGroup;
34
35 enum Domain { File, User };
36 Config( const QString &name, Domain domain=User );
37 ~Config();
38
39 bool isValid() const;
40 bool hasKey( const QString &key ) const;
41
42 void setGroup( const QString &gname );
43 void writeEntry( const QString &key, const QString &value );
44 void writeEntry( const QString &key, int num );
45#ifdef Q_HAS_BOOL_TYPE
46 void writeEntry( const QString &key, bool b );
47#endif
48 void writeEntry( const QString &key, const QStringList &lst, const QChar &sep );
49
50 QString readEntry( const QString &key, const QString &deflt = QString::null );
51 QString readEntryDirect( const QString &key, const QString &deflt = QString::null );
52 int readNumEntry( const QString &key, int deflt = -1 );
53 bool readBoolEntry( const QString &key, bool deflt = FALSE );
54 QStringList readListEntry( const QString &key, const QChar &sep );
55
56 void clearGroup();
57
58 void write( const QString &fn = QString::null );
59
60protected:
61 void read();
62 bool parse( const QString &line );
63
64 QMap< QString, ConfigGroup > groups;
65 QMap< QString, ConfigGroup >::Iterator git;
66 QString filename;
67 QString lang;
68 QString glang;
69 bool changed;
70 ConfigPrivate *d;
71 static QString configFilename(const QString& name, Domain);
72};
73
74#endif
diff --git a/noncore/games/kpacman/keys.cpp b/noncore/games/kpacman/keys.cpp
index 8b17785..07ce135 100644
--- a/noncore/games/kpacman/keys.cpp
+++ b/noncore/games/kpacman/keys.cpp
@@ -15,3 +15,3 @@
15#include <qpe/qpeapplication.h> 15#include <qpe/qpeapplication.h>
16#include "config.h" 16#include <qpe/config.h>
17#include "keys.h" 17#include "keys.h"
@@ -195,2 +195,3 @@ void Keys::ok()
195{ 195{
196 /*
196 APP_CONFIG_BEGIN( cfg ); 197 APP_CONFIG_BEGIN( cfg );
@@ -201,2 +202,3 @@ void Keys::ok()
201 APP_CONFIG_END( cfg ); 202 APP_CONFIG_END( cfg );
203 */
202 accept(); 204 accept();
diff --git a/noncore/games/kpacman/kpacman.cpp b/noncore/games/kpacman/kpacman.cpp
index aee8eea..812e9ea 100644
--- a/noncore/games/kpacman/kpacman.cpp
+++ b/noncore/games/kpacman/kpacman.cpp
@@ -9,3 +9,3 @@
9#include <qmenubar.h> 9#include <qmenubar.h>
10#include "config.h" 10#include <qpe/config.h>
11#include <qapplication.h> 11#include <qapplication.h>
@@ -72,3 +72,3 @@ Kpacman::~Kpacman()
72{ 72{
73 APP_CONFIG_BEGIN( cfg ); 73 /* APP_CONFIG_BEGIN( cfg );
74 cfg->writeEntry("FocusOutPause", focusOutPause); 74 cfg->writeEntry("FocusOutPause", focusOutPause);
@@ -77,2 +77,3 @@ Kpacman::~Kpacman()
77 APP_CONFIG_END( cfg ); 77 APP_CONFIG_END( cfg );
78 */
78 delete _menuBar; 79 delete _menuBar;
@@ -145,7 +146,8 @@ int Kpacman::lookupSchemes()
145 APP_CONFIG_BEGIN( cfg ); 146 APP_CONFIG_BEGIN( cfg );
146 int ModeCount = cfg->readNumEntry("ModeCount", -1); 147 int ModeCount = cfg->readNumEntry("ModeCount", 0);
147 int Mode = cfg->readNumEntry("Mode", -1); 148 int Mode = cfg->readNumEntry("Mode", 0);
148 int SchemeCount = cfg->readNumEntry("SchemeCount"); 149 int SchemeCount = cfg->readNumEntry("SchemeCount", 0);
149 int Scheme = cfg->readNumEntry("Scheme", -1); 150 int Scheme = cfg->readNumEntry("Scheme", 0);
150 151
152 /*
151 if (SchemeCount == 0 || Scheme == -1) { 153 if (SchemeCount == 0 || Scheme == -1) {
@@ -157,2 +159,3 @@ int Kpacman::lookupSchemes()
157 } 159 }
160 */
158 161
@@ -351,3 +354,3 @@ void Kpacman::schemeChecked(int id)
351 } 354 }
352 355 /*
353 APP_CONFIG_BEGIN( cfg ); 356 APP_CONFIG_BEGIN( cfg );
@@ -356,2 +359,3 @@ void Kpacman::schemeChecked(int id)
356 APP_CONFIG_END( cfg ); 359 APP_CONFIG_END( cfg );
360 */
357 361
diff --git a/noncore/games/kpacman/kpacman.pro b/noncore/games/kpacman/kpacman.pro
index e193ebc..81d369a 100644
--- a/noncore/games/kpacman/kpacman.pro
+++ b/noncore/games/kpacman/kpacman.pro
@@ -19,3 +19,2 @@ HEADERS = kpacmanwidget.h \
19 colors.h \ 19 colors.h \
20 config.h \
21 portable.h 20 portable.h
@@ -34,3 +33,2 @@ SOURCES = kpacmanwidget.cpp \
34 kpacman.cpp \ 33 kpacman.cpp \
35 config.cpp \
36 main.cpp 34 main.cpp
diff --git a/noncore/games/kpacman/kpacmanwidget.cpp b/noncore/games/kpacman/kpacmanwidget.cpp
index cf2aed9..823d2bf 100644
--- a/noncore/games/kpacman/kpacmanwidget.cpp
+++ b/noncore/games/kpacman/kpacmanwidget.cpp
@@ -11,3 +11,3 @@
11#include <qpe/qpeapplication.h> 11#include <qpe/qpeapplication.h>
12#include "config.h" 12#include <qpe/config.h>
13#include "kpacmanwidget.h" 13#include "kpacmanwidget.h"
diff --git a/noncore/games/kpacman/painter.cpp b/noncore/games/kpacman/painter.cpp
index 80aeab0..d8c7460 100644
--- a/noncore/games/kpacman/painter.cpp
+++ b/noncore/games/kpacman/painter.cpp
@@ -9,3 +9,3 @@
9#include <qpe/qpeapplication.h> 9#include <qpe/qpeapplication.h>
10#include "config.h" 10#include <qpe/config.h>
11#endif 11#endif
diff --git a/noncore/games/kpacman/referee.cpp b/noncore/games/kpacman/referee.cpp
index 6d8f3fb..1b810d8 100644
--- a/noncore/games/kpacman/referee.cpp
+++ b/noncore/games/kpacman/referee.cpp
@@ -13,3 +13,3 @@
13#include <qpe/qpeapplication.h> 13#include <qpe/qpeapplication.h>
14#include "config.h" 14#include <qpe/config.h>
15#include "referee.h" 15#include "referee.h"
diff --git a/noncore/games/kpacman/status.cpp b/noncore/games/kpacman/status.cpp
index 1ef910d..2a17c21 100644
--- a/noncore/games/kpacman/status.cpp
+++ b/noncore/games/kpacman/status.cpp
@@ -11,3 +11,3 @@
11#include <qpe/qpeapplication.h> 11#include <qpe/qpeapplication.h>
12#include "config.h" 12#include <qpe/config.h>
13#include "status.h" 13#include "status.h"
@@ -249,3 +249,3 @@ void Status::confMisc(bool defGroup)
249 fillStrList(levelPixmapName, 249 fillStrList(levelPixmapName,
250 cfg->readEntry("LevelPixmapName", "level.xpm"), maxLevel+1); 250 cfg->readEntry("LevelPixmapName", "fruit.xpm"), maxLevel+1);
251 APP_CONFIG_END( cfg ); 251 APP_CONFIG_END( cfg );