-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
161 lines (140 loc) · 6.15 KB
/
main.cpp
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
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQuickItem>
#include <QQmlProperty>
#include <QQmlContext>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QFileInfo>
#include <QFile>
#include <QStandardPaths>
#include <QDir>
#include <QtGlobal>
#include <QScreen>
#include <QCryptographicHash>
#include "lib/ciqualquerymodel.h"
#include "lib/producttable.h"
//#define DB_MD5_SUM "a1c6a9f6a718c47c4c3f5d297dd89c44"
#define DB_MD5_SUM "6850263c90fba3765fb5808a7b5fb2c3"
void importDB(QFile *dbAssetUrl, QString folder) {
QDir* dir = new QDir(folder + "/data");
if (!dir->exists()) {
qDebug() << "INFO: Creating data folder";
dir->mkpath(".");
}
else {
qDebug() << "INFO: data folder already exists";
}
qDebug() << "INFO: Copying file :" << (dbAssetUrl->copy(folder + "/data/CiKL.sqlite") ? " success" : " failed!");
QFile::setPermissions(folder + "/data/CiKL.sqlite", QFile::WriteOwner | QFile::ReadOwner);
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QScreen *srn = QGuiApplication::screens().at(0);
qDebug() << (qreal)srn->logicalDotsPerInch();
QString folder = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
qDebug() << "INFO: data folder : " << folder;
QString dbAssetUrl;
#ifdef Q_OS_ANDROID
dbAssetUrl = "assets:/data/CiKL.sqlite";
#else
dbAssetUrl = "data/CiKL.sqlite";
#endif
QFile efile(dbAssetUrl);
// Check the existence of the DB file in the package
if (efile.exists()) {
qDebug() << "INFO: Found database file in assets";
qDebug() << "INFO: Database size : " << efile.size() << " bytes";
}
else {
qDebug() << "ERROR: No database found in assets !";
return 0;
}
QFile dfile(folder + "/data/CiKL.sqlite");
// Check the existence of the DB file in the local files
if (!dfile.exists()) {
importDB(&efile, folder);
}
else {
if (dfile.open(QFile::ReadOnly)) {
QCryptographicHash hash(QCryptographicHash::Md5);
// If it exists, we compare the md5 sum with the DB in the package so we can find out if a new version has been distributed
if (hash.addData(&dfile)) {
qDebug() << "INFO: Md5sum: " << hash.result().toHex();
if (hash.result().toHex() == DB_MD5_SUM) {
qDebug() << "INFO: Hey that's a good DB man !";
}
else {
qDebug() << "ERROR: Wrong md5 sum, will replace the database";
// We replace the local DB with DB from the package
dfile.remove();
importDB(&efile, folder);
}
}
}
}
// Now we can open the local DB
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(folder + "/data/CiKL.sqlite");
db.open();
// We create the model that will be used by the ListView objects
CiqualQueryModel searchModel;
CiqualQueryModel componentModel;
CiqualQueryModel groupModel;
CiqualQueryModel groupFilterModel;
// Setting the query for the search and the parameters
searchModel.setQuery("SELECT * FROM CiKL "
"WHERE blandfdnm LIKE :searchString "
"OR blandgpfr LIKE :searchString "
"OR origfdnm LIKE :searchString"
"OR origgpfr LIKE :searchString"
"ORDER BY origfdnm :sorting");
searchModel.addParameter(":searchString", "'%'");
searchModel.addParameter(":sorting", "ASC");
searchModel.refresh();
// Setting the query for the component sorting and the parameters
componentModel.setQuery("SELECT origgpfr, origfdcd, origfdnm, :column as value, "
"(CASE "
"WHEN SUBSTR(:column, 1, 1) = \"<\" THEN SUBSTR(:column, 3) "
"WHEN :column LIKE 'traces' THEN '0' "
"WHEN :column LIKE '-' THEN '-2' "
"WHEN :column LIKE '0' THEN '-1' "
"ELSE :column END) as newValue "
"FROM CiKL WHERE blandfdnm LIKE :searchString "
"OR blandgpfr LIKE :searchString "
"OR origfdnm LIKE :searchString"
"OR origgpfr LIKE :searchString"
"ORDER BY newValue + 0 :sorting");
componentModel.addParameter(":searchString", "'%'");
componentModel.addParameter(":column", "energie_ue_kcal");
componentModel.addParameter(":sorting", "DESC");
componentModel.refresh();
// Setting the query for the group list and the parameter
groupModel.setQuery("SELECT DISTINCT origgpcd, origgpfr "
"FROM CiKL ORDER BY origgpfr :sorting");
groupModel.addParameter(":sorting", "ASC");
groupModel.refresh();
groupFilterModel.setQuery("SELECT origgpcd, origgpfr, origfdcd, origfdnm "
"FROM CiKL WHERE origgpcd LIKE :group "
"AND (blandfdnm LIKE :searchString "
"OR origfdnm LIKE :searchString) "
"ORDER BY origfdnm :sorting");
groupFilterModel.addParameter(":group", "0");
groupFilterModel.addParameter(":searchString", "'%'");
groupFilterModel.addParameter(":sorting", "ASC");
groupFilterModel.refresh();
QQmlApplicationEngine engine;
// We register the models that will be used by the ListView objects
engine.rootContext()->setContextProperty("globalSearchModel", &searchModel);
engine.rootContext()->setContextProperty("componentSearchModel", &componentModel);
engine.rootContext()->setContextProperty("groupsListModel", &groupModel);
engine.rootContext()->setContextProperty("groupsFilterModel", &groupFilterModel);
ProductTable productTable;
engine.rootContext()->setContextProperty("productData", &productTable);
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}