From 6aef6d7f4ecf785e1e9cde372d617de2dedee99a Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Sat, 27 May 2023 17:49:43 +0400 Subject: [PATCH] Integrate GApplication with QFileOpenEvent and Core::Application::activate --- Telegram/SourceFiles/core/application.cpp | 29 +++++++++++---- Telegram/SourceFiles/core/application.h | 4 ++ .../platform/linux/specific_linux.cpp | 37 +++---------------- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index fbc4c6fb1..8d8e7fc2f 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -107,6 +107,7 @@ namespace { constexpr auto kQuitPreventTimeoutMs = crl::time(1500); constexpr auto kAutoLockTimeoutLateMs = crl::time(3000); constexpr auto kClearEmojiImageSourceTimeout = 10 * crl::time(1000); +constexpr auto kFileOpenTimeoutMs = crl::time(1000); LaunchState GlobalLaunchState/* = LaunchState::Running*/; @@ -161,7 +162,8 @@ Application::Application(not_null launcher) , _langCloudManager(std::make_unique(langpack())) , _emojiKeywords(std::make_unique()) , _tray(std::make_unique()) -, _autoLockTimer([=] { checkAutoLock(); }) { +, _autoLockTimer([=] { checkAutoLock(); }) +, _fileOpenTimer([=] { checkFileOpen(); }) { Ui::Integration::Set(&_private->uiIntegration); _platformIntegration->init(); @@ -656,14 +658,21 @@ bool Application::eventFilter(QObject *object, QEvent *e) { case QEvent::FileOpen: { if (object == QCoreApplication::instance()) { const auto event = static_cast(e); - const auto url = QString::fromUtf8( - event->url().toEncoded().trimmed()); - if (url.startsWith(u"tg://"_q, Qt::CaseInsensitive)) { + if (const auto file = event->file(); !file.isEmpty()) { + _filesToOpen.append(file); + _fileOpenTimer.callOnce(kFileOpenTimeoutMs); + } else if (event->url().scheme() == u"tg"_q) { + const auto url = QString::fromUtf8( + event->url().toEncoded().trimmed()); cSetStartUrl(url.mid(0, 8192)); checkStartUrl(); - } - if (_lastActivePrimaryWindow && StartUrlRequiresActivate(url)) { - _lastActivePrimaryWindow->activate(); + if (_lastActivePrimaryWindow + && StartUrlRequiresActivate(url)) { + _lastActivePrimaryWindow->activate(); + } + } else if (event->url().scheme() == u"interpret"_q) { + _filesToOpen.append(event->url().toString()); + _fileOpenTimer.callOnce(kFileOpenTimeoutMs); } } } break; @@ -1033,6 +1042,12 @@ bool Application::canApplyLangPackWithoutRestart() const { return true; } +void Application::checkFileOpen() { + cSetSendPaths(_filesToOpen); + _filesToOpen.clear(); + checkSendPaths(); +} + void Application::checkSendPaths() { if (!cSendPaths().isEmpty() && _lastActivePrimaryWindow diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index 0a90308b9..ef65d170e 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -263,6 +263,7 @@ public: // Internal links. void checkStartUrl(); void checkSendPaths(); + void checkFileOpen(); bool openLocalUrl(const QString &url, QVariant context); bool openInternalUrl(const QString &url, QVariant context); [[nodiscard]] QString changelogLink() const; @@ -437,6 +438,9 @@ private: crl::time _shouldLockAt = 0; base::Timer _autoLockTimer; + QStringList _filesToOpen; + base::Timer _fileOpenTimer; + std::optional _saveSettingsTimer; struct LeaveFilter { diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 6fd0d7478..817997802 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -262,11 +262,8 @@ void LaunchGApplication() { app->signal_activate().connect([] { Core::Sandbox::Instance().customEnterFromEventLoop([] { - const auto window = Core::IsAppLaunched() - ? Core::App().activePrimaryWindow() - : nullptr; - if (window) { - window->activate(); + if (Core::IsAppLaunched()) { + Core::App().activate(); } }); }, true); @@ -276,33 +273,9 @@ void LaunchGApplication() { const Glib::ustring &hint) { Core::Sandbox::Instance().customEnterFromEventLoop([&] { for (const auto &file : files) { - if (file->get_uri_scheme() == "file") { - gSendPaths.append( - QString::fromStdString(file->get_path())); - continue; - } - const auto url = QString::fromStdString(file->get_uri()); - if (url.isEmpty()) { - continue; - } - if (url.startsWith(qstr("interpret://"))) { - gSendPaths.append(url); - continue; - } - if (Core::StartUrlRequiresActivate(url)) { - const auto window = Core::IsAppLaunched() - ? Core::App().activePrimaryWindow() - : nullptr; - if (window) { - window->activate(); - } - } - cSetStartUrl(url); - Core::App().checkStartUrl(); - } - - if (!cSendPaths().isEmpty()) { - Core::App().checkSendPaths(); + QFileOpenEvent e( + QUrl(QString::fromStdString(file->get_uri()))); + QGuiApplication::sendEvent(qApp, &e); } }); }, true);