summaryrefslogtreecommitdiff
path: root/noncore/apps/zsafe/qsettings.cpp
blob: ee55339e38888b10456a96d4c3fe3b06bdcf53e8 (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
/*
** $Id$
*/

#include "qsettings.h"
#include <stdio.h>
#include <qfile.h>
#include <qtextstream.h>


QSettings::QSettings(const QString &_fn)
{
	// read the prefs from the file
	fn = _fn;
	
	QFile f(_fn);
    if ( f.open(IO_ReadOnly) ) {    // file opened successfully
        QTextStream t( &f );        // use a text stream
        QString s;
        while ( !t.eof() ) {        // until end of file...
            s = t.readLine();       // line of text excluding '\n'
			char buf[256];
			sprintf (buf, "%s", (const char *) s);
			int pos = s.find (" = ");
			QString key = s.left (pos);
			QString val = s.right (s.length() - pos - 3);
			writeEntry (key, val);
			
			sprintf (buf, "%s|%s", (const char *)key, (const char *)val);
        }
        f.close();
    }
	

}

QSettings::~QSettings()
{
	// write out the prefs to the file
	QAsciiDictIterator <QString> it( prefs ); // iterator for dict
	QFile f(fn);
	f.open(IO_WriteOnly);
	QTextStream ts( &f );

    while ( it.current() )
	{
	   QString *key = new QString(it.currentKey());
	   char buf[256];
	   sprintf	 (buf, "%s", (const char *) *( it.current()));
	   QString *val = new QString(buf);
	   sprintf (buf, "%s %s", (const char *)*key, (const char *)*val);
	   ts << *key << " = " << *val << endl;
       ++it;
	}
	
	f.close();
	prefs.clear();
}

void QSettings::insertSearchPath (System sys, const QString &str)
{
	fn = str;
}

QString QSettings::readEntry (const QString &key, const QString &def)
{
	
	QString *s = prefs.find((const char *)key);
	if (!s)
		return def;
	else 
		return *s;
	
}

int     QSettings::readNumEntry (const QString &key, int def)
{
	QString *s = prefs[key];
	if (!s) return def;
    return s->toInt();
}

bool    QSettings::readBoolEntry (const QString &key, bool def)
{
	QString *s = prefs[key];
	if (!s) return def;
	if (*s == "1")
		return true;
	else
		return false;
}

bool    QSettings::writeEntry (const QString &key, int val)
{
	char buf[64];
	sprintf (buf, "%d", val);
	QString *v = new QString(buf);
    prefs.replace ((const char *)key, v);
	return true;
}

bool    QSettings::writeEntry (const QString &key, bool val)
{
	QString *v;
	if (val)
	   v = new QString("1");
	else
       v = new QString("0");
    prefs.replace ((const char *)key, v);
	return true;
}

bool    QSettings::writeEntry (const QString &key, const QString &val)
{
	QString *v = new QString (val);
	prefs.replace ((const char *)key, v);
	return true;
}

bool    QSettings::writeEntry (const QString &key, const char *val)
{
	QString *v = new QString (val);
	prefs.replace ((const char *)key, v);
	return true;
}

bool    QSettings::removeEntry (const QString &key)
{
	prefs.remove (key);
	return true;
}

QStringList QSettings::entryList (const QString &key) const
{
	QAsciiDictIterator <QString> it( prefs ); // iterator for dict
	QStringList list;

    while ( it.current() )
	{
	   char buf[512];
	   sprintf	 (buf, "%s", (const char *) *( it.current()));
	   QString *val = new QString(buf);
       sprintf(buf, "%s -> %s\n", it.currentKey(), (const char *)*val );
	   QString *cat = new QString(it.currentKey());
       if (cat->contains("-field", FALSE))
	      list.append (it.currentKey());
       ++it;
    
	}
	return list;
}