Added option to skip the leading zero of an incoming call.
[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         defaultValues["ignore_first_zero"] = "1";
189         defaultValuesLoaded = true;
190     }
191
192     QMap<QString, QString>::const_iterator it = defaultValues.find(name);
193
194     if(it != defaultValues.end())
195     {
196         return it.value();
197     }
198
199     return "";
200
201 }
202
203 bool Settings::reset()
204 {
205     bool close = !editing_;
206
207     startEdit();
208
209     QSqlQuery query;
210
211     bool ret = query.exec("DELETE FROM settings");
212
213     if(close)
214     {
215         endEdit();
216     }
217
218     return ret;
219 }
220
221 Settings::ConnectionType Settings::getConnectionType()
222 {
223     QString value = get("connection");
224
225     if(value == "any")
226     {
227         return ANY;
228     }
229     else if(value == "wlan")
230     {
231         return WLAN;
232     }
233     else if(value == "gprs")
234     {
235         return GPRS;
236     }
237     else
238     {
239         if(value != "global")
240         {
241             qDebug() << "Unknown connection type in settings, using default";
242         }
243
244         QList<QString> values;
245
246         GConfClient* gcClient = NULL;
247         gcClient = gconf_client_get_default();
248
249         g_assert(GCONF_IS_CLIENT(gcClient));
250
251         GError* error = NULL;
252         GSList* list = NULL;
253         list = gconf_client_get_list(gcClient,
254                                      "/system/osso/connectivity/network_type/auto_connect",
255                                      GCONF_VALUE_STRING,
256                                      &error);
257
258         if(error)
259         {
260             qDebug() << "Error: " << error->message;
261             g_error_free(error);
262         }
263         else
264         {
265             while(list)
266             {
267                 values.push_back((char *)list->data);
268                 list = list->next;
269             }
270         }
271
272         g_object_unref(gcClient);
273
274         if(values.size() == 0)
275         {
276             return ALWAYS_ASK;
277         }
278         else
279         {
280             QString value = values.at(0);
281
282             if(value == "*")
283             {
284                 return ANY;
285             }
286             else if(value == "GPRS")
287             {
288                 return GPRS;
289             }
290             else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
291             {
292                 return WLAN;
293             }
294             else
295             {
296                 qDebug() << "Unknown connection type: " << value;
297                 return ALWAYS_ASK;
298             }
299         }
300
301     }
302
303     return ALWAYS_ASK;
304 }
305
306 Settings::Settings(): editing_(false)
307 {
308 }
309
310 Settings::~Settings()
311 {
312     DB::removeDatabase();
313 }