-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
540 lines (434 loc) · 12.9 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
#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 <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 <iostream>
#ifdef WIN32
#define FEBIOBINARY "\\febio4.exe"
#define FBSBINARY "\\FEBioStudio2.exe"
#define FBSUPDATERBINARY "\\FEBioStudioUpdater.exe"
#define MVUTIL "\\mvUtil.exe"
#elif __APPLE__
#define FEBIOBINARY "/febio4"
#define FBSBINARY "/FEBioStudio"
#define FBSUPDATERBINARY "/FEBioStudioUpdater"
#define MVUTIL "/mvUtil"
#else
#define FEBIOBINARY "/febio4"
#define FBSBINARY "/FEBioStudio"
#define FBSUPDATERBINARY "/FEBioStudioUpdater"
#define MVUTIL "/mvUtil"
#endif
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;
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;
};
CMainWindow::CMainWindow(bool devChannel, bool updaterUpdateCheck)
: ui(new Ui::CMainWindow), restclient(new QNetworkAccessManager), m_devChannel(devChannel), m_updaterUpdateCheck(updaterUpdateCheck)
{
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;
}
}
}
}
bool CMainWindow::NetworkAccessibleCheck()
{
// return restclient->networkAccessible() == QNetworkAccessManager::Accessible;
return true;
}
void CMainWindow::getFile()
{
ui->downloadFileLabel->setText(QString("Downloading %1...").arg(ui->updateWidget->updateFiles[ui->updateWidget->currentIndex]));
ui->downloadOverallLabel->setText(QString("Downloading File %1 of %2").arg(ui->updateWidget->currentIndex + 1).arg(ui->updateWidget->updateFiles.size()));
QUrl myurl;
myurl.setScheme(ServerSettings::Scheme());
myurl.setHost(ServerSettings::URL());
myurl.setPort(ServerSettings::Port());
if(ui->updateWidget->doingUpdaterUpdate)
{
myurl.setPath(ui->updateWidget->updaterBase + "/" + ui->updateWidget->updateFiles[ui->updateWidget->currentIndex]);
}
else
{
myurl.setPath(ui->updateWidget->urlBase + "/" + ui->updateWidget->updateFiles[ui->updateWidget->currentIndex]);
}
QNetworkRequest request;
request.setUrl(myurl);
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::SameOriginRedirectPolicy);
if(NetworkAccessibleCheck())
{
QNetworkReply* reply = restclient->get(request);
QObject::connect(reply, &QNetworkReply::downloadProgress, this, &CMainWindow::progress);
}
}
void CMainWindow::getFileReponse(QNetworkReply *r)
{
int statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(statusCode != 200)
{
QMessageBox::critical(this, "Update Failed", QString("Update Failed!\n\nUnable to download %1.").arg(ui->updateWidget->updateFiles[ui->updateWidget->currentIndex]));
QApplication::quit();
return;
}
QByteArray data = r->readAll();
QFileInfo fileInfo(QApplication::applicationDirPath() + QString(REL_ROOT) + ui->updateWidget->updateFiles[ui->updateWidget->currentIndex]);
// 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();
// If the file doesn't already exist, add it to autoUpdate.xml for deletion during uninstalltion.
if(!QFile::exists(fileName)) ui->updateWidget->newFiles.append(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);
ui->updateWidget->currentIndex++;
ui->updateWidget->downloadedSize += data.size();
if(ui->updateWidget->currentIndex < ui->updateWidget->updateFiles.size())
{
getFile();
}
else
{
downloadsFinished();
}
}
void CMainWindow::initializePage(int id)
{
switch(id)
{
case 1:
ui->updateWidget->checkForUpdate(m_devChannel, m_updaterUpdateCheck);
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);
}
void CMainWindow::connFinished(QNetworkReply *r)
{
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->updateWidget->downloadedSize)/(float)ui->updateWidget->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->updateWidget->newDirs.append(dir.absolutePath());
}
void CMainWindow::downloadsFinished()
{
QStringList oldFiles;
QStringList oldDirs;
readXML(oldFiles, oldDirs);
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->updateWidget->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->updateWidget->newFiles)
{
writer.add_leaf("file", file.toStdString().c_str());
}
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");
for(auto filename : ui->updateWidget->updateFiles)
{
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();
}