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