First commit
[jenirok] / src / common / settings.cpp
1 /*
2  * This file is part of Jenirok.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  */
18
19 #include <QtSql/QSqlQuery>
20 #include <QtCore/QVariant>
21 #include "settings.h"
22 #include "db.h"
23
24 Settings* Settings::instance_ = 0;
25
26 Settings* Settings::instance()
27 {
28         if(!instance_)
29         {
30                 instance_ = new Settings;
31         }
32
33         return instance_;
34 }
35
36 bool Settings::set(QString const& name, QString const& value)
37 {
38         bool connected = DB::connected();
39
40         if(!connected)
41         {
42                 if(!DB::connect())
43                 {
44                         return false;
45                 }
46         }
47
48         QSqlQuery deleteQuery;
49         deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
50         deleteQuery.bindValue(":name", QVariant(name));
51         deleteQuery.exec();
52
53         QSqlQuery query;
54         query.prepare("INSERT INTO settings(name, value) VALUES(:name, :value)");
55         query.bindValue(":name", QVariant(name));
56         query.bindValue(":value", QVariant(value));
57
58         bool returnValue = query.exec();
59
60         if(!connected)
61         {
62                 DB::disconnect();
63         }
64
65         return returnValue;
66 }
67
68 QString Settings::get(QString const& name)
69 {
70         QString result = "";
71
72         bool connected = DB::connected();
73
74         if(!connected)
75         {
76                 if(!DB::connect())
77                 {
78                         return result;
79                 }
80         }
81
82         QSqlQuery query;
83
84         query.prepare("SELECT value FROM settings WHERE name = :name");
85         query.bindValue(":name", name);
86
87         if(query.exec() && query.next())
88         {
89                 result = query.value(0).toString();
90         }
91         else
92         {
93                 result = getDefaultValue(name);
94         }
95
96         if(!connected)
97         {
98                 DB::disconnect();
99         }
100
101         return result;
102
103 }
104
105 QString Settings::getDefaultValue(QString const& name)
106 {
107         static bool defaultValuesLoaded = false;
108         static QMap <QString, QString> defaultValues;
109
110         if(!defaultValuesLoaded)
111         {
112                 defaultValues["autostart"] = "1";
113                 defaultValues["eniro_site"] = tr("fi");
114                 defaultValues["cache_size"] = "200";
115                 defaultValuesLoaded = true;
116         }
117
118         QMap<QString, QString>::const_iterator it = defaultValues.find(name);
119
120         if(it != defaultValues.end())
121         {
122                 return it.value();
123         }
124
125         return "";
126
127 }
128
129 bool Settings::reset()
130 {
131         bool connected = DB::connected();
132
133         if(!connected)
134         {
135                 if(!DB::connect())
136                 {
137                         return false;
138                 }
139         }
140
141         QSqlQuery query;
142
143         bool ret = query.exec("DELETE FROM settings");
144
145         if(!connected)
146         {
147                 DB::disconnect();
148         }
149
150         return ret;
151 }
152
153 Settings::Settings()
154 {
155 }