forked from condo4/carbudget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarmanager.cpp
More file actions
171 lines (152 loc) · 4.89 KB
/
Copy pathcarmanager.cpp
File metadata and controls
171 lines (152 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* CarBudget, Sailfish application to manage car cost
*
* Copyright (C) 2014 Fabien Proriol
*
* This file is part of CarBudget.
*
* CarBudget is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* CarBudget is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details. You should have received a copy of the GNU
* General Public License along with CarBudget. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Fabien Proriol
*/
#include "carmanager.h"
#include <QSettings>
void CarManager::refresh()
{
_cars.clear();
QDir home(QDir::homePath());
home.mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
home.setFilter(QDir::Files);
home.setNameFilters(QStringList()<<"*.cbg");
QStringList homeFileList = home.entryList();
foreach(QString file, homeFileList)
{
QFile::rename(QDir::homePath() + QDir::separator() + file,
QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QDir::separator() + file);
}
dir.setFilter(QDir::Files);
dir.setNameFilters(QStringList()<<"*.cbg");
QStringList fileList = dir.entryList();
foreach(QString file, fileList)
{
_cars.append(file.replace(".cbg",""));
}
emit carsChanged();
}
CarManager::CarManager(QObject *parent) :
QObject(parent)
{
QSettings settings;
refresh();
if(settings.contains("SelectedCar"))
{
if(settings.value("SelectedCar").toString() != "NOT_SET")
_car = new Car(settings.value("SelectedCar").toString());
else
_car = NULL;
}
else
{
_car = NULL;
}
emit carsChanged();
emit carChanged();
}
QStringList CarManager::cars()
{
return _cars;
}
Car *CarManager::car()
{
return _car;
}
void CarManager::selectCar(QString name)
{
QSettings settings;
settings.setValue("SelectedCar",name);
if(_car != NULL) delete _car;
_car = new Car(name);
emit carChanged();
}
void CarManager::delCar(QString name)
{
QSettings settings;
if(_car && _car->getName() == name)
{
settings.setValue("SelectedCar","NOT_SET");
delete _car;
_car = NULL;
}
QFile::remove( QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QDir::separator() + name + ".cbg");
refresh();
}
void CarManager::createCar(QString name)
{
bool error = false;
QString db_name = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QDir::separator() + name + ".cbg";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(db_name);
if(!db.open())
{
qDebug() << "ERROR: fail to open file";
}
qDebug() << "DB:" << db_name;
QSqlQuery query(db);
if(!query.exec("CREATE TABLE CarBudget (id VARCHAR(20) PRIMARY KEY, value VARCHAR(20));"))
{
qDebug() << query.lastError();
error = true;
}
if(!query.exec(QString("INSERT INTO CarBudget (id, value) VALUES ('version','%1');").arg(DB_VERSION)))
{
qDebug() << query.lastError();
error = true;
}
if(!query.exec("CREATE TABLE StationList (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);"))
{
qDebug() << query.lastError();
error = true;
}
if(!query.exec("CREATE TABLE TireList (id INTEGER PRIMARY KEY AUTOINCREMENT, buydate DATE, trashdate DATE DEFAULT NULL, price DOUBLE, quantity INT, name TEXT, manufacturer TEXT, model TEXT);"))
{
qDebug() << query.lastError();
error = true;
}
if(!query.exec("CREATE TABLE Event (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, distance UNSIGNED BIG INT);"))
{
qDebug() << query.lastError();
error = true;
}
if(!query.exec("CREATE TABLE TankList (event INTEGER, quantity DOUBLE, price DOUBLE, full TINYINT, station INTEGER, note TEXT);"))
{
qDebug() << query.lastError();
error = true;
}
if(!query.exec("CREATE TABLE CostList (event INTEGER, cost DOUBLE, desc TEXT);"))
{
qDebug() << query.lastError();
error = true;
}
if(!query.exec("CREATE TABLE TireUsage (event_mount INTEGER, event_umount INTEGER, tire INTEGER);"))
{
qDebug() << query.lastError();
error = true;
}
if(!query.exec("CREATE TABLE PeriodicList (id INTEGER PRIMARY KEY AUTOINCREMENT, first DATE, last DATE, cost DOUBLE, desc TEXT, period INTEGER);"))
{
qDebug() << query.lastError();
error = true;
}
if(!error) db.commit();
db.close();
refresh();
}