summaryrefslogtreecommitdiff
path: root/src/installationmanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/installationmanager.cpp')
-rw-r--r--src/installationmanager.cpp113
1 files changed, 102 insertions, 11 deletions
diff --git a/src/installationmanager.cpp b/src/installationmanager.cpp
index 62a97591..79f5ee91 100644
--- a/src/installationmanager.cpp
+++ b/src/installationmanager.cpp
@@ -108,6 +108,7 @@ InstallationManager::~InstallationManager()
void InstallationManager::setParentWidget(QWidget *widget)
{
+ m_ParentWidget = widget;
for (IPluginInstaller *installer : m_Installers) {
installer->setParentWidget(widget);
}
@@ -129,6 +130,7 @@ bool InstallationManager::extractFiles(QDir extractPath)
{
m_InstallationProgress = new QProgressDialog(m_ParentWidget);
ON_BLOCK_EXIT([this]() {
+ m_InstallationProgress->cancel();
m_InstallationProgress->hide();
m_InstallationProgress->deleteLater();
m_InstallationProgress = nullptr;
@@ -139,6 +141,7 @@ bool InstallationManager::extractFiles(QDir extractPath)
m_InstallationProgress->setWindowTitle(tr("Extracting files"));
m_InstallationProgress->setWindowModality(Qt::WindowModal);
m_InstallationProgress->setFixedSize(600, 100);
+ m_InstallationProgress->setAutoReset(false);
m_InstallationProgress->show();
// unpack only the files we need for the installer
@@ -154,7 +157,7 @@ bool InstallationManager::extractFiles(QDir extractPath)
if (m_Progress != m_InstallationProgress->value())
m_InstallationProgress->setValue(m_Progress);
QCoreApplication::processEvents();
- } while (!future.isFinished() || m_InstallationProgress->isVisible());
+ } while (!future.isFinished());
if (!future.result()) {
if (m_ArchiveHandler->getLastError() == Archive::ERROR_EXTRACT_CANCELLED) {
if (!m_ErrorMessage.isEmpty()) {
@@ -207,6 +210,60 @@ QStringList InstallationManager::extractFiles(std::vector<std::shared_ptr<const
}
+QString InstallationManager::createFile(std::shared_ptr<const MOBase::FileTreeEntry> entry)
+{
+ // Use QTemporaryFile to create the temporary file with the given template:
+ QTemporaryFile tempFile(QDir::cleanPath(QDir::tempPath() + QDir::separator() + "mo2-install"));
+
+ // Turn-off autoRemove otherwise the file is deleted when destructor is called:
+ tempFile.setAutoRemove(false);
+
+ // Open/Close the file so that installer can use it properly:
+ if (!tempFile.open()) {
+ return QString();
+ }
+ tempFile.close();
+
+ // fileName() returns the full path since we provide a full path in the constructor:
+ const QString absPath = tempFile.fileName();
+
+ m_CreatedFiles[entry] = absPath;
+ m_TempFilesToDelete.insert(QDir::temp().relativeFilePath(absPath));
+
+ // Returns the path with native separators:
+ return QDir::toNativeSeparators(absPath);
+}
+
+void InstallationManager::cleanCreatedFiles(std::shared_ptr<const MOBase::IFileTree> fileTree)
+{
+ // We simply have to check if all the entries have fileTree as a parent:
+ for (auto it = std::begin(m_CreatedFiles); it != std::end(m_CreatedFiles); ) {
+
+ // Find the parent - Could this be in FileTreeEntry?
+ bool found = false;
+ {
+ auto parent = it->first->parent();
+ while (parent && !found) {
+ if (parent == fileTree) {
+ found = true;
+ }
+ else {
+ parent = parent->parent();
+ }
+ }
+ }
+
+ // If the parent was not found, we remove the entry, otherwize we move to the next one:
+ if (!found) {
+ it = m_CreatedFiles.erase(it);
+ }
+ else {
+ ++it;
+ }
+ }
+}
+
+
IPluginInstaller::EInstallResult InstallationManager::installArchive(GuessedValue<QString> &modName, const QString &archiveName, int modId)
{
// in earlier versions the modName was copied here and the copy passed to install. I don't know why I did this and it causes
@@ -368,6 +425,7 @@ IPluginInstaller::EInstallResult InstallationManager::doInstall(GuessedValue<QSt
log::debug("installing to \"{}\"", targetDirectoryNative);
+ // Extract the archive:
m_InstallationProgress = new QProgressDialog(m_ParentWidget);
ON_BLOCK_EXIT([this] () {
m_InstallationProgress->cancel();
@@ -378,6 +436,13 @@ IPluginInstaller::EInstallResult InstallationManager::doInstall(GuessedValue<QSt
m_ProgressFile = QString();
});
+ // Turn off auto-reset otherwize the progress dialog is reset before the end. This
+ // is kind of annoying because updateProgress consider percentage of progression
+ // through the archive (pack), while we are waiting for extracting archive entries, so
+ // the percentage of in updateProgress is not really related to the percentage of files
+ // extracted...
+ m_InstallationProgress->setAutoReset(false);
+
m_InstallationProgress->setWindowFlags(
m_InstallationProgress->windowFlags() & (~Qt::WindowContextHelpButtonHint));
m_InstallationProgress->setWindowModality(Qt::WindowModal);
@@ -410,6 +475,24 @@ IPluginInstaller::EInstallResult InstallationManager::doInstall(GuessedValue<QSt
}
}
+ // Copy the created files:
+ for (auto& p : m_CreatedFiles) {
+ QString destPath = QDir::cleanPath(targetDirectory + QDir::separator() + p.first->path());
+ log::debug("Moving {} to {}.", p.second, destPath);
+
+ // We need to remove the path if it exists:
+ if (QFile::exists(destPath)) {
+ QFile::remove(destPath);
+ }
+
+ QDir dir = QFileInfo(destPath).absoluteDir();
+ if (!dir.exists()) {
+ dir.mkpath(".");
+ }
+
+ QFile::copy(p.second, destPath);
+ }
+
QSettings settingsFile(targetDirectory + "/meta.ini", QSettings::IniFormat);
// overwrite settings only if they are actually are available or haven't been set before
@@ -454,12 +537,12 @@ IPluginInstaller::EInstallResult InstallationManager::doInstall(GuessedValue<QSt
}
-bool InstallationManager::wasCancelled()
+bool InstallationManager::wasCancelled() const
{
return m_ArchiveHandler->getLastError() == Archive::ERROR_EXTRACT_CANCELLED;
}
-bool InstallationManager::isRunning()
+bool InstallationManager::isRunning() const
{
return m_IsRunning;
}
@@ -467,6 +550,10 @@ bool InstallationManager::isRunning()
void InstallationManager::postInstallCleanup()
{
+ // Clear the list of created files:
+ m_CreatedFiles.clear();
+
+ // Close the archive:
m_ArchiveHandler->close();
// directories we may want to remove. sorted from longest to shortest to ensure we remove subdirectories first.
@@ -618,15 +705,23 @@ IPluginInstaller::EInstallResult InstallationManager::install(const QString &fil
= installerSimple->install(modName, filesTree, version, modID);
if (installResult == IPluginInstaller::RESULT_SUCCESS) {
- // Note: Have to maintain a pointer to IFileTree because install() takes a
- // reference. This could be an issue if ever a plugin creates a new tree that
- // is not a ArchiveFileTree.
+ // Downcast to an actual ArchiveFileTree and map to the archive. Test if
+ // the tree is still an ArchiveFileTree, otherwize it means the installer
+ // did some bad stuff.
ArchiveFileTree* p = dynamic_cast<ArchiveFileTree*>(filesTree.get());
if (p == nullptr) {
throw IncompatibilityException(tr("Invalid file tree returned by plugin."));
}
+
+ // Detach the file tree (this ensure the parent is null and call to path()
+ // stops at this root):
+ p->detach();
+
p->mapToArchive(m_ArchiveHandler);
+ // Clean the created files:
+ cleanCreatedFiles(filesTree);
+
// the simple installer only prepares the installation, the rest
// works the same for all installers
installResult = doInstall(modName, gameName, modID, version, newestVersion, categoryID, fileCategoryID, repository);
@@ -743,9 +838,5 @@ void InstallationManager::registerInstaller(IPluginInstaller *installer)
QStringList InstallationManager::getSupportedExtensions() const
{
- QStringList result;
- foreach (const QString &extension, m_SupportedExtensions) {
- result.append(extension);
- }
- return result;
+ return QStringList(std::begin(m_SupportedExtensions), std::end(m_SupportedExtensions));
}