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 = 4;
32 static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = {
39 static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = {
47 Settings* Settings::instance_ = 0;
49 Settings* Settings::instance()
53 instance_ = new Settings;
59 void Settings::getLanguages(QList<Settings::Language>& languages)
61 for(int i = 0; i < LANGUAGE_COUNT; i++)
64 lang.name = LANGUAGE_NAMES[i];
65 lang.id = LANGUAGE_IDS[i];
66 languages.push_back(lang);
70 void Settings::loadLanguage(QApplication& app)
72 QString language = get("language");
74 if(language.isEmpty())
76 language = QLocale::system().name();
79 QTranslator* translator = new QTranslator(&app);
80 translator->load(":/translations/" + language);
81 app.installTranslator(translator);
84 void Settings::close()
90 void Settings::startEdit()
92 if(!editing_ && !DB::connected())
94 editing_ = DB::connect();
98 void Settings::endEdit()
107 bool Settings::set(QString const& name, QString const& value)
109 bool close = !editing_;
113 QSqlQuery deleteQuery;
114 deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
115 deleteQuery.bindValue(":name", QVariant(name));
119 query.prepare("INSERT INTO settings(name, value) VALUES(:name, :value)");
120 query.bindValue(":name", QVariant(name));
121 query.bindValue(":value", QVariant(value));
123 bool returnValue = query.exec();
133 QString Settings::get(QString const& name)
137 bool close = !editing_;
143 query.prepare("SELECT value FROM settings WHERE name = :name");
144 query.bindValue(":name", name);
146 if(query.exec() && query.next())
148 result = query.value(0).toString();
152 result = getDefaultValue(name);
164 QString Settings::getDefaultValue(QString const& name)
166 static bool defaultValuesLoaded = false;
167 static QMap <QString, QString> defaultValues;
169 if(!defaultValuesLoaded)
171 defaultValues["autostart"] = "1";
172 defaultValues["eniro_site"] = tr("fi");
173 defaultValues["cache_size"] = "200";
174 defaultValues["connection"] = "global";
175 defaultValuesLoaded = true;
178 QMap<QString, QString>::const_iterator it = defaultValues.find(name);
180 if(it != defaultValues.end())
189 bool Settings::reset()
191 bool close = !editing_;
197 bool ret = query.exec("DELETE FROM settings");
207 Settings::ConnectionType Settings::getConnectionType()
209 QString value = get("connection");
215 else if(value == "wlan")
219 else if(value == "gprs")
225 if(value != "global")
227 qDebug() << "Unknown connection type in settings, using default";
230 QList<QString> values;
232 GConfClient* gcClient = NULL;
233 gcClient = gconf_client_get_default();
235 g_assert(GCONF_IS_CLIENT(gcClient));
237 GError* error = NULL;
239 list = gconf_client_get_list(gcClient,
240 "/system/osso/connectivity/network_type/auto_connect",
246 qDebug() << "Error: " << error->message;
253 values.push_back((char *)list->data);
258 g_object_unref(gcClient);
260 if(values.size() == 0)
266 QString value = values.at(0);
272 else if(value == "GPRS")
276 else if(value == "WLAN_INFRA" || value == "WLAN_ADHOC" || value == "WLAN")
282 qDebug() << "Unknown connection type: " << value;
292 Settings::Settings(): editing_(false)
296 Settings::~Settings()
298 DB::removeDatabase();