launch/installwizard_patch.cpp

199 lines
6.2 KiB
C++
Raw Normal View History

/*
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>
#include "installwizard_patch.h"
#include "ui_installwizard_patch.h"
2014-05-13 03:28:20 +00:00
#include "installwizard.h"
#include "fileextract.h"
2014-05-13 09:34:15 +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),
isDownloadFinished(false),
2014-05-13 09:34:15 +00:00
isPatchInstalled(false),
extractWorker(NULL),
isExtractFinished(false)
{
ui->setupUi(this);
}
InstallWizard_Patch::~InstallWizard_Patch()
{
extractThread.quit();
extractThread.wait();
2014-05-13 03:28:20 +00:00
delete patchFile;
delete ui;
}
2014-05-13 03:28:20 +00:00
void InstallWizard_Patch::initializePage()
{
isCancelled = isDownloadFinished = isPatchInstalled = isExtractFinished = false;
extractWorker = NULL;
2014-05-13 03:28:20 +00:00
patchFile = new QTemporaryFile;
patchFile->open();
#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);
connect(networkReply, &QNetworkReply::readyRead, this, &InstallWizard_Patch::downloadRead);
connect(networkReply, &QNetworkReply::downloadProgress, this, &InstallWizard_Patch::updateProgress);
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
{
return isDownloadFinished && isExtractFinished && isPatchInstalled;
2014-05-13 03:28:20 +00:00
}
void InstallWizard_Patch::cancel()
{
if (isCancelled)
return;
delete patchFile;
patchFile = NULL;
if (!isDownloadFinished)
networkReply->abort();
if (!isExtractFinished)
{
extractWorker->cancel();
}
2014-05-13 03:28:20 +00:00
isCancelled = true;
}
void InstallWizard_Patch::downloadRead()
{
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;
patchFile->flush();
2014-05-13 03:28:20 +00:00
isDownloadFinished = true;
2014-05-13 07:23:47 +00:00
// Build a list of pak files to extract.
QList<FileOperation> filesToExtract;
2014-05-13 07:23:47 +00:00
for (int i = 1; i <= 8; i++)
2014-05-13 07:23:47 +00:00
{
FileOperation fo;
fo.source = QString("quake3-latest-pk3s/baseq3/pak%1.pk3").arg(i);
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
}
// 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
void InstallWizard_Patch::updateProgress(qint64 bytesRead, qint64 bytesTotal)
{
if (!isDownloadFinished)
2014-05-13 07:23:47 +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
}
else if (!isExtractFinished)
2014-05-13 09:34:15 +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
ui->pbProgress->setMaximum((int)bytesTotal);
ui->pbProgress->setValue((int)bytesRead);
}
2014-05-13 09:34:15 +00:00
void InstallWizard_Patch::setExtractFilename(const QString &filename)
{
extractFilename = filename;
}
2014-05-13 09:34:15 +00:00
void InstallWizard_Patch::setErrorMessage(const QString &message)
{
ui->lblStatus->setText(message);
}
2014-05-13 09:34:15 +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
// Complete the transaction.
const QString transactionError = FileUtils::completeTransaction(renameOperations);
2014-05-13 09:34:15 +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();
wizard()->next();
2014-05-13 03:28:20 +00:00
}