-
Notifications
You must be signed in to change notification settings - Fork 0
/
epn_dialog.cpp
382 lines (352 loc) · 14.8 KB
/
epn_dialog.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <QMenu>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonValue>
#include <QProcess>
#include <QDir>
#include <QDebug>
#include "epn_dialog.h"
#include "ui_epn_dialog.h"
#include "logger.h"
extern Logger logger;
EPN_Dialog::EPN_Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::EPN_Dialog)
{
ui->setupUi(this);
connect(ui->okButton, SIGNAL(released()), this, SLOT(saveSettings()));
connect(ui->cancelButton, SIGNAL(released()), this, SLOT(cancel()));
setWindowTitle(QString("e-pn v")+VERSION);
// Create a tray icon
trayIcon = new QSystemTrayIcon(QIcon(":/icons/epn-icon.png"));
trayIcon->setToolTip(QString("e-Protocol Notification"));
QMenu *trayMenu = new QMenu();
openAction = trayMenu->addAction("Ρυθμίσεις");
//trayMenu->addSeparator();
//exitAction = trayMenu->addAction("Έξοδος");
connect(openAction, SIGNAL(triggered(bool)), this, SLOT(show()));
//connect(exitAction, SIGNAL(triggered(bool)), this, SLOT(quit()));
shortcut = new QShortcut(QKeySequence("Ctrl+Q"),this);
connect(shortcut, SIGNAL(activated()), this, SLOT(quit()));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconCheckForDoubleClick(QSystemTrayIcon::ActivationReason)));
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
popup = new Popup(this);
settings = new QSettings("epn.ini", QSettings::IniFormat);
username = settings->value("username", "").toString();
ui->usernameEdit->setText(username);
url = settings->value("url", "").toUrl();
timer = new QTimer(this);
timer->start(5*60*1000);
ui->minEdit->setText(QString::number(2));
connect(timer, SIGNAL(timeout()), this, SLOT(getUpdate()));
connect(&fileDownloader, SIGNAL(hashChecked()), this, SLOT(upgradeProgram()));
dontshowagain = false;
lowPriorityMsg = 0;
}
EPN_Dialog::~EPN_Dialog()
{
settings->setValue("url", url.toString());
timer->stop();
delete ui;
delete popup;
delete settings;
delete timer;
}
void EPN_Dialog::replyFinished(QNetworkReply *reply)
{
QJsonDocument jdoc;
QJsonParseError jerr;
QJsonObject jobj, joptobj;
QJsonValue val;
bool silence;
if (reply->error() == QNetworkReply::NoError) {
//qDebug() << reply->readAll();
jdoc = QJsonDocument::fromJson(reply->readAll(), &jerr);
if (jerr.error == QJsonParseError::NoError) {
dontshowagain = false;
jobj = jdoc.object();
// Έλεγχος τιμών
silence = jobj.value("silence").toBool();
val = jobj.value("message");
if (val != QJsonValue::Undefined && !silence) { // Αν υπάρχει μήνυμα
if (!jobj.value("error").toBool()) { // Server returns error message
if (jobj.value("priority").toString() == "high") { // high priority
popup->setPriority(Popup::HighPriority);
popup->showPopup("e-protocol", val.toString());
}
else {
if (jobj.value("priority").toString() == "normal") { // normal priority
popup->setPriority(Popup::NormalPriority);
if (!lowPriorityMsg) // Show low priority messages only 1/4 of the time (when value is 0)
popup->showPopup("e-protocol", val.toString());
}
else {
popup->setPriority(Popup::NoPriority);
if (!lowPriorityMsg) // Show low priority messages only 1/4 of the time (when value is 0)
popup->showPopup("e-protocol", val.toString());
}
lowPriorityMsg = (lowPriorityMsg + 1) % 6;
}
}
else {
popup->setPriority(Popup::Error);
popup->showPopup("e-protocol", val.toString());
}
}
val = jobj.value("options");
if (val != QJsonValue::Undefined) { // Αν υπάρχουν ρυθμίσεις για το πρόγραμμα
joptobj = val.toObject();
val = joptobj.value("timeout");
timeout = val.toInt(30*60*1000); // Default: 30min
if (timeout >= 60000) // Just in case...
timer->setInterval(timeout);
ui->minEdit->setText(QString::number(timeout/60000));
val = joptobj.value("popuptimeout");
popup->setTimeout((val.toInt()<2000)?10000:val.toInt());
if (joptobj.value("url") != QJsonValue::Undefined) {
url = joptobj.value("url").toString(); // Δες αν υπάρχει νέα ρύθμιση για το url
settings->setValue("url",url.toString());
}
val = joptobj.value("version");
if (val != QJsonValue::Undefined) {
version = joptobj.value("version").toString(); // Διάβασε την τελευταία έκδοση του προγράμματος
if ((joptobj.value("forceupgrade").toBool() && compareVersions(QString(VERSION),version)!=0) ||
(compareVersions(QString(VERSION),version)>0)) {
// Υπάρχει νέα έκδοση του προγράμματος. Κατέβασέ το!
qDebug() << "The is a new version available! (" << version << ")";
logger.write(QString("The is a new version available! (") + version + ")");
val = joptobj.value("filelist");
if (val != QJsonValue::Undefined) {
fileDownloader.getFiles(joptobj.value("filelist").toArray().toVariantList(), QUrl(url.toString() + QString("/download/")));
}
}
}
}
if (jobj.value("error").toBool())
trayIcon->setIcon(QIcon(":/icons/epn-icon-error.png"));
else
trayIcon->setIcon(QIcon(":/icons/epn-icon.png"));
}
else {
qDebug() << "Json Error: " << jerr.errorString();
logger.write(QString("Json Error: ") + jerr.errorString());
if (!dontshowagain) {
popup->setPriority(Popup::Error);
popup->showPopup("e-protocol","Σφάλμα! Μη έγκυρη απάντηση από το διακομιστή.");
trayIcon->setIcon(QIcon(":/icons/epn-icon-error.png"));
}
dontshowagain = true;
}
}
else {
qDebug() << "Network Error: " << reply->errorString();
logger.write(QString("Network Error: ") + reply->errorString());
if (!dontshowagain) {
popup->setPriority(Popup::Error);
popup->showPopup("e-protocol","Σφάλμα! Πρόβλημα σύνδεσης με το διακομιστή.");
trayIcon->setIcon(QIcon(":/icons/epn-icon-error.png"));
}
dontshowagain = true;
}
reply->deleteLater();
}
void EPN_Dialog::getUpdate()
{
QUrl query;
query = QUrl(url.toString() + QString("?username=") + username);
if (username.isEmpty()) {
show();
}
else
networkManager->get(QNetworkRequest(query));
qDebug() << "Trying request from " << query.toString();
logger.write(QString("Trying request from ") + query.toString());
}
void EPN_Dialog::saveSettings(void)
{
dontshowagain = false;
username = ui->usernameEdit->text();
settings->setValue("username", username);
getUpdate();
hide();
}
void EPN_Dialog::quit(void) {
trayIcon->hide();
QApplication::quit();
}
void EPN_Dialog::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
hide();
event->ignore();
}
}
int EPN_Dialog::compareVersions(QString ver1, QString ver2)
{
// Οι εκδόσεις είναι πάντα της μορφής x.y.z
QStringList listver1 = ver1.split(".");
QStringList listver2 = ver2.split(".");
if (listver1.at(0).toInt()>listver2.at(0).toInt())
return -1;
else if (listver1.at(0).toInt()<listver2.at(0).toInt())
return 1;
else {
if (listver1.at(1).toInt()>listver2.at(1).toInt())
return -1;
else if (listver1.at(1).toInt()<listver2.at(1).toInt())
return 1;
else {
if (listver1.at(2).toInt()>listver2.at(2).toInt())
return -1;
else if (listver1.at(2).toInt()<listver2.at(2).toInt())
return 1;
return 0;
}
}
}
void EPN_Dialog::cancel(void)
{
ui->usernameEdit->setText(username);
hide();
}
void EPN_Dialog::iconCheckForDoubleClick(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::DoubleClick) {
show();
raise();
activateWindow();
}
}
void EPN_Dialog::upgradeProgram()
{
qDebug() << "Upgrading program...";
logger.write(QString("Upgrading program..."));
bool success = true;
if (fileDownloader.error() == FileDownloader::NoError) { // Files downloaded with no errors
QStringList files = fileDownloader.downloadedFiles();
QMap<int,QByteArray> data = fileDownloader.downloadedData();
for (int i=0; i<files.size(); i++) {
QStringList path = files[i].split("/");
if (path.size() > 1) {
path.removeLast();
QString tmppath=path.join('/');
QDir dir;
if (dir.mkpath(tmppath)==false) { // We've got trouble...
success = false;
qDebug() << "Error creating dir path " << tmppath;
logger.write(QString("Error creating dir path ") + tmppath);
}
}
QFile f((files[i].endsWith(".ex")?(files[i]+"e"):files[i])+".new");
if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
if (f.write(data[i]) == -1) {
success = false;
qDebug() << "Error writing data to " << files[i];
logger.write(QString("Error writing data to ") + files[i]);
}
f.close();
}
else {
qDebug() << "Failed opening " << files[i];
logger.write(QString("Failed opening ") + files[i]);
success = false;
}
}
if (success) { // All OK for now. Do the renaming
for (int i=0; i<files.size(); i++) {
QString fname = files[i].endsWith(".ex")?(files[i]+"e"):files[i];
if (QFile::exists(fname)) {
if (QFile::exists(fname+".old"))
QFile::remove(fname+".old");
if (!QFile::rename(fname,fname+".old")) {
qDebug() << "Failed renaming " << fname << " to " << fname << ".old";
logger.write(QString("Failed renaming ") + fname + " to " + fname + ".old");
success = false;
}
}
}
if (success) {
for (int i=0; i<files.size(); i++) {
QString fname = files[i].endsWith(".ex")?(files[i]+"e"):files[i];
if (!QFile::rename(fname+".new", fname)) {
qDebug() << "Failed renaming " << files[i] << ".new to " << files[i];
logger.write(QString("Failed renaming ") + files[i] + ".new to " + files[i]);
success = false;
}
}
}
else {
for (int i=0; i<files.size(); i++) {
QString fname = files[i].endsWith(".ex")?(files[i]+"e"):files[i];
if (!QFile::rename(fname+".old", fname)) {
qDebug() << "Failed renaming " << fname << ".old to " << fname;
qDebug() << "Something went horribly wrong! :-(";
logger.write(QString("Failed renaming ") + fname + ".old to " + fname);
logger.write(QString("Something went horribly wrong! :-("));
success = false;
}
}
}
if (success) {
// Damned avast!
for (int i=0; i<files.size(); i++) {
if (files[i].endsWith(".ex")) {
if (xor_decrypt(files[i]+"e") == 0) {
// if (!QFile::rename(files[i],files[i]+"e")) {
// qDebug() << "Failed renaming exe file!";
// logger.write(QString("Failed renaming exe file!"));
// success = false;
// }
}
else {
success = false;
}
}
}
}
if (success) {
if (QProcess::startDetached(files[0].endsWith(".ex")?(files[0]+"e"):files[0])) // Start new version and quit this one
quit();
}
}
}
else {
qDebug() << "Downloader returned errno:" << fileDownloader.error() << ". Aborting...";
logger.write(QString("Downloader returned errno:") + fileDownloader.error() + ". Aborting...");
}
}
int EPN_Dialog::xor_decrypt(QString filename)
{
QByteArray buffer;
QFile file(filename);
if (!file.open(QIODevice::ReadWrite)) {
qDebug() << "Error opening " << filename << " rw";
logger.write(QString("Error opening " + filename + " rw"));
return 1;
}
buffer = file.readAll();
if (file.error() != QFile::NoError) {
qDebug() << file.errorString();
logger.write(file.errorString());
file.close();
return 1;
}
for (int i=0; i<buffer.size(); i++) {
buffer[i] = buffer[i] ^ 158;
}
file.seek(0);
if (file.write(buffer) == -1) {
qDebug() << file.errorString();
logger.write(file.errorString());
file.close();
return 1;
}
file.close();
return 0;
}