From fccc653bdf5fcca17a249cf0a8341bc462d4efe9 Mon Sep 17 00:00:00 2001 From: "J.D. Purcell" Date: Thu, 12 Oct 2023 20:21:47 -0400 Subject: [PATCH] Windows: Fix "Show in Explorer" with long paths even if 8.3 names are disabled. --- src/mainwindow.cpp | 7 ++++--- src/qvwin32functions.cpp | 12 ++++++++++++ src/qvwin32functions.h | 2 ++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 8b3714d6..f70de01c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -913,15 +913,16 @@ void MainWindow::openContainingFolder() const QFileInfo selectedFileInfo = getCurrentFileDetails().fileInfo; -#ifdef Q_OS_WIN +#ifdef WIN32_LOADED QString pathToSelect = QDir::toNativeSeparators(selectedFileInfo.absoluteFilePath()); - if (pathToSelect.length() > 259) + if (pathToSelect.length() > 259 && pathToSelect.startsWith(R"(\\)")) { + // The Shell API seems to handle long paths, unless they are UNC :( pathToSelect = QVWin32Functions::getShortPath(pathToSelect); if (pathToSelect.isEmpty()) return; } - QProcess::startDetached("explorer", QStringList() << "/select," << pathToSelect); + QVWin32Functions::showInExplorer(pathToSelect); #elif defined Q_OS_MACOS QProcess::execute("open", QStringList() << "-R" << selectedFileInfo.absoluteFilePath()); #else diff --git a/src/qvwin32functions.cpp b/src/qvwin32functions.cpp index 0b32d21c..a443f47d 100644 --- a/src/qvwin32functions.cpp +++ b/src/qvwin32functions.cpp @@ -173,6 +173,18 @@ QString QVWin32Functions::getShortPath(const QString &path) return getLongOrShortPath(path, GetShortPathNameW); } +bool QVWin32Functions::showInExplorer(const QString &path) +{ + bool result = false; + LPITEMIDLIST pIdl; + if (SUCCEEDED(SHParseDisplayName(reinterpret_cast(path.utf16()), nullptr, &pIdl, 0, nullptr))) + { + result = SUCCEEDED(SHOpenFolderAndSelectItems(pIdl, 0, nullptr, 0)); + ILFree(pIdl); + } + return result; +} + QByteArray QVWin32Functions::getIccProfileForWindow(const QWindow *window) { QByteArray result; diff --git a/src/qvwin32functions.h b/src/qvwin32functions.h index a4c2a4d5..1a490e66 100644 --- a/src/qvwin32functions.h +++ b/src/qvwin32functions.h @@ -18,6 +18,8 @@ class QVWin32Functions static QString getShortPath(const QString &path); + static bool showInExplorer(const QString &path); + static QByteArray getIccProfileForWindow(const QWindow *window); };