2014-05-14 12:33:47 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QThread>
|
|
|
|
#include "installwizard_copy.h"
|
|
|
|
#include "ui_installwizard_copy.h"
|
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
CopyWorker::CopyWorker(const QList<InstallWizard::CopyFile> ©Files) : copyFiles(copyFiles), isCancelled(false)
|
2014-05-14 12:33:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
void CopyWorker::copy()
|
2014-05-14 12:33:47 +00:00
|
|
|
{
|
2014-05-15 04:20:50 +00:00
|
|
|
for (int i = 0; i < copyFiles.size(); i++)
|
2014-05-14 12:33:47 +00:00
|
|
|
{
|
2014-05-15 04:20:50 +00:00
|
|
|
QFile sourceFile(copyFiles.at(i).source);
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
if (!sourceFile.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
emit errorMessage(QString("'%1': %2").arg(copyFiles.at(i).source).arg(sourceFile.errorString()));
|
|
|
|
return;
|
|
|
|
}
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
QFile destinationFile(copyFiles.at(i).dest);
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
if (!destinationFile.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
emit errorMessage(QString("'%1': %2").arg(copyFiles.at(i).dest).arg(destinationFile.errorString()));
|
|
|
|
return;
|
|
|
|
}
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
const qint64 totalBytes = sourceFile.size();
|
|
|
|
qint64 totalBytesWritten = 0;
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
const qint64 bytesRead = sourceFile.read(buffer, bufferSize);
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
if (bytesRead == 0)
|
|
|
|
break;
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
const qint64 bytesWritten = destinationFile.write(buffer, bytesRead);
|
|
|
|
totalBytesWritten += bytesWritten;
|
|
|
|
emit progressChanged(totalBytesWritten, totalBytes);
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
QMutexLocker locker(&cancelMutex);
|
2014-05-14 12:33:47 +00:00
|
|
|
|
2014-05-15 04:20:50 +00:00
|
|
|
if (isCancelled)
|
|
|
|
break;
|
|
|
|
}
|
2014-05-14 12:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit copyFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CopyWorker::cancel()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&cancelMutex);
|
|
|
|
isCancelled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstallWizard_Copy::InstallWizard_Copy(QWidget *parent) :
|
|
|
|
QWizardPage(parent),
|
|
|
|
ui(new Ui::InstallWizard_Copy),
|
|
|
|
copyWorker(NULL),
|
|
|
|
isCopyFinished(false)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
InstallWizard_Copy::~InstallWizard_Copy()
|
|
|
|
{
|
|
|
|
copyThread.quit();
|
|
|
|
copyThread.wait();
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallWizard_Copy::initializePage()
|
|
|
|
{
|
|
|
|
isCopyFinished = false;
|
|
|
|
|
|
|
|
// Try to create the destination directory and baseq3 subdirectory.
|
2014-05-15 07:21:13 +00:00
|
|
|
const QString quake3Path(((InstallWizard *)wizard())->getQuakePath() + QString("/baseq3"));
|
2014-05-14 12:33:47 +00:00
|
|
|
QDir dir;
|
|
|
|
|
|
|
|
if (!dir.mkpath(quake3Path))
|
|
|
|
{
|
|
|
|
ui->lblStatus->setText(QString("Error creating directory '%1'").arg(quake3Path));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start copy thread.
|
2014-05-15 04:20:50 +00:00
|
|
|
copyWorker = new CopyWorker(((InstallWizard *)wizard())->getCopyFiles());
|
2014-05-14 12:33:47 +00:00
|
|
|
copyWorker->moveToThread(©Thread);
|
|
|
|
connect(©Thread, &QThread::finished, copyWorker, &QObject::deleteLater);
|
|
|
|
connect(this, &InstallWizard_Copy::copy, copyWorker, &CopyWorker::copy);
|
|
|
|
connect(copyWorker, &CopyWorker::progressChanged, this, &InstallWizard_Copy::setCopyProgress);
|
|
|
|
connect(copyWorker, &CopyWorker::errorMessage, this, &InstallWizard_Copy::setCopyErrorMessage);
|
|
|
|
connect(copyWorker, &CopyWorker::copyFinished, this, &InstallWizard_Copy::finishCopy);
|
|
|
|
copyThread.start();
|
2014-05-15 04:20:50 +00:00
|
|
|
|
|
|
|
emit copy();
|
2014-05-14 12:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InstallWizard_Copy::isComplete() const
|
|
|
|
{
|
|
|
|
return isCopyFinished;
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:40:17 +00:00
|
|
|
int InstallWizard_Copy::nextId() const
|
|
|
|
{
|
|
|
|
if (((InstallWizard *)wizard())->getIsQuake3PatchRequired())
|
|
|
|
{
|
|
|
|
return InstallWizard::Page_Patch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return InstallWizard::Page_Finished;
|
|
|
|
}
|
|
|
|
|
2014-05-14 12:33:47 +00:00
|
|
|
void InstallWizard_Copy::cancel()
|
|
|
|
{
|
|
|
|
if (!isCopyFinished)
|
|
|
|
{
|
|
|
|
copyWorker->cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallWizard_Copy::setCopyProgress(qint64 bytesWritten, qint64 bytesTotal)
|
|
|
|
{
|
|
|
|
ui->lblStatus->setText(QString("Copying %1MB / %2MB").arg(bytesWritten / 1024.0 / 1024.0, 0, 'f', 2).arg(bytesTotal / 1024.0 / 1024.0, 0, 'f', 2));
|
|
|
|
ui->pbProgress->setMaximum((int)bytesTotal);
|
|
|
|
ui->pbProgress->setValue((int)bytesWritten);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallWizard_Copy::setCopyErrorMessage(const QString &message)
|
|
|
|
{
|
|
|
|
ui->lblStatus->setText(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallWizard_Copy::finishCopy()
|
|
|
|
{
|
|
|
|
copyThread.quit();
|
|
|
|
copyThread.wait();
|
|
|
|
isCopyFinished = true;
|
|
|
|
emit completeChanged();
|
2014-05-15 03:54:18 +00:00
|
|
|
wizard()->next();
|
2014-05-14 12:33:47 +00:00
|
|
|
}
|