From ebb855dc1421e89813a1f4a74a4963d76aba9747 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Sat, 25 May 2019 03:10:37 -0400 Subject: ExploreFile() will select the file in explorer when the path is a file replaced more ShellExecute() calls with ExploreFile() --- src/downloadmanager.cpp | 25 ++++++++++++------------- src/modinfodialog.cpp | 2 +- src/organizercore.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- src/organizercore.h | 2 +- src/overwriteinfodialog.cpp | 3 ++- 5 files changed, 55 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index 78162f11..686092b5 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -1037,7 +1037,7 @@ void DownloadManager::openFile(int index) return; } - ::ShellExecuteW(nullptr, L"explore", ToWString(QDir::toNativeSeparators(m_OutputDirectory)).c_str(), nullptr, nullptr, SW_SHOWNORMAL); + ExploreFile(m_OutputDirectory); return; } @@ -1047,23 +1047,22 @@ void DownloadManager::openInDownloadsFolder(int index) reportError(tr("OpenFileInDownloadsFolder: invalid download index %1").arg(index)); return; } - QString params = "/select,\""; - QDir path = QDir(m_OutputDirectory); - if (path.exists(getFileName(index))) { - params = params + QDir::toNativeSeparators(getFilePath(index)) + "\""; - ::ShellExecuteW(nullptr, nullptr, L"explorer", ToWString(params).c_str(), nullptr, SW_SHOWNORMAL); - return; - } - else if (path.exists(getFileName(index) + ".unfinished")) { - params = params + QDir::toNativeSeparators(getFilePath(index)+".unfinished") + "\""; + const auto path = getFilePath(index); - ::ShellExecuteW(nullptr, nullptr, L"explorer", ToWString(params).c_str(), nullptr, SW_SHOWNORMAL); + if (QFile::exists(path)) { + ExploreFile(path); return; } + else { + const auto unfinished = path + ".unfinished"; + if (QFile::exists(unfinished)) { + ExploreFile(unfinished); + return; + } + } - ::ShellExecuteW(nullptr, L"explore", ToWString(QDir::toNativeSeparators(m_OutputDirectory)).c_str(), nullptr, nullptr, SW_SHOWNORMAL); - return; + ExploreFile(m_OutputDirectory); } diff --git a/src/modinfodialog.cpp b/src/modinfodialog.cpp index 83c0169a..5fb3e9cf 100644 --- a/src/modinfodialog.cpp +++ b/src/modinfodialog.cpp @@ -1242,7 +1242,7 @@ bool ModInfoDialog::recursiveDelete(const QModelIndex &index) void ModInfoDialog::on_openInExplorerButton_clicked() { - ::ShellExecuteW(nullptr, L"explore", ToWString(m_ModInfo->absolutePath()).c_str(), nullptr, nullptr, SW_SHOWNORMAL); + ExploreFile(m_ModInfo->absolutePath()); } void ModInfoDialog::deleteFile(const QModelIndex &index) diff --git a/src/organizercore.cpp b/src/organizercore.cpp index 2542545f..d228dfe1 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -330,25 +330,59 @@ bool GetFileExecutionContext( } } -bool ExploreFile(const QString& path) + +bool ExploreDirectory(const QFileInfo& info) { - const auto ws = path.toStdWString(); + const auto path = QDir::toNativeSeparators(info.absoluteFilePath()); + const auto ws_path = path.toStdWString(); const auto h = ::ShellExecuteW( - nullptr, L"explore", ws.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + nullptr, L"explore", ws_path.c_str(), nullptr, nullptr, SW_SHOWNORMAL); // anything <= 32 is not an actual HINSTANCE and signals failure return (h > reinterpret_cast(32)); } +bool ExploreFileInDirectory(const QFileInfo& info) +{ + const auto path = QDir::toNativeSeparators(info.absoluteFilePath()); + const auto params = "/select,\"" + path + "\""; + const auto ws_params = params.toStdWString(); + + const auto h = ::ShellExecuteW( + nullptr, nullptr, L"explorer", ws_params.c_str(), nullptr, SW_SHOWNORMAL); + + // anything <= 32 is not an actual HINSTANCE and signals failure + return (h > reinterpret_cast(32)); +} + + bool ExploreFile(const QFileInfo& info) { - return ExploreFile(info.absolutePath()); + if (info.isFile()) { + return ExploreFileInDirectory(info); + } else if (info.isDir()) { + return ExploreDirectory(info); + } else { + // try the parent directory + const auto parent = info.dir(); + + if (parent.exists()) { + return ExploreDirectory(parent.absolutePath()); + } + } + + return false; +} + +bool ExploreFile(const QString& path) +{ + return ExploreFile(QFileInfo(path)); } bool ExploreFile(const QDir& dir) { - return ExploreFile(dir.absolutePath()); + return ExploreFile(QFileInfo(dir.absolutePath())); } diff --git a/src/organizercore.h b/src/organizercore.h index 103443eb..3f773277 100644 --- a/src/organizercore.h +++ b/src/organizercore.h @@ -67,8 +67,8 @@ bool GetFileExecutionContext( QWidget* parent, const QFileInfo &targetInfo, QFileInfo &binaryInfo, QString &arguments, FileExecutionTypes& type); -bool ExploreFile(const QString& path); bool ExploreFile(const QFileInfo& info); +bool ExploreFile(const QString& path); bool ExploreFile(const QDir& dir); diff --git a/src/overwriteinfodialog.cpp b/src/overwriteinfodialog.cpp index 3d82cf17..e9d7ce06 100644 --- a/src/overwriteinfodialog.cpp +++ b/src/overwriteinfodialog.cpp @@ -21,6 +21,7 @@ along with Mod Organizer. If not, see . #include "ui_overwriteinfodialog.h" #include "report.h" #include "utility.h" +#include "organizercore.h" #include #include #include @@ -263,7 +264,7 @@ void OverwriteInfoDialog::createDirectoryTriggered() void OverwriteInfoDialog::on_explorerButton_clicked() { - ::ShellExecuteW(nullptr, L"explore", ToWString(m_ModInfo->absolutePath()).c_str(), nullptr, nullptr, SW_SHOWNORMAL); + ExploreFile(m_ModInfo->absolutePath()); } void OverwriteInfoDialog::on_filesView_customContextMenuRequested(const QPoint &pos) -- cgit v1.3.1