]> git.sven.stormbind.net Git - sven/vym.git/blob - src/settings.cpp
New upstream version 2.9.22
[sven/vym.git] / src / settings.cpp
1 #include <iostream>
2
3 #include <QDebug>
4
5 #include "file.h"
6 #include "settings.h"
7 #include <qregexp.h>
8
9 /////////////////////////////////////////////////////////////////
10 // SimpleSettings
11 /////////////////////////////////////////////////////////////////
12 SimpleSettings::SimpleSettings() { clear(); }
13
14 SimpleSettings::~SimpleSettings() {}
15
16 void SimpleSettings::clear()
17 {
18     keylist.clear();
19     valuelist.clear();
20 }
21
22 bool SimpleSettings::readSettings(const QString &path)
23 {
24     QString s;
25     if (!loadStringFromDisk(path, s)) {
26         qWarning() << "SimpleSettings::readSettings() Couldn't read " + path;
27         return false;
28     }
29     QStringList lines;
30     lines = s.split(QRegExp("\n"));
31     int i;
32     QStringList::Iterator it = lines.begin();
33     while (it != lines.end()) {
34         i = (*it).indexOf("=", 0);
35         keylist.append((*it).left(i));
36         valuelist.append((*it).right((*it).length() - i - 1));
37         it++;
38     }
39     return true;
40 }
41
42 void SimpleSettings::writeSettings(const QString &path)
43 {
44     QString s;
45     QStringList::Iterator itk = keylist.begin();
46     QStringList::Iterator itv = valuelist.begin();
47
48     // First search for value in settings saved in map
49     while (itk != keylist.end()) {
50         s += *itk + "=" + *itv + "\n";
51         itk++;
52         itv++;
53     }
54     if (!saveStringToDisk(path, s))
55         qWarning() << "SimpleSettings::writeSettings() Couldn't write " + path;
56 }
57
58 /*
59 QString SimpleSettings::readValue (const QString &key)
60 {
61     QStringList::Iterator itk=keylist.begin();
62     QStringList::Iterator itv=valuelist.begin();
63
64     // First search for value in settings saved in map
65     while (itk !=keylist.end() )
66     {
67     if (*itk == key)
68         return *itv;
69     itk++;
70     itv++;
71     }
72     qWarning ("SimpleSettings::readValue()  Couldn't find key "+key);
73     return "";
74 }
75 */
76
77 QString SimpleSettings::value(const QString &key, const QString &def)
78 {
79     QStringList::Iterator itk = keylist.begin();
80     QStringList::Iterator itv = valuelist.begin();
81
82     // First search for value in settings saved in map
83     while (itk != keylist.end()) {
84         if (*itk == key)
85             return *itv;
86         itk++;
87         itv++;
88     }
89     return def;
90 }
91
92 int SimpleSettings::numValue(const QString &key, const int &def)
93 {
94     QStringList::Iterator itk = keylist.begin();
95     QStringList::Iterator itv = valuelist.begin();
96
97     // First search for value in settings saved in map
98     while (itk != keylist.end()) {
99         if (*itk == key) {
100             bool ok;
101             int i = (*itv).toInt(&ok, 10);
102             if (ok)
103                 return i;
104             else
105                 return def;
106         }
107         itk++;
108         itv++;
109     }
110     return def;
111 }
112
113 void SimpleSettings::setValue(const QString &key, const QString &value)
114 {
115     QStringList::Iterator itk = keylist.begin();
116     QStringList::Iterator itv = valuelist.begin();
117
118     if (!key.isEmpty()) {
119         // Search for existing Value first
120         while (itk != keylist.end()) {
121             if (*itk == key) {
122                 if (!value.isEmpty())
123                     *itv = value;
124                 else
125                     *itv = "";
126                 *itv = value;
127                 return;
128             }
129             itk++;
130             itv++;
131         }
132
133         // If no Value exists, append a new one
134         keylist.append(key);
135         valuelist.append(value);
136     }
137 }
138
139 /////////////////////////////////////////////////////////////////
140 // Settings
141 /////////////////////////////////////////////////////////////////
142 Settings::Settings() { clear(); }
143
144 Settings::Settings(const QString &organization, const QString &application)
145     : QSettings(organization, application)
146 {
147     clear();
148 }
149
150 Settings::~Settings() {}
151
152 void Settings::clear()
153 {
154     pathlist.clear();
155     keylist.clear();
156     valuelist.clear();
157 }
158
159 void Settings::clearLocal(const QString &fpath, const QString &key)
160 {
161     int i = 0;
162     while (i < pathlist.count()) {
163         if (fpath == pathlist.at(i) && keylist.at(i).startsWith(key)) {
164             pathlist.removeAt(i);
165             keylist.removeAt(i);
166             valuelist.removeAt(i);
167         }
168         else
169             i++;
170     }
171 }
172
173 QVariant Settings::localValue(const QString &fpath, const QString &key,
174                               QVariant def)
175 {
176     // First search for value in settings saved in map
177     int i = 0;
178     while (i < pathlist.count()) {
179         if (pathlist.at(i) == fpath && keylist.at(i) == key)
180             return valuelist.at(i);
181         i++;
182     }
183
184     // Fall back to global vym settings
185     return value(key, def);
186 }
187
188 void Settings::setLocalValue(const QString &fpath, const QString &key,
189                              QVariant value)
190 {
191     if (!fpath.isEmpty() && !key.isEmpty() && !value.isNull()) {
192         // Search for existing Value first
193         int i = 0;
194         while (i < pathlist.count()) {
195             if (pathlist.at(i) == fpath && keylist.at(i) == key) {
196                 valuelist[i] = value;
197                 return;
198             }
199             i++;
200         }
201
202         // If no Value exists, append a new one
203         pathlist.append(fpath);
204         keylist.append(key);
205         valuelist.append(value);
206     }
207 }
208
209 QString Settings::getDataXML(const QString &fpath)
210 {
211     QString s;
212     int i = 0;
213     while (i < pathlist.count()) {
214         if (pathlist.at(i) == fpath)
215             if (!valuelist.at(i).isNull())
216                 s += indent() +
217                      valueElement("setting",
218                                   getCDATA(valuelist.at(i).toString()),
219                                   attribut("key", keylist.at(i)));
220         i++;
221     }
222     return s;
223 }