-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathMainWindow.cpp
711 lines (556 loc) · 17.2 KB
/
MainWindow.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
#include <vector>
#include <QApplication>
#include <QProcess>
#include <QDialog>
#include <QDialogButtonBox>
#include <QWizardPage>
#include <QPushButton>
#include <QLabel>
#include <QPixmap>
#include <QAbstractButton>
#include <QMessageBox>
#include <QListWidget>
#include <QTextEdit>
#include <QBoxLayout>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QSslError>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFileInfo>
#include <QDir>
#include <QProgressBar>
#include <QCheckBox>
#include <QDateTime>
#include <QSaveFile>
#include <QDebug>
#include <QLocale>
#include <QStringList>
#include <QXmlStreamReader>
#include "FEBioStudioUpdater.h"
#include "MainWindow.h"
#include <XML/XMLWriter.h>
#include <FEBioStudio/UpdateChecker.h>
#include <FEBioStudio/ServerSettings.h>
#include "ZipThread.h"
#include <iostream>
namespace Ui
{
class MyWizardPage : public QWizardPage
{
public:
MyWizardPage()
{
complete = false;
emit completeChanged();
}
bool isComplete() const
{
return complete;
}
void setComplete(bool comp)
{
complete = comp;
emit completeChanged();
}
private:
bool complete;
};
}
class Ui::CMainWindow
{
public:
QWizardPage* startPage;
MyWizardPage* infoPage;
QVBoxLayout* infoLayout;
::CUpdateWidget* updateWidget;
MyWizardPage* downloadPage;
QLabel* downloadOverallLabel;
QProgressBar* overallProgress;
QLabel* downloadFileLabel;
QProgressBar* fileProgress;
QCheckBox* relaunch;
CMainWindow() : currentIndex(0), currentZippedIndex(0), overallSize(0), downloadedSize(0) { }
void setup(::CMainWindow* wnd, bool correctDir)
{
m_wnd = wnd;
// Start page
startPage = new QWizardPage;
QVBoxLayout* startLayout = new QVBoxLayout;
if(correctDir)
{
startLayout->addWidget(new QLabel("Welcome to the FEBio Studio Auto-Updater.\n\nClick Next to check for updates."));
}
else
{
startLayout->addWidget(new QLabel("This updater does not appear to be located in the 'bin' folder of an FEBio Studio "
"installation.\n\nUpdate cannot proceed."));
}
startPage->setLayout(startLayout);
wnd->addPage(startPage);
if(correctDir)
{
// Info page
infoPage = new MyWizardPage;
infoLayout = new QVBoxLayout;
infoLayout->addWidget(updateWidget = new ::CUpdateWidget);
infoPage->setLayout(infoLayout);
wnd->addPage(infoPage);
downloadPage = new MyWizardPage;
QVBoxLayout* downloadLayout = new QVBoxLayout;
downloadLayout->addWidget(downloadFileLabel = new QLabel);
downloadLayout->addWidget(fileProgress = new QProgressBar);
downloadLayout->addWidget(downloadOverallLabel= new QLabel);
downloadLayout->addWidget(overallProgress = new QProgressBar);
downloadPage->setLayout(downloadLayout);
wnd->addPage(downloadPage);
}
relaunch = nullptr;
QObject::connect(updateWidget, &::CUpdateWidget::ready, m_wnd, &::CMainWindow::updateWidgetReady);
}
void downloadsFinished()
{
fileProgress->hide();
downloadOverallLabel->hide();
overallProgress->hide();
if(updateWidget->doingUpdaterUpdate)
{
downloadFileLabel->setText("Auto-Updater update complete!\n\nThe updater must now restart to update FEBio Studio.\n\n"
"Click Finish to restart the updater and start the update to FEBio Studio.");
downloadFileLabel->setWordWrap(true);
}
else
{
downloadFileLabel->setText("Update Complete!");
downloadPage->layout()->addWidget(relaunch = new QCheckBox("Relaunch FEBio Studio."));
relaunch->setChecked(true);
}
downloadPage->setComplete(true);
QList<QWizard::WizardButton> button_layout;
button_layout << QWizard::Stretch << QWizard::FinishButton;
m_wnd->setButtonLayout(button_layout);
}
public:
::CMainWindow* m_wnd;
QStringList newFiles;
QStringList newDirs;
int currentIndex;
int currentZippedIndex;
std::vector<int> zippedFiles;
qint64 overallSize;
qint64 downloadedSize;
};
CMainWindow::CMainWindow(bool devChannel, bool updaterUpdateCheck, QString& branch)
: ui(new Ui::CMainWindow), restclient(new QNetworkAccessManager), m_devChannel(devChannel),
m_updaterUpdateCheck(updaterUpdateCheck), m_downloadingSDK(false), m_branch(branch)
{
QString dir = QApplication::applicationDirPath();
bool correctDir = false;
#ifdef WIN32
if(dir.contains("FEBio") && dir.contains("Studio") && dir.contains("bin"))
{
correctDir = true;
}
#elif __APPLE__
if(dir.contains("FEBio") && dir.contains("Studio") && dir.contains("MacOS"))
{
correctDir = true;
}
#else
if(dir.contains("FEBio") && dir.contains("Studio") && dir.contains("bin"))
{
correctDir = true;
}
#endif
setWindowTitle("FEBio Studio Updater");
setWizardStyle(QWizard::ModernStyle);
setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/leftSide.png"));
setPixmap(QWizard::BackgroundPixmap, QPixmap(":/images/leftSide.png"));
setPixmap(QWizard::LogoPixmap, QPixmap(":/images/FEBioStudio.png"));
ui->setup(this, correctDir);
QList<QWizard::WizardButton> button_layout;
button_layout << QWizard::Stretch << QWizard::NextButton << QWizard::CancelButton;
setButtonLayout(button_layout);
connect(this->button(QWizard::FinishButton), &QPushButton::clicked, this, &CMainWindow::onFinish);
connect(restclient, &QNetworkAccessManager::finished, this, &CMainWindow::connFinished);
connect(restclient, &QNetworkAccessManager::sslErrors, this, &CMainWindow::sslErrorHandler);
}
bool CMainWindow::checkBinaries()
{
#ifdef WIN32
if(!isFileWriteable(QApplication::applicationDirPath() + FBSBINARY, "FEBio Studio"))
{
return false;
}
if(!isFileWriteable(QApplication::applicationDirPath() + FEBIOBINARY, "FEBio"))
{
return false;
}
#endif
return true;
}
bool CMainWindow::isFileWriteable(QString filename, QString niceName)
{
QDialog dlg;
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(new QLabel(QString("Cannot write to %1.\n\nIs %2 still running?").arg(filename).arg(niceName)));
QDialogButtonBox* box = new QDialogButtonBox;
box->addButton("Retry", QDialogButtonBox::AcceptRole);
box->addButton(QDialogButtonBox::Cancel);
layout->addWidget(box);
dlg.setLayout(layout);
connect(box, &QDialogButtonBox::accepted, &dlg, &QDialog::accept);
connect(box, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
QFile file(filename);
while(true)
{
if(file.open(QIODevice::Append))
{
file.close();
return true;
}
else
{
if(!dlg.exec())
{
return false;
}
}
}
}
void CMainWindow::getFile()
{
ReleaseFile& currentFile = ui->updateWidget->updateFiles[ui->currentIndex];
ui->downloadFileLabel->setText(QString("Downloading %1...").arg(currentFile.name));
ui->downloadOverallLabel->setText(QString("Downloading File %1 of %2").arg(ui->currentIndex + 1).arg(ui->updateWidget->updateFiles.size()));
QUrl myurl;
myurl.setScheme(ServerSettings::Scheme());
myurl.setHost(ServerSettings::URL());
myurl.setPort(ServerSettings::Port());
myurl.setPath(currentFile.baseURL + "/" + currentFile.name);
if(!m_branch.isEmpty())
{
QUrlQuery query;
query.addQueryItem("branch", m_branch);
myurl.setQuery(query);
}
QNetworkRequest request;
request.setUrl(myurl);
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::SameOriginRedirectPolicy);
QNetworkReply* reply = restclient->get(request);
QObject::connect(reply, &QNetworkReply::downloadProgress, this, &CMainWindow::progress);
}
void CMainWindow::getFileReponse(QNetworkReply *r)
{
ReleaseFile& currentFile = ui->updateWidget->updateFiles[ui->currentIndex];
int statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(statusCode != 200)
{
QMessageBox::critical(this, "Update Failed", QString("Update Failed!\n\nUnable to download %1.").arg(currentFile.name));
QApplication::quit();
return;
}
QByteArray data = r->readAll();
QFileInfo fileInfo(QApplication::applicationDirPath() + QString(REL_ROOT) + currentFile.name);
// Ensure that the path to the file exists. Add any newly created directories to autoUpdate.xml
// for deletion during uninstalltion
makePath(QDir::fromNativeSeparators(fileInfo.absolutePath()));
QString fileName = fileInfo.absoluteFilePath();
addNewFile(fileName);
// If we're downloading an updater file, add the suffix ".temp"
if(ui->updateWidget->doingUpdaterUpdate)
{
if(!fileName.contains("mvUtil"))
{
fileName += ".temp";
}
}
QSaveFile file(fileName);
file.open(QIODevice::WriteOnly);
file.write(data);
file.commit();
file.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner | QFileDevice::WriteUser | QFileDevice::ExeUser | QFileDevice::ReadUser);
if(currentFile.name.endsWith(".zip"))
{
ui->zippedFiles.push_back(ui->currentIndex);
}
ui->currentIndex++;
ui->downloadedSize += data.size();
if(ui->currentIndex < ui->updateWidget->updateFiles.size())
{
getFile();
}
else
{
unzipFiles();
}
}
void CMainWindow::unzipFiles()
{
if(ui->currentZippedIndex < ui->zippedFiles.size())
{
ReleaseFile& currentFile = ui->updateWidget->updateFiles[ui->zippedFiles[ui->currentZippedIndex]];
ui->downloadOverallLabel->hide();
ui->overallProgress->hide();
ui->downloadFileLabel->setText(QString("Unzipping %1...").arg(currentFile.name));
QFileInfo info(QApplication::applicationDirPath() + QString(REL_ROOT) + currentFile.name);
QString zipFileName = info.absoluteFilePath();
QString outDir = info.absolutePath();
ZipThread* thread = new ZipThread(this, zipFileName, outDir);
connect(thread, &ZipThread::progress, this, &CMainWindow::progress);
connect(thread, &ZipThread::resultReady, this, &CMainWindow::unzipFinished);
thread->run();
}
else
{
if(ui->updateWidget->m_getSDK && ui->updateWidget->m_getSDK->isChecked())
{
getSDK();
}
else
{
downloadsFinished();
}
}
}
void CMainWindow::unzipFinished()
{
ReleaseFile& currentFile = ui->updateWidget->updateFiles[ui->zippedFiles[ui->currentZippedIndex]];
QFile zip = QApplication::applicationDirPath() + QString(REL_ROOT) + currentFile.name;
if(zip.exists())
{
zip.remove();
}
ui->currentZippedIndex++;
unzipFiles();
}
void CMainWindow::getSDK()
{
ui->downloadOverallLabel->hide();
ui->overallProgress->hide();
ui->downloadFileLabel->setText("Downloading SDK...");
QUrl myurl;
myurl.setScheme(ServerSettings::Scheme());
myurl.setHost(ServerSettings::URL());
myurl.setPort(ServerSettings::Port());
myurl.setPath(ui->updateWidget->m_sdk.baseURL + "/" + ui->updateWidget->m_sdk.name);
QNetworkRequest request;
request.setUrl(myurl);
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::SameOriginRedirectPolicy);
m_downloadingSDK = true;
QNetworkReply* reply = restclient->get(request);
QObject::connect(reply, &QNetworkReply::downloadProgress, this, &CMainWindow::progress);
}
void CMainWindow::getSDKResponse(QNetworkReply *r)
{
int statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(statusCode != 200)
{
QMessageBox::critical(this, "Update Failed", "Update Failed!\n\nUnable to download SDK.");
QApplication::quit();
return;
}
QByteArray data = r->readAll();
QFileInfo zipFileInfo(QApplication::applicationDirPath() + QString(REL_ROOT) + "sdk.zip");
QString zipFileName = zipFileInfo.absoluteFilePath();
QFileInfo sdkDir(QApplication::applicationDirPath() + QString(REL_ROOT) + "sdk");
QString sdkDirName = sdkDir.absoluteFilePath();
// If the sdk dir doesn't already exist, create it and add it to autoUpdate.xml for deletion during uninstalltion.
makePath(QDir::fromNativeSeparators(sdkDirName));
QSaveFile file(zipFileName);
file.open(QIODevice::WriteOnly);
file.write(data);
file.commit();
file.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner | QFileDevice::WriteUser | QFileDevice::ExeUser | QFileDevice::ReadUser);
ui->downloadFileLabel->setText("Unzipping SDK...");
ZipThread* thread = new ZipThread(this, zipFileName, sdkDirName);
connect(thread, &ZipThread::progress, this, &CMainWindow::progress);
connect(thread, &ZipThread::resultReady, this, &CMainWindow::unzipSDKFinished);
thread->run();
}
void CMainWindow::unzipSDKFinished()
{
QFile zip(QApplication::applicationDirPath() + QString(REL_ROOT) + "sdk.zip");
if(zip.exists())
{
zip.remove();
}
downloadsFinished();
}
void CMainWindow::initializePage(int id)
{
switch(id)
{
case 1:
ui->updateWidget->checkForUpdate(m_devChannel, true, m_updaterUpdateCheck, m_branch);
break;
case 2:
deleteFiles();
getFile();
break;
default:
break;
}
}
void CMainWindow::updateWidgetReady(bool update, bool terminal)
{
if(!update || terminal)
{
this->removePage(2);
QList<QWizard::WizardButton> button_layout;
button_layout << QWizard::Stretch << QWizard::FinishButton;
setButtonLayout(button_layout);
}
else
{
if(!checkBinaries())
{
QApplication::quit();
}
}
ui->infoPage->setComplete(true);
ui->overallSize = ui->updateWidget->overallSize;
}
void CMainWindow::connFinished(QNetworkReply *r)
{
if(m_downloadingSDK)
{
getSDKResponse(r);
}
else
{
getFileReponse(r);
}
}
void CMainWindow::sslErrorHandler(QNetworkReply *reply, const QList<QSslError> &errors)
{
for(QSslError error : errors)
{
qDebug() << error.errorString();
}
reply->ignoreSslErrors();
}
void CMainWindow::progress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->overallProgress->setValue((float)(bytesReceived + ui->downloadedSize)/(float)ui->overallSize*100);
ui->fileProgress->setValue((float)bytesReceived/(float)bytesTotal*100);
}
void CMainWindow::deleteFiles()
{
for(auto file : ui->updateWidget->deleteFiles)
{
QFile::remove(QApplication::applicationDirPath() + QString(REL_ROOT) + file);
}
}
void CMainWindow::makePath(QString path)
{
QDir dir(path);
if(dir.exists())
{
return;
}
QString parentPath = path.left(path.lastIndexOf("/"));
makePath(parentPath);
dir.mkdir(dir.absolutePath());
ui->newDirs.append(dir.absolutePath());
}
void CMainWindow::addNewFile(const QString filename)
{
// If the file doesn't already exist, add it to autoUpdate.xml for deletion during uninstalltion.
if(!QFile::exists(filename)) ui->newFiles.append(filename);
}
void CMainWindow::downloadsFinished()
{
// Run install commands
for(auto cmd : ui->updateWidget->installCmds)
{
std::system(cmd.toStdString().c_str());
}
QStringList oldFiles;
QStringList oldDirs;
QStringList oldCmds;
readXML(oldFiles, oldDirs, oldCmds);
XMLWriter writer;
writer.open(QString(QApplication::applicationDirPath() + "/autoUpdate.xml").toStdString().c_str());
XMLElement root("autoUpdate");
writer.add_branch(root);
// If we're doing an updater update, don't update the last update time in autoUpdate.xml
if(m_updaterUpdateCheck && ui->updateWidget->doingUpdaterUpdate)
{
writer.add_leaf("lastUpdate", std::to_string(ui->updateWidget->lastUpdate));
}
else
{
writer.add_leaf("lastUpdate", std::to_string(ui->updateWidget->serverTime));
}
for(auto dir : oldDirs)
{
writer.add_leaf("dir", dir.toStdString().c_str());
}
for(auto dir : ui->newDirs)
{
writer.add_leaf("dir", dir.toStdString().c_str());
}
for(auto file : oldFiles)
{
writer.add_leaf("file", file.toStdString().c_str());
}
for(auto file : ui->newFiles)
{
writer.add_leaf("file", file.toStdString().c_str());
}
if(!oldCmds.isEmpty() || !ui->updateWidget->uninstallCmds.isEmpty())
{
XMLElement uninstallCmds("uninstallCmds");
writer.add_branch(uninstallCmds);
for(auto cmd : oldCmds)
{
writer.add_leaf("cmd", cmd.toStdString().c_str());
}
for(auto cmd : ui->updateWidget->uninstallCmds)
{
writer.add_leaf("cmd", cmd.toStdString().c_str());
}
writer.close_branch();
}
writer.close_branch();
writer.close();
ui->downloadsFinished();
}
void CMainWindow::onFinish()
{
if(ui->updateWidget->doingUpdaterUpdate)
{
QStringList args;
args.push_back(QApplication::applicationDirPath() + FBSUPDATERBINARY);
if(m_devChannel) args.push_back("-d");
if(!m_branch.isEmpty())
{
args.push_back("-b");
args.push_back(m_branch);
}
for(auto file : ui->updateWidget->updateFiles)
{
QString& filename = file.name;
if(filename.contains("mvUtil")) continue;
QFileInfo fileInfo(QApplication::applicationDirPath() + QString(REL_ROOT) + filename);
QString absName = fileInfo.absoluteFilePath();
args.push_back(absName + ".temp");
args.push_back(absName);
}
QProcess* mvUtil = new QProcess;
mvUtil->startDetached(QApplication::applicationDirPath() + MVUTIL, args);
}
else
{
if(ui->relaunch && ui->relaunch->isChecked())
{
QProcess* fbs = new QProcess;
fbs->startDetached(QApplication::applicationDirPath() + FBSBINARY, QStringList());
}
}
QWizard::accept();
}