Daemon's memory usage decreased of about 6 MB.
[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 void Settings::close()
37 {
38     delete instance_;
39     instance_ = 0;
40 }
41
42 bool Settings::set(QString const& name, QString const& value)
43 {
44     bool connected = DB::connected();
45
46     if(!connected)
47     {
48         if(!DB::connect())
49         {
50             return false;
51         }
52     }
53
54     QSqlQuery deleteQuery;
55     deleteQuery.prepare("DELETE FROM settings WHERE name = :name");
56     deleteQuery.bindValue(":name", QVariant(name));
57     deleteQuery.exec();
58
59     QSqlQuery query;
60     query.prepare("INSERT INTO settings(name, value) VALUES(:name, :value)");
61     query.bindValue(":name", QVariant(name));
62     query.bindValue(":value", QVariant(value));
63
64     bool returnValue = query.exec();
65
66     if(!connected)
67     {
68         DB::disconnect();
69     }
70
71     return returnValue;
72 }
73
74 QString Settings::get(QString const& name)
75 {
76     QString result = "";
77
78     bool connected = DB::connected();
79
80     if(!connected)
81     {
82         if(!DB::connect())
83         {
84             return result;
85         }
86     }
87
88     QSqlQuery query;
89
90     query.prepare("SELECT value FROM settings WHERE name = :name");
91     query.bindValue(":name", name);
92
93     if(query.exec() && query.next())
94     {
95         result = query.value(0).toString();
96     }
97     else
98     {
99         result = getDefaultValue(name);
100     }
101
102     if(!connected)
103     {
104         DB::disconnect();
105     }
106
107     return result;
108
109 }
110
111 QString Settings::getDefaultValue(QString const& name)
112 {
113     static bool defaultValuesLoaded = false;
114     static QMap <QString, QString> defaultValues;
115
116     if(!defaultValuesLoaded)
117     {
118         defaultValues["autostart"] = "1";
119         defaultValues["eniro_site"] = tr("fi");
120         defaultValues["cache_size"] = "200";
121         defaultValuesLoaded = true;
122     }
123
124     QMap<QString, QString>::const_iterator it = defaultValues.find(name);
125
126     if(it != defaultValues.end())
127     {
128         return it.value();
129     }
130
131     return "";
132
133 }
134
135 bool Settings::reset()
136 {
137     bool connected = DB::connected();
138
139     if(!connected)
140     {
141         if(!DB::connect())
142         {
143             return false;
144         }
145     }
146
147     QSqlQuery query;
148
149     bool ret = query.exec("DELETE FROM settings");
150
151     if(!connected)
152     {
153         DB::disconnect();
154     }
155
156     return ret;
157 }
158
159 Settings::Settings()
160 {
161 }