From 95b4f56b861ce497183843c0e2af34f8239b99e3 Mon Sep 17 00:00:00 2001
From: Ilya Fedin <fedin-ilja2010@ya.ru>
Date: Tue, 28 Apr 2020 12:26:12 +0400
Subject: [PATCH] Don't use QDesktopServices::openUrl on snap

---
 Telegram/SourceFiles/boxes/about_box.cpp      |  5 +--
 Telegram/SourceFiles/core/application.cpp     |  3 +-
 .../SourceFiles/core/click_handler_types.cpp  |  1 -
 Telegram/SourceFiles/core/file_utilities.cpp  | 11 +++++
 Telegram/SourceFiles/core/file_utilities.h    |  2 +
 Telegram/SourceFiles/core/ui_integration.cpp  |  4 +-
 .../history/history_location_manager.cpp      |  5 +--
 .../platform/linux/file_utilities_linux.cpp   | 40 +++++++++++++++++++
 .../platform/linux/file_utilities_linux.h     |  8 ----
 .../platform/mac/file_utilities_mac.h         |  4 ++
 .../platform/platform_file_utilities.h        |  1 +
 .../platform/win/file_utilities_win.h         |  4 ++
 .../SourceFiles/settings/settings_main.cpp    |  4 +-
 13 files changed, 72 insertions(+), 20 deletions(-)

diff --git a/Telegram/SourceFiles/boxes/about_box.cpp b/Telegram/SourceFiles/boxes/about_box.cpp
index 63883d663..f148f3c57 100644
--- a/Telegram/SourceFiles/boxes/about_box.cpp
+++ b/Telegram/SourceFiles/boxes/about_box.cpp
@@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "ui/widgets/buttons.h"
 #include "ui/widgets/labels.h"
 #include "ui/text/text_utilities.h"
-#include "platform/platform_file_utilities.h"
+#include "core/file_utilities.h"
 #include "base/platform/base_platform_info.h"
 #include "core/click_handler_types.h"
 #include "core/update_checker.h"
@@ -23,7 +23,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 #include <QtGui/QGuiApplication>
 #include <QtGui/QClipboard>
-#include <QtGui/QDesktopServices>
 
 namespace {
 
@@ -109,7 +108,7 @@ void AboutBox::showVersionHistory() {
 
 		Ui::show(Box<InformBox>("The link to the current private alpha version of Telegram Desktop was copied to the clipboard."));
 	} else {
-		QDesktopServices::openUrl(qsl("https://desktop.telegram.org/changelog"));
+		File::OpenUrl(qsl("https://desktop.telegram.org/changelog"));
 	}
 }
 
diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp
index 124abd256..584636fea 100644
--- a/Telegram/SourceFiles/core/application.cpp
+++ b/Telegram/SourceFiles/core/application.cpp
@@ -36,6 +36,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "observer_peer.h"
 #include "storage/storage_databases.h"
 #include "mainwidget.h"
+#include "core/file_utilities.h"
 #include "main/main_account.h"
 #include "media/view/media_view_overlay_widget.h"
 #include "mtproto/dc_options.h"
