/
updateform.cpp
879 lines (835 loc) · 28.8 KB
/
updateform.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//This program 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.
//This program 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.
#include "updateform.hpp"
#include <QtXml>
#include <QFile>
#include <QDesktopServices>
#if QT_VERSION >= 0x050000
#include <QTemporaryDir>
#endif
#include <QNetworkReply>
#ifdef HUGGLE_WIN
#ifndef HUGGLE_NOUPDATER
#include <windows.h>
#include <Shellapi.h>
#include <tchar.h>
#include <string>
#endif
#endif
#include <huggle_core/configuration.hpp>
#include <huggle_core/core.hpp>
#include <huggle_core/exception.hpp>
#include <huggle_core/generic.hpp>
#include <huggle_core/localization.hpp>
#include <huggle_core/syslog.hpp>
#include "uigeneric.hpp"
#include "ui_updateform.h"
#include <huggle_core/webserverquery.hpp>
#define LOG Syslog::HuggleLogs->Log
using namespace Huggle;
UpdateForm::UpdateForm(QWidget *parent) : QDialog(parent), ui(new Ui::UpdateForm)
{
this->ui->setupUi(this);
this->qData = NULL;
this->timer = new QTimer(this);
this->ui->progressBar_2->setVisible(false);
this->ui->checkBox_2->setChecked(Configuration::HuggleConfiguration->SystemConfig_NotifyBeta);
if (hcfg->SystemConfig_UM)
{
this->ui->checkBox->setEnabled(false);
this->ui->checkBox_2->setEnabled(false);
this->RootPath = hcfg->UpdaterRoot;
this->ui->label->setText(_l("updater-wait"));
this->ui->pushButton->setEnabled(false);
this->ui->pushButton_2->setEnabled(false);
this->Update();
}
else
{
this->setWindowTitle(_l("updater-title"));
this->ui->pushButton->setText(_l("updater-update"));
this->ui->pushButton_2->setText(_l("updater-close"));
this->ui->checkBox->setText(_l("updater-disable-notify"));
}
}
UpdateForm::~UpdateForm()
{
while (this->Instructions.count())
{
delete this->Instructions.at(0);
this->Instructions.removeAt(0);
}
delete this->ui;
delete this->manager;
delete this->file;
delete this->reply;
delete this->timer;
delete this->manualDownloadpage;
}
void UpdateForm::Check()
{
#ifndef HUGGLE_NOUPDATER
this->qData = new WebserverQuery();
this->qData->URL = "https://huggle.toolforge.org/updater/?version=" + QUrl::toPercentEncoding(HUGGLE_VERSION)
+ "&os=" + QUrl::toPercentEncoding(Configuration::HuggleConfiguration->Platform)
+ "&language=" + Localizations::HuggleLocalizations->PreferredLanguage;
if (Configuration::HuggleConfiguration->SystemConfig_NotifyBeta)
this->qData->URL += "¬ifybeta";
HUGGLE_DEBUG1("checking for update at " + this->qData->URL);
this->qData->Process();
connect(this->timer, SIGNAL(timeout()), this, SLOT(OnTick()));
this->qData->IncRef();
this->timer->start(HUGGLE_TIMER);
#endif
}
static QString TrimSlashes(QString path)
{
while (path.contains("//"))
path = path.replace("//", "/");
#ifdef HUGGLE_WIN
path = path.replace("/", "\\");
while (path.contains("\\\\"))
path = path.replace("\\\\", "\\");
#endif
return path;
}
#if QT_VERSION >= 0x050000
// Checks if OS is supported by updater
static bool IsSupported()
{
#ifdef HUGGLE_WIN
return true;
#else
return false;
#endif
}
#endif
static void recurseAddDir(QDir d, QStringList &list, QString path, QStringList &dirs)
{
QStringList qsl = d.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
foreach(QString file, qsl)
{
QFileInfo finfo(QString("%1/%2").arg(d.path()).arg(file));
if (finfo.isSymLink())
return;
if (finfo.isDir())
{
QString dirname = finfo.fileName();
QDir sd(finfo.filePath());
dirs << QDir::toNativeSeparators(path + dirname);
recurseAddDir(sd, list, path + dirname + QDir::separator(), dirs);
}
else
{
list << QDir::toNativeSeparators(TrimSlashes(path + finfo.fileName()));
}
}
}
#if QT_VERSION >= 0x050000
static void recurseAddDirWithSources(QDir d, QStringList &lf_to, QStringList &lf_from, QString path, QString source, QStringList &dirs)
{
QStringList qsl = d.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
foreach(QString file, qsl)
{
QFileInfo finfo(QString("%1/%2").arg(d.path()).arg(file));
if (finfo.isSymLink())
return;
if (finfo.isDir())
{
QString dirname = finfo.fileName();
QDir sd(finfo.filePath());
dirs << QDir::toNativeSeparators(path + "/" + dirname);
recurseAddDirWithSources(sd, lf_to, lf_from, path + "/" + dirname + QDir::separator(), source + "/" + dirname, dirs);
}
else
{
lf_from << QDir::toNativeSeparators(TrimSlashes(source + "/" + finfo.fileName()));
lf_to << QDir::toNativeSeparators(TrimSlashes(path + "/" + finfo.fileName()));
}
}
}
#endif
void Huggle::UpdateForm::on_pushButton_clicked()
{
Configuration::HuggleConfiguration->SystemConfig_NotifyBeta = this->ui->checkBox_2->isChecked();
if (this->ui->checkBox->isChecked())
{
Configuration::HuggleConfiguration->SystemConfig_EnableUpdates = false;
Configuration::HuggleConfiguration->SaveSystemConfig();
}
if (this->manualDownloadpage != nullptr)
{
QDesktopServices::openUrl(*(this->manualDownloadpage));
}
else
{
#if QT_VERSION >= 0x050000
this->ui->pushButton->setEnabled(false);
this->ui->label->setText("Preparing files...");
this->ui->pushButton_2->setEnabled(false);
if (this->Instructions.count() < 1)
{
this->Fail("Manifest contains no data");
return;
}
if (!IsSupported())
{
// this is unlike error as there should be no manifest provided for unknown OS
this->Fail("This operating system is not supported by updater");
return;
}
// first we need to get a temporary folder to which we backup manifest and whole huggle binary folder
QTemporaryDir temp;
if (!temp.isValid())
{
this->Fail("Unable to get a temporary folder");
return;
}
temp.setAutoRemove(false);
Syslog::HuggleLogs->Log("Backing up huggle to " + temp.path());
this->TempPath = temp.path();
// copy folder that contains binary data to temporary folder
QDir binary_folder(QCoreApplication::applicationDirPath());
recurseAddDir(binary_folder, values, "", dirs);
foreach (QString directory, dirs)
{
if (!QDir().mkpath(temp.path() + QDir::separator() + directory))
{
this->Fail("Failed to create " + temp.path() + QDir::separator() + directory);
return;
}
}
this->RootPath = binary_folder.path();
this->MovingFiles = true;
this->ui->progressBar->setMaximum(values.count());
this->timer->start(20);
#endif
}
}
void Huggle::UpdateForm::on_pushButton_2_clicked()
{
if (hcfg->SystemConfig_UM)
{
#ifdef HUGGLE_WIN
this->Generic_Exec(this->RootPath + "/huggle.exe", QStringList());
#endif
QApplication::exit();
return;
}
Configuration::HuggleConfiguration->SystemConfig_NotifyBeta = this->ui->checkBox_2->isChecked();
if (this->ui->checkBox->isChecked())
{
Configuration::HuggleConfiguration->SystemConfig_EnableUpdates = false;
Configuration::HuggleConfiguration->SaveSystemConfig();
}
this->close();
}
void UpdateForm::OnTick()
{
#ifndef HUGGLE_NOUPDATER
if (this->MovingFiles)
{
if (this->CurrentFile >= this->values.count())
{
this->timer->stop();
this->PreparationFinish();
return;
}
QString file_ = this->values.at(this->CurrentFile);
if (!QFile::copy(this->RootPath + QDir::separator() + file_,
this->TempPath + QDir::separator() + file_))
{
this->Fail("Unable to copy " + this->RootPath + QDir::separator() + file_ +
" to " + this->TempPath + QDir::separator() + file_);
}
this->CurrentFile++;
this->ui->progressBar->setValue(this->CurrentFile);
return;
}
if (hcfg->SystemConfig_UM)
{
this->NextInstruction();
return;
}
if (!this->qData->IsProcessed())
return;
QDomDocument r;
this->Manifest = this->qData->Result->Data;
r.setContent(this->qData->Result->Data);
QDomNodeList l = r.elementsByTagName("obsolete");
if (l.count() == 0)
{
// there is no new version of huggle
goto finish;
} else
{
QString version = l.at(0).toElement().text();
QString info = _l("updater-update-available", version);
l = r.elementsByTagName("info");
if (l.count() > 0)
{
// we don't know how to update o.O
info = l.at(0).toElement().text();
this->ui->pushButton->setEnabled(false);
info = info.replace("$LATESTHUGGLE", version).replace("$VERSION", hcfg->HuggleVersion);
this->ui->label->setText(info);
l = r.elementsByTagName("manualDownloadpage");
if(l.count() > 0)
{
this->manualDownloadpage = new QUrl(l.at(0).toElement().text());
this->ui->pushButton->setText(_l("updater-open-manualdownloadpage"));
this->ui->pushButton->setEnabled(true);
}
this->show();
goto finish;
} else
{
// get the instructions
l = r.elementsByTagName("update");
if (l.count())
{
l = l.at(0).childNodes();
int id = 0;
while (id < l.count())
{
QDomElement element = l.at(id).toElement();
if (this->parse_xml(&element))
id++;
else
goto invalid;
}
}
// we are ready for update now
this->ui->pushButton->setEnabled(true);
info = "Huggle can be updated from " + hcfg->HuggleVersion + " to " + version + " please click Update button to proceed.";
this->ui->label->setText(info);
this->show();
goto finish;
}
}
invalid:
this->ui->pushButton->setEnabled(false);
this->ui->label->setText("FAILED: the update manifest is invalid. I can't update. The error is: " + this->Error + "\n\nPlease send this error to developers");
finish:
this->qData->DecRef();
this->timer->stop();
#endif
}
void UpdateForm::Exit()
{
this->close();
Core::HuggleCore->Shutdown();
}
void Huggle::UpdateForm::on_label_linkActivated(const QString &link)
{
QDesktopServices::openUrl(link);
}
void UpdateForm::httpReadyRead()
{
if (this->file)
this->file->write(this->reply->readAll());
}
void UpdateForm::httpDownloadFinished()
{
this->ui->progressBar_2->setVisible(false);
this->file->flush();
this->file->close();
this->Write("Working...");
delete this->file;
this->file = nullptr;
// there is a bug in Qt, we need to reopen the file for hash to be correct
QString hash = this->MD5(this->TempPath + "temp/" + this->inst->Destination);
if (hash != this->inst->Hash_MD5)
{
LOG("Hash " + hash + " not matches " + this->inst->Hash_MD5);
this->Fail("Hash for " + this->inst->Source + " doesn't match");
} else
{
LOG("Hash " + hash + " matches " + this->inst->Hash_MD5);
}
this->reply->deleteLater();
this->reply = nullptr;
delete this->inst;
this->inst = nullptr;
}
void UpdateForm::updateDownloadProgress(qint64 value, qint64 max)
{
if (this->ui->progressBar_2->maximum() != max)
this->ui->progressBar_2->setMaximum(max);
this->ui->progressBar_2->setValue(value);
}
bool Huggle::UpdateForm::parse_xml(QDomElement *line)
{
if (line->tagName() == "download")
{
if (!line->attributes().contains("to"))
{
Syslog::HuggleLogs->WarningLog("Invalid updater instruction: download is missing to, ignoring the update");
return false;
}
if (!line->attributes().contains("md5"))
{
Syslog::HuggleLogs->WarningLog("Invalid updater instruction: download is missing md5, ignoring the update");
return false;
}
Instruction *i = new Instruction(Instruction_Download, line->text(), line->attribute("to"),
false, false, false, false, line->attribute("md5"));
if (line->attributes().contains("compare"))
i->Original = line->attribute("compare");
this->Instructions.append(i);
return true;
}
if (line->tagName() == "move")
{
if (!line->attributes().contains("to"))
{
Syslog::HuggleLogs->WarningLog("Invalid updater instruction: move is missing to, ignoring the update");
return false;
}
if (!line->attributes().contains("from"))
{
Syslog::HuggleLogs->WarningLog("Invalid updater instruction: move is missing from, ignoring the update");
return false;
}
bool elevated_ = false;
bool overwrite_ = false;
bool merging_ = false;
bool recursive_ = false;
if (line->attributes().contains("merging") && line->attribute("merging") == "true")
merging_ = true;
if (line->attributes().contains("recursive") && line->attribute("recursive") == "true")
recursive_ = true;
if (line->attributes().contains("elevated") && line->attribute("elevated") == "true")
elevated_ = true;
if (line->attributes().contains("overwrite") && line->attribute("overwrite") == "true")
overwrite_ = true;
this->Instructions.append(new Instruction(Instruction_Move, line->attribute("from"), line->attribute("to"), elevated_, recursive_, merging_, overwrite_));
return true;
}
if (line->tagName() == "copy")
{
if (!line->attributes().contains("to"))
{
Syslog::HuggleLogs->WarningLog("Invalid updater instruction: copy is missing to, ignoring the update");
return false;
}
if (!line->attributes().contains("from"))
{
Syslog::HuggleLogs->WarningLog("Invalid updater instruction: copy is missing from, ignoring the update");
return false;
}
bool elevated_ = false;
bool overwrite_ = false;
bool merging_ = false;
bool recursive_ = false;
if (line->attributes().contains("merging") && line->attribute("merging") == "true")
merging_ = true;
if (line->attributes().contains("recursive") && line->attribute("recursive") == "true")
recursive_ = true;
if (line->attributes().contains("elevated") && line->attribute("elevated") == "true")
elevated_ = true;
if (line->attributes().contains("overwrite") && line->attribute("overwrite") == "true")
overwrite_ = true;
this->Instructions.append(new Instruction(Instruction_Copy, line->attribute("from"), line->attribute("to"), elevated_, recursive_, merging_, overwrite_));
return true;
}
if (line->tagName() == "folder")
{
this->Instructions.append(new Instruction(Instruction_Folder, line->text(), ""));
return true;
}
if (line->tagName() == "remove")
{
bool elevated_ = false;
bool overwrite_ = false;
bool recursive_ = false;
if (line->attributes().contains("recursive") && line->attribute("recursive") == "true")
recursive_ = true;
if (line->attributes().contains("elevated") && line->attribute("elevated") == "true")
elevated_ = true;
if (line->attributes().contains("overwrite") && line->attribute("overwrite") == "true")
overwrite_ = true;
this->Instructions.append(new Instruction(Instruction_Delete, line->text(), "", elevated_, recursive_, false, overwrite_));
return true;
}
if (line->tagName() == "exec")
{
bool root = false;
if (line->attributes().contains("elevated") && line->attribute("elevated") == "true")
root = true;
this->Instructions.append(new Instruction(Instruction_Execute, line->text(), "", root));
return true;
}
return true;
}
void UpdateForm::reject()
{
//if (hcfg->UpdaterMode)
// return;
if (this->ui->checkBox->isChecked())
{
Configuration::HuggleConfiguration->SystemConfig_EnableUpdates = false;
Configuration::HuggleConfiguration->SaveSystemConfig();
}
Configuration::HuggleConfiguration->SystemConfig_NotifyBeta = this->ui->checkBox_2->isChecked();
QDialog::reject();
}
#if defined(HUGGLE_WIN) && !defined(HUGGLE_NOUPDATER)
#ifdef UNICODE
#define HUGGLE_LPCSTR LPCWSTR
#else
#define HUGGLE_LPCSTR LPCSTR
#endif
static HUGGLE_LPCSTR QString2Msdn(QString text)
{
#ifdef UNICODE
wchar_t *temp = (wchar_t*)calloc(text.length() + 1, sizeof(wchar_t));
text.toWCharArray(temp);
return _wcsdup(_T(temp));
#else
QByteArray byte_array = text.toLocal8Bit();
return _strdup(byte_array.constData());
#endif
}
#endif
void UpdateForm::PreparationFinish()
{
#ifndef HUGGLE_NOUPDATER
QString manifest_path = this->TempPath + QDir::separator() + "updater.xml";
QFile manifest(manifest_path);
if (!manifest.open(QIODevice::ReadWrite))
{
this->Fail("Unable to open the manifest file");
return;
}
manifest.write(this->Manifest.toUtf8());
manifest.close();
// now we can launch the copy of huggle with parameters for update
#ifdef HUGGLE_WIN
QString program = this->TempPath + QDir::separator() + "huggle.exe";
QString arguments = "--huggleinternal-update \"" + this->RootPath + "\" -v --syslog \"" + this->TempPath + QDir::separator() + "update.log\"";
/*
QProcess *ClientProcess = new QProcess( this );
// exit calling application on called application start
connect( ClientProcess, SIGNAL( started() ), this, SLOT( Exit() ) );
// receive errors
connect(ClientProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(ProcessError(QProcess::ProcessError)));
ClientProcess->startDetached( program, arguments );*/
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
SHELLEXECUTEINFO shExInfo {};
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
shExInfo.cbSize = sizeof(shExInfo);
shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExInfo.hwnd = 0;
shExInfo.lpVerb = QString2Msdn("runas");
shExInfo.lpFile = QString2Msdn(program);
shExInfo.lpParameters = QString2Msdn(arguments);
shExInfo.lpDirectory = QString2Msdn(this->TempPath);
shExInfo.nShow = SW_SHOW;
shExInfo.hInstApp = 0;
if (ShellExecuteEx(&shExInfo))
{
Exit();
} else
{
this->Fail("Unable to launch the updater");
}
#endif
#endif
this->close();
}
void UpdateForm::Fail(QString reason)
{
if (hcfg->SystemConfig_UM)
reason += "\n\nUpdating of huggle has failed, please note that there is full "\
"backup in " + this->TempPath +
" this log has file update.log, please send it to developers "\
"for analysis of update problem. ";
UiGeneric::MessageBox("Fatal", reason, MessageBoxStyleError, true);
this->Error = reason;
if (this->timer)
this->timer->stop();
this->close();
if (hcfg->SystemConfig_UM)
QApplication::exit();
}
bool UpdateForm::ProcessManifest(QString data)
{
QDomDocument r;
r.setContent(data);
QDomNodeList l;
l = r.elementsByTagName("update");
if (l.count())
{
l = l.at(0).childNodes();
int id = 0;
while (id < l.count())
{
QDomElement element = l.at(id).toElement();
if (this->parse_xml(&element))
id++;
else
return false;
}
return true;
}
return false;
}
void UpdateForm::ProcessDownload()
{
QString download_target = this->TempPath + "temp/";
if (QFile().exists(download_target + this->inst->Destination))
{
this->Fail(download_target + this->inst->Destination + " already exists!");
return;
}
if (!QDir().exists(download_target) && !QDir().mkpath(download_target))
{
this->Fail("Unable to create a temporary folder");
return;
}
if (!this->inst->Original.isEmpty() && !this->inst->Hash_MD5.isEmpty())
{
QString path = this->Path(this->inst->Original);
if (QFile().exists(path))
{
QString hash = this->MD5(path);
if (hash == this->inst->Hash_MD5)
{
LOG("Skipping " + this->inst->Source);
if (!QFile().copy(path, download_target + this->inst->Destination))
this->Fail("Unable to copy " +
path +
" to " + download_target + this->inst->Destination);
delete this->inst;
this->inst = nullptr;
return;
}
}
}
this->ui->progressBar_2->setValue(0);
this->ui->progressBar_2->setVisible(true);
if (this->file)
{
this->file->close();
delete this->file;
this->file = nullptr;
}
this->Write("Downloading " + this->inst->Source + "...");
delete this->manager;
this->manager = new QNetworkAccessManager(this);
this->file = new QFile(download_target + this->inst->Destination);
QFileInfo info(download_target + this->inst->Destination);
if (!QDir().exists(info.absoluteDir().path()) && !QDir().mkpath(info.absoluteDir().path()))
{
this->Fail("Unable to create folder " + info.absoluteDir().path());
return;
}
if (!this->file->open(QIODevice::ReadWrite))
{
this->Fail("Unable to open " + info.absolutePath() + " for writing");
return;
}
this->url = QUrl(this->inst->Source);
this->reply = this->manager->get(QNetworkRequest(this->url));
connect(this->reply, SIGNAL(finished()), this, SLOT(httpDownloadFinished()));
connect(this->reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
connect(this->reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64,qint64)));
}
void UpdateForm::Write(QString message)
{
LOG(message);
this->ui->label->setText(message);
}
QString UpdateForm::MD5(QString file)
{
QFile *source_ = new QFile(file);
if (!source_->open(QIODevice::ReadOnly))
{
this->Fail("Unable to open " + file);
}
QByteArray data = source_->readAll();
source_->close();
delete source_;
return QString(QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex());
}
void UpdateForm::NextInstruction()
{
#if QT_VERSION >= 0x050000
if (this->inst)
return;
if (this->Instructions.count() == 0)
{
this->ui->pushButton_2->setEnabled(true);
this->Write("Update was successfully finished, you can start huggle now by closing this form!");
this->timer->stop();
return;
}
int value = this->ui->progressBar->value();
if (this->ui->progressBar->maximum() > value)
this->ui->progressBar->setValue(value+1);
// we read the instruction and process it
this->inst = this->Instructions.at(0);
this->Instructions.removeAt(0);
switch (this->inst->Type)
{
case Instruction_Download:
ProcessDownload();
return;
case Instruction_Folder:
if (!QDir().mkpath(this->Path(this->inst->Source)))
{
this->Fail("Unable to create folder " + this->Path(this->inst->Source));
}
break;
case Instruction_Delete:
{
QString path = this->Path(this->inst->Source);
this->Write("Deleting: " + path);
if (path.isEmpty())
{
this->Fail("Refusing to delete everything in folder");
break;
}
if (this->inst->Is_Recursive)
{
if (!QDir(path).removeRecursively())
{
this->Fail("Unable to delete: " + path);
break;
}
} else
{
if (!QFile(path).remove())
{
this->Fail("Unable to delete: " + path);
break;
}
}
}
break;
case Instruction_Copy:
{
QString from = this->Path(this->inst->Source);
QString to = this->Path(this->inst->Destination);
this->Write("Copying " + from + " to " + to);
if (this->inst->Is_Recursive)
{
// first recreate the hierarchy of data structures
QStringList folders;
QStringList files_to;
QStringList files_from;
QDir target(to);
QDir source(from);
if (!target.exists() && !target.mkpath(to))
{
this->Fail("Unable to create folder " + to);
break;
}
if (!source.exists())
{
this->Fail("Source " + from + " doesn't exist");
break;
}
recurseAddDirWithSources(source, files_to, files_from, to, from, folders);
foreach (QString directory, folders)
{
LOG("Creating folder " + directory);
if (!QDir().mkpath(directory))
{
this->Fail("Failed to create " + directory);
return;
}
}
int x = 0;
while (x < files_from.count())
{
QString fromf = files_from.at(x);
QString tof = files_to.at(x);
LOG("Copying " + fromf + " to " + tof);
if (!QFile().copy(fromf, tof))
{
this->Fail("Unable to copy " + fromf + " to " + tof);
return;
}
x++;
}
} else
{
if (!QFile().copy(from, to))
{
this->Fail("Unable to copy from " + from + " to " + to);
break;
}
}
}
break;
case Instruction_Execute:
this->Fail("Execute is not implemented yet");
break;
case Instruction_Move:
this->Fail("Move is not implemented yet");
break;
}
delete this->inst;
this->inst = nullptr;
#endif
return;
}
void UpdateForm::Update()
{
this->TempPath = QApplication::applicationDirPath() + QDir::separator();
connect(this->timer, SIGNAL(timeout()), this, SLOT(OnTick()));
LOG("Reading manifest");
QFile file(QApplication::applicationDirPath() + "/updater.xml");
if (!file.open(QIODevice::ReadOnly))
{
this->Fail("Unable to open the manifest file @ " + QApplication::applicationDirPath() + "/updater.xml");
return;
}
this->Manifest = QString(file.readAll());
if (!this->ProcessManifest(this->Manifest))
{
this->Fail("Manifest can't be read");
return;
}
file.close();
this->ui->progressBar->setMaximum(this->Instructions.count());
LOG("Processing update");
this->timer->start(HUGGLE_TIMER);
}
void UpdateForm::Generic_Exec(QString path, QStringList parameters)
{
QProcess *ClientProcess = new QProcess(this);
ClientProcess->startDetached(path, parameters);
}
QString UpdateForm::Path(QString text)
{
QString path = text;
path = path.replace("$root_bck", this->TempPath)
.replace("$root", this->RootPath)
.replace("$temp", this->TempPath + "/temp");
return TrimSlashes(path);
}
Instruction::Instruction(InstructionType type, QString from, QString to, bool is_elevated, bool is_recursive, bool merge, bool is_overwriting, QString md5)
{
this->Elevated = is_elevated;
this->Overwrite = is_overwriting;
this->Destination = to;
this->Source = from;
this->Hash_MD5 = md5;
this->Is_Merging = merge;
this->Is_Recursive = is_recursive;
this->Type = type;
}