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