Fixed some issues with special characters and adding contact to address book.
[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     if(!query.exec())
112     {
113         ret = false;
114     }
115
116     query.clear();
117
118     QString cacheSize = Settings::instance()->get("cache_size");
119
120     int cacheLimit = 0;
121
122     // Delete old entries from cache
123     if((cacheLimit = cacheSize.toInt()) > 0)
124     {
125         if(query.exec("SELECT COUNT(*) FROM cache") && query.next())
126         {
127             int itemsToDelete = query.value(0).toInt() - cacheLimit;
128
129             for(int i = 0; i < itemsToDelete; i++)
130             {
131                 query.clear();
132
133                 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
134                 {
135                     QSqlError error = query.lastError();
136                     qDebug() << "Unable to delete old cache entries: " << error.text();
137                     ret = false;
138                 }
139             }
140         }
141         else
142         {
143             QSqlError error = query.lastError();
144             qDebug() << "Unable to get count for cache entries: " << error.text();
145             ret = false;
146         }
147
148     }
149
150     if(!connected)
151     {
152         DB::disconnect();
153     }
154
155     return ret;
156
157 }
158
159 bool Cache::logItem(Source::Result const& result, bool missed, unsigned int time)
160 {
161     bool connected = DB::connected();
162
163     if(!connected)
164     {
165         DB::connect();
166     }
167
168     bool ret = true;
169
170     QSqlQuery query;
171
172     query.prepare("INSERT INTO log(number, name, street, city, country, time, missed) VALUES(:number, :name, :street, :city, :country, :time, :missed)");
173     query.bindValue(":number", result.number);
174     query.bindValue(":name", result.name);
175     query.bindValue(":street", result.street);
176     query.bindValue(":city", result.city);
177     query.bindValue(":country", result.country);
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, country, 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.result.country = query.value(4).toString();
246         details.time = query.value(5).toInt();
247
248         int missed = query.value(6).toInt();
249
250         details.missed = missed ? true : false;
251
252         items.push_back(details);
253     }
254
255     if(!connected)
256     {
257         DB::disconnect();
258     }
259
260 }
261
262 int Cache::clearLog()
263 {
264     bool connected = DB::connected();
265
266     if(!connected)
267     {
268         DB::connect();
269     }
270
271     QSqlQuery query;
272     int ret = -1;
273
274     if(query.exec("DELETE FROM log"))
275     {
276         ret = query.numRowsAffected();
277     }
278
279     if(!connected)
280     {
281         DB::disconnect();
282     }
283
284     return ret;
285 }
286
287 Cache& Cache::instance()
288 {
289     if(!instance_)
290     {
291         instance_ = new Cache;
292     }
293
294     return *instance_;
295 }