Added call log and German translation.
[jenirok] / src / common / db.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 <QtCore/QDir>
20 #include <QtCore/QDebug>
21 #include <QtCore/QVariant>
22 #include <QtSql/QSqlQuery>
23 #include <QtSql/QSqlError>
24 #include "db.h"
25
26 namespace
27 {
28     const QString SQL_DRIVER = "QSQLITE";
29     const QString SQL_DATABASE = ".jenirok.db";
30     const int DB_VERSION = 2;
31 }
32
33 bool DB::initialized_ = false;
34 QSqlDatabase DB::db_;
35
36 bool DB::connect()
37 {
38     if(!initialized_)
39     {
40         db_ = QSqlDatabase::addDatabase(SQL_DRIVER);
41         QString path = QDir::home().path() + QDir::separator() + SQL_DATABASE;
42         db_.setDatabaseName(QDir::toNativeSeparators(path));
43     }
44
45     bool ret = db_.open();
46
47     if(!ret)
48     {
49         QSqlError error = db_.lastError();
50         qDebug() << error.text();
51     }
52
53     if(ret && !initialized_)
54     {
55         QSqlQuery query("SELECT value FROM settings WHERE name = 'db_version'");
56
57         if(query.next())
58         {
59             int currentVersion = query.value(0).toString().toInt();
60
61             if(currentVersion < DB_VERSION)
62             {
63                 ret = createTables(true);
64             }
65         }
66         else
67         {
68             ret = createTables(false);
69         }
70     }
71
72     if(!initialized_)
73     {
74         initialized_ = true;
75     }
76
77     return ret;
78 }
79
80 void DB::disconnect()
81 {
82     db_.close();
83 }
84
85 void DB::removeDatabase()
86 {
87     if(!initialized_)
88     {
89         return;
90     }
91
92     db_.close();
93     db_ = QSqlDatabase();
94     QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
95     initialized_ = false;
96 }
97
98 bool DB::connected()
99 {
100     return db_.isOpen();
101 }
102
103 QSqlDatabase& DB::instance()
104 {
105     return db_;
106 }
107
108 bool DB::createTables(bool update)
109 {
110     QSqlQuery query;
111
112     bool ret = true;
113
114     ret = ret && query.exec("CREATE TABLE IF NOT EXISTS cache (id INTEGER PRIMARY KEY, number VARCHAR(32) NOT NULL UNIQUE, name VARCHAR(255) NOT NULL, street VARCHAR(255) NOT NULL, city VARCHAR(255) NOT NULL)");
115     ret = ret && query.exec("CREATE TABLE IF NOT EXISTS settings (name VARCHAR(255) NOT NULL PRIMARY KEY, value VARCHAR(255) NOT NULL)");
116     ret = ret && query.exec("CREATE TABLE IF NOT EXISTS log (id INTEGER PRIMARY KEY, number VARCHAR(32) NOT NULL, name VARCHAR(255), street VARCHAR(255), city VARCHAR(255), time INTEGER NOT NULL, missed INTEGER NOT NULL)");
117
118     if(update)
119     {
120         ret = ret && query.exec("UPDATE settings SET value = '" + QString::number(DB_VERSION) + "' WHERE name = 'db_version'");
121     }
122     else
123     {
124         ret = ret && query.exec("INSERT INTO settings(name, value) VALUES('db_version', '" + QString::number(DB_VERSION) + "')");
125     }
126
127     return ret;
128 }