Added some sort of Ovi Maps integration. All translations updated.
[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
112     if(!query.exec())
113     {
114         ret = false;
115     }
116
117     query.clear();
118
119     QString cacheSize = Settings::instance()->get("cache_size");
120
121     int cacheLimit = 0;
122
123     // Delete old entries from cache
124     if((cacheLimit = cacheSize.toInt()) > 0)
125     {
126         if(query.exec("SELECT COUNT(*) FROM cache") && query.next())
127         {
128             int itemsToDelete = query.value(0).toInt() - cacheLimit;
129
130             for(int i = 0; i < itemsToDelete; i++)
131             {
132                 query.clear();
133
134                 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
135                 {
136                     QSqlError error = query.lastError();
137                     qDebug() << "Unable to delete old cache entries: " << error.text();
138                     ret = false;
139                 }
140             }
141         }
142         else
143         {
144             QSqlError error = query.lastError();
145             qDebug() << "Unable to get count for cache entries: " << error.text();
146             ret = false;
147         }
148
149     }
150
151     if(!connected)
152     {
153         DB::disconnect();
154     }
155
156     return ret;
157
158 }
159
160 bool Cache::logItem(Source::Result const& result, bool missed, unsigned int time)
161 {
162     bool connected = DB::connected();
163
164     if(!connected)
165     {
166         DB::connect();
167     }
168
169     bool ret = true;
170
171     QSqlQuery query;
172
173     query.prepare("INSERT INTO log(number, name, street, city, country, time, missed) VALUES(:number, :name, :street, :city, :country, :time, :missed)");
174     query.bindValue(":number", result.number);
175     query.bindValue(":name", result.name);
176     query.bindValue(":street", result.street);
177     query.bindValue(":city", result.city);
178     query.bindValue(":country", result.country);
179     query.bindValue(":time", time);
180     int misVal = missed ? 1 : 0;
181
182     query.bindValue(":missed", misVal);
183
184     if(!query.exec())
185     {
186         ret = false;
187     }
188
189     query.clear();
190
191     // Delete old entries from cache
192     if(LOG_MAX_SIZE > 0)
193     {
194         if(query.exec("SELECT COUNT(*) FROM log") && query.next())
195         {
196             int itemsToDelete = query.value(0).toInt() - LOG_MAX_SIZE;
197
198             for(int i = 0; i < itemsToDelete; i++)
199             {
200                 query.clear();
201
202                 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
203                 {
204                     QSqlError error = query.lastError();
205                     qDebug() << "Unable to delete old cache entries: " << error.text();
206                     ret = false;
207                 }
208             }
209         }
210         else
211         {
212             QSqlError error = query.lastError();
213             qDebug() << "Unable to get count for cache entries: " << error.text();
214             ret = false;
215         }
216
217     }
218
219     if(!connected)
220     {
221         DB::disconnect();
222     }
223
224     return ret;
225
226 }
227
228 void Cache::getLogItems(QList<Cache::LogDetails>& items, int limit)
229 {
230     bool connected = DB::connected();
231
232     if(!connected)
233     {
234         DB::connect();
235     }
236
237     QSqlQuery query("SELECT number, name, street, city, country, time, missed FROM log ORDER BY time DESC LIMIT " + QString::number(limit));
238
239     while(query.next())
240     {
241         LogDetails details;
242         details.result.number = query.value(0).toString();
243         details.result.name = query.value(1).toString();
244         details.result.street = query.value(2).toString();
245         details.result.city = query.value(3).toString();
246         details.result.country = query.value(4).toString();
247         details.time = query.value(5).toInt();
248
249         int missed = query.value(6).toInt();
250
251         details.missed = missed ? true : false;
252
253         items.push_back(details);
254     }
255
256     if(!connected)
257     {
258         DB::disconnect();
259     }
260
261 }
262
263 int Cache::clearLog()
264 {
265     bool connected = DB::connected();
266
267     if(!connected)
268     {
269         DB::connect();
270     }
271
272     QSqlQuery query;
273     int ret = -1;
274
275     if(query.exec("DELETE FROM log"))
276     {
277         ret = query.numRowsAffected();
278     }
279
280     if(!connected)
281     {
282         DB::disconnect();
283     }
284
285     return ret;
286 }
287
288 Cache& Cache::instance()
289 {
290     if(!instance_)
291     {
292         instance_ = new Cache;
293     }
294
295     return *instance_;
296 }