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 = 3;
32 static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = {
38 static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = {
45 Settings* Settings::instance_ = 0;
47 Settings* Settings::instance()
51 instance_ = new Settings;
57 void Settings::getLanguages(QList<Settings::Language>& languages)
59 for(int i = 0; i < LANGUAGE_COUNT; i++)
62 lang.name = LANGUAGE_NAMES[i];
63 lang.id = LANGUAGE_IDS[i];
64 languages.push_back(lang);
68 void Settings::loadLanguage(QApplication& app)
70 QString language = get("language");
72 if(language.isEmpty())
74 language = QLocale::system().name();
77 QTranslator* translator = new QTranslator(&app);
78 translator->load(":/translations/" + language);
79 app.installTranslator(translator);
82 void Settings::close()
88 void Settings::startEdit()
90 if(!editing_ && !DB::connected())
92 editing_ = DB::connect();
96 void Settings::endEdit()
105 bool Settings::set(QString const& name, QString const& value)
107 bool close = !editing_;
111 QSqlQuery deleteQuery;
112 deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
113 deleteQuery.bindValue(":name", QVariant(name));
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();
131 QString Settings::get(QString const& name)
135 bool close = !editing_;
141 query.prepare("SELECT value FROM settings WHERE name = :name");
142 query.bindValue(":name", name);
144 if(query.exec() && query.next())
146 result = query.value(0).toString();
150 result = getDefaultValue(name);
162 QString Settings::getDefaultValue(QString const& name)
164 static bool defaultValuesLoaded = false;
165 static QMap <QString, QString> defaultValues;
167 if(!defaultValuesLoaded)
169 defaultValues["autostart"] = "1";
170 defaultValues["eniro_site"] = tr("fi");
171 defaultValues["cache_size"] = "200";
172 defaultValues["connection"] = "global";
173 defaultValuesLoaded = true;
176 QMap<QString, QString>::const_iterator it = defaultValues.find(name);
178 if(it != defaultValues.end())
187 bool Settings::reset()
189 bool close = !editing_;
195 bool ret = query.exec("DELETE FROM settings");
205 Settings::ConnectionType Settings::getConnectionType()
207 QString value = get("connection");
213 else if(value == "wlan")
217 else if(value == "gprs")
223 if(value != "global")
225 qDebug() << "Unknown connection type in settings, using default";
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;
237 list = gconf_client_get_list(gcClient,
238 "/system/osso/connectivity/network_type/auto_connect",
244 qDebug() << "Error: " << error->message;
251 values.push_back((char *)list->data);
256 g_object_unref(gcClient);
258 if(values.size() == 0)
264 QString value = values.at(0);
270 else if(value == "GPRS")
274 else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
280 qDebug() << "Unknown connection type: " << value;
290 Settings::Settings(): editing_(false)
294 Settings::~Settings()
296 DB::removeDatabase();