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