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();
135 QString Settings::get(QString const& name)
139 bool close = !editing_;
145 query.prepare("SELECT value FROM settings WHERE name = :name");
146 query.bindValue(":name", name);
148 if(query.exec() && query.next())
150 result = query.value(0).toString();
154 result = getDefaultValue(name);
166 QString Settings::getDefaultValue(QString const& name)
168 static bool defaultValuesLoaded = false;
169 static QMap <QString, QString> defaultValues;
171 if(!defaultValuesLoaded)
173 defaultValues["autostart"] = "1";
174 defaultValues["eniro_site"] = tr("fi");
175 defaultValues["cache_size"] = "200";
176 defaultValues["connection"] = "global";
177 defaultValuesLoaded = true;
180 QMap<QString, QString>::const_iterator it = defaultValues.find(name);
182 if(it != defaultValues.end())
191 bool Settings::reset()
193 bool close = !editing_;
199 bool ret = query.exec("DELETE FROM settings");
209 Settings::ConnectionType Settings::getConnectionType()
211 QString value = get("connection");
217 else if(value == "wlan")
221 else if(value == "gprs")
227 if(value != "global")
229 qDebug() << "Unknown connection type in settings, using default";
232 QList<QString> values;
234 GConfClient* gcClient = NULL;
235 gcClient = gconf_client_get_default();
237 g_assert(GCONF_IS_CLIENT(gcClient));
239 GError* error = NULL;
241 list = gconf_client_get_list(gcClient,
242 "/system/osso/connectivity/network_type/auto_connect",
248 qDebug() << "Error: " << error->message;
255 values.push_back((char *)list->data);
260 g_object_unref(gcClient);
262 if(values.size() == 0)
268 QString value = values.at(0);
274 else if(value == "GPRS")
278 else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
284 qDebug() << "Unknown connection type: " << value;
294 Settings::Settings(): editing_(false)
298 Settings::~Settings()
300 DB::removeDatabase();