dde-dock/plugins/power/powerplugin.cpp
donghualin c78f101e84 fix: 修复从任务栏无法打开控制中心问题
由于控制中心适配了v23的接口,所以需要在任务栏增加USE_AM宏的控制,未使用USE_AM宏的调用com.deepin.dde.ControlCenter接口,使用USE_AM宏的使用org.deepin.dde.ControlCenter1接口。

Log: 优化任务栏打开控制中心的问题
Influence: 任务栏-任务栏设置-打开控制中心 任务栏-蓝牙-蓝牙设置 任务栏-日期-日期时间设置 任务栏-电源-电源设置 任务栏-关机-关机设置 任务栏-声音-声音设置 观察是否正常打开控制中心
Bug: https://pms.uniontech.com/bug-view-149189.html
Change-Id: I3373fc641c35e7589730a7cd595f2f55929ad2ec
2022-08-16 10:29:26 +00:00

339 lines
10 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: sbw <sbw@sbw.so>
*
* Maintainer: sbw <sbw@sbw.so>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "powerplugin.h"
#include "dbus/dbusaccount.h"
#include "../widgets/tipswidget.h"
#include "../frame/util/utils.h"
#include <QIcon>
#include <QGSettings>
#include <DDBusSender>
#define PLUGIN_STATE_KEY "enable"
#define DELAYTIME (20 * 1000)
using namespace Dock;
static QGSettings *GSettingsByApp()
{
static QGSettings settings("com.deepin.dde.dock.module.power");
return &settings;
}
PowerPlugin::PowerPlugin(QObject *parent)
: QObject(parent)
, m_pluginLoaded(false)
, m_showTimeToFull(true)
, m_powerStatusWidget(nullptr)
, m_tipsLabel(new TipsWidget)
, m_systemPowerInter(nullptr)
, m_powerInter(nullptr)
, m_preChargeTimer(new QTimer(this))
{
m_tipsLabel->setVisible(false);
m_tipsLabel->setObjectName("power");
m_preChargeTimer->setInterval(DELAYTIME);
m_preChargeTimer->setSingleShot(true);
connect(m_preChargeTimer,&QTimer::timeout,this,&PowerPlugin::refreshTipsData);
}
const QString PowerPlugin::pluginName() const
{
return "power";
}
const QString PowerPlugin::pluginDisplayName() const
{
return tr("Power");
}
QWidget *PowerPlugin::itemWidget(const QString &itemKey)
{
if (itemKey == POWER_KEY)
return m_powerStatusWidget.data();
return nullptr;
}
QWidget *PowerPlugin::itemTipsWidget(const QString &itemKey)
{
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
if (data.isEmpty()) {
return nullptr;
}
m_tipsLabel->setObjectName(itemKey);
refreshTipsData();
return m_tipsLabel.data();
}
void PowerPlugin::init(PluginProxyInterface *proxyInter)
{
m_proxyInter = proxyInter;
if (!pluginIsDisable()) {
loadPlugin();
}
}
void PowerPlugin::pluginStateSwitched()
{
m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, pluginIsDisable());
refreshPluginItemsVisible();
}
bool PowerPlugin::pluginIsDisable()
{
return !m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool();
}
const QString PowerPlugin::itemCommand(const QString &itemKey)
{
if (itemKey == POWER_KEY)
#ifdef USE_AM
return QString("dbus-send --print-reply --dest=org.deepin.dde.ControlCenter1 /org/deepin/dde/ControlCenter1 org.deepin.dde.ControlCenter1.ShowPage \"string:power\"");
#else
return QString("dbus-send --print-reply --dest=com.deepin.dde.ControlCenter /com/deepin/dde/ControlCenter com.deepin.dde.ControlCenter.ShowPage \"string:power\"");
#endif
return QString();
}
const QString PowerPlugin::itemContextMenu(const QString &itemKey)
{
if (itemKey != POWER_KEY) {
return QString();
}
QList<QVariant> items;
items.reserve(6);
if (!QFile::exists(ICBC_CONF_FILE)) {
QMap<QString, QVariant> power;
power["itemId"] = "power";
power["itemText"] = tr("Power settings");
power["isActive"] = true;
items.push_back(power);
}
QMap<QString, QVariant> menu;
menu["items"] = items;
menu["checkableMenu"] = false;
menu["singleCheck"] = false;
return QJsonDocument::fromVariant(menu).toJson();
}
void PowerPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
{
Q_UNUSED(itemKey)
Q_UNUSED(checked)
if (menuId == "power") {
#ifdef USE_AM
DDBusSender()
.service("org.deepin.dde.ControlCenter1")
.interface("org.deepin.dde.ControlCenter1")
.path("/org/deepin/dde/ControlCenter1")
.method(QString("ShowPage"))
.arg(QString("power"))
.call();
#else
DDBusSender()
.service("com.deepin.dde.ControlCenter")
.interface("com.deepin.dde.ControlCenter")
.path("/com/deepin/dde/ControlCenter")
.method(QString("ShowPage"))
.arg(QString("power"))
.call();
#endif
}
}
void PowerPlugin::refreshIcon(const QString &itemKey)
{
if (itemKey == POWER_KEY) {
m_powerStatusWidget->refreshIcon();
}
}
int PowerPlugin::itemSortKey(const QString &itemKey)
{
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
return m_proxyInter->getValue(this, key, 5).toInt();
}
void PowerPlugin::setSortKey(const QString &itemKey, const int order)
{
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
m_proxyInter->saveValue(this, key, order);
}
void PowerPlugin::pluginSettingsChanged()
{
refreshPluginItemsVisible();
}
QIcon PowerPlugin::icon(const DockPart &dockPart)
{
Q_UNUSED(dockPart);
static QIcon batteryIcon;
const QPixmap pixmap = m_powerStatusWidget->getBatteryIcon();
batteryIcon.detach();
batteryIcon.addPixmap(pixmap);
return batteryIcon;
}
void PowerPlugin::updateBatteryVisible()
{
const bool exist = !m_powerInter->batteryPercentage().isEmpty();
if (!exist)
m_proxyInter->itemRemoved(this, POWER_KEY);
else if (exist && !pluginIsDisable())
m_proxyInter->itemAdded(this, POWER_KEY);
}
void PowerPlugin::loadPlugin()
{
if (m_pluginLoaded) {
qDebug() << "power plugin has been loaded! return";
return;
}
m_pluginLoaded = true;
m_powerStatusWidget.reset(new PowerStatusWidget);
connect(m_powerStatusWidget.get(), &PowerStatusWidget::iconChanged, this, [ this ] {
m_proxyInter->itemUpdate(this, POWER_KEY);
});
m_powerInter = new DBusPower(this);
m_systemPowerInter = new SystemPowerInter("com.deepin.system.Power", "/com/deepin/system/Power", QDBusConnection::systemBus(), this);
m_systemPowerInter->setSync(true);
connect(GSettingsByApp(), &QGSettings::changed, this, &PowerPlugin::onGSettingsChanged);
connect(m_systemPowerInter, &SystemPowerInter::BatteryStatusChanged, [&](uint value) {
if (value == BatteryState::CHARGING)
m_preChargeTimer->start();
refreshTipsData();
});
connect(m_systemPowerInter, &SystemPowerInter::BatteryTimeToEmptyChanged, this, &PowerPlugin::refreshTipsData);
connect(m_systemPowerInter, &SystemPowerInter::BatteryTimeToFullChanged, this, &PowerPlugin::refreshTipsData);
connect(m_powerInter, &DBusPower::BatteryPercentageChanged, this, &PowerPlugin::updateBatteryVisible);
updateBatteryVisible();
onGSettingsChanged("showtimetofull");
}
void PowerPlugin::refreshPluginItemsVisible()
{
if (pluginIsDisable()) {
m_proxyInter->itemRemoved(this, POWER_KEY);
} else {
if (!m_pluginLoaded) {
loadPlugin();
return;
}
updateBatteryVisible();
}
}
void PowerPlugin::onGSettingsChanged(const QString &key)
{
if (key != "showtimetofull") {
return;
}
if (GSettingsByApp()->keys().contains("showtimetofull")) {
const bool isEnable = GSettingsByApp()->keys().contains("showtimetofull") && GSettingsByApp()->get("showtimetofull").toBool();
m_showTimeToFull = isEnable && GSettingsByApp()->get("showtimetofull").toBool();
}
refreshTipsData();
}
void PowerPlugin::refreshTipsData()
{
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
const uint percentage = qMin(100.0, qMax(0.0, data.value("Display")));
const QString value = QString("%1%").arg(std::round(percentage));
const int batteryState = m_powerInter->batteryState()["Display"];
if (m_preChargeTimer->isActive() && m_showTimeToFull) {
// 插入电源后20秒内算作预充电时间此时计算剩余充电时间是不准确的
QString tips = tr("Capacity %1 ...").arg(value);
m_tipsLabel->setText(tips);
return;
}
if (batteryState == BatteryState::DIS_CHARGING || batteryState == BatteryState::NOT_CHARGED || batteryState == BatteryState::UNKNOWN) {
QString tips;
qulonglong timeToEmpty = m_systemPowerInter->batteryTimeToEmpty();
QDateTime time = QDateTime::fromTime_t(timeToEmpty).toUTC();
uint hour = time.toString("hh").toUInt();
uint min = time.toString("mm").toUInt();
uint sencond = time.toString("ss").toInt();
if (sencond > 0)
min += 1;
if (!m_showTimeToFull) {
tips = tr("Capacity %1").arg(value);
} else {
if (hour == 0) {
tips = tr("Capacity %1, %2 min remaining").arg(value).arg(min);
} else {
tips = tr("Capacity %1, %2 hr %3 min remaining").arg(value).arg(hour).arg(min);
}
}
m_tipsLabel->setText(tips);
} else if (batteryState == BatteryState::FULLY_CHARGED || percentage == 100.) {
m_tipsLabel->setText(tr("Capacity %1, fully charged").arg(value));
} else {
qulonglong timeToFull = m_systemPowerInter->batteryTimeToFull();
QDateTime time = QDateTime::fromTime_t(timeToFull).toUTC();
uint hour = time.toString("hh").toUInt();
uint min = time.toString("mm").toUInt();
QString tips;
if (!m_showTimeToFull) {
tips = tr("Charging %1").arg(value);
} else {
if (timeToFull == 0) { // 电量已充満或电量计算中,剩余充满时间会返回0
tips = tr("Capacity %1 ...").arg(value);
} else {
hour == 0 ? tips = tr("Charging %1, %2 min until full").arg(value).arg(min)
: tips = tr("Charging %1, %2 hr %3 min until full").arg(value).arg(hour).arg(min);
}
}
m_tipsLabel->setText(tips);
}
}