From a7e695d914ac2341ee5a32a1df1320089eec3aab Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Sun, 7 May 2017 16:28:58 +0300
Subject: [PATCH] Better show in folder in Windows / Linux.

Inspired by https://github.com/qbittorrent/qBittorrent.
---
 .../platform/linux/file_utilities_linux.cpp   | 30 ++++++++++++++++---
 .../platform/win/file_utilities_win.cpp       | 13 ++++++--
 2 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp b/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp
index fddf1771b..4cb9a9065 100644
--- a/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp
+++ b/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp
@@ -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(' ')));
 	}
 }
 
diff --git a/Telegram/SourceFiles/platform/win/file_utilities_win.cpp b/Telegram/SourceFiles/platform/win/file_utilities_win.cpp
index 83adda30a..45f4ddb8d 100644
--- a/Telegram/SourceFiles/platform/win/file_utilities_win.cpp
+++ b/Telegram/SourceFiles/platform/win/file_utilities_win.cpp
@@ -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) {