2014-05-17 07:10:16 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2013 The ioquake Group
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2014-05-13 03:28:20 +00:00
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
2014-05-12 09:35:28 +00:00
|
|
|
#include "installwizard_patch.h"
|
|
|
|
#include "ui_installwizard_patch.h"
|
2014-05-13 03:28:20 +00:00
|
|
|
#include "installwizard.h"
|
2014-05-18 11:41:54 +00:00
|
|
|
#include "fileextract.h"
|
2014-05-13 09:34:15 +00:00
|
|
|
|
2014-05-12 09:35:28 +00:00
|
|
|
InstallWizard_Patch::InstallWizard_Patch(QWidget *parent) :
|
|
|
|
QWizardPage(parent),
|
2014-05-13 03:28:20 +00:00
|
|
|
ui(new Ui::InstallWizard_Patch),
|
|
|
|
patchFile(NULL),
|
|
|
|
networkReply(NULL),
|
|
|
|
isCancelled(false),
|
2014-05-13 04:24:23 +00:00
|
|
|
isDownloadFinished(false),
|
2014-05-13 09:34:15 +00:00
|
|
|
isPatchInstalled(false),
|
2014-05-18 11:41:54 +00:00
|
|
|
extractWorker(NULL),
|
|
|
|
isExtractFinished(false)
|
2014-05-12 09:35:28 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
InstallWizard_Patch::~InstallWizard_Patch()
|
|
|
|
{
|
2014-05-18 11:41:54 +00:00
|
|
|
extractThread.quit();
|
|
|
|
extractThread.wait();
|
2014-05-13 03:28:20 +00:00
|
|
|
delete patchFile;
|
2014-05-12 09:35:28 +00:00
|
|
|
delete ui;
|
|
|
|
}
|
2014-05-13 03:28:20 +00:00
|
|
|
|
|
|
|
void InstallWizard_Patch::initializePage()
|
|
|
|
{
|
2014-05-18 11:41:54 +00:00
|
|
|
isCancelled = isDownloadFinished = isPatchInstalled = isExtractFinished = false;
|
|
|
|
extractWorker = NULL;
|
2014-05-13 03:28:20 +00:00
|
|
|
|
|
|
|
patchFile = new QTemporaryFile;
|
|
|
|
patchFile->open();
|
|
|
|
|
2014-05-28 08:30:02 +00:00
|
|
|
#ifdef QT_DEBUG
|
|
|
|
QNetworkRequest networkRequest(QUrl("http://localhost:8080/quake3-latest-pk3s.zip"));
|
|
|
|
#else
|
|
|
|
QNetworkRequest networkRequest(QUrl("http://ioquake3.org/data/quake3-latest-pk3s.zip"));
|
|
|
|
networkRequest.setRawHeader("Referer", "http://ioquake3.org/extras/patch-data/");
|
|
|
|
//networkRequest.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
networkReply = nam.get(networkRequest);
|
2014-05-17 07:16:04 +00:00
|
|
|
connect(networkReply, &QNetworkReply::readyRead, this, &InstallWizard_Patch::downloadRead);
|
2014-05-18 11:41:54 +00:00
|
|
|
connect(networkReply, &QNetworkReply::downloadProgress, this, &InstallWizard_Patch::updateProgress);
|
2014-05-17 07:16:04 +00:00
|
|
|
connect(networkReply, &QNetworkReply::finished, this, &InstallWizard_Patch::downloadFinished);
|
2014-05-13 03:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstallWizard_Patch::cleanupPage()
|
|
|
|
{
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstallWizard_Patch::isComplete() const
|
|
|
|
{
|
2014-05-18 11:41:54 +00:00
|
|
|
return isDownloadFinished && isExtractFinished && isPatchInstalled;
|
2014-05-13 03:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstallWizard_Patch::cancel()
|
|
|
|
{
|
|
|
|
if (isCancelled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
delete patchFile;
|
|
|
|
patchFile = NULL;
|
2014-05-13 05:29:54 +00:00
|
|
|
|
|
|
|
if (!isDownloadFinished)
|
|
|
|
networkReply->abort();
|
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
if (!isExtractFinished)
|
|
|
|
{
|
|
|
|
extractWorker->cancel();
|
|
|
|
}
|
|
|
|
|
2014-05-13 03:28:20 +00:00
|
|
|
isCancelled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallWizard_Patch::downloadRead()
|
|
|
|
{
|
2014-05-28 08:30:02 +00:00
|
|
|
patchFile->write(networkReply->readAll());
|
2014-05-13 03:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstallWizard_Patch::downloadFinished()
|
|
|
|
{
|
|
|
|
Q_ASSERT(networkReply);
|
|
|
|
|
|
|
|
if (!isCancelled && networkReply->error())
|
|
|
|
{
|
|
|
|
ui->lblStatus->setText(networkReply->errorString());
|
2014-05-15 10:01:31 +00:00
|
|
|
networkReply->abort();
|
|
|
|
networkReply->deleteLater();
|
|
|
|
isCancelled = true;
|
|
|
|
return;
|
2014-05-13 03:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
networkReply->deleteLater();
|
2014-05-13 07:23:47 +00:00
|
|
|
|
|
|
|
if (isCancelled)
|
|
|
|
return;
|
|
|
|
|
2014-05-28 08:30:02 +00:00
|
|
|
patchFile->flush();
|
2014-05-13 03:28:20 +00:00
|
|
|
isDownloadFinished = true;
|
2014-05-13 07:23:47 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
// Build a list of pak files to extract.
|
|
|
|
QList<FileOperation> filesToExtract;
|
2014-05-13 07:23:47 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
for (int i = 1; i <= 8; i++)
|
2014-05-13 07:23:47 +00:00
|
|
|
{
|
2014-05-18 11:41:54 +00:00
|
|
|
FileOperation fo;
|
2014-05-28 08:30:02 +00:00
|
|
|
fo.source = QString("quake3-latest-pk3s/baseq3/pak%1.pk3").arg(i);
|
2014-05-18 11:41:54 +00:00
|
|
|
fo.dest = QString("%1/baseq3/pak%2.pk3").arg(((InstallWizard *)wizard())->getQuakePath()).arg(i);
|
|
|
|
filesToExtract.append(fo);
|
2014-05-13 07:23:47 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
// Start extract thread.
|
|
|
|
qRegisterMetaType<QList<FileOperation> >("QList<FileOperation>");
|
|
|
|
extractWorker = new FileExtractWorker(patchFile->fileName(), filesToExtract);
|
|
|
|
extractWorker->moveToThread(&extractThread);
|
|
|
|
connect(&extractThread, &QThread::finished, extractWorker, &QObject::deleteLater);
|
|
|
|
connect(this, &InstallWizard_Patch::extract, extractWorker, &FileExtractWorker::extract);
|
|
|
|
connect(extractWorker, &FileExtractWorker::fileChanged, this, &InstallWizard_Patch::setExtractFilename);
|
|
|
|
connect(extractWorker, &FileExtractWorker::progressChanged, this, &InstallWizard_Patch::updateProgress);
|
|
|
|
connect(extractWorker, &FileExtractWorker::errorMessage, this, &InstallWizard_Patch::setErrorMessage);
|
|
|
|
connect(extractWorker, &FileExtractWorker::finished, this, &InstallWizard_Patch::finishExtract);
|
|
|
|
extractThread.start();
|
|
|
|
emit extract();
|
|
|
|
}
|
2014-05-13 07:23:47 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
void InstallWizard_Patch::updateProgress(qint64 bytesRead, qint64 bytesTotal)
|
|
|
|
{
|
|
|
|
if (!isDownloadFinished)
|
2014-05-13 07:23:47 +00:00
|
|
|
{
|
2014-05-18 11:41:54 +00:00
|
|
|
ui->lblStatus->setText(QString("Downloading %1MB / %2MB").arg(bytesRead / 1024.0 / 1024.0, 0, 'f', 2).arg(bytesTotal / 1024.0 / 1024.0, 0, 'f', 2));
|
2014-05-13 07:23:47 +00:00
|
|
|
}
|
2014-05-18 11:41:54 +00:00
|
|
|
else if (!isExtractFinished)
|
2014-05-13 09:34:15 +00:00
|
|
|
{
|
2014-05-18 11:41:54 +00:00
|
|
|
ui->lblStatus->setText(QString("Extracting %1 %2MB / %3MB").arg(extractFilename).arg(bytesRead / 1024.0 / 1024.0, 0, 'f', 2).arg(bytesTotal / 1024.0 / 1024.0, 0, 'f', 2));
|
|
|
|
}
|
2014-05-13 09:34:15 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
ui->pbProgress->setMaximum((int)bytesTotal);
|
|
|
|
ui->pbProgress->setValue((int)bytesRead);
|
|
|
|
}
|
2014-05-13 09:34:15 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
void InstallWizard_Patch::setExtractFilename(const QString &filename)
|
|
|
|
{
|
|
|
|
extractFilename = filename;
|
|
|
|
}
|
2014-05-13 09:34:15 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
void InstallWizard_Patch::setErrorMessage(const QString &message)
|
|
|
|
{
|
|
|
|
ui->lblStatus->setText(message);
|
|
|
|
}
|
2014-05-13 09:34:15 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
void InstallWizard_Patch::finishExtract(QList<FileOperation> renameOperations)
|
|
|
|
{
|
|
|
|
extractThread.quit();
|
|
|
|
extractThread.wait();
|
|
|
|
isExtractFinished = true;
|
|
|
|
emit completeChanged();
|
2014-05-13 09:34:15 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
// Complete the transaction.
|
|
|
|
const QString transactionError = FileUtils::completeTransaction(renameOperations);
|
2014-05-13 09:34:15 +00:00
|
|
|
|
2014-05-18 11:41:54 +00:00
|
|
|
if (!transactionError.isEmpty())
|
|
|
|
{
|
|
|
|
ui->lblStatus->setText(transactionError);
|
|
|
|
return;
|
2014-05-13 09:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isPatchInstalled = true;
|
2014-05-13 03:28:20 +00:00
|
|
|
emit completeChanged();
|
2014-05-15 03:54:18 +00:00
|
|
|
wizard()->next();
|
2014-05-13 03:28:20 +00:00
|
|
|
}
|