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 = 5;
32 static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = {
40 static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = {
49 Settings* Settings::instance_ = 0;
51 Settings* Settings::instance()
55 instance_ = new Settings;
61 void Settings::getLanguages(QList<Settings::Language>& languages)
63 for(int i = 0; i < LANGUAGE_COUNT; i++)
66 lang.name = LANGUAGE_NAMES[i];
67 lang.id = LANGUAGE_IDS[i];
68 languages.push_back(lang);
72 void Settings::loadLanguage(QApplication& app)
74 QString language = get("language");
76 if(language.isEmpty())
78 language = QLocale::system().name();
81 QTranslator* translator = new QTranslator(&app);
82 translator->load(":/translations/" + language);
83 app.installTranslator(translator);
86 void Settings::close()
92 void Settings::startEdit()
94 if(!editing_ && !DB::connected())
96 editing_ = DB::connect();
100 void Settings::endEdit()
109 bool Settings::set(QString const& name, QString const& value)
111 bool close = !editing_;
115 QSqlQuery deleteQuery;
116 deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
117 deleteQuery.bindValue(":name", QVariant(name));
121 query.prepare("INSERT INTO settings(name, value) VALUES(:name, :value)");
122 query.bindValue(":name", QVariant(name));
123 query.bindValue(":value", QVariant(value));
125 bool returnValue = query.exec();
132 settings_[name] = value;
137 QString Settings::get(QString const& name)
139 QMap<QString, QString>::iterator it;
141 if((it = settings_.find(name)) != settings_.end())
148 bool close = !editing_;
154 query.prepare("SELECT value FROM settings WHERE name = :name");
155 query.bindValue(":name", name);
157 if(query.exec() && query.next())
159 result = query.value(0).toString();
163 result = getDefaultValue(name);
171 settings_[name] = result;
177 QString Settings::getDefaultValue(QString const& name)
179 static bool defaultValuesLoaded = false;
180 static QMap <QString, QString> defaultValues;
182 if(!defaultValuesLoaded)
184 defaultValues["autostart"] = "1";
185 defaultValues["eniro_site"] = tr("fi");
186 defaultValues["cache_size"] = "200";
187 defaultValues["connection"] = "global";
188 defaultValuesLoaded = true;
191 QMap<QString, QString>::const_iterator it = defaultValues.find(name);
193 if(it != defaultValues.end())
202 bool Settings::reset()
204 bool close = !editing_;
210 bool ret = query.exec("DELETE FROM settings");
220 Settings::ConnectionType Settings::getConnectionType()
222 QString value = get("connection");
228 else if(value == "wlan")
232 else if(value == "gprs")
238 if(value != "global")
240 qDebug() << "Unknown connection type in settings, using default";
243 QList<QString> values;
245 GConfClient* gcClient = NULL;
246 gcClient = gconf_client_get_default();
248 g_assert(GCONF_IS_CLIENT(gcClient));
250 GError* error = NULL;
252 list = gconf_client_get_list(gcClient,
253 "/system/osso/connectivity/network_type/auto_connect",
259 qDebug() << "Error: " << error->message;
266 values.push_back((char *)list->data);
271 g_object_unref(gcClient);
273 if(values.size() == 0)
279 QString value = values.at(0);
285 else if(value == "GPRS")
289 else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
295 qDebug() << "Unknown connection type: " << value;
305 Settings::Settings(): editing_(false)
309 Settings::~Settings()
311 DB::removeDatabase();