Better cache handling. Added clear cache button to settings.
[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 <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 bool Cache::findItem(QString const& number, Eniro::Result& result)
58 {
59     bool connected = DB::connected();
60
61     if(!connected)
62     {
63         DB::connect();
64     }
65
66     QSqlQuery query;
67     query.prepare("SELECT name, street, city FROM cache WHERE number = :number");
68     query.bindValue(":number", number);
69
70     bool ret = false;
71
72     if(query.exec() && query.next())
73     {
74         result.number = number;
75         result.name = query.value(0).toString();
76         result.street = query.value(1).toString();
77         result.city = query.value(2).toString();
78
79         ret = true;
80     }
81
82     if(!connected)
83     {
84         DB::disconnect();
85     }
86
87     return ret;
88 }
89
90 bool Cache::addItem(Eniro::Result const& result)
91 {
92     bool connected = DB::connected();
93
94     if(!connected)
95     {
96         DB::connect();
97     }
98
99     bool ret = true;
100
101     QSqlQuery query;
102
103     query.prepare("INSERT INTO cache(number, name, street, city) VALUES(:number, :name, :street, :city)");
104     query.bindValue(":number", result.number);
105     query.bindValue(":name", result.name);
106     query.bindValue(":street", result.street);
107     query.bindValue(":city", result.city);
108
109
110     if(!query.exec())
111     {
112         ret = false;
113     }
114
115     query.clear();
116
117     QString cacheSize = Settings::instance()->get("cache_size");
118
119     int cacheLimit = 0;
120
121     // Delete old entries from cache
122     if((cacheLimit = cacheSize.toInt()) > 0)
123     {
124         if(query.exec("SELECT COUNT(*) FROM cache") && query.next())
125         {
126             int itemsToDelete = query.value(0).toInt() - cacheLimit;
127
128             for(int i = 0; i < itemsToDelete; i++)
129             {
130                 query.clear();
131
132                 if(!query.exec("DELETE FROM cache WHERE id = (SELECT MIN(id) FROM cache)"))
133                 {
134                     QSqlError error = query.lastError();
135                     qDebug() << "Unable to delete old cache entries: " << error.text();
136                     ret = false;
137                 }
138             }
139         }
140         else
141         {
142             QSqlError error = query.lastError();
143             qDebug() << "Unable to get count for cache entries: " << error.text();
144             ret = false;
145         }
146
147     }
148
149     if(!connected)
150     {
151         DB::disconnect();
152     }
153
154     return ret;
155
156 }
157
158 Cache& Cache::instance()
159 {
160     if(!instance_)
161     {
162         instance_ = new Cache;
163     }
164
165     return *instance_;
166 }