Diffstat (limited to 'noncore/apps/zsafe/extra/qsettings.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/zsafe/extra/qsettings.cpp | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/noncore/apps/zsafe/extra/qsettings.cpp b/noncore/apps/zsafe/extra/qsettings.cpp new file mode 100644 index 0000000..def2e2d --- a/dev/null +++ b/noncore/apps/zsafe/extra/qsettings.cpp | |||
@@ -0,0 +1,156 @@ | |||
1 | /* | ||
2 | ** $Id$ | ||
3 | */ | ||
4 | |||
5 | #include "qsettings.h" | ||
6 | #include <stdio.h> | ||
7 | #include <qfile.h> | ||
8 | #include <qtextstream.h> | ||
9 | |||
10 | |||
11 | QSettings::QSettings(const QString &_fn) | ||
12 | { | ||
13 | qWarning("Settings "+_fn); | ||
14 | // read the prefs from the file | ||
15 | fn = _fn; | ||
16 | |||
17 | QFile f(_fn); | ||
18 | if ( f.open(IO_ReadOnly) ) { // file opened successfully | ||
19 | QTextStream t( &f ); // use a text stream | ||
20 | QString s; | ||
21 | while ( !t.eof() ) { // until end of file... | ||
22 | s = t.readLine(); // line of text excluding '\n' | ||
23 | char buf[256]; | ||
24 | sprintf (buf, "%s", (const char *) s); | ||
25 | int pos = s.find (" = "); | ||
26 | QString key = s.left (pos); | ||
27 | QString val = s.right (s.length() - pos - 3); | ||
28 | writeEntry (key, val); | ||
29 | |||
30 | sprintf (buf, "%s|%s", (const char *)key, (const char *)val); | ||
31 | } | ||
32 | f.close(); | ||
33 | } | ||
34 | |||
35 | |||
36 | } | ||
37 | |||
38 | QSettings::~QSettings() | ||
39 | { | ||
40 | // write out the prefs to the file | ||
41 | QAsciiDictIterator <QString> it( prefs ); // iterator for dict | ||
42 | QFile f(fn); | ||
43 | f.open(IO_WriteOnly); | ||
44 | QTextStream ts( &f ); | ||
45 | |||
46 | while ( it.current() ) | ||
47 | { | ||
48 | QString *key = new QString(it.currentKey()); | ||
49 | char buf[256]; | ||
50 | sprintf (buf, "%s", (const char *) *( it.current())); | ||
51 | QString *val = new QString(buf); | ||
52 | sprintf (buf, "%s %s", (const char *)*key, (const char *)*val); | ||
53 | ts << *key << " = " << *val << endl; | ||
54 | ++it; | ||
55 | } | ||
56 | |||
57 | f.close(); | ||
58 | prefs.clear(); | ||
59 | } | ||
60 | |||
61 | void QSettings::insertSearchPath (System sys, const QString &str) | ||
62 | { | ||
63 | fn = str; | ||
64 | } | ||
65 | |||
66 | QString QSettings::readEntry (const QString &key, const QString &def) | ||
67 | { | ||
68 | |||
69 | QString *s = prefs.find((const char *)key); | ||
70 | if (!s) | ||
71 | return def; | ||
72 | else | ||
73 | return *s; | ||
74 | |||
75 | } | ||
76 | |||
77 | int QSettings::readNumEntry (const QString &key, int def) | ||
78 | { | ||
79 | QString *s = prefs[key]; | ||
80 | if (!s) return def; | ||
81 | return s->toInt(); | ||
82 | } | ||
83 | |||
84 | bool QSettings::readBoolEntry (const QString &key, bool def) | ||
85 | { | ||
86 | QString *s = prefs[key]; | ||
87 | if (!s) return def; | ||
88 | if (*s == "1") | ||
89 | return true; | ||
90 | else | ||
91 | return false; | ||
92 | } | ||
93 | |||
94 | bool QSettings::writeEntry (const QString &key, int val) | ||
95 | { | ||
96 | char buf[64]; | ||
97 | sprintf (buf, "%d", val); | ||
98 | QString *v = new QString(buf); | ||
99 | prefs.replace ((const char *)key, v); | ||
100 | return true; | ||
101 | } | ||
102 | |||
103 | bool QSettings::writeEntry (const QString &key, bool val) | ||
104 | { | ||
105 | QString *v; | ||
106 | if (val) | ||
107 | v = new QString("1"); | ||
108 | else | ||
109 | v = new QString("0"); | ||
110 | prefs.replace ((const char *)key, v); | ||
111 | return true; | ||
112 | } | ||
113 | |||
114 | bool QSettings::writeEntry (const QString &key, const QString &val) | ||
115 | { | ||
116 | QString *v = new QString (val); | ||
117 | prefs.replace ((const char *)key, v); | ||
118 | return true; | ||
119 | } | ||
120 | |||
121 | bool QSettings::writeEntry (const QString &key, const char *val) | ||
122 | { | ||
123 | QString *v = new QString (val); | ||
124 | prefs.replace ((const char *)key, v); | ||
125 | return true; | ||
126 | } | ||
127 | |||
128 | bool QSettings::removeEntry (const QString &key) | ||
129 | { | ||
130 | prefs.remove (key); | ||
131 | return true; | ||
132 | } | ||
133 | |||
134 | QStringList QSettings::entryList (const QString &key) const | ||
135 | { | ||
136 | qDebug("entryList: "+key); | ||
137 | QStringList list; | ||
138 | if(!prefs.isEmpty()) { | ||
139 | QAsciiDictIterator <QString> it( prefs ); // iterator for dict | ||
140 | qDebug("ready"); | ||
141 | while ( it.current() ) | ||
142 | { | ||
143 | char buf[512]; | ||
144 | sprintf(buf, "%s", (const char *) *( it.current())); | ||
145 | QString *val = new QString(buf); | ||
146 | sprintf(buf, "%s -> %s\n", it.currentKey(), (const char *)*val ); | ||
147 | QString *cat = new QString(it.currentKey()); | ||
148 | if (cat->contains("-field", FALSE)) | ||
149 | list.append (it.currentKey()); | ||
150 | ++it; | ||
151 | } | ||
152 | } | ||
153 | qWarning("Return here"); | ||
154 | return list; | ||
155 | } | ||
156 | |||