From 4864a6996f8f0b06c56dce3b89825eaf7affc009 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Thu, 1 Aug 2024 14:47:33 +0200
Subject: [PATCH] Open links from tonsite:// externally.

---
 Telegram/SourceFiles/iv/iv_controller.cpp | 22 +++++++++++++++++-----
 Telegram/SourceFiles/iv/iv_controller.h   |  1 +
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/Telegram/SourceFiles/iv/iv_controller.cpp b/Telegram/SourceFiles/iv/iv_controller.cpp
index 067e90b485..11df085275 100644
--- a/Telegram/SourceFiles/iv/iv_controller.cpp
+++ b/Telegram/SourceFiles/iv/iv_controller.cpp
@@ -257,10 +257,15 @@ void Controller::initControls() {
 		_subtitleText.value(),
 		st::ivSubtitle);
 	_subtitle->setSelectable(true);
-	_subtitleText.value(
-	) | rpl::start_with_next([=](const QString &subtitle) {
+
+	_windowTitleText = _subtitleText.value(
+	) | rpl::map([=](const QString &subtitle) {
 		const auto prefix = tr::lng_iv_window_title(tr::now);
-		_window->setWindowTitle(prefix + ' ' + QChar(0x2014) + ' ' + subtitle);
+		return prefix + ' ' + QChar(0x2014) + ' ' + subtitle;
+	});
+	_windowTitleText.value(
+	) | rpl::start_with_next([=](const QString &title) {
+		_window->setWindowTitle(title);
 	}, _subtitle->lifetime());
 
 	_menuToggle.create(_subtitleWrap.get(), st::ivMenuToggle);
@@ -355,6 +360,7 @@ void Controller::showTonSite(
 	}) | rpl::map([=](QString value) {
 		return HttpsToTonsite(value);
 	});
+	_windowTitleText = _subtitleText;
 	_menuToggle->hide();
 }
 
@@ -488,7 +494,12 @@ void Controller::createWebview(const Webview::StorageId &storageId) {
 	}, _container->lifetime());
 
 	raw->setNavigationStartHandler([=](const QString &uri, bool newWindow) {
-		return true;
+		if (uri.startsWith(u"http://desktop-app-resource/"_q)
+			|| QUrl(uri).host().toLower().endsWith(u".magic.org"_q)) {
+			return true;
+		}
+		_events.fire({ .type = Event::Type::OpenLink, .url = uri });
+		return false;
 	});
 	raw->setNavigationDoneHandler([=](bool success) {
 	});
@@ -578,7 +589,8 @@ void Controller::createWebview(const Webview::StorageId &storageId) {
 				|| index >= _pages.size()) {
 				return Webview::DataResult::Failed;
 			}
-			return finishWith(WrapPage(_pages[index]), "text/html; charset=utf-8");
+			return finishWith(
+				WrapPage(_pages[index]), "text/html; charset=utf-8");
 		} else if (id.starts_with("page") && id.ends_with(".json")) {
 			auto index = 0;
 			const auto result = std::from_chars(
diff --git a/Telegram/SourceFiles/iv/iv_controller.h b/Telegram/SourceFiles/iv/iv_controller.h
index c92f4c4b2b..30e975d023 100644
--- a/Telegram/SourceFiles/iv/iv_controller.h
+++ b/Telegram/SourceFiles/iv/iv_controller.h
@@ -132,6 +132,7 @@ private:
 	std::unique_ptr<Ui::RpWidget> _subtitleWrap;
 	rpl::variable<QString> _url;
 	rpl::variable<QString> _subtitleText;
+	rpl::variable<QString> _windowTitleText;
 	std::unique_ptr<Ui::FlatLabel> _subtitle;
 	Ui::Animations::Simple _subtitleBackShift;
 	Ui::Animations::Simple _subtitleForwardShift;