Added call log and German translation.
[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, time, missed) VALUES(:number, :name, :street, :city, :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(":time", time);
179     int misVal = missed ? 1 : 0;
180
181     query.bindValue(":missed", misVal);
182
183     if(!query.exec())
184     {
185         ret = false;
186     }
187
188     query.clear();
189
190     // Delete old entries from cache
191     if(LOG_MAX_SIZE > 0)
192     {
193         if(query.exec("SELECT COUNT(*) FROM log") && query.next())
194         {
195             int itemsToDelete = query.value(0).toInt() - LOG_MAX_SIZE;
196
197             for(int i = 0; i < itemsToDelete; i++)
198             {
199                 query.clear();
200
201                 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
202                 {
203                     QSqlError error = query.lastError();
204                     qDebug() << "Unable to delete old cache entries: " << error.text();
205                     ret = false;
206                 }
207             }
208         }
209         else
210         {
211             QSqlError error = query.lastError();
212             qDebug() << "Unable to get count for cache entries: " << error.text();
213             ret = false;
214         }
215
216     }
217
218     if(!connected)
219     {
220         DB::disconnect();
221     }
222
223     return ret;
224
225 }
226
227 void Cache::getLogItems(QList<Cache::LogDetails>& items, int limit)
228 {
229     bool connected = DB::connected();
230
231     if(!connected)
232     {
233         DB::connect();
234     }
235
236     QSqlQuery query("SELECT number, name, street, city, time, missed FROM log ORDER BY time DESC LIMIT " + QString::number(limit));
237
238     while(query.next())
239     {
240         LogDetails details;
241         details.result.number = query.value(0).toString();
242         details.result.name = query.value(1).toString();
243         details.result.street = query.value(2).toString();
244         details.result.city = query.value(3).toString();
245         details.time = query.value(4).toInt();
246
247         int missed = query.value(5).toInt();
248
249         details.missed = missed ? true : false;
250
251         items.push_back(details);
252     }
253
254     if(!connected)
255     {
256         DB::disconnect();
257     }
258
259 }
260
261 int Cache::clearLog()
262 {
263     bool connected = DB::connected();
264
265     if(!connected)
266     {
267         DB::connect();
268     }
269
270     QSqlQuery query;
271     int ret = -1;
272
273     if(query.exec("DELETE FROM log"))
274     {
275         ret = query.numRowsAffected();
276     }
277
278     if(!connected)
279     {
280         DB::disconnect();
281     }
282
283     return ret;
284 }
285
286 Cache& Cache::instance()
287 {
288     if(!instance_)
289     {
290         instance_ = new Cache;
291     }
292
293     return *instance_;
294 }