kiran-cpanel-power/0000-feature-upower-glib-yse-UPower-glib-to-determine-whe-b09567c6.patch
2022-01-20 17:15:29 +08:00

146 lines
4.3 KiB
Diff

From b09567c650c78c1d4699e092d56d783ae2568b7c Mon Sep 17 00:00:00 2001
From: liuxinhao <liuxinhao@kylinos.com.cn>
Date: Tue, 21 Dec 2021 14:18:47 +0800
Subject: [PATCH 0/5] feature(upower-glib): yse UPower-glib to determine
whether there is a battery option
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 使用upower-glib枚举出所有的设备判断是否存在电池选项
---
CMakeLists.txt | 7 +++++--
src/kcp-power-interface.cpp | 10 +++++++++-
src/upower-interface.cpp | 40 +++++++++++++++++++++++++++++++++++++
src/upower-interface.h | 13 ++++++++++++
4 files changed, 67 insertions(+), 3 deletions(-)
create mode 100644 src/upower-interface.cpp
create mode 100644 src/upower-interface.h
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 806bc1b..9b7831c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,7 @@ pkg_search_module(KIRAN_WIDGETS_QT5 REQUIRED kiranwidgets-qt5)
pkg_search_module(KIRAN_CC_DAEMON REQUIRED kiran-cc-daemon)
pkg_search_module(KLOG_QT5 REQUIRED klog-qt5)
pkg_search_module(QGSETTINGS REQUIRED gsettings-qt)
+pkg_search_module(UPOWER_GLIB REQUIRED upower-glib)
#通过kiran control panel的pkgconfig配置文件取出插件Desktop安装位置、插件共享库安装位置
pkg_search_module(KIRAN_CONTROL_PANEL_PKG REQUIRED kiran-control-panel)
@@ -65,7 +66,8 @@ target_include_directories(${PROJECT_NAME} PRIVATE
${KIRAN_CC_DAEMON_INCLUDE_DIRS}
${KLOG_QT5_INCLUDE_DIRS}
${KIRAN_CONTROL_PANEL_PKG_INCLUDE_DIRS}
- ${QGSETTINGS_INCLUDE_DIRS})
+ ${QGSETTINGS_INCLUDE_DIRS}
+ ${UPOWER_GLIB_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME}
Qt5::Widgets
@@ -73,7 +75,8 @@ target_link_libraries(${PROJECT_NAME}
${KIRAN_WIDGETS_QT5_LIBRARIES}
${KIRAN_CC_DAEMON_LIBRARIES}
${KLOG_QT5_LIBRARIES}
- ${QGSETTINGS_LIBRARIES})
+ ${QGSETTINGS_LIBRARIES}
+ ${UPOWER_GLIB_LIBRARIES})
include(GNUInstallDirs)
# 安装插件翻译
diff --git a/src/kcp-power-interface.cpp b/src/kcp-power-interface.cpp
index b0f03c4..40a28c3 100644
--- a/src/kcp-power-interface.cpp
+++ b/src/kcp-power-interface.cpp
@@ -7,6 +7,7 @@
#include "config.h"
#include "general-settings-page.h"
#include "power-settings-page.h"
+#include "upower-interface.h"
#include <qt5-log-i.h>
@@ -71,5 +72,12 @@ bool KcpPowerInterface::haveUnsavedOptions()
QStringList KcpPowerInterface::visibleSubItems()
{
- return QStringList() << "GeneralSettings" << "PowerSettings" << "BatterySettings";
+ QStringList subItem({{"GeneralSettings"},{"PowerSettings"}});
+
+ if( UPowerInterface::haveBattery() )
+ {
+ subItem << "BatterySettings";
+ }
+
+ return subItem;
}
diff --git a/src/upower-interface.cpp b/src/upower-interface.cpp
new file mode 100644
index 0000000..3159718
--- /dev/null
+++ b/src/upower-interface.cpp
@@ -0,0 +1,40 @@
+//
+// Created by lxh on 2021/12/21.
+//
+
+#include "upower-interface.h"
+#include <upower.h>
+#include <cstdio>
+
+bool UPowerInterface::haveBattery()
+{
+ GError *error = nullptr;
+ auto upClient = up_client_new();
+ bool hasBattery = false;
+
+#if !UP_CHECK_VERSION(0, 99, 0)
+ gboolean ret;
+ ret = up_client_enumerate_devices_sync(upClient, NULL, &error);
+ if (!ret)
+ {
+ fprintf(stderr, "failed to get device list: %s", error->message);
+ g_error_free(error);
+ }
+#endif
+
+ auto devices = up_client_get_devices(upClient);
+ void *device = nullptr;
+ UpDeviceKind kind;
+ for (int i = 0; i < devices->len; i++)
+ {
+ device = g_ptr_array_index(devices, i);
+ g_object_get(device,
+ "kind", &kind,
+ NULL);
+ if (kind == UP_DEVICE_KIND_BATTERY)
+ hasBattery = true;
+ }
+ g_ptr_array_unref (devices);
+ g_object_unref(upClient);
+ return hasBattery;
+}
diff --git a/src/upower-interface.h b/src/upower-interface.h
new file mode 100644
index 0000000..1c90165
--- /dev/null
+++ b/src/upower-interface.h
@@ -0,0 +1,13 @@
+//
+// Created by lxh on 2021/12/21.
+//
+
+#ifndef KIRAN_POWER_MANAGER_SRC_U_POWER_INTERFACE_H_
+#define KIRAN_POWER_MANAGER_SRC_U_POWER_INTERFACE_H_
+
+namespace UPowerInterface
+{
+ bool haveBattery();
+};
+
+#endif //KIRAN_POWER_MANAGER_SRC_U_POWER_INTERFACE_H_
--
2.27.0