46ea56ebc1644ced273c067e75ddc18b41fe9bd7
[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 <QDebug>
22 #include <gconf/gconf-client.h>
23 #include "settings.h"
24 #include "db.h"
25
26 Settings* Settings::instance_ = 0;
27
28 Settings* Settings::instance()
29 {
30     if(!instance_)
31     {
32         instance_ = new Settings;
33     }
34
35     return instance_;
36 }
37
38 void Settings::close()
39 {
40     delete instance_;
41     instance_ = 0;
42 }
43
44 bool Settings::set(QString const& name, QString const& value)
45 {
46     bool connected = DB::connected();
47
48     if(!connected)
49     {
50         if(!DB::connect())
51         {
52             return false;
53         }
54     }
55
56     QSqlQuery deleteQuery;
57     deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
58     deleteQuery.bindValue(":name", QVariant(name));
59     deleteQuery.exec();
60
61     QSqlQuery query;
62     query.prepare("INSERT INTO settings(name, value) VALUES(:name, :value)");
63     query.bindValue(":name", QVariant(name));
64     query.bindValue(":value", QVariant(value));
65
66     bool returnValue = query.exec();
67
68     if(!connected)
69     {
70         DB::disconnect();
71     }
72
73     return returnValue;
74 }
75
76 QString Settings::get(QString const& name)
77 {
78     QString result = "";
79
80     bool connected = DB::connected();
81
82     if(!connected)
83     {
84         if(!DB::connect())
85         {
86             return result;
87         }
88     }
89
90     QSqlQuery query;
91
92     query.prepare("SELECT value FROM settings WHERE name = :name");
93     query.bindValue(":name", name);
94
95     if(query.exec() && query.next())
96     {
97         result = query.value(0).toString();
98     }
99     else
100     {
101         result = getDefaultValue(name);
102     }
103
104     if(!connected)
105     {
106         DB::disconnect();
107     }
108
109     return result;
110
111 }
112
113 QString Settings::getDefaultValue(QString const& name)
114 {
115     static bool defaultValuesLoaded = false;
116     static QMap <QString, QString> defaultValues;
117
118     if(!defaultValuesLoaded)
119     {
120         defaultValues["autostart"] = "1";
121         defaultValues["eniro_site"] = tr("fi");
122         defaultValues["cache_size"] = "200";
123         defaultValues["connection"] = "global";
124         defaultValuesLoaded = true;
125     }
126
127     QMap<QString, QString>::const_iterator it = defaultValues.find(name);
128
129     if(it != defaultValues.end())
130     {
131         return it.value();
132     }
133
134     return "";
135
136 }
137
138 bool Settings::reset()
139 {
140     bool connected = DB::connected();
141
142     if(!connected)
143     {
144         if(!DB::connect())
145         {
146             return false;
147         }
148     }
149
150     QSqlQuery query;
151
152     bool ret = query.exec("DELETE FROM settings");
153
154     if(!connected)
155     {
156         DB::disconnect();
157     }
158
159     return ret;
160 }
161
162 Settings::ConnectionType Settings::getConnectionType()
163 {
164     QString value = get("connection");
165
166     if(value == "any")
167     {
168         return ANY;
169     }
170     else if(value == "wlan")
171     {
172         return WLAN;
173     }
174     else if(value == "gprs")
175     {
176         return GPRS;
177     }
178     else
179     {
180         if(value != "global")
181         {
182             qDebug() << "Unknown connection type in settings, using default";
183         }
184
185         QList<QString> values;
186
187         GConfClient* gcClient = NULL;
188         gcClient = gconf_client_get_default();
189
190         g_assert(GCONF_IS_CLIENT(gcClient));
191
192         GError* error = NULL;
193         GSList* list = NULL;
194         list = gconf_client_get_list(gcClient,
195                                      "/system/osso/connectivity/network_type/auto_connect",
196                                      GCONF_VALUE_STRING,
197                                      &error);
198
199         if(error)
200         {
201             qDebug() << "Error: " << error->message;
202             g_error_free(error);
203         }
204         else
205         {
206             while(list)
207             {
208                 values.push_back((char *)list->data);
209                 list = list->next;
210             }
211
212         }
213
214         g_object_unref(gcClient);
215
216         if(values.size() == 0)
217         {
218             return ALWAYS_ASK;
219         }
220         else
221         {
222             QString value = values.at(0);
223
224             if(value == "*")
225             {
226                 return ANY;
227             }
228             else if(value == "GPRS")
229             {
230                 return GPRS;
231             }
232             else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
233             {
234                 return WLAN;
235             }
236             else
237             {
238                 qDebug() << "Unknown connection type: " << value;
239                 return ALWAYS_ASK;
240             }
241         }
242
243     }
244
245     return ALWAYS_ASK;
246 }
247
248 Settings::Settings()
249 {
250 }