Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Jenirok
Summary Tracker SCM Files
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  */
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"
28 namespace
29 {
30     static int const LANGUAGE_COUNT = 3;
32     static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = {
33        "English",
34        "Norsk",
35        "Suomi"
36     };
38     static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = {
39        "en_US",
40        "nb_NO",
41        "fi_FI"
42     };
43 }
45 Settings* Settings::instance_ = 0;
47 Settings* Settings::instance()
48 {
49     if(!instance_)
50     {
51         instance_ = new Settings;
52     }
54     return instance_;
55 }
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 }
68 void Settings::loadLanguage(QApplication& app)
69 {
70     QString language = get("language");
72     if(language.isEmpty())
73     {
74        language = QLocale::system().name();
75     }
77     QTranslator* translator = new QTranslator(&app);
78     translator->load(":/translations/" + language);
79     app.installTranslator(translator);
80 }
82 void Settings::close()
83 {
84     delete instance_;
85     instance_ = 0;
86 }
88 void Settings::startEdit()
89 {
90     if(!editing_ && !DB::connected())
91     {
92         editing_ = DB::connect();
93     }
94 }
96 void Settings::endEdit()
97 {
98     if(editing_)
99     {
100         DB::disconnect();
101         editing_ = false;
102     }
105 bool Settings::set(QString const& name, QString const& value)
107     bool close = !editing_;
109     startEdit();
111     QSqlQuery deleteQuery;
112     deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
113     deleteQuery.bindValue(":name", QVariant(name));
114     deleteQuery.exec();
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));
121     bool returnValue = query.exec();
123     if(close)
124     {
125         endEdit();
126     }
128     return returnValue;
131 QString Settings::get(QString const& name)
133     QString result = "";
135     bool close = !editing_;
137     startEdit();
139     QSqlQuery query;
141     query.prepare("SELECT value FROM settings WHERE name = :name");
142     query.bindValue(":name", name);
144     if(query.exec() && query.next())
145     {
146         result = query.value(0).toString();
147     }
148     else
149     {
150         result = getDefaultValue(name);
151     }
153     if(close)
154     {
155         endEdit();
156     }
158     return result;
162 QString Settings::getDefaultValue(QString const& name)
164     static bool defaultValuesLoaded = false;
165     static QMap <QString, QString> defaultValues;
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     }
176     QMap<QString, QString>::const_iterator it = defaultValues.find(name);
178     if(it != defaultValues.end())
179     {
180         return it.value();
181     }
183     return "";
187 bool Settings::reset()
189     bool close = !editing_;
191     startEdit();
193     QSqlQuery query;
195     bool ret = query.exec("DELETE FROM settings");
197     if(close)
198     {
199         endEdit();
200     }
202     return ret;
205 Settings::ConnectionType Settings::getConnectionType()
207     QString value = get("connection");
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         }
228         QList<QString> values;
230         GConfClient* gcClient = NULL;
231         gcClient = gconf_client_get_default();
233         g_assert(GCONF_IS_CLIENT(gcClient));
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);
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         }
256         g_object_unref(gcClient);
258         if(values.size() == 0)
259         {
260             return ALWAYS_ASK;
261         }
262         else
263         {
264             QString value = values.at(0);
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         }
285     }
287     return ALWAYS_ASK;
290 Settings::Settings(): editing_(false)
294 Settings::~Settings()
296     DB::removeDatabase();

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment