Added call log and German translation.
[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     return returnValue;
133 }
134
135 QString Settings::get(QString const& name)
136 {
137     QString result = "";
138
139     bool close = !editing_;
140
141     startEdit();
142
143     QSqlQuery query;
144
145     query.prepare("SELECT value FROM settings WHERE name = :name");
146     query.bindValue(":name", name);
147
148     if(query.exec() && query.next())
149     {
150         result = query.value(0).toString();
151     }
152     else
153     {
154         result = getDefaultValue(name);
155     }
156
157     if(close)
158     {
159         endEdit();
160     }
161
162     return result;
163
164 }
165
166 QString Settings::getDefaultValue(QString const& name)
167 {
168     static bool defaultValuesLoaded = false;
169     static QMap <QString, QString> defaultValues;
170
171     if(!defaultValuesLoaded)
172     {
173         defaultValues["autostart"] = "1";
174         defaultValues["eniro_site"] = tr("fi");
175         defaultValues["cache_size"] = "200";
176         defaultValues["connection"] = "global";
177         defaultValuesLoaded = true;
178     }
179
180     QMap<QString, QString>::const_iterator it = defaultValues.find(name);
181
182     if(it != defaultValues.end())
183     {
184         return it.value();
185     }
186
187     return "";
188
189 }
190
191 bool Settings::reset()
192 {
193     bool close = !editing_;
194
195     startEdit();
196
197     QSqlQuery query;
198
199     bool ret = query.exec("DELETE FROM settings");
200
201     if(close)
202     {
203         endEdit();
204     }
205
206     return ret;
207 }
208
209 Settings::ConnectionType Settings::getConnectionType()
210 {
211     QString value = get("connection");
212
213     if(value == "any")
214     {
215         return ANY;
216     }
217     else if(value == "wlan")
218     {
219         return WLAN;
220     }
221     else if(value == "gprs")
222     {
223         return GPRS;
224     }
225     else
226     {
227         if(value != "global")
228         {
229             qDebug() << "Unknown connection type in settings, using default";
230         }
231
232         QList<QString> values;
233
234         GConfClient* gcClient = NULL;
235         gcClient = gconf_client_get_default();
236
237         g_assert(GCONF_IS_CLIENT(gcClient));
238
239         GError* error = NULL;
240         GSList* list = NULL;
241         list = gconf_client_get_list(gcClient,
242                                      "/system/osso/connectivity/network_type/auto_connect",
243                                      GCONF_VALUE_STRING,
244                                      &error);
245
246         if(error)
247         {
248             qDebug() << "Error: " << error->message;
249             g_error_free(error);
250         }
251         else
252         {
253             while(list)
254             {
255                 values.push_back((char *)list->data);
256                 list = list->next;
257             }
258         }
259
260         g_object_unref(gcClient);
261
262         if(values.size() == 0)
263         {
264             return ALWAYS_ASK;
265         }
266         else
267         {
268             QString value = values.at(0);
269
270             if(value == "*")
271             {
272                 return ANY;
273             }
274             else if(value == "GPRS")
275             {
276                 return GPRS;
277             }
278             else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
279             {
280                 return WLAN;
281             }
282             else
283             {
284                 qDebug() << "Unknown connection type: " << value;
285                 return ALWAYS_ASK;
286             }
287         }
288
289     }
290
291     return ALWAYS_ASK;
292 }
293
294 Settings::Settings(): editing_(false)
295 {
296 }
297
298 Settings::~Settings()
299 {
300     DB::removeDatabase();
301 }