-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgft.cpp
203 lines (176 loc) · 5.5 KB
/
gft.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QDir>
#include "gftprogram.h"
#include "gft.h"
#include "gftsecret.h"
#include "database.h"
#include "trace.h"
#include "platform.h"
const int GFT_RECORDS_PER_UPLOAD = 150;
static Gft *instance_;
Gft *Gft::instance() {
if (!instance_) {
instance_ = new Gft();
}
return instance_;
}
void Gft::close() {
delete instance_;
instance_ = 0;
}
Gft::Gft(QObject *parent): O2Gft(parent), program(0) {
setClientId(GFT_OAUTH_CLIENT_ID);
setClientSecret(GFT_OAUTH_CLIENT_SECRET);
}
Gft::~Gft() {
delete program;
}
bool Gft::enabled() {
return QSettings().value("gft.enabled", false).toBool();
}
void Gft::setEnabled(bool v) {
QSettings().setValue("gft.enabled", v);
emit enabledChanged();
}
void Gft::upload() {
Trace t("Gft::upload");
// Create program if doesn't exist
if (!program) {
program = new GftProgram;
connect(program, SIGNAL(stepCompleted(GftIdList)), this, SLOT(onStepCompleted(GftIdList)));
connect(program, SIGNAL(programCompleted(bool)), this, SLOT(onProgramCompleted(bool)));
}
uploadedRecords.clear();
Database db(Platform::instance()->dbPath());
// Do nothing if database is empty
{
qlonglong total = -1;
QSqlQuery query("select count(*) from log where ingft = 0", db.db());
query.next();
total = query.value(0).toLongLong();
if (total == 0) {
qDebug() << "No records to upload";
emit uploadFinished(UploadComplete);
return;
}
}
// Mark all records as uploaded if GFT is not enabled, then succeed
if (!enabled()) {
qDebug() << "GFT not enabled";
QSqlQuery query("update log set ingft = 1", db.db());
query.exec();
emit uploadFinished(UploadComplete);
return;
}
// Fail if not logged in
if (!linked()) {
qDebug() << "Not logged in";
emit uploadFinished(UploadFailed);
return;
}
// Create Gft program's instructions
QList<GftInstruction> instructions;
instructions.append(GftInstruction(GftFindTable, "Steps"));
instructions.append(GftInstruction(GftCreateTableIf, "Steps"));
QSqlQuery query(db.db());
query.setForwardOnly(true);
if (!query.exec("select id, date, steps from log where ingft = 0")) {
qCritical() << "Gft::upload: Could not query log:" << query.lastError().text();
emit uploadFinished(UploadFailed);
return;
}
int numRecords = 0;
QString sql;
QList<qlonglong> idList;
while (query.next()) {
if (++numRecords > GFT_RECORDS_PER_UPLOAD) {
break;
}
qlonglong id = query.value(0).toLongLong();
QString date = sanitize(query.value(1).toString().replace('T', ' '));
int steps = query.value(2).toInt();
QString tags = getTags(db, id);
QString device = Platform::instance()->deviceId();
sql.append(QString("INSERT INTO $T (steps,date,tags,device) VALUES (%1,'%2','%3','%4');\n").arg(steps).arg(date, tags, device));
idList.append(id);
}
instructions.append(GftInstruction(GftQuery, sql, idList));
// Execute Gft program
program->setInstructions(instructions);
program->step();
}
QString Gft::getTags(Database &db, qlonglong id) {
QString ret;
QSqlQuery query(db.db());
query.prepare("select name, value from tags where logId = ?");
query.bindValue(0, id);
if (!query.exec()) {
qCritical() << "Gft::getTags: Could not query log:" << query.lastError().text();
return ret;
}
QSqlRecord record = query.record();
int nameIndex = record.indexOf("name");
int valueIndex = record.indexOf("value");
while (query.next()) {
QString name = query.value(nameIndex).toString();
QString value = query.value(valueIndex).toString();
ret.append(sanitize(name));
ret.append("=");
ret.append(sanitize(value));
ret.append(";");
}
return ret;
}
QString Gft::sanitize(const QString &s) {
QString ret = s;
ret.remove('\'');
ret.remove('"');
ret.remove(';');
ret.remove('=');
ret.remove('\\');
return ret;
}
void Gft::onStepCompleted(GftIdList recordIdList) {
Trace t("Gft::onStepCompleted");
foreach (qlonglong recordId, recordIdList) {
uploadedRecords.append(recordId);
}
}
void Gft::onProgramCompleted(bool failed) {
Trace _("Gft::onProgramCompleted");
int result = UploadFailed;
// Mark uploaded records in the log database
Database db(Platform::instance()->dbPath());
db.transaction();
foreach (qlonglong id, uploadedRecords) {
QSqlQuery query(db.db());
query.prepare("update log set ingft = 1 where id = ?");
query.bindValue(0, id);
query.exec();
}
db.commit();
// Determine upload result
if (failed) {
qDebug() << "Result: Failed";
result = UploadFailed;
} else {
// Are there any records left?
qlonglong total = -1;
QSqlQuery query("select count(*) from log where ingft = 0", db.db());
query.next();
total = query.value(0).toLongLong();
qDebug() << total << "records left";
if (total == 0) {
qDebug() << "Result: Complete";
result = UploadComplete;
} else {
qDebug() << "Result: Incomplete";
result = UploadIncomplete;
}
}
emit uploadFinished(result);
}