5f9a1651b1a3ba2d4262c0421182afa67d5b7ce2
[jenirok] / src / common / cache.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 <QtSql/QSqlError>
21 #include <QtCore/QVariant>
22 #include <QtCore/QDebug>
23 #include "cache.h"
24 #include "db.h"
25 #include "settings.h"
26
27 Cache* Cache::instance_ = 0;
28
29 Cache::Cache()
30 {
31 }
32
33 int Cache::clear()
34 {
35     bool connected = DB::connected();
36
37     if(!connected)
38     {
39         DB::connect();
40     }
41
42     QSqlQuery query;
43     int ret = -1;
44
45     if(query.exec("DELETE FROM cache"))
46     {
47         ret = query.numRowsAffected();
48     }
49
50     if(!connected)
51     {
52         DB::disconnect();
53     }
54
55     return ret;
56 }
57
58 bool Cache::findItem(QString const& number, Source::Result& result)
59 {
60     bool connected = DB::connected();
61
62     if(!connected)
63     {
64         DB::connect();
65     }
66
67     QSqlQuery query("SELECT name, street, city FROM cache WHERE number LIKE '%" + number.right(7) + "'");
68
69     //query.prepare("SELECT name, street, city FROM cache WHERE number = :number");
70     //query.bindValue(":number", number);
71
72     bool ret = false;
73
74     if(query.next())
75     {
76         result.number = number;
77         result.name = query.value(0).toString();
78         result.street = query.value(1).toString();
79         result.city = query.value(2).toString();
80
81         ret = true;
82     }
83
84     if(!connected)
85     {
86         DB::disconnect();
87     }
88
89     return ret;
90 }
91
92 bool Cache::addItem(Source::Result const& result)
93 {
94     bool connected = DB::connected();
95
96     if(!connected)
97     {
98         DB::connect();
99     }
100
101     bool ret = true;
102
103     QSqlQuery query;
104
105     query.prepare("INSERT INTO cache(number, name, street, city) VALUES(:number, :name, :street, :city)");
106     query.bindValue(":number", result.number);
107     query.bindValue(":name", result.name);
108     query.bindValue(":street", result.street);
109     query.bindValue(":city", result.city);
110
111     qDebug() << result.number << result.name << result.street << result.city;
112
113
114     if(!query.exec())
115     {
116         qDebug() << "Unable to add item to cache";
117         ret = false;
118     }
119
120     query.clear();
121
122     QString cacheSize = Settings::instance()->get("cache_size");
123
124     int cacheLimit = 0;
125
126     // Delete old entries from cache
127     if((cacheLimit = cacheSize.toInt()) > 0)
128     {
129         if(query.exec("SELECT COUNT(*) FROM cache") && query.next())
130         {
131             int itemsToDelete = query.value(0).toInt() - cacheLimit;
132
133             for(int i = 0; i < itemsToDelete; i++)
134             {
135                 query.clear();
136
137                 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
138                 {
139                     QSqlError error = query.lastError();
140                     qDebug() << "Unable to delete old cache entries: " << error.text();
141                     ret = false;
142                 }
143             }
144         }
145         else
146         {
147             QSqlError error = query.lastError();
148             qDebug() << "Unable to get count for cache entries: " << error.text();
149             ret = false;
150         }
151
152     }
153
154     if(!connected)
155     {
156         DB::disconnect();
157     }
158
159     return ret;
160
161 }
162
163 bool Cache::logItem(Source::Result const& result, bool missed, unsigned int time)
164 {
165     bool connected = DB::connected();
166
167     if(!connected)
168     {
169         DB::connect();
170     }
171
172     bool ret = true;
173
174     QSqlQuery query;
175
176     query.prepare("INSERT INTO log(number, name, street, city, country, time, missed) VALUES(:number, :name, :street, :city, :country, :time, :missed)");
177     query.bindValue(":number", result.number);
178     query.bindValue(":name", result.name);
179     query.bindValue(":street", result.street);
180     query.bindValue(":city", result.city);
181     query.bindValue(":country", result.country);
182     query.bindValue(":time", time);
183     int misVal = missed ? 1 : 0;
184
185     query.bindValue(":missed", misVal);
186
187     if(!query.exec())
188     {
189         ret = false;
190     }
191
192     query.clear();
193
194     // Delete old entries from cache
195     if(LOG_MAX_SIZE > 0)
196     {
197         if(query.exec("SELECT COUNT(*) FROM log") && query.next())
198         {
199             int itemsToDelete = query.value(0).toInt() - LOG_MAX_SIZE;
200
201             for(int i = 0; i < itemsToDelete; i++)
202             {
203                 query.clear();
204
205                 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
206                 {
207                     QSqlError error = query.lastError();
208                     qDebug() << "Unable to delete old cache entries: " << error.text();
209                     ret = false;
210                 }
211             }
212         }
213         else
214         {
215             QSqlError error = query.lastError();
216             qDebug() << "Unable to get count for cache entries: " << error.text();
217             ret = false;
218         }
219
220     }
221
222     if(!connected)
223     {
224         DB::disconnect();
225     }
226
227     return ret;
228
229 }
230
231 void Cache::getLogItems(QList<Cache::LogDetails>& items, int limit)
232 {
233     bool connected = DB::connected();
234
235     if(!connected)
236     {
237         DB::connect();
238     }
239
240     QSqlQuery query("SELECT number, name, street, city, country, time, missed FROM log ORDER BY time DESC LIMIT " + QString::number(limit));
241
242     while(query.next())
243     {
244         LogDetails details;
245         details.result.number = query.value(0).toString();
246         details.result.name = query.value(1).toString();
247         details.result.street = query.value(2).toString();
248         details.result.city = query.value(3).toString();
249         details.result.country = query.value(4).toString();
250         details.time = query.value(5).toInt();
251
252         int missed = query.value(6).toInt();
253
254         details.missed = missed ? true : false;
255
256         items.push_back(details);
257     }
258
259     if(!connected)
260     {
261         DB::disconnect();
262     }
263
264 }
265
266 int Cache::clearLog()
267 {
268     bool connected = DB::connected();
269
270     if(!connected)
271     {
272         DB::connect();
273     }
274
275     QSqlQuery query;
276     int ret = -1;
277
278     if(query.exec("DELETE FROM log"))
279     {
280         ret = query.numRowsAffected();
281     }
282
283     if(!connected)
284     {
285         DB::disconnect();
286     }
287
288     return ret;
289 }
290
291 Cache& Cache::instance()
292 {
293     if(!instance_)
294     {
295         instance_ = new Cache;
296     }
297
298     return *instance_;
299 }