Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Jenirok
Summary Tracker SCM Files
89c10cd68dd1afe8fb861ded165a168b944c63fb
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 = 2;
32     static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = {
33        "English",
34        "Suomi"
35     };
37     static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = {
38        "en_US",
39        "fi_FI"
40     };
41 }
43 Settings* Settings::instance_ = 0;
45 Settings* Settings::instance()
46 {
47     if(!instance_)
48     {
49         instance_ = new Settings;
50     }
52     return instance_;
53 }
55 void Settings::getLanguages(QList<Settings::Language>& languages)
56 {
57     for(int i = 0; i < LANGUAGE_COUNT; i++)
58     {
59         Language lang;
60         lang.name = LANGUAGE_NAMES[i];
61         lang.id = LANGUAGE_IDS[i];
62         languages.push_back(lang);
63     }
64 }
66 void Settings::loadLanguage(QApplication& app)
67 {
68     QString language = get("language");
70     if(language.isEmpty())
71     {
72        language = QLocale::system().name();
73     }
75     QTranslator* translator = new QTranslator(&app);
76     translator->load(":/translations/" + language);
77     app.installTranslator(translator);
78 }
80 void Settings::close()
81 {
82     delete instance_;
83     instance_ = 0;
84 }
86 void Settings::startEdit()
87 {
88     if(!editing_ && !DB::connected())
89     {
90         editing_ = DB::connect();
91     }
92 }
94 void Settings::endEdit()
95 {
96     if(editing_)
97     {
98         DB::disconnect();
99         editing_ = false;
100     }
103 bool Settings::set(QString const& name, QString const& value)
105     bool close = !editing_;
107     startEdit();
109     QSqlQuery deleteQuery;
110     deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
111     deleteQuery.bindValue(":name", QVariant(name));
112     deleteQuery.exec();
114     QSqlQuery query;
115     query.prepare("INSERT INTO settings(name, value) VALUES(:name, :value)");
116     query.bindValue(":name", QVariant(name));
117     query.bindValue(":value", QVariant(value));
119     bool returnValue = query.exec();
121     if(close)
122     {
123         endEdit();
124     }
126     return returnValue;
129 QString Settings::get(QString const& name)
131     QString result = "";
133     bool close = !editing_;
135     startEdit();
137     QSqlQuery query;
139     query.prepare("SELECT value FROM settings WHERE name = :name");
140     query.bindValue(":name", name);
142     if(query.exec() && query.next())
143     {
144         result = query.value(0).toString();
145     }
146     else
147     {
148         result = getDefaultValue(name);
149     }
151     if(close)
152     {
153         endEdit();
154     }
156     return result;
160 QString Settings::getDefaultValue(QString const& name)
162     static bool defaultValuesLoaded = false;
163     static QMap <QString, QString> defaultValues;
165     if(!defaultValuesLoaded)
166     {
167         defaultValues["autostart"] = "1";
168         defaultValues["eniro_site"] = tr("fi");
169         defaultValues["cache_size"] = "200";
170         defaultValues["connection"] = "global";
171         defaultValuesLoaded = true;
172     }
174     QMap<QString, QString>::const_iterator it = defaultValues.find(name);
176     if(it != defaultValues.end())
177     {
178         return it.value();
179     }
181     return "";
185 bool Settings::reset()
187     bool close = !editing_;
189     startEdit();
191     QSqlQuery query;
193     bool ret = query.exec("DELETE FROM settings");
195     if(close)
196     {
197         endEdit();
198     }
200     return ret;
203 Settings::ConnectionType Settings::getConnectionType()
205     QString value = get("connection");
207     if(value == "any")
208     {
209         return ANY;
210     }
211     else if(value == "wlan")
212     {
213         return WLAN;
214     }
215     else if(value == "gprs")
216     {
217         return GPRS;
218     }
219     else
220     {
221         if(value != "global")
222         {
223             qDebug() << "Unknown connection type in settings, using default";
224         }
226         QList<QString> values;
228         GConfClient* gcClient = NULL;
229         gcClient = gconf_client_get_default();
231         g_assert(GCONF_IS_CLIENT(gcClient));
233         GError* error = NULL;
234         GSList* list = NULL;
235         list = gconf_client_get_list(gcClient,
236                                      "/system/osso/connectivity/network_type/auto_connect",
237                                      GCONF_VALUE_STRING,
238                                      &error);
240         if(error)
241         {
242             qDebug() << "Error: " << error->message;
243             g_error_free(error);
244         }
245         else
246         {
247             while(list)
248             {
249                 values.push_back((char *)list->data);
250                 list = list->next;
251             }
252         }
254         g_object_unref(gcClient);
256         if(values.size() == 0)
257         {
258             return ALWAYS_ASK;
259         }
260         else
261         {
262             QString value = values.at(0);
264             if(value == "*")
265             {
266                 return ANY;
267             }
268             else if(value == "GPRS")
269             {
270                 return GPRS;
271             }
272             else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
273             {
274                 return WLAN;
275             }
276             else
277             {
278                 qDebug() << "Unknown connection type: " << value;
279                 return ALWAYS_ASK;
280             }
281         }
283     }
285     return ALWAYS_ASK;
288 Settings::Settings(): editing_(false)
292 Settings::~Settings()
294     DB::removeDatabase();

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment