From 17bb430006c155305dbb5407fd3d081326af9b1b Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Tue, 2 Jul 2024 13:34:52 +0400
Subject: [PATCH] Register tg:// scheme on first launch.

Also, allow disabling tg:// re-registration on update.
---
 Telegram/SourceFiles/core/application.cpp     | 19 +++++++++++++++++--
 Telegram/SourceFiles/core/application.h       |  3 +++
 .../settings/settings_experimental.cpp        |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp
index c33b24c2a..e3e0de6c6 100644
--- a/Telegram/SourceFiles/core/application.cpp
+++ b/Telegram/SourceFiles/core/application.cpp
@@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "base/battery_saving.h"
 #include "base/event_filter.h"
 #include "base/concurrent_timer.h"
+#include "base/options.h"
 #include "base/qt_signal_producer.h"
 #include "base/timer.h"
 #include "base/unixtime.h"
@@ -140,10 +141,18 @@ void SetCrashAnnotationsGL() {
 #endif // DESKTOP_APP_USE_ANGLE
 }
 
+base::options::toggle OptionSkipUrlSchemeRegister({
+	.id = kOptionSkipUrlSchemeRegister,
+	.name = "Skip URL scheme register",
+	.description = "Don't re-register tg:// URL scheme on autoupdate.",
+});
+
 } // namespace
 
 Application *Application::Instance = nullptr;
 
+const char kOptionSkipUrlSchemeRegister[] = "skip-url-scheme-register";
+
 struct Application::Private {
 	base::Timer quitTimer;
 	UiIntegration uiIntegration;
@@ -263,12 +272,12 @@ void Application::run() {
 
 	if (const auto old = Local::oldSettingsVersion()) {
 		if (old < AppVersion) {
-			InvokeQueued(this, [] { RegisterUrlScheme(); });
+			autoRegisterUrlScheme();
 			Platform::NewVersionLaunched(old);
 		}
 	} else {
 		// Initial launch.
-		InvokeQueued(this, [] { RegisterUrlScheme(); });
+		autoRegisterUrlScheme();
 	}
 
 	if (cAutoStart() && !Platform::AutostartSupported()) {
@@ -410,6 +419,12 @@ void Application::run() {
 	processCreatedWindow(_lastActivePrimaryWindow);
 }
 
+void Application::autoRegisterUrlScheme() {
+	if (!OptionSkipUrlSchemeRegister.value()) {
+		InvokeQueued(this, [] { RegisterUrlScheme(); });
+	}
+}
+
 void Application::showAccount(not_null<Main::Account*> account) {
 	if (const auto separate = separateWindowFor(account)) {
 		_lastActivePrimaryWindow = separate;
diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h
index 77fe09b7f..eca07e751 100644
--- a/Telegram/SourceFiles/core/application.h
+++ b/Telegram/SourceFiles/core/application.h
@@ -126,6 +126,8 @@ enum class QuitReason {
 	QtQuitEvent,
 };
 
+extern const char kOptionSkipUrlSchemeRegister[];
+
 class Application final : public QObject {
 public:
 	struct ProxyChange {
@@ -349,6 +351,7 @@ private:
 	friend bool IsAppLaunched();
 	friend Application &App();
 
+	void autoRegisterUrlScheme();
 	void clearEmojiSourceImages();
 	[[nodiscard]] auto prepareEmojiSourceImages()
 		-> std::shared_ptr<Ui::Emoji::UniversalImages>;
diff --git a/Telegram/SourceFiles/settings/settings_experimental.cpp b/Telegram/SourceFiles/settings/settings_experimental.cpp
index 93e9e74dc..f578bce82 100644
--- a/Telegram/SourceFiles/settings/settings_experimental.cpp
+++ b/Telegram/SourceFiles/settings/settings_experimental.cpp
@@ -152,6 +152,7 @@ void SetupExperimental(
 	addToggle(kOptionAutoScrollInactiveChat);
 	addToggle(Window::Notifications::kOptionGNotification);
 	addToggle(Core::kOptionFreeType);
+	addToggle(Core::kOptionSkipUrlSchemeRegister);
 	addToggle(Data::kOptionExternalVideoPlayer);
 	addToggle(Window::kOptionNewWindowsSizeAsFirst);
 }