From 2fb491711188d413f65bdd8193644d25ae03c2c2 Mon Sep 17 00:00:00 2001 From: Thomas Tanner Date: Sun, 27 Sep 2015 13:29:04 +0100 Subject: Changes to go with cleaned up archive library --- src/installationmanager.cpp | 113 +++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 58 deletions(-) (limited to 'src/installationmanager.cpp') diff --git a/src/installationmanager.cpp b/src/installationmanager.cpp index 75abd750..ead9b3dc 100644 --- a/src/installationmanager.cpp +++ b/src/installationmanager.cpp @@ -81,9 +81,9 @@ InstallationManager::InstallationManager() CreateArchiveType CreateArchiveFunc = resolveFunction(archiveLib, "CreateArchive"); - m_CurrentArchive = CreateArchiveFunc(); - if (!m_CurrentArchive->isValid()) { - throw MyException(getErrorString(m_CurrentArchive->getLastError())); + m_ArchiveHandler = CreateArchiveFunc(); + if (!m_ArchiveHandler->isValid()) { + throw MyException(getErrorString(m_ArchiveHandler->getLastError())); } m_InstallationProgress.setWindowFlags(m_InstallationProgress.windowFlags() & (~Qt::WindowContextHelpButtonHint)); @@ -92,7 +92,7 @@ InstallationManager::InstallationManager() InstallationManager::~InstallationManager() { - delete m_CurrentArchive; + delete m_ArchiveHandler; } void InstallationManager::setParentWidget(QWidget *widget) @@ -104,34 +104,32 @@ void InstallationManager::setParentWidget(QWidget *widget) } -void InstallationManager::queryPassword(LPSTR password) +void InstallationManager::queryPassword(QString *password) { - QString result = QInputDialog::getText(nullptr, tr("Password required"), tr("Password"), QLineEdit::Password); - strncpy(password, result.toLocal8Bit().constData(), MAX_PASSWORD_LENGTH); + *password = QInputDialog::getText(nullptr, tr("Password required"), tr("Password"), QLineEdit::Password); } -void InstallationManager::mapToArchive(const DirectoryTree::Node *node, std::wstring path, FileData * const *data) +void InstallationManager::mapToArchive(const DirectoryTree::Node *node, QString path, FileData * const *data) { if (path.length() > 0) { // when using a long windows path (starting with \\?\) we apparently can have redundant // . components in the path. This wasn't a problem with "regular" path names. - if (path == L".") { + if (path == ".") { path.clear(); } else { - path.append(L"\\"); + path.append("\\"); } } for (DirectoryTree::const_leaf_iterator iter = node->leafsBegin(); iter != node->leafsEnd(); ++iter) { - std::wstring temp = path + iter->getName().toStdWString(); - data[iter->getIndex()]->addOutputFileName(temp.c_str()); + data[iter->getIndex()]->addOutputFileName(path + iter->getName().toQString()); } for (DirectoryTree::const_node_iterator iter = node->nodesBegin(); iter != node->nodesEnd(); ++iter) { - std::wstring temp = path + (*iter)->getData().name.toStdWString(); + QString temp = path + (*iter)->getData().name.toQString(); if ((*iter)->getData().index != -1) { - data[(*iter)->getData().index]->addOutputFileName(temp.c_str()); + data[(*iter)->getData().index]->addOutputFileName(temp); } mapToArchive(*iter, temp, data); } @@ -142,11 +140,9 @@ void InstallationManager::mapToArchive(const DirectoryTree::Node *baseNode) { FileData* const *data; size_t size; - m_CurrentArchive->getFileList(data, size); + m_ArchiveHandler->getFileList(data, size); - std::wstring currentPath; - - mapToArchive(baseNode, currentPath, data); + mapToArchive(baseNode, "", data); } @@ -154,15 +150,15 @@ bool InstallationManager::unpackSingleFile(const QString &fileName) { FileData* const *data; size_t size; - m_CurrentArchive->getFileList(data, size); + m_ArchiveHandler->getFileList(data, size); QString baseName = QFileInfo(fileName).fileName(); bool available = false; for (size_t i = 0; i < size; ++i) { - if (_wcsicmp(data[i]->getFileName(), ToWString(fileName).c_str()) == 0) { + if (data[i]->getFileName().compare(fileName, Qt::CaseInsensitive) == 0) { available = true; - data[i]->addOutputFileName(ToWString(baseName).c_str()); + data[i]->addOutputFileName(baseName); m_TempFilesToDelete.insert(baseName); } } @@ -177,10 +173,10 @@ bool InstallationManager::unpackSingleFile(const QString &fileName) m_InstallationProgress.setWindowModality(Qt::WindowModal); m_InstallationProgress.show(); - bool res = m_CurrentArchive->extract(ToWString(QDir::toNativeSeparators(QDir::tempPath())).c_str(), + bool res = m_ArchiveHandler->extract(QDir::tempPath(), new MethodCallback(this, &InstallationManager::updateProgress), - new MethodCallback(this, &InstallationManager::dummyProgressFile), - new MethodCallback(this, &InstallationManager::report7ZipError)); + nullptr, + new MethodCallback(this, &InstallationManager::report7ZipError)); m_InstallationProgress.hide(); @@ -223,25 +219,30 @@ QStringList InstallationManager::extractFiles(const QStringList &filesOrig, bool FileData* const *data; size_t size; - m_CurrentArchive->getFileList(data, size); + m_ArchiveHandler->getFileList(data, size); for (size_t i = 0; i < size; ++i) { - if (files.contains(ToQString(data[i]->getFileName()), Qt::CaseInsensitive)) { - const wchar_t *targetFile = data[i]->getFileName(); + //FIXME Use qstring all the way through + if (files.contains(data[i]->getFileName(), Qt::CaseInsensitive)) { + std::wstring temp = data[i]->getFileName().toStdWString(); + wchar_t const * const origFile = temp.c_str(); + const wchar_t *targetFile = origFile; + //Note: I don't think 'flatten' is ever set to true. so this code + //might never be executed if (flatten) { - targetFile = wcsrchr(data[i]->getFileName(), '\\'); + targetFile = wcsrchr(origFile/*data[i]->getFileName()*/, '\\'); if (targetFile == nullptr) { - targetFile = wcsrchr(data[i]->getFileName(), '/'); + targetFile = wcsrchr(origFile/*data[i]->getFileName()*/, '/'); } if (targetFile == nullptr) { - qCritical("failed to find backslash in %ls", data[i]->getFileName()); + qCritical() << "Failed to find backslash in " << data[i]->getFileName(); continue; } else { // skip the slash ++targetFile; } } - data[i]->addOutputFileName(targetFile); + data[i]->addOutputFileName(ToQString(targetFile)); result.append(QDir::tempPath().append("/").append(ToQString(targetFile))); @@ -256,12 +257,12 @@ QStringList InstallationManager::extractFiles(const QStringList &filesOrig, bool m_InstallationProgress.show(); // unpack only the files we need for the installer - if (!m_CurrentArchive->extract(ToWString(QDir::toNativeSeparators(QDir::tempPath())).c_str(), + if (!m_ArchiveHandler->extract(QDir::tempPath(), new MethodCallback(this, &InstallationManager::updateProgress), - new MethodCallback(this, &InstallationManager::dummyProgressFile), - new MethodCallback(this, &InstallationManager::report7ZipError))) { + nullptr, + new MethodCallback(this, &InstallationManager::report7ZipError))) { m_InstallationProgress.hide(); - throw MyException(QString("extracting failed (%1)").arg(m_CurrentArchive->getLastError())); + throw MyException(QString("extracting failed (%1)").arg(m_ArchiveHandler->getLastError())); } m_InstallationProgress.hide(); @@ -286,7 +287,7 @@ DirectoryTree *InstallationManager::createFilesTree() { FileData* const *data; size_t size; - m_CurrentArchive->getFileList(data, size); + m_ArchiveHandler->getFileList(data, size); QScopedPointer result(new DirectoryTree); @@ -296,7 +297,7 @@ DirectoryTree *InstallationManager::createFilesTree() // grouping the filenames first, but so far there doesn't seem to be an actual performance problem DirectoryTree::Node *currentNode = result.data(); - QString fileName = ToQString(data[i]->getFileName()); + QString fileName = data[i]->getFileName(); QStringList components = fileName.split("\\"); // iterate over all path-components of this filename (including the filename itself) @@ -389,26 +390,22 @@ void InstallationManager::updateProgress(float percentage) { m_InstallationProgress.setValue(static_cast(percentage * 100.0)); if (m_InstallationProgress.wasCanceled()) { - m_CurrentArchive->cancel(); + m_ArchiveHandler->cancel(); m_InstallationProgress.reset(); } } -void InstallationManager::updateProgressFile(LPCWSTR fileName) +void InstallationManager::updateProgressFile(QString const &fileName) { -#if QT_VERSION >= QT_VERSION_CHECK(5,0,0) - m_InstallationProgress.setLabelText(QString::fromWCharArray(fileName)); -#else - m_InstallationProgress.setLabelText(QString::fromUtf16(fileName)); -#endif + m_InstallationProgress.setLabelText(fileName); } -void InstallationManager::report7ZipError(LPCWSTR errorMessage) +void InstallationManager::report7ZipError(QString const &errorMessage) { - reportError(QString::fromWCharArray(errorMessage)); - m_CurrentArchive->cancel(); + reportError(errorMessage); + m_ArchiveHandler->cancel(); } @@ -538,15 +535,15 @@ bool InstallationManager::doInstall(GuessedValue &modName, int modID, m_InstallationProgress.setValue(0); m_InstallationProgress.setWindowModality(Qt::WindowModal); m_InstallationProgress.show(); - if (!m_CurrentArchive->extract(ToWString("\\\\?\\" + targetDirectoryNative).c_str(), + if (!m_ArchiveHandler->extract(targetDirectory, new MethodCallback(this, &InstallationManager::updateProgress), - new MethodCallback(this, &InstallationManager::updateProgressFile), - new MethodCallback(this, &InstallationManager::report7ZipError))) { + new MethodCallback(this, &InstallationManager::updateProgressFile), + new MethodCallback(this, &InstallationManager::report7ZipError))) { m_InstallationProgress.hide(); - if (m_CurrentArchive->getLastError() == Archive::ERROR_EXTRACT_CANCELLED) { + if (m_ArchiveHandler->getLastError() == Archive::ERROR_EXTRACT_CANCELLED) { return false; } else { - throw MyException(QString("extracting failed (%1)").arg(m_CurrentArchive->getLastError())); + throw MyException(QString("extracting failed (%1)").arg(m_ArchiveHandler->getLastError())); } } @@ -587,13 +584,13 @@ bool InstallationManager::doInstall(GuessedValue &modName, int modID, bool InstallationManager::wasCancelled() { - return m_CurrentArchive->getLastError() == Archive::ERROR_EXTRACT_CANCELLED; + return m_ArchiveHandler->getLastError() == Archive::ERROR_EXTRACT_CANCELLED; } void InstallationManager::postInstallCleanup() { - m_CurrentArchive->close(); + m_ArchiveHandler->close(); // directories we may want to remove. sorted from longest to shortest to ensure we remove subdirectories first. auto longestFirst = [](const QString &LHS, const QString &RHS) -> bool { @@ -679,13 +676,13 @@ bool InstallationManager::install(const QString &fileName, GuessedValue //If there's an archive already open, close it. This happens with the bundle //installer when it uncompresses a split archive, then finds it has a real archive //to deal with. - m_CurrentArchive->close(); + m_ArchiveHandler->close(); // open the archive and construct the directory tree the installers work on - bool archiveOpen = m_CurrentArchive->open(ToWString(QDir::toNativeSeparators(fileName)).c_str(), - new MethodCallback(this, &InstallationManager::queryPassword)); + bool archiveOpen = m_ArchiveHandler->open(fileName, + new MethodCallback(this, &InstallationManager::queryPassword)); if (!archiveOpen) { - qDebug("integrated archiver can't open %s. errorcode %d", qPrintable(fileName), m_CurrentArchive->getLastError()); + qDebug("integrated archiver can't open %s. errorcode %d", qPrintable(fileName), m_ArchiveHandler->getLastError()); } ON_BLOCK_EXIT(std::bind(&InstallationManager::postInstallCleanup, this)); -- cgit v1.3.1