@@ -300,7 +301,7 @@ void Application::showDocument(not_null<DocumentData*> document, HistoryItem *it
 	if (cUseExternalVideoPlayer()
 		&& document->isVideoFile()
 		&& document->loaded()) {
-		QDesktopServices::openUrl(QUrl("file:///" + document->location(false).fname));
+		File::Launch(document->location(false).fname);
 	} else {
 		_mediaView->showDocument(document, item);
 		_mediaView->activateWindow();
diff --git a/Telegram/SourceFiles/core/click_handler_types.cpp b/Telegram/SourceFiles/core/click_handler_types.cpp
index e71db35d9..78265bf30 100644
--- a/Telegram/SourceFiles/core/click_handler_types.cpp
+++ b/Telegram/SourceFiles/core/click_handler_types.cpp
@@ -22,7 +22,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "facades.h"
 #include "app.h"
 
-#include <QtGui/QDesktopServices>
 #include <QtGui/QGuiApplication>
 
 namespace {
diff --git a/Telegram/SourceFiles/core/file_utilities.cpp b/Telegram/SourceFiles/core/file_utilities.cpp
index e70eb1ab3..950a48829 100644
--- a/Telegram/SourceFiles/core/file_utilities.cpp
+++ b/Telegram/SourceFiles/core/file_utilities.cpp
@@ -118,6 +118,13 @@ QString filedialogNextFilename(
 
 namespace File {
 
+void OpenUrl(const QString &url) {
+	crl::on_main([=] {
+		Ui::PreventDelayedActivation();
+		Platform::File::UnsafeOpenUrl(url);
+	});
+}
+
 void OpenEmailLink(const QString &email) {
 	crl::on_main([=] {
 		Ui::PreventDelayedActivation();
@@ -162,6 +169,10 @@ QString DefaultDownloadPath() {
 
 namespace internal {
 
+void UnsafeOpenUrlDefault(const QString &url) {
+	QDesktopServices::openUrl(url);
+}
+
 void UnsafeOpenEmailLinkDefault(const QString &email) {
 	auto url = QUrl(qstr("mailto:") + email);
 	QDesktopServices::openUrl(url);
diff --git a/Telegram/SourceFiles/core/file_utilities.h b/Telegram/SourceFiles/core/file_utilities.h
index 7a0b4c74f..2c5341109 100644
--- a/Telegram/SourceFiles/core/file_utilities.h
+++ b/Telegram/SourceFiles/core/file_utilities.h
@@ -30,6 +30,7 @@ QString filedialogNextFilename(
 namespace File {
 
 // Those functions are async wrappers to Platform::File::Unsafe* calls.
+void OpenUrl(const QString &url);
 void OpenEmailLink(const QString &email);
 void OpenWith(const QString &filepath, QPoint menuPosition);
 void Launch(const QString &filepath);
@@ -43,6 +44,7 @@ inline QString UrlToLocalDefault(const QUrl &url) {
 	return url.toLocalFile();
 }
 
+void UnsafeOpenUrlDefault(const QString &url);
 void UnsafeOpenEmailLinkDefault(const QString &email);
 void UnsafeLaunchDefault(const QString &filepath);
 
diff --git a/Telegram/SourceFiles/core/ui_integration.cpp b/Telegram/SourceFiles/core/ui_integration.cpp
index 07708466a..a5a03fa89 100644
--- a/Telegram/SourceFiles/core/ui_integration.cpp
+++ b/Telegram/SourceFiles/core/ui_integration.cpp
@@ -143,7 +143,9 @@ bool UiIntegration::handleUrlClick(
 		Core::App().openInternalUrl(local, context);
 		return true;
 	}
-	return false;
+
+	File::OpenUrl(url);
+	return true;
 
 }
 
diff --git a/Telegram/SourceFiles/history/history_location_manager.cpp b/Telegram/SourceFiles/history/history_location_manager.cpp
index 292dabec1..69e93ed00 100644
--- a/Telegram/SourceFiles/history/history_location_manager.cpp
+++ b/Telegram/SourceFiles/history/history_location_manager.cpp
@@ -8,13 +8,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "history/history_location_manager.h"
 
 #include "mainwidget.h"
+#include "core/file_utilities.h"
 #include "lang/lang_keys.h"
 #include "ui/image/image.h"
 #include "data/data_file_origin.h"
 #include "platform/platform_specific.h"
 
-#include <QtGui/QDesktopServices>
-
 namespace {
 
 constexpr auto kCoordPrecision = 8;
@@ -32,7 +31,7 @@ QString LocationClickHandler::copyToClipboardContextItemText() const {
 
 void LocationClickHandler::onClick(ClickContext context) const {
 	if (!psLaunchMaps(_point)) {
-		QDesktopServices::openUrl(_text);
+		File::OpenUrl(_text);
 	}
 }
 
diff --git a/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp b/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp
index 88f8a69a9..bb7445ef0 100644
--- a/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp
+++ b/Telegram/SourceFiles/platform/linux/file_utilities_linux.cpp
@@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 #include "platform/linux/linux_libs.h"
 #include "platform/linux/linux_gdk_helper.h"
+#include "platform/linux/specific_linux.h"
 #include "core/application.h"
 #include "mainwindow.h"
 #include "boxes/abstract_box.h"
@@ -18,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "facades.h"
 
 #include <QtCore/QProcess>
+#include <QtGui/QDesktopServices>
 
 #ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
 #include <private/qguiapplication_p.h>
@@ -57,6 +59,44 @@ QByteArray EscapeShell(const QByteArray &content) {
 
 } // namespace internal
 
+void UnsafeOpenUrl(const QString &url) {
+	if (InSnap()) {
+		const QStringList arguments{
+			url
+		};
+		QProcess process;
+		process.startDetached(qsl("xdg-open"), arguments);
+	} else {
+		QDesktopServices::openUrl(url);
+	}
+}
+
+void UnsafeOpenEmailLink(const QString &email) {
+	const auto url = qstr("mailto:") + email;
+
+	if (InSnap()) {
+		const QStringList arguments{
+			url
+		};
+		QProcess process;
+		process.startDetached(qsl("xdg-open"), arguments);
+	} else {
+		QDesktopServices::openUrl(QUrl(url));
+	}
+}
+
+void UnsafeLaunch(const QString &filepath) {
+	if (InSnap()) {
+		const QStringList arguments{
+			QFileInfo(filepath).absoluteFilePath()
+		};
+		QProcess process;
+		process.startDetached(qsl("xdg-open"), arguments);
+	} else {
+		QDesktopServices::openUrl(QUrl::fromLocalFile(filepath));
+	}
+}
+
 void UnsafeShowInFolder(const QString &filepath) {
 	// Hide mediaview to make other apps visible.
 	Ui::hideLayer(anim::type::instant);
diff --git a/Telegram/SourceFiles/platform/linux/file_utilities_linux.h b/Telegram/SourceFiles/platform/linux/file_utilities_linux.h
index cd15bcdd9..08de04aae 100644
--- a/Telegram/SourceFiles/platform/linux/file_utilities_linux.h
+++ b/Telegram/SourceFiles/platform/linux/file_utilities_linux.h
@@ -33,10 +33,6 @@ inline QString UrlToLocal(const QUrl &url) {
 	return ::File::internal::UrlToLocalDefault(url);
 }
 
-inline void UnsafeOpenEmailLink(const QString &email) {
-	return ::File::internal::UnsafeOpenEmailLinkDefault(email);
-}
-
 inline bool UnsafeShowOpenWithDropdown(const QString &filepath, QPoint menuPosition) {
 	return false;
 }
@@ -45,10 +41,6 @@ inline bool UnsafeShowOpenWith(const QString &filepath) {
 	return false;
 }
 
-inline void UnsafeLaunch(const QString &filepath) {
-	return ::File::internal::UnsafeLaunchDefault(filepath);
-}
-
 inline void PostprocessDownloaded(const QString &filepath) {
 }
 
diff --git a/Telegram/SourceFiles/platform/mac/file_utilities_mac.h b/Telegram/SourceFiles/platform/mac/file_utilities_mac.h
index 284474e16..aacb80c4c 100644
--- a/Telegram/SourceFiles/platform/mac/file_utilities_mac.h
+++ b/Telegram/SourceFiles/platform/mac/file_utilities_mac.h
@@ -12,6 +12,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 namespace Platform {
 namespace File {
 
+inline void UnsafeOpenUrl(const QString &url) {
+	return ::File::internal::UnsafeOpenUrlDefault(url);
+}
+
 inline void UnsafeOpenEmailLink(const QString &email) {
 	return ::File::internal::UnsafeOpenEmailLinkDefault(email);
 }
diff --git a/Telegram/SourceFiles/platform/platform_file_utilities.h b/Telegram/SourceFiles/platform/platform_file_utilities.h
index 68b1058b1..b78adf552 100644
--- a/Telegram/SourceFiles/platform/platform_file_utilities.h
+++ b/Telegram/SourceFiles/platform/platform_file_utilities.h
@@ -15,6 +15,7 @@ namespace File {
 QString UrlToLocal(const QUrl &url);
 
 // All these functions may enter a nested event loop. Use with caution.
+void UnsafeOpenUrl(const QString &url);
 void UnsafeOpenEmailLink(const QString &email);
 bool UnsafeShowOpenWithDropdown(const QString &filepath, QPoint menuPosition);
 bool UnsafeShowOpenWith(const QString &filepath);
diff --git a/Telegram/SourceFiles/platform/win/file_utilities_win.h b/Telegram/SourceFiles/platform/win/file_utilities_win.h
index c7c3f598d..3647b1ca5 100644
--- a/Telegram/SourceFiles/platform/win/file_utilities_win.h
+++ b/Telegram/SourceFiles/platform/win/file_utilities_win.h
@@ -16,5 +16,9 @@ inline QString UrlToLocal(const QUrl &url) {
 	return url.toLocalFile();
 }
 
+inline void UnsafeOpenUrl(const QString &url) {
+	return ::File::internal::UnsafeOpenUrlDefault(url);
+}
+
 } // namespace File
 } // namespace Platform
diff --git a/Telegram/SourceFiles/settings/settings_main.cpp b/Telegram/SourceFiles/settings/settings_main.cpp
index 6bcf8dbdd..460ac7c86 100644
--- a/Telegram/SourceFiles/settings/settings_main.cpp
+++ b/Telegram/SourceFiles/settings/settings_main.cpp
@@ -36,8 +36,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "app.h"
 #include "styles/style_settings.h"
 
-#include <QtGui/QDesktopServices>
-
 namespace Settings {
 
 void SetupLanguageButton(
@@ -271,7 +269,7 @@ void SetupInterfaceScale(
 }
 
 void OpenFaq() {
-	QDesktopServices::openUrl(telegramFaqLink());
+	File::OpenUrl(telegramFaqLink());
 }
 
 void SetupFaq(not_null<Ui::VerticalLayout*> container, bool icon) {