89c10cd68dd1afe8fb861ded165a168b944c63fb
[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 = 2;
31
32     static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = {
33        "English",
34        "Suomi"
35     };
36
37     static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = {
38        "en_US",
39        "fi_FI"
40     };
41 }
42
43 Settings* Settings::instance_ = 0;
44
45 Settings* Settings::instance()
46 {
47     if(!instance_)
48     {
49         instance_ = new Settings;
50     }
51
52     return instance_;
53 }
54
55 void Settings::getLanguages(QList<Settings::Language>& languages)
56 {
57     for(int i = 0; i < LANGUAGE_COUNT; i++)
58     {
59         Language lang;
60         lang.name = LANGUAGE_NAMES[i];
61         lang.id = LANGUAGE_IDS[i];
62         languages.push_back(lang);
63     }
64 }
65
66 void Settings::loadLanguage(QApplication& app)
67 {
68     QString language = get("language");
69
70     if(language.isEmpty())
71     {
72        language = QLocale::system().name();
73     }
74
75     QTranslator* translator = new QTranslator(&app);
76     translator->load(":/translations/" + language);
77     app.installTranslator(translator);
78 }
79
80 void Settings::close()
81 {
82     delete instance_;
83     instance_ = 0;
84 }
85
86 void Settings::startEdit()
87 {
88     if(!editing_ && !DB::connected())
89     {
90         editing_ = DB::connect();
91     }
92 }
93
94 void Settings::endEdit()
95 {
96     if(editing_)
97     {
98         DB::disconnect();
99         editing_ = false;
100     }
101 }
102
103 bool Settings::set(QString const& name, QString const& value)
104 {
105     bool close = !editing_;
106
107     startEdit();
108
109     QSqlQuery deleteQuery;
110     deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
111     deleteQuery.bindValue(":name", QVariant(name));
112     deleteQuery.exec();
113
114     QSqlQuery query;
115     query.prepare("INSERT INTO settings(name, value) VALUES(:name, :value)");
116     query.bindValue(":name", QVariant(name));
117     query.bindValue(":value", QVariant(value));
118
119     bool returnValue = query.exec();
120
121     if(close)
122     {
123         endEdit();
124     }
125
126     return returnValue;
127 }
128
129 QString Settings::get(QString const& name)
130 {
131     QString result = "";
132
133     bool close = !editing_;
134
135     startEdit();
136
137     QSqlQuery query;
138
139     query.prepare("SELECT value FROM settings WHERE name = :name");
140     query.bindValue(":name", name);
141
142     if(query.exec() && query.next())
143     {
144         result = query.value(0).toString();
145     }
146     else
147     {
148         result = getDefaultValue(name);
149     }
150
151     if(close)
152     {
153         endEdit();
154     }
155
156     return result;
157
158 }
159
160 QString Settings::getDefaultValue(QString const& name)
161 {
162     static bool defaultValuesLoaded = false;
163     static QMap <QString, QString> defaultValues;
164
165     if(!defaultValuesLoaded)
166     {
167         defaultValues["autostart"] = "1";
168         defaultValues["eniro_site"] = tr("fi");
169         defaultValues["cache_size"] = "200";
170         defaultValues["connection"] = "global";
171         defaultValuesLoaded = true;
172     }
173
174     QMap<QString, QString>::const_iterator it = defaultValues.find(name);
175
176     if(it != defaultValues.end())
177     {
178         return it.value();
179     }
180
181     return "";
182
183 }
184
185 bool Settings::reset()
186 {
187     bool close = !editing_;
188
189     startEdit();
190
191     QSqlQuery query;
192
193     bool ret = query.exec("DELETE FROM settings");
194
195     if(close)
196     {
197         endEdit();
198     }
199
200     return ret;
201 }
202
203 Settings::ConnectionType Settings::getConnectionType()
204 {
205     QString value = get("connection");
206
207     if(value == "any")
208     {
209         return ANY;
210     }
211     else if(value == "wlan")
212     {
213         return WLAN;
214     }
215     else if(value == "gprs")
216     {
217         return GPRS;
218     }
219     else
220     {
221         if(value != "global")
222         {
223             qDebug() << "Unknown connection type in settings, using default";
224         }
225
226         QList<QString> values;
227
228         GConfClient* gcClient = NULL;
229         gcClient = gconf_client_get_default();
230
231         g_assert(GCONF_IS_CLIENT(gcClient));
232
233         GError* error = NULL;
234         GSList* list = NULL;
235         list = gconf_client_get_list(gcClient,
236                                      "/system/osso/connectivity/network_type/auto_connect",
237                                      GCONF_VALUE_STRING,
238                                      &error);
239
240         if(error)
241         {
242             qDebug() << "Error: " << error->message;
243             g_error_free(error);
244         }
245         else
246         {
247             while(list)
248             {
249                 values.push_back((char *)list->data);
250                 list = list->next;
251             }
252         }
253
254         g_object_unref(gcClient);
255
256         if(values.size() == 0)
257         {
258             return ALWAYS_ASK;
259         }
260         else
261         {
262             QString value = values.at(0);
263
264             if(value == "*")
265             {
266                 return ANY;
267             }
268             else if(value == "GPRS")
269             {
270                 return GPRS;
271             }
272             else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
273             {
274                 return WLAN;
275             }
276             else
277             {
278                 qDebug() << "Unknown connection type: " << value;
279                 return ALWAYS_ASK;
280             }
281         }
282
283     }
284
285     return ALWAYS_ASK;
286 }
287
288 Settings::Settings(): editing_(false)
289 {
290 }
291
292 Settings::~Settings()
293 {
294     DB::removeDatabase();
295 }