From 9796c3d4224e7c5b15edca3ca0b158451809b973 Mon Sep 17 00:00:00 2001 From: kylinsecos_admin Date: Thu, 3 Mar 2022 14:46:14 +0800 Subject: [PATCH] feature(dbus): Add dbus method Setenv MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加dbus函数Setenv Signed-off-by: kylinsecos_admin --- src/org.gnome.SessionManager.xml | 10 ++++++++++ src/session-manager.cpp | 12 ++++++++++++ src/session-manager.h | 3 +++ src/utils.cpp | 18 +++++++++--------- src/utils.h | 6 +++--- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/org.gnome.SessionManager.xml b/src/org.gnome.SessionManager.xml index 60827ce..9b4cec5 100644 --- a/src/org.gnome.SessionManager.xml +++ b/src/org.gnome.SessionManager.xml @@ -116,6 +116,16 @@ Whether the user can reboot. + + + The environment variable name + + + The environment variable value + + Adds the variable name to the application launch environment with the specified value. May only be used during the Session Manager initialization phase. + + The inhibitor cookie. diff --git a/src/session-manager.cpp b/src/session-manager.cpp index 5047c59..7f57255 100644 --- a/src/session-manager.cpp +++ b/src/session-manager.cpp @@ -279,6 +279,18 @@ void SessionManager::CanReboot(MethodInvocation &invocation) invocation.ret(this->power_.can_power_action(PowerAction::POWER_ACTION_REBOOT)); } +void SessionManager::Setenv(const Glib::ustring &name, const Glib::ustring &value, MethodInvocation &invocation) +{ + KLOG_PROFILE("name: %s, value: %s.", name.c_str(), value.c_str()); + + if (this->current_phase_ > KSMPhase::KSM_PHASE_INITIALIZATION) + { + DBUS_ERROR_REPLY_AND_RET(KSMErrorCode::ERROR_MANAGER_PHASE_INVALID); + } + Utils::setenv(name, value); + invocation.ret(); +} + void SessionManager::init() { KLOG_PROFILE(""); diff --git a/src/session-manager.h b/src/session-manager.h index c8cf075..9d26c57 100644 --- a/src/session-manager.h +++ b/src/session-manager.h @@ -88,6 +88,9 @@ protected: virtual void RequestReboot(MethodInvocation &invocation); virtual void CanReboot(MethodInvocation &invocation); + // 添加会话程序的环境变量 + virtual void Setenv(const Glib::ustring &name, const Glib::ustring &value, MethodInvocation &invocation); + private: void init(); diff --git a/src/utils.cpp b/src/utils.cpp index e70cc98..0cbe117 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -87,13 +87,13 @@ std::vector Utils::get_autostart_dirs() return dirs; } -void Utils::setenv(const std::string &key, const std::string &value) +void Utils::setenv(const std::string &name, const std::string &value) { - KLOG_DEBUG("key: %s, value: %s.", key.c_str(), value.c_str()); + KLOG_DEBUG("name: %s, value: %s.", name.c_str(), value.c_str()); - Glib::setenv(key, value); - Utils::setenv_to_dbus(key, value); - Utils::setenv_to_systemd(key, value); + Glib::setenv(name, value); + Utils::setenv_to_dbus(name, value); + Utils::setenv_to_systemd(name, value); } void Utils::setenvs(const std::map &envs) @@ -196,7 +196,7 @@ std::string Utils::phase_enum2str(KSMPhase phase) return std::string(); } -void Utils::setenv_to_dbus(const std::string &key, const std::string &value) +void Utils::setenv_to_dbus(const std::string &name, const std::string &value) { try { @@ -206,7 +206,7 @@ void Utils::setenv_to_dbus(const std::string &key, const std::string &value) DAEMON_DBUS_INTERFACE_NAME); using param_type = std::tuple>; - std::map envs{{key, value}}; + std::map envs{{name, value}}; auto parameter = Glib::Variant::create(std::make_tuple(envs)); daemon_proxy->call_sync("UpdateActivationEnvironment", parameter); } @@ -217,7 +217,7 @@ void Utils::setenv_to_dbus(const std::string &key, const std::string &value) } } -void Utils::setenv_to_systemd(const std::string &key, const std::string &value) +void Utils::setenv_to_systemd(const std::string &name, const std::string &value) { try { @@ -226,7 +226,7 @@ void Utils::setenv_to_systemd(const std::string &key, const std::string &value) SYSTEMD_DBUS_OBJECT_PATH, SYSTEMD_DBUS_INTERFACE_NAME); using param_type = std::tuple>; - auto env = fmt::format("{0}={1}", key, value); + auto env = fmt::format("{0}={1}", name, value); auto parameter = Glib::Variant::create(std::make_tuple(std::vector{env})); systemd_proxy->call_sync("SetEnvironment", parameter); } diff --git a/src/utils.h b/src/utils.h index 2501734..d51de5b 100644 --- a/src/utils.h +++ b/src/utils.h @@ -32,7 +32,7 @@ public: // 获取自动启动程序的目录列表 static std::vector get_autostart_dirs(); // 设置环境变量 - static void setenv(const std::string &key, const std::string &value); + static void setenv(const std::string &name, const std::string &value); static void setenvs(const std::map &envs); // 生成cookie static uint32_t generate_cookie(); @@ -41,8 +41,8 @@ public: static std::string phase_enum2str(KSMPhase phase); private: - static void setenv_to_dbus(const std::string &key, const std::string &value); - static void setenv_to_systemd(const std::string &key, const std::string &value); + static void setenv_to_dbus(const std::string &name, const std::string &value); + static void setenv_to_systemd(const std::string &name, const std::string &value); private: static Glib::Rand rand_; -- 2.27.0