From 5213f35ec081a093bf0fecc5692e0e78597545f7 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 23 Jul 2016 11:24:58 +0800 Subject: [PATCH] Refactor --- src/base/utils/misc.cpp | 103 +++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 55 deletions(-) diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index a04b61eb1..142a1cf53 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -572,65 +572,58 @@ void Utils::Misc::openPath(const QString &absolutePath) void Utils::Misc::openFolderSelect(const QString &absolutePath) { const QString path = Utils::Fs::fromNativePath(absolutePath); -#ifdef Q_OS_WIN - if (QFileInfo(path).exists()) { - // Syntax is: explorer /select, "C:\Folder1\Folder2\file_to_select" - // Dir separators MUST be win-style slashes - - // QProcess::startDetached() has an obscure bug. If the path has - // no spaces and a comma(and maybe other special characters) it doesn't - // get wrapped in quotes. So explorer.exe can't find the correct path and - // displays the default one. If we wrap the path in quotes and pass it to - // QProcess::startDetached() explorer.exe still shows the default path. In - // this case QProcess::startDetached() probably puts its own quotes around ours. - - STARTUPINFO startupInfo; - ::ZeroMemory(&startupInfo, sizeof(startupInfo)); - startupInfo.cb = sizeof(startupInfo); - - PROCESS_INFORMATION processInfo; - ::ZeroMemory(&processInfo, sizeof(processInfo)); - - QString cmd = QString("explorer.exe /select,\"%1\"").arg(Utils::Fs::toNativePath(absolutePath)); - LPWSTR lpCmd = new WCHAR[cmd.size() + 1]; - cmd.toWCharArray(lpCmd); - lpCmd[cmd.size()] = 0; - - bool ret = ::CreateProcessW(NULL, lpCmd, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo); - delete [] lpCmd; - - if (ret) { - ::CloseHandle(processInfo.hProcess); - ::CloseHandle(processInfo.hThread); - } - } - else { - // If the item to select doesn't exist, try to open its parent + // If the item to select doesn't exist, try to open its parent + if (!QFileInfo(path).exists()) { openPath(path.left(path.lastIndexOf("/"))); + return; + } +#ifdef Q_OS_WIN + // Syntax is: explorer /select, "C:\Folder1\Folder2\file_to_select" + // Dir separators MUST be win-style slashes + + // QProcess::startDetached() has an obscure bug. If the path has + // no spaces and a comma(and maybe other special characters) it doesn't + // get wrapped in quotes. So explorer.exe can't find the correct path and + // displays the default one. If we wrap the path in quotes and pass it to + // QProcess::startDetached() explorer.exe still shows the default path. In + // this case QProcess::startDetached() probably puts its own quotes around ours. + + STARTUPINFO startupInfo; + ::ZeroMemory(&startupInfo, sizeof(startupInfo)); + startupInfo.cb = sizeof(startupInfo); + + PROCESS_INFORMATION processInfo; + ::ZeroMemory(&processInfo, sizeof(processInfo)); + + QString cmd = QString("explorer.exe /select,\"%1\"").arg(Utils::Fs::toNativePath(absolutePath)); + LPWSTR lpCmd = new WCHAR[cmd.size() + 1]; + cmd.toWCharArray(lpCmd); + lpCmd[cmd.size()] = 0; + + bool ret = ::CreateProcessW(NULL, lpCmd, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo); + delete [] lpCmd; + + if (ret) { + ::CloseHandle(processInfo.hProcess); + ::CloseHandle(processInfo.hThread); } #elif defined(Q_OS_UNIX) && !defined(Q_OS_MAC) - if (QFileInfo(path).exists()) { - QProcess proc; - proc.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory"); - proc.waitForFinished(); - QString output = proc.readLine().simplified(); - if ((output == "dolphin.desktop") || (output == "org.kde.dolphin.desktop")) - proc.startDetached("dolphin", QStringList() << "--select" << Utils::Fs::toNativePath(path)); - else if ((output == "nautilus.desktop") || (output == "org.gnome.Nautilus.desktop") - || (output == "nautilus-folder-handler.desktop")) - proc.startDetached("nautilus", QStringList() << "--no-desktop" << Utils::Fs::toNativePath(path)); - else if (output == "nemo.desktop") - proc.startDetached("nemo", QStringList() << "--no-desktop" << Utils::Fs::toNativePath(path)); - else if ((output == "konqueror.desktop") || (output == "kfmclient_dir.desktop")) - proc.startDetached("konqueror", QStringList() << "--select" << Utils::Fs::toNativePath(path)); - else - // "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003 - openPath(path.left(path.lastIndexOf("/"))); - } - else { - // If the item to select doesn't exist, try to open its parent + QProcess proc; + proc.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory"); + proc.waitForFinished(); + QString output = proc.readLine().simplified(); + if ((output == "dolphin.desktop") || (output == "org.kde.dolphin.desktop")) + proc.startDetached("dolphin", QStringList() << "--select" << Utils::Fs::toNativePath(path)); + else if ((output == "nautilus.desktop") || (output == "org.gnome.Nautilus.desktop") + || (output == "nautilus-folder-handler.desktop")) + proc.startDetached("nautilus", QStringList() << "--no-desktop" << Utils::Fs::toNativePath(path)); + else if (output == "nemo.desktop") + proc.startDetached("nemo", QStringList() << "--no-desktop" << Utils::Fs::toNativePath(path)); + else if ((output == "konqueror.desktop") || (output == "kfmclient_dir.desktop")) + proc.startDetached("konqueror", QStringList() << "--select" << Utils::Fs::toNativePath(path)); + else + // "caja" manager can't pinpoint the file, see: https://github.com/qbittorrent/qBittorrent/issues/5003 openPath(path.left(path.lastIndexOf("/"))); - } #else openPath(path.left(path.lastIndexOf("/"))); #endif