From 26d4b8d60a30821c398ef4be3b6352f5041a56a3 Mon Sep 17 00:00:00 2001 From: liuxinhao Date: Mon, 15 Jan 2024 17:28:15 +0800 Subject: [PATCH 15/17] fix(power): profile mode using kiran-cc-daemon power interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 计算机模式接口使用控制中心后端所提供的接口 Closes #25242 --- plugins/power/CMakeLists.txt | 9 +- ...com.kylinsec.Kiran.SessionDaemon.Power.xml | 16 ++ .../power/data/net.hadess.PowerProfiles.xml | 144 ------------------ plugins/power/dbus/power-profiles-wrapper.cpp | 124 --------------- plugins/power/dbus/power-profiles-wrapper.h | 50 ------ plugins/power/pages/general-settings-page.cpp | 47 ++++-- plugins/power/pages/general-settings-page.h | 4 +- translations/kiran-control-panel.zh_CN.ts | 59 ++++--- 8 files changed, 86 insertions(+), 367 deletions(-) delete mode 100644 plugins/power/data/net.hadess.PowerProfiles.xml delete mode 100644 plugins/power/dbus/power-profiles-wrapper.cpp delete mode 100644 plugins/power/dbus/power-profiles-wrapper.h diff --git a/plugins/power/CMakeLists.txt b/plugins/power/CMakeLists.txt index 37d025d..99dc819 100644 --- a/plugins/power/CMakeLists.txt +++ b/plugins/power/CMakeLists.txt @@ -7,6 +7,7 @@ set(TARGET_NAME kiran-cpanel-power) pkg_search_module(UPOWER_GLIB REQUIRED upower-glib) set(POWER_PLUGIN_DBUS_SRC_LIST "") + # com.kylinsec.Kiran.SessionDaemon.Power set(KSD_POWER_XML data/com.kylinsec.Kiran.SessionDaemon.Power.xml) set_source_files_properties(${KSD_POWER_XML} @@ -17,13 +18,7 @@ kiran_qt5_add_dbus_interface_ex(KSD_POWER_SRC ksd_power_proxy KSDPowerProxy) list(APPEND POWER_PLUGIN_DBUS_SRC_LIST ${KSD_POWER_SRC}) -# net.hadess.PowerProfiles -set(POWERPROFILES_XML data/net.hadess.PowerProfiles.xml) -kiran_qt5_add_dbus_interface_ex(POWER_PROFILES_SRC - ${POWERPROFILES_XML} - power_profiles_proxy - PowerProfilesProxy) -list(APPEND POWER_PLUGIN_DBUS_SRC_LIST ${POWER_PROFILES_SRC}) + # org.gnome.SessionManager.xml - kiran-session-manager set(GNOME_SESSION_MANAGER_XML data/org.gnome.SessionManager.xml) kiran_qt5_add_dbus_interface_ex(GNOME_SESSION_MANAGER_SOUCE diff --git a/plugins/power/data/com.kylinsec.Kiran.SessionDaemon.Power.xml b/plugins/power/data/com.kylinsec.Kiran.SessionDaemon.Power.xml index 281ffdb..c32bbb5 100644 --- a/plugins/power/data/com.kylinsec.Kiran.SessionDaemon.Power.xml +++ b/plugins/power/data/com.kylinsec.Kiran.SessionDaemon.Power.xml @@ -82,6 +82,13 @@ Enable cpu save energy when the ups or battery power is low. + + + The profile mode. Refer to PowerProfileMode in power-i.h + + Switch profile mode. + + Indicates whether the system is running on battery power. @@ -107,6 +114,11 @@ Whether does the cpu save energy when the ups or battery power is low. + + + The active profile mode. + + @@ -120,5 +132,9 @@ + + + + diff --git a/plugins/power/data/net.hadess.PowerProfiles.xml b/plugins/power/data/net.hadess.PowerProfiles.xml deleted file mode 100644 index a3241fb..0000000 --- a/plugins/power/data/net.hadess.PowerProfiles.xml +++ /dev/null @@ -1,144 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/plugins/power/dbus/power-profiles-wrapper.cpp b/plugins/power/dbus/power-profiles-wrapper.cpp deleted file mode 100644 index 6262fdc..0000000 --- a/plugins/power/dbus/power-profiles-wrapper.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Copyright (c) 2020 ~ 2023 KylinSec Co., Ltd. - * kiran-control-panel is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, - * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, - * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - * - * Author: liuxinhao - */ -#include "power-profiles-wrapper.h" -#include -#include -#include -#include -#include -#include "power_profiles_proxy.h" - -#define POWERPROFILES_SERVICE_NAME "net.hadess.PowerProfiles" -#define POWERPROFILES_SERVICE_PATH "/net/hadess/PowerProfiles" -#define POWERPROFILES_SERVICE_PROPERTY_ACTIVE_PROFILE "ActiveProfile" -#define POWERPROFILES_SERVICE_PROPERTY_PROFILES "Profiles" - -#define FREEDESKTOP_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties" -#define FREEDESKTOP_PROPERTIES_METHOD_GET "Get" -#define FREEDESKTOP_PROPERTIES_METHOD_SET "Set" -#define FREEDESKTOP_PROPERTIES_SIGNAL_PROPERTIES_CHANGED "PropertiesChanged" - -Q_DECLARE_METATYPE(QList) -PowerProfilesWrapper::PowerProfilesWrapper(QObject* parent) - : QObject(parent), - m_profileProxy(new PowerProfilesProxy(POWERPROFILES_SERVICE_NAME, POWERPROFILES_SERVICE_PATH, QDBusConnection::systemBus(), this)) -{ - qDBusRegisterMetaType>(); - if (QDBusConnection::systemBus().interface()->isServiceRegistered(POWERPROFILES_SERVICE_NAME)) - { - m_isValid = true; - auto powerProfiles = m_profileProxy->profiles(); - for (auto powerProfile : powerProfiles) - { - KLOG_DEBUG() << powerProfile["Profile"].toString()<< powerProfile["Driver"].toString(); - if (powerProfile.contains("Profile")) - { - m_profiles << profileNameTransToLocale(powerProfile["Profile"].toString()); - } - } - - auto activeProfile = m_profileProxy->activeProfile(); - m_activeProfile = activeProfile; - connect(m_profileProxy, &PowerProfilesProxy::ActiveProfileChanged, - this, &PowerProfilesWrapper::onActiveProfileChanged); - } - else - { - KLOG_WARNING() << "power profiles init failed," << POWERPROFILES_SERVICE_NAME << "isn't registered"; - } -} - -PowerProfilesWrapper::~PowerProfilesWrapper() -{ -} - -bool PowerProfilesWrapper::isValid() -{ - return m_isValid; -} - -QStringList PowerProfilesWrapper::supportedProfiles() -{ - return m_profiles; -} - -QString PowerProfilesWrapper::activeProfile() -{ - return profileNameTransToLocale(m_activeProfile); -} - -void PowerProfilesWrapper::setActiveProfile(const QString& profileName) -{ - QString value = localeTransToProfileName(profileName); - m_profileProxy->setActiveProfile(value); -} - -void PowerProfilesWrapper::onActiveProfileChanged(const QString& profileName) -{ - m_activeProfile = profileName; - QString localeProfileName = profileNameTransToLocale(profileName); - emit activeProfileChanged(localeProfileName); -} - -QString PowerProfilesWrapper::profileNameTransToLocale(const QString& profileName) -{ - QMap transMap = { - {"power-saver", tr("power-saver")}, - {"balanced", tr("balanced")}, - {"performance", tr("performance")}}; - - QString localeProfileName = profileName; - if (transMap.contains(profileName)) - { - localeProfileName = transMap[profileName]; - } - - return localeProfileName; -} - -QString PowerProfilesWrapper::localeTransToProfileName(const QString& locale) -{ - QMap transMap = { - {tr("power-saver"), "power-saver"}, - {tr("balanced"), "balanced"}, - {tr("performance"), "performance"}}; - - QString profileName = locale; - if (transMap.contains(locale)) - { - profileName = transMap[locale]; - } - - return profileName; -} diff --git a/plugins/power/dbus/power-profiles-wrapper.h b/plugins/power/dbus/power-profiles-wrapper.h deleted file mode 100644 index 0e5723c..0000000 --- a/plugins/power/dbus/power-profiles-wrapper.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright (c) 2020 ~ 2023 KylinSec Co., Ltd. - * kiran-control-panel is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, - * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, - * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - * - * Author: liuxinhao - */ -#pragma once - -#include -#include - -class PowerProfilesProxy; -class PowerProfilesWrapper : public QObject -{ - Q_OBJECT -public: - PowerProfilesWrapper(QObject* parent = nullptr); - ~PowerProfilesWrapper(); - - bool isValid(); - - QStringList supportedProfiles(); - QString activeProfile(); - void setActiveProfile(const QString& profileName); - -private slots: - void onActiveProfileChanged(const QString& profileName); - -signals: - void activeProfileChanged(const QString& profileName); - -private: - // 由英文的ProfileName装换成本地locale的翻译文本 - QString profileNameTransToLocale(const QString& profileName); - // 由于locale的翻译文本,转换成英文的ProfileName - QString localeTransToProfileName(const QString& locale); - -private: - PowerProfilesProxy* m_profileProxy = nullptr; - bool m_isValid = false; - QString m_activeProfile; - QStringList m_profiles; -}; \ No newline at end of file diff --git a/plugins/power/pages/general-settings-page.cpp b/plugins/power/pages/general-settings-page.cpp index 9176a00..430d103 100644 --- a/plugins/power/pages/general-settings-page.cpp +++ b/plugins/power/pages/general-settings-page.cpp @@ -19,7 +19,6 @@ #include #include #include "dbus/kwin-color-correct.h" -#include "dbus/power-profiles-wrapper.h" #include "dbus/power.h" #include "kiran-message-box.h" #include "kiran-session-daemon/power-i.h" @@ -41,8 +40,7 @@ GeneralSettingsPage::GeneralSettingsPage(QWidget* parent) : QWidget(parent), ui(new Ui::GeneralSettingsPage), m_powerInterface(PowerInterface::getInstance()), - m_kwinColorCorrect(new KWinColorCorrect(this)), - m_powerprofiles(new PowerProfilesWrapper(this)) + m_kwinColorCorrect(new KWinColorCorrect(this)) { ui->setupUi(this); init(); @@ -147,13 +145,18 @@ void GeneralSettingsPage::initUI() } // 初始化计算机模式 - if (m_powerprofiles->isValid()) + struct PowerProfileInfo { - ui->combo_computerMode->addItems(m_powerprofiles->supportedProfiles()); - } - else + QString name; + int index; + }; + QList profiles = { + {tr("Energy-saving mode"), POWER_PROFILE_MODE_SAVER}, + {tr("Balanced mode"), POWER_PROFILE_MODE_BALANCED}, + {tr("High performance mode"), POWER_PROFILE_MODE_PERFORMANCE}}; + for (auto profile : profiles) { - ui->widget_computerMode->setVisible(false); + ui->combo_computerMode->addItem(profile.name, profile.index); } /// 初始化QSlider,和延迟设置的Timer @@ -209,7 +212,7 @@ void GeneralSettingsPage::initConnection() connect(ui->combo_closingLid, QOverload::of(&QComboBox::currentIndexChanged), this, &GeneralSettingsPage::updateEventAction); - connect(ui->combo_computerMode, &QComboBox::currentTextChanged, + connect(ui->combo_computerMode, QOverload::of(&QComboBox::currentIndexChanged), this, &GeneralSettingsPage::updateCurrentComputerMode); connect(&m_brightnessSettingTimer, &QTimer::timeout, @@ -271,10 +274,15 @@ void GeneralSettingsPage::load() updateEventActionComboCurrent(ui->combo_closingLid, POWER_EVENT_LID_CLOSED); // 计算机模式 - if (m_powerprofiles->isValid()) + QSignalBlocker blocker(ui->combo_computerMode); + int activePorfileComboxIdx = ui->combo_computerMode->findData(m_powerInterface->activeProfile()); + if (activePorfileComboxIdx != -1) { - QSignalBlocker blocker(ui->combo_computerMode); - ui->combo_computerMode->setCurrentText(m_powerprofiles->activeProfile()); + ui->combo_computerMode->setCurrentIndex(activePorfileComboxIdx); + } + else + { + KLOG_ERROR() << "can not fidn current active computer mode in combobox:" << m_powerInterface->activeProfile(); } // 显示器亮度调整 @@ -496,9 +504,20 @@ void GeneralSettingsPage::onSliderColorTempValueChanged(int value) m_colorTempSettingTimer.start(); } -void GeneralSettingsPage::updateCurrentComputerMode(const QString& text) +void GeneralSettingsPage::updateCurrentComputerMode(int idx) { - m_powerprofiles->setActiveProfile(text); + auto computerMode = ui->combo_computerMode->itemData(idx); + auto reply = m_powerInterface->SwitchProfile(computerMode.toInt()); + reply.waitForFinished(); + if (reply.isError()) + { + KLOG_ERROR() << "set current computer mode" << computerMode.toInt() + << "failed," << reply.error(); + } + else + { + KLOG_DEBUG() << "set current computer mode" << computerMode.toInt(); + } } void GeneralSettingsPage::setUiBrightnessPercent(int percent) diff --git a/plugins/power/pages/general-settings-page.h b/plugins/power/pages/general-settings-page.h index 63e8591..e270253 100644 --- a/plugins/power/pages/general-settings-page.h +++ b/plugins/power/pages/general-settings-page.h @@ -29,7 +29,6 @@ class PowerInterface; class KiranSwitchButton; class QGSettings; class KWinColorCorrect; -class PowerProfilesWrapper; class GeneralSettingsPage : public QWidget { Q_OBJECT @@ -62,13 +61,12 @@ private slots: void onSwitchAutoColorTempToggoled(bool checked); void updateColorTempatureValue(); void onSliderColorTempValueChanged(int value); - void updateCurrentComputerMode(const QString& text); + void updateCurrentComputerMode(int idx); private: Ui::GeneralSettingsPage *ui; PowerInterface* m_powerInterface; KWinColorCorrect* m_kwinColorCorrect; - PowerProfilesWrapper* m_powerprofiles; QTimer m_brightnessSettingTimer; QTimer m_idleTimeSettingTimer; QTimer m_colorTempSettingTimer; diff --git a/translations/kiran-control-panel.zh_CN.ts b/translations/kiran-control-panel.zh_CN.ts index 31fc01e..83cac3b 100644 --- a/translations/kiran-control-panel.zh_CN.ts +++ b/translations/kiran-control-panel.zh_CN.ts @@ -2297,50 +2297,65 @@ 待机时唤醒需要输入密码 - - + + shutdown 关机 - - - + + + hibernate 休眠 - - - + + + suspend 待机 - - + + display off 关闭显示器 - - - + + + do nothing 不执行操作 - + + Energy-saving mode + 节能模式 + + + + Balanced mode + 平衡模式 + + + + High performance mode + 高性能模式 + + + ERROR 错误 - + %1hour %1小时 - + %1minute %1分钟 @@ -4329,22 +4344,16 @@ This is line 50 of the test text PowerProfilesWrapper - - power-saver - 省电模式 + 省电模式 - - balanced - 平衡模式 + 平衡模式 - - performance - 性能模式 + 性能模式 -- 2.33.0