Better show in folder in Windows / Linux.

Inspired by https://github.com/qbittorrent/qBittorrent.
This commit is contained in:
John Preston 2017-05-07 16:28:58 +03:00
parent 291a6b73ab
commit a7e695d914
2 changed files with 36 additions and 7 deletions

View File

@ -61,10 +61,32 @@ QByteArray EscapeShell(const QByteArray &content) {
} // namespace internal
void UnsafeShowInFolder(const QString &filepath) {
Ui::hideLayer(true);
auto result = system(("xdg-open " + internal::EscapeShell(QFile::encodeName(QFileInfo(filepath).absoluteDir().absolutePath()))).constData());
if (result) {
LOG(("Failed to launch xdg-open"));
Ui::hideLayer(true); // Hide mediaview to make other apps visible.
auto absolutePath = QFileInfo(filepath).absoluteFilePath();
QProcess process;
process.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory");
process.waitForFinished();
auto output = QString::fromLatin1(process.readLine().simplified());
auto command = qsl("xdg-open");
auto arguments = QStringList();
if (output == qstr("dolphin.desktop") || output == qstr("org.kde.dolphin.desktop")) {
command = qsl("dolphin");
arguments << "--select" << absolutePath;
} else if (output == qstr("nautilus.desktop") || output == qstr("org.gnome.Nautilus.desktop") || output == qstr("nautilus-folder-handler.desktop")) {
command = qsl("nautilus");
arguments << "--no-desktop" << absolutePath;
} else if (output == qstr("nemo.desktop")) {
command = qsl("nemo");
arguments << "--no-desktop" << absolutePath;
} else if (output == qstr("konqueror.desktop") || output == qstr("kfmclient_dir.desktop")) {
command = qsl("konqueror");
arguments << "--select" << absolutePath;
} else {
arguments << QFileInfo(filepath).absoluteDir().absolutePath();
}
if (!process.startDetached(command, arguments)) {
LOG(("Failed to launch '%1 %2'").arg(command).arg(arguments.join(' ')));
}
}

View File

@ -266,9 +266,16 @@ void UnsafeLaunch(const QString &filepath) {
}
void UnsafeShowInFolder(const QString &filepath) {
auto pathEscaped = QDir::toNativeSeparators(filepath).replace('"', qsl("\"\""));
auto wstringParam = (qstr("/select,") + pathEscaped).toStdWString();
ShellExecute(0, 0, L"explorer", wstringParam.c_str(), 0, SW_SHOWNORMAL);
auto nativePath = QDir::toNativeSeparators(filepath);
auto wstringPath = nativePath.toStdWString();
if (auto pidl = ILCreateFromPathW(wstringPath.c_str())) {
SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);
ILFree(pidl);
} else {
auto pathEscaped = nativePath.replace('"', qsl("\"\""));
auto wstringParam = (qstr("/select,") + pathEscaped).toStdWString();
ShellExecute(0, 0, L"explorer", wstringParam.c_str(), 0, SW_SHOWNORMAL);
}
}
void PostprocessDownloaded(const QString &filepath) {