From 2f0d14bd35f657825694d13948d4241cf60eb985 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Mon, 21 Nov 2022 08:38:14 +0400 Subject: [PATCH] Use customWorkingDir() outside of Core::Launcher This allows to add -workdir to shortcuts only if the process was launched with -workdir --- Telegram/SourceFiles/core/application.cpp | 4 +- Telegram/SourceFiles/core/sandbox.cpp | 4 ++ Telegram/SourceFiles/core/sandbox.h | 1 + .../platform/linux/launcher_linux.cpp | 11 ++++++ .../platform/linux/launcher_linux.h | 2 + .../platform/linux/specific_linux.cpp | 39 ++++++++++++------- Telegram/lib_base | 2 +- 7 files changed, 48 insertions(+), 15 deletions(-) diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index 524b8111c..8114af38a 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -1446,7 +1446,9 @@ void Application::startShortcuts() { void Application::RegisterUrlScheme() { base::Platform::RegisterUrlScheme(base::Platform::UrlSchemeDescriptor{ .executable = cExeDir() + cExeName(), - .arguments = qsl("-workdir \"%1\"").arg(cWorkingDir()), + .arguments = Sandbox::Instance().customWorkingDir() + ? qsl("-workdir \"%1\"").arg(cWorkingDir()) + : QString(), .protocol = qsl("tg"), .protocolName = qsl("Telegram Link"), .shortAppName = qsl("tdesktop"), diff --git a/Telegram/SourceFiles/core/sandbox.cpp b/Telegram/SourceFiles/core/sandbox.cpp index 692f98a6d..875368122 100644 --- a/Telegram/SourceFiles/core/sandbox.cpp +++ b/Telegram/SourceFiles/core/sandbox.cpp @@ -511,6 +511,10 @@ void Sandbox::refreshGlobalProxy() { } } +bool Sandbox::customWorkingDir() const { + return _launcher->customWorkingDir(); +} + uint64 Sandbox::installationTag() const { return _launcher->installationTag(); } diff --git a/Telegram/SourceFiles/core/sandbox.h b/Telegram/SourceFiles/core/sandbox.h index d2f971c4d..098ab2c5f 100644 --- a/Telegram/SourceFiles/core/sandbox.h +++ b/Telegram/SourceFiles/core/sandbox.h @@ -41,6 +41,7 @@ public: int start(); void refreshGlobalProxy(); + bool customWorkingDir() const; uint64 installationTag() const; void postponeCall(FnMut &&callable); diff --git a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp index 2b1681dc2..38d2bbabe 100644 --- a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp @@ -24,6 +24,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Platform { namespace { +Launcher *LauncherInstance = nullptr; + class Arguments { public: void push(QByteArray argument) { @@ -48,6 +50,15 @@ private: Launcher::Launcher(int argc, char *argv[]) : Core::Launcher(argc, argv) , _arguments(argv, argv + argc) { + Expects(LauncherInstance == nullptr); + + LauncherInstance = this; +} + +Launcher &Launcher::Instance() { + Expects(LauncherInstance != nullptr); + + return *LauncherInstance; } int Launcher::exec() { diff --git a/Telegram/SourceFiles/platform/linux/launcher_linux.h b/Telegram/SourceFiles/platform/linux/launcher_linux.h index 36625a36a..99d7259ee 100644 --- a/Telegram/SourceFiles/platform/linux/launcher_linux.h +++ b/Telegram/SourceFiles/platform/linux/launcher_linux.h @@ -15,6 +15,8 @@ class Launcher : public Core::Launcher { public: Launcher(int argc, char *argv[]); + static Launcher &Instance(); + int exec() override; private: diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 68b3fb03d..ac36eec24 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/platform/base_platform_info.h" #include "platform/linux/linux_desktop_environment.h" #include "platform/linux/linux_wayland_integration.h" +#include "platform/platform_launcher.h" #include "lang/lang_keys.h" #include "mainwindow.h" #include "storage/localstorage.h" @@ -139,20 +140,21 @@ void PortalAutostart(bool start, bool silent) { const auto handleToken = Glib::ustring("tdesktop") + std::to_string(base::RandomValue()); + std::vector commandline; + commandline.push_back(cExeName().toStdString()); + if (Core::Sandbox::Instance().customWorkingDir()) { + commandline.push_back("-workdir"); + commandline.push_back(cWorkingDir().toStdString()); + } + commandline.push_back("-autostart"); + std::map options; options["handle_token"] = Glib::Variant::create( handleToken); options["reason"] = Glib::Variant::create( tr::lng_settings_auto_start(tr::now).toStdString()); options["autostart"] = Glib::Variant::create(start); - options["commandline"] = Glib::Variant>::create({ - cExeName().toStdString(), - "-workdir", - cWorkingDir().toStdString(), - "-autostart", - }); + options["commandline"] = base::Platform::MakeGlibVariant(commandline); options["dbus-activatable"] = Glib::Variant::create(false); auto uniqueName = connection->get_unique_name(); @@ -443,11 +445,11 @@ bool GenerateDesktopFile( qsl("^Exec=telegram-desktop(.*)$"), QRegularExpression::MultilineOption), qsl("Exec=%1\\1").arg( - KShell::joinArgs({ + KShell::joinArgs(QStringList{ cExeDir() + cExeName(), - "-workdir", - cWorkingDir(), - }).replace('\\', "\\\\"))); + } + (Core::Sandbox::Instance().customWorkingDir() + ? QStringList{ "-workdir", cWorkingDir() } + : QStringList{})).replace('\\', "\\\\"))); fileText = fileText.replace( QRegularExpression( @@ -661,7 +663,18 @@ void start() { } if (!Core::UpdaterDisabled()) { - return qsl("org.telegram.desktop.%1.desktop").arg(h); + QByteArray md5Hash(h); + if (!Launcher::Instance().customWorkingDir()) { + const auto exePath = QFile::encodeName( + cExeDir() + cExeName()); + + hashMd5Hex( + exePath.constData(), + exePath.size(), + md5Hash.data()); + } + + return qsl("org.telegram.desktop.%1.desktop").arg(md5Hash); } return qsl("org.telegram.desktop.desktop"); diff --git a/Telegram/lib_base b/Telegram/lib_base index d3f9e8261..e1e4c68c9 160000 --- a/Telegram/lib_base +++ b/Telegram/lib_base @@ -1 +1 @@ -Subproject commit d3f9e826197c6ed42f8ef2d021d8eb74b2d390cc +Subproject commit e1e4c68c91b2a56fd4d61cd843bb3bb5d4d99498