diff --git a/filecopy.cpp b/filecopy.cpp new file mode 100644 index 0000000..4e9a00e --- /dev/null +++ b/filecopy.cpp @@ -0,0 +1,80 @@ +/* +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. +*/ +#include +#include +#include "filecopy.h" + +FileCopyWorker::FileCopyWorker(const QList &files) : files(files), isCancelled(false) +{ +} + +void FileCopyWorker::copy() +{ + for (int i = 0; i < files.size(); i++) + { + QFile sourceFile(files.at(i).source); + + if (!sourceFile.open(QIODevice::ReadOnly)) + { + emit errorMessage(QString("'%1': %2").arg(files.at(i).source).arg(sourceFile.errorString())); + return; + } + + QFile destinationFile(files.at(i).dest); + + if (!destinationFile.open(QIODevice::WriteOnly)) + { + emit errorMessage(QString("'%1': %2").arg(files.at(i).dest).arg(destinationFile.errorString())); + return; + } + + emit fileChanged(QFileInfo(sourceFile.fileName()).fileName()); + const qint64 totalBytes = sourceFile.size(); + qint64 totalBytesWritten = 0; + + for (;;) + { + const qint64 bytesRead = sourceFile.read(buffer, bufferSize); + + if (bytesRead == 0) + break; + + const qint64 bytesWritten = destinationFile.write(buffer, bytesRead); + totalBytesWritten += bytesWritten; + emit progressChanged(totalBytesWritten, totalBytes); + + QMutexLocker locker(&cancelMutex); + + if (isCancelled) + break; + } + } + + emit copyFinished(); +} + +void FileCopyWorker::cancel() +{ + QMutexLocker locker(&cancelMutex); + isCancelled = true; +} diff --git a/filecopy.h b/filecopy.h new file mode 100644 index 0000000..9bc3496 --- /dev/null +++ b/filecopy.h @@ -0,0 +1,61 @@ +/* +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. +*/ +#ifndef FILECOPY_H +#define FILECOPY_H + +#include +#include +#include + +struct FileCopyOperation +{ + QString source; + QString dest; +}; + +class FileCopyWorker : public QObject +{ + Q_OBJECT + +public: + FileCopyWorker(const QList &files); + void cancel(); + +public slots: + void copy(); + +signals: + void fileChanged(const QString &filename); + void progressChanged(qint64 bytesWritten, qint64 bytesTotal); + void errorMessage(const QString &message); + void copyFinished(); + +private: + static const int bufferSize = 32 * 1024; + const QList files; + char buffer[bufferSize]; + bool isCancelled; + QMutex cancelMutex; +}; + +#endif // FILECOPY_H diff --git a/installwizard.cpp b/installwizard.cpp index c4d8dc2..12ea91f 100644 --- a/installwizard.cpp +++ b/installwizard.cpp @@ -37,22 +37,22 @@ InstallWizard::~InstallWizard() delete ui; } -void InstallWizard::clearCopyFiles() +void InstallWizard::clearFileCopyOperations() { - copyFiles.clear(); + fileCopyOperations.clear(); } -void InstallWizard::addCopyFile(const QString &source, const QString &dest) +void InstallWizard::addFileCopyOperation(const QString &source, const QString &dest) { - CopyFile cf; - cf.source = source; - cf.dest = dest; - copyFiles.append(cf); + FileCopyOperation fco; + fco.source = source; + fco.dest = dest; + fileCopyOperations.append(fco); } -const QList &InstallWizard::getCopyFiles() const +const QList &InstallWizard::getFileCopyOperations() const { - return copyFiles; + return fileCopyOperations; } bool InstallWizard::getIsQuake3PatchRequired() const diff --git a/installwizard.h b/installwizard.h index 0713b9f..86cb780 100644 --- a/installwizard.h +++ b/installwizard.h @@ -2,6 +2,7 @@ #define INSTALLWIZARD_H #include +#include "filecopy.h" namespace Ui { class InstallWizard; @@ -18,15 +19,9 @@ public: explicit InstallWizard(QWidget *parent, Settings *settings); ~InstallWizard(); - struct CopyFile - { - QString source; - QString dest; - }; - - void clearCopyFiles(); - void addCopyFile(const QString &source, const QString &dest); - const QList &getCopyFiles() const; + void clearFileCopyOperations(); + void addFileCopyOperation(const QString &source, const QString &dest); + const QList &getFileCopyOperations() const; bool getIsQuake3PatchRequired() const; void setIsQuake3PatchRequired(bool value); @@ -51,7 +46,7 @@ private: Ui::InstallWizard *ui; QPushButton *cancelButton; Settings *settings; - QList copyFiles; + QList fileCopyOperations; bool isQuake3PatchRequired; QString quakePath; }; diff --git a/installwizard_copy.cpp b/installwizard_copy.cpp index 280e266..51bffb9 100644 --- a/installwizard_copy.cpp +++ b/installwizard_copy.cpp @@ -4,61 +4,6 @@ #include "installwizard_copy.h" #include "ui_installwizard_copy.h" -CopyWorker::CopyWorker(const QList ©Files) : copyFiles(copyFiles), isCancelled(false) -{ -} - -void CopyWorker::copy() -{ - for (int i = 0; i < copyFiles.size(); i++) - { - QFile sourceFile(copyFiles.at(i).source); - - if (!sourceFile.open(QIODevice::ReadOnly)) - { - emit errorMessage(QString("'%1': %2").arg(copyFiles.at(i).source).arg(sourceFile.errorString())); - return; - } - - QFile destinationFile(copyFiles.at(i).dest); - - if (!destinationFile.open(QIODevice::WriteOnly)) - { - emit errorMessage(QString("'%1': %2").arg(copyFiles.at(i).dest).arg(destinationFile.errorString())); - return; - } - - emit fileChanged(QFileInfo(sourceFile.fileName()).fileName()); - const qint64 totalBytes = sourceFile.size(); - qint64 totalBytesWritten = 0; - - for (;;) - { - const qint64 bytesRead = sourceFile.read(buffer, bufferSize); - - if (bytesRead == 0) - break; - - const qint64 bytesWritten = destinationFile.write(buffer, bytesRead); - totalBytesWritten += bytesWritten; - emit progressChanged(totalBytesWritten, totalBytes); - - QMutexLocker locker(&cancelMutex); - - if (isCancelled) - break; - } - } - - emit copyFinished(); -} - -void CopyWorker::cancel() -{ - QMutexLocker locker(&cancelMutex); - isCancelled = true; -} - InstallWizard_Copy::InstallWizard_Copy(QWidget *parent) : QWizardPage(parent), ui(new Ui::InstallWizard_Copy), @@ -92,14 +37,14 @@ void InstallWizard_Copy::initializePage() } // Start copy thread. - copyWorker = new CopyWorker(((InstallWizard *)wizard())->getCopyFiles()); + copyWorker = new FileCopyWorker(((InstallWizard *)wizard())->getFileCopyOperations()); copyWorker->moveToThread(©Thread); connect(©Thread, &QThread::finished, copyWorker, &QObject::deleteLater); - connect(this, &InstallWizard_Copy::copy, copyWorker, &CopyWorker::copy); - connect(copyWorker, &CopyWorker::fileChanged, this, &InstallWizard_Copy::setCopyFilename); - connect(copyWorker, &CopyWorker::progressChanged, this, &InstallWizard_Copy::setCopyProgress); - connect(copyWorker, &CopyWorker::errorMessage, this, &InstallWizard_Copy::setCopyErrorMessage); - connect(copyWorker, &CopyWorker::copyFinished, this, &InstallWizard_Copy::finishCopy); + connect(this, &InstallWizard_Copy::copy, copyWorker, &FileCopyWorker::copy); + connect(copyWorker, &FileCopyWorker::fileChanged, this, &InstallWizard_Copy::setCopyFilename); + connect(copyWorker, &FileCopyWorker::progressChanged, this, &InstallWizard_Copy::setCopyProgress); + connect(copyWorker, &FileCopyWorker::errorMessage, this, &InstallWizard_Copy::setCopyErrorMessage); + connect(copyWorker, &FileCopyWorker::copyFinished, this, &InstallWizard_Copy::finishCopy); copyThread.start(); emit copy(); diff --git a/installwizard_copy.h b/installwizard_copy.h index 6cc614b..e288845 100644 --- a/installwizard_copy.h +++ b/installwizard_copy.h @@ -1,39 +1,14 @@ #ifndef INSTALLWIZARD_COPY_H #define INSTALLWIZARD_COPY_H -#include #include -#include #include "installwizard.h" namespace Ui { class InstallWizard_Copy; } -class CopyWorker : public QObject -{ - Q_OBJECT - -public: - CopyWorker(const QList ©Files); - void cancel(); - -public slots: - void copy(); - -signals: - void fileChanged(const QString &filename); - void progressChanged(qint64 bytesWritten, qint64 bytesTotal); - void errorMessage(const QString &message); - void copyFinished(); - -private: - static const int bufferSize = 32 * 1024; - const QList copyFiles; - char buffer[bufferSize]; - bool isCancelled; - QMutex cancelMutex; -}; +class FileCopyWorker; class InstallWizard_Copy : public QWizardPage { @@ -58,7 +33,7 @@ signals: private: Ui::InstallWizard_Copy *ui; - CopyWorker *copyWorker; + FileCopyWorker *copyWorker; QThread copyThread; bool isCopyFinished; diff --git a/installwizard_eula.cpp b/installwizard_eula.cpp index f2ae813..cf0c837 100644 --- a/installwizard_eula.cpp +++ b/installwizard_eula.cpp @@ -16,7 +16,7 @@ InstallWizard_Eula::~InstallWizard_Eula() int InstallWizard_Eula::nextId() const { - if (!((InstallWizard *)wizard())->getCopyFiles().isEmpty()) + if (!((InstallWizard *)wizard())->getFileCopyOperations().isEmpty()) { return InstallWizard::Page_Copy; } diff --git a/installwizard_setup.cpp b/installwizard_setup.cpp index 5539db0..9ee9c55 100644 --- a/installwizard_setup.cpp +++ b/installwizard_setup.cpp @@ -72,8 +72,8 @@ bool InstallWizard_Setup::validatePage() iw->setIsQuake3PatchRequired(true); // Copy page will copy baseq3/pak0.pk3. - iw->clearCopyFiles(); - iw->addCopyFile(ui->cbInstallSource->currentText() + QString("/QUAKE3/baseq3/pak0.pk3"), ui->txtInstallDest->text() + QString("/baseq3/pak0.pk3")); + iw->clearFileCopyOperations(); + iw->addFileCopyOperation(ui->cbInstallSource->currentText() + QString("/QUAKE3/baseq3/pak0.pk3"), ui->txtInstallDest->text() + QString("/baseq3/pak0.pk3")); iw->setQuakePath(ui->txtInstallDest->text()); } else if (ui->stackPages->currentIndex() == Page_InstallSteam) @@ -98,11 +98,11 @@ bool InstallWizard_Setup::validatePage() // Copy page will copy baseq3/*.pk3 files. QFileInfoList pakFiles = steamQuakeDir.entryInfoList(QStringList("*.pk3"), QDir::Files | QDir::NoSymLinks | QDir::Readable, QDir::Name); - iw->clearCopyFiles(); + iw->clearFileCopyOperations(); for (int i = 0; i < pakFiles.size(); i++) { - iw->addCopyFile(pakFiles.at(i).absoluteFilePath(), ui->txtInstallSteamDest->text() + QString("/baseq3/") + pakFiles.at(i).fileName()); + iw->addFileCopyOperation(pakFiles.at(i).absoluteFilePath(), ui->txtInstallSteamDest->text() + QString("/baseq3/") + pakFiles.at(i).fileName()); } iw->setQuakePath(ui->txtInstallSteamDest->text()); diff --git a/launch.pro b/launch.pro index 993eb4b..b4e62ba 100644 --- a/launch.pro +++ b/launch.pro @@ -20,7 +20,8 @@ SOURCES += main.cpp\ installwizard_finished.cpp \ installwizard_patch.cpp \ installwizard_eula.cpp \ - installwizard_copy.cpp + installwizard_copy.cpp \ + filecopy.cpp HEADERS += mainwindow.h \ settings.h \ @@ -29,7 +30,8 @@ HEADERS += mainwindow.h \ installwizard_finished.h \ installwizard_patch.h \ installwizard_eula.h \ - installwizard_copy.h + installwizard_copy.h \ + filecopy.h FORMS += mainwindow.ui \ installwizard.ui \