182f0ab4084382de9d1dc7a08ce70ff784af3834
[jenirok] / src / common / settings.cpp
1 /*
2  * This file is part of Jenirok.
3  *
4  * Jenirok is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Jenirok is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtSql/QSqlQuery>
20 #include <QtCore/QVariant>
21 #include <QtCore/QDebug>
22 #include <QtCore/QTranslator>
23 #include <QtCore/QLocale>
24 #include <gconf/gconf-client.h>
25 #include "settings.h"
26 #include "db.h"
27
28 namespace
29 {
30     static int const LANGUAGE_COUNT = 4;
31
32     static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = {
33        "English",
34        "Norsk",
35        "Suomi",
36        "Svenska"
37     };
38
39     static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = {
40        "en_US",
41        "nb_NO",
42        "fi_FI",
43        "sv_SE"
44     };
45 }
46
47 Settings* Settings::instance_ = 0;
48
49 Settings* Settings::instance()
50 {
51     if(!instance_)
52     {
53         instance_ = new Settings;
54     }
55
56     return instance_;
57 }
58
59 void Settings::getLanguages(QList<Settings::Language>& languages)
60 {
61     for(int i = 0; i < LANGUAGE_COUNT; i++)
62     {
63         Language lang;
64         lang.name = LANGUAGE_NAMES[i];
65         lang.id = LANGUAGE_IDS[i];
66         languages.push_back(lang);
67     }
68 }
69
70 void Settings::loadLanguage(QApplication& app)
71 {
72     QString language = get("language");
73
74     if(language.isEmpty())
75     {
76        language = QLocale::system().name();
77     }
78
79     QTranslator* translator = new QTranslator(&app);
80     translator->load(":/translations/" + language);
81     app.installTranslator(translator);
82 }
83
84 void Settings::close()
85 {
86     delete instance_;
87     instance_ = 0;
88 }
89
90 void Settings::startEdit()
91 {
92     if(!editing_ && !DB::connected())
93     {
94         editing_ = DB::connect();
95     }
96 }
97
98 void Settings::endEdit()
99 {
100     if(editing_)
101     {
102         DB::disconnect();
103         editing_ = false;
104     }
105 }
106
107 bool Settings::set(QString const& name, QString const& value)
108 {
109     bool close = !editing_;
110
111     startEdit();
112
113     QSqlQuery deleteQuery;
114     deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
115     deleteQuery.bindValue(":name", QVariant(name));
116     deleteQuery.exec();
117
118     QSqlQuery query;
119     query.prepare("INSERT INTO settings(name, value) VALUES(:name, :value)");
120     query.bindValue(":name", QVariant(name));
121     query.bindValue(":value", QVariant(value));
122
123     bool returnValue = query.exec();
124
125     if(close)
126     {
127         endEdit();
128     }
129
130     return returnValue;
131 }
132
133 QString Settings::get(QString const& name)
134 {
135     QString result = "";
136
137     bool close = !editing_;
138
139     startEdit();
140
141     QSqlQuery query;
142
143     query.prepare("SELECT value FROM settings WHERE name = :name");
144     query.bindValue(":name", name);
145
146     if(query.exec() && query.next())
147     {
148         result = query.value(0).toString();
149     }
150     else
151     {
152         result = getDefaultValue(name);
153     }
154
155     if(close)
156     {
157         endEdit();
158     }
159
160     return result;
161
162 }
163
164 QString Settings::getDefaultValue(QString const& name)
165 {
166     static bool defaultValuesLoaded = false;
167     static QMap <QString, QString> defaultValues;
168
169     if(!defaultValuesLoaded)
170     {
171         defaultValues["autostart"] = "1";
172         defaultValues["eniro_site"] = tr("fi");
173         defaultValues["cache_size"] = "200";
174         defaultValues["connection"] = "global";
175         defaultValuesLoaded = true;
176     }
177
178     QMap<QString, QString>::const_iterator it = defaultValues.find(name);
179
180     if(it != defaultValues.end())
181     {
182         return it.value();
183     }
184
185     return "";
186
187 }
188
189 bool Settings::reset()
190 {
191     bool close = !editing_;
192
193     startEdit();
194
195     QSqlQuery query;
196
197     bool ret = query.exec("DELETE FROM settings");
198
199     if(close)
200     {
201         endEdit();
202     }
203
204     return ret;
205 }
206
207 Settings::ConnectionType Settings::getConnectionType()
208 {
209     QString value = get("connection");
210
211     if(value == "any")
212     {
213         return ANY;
214     }
215     else if(value == "wlan")
216     {
217         return WLAN;
218     }
219     else if(value == "gprs")
220     {
221         return GPRS;
222     }
223     else
224     {
225         if(value != "global")
226         {
227             qDebug() << "Unknown connection type in settings, using default";
228         }
229
230         QList<QString> values;
231
232         GConfClient* gcClient = NULL;
233         gcClient = gconf_client_get_default();
234
235         g_assert(GCONF_IS_CLIENT(gcClient));
236
237         GError* error = NULL;
238         GSList* list = NULL;
239         list = gconf_client_get_list(gcClient,
240                                      "/system/osso/connectivity/network_type/auto_connect",
241                                      GCONF_VALUE_STRING,
242                                      &error);
243
244         if(error)
245         {
246             qDebug() << "Error: " << error->message;
247             g_error_free(error);
248         }
249         else
250         {
251             while(list)
252             {
253                 values.push_back((char *)list->data);
254                 list = list->next;
255             }
256         }
257
258         g_object_unref(gcClient);
259
260         if(values.size() == 0)
261         {
262             return ALWAYS_ASK;
263         }
264         else
265         {
266             QString value = values.at(0);
267
268             if(value == "*")
269             {
270                 return ANY;
271             }
272             else if(value == "GPRS")
273             {
274                 return GPRS;
275             }
276             else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
277             {
278                 return WLAN;
279             }
280             else
281             {
282                 qDebug() << "Unknown connection type: " << value;
283                 return ALWAYS_ASK;
284             }
285         }
286
287     }
288
289     return ALWAYS_ASK;
290 }
291
292 Settings::Settings(): editing_(false)
293 {
294 }
295
296 Settings::~Settings()
297 {
298     DB::removeDatabase();
299 }