2 * This file is part of Jenirok.
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.
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.
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/>.
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>
30 static int const LANGUAGE_COUNT = 2;
32 static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = {
37 static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = {
43 Settings* Settings::instance_ = 0;
45 Settings* Settings::instance()
49 instance_ = new Settings;
55 void Settings::getLanguages(QList<Settings::Language>& languages)
57 for(int i = 0; i < LANGUAGE_COUNT; i++)
60 lang.name = LANGUAGE_NAMES[i];
61 lang.id = LANGUAGE_IDS[i];
62 languages.push_back(lang);
66 void Settings::loadLanguage(QApplication& app)
68 QString language = get("language");
70 if(language.isEmpty())
72 language = QLocale::system().name();
75 QTranslator* translator = new QTranslator(&app);
76 translator->load(":/translations/" + language);
77 app.installTranslator(translator);
80 void Settings::close()
86 void Settings::startEdit()
88 if(!editing_ && !DB::connected())
90 editing_ = DB::connect();
94 void Settings::endEdit()
103 bool Settings::set(QString const& name, QString const& value)
105 bool close = !editing_;
109 QSqlQuery deleteQuery;
110 deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
111 deleteQuery.bindValue(":name", QVariant(name));
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();
129 QString Settings::get(QString const& name)
133 bool close = !editing_;
139 query.prepare("SELECT value FROM settings WHERE name = :name");
140 query.bindValue(":name", name);
142 if(query.exec() && query.next())
144 result = query.value(0).toString();
148 result = getDefaultValue(name);
160 QString Settings::getDefaultValue(QString const& name)
162 static bool defaultValuesLoaded = false;
163 static QMap <QString, QString> defaultValues;
165 if(!defaultValuesLoaded)
167 defaultValues["autostart"] = "1";
168 defaultValues["eniro_site"] = tr("fi");
169 defaultValues["cache_size"] = "200";
170 defaultValues["connection"] = "global";
171 defaultValuesLoaded = true;
174 QMap<QString, QString>::const_iterator it = defaultValues.find(name);
176 if(it != defaultValues.end())
185 bool Settings::reset()
187 bool close = !editing_;
193 bool ret = query.exec("DELETE FROM settings");
203 Settings::ConnectionType Settings::getConnectionType()
205 QString value = get("connection");
211 else if(value == "wlan")
215 else if(value == "gprs")
221 if(value != "global")
223 qDebug() << "Unknown connection type in settings, using default";
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;
235 list = gconf_client_get_list(gcClient,
236 "/system/osso/connectivity/network_type/auto_connect",
242 qDebug() << "Error: " << error->message;
249 values.push_back((char *)list->data);
254 g_object_unref(gcClient);
256 if(values.size() == 0)
262 QString value = values.at(0);
268 else if(value == "GPRS")
272 else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
278 qDebug() << "Unknown connection type: " << value;
288 Settings::Settings(): editing_(false)
292 Settings::~Settings()
294 DB::removeDatabase();