From a2ca7b92a227530579c442cf553631dcccdb7303 Mon Sep 17 00:00:00 2001 From: pvdk Date: Wed, 1 Jan 2014 17:15:34 +0100 Subject: [PATCH] Working on the unshield implementation, added support for moving temp files --- apps/wizard/unshieldthread.cpp | 140 +++++++++++++++++++++++++++++++-- apps/wizard/unshieldthread.hpp | 5 ++ 2 files changed, 140 insertions(+), 5 deletions(-) diff --git a/apps/wizard/unshieldthread.cpp b/apps/wizard/unshieldthread.cpp index d759b56a6c..0f8dbc4771 100644 --- a/apps/wizard/unshieldthread.cpp +++ b/apps/wizard/unshieldthread.cpp @@ -2,6 +2,8 @@ #include +#include +#include #include #include #include @@ -86,6 +88,91 @@ void Wizard::UnshieldThread::setupSettings() mIniSettings.readFile(stream); } +bool Wizard::UnshieldThread::removeDirectory(const QString &dirName) +{ + bool result = true; + QDir dir(dirName); + + if (dir.exists(dirName)) + { + QFileInfoList list(dir.entryInfoList(QDir::NoDotAndDotDot | + QDir::System | QDir::Hidden | + QDir::AllDirs | QDir::Files, QDir::DirsFirst)); + foreach(QFileInfo info, list) { + if (info.isDir()) { + result = removeDirectory(info.absoluteFilePath()); + } else { + result = QFile::remove(info.absoluteFilePath()); + } + + if (!result) + return result; + } + + result = dir.rmdir(dirName); + } + return result; +} + +bool Wizard::UnshieldThread::moveFile(const QString &source, const QString &destination) +{ + QDir dir; + QFile file; + + if (dir.rename(source, destination)) { + return true; + } else { + if (file.copy(source, destination)) { + return file.remove(source); + } else { + qDebug() << "copy failed! " << file.errorString(); + } + } + + return false; +} + +bool Wizard::UnshieldThread::moveDirectory(const QString &source, const QString &destination) +{ + QDir sourceDir(source); + QDir destDir(destination); + + if (!destDir.exists()) { + sourceDir.mkpath(destination); + destDir.refresh(); + } + + if (!destDir.exists()) + return false; + + bool result = true; + + QFileInfoList list(sourceDir.entryInfoList(QDir::NoDotAndDotDot | + QDir::System | QDir::Hidden | + QDir::AllDirs | QDir::Files, QDir::DirsFirst)); + + foreach (const QFileInfo &info, list) { + QString relativePath(info.absoluteFilePath()); + relativePath.remove(source); + + if (info.isSymLink()) + continue; + + if (info.isDir()) { + result = moveDirectory(info.absoluteFilePath(), destDir.absolutePath() + relativePath); + } else { + qDebug() << "moving: " << info.absoluteFilePath() << " to: " << destDir.absolutePath() + relativePath; + + result = moveFile(info.absoluteFilePath(), destDir.absolutePath() + relativePath); + } + + //if (!result) + // return result; + } + + return result && removeDirectory(sourceDir.absolutePath()); +} + void Wizard::UnshieldThread::extract() { emit textChanged(QLatin1String("Starting installation")); @@ -129,10 +216,48 @@ void Wizard::UnshieldThread::extract() // TODO: Throw error; // Move the files from the temporary path to the destination folder - qDebug() << "rename: " << morrowindTempPath << " to: " << mPath; - if (!dir.rename(morrowindTempPath, mPath)) + //qDebug() << "rename: " << morrowindTempPath << " to: " << mPath; + morrowindTempPath.append(QDir::separator() + QLatin1String("Data Files")); + if (!moveDirectory(morrowindTempPath, mPath)) qDebug() << "failed!"; + QDir source(QLatin1String("/mnt/cdrom/")); + QStringList directories; + directories << QLatin1String("Fonts") + << QLatin1String("Music") + << QLatin1String("Sound") + << QLatin1String("Splash"); + + QFileInfoList list(sourceDir.entryInfoList(QDir::NoDotAndDotDot | + QDir::System | QDir::Hidden | + QDir::AllDirs)); + + + foreach(QFileInfo info, list) { + if (info.isSymLink()) + continue; + + if (directories.contains(info.fileName())) + copyDirectory(info.absoluteFilePath(), mPath); + } + + + bfs::path fonts = findFile(from, "fonts", false); + if(fonts.string() != "") + installToPath(fonts, dFiles / "Fonts"); + + bfs::path music = findFile(from, "music", false); + if(music.string() != "") + installToPath(music, dFiles / "Music"); + + bfs::path sound = findFile(from, "sound", false); + if(sound.string() != "") + installToPath(sound, dFiles / "Sound"); + + bfs::path splash = findFile(from, "splash", false); + if(splash.string() != "") + installToPath(splash, dFiles / "Splash"); + } // if(!mMorrowindDone && mMorrowindPath.string().length() > 0) @@ -181,16 +306,21 @@ bool Wizard::UnshieldThread::extractFile(Unshield *unshield, const QString &outp { bool success; QString path(outputDir); - path.append('/'); + path.append(QDir::separator()); int directory = unshield_file_directory(unshield, index); if (!prefix.isEmpty()) - path.append(prefix + QLatin1Char('/')); + path.append(prefix + QDir::separator()); if (directory >= 0) - path.append(QString::fromLatin1(unshield_directory_name(unshield, directory)) + QLatin1Char('/')); + path.append(QString::fromLatin1(unshield_directory_name(unshield, directory)) + QDir::separator()); + // Ensure the path has the right separators + path.replace(QLatin1Char('\\'), QDir::separator()); + path = QDir::toNativeSeparators(path); + + qDebug() << "path is: " << path << QString::fromLatin1(unshield_directory_name(unshield, directory)) + QLatin1Char('/'); // Ensure the target path exists QDir dir; dir.mkpath(path); diff --git a/apps/wizard/unshieldthread.hpp b/apps/wizard/unshieldthread.hpp index 7f7250875b..cc3740ad82 100644 --- a/apps/wizard/unshieldthread.hpp +++ b/apps/wizard/unshieldthread.hpp @@ -29,6 +29,11 @@ namespace Wizard private: + bool removeDirectory(const QString &dirName); + + bool moveFile(const QString &source, const QString &destination); + bool moveDirectory(const QString &source, const QString &destination); + void setupSettings(); void extract();