2023-02-16 13:51:55 +08:00
|
|
|
|
// Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
|
|
|
|
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
2022-09-06 11:36:55 +08:00
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
|
|
|
|
#include "shutdownplugin.h"
|
|
|
|
|
#include "dbus/dbusaccount.h"
|
2020-08-19 14:13:35 +08:00
|
|
|
|
#include "../frame/util/utils.h"
|
2020-06-29 15:35:51 +08:00
|
|
|
|
#include "../widgets/tipswidget.h"
|
2021-12-31 14:54:12 +08:00
|
|
|
|
#include "./dbus/dbuspowermanager.h"
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
2020-10-20 19:58:30 +08:00
|
|
|
|
#include <DSysInfo>
|
2020-12-10 17:10:59 +08:00
|
|
|
|
#include <DDBusSender>
|
2022-12-01 12:38:38 +08:00
|
|
|
|
#include <DGuiApplicationHelper>
|
2020-10-20 19:58:30 +08:00
|
|
|
|
|
2018-10-28 15:50:20 +08:00
|
|
|
|
#include <QIcon>
|
|
|
|
|
#include <QSettings>
|
2022-12-01 12:38:38 +08:00
|
|
|
|
#include <QPainter>
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
2021-02-27 15:50:24 +08:00
|
|
|
|
#define PLUGIN_STATE_KEY "enable"
|
|
|
|
|
#define GSETTING_SHOW_SUSPEND "showSuspend"
|
|
|
|
|
#define GSETTING_SHOW_HIBERNATE "showHibernate"
|
|
|
|
|
#define GSETTING_SHOW_SHUTDOWN "showShutdown"
|
|
|
|
|
#define GSETTING_SHOW_LOCK "showLock"
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
2020-10-20 19:58:30 +08:00
|
|
|
|
DCORE_USE_NAMESPACE
|
2022-12-01 12:38:38 +08:00
|
|
|
|
DGUI_USE_NAMESPACE
|
2020-06-29 15:35:51 +08:00
|
|
|
|
using namespace Dock;
|
|
|
|
|
|
2018-10-28 15:50:20 +08:00
|
|
|
|
ShutdownPlugin::ShutdownPlugin(QObject *parent)
|
2021-02-27 15:50:24 +08:00
|
|
|
|
: QObject(parent)
|
|
|
|
|
, m_pluginLoaded(false)
|
2021-05-25 13:05:45 +08:00
|
|
|
|
, m_shutdownWidget(nullptr)
|
2021-02-27 15:50:24 +08:00
|
|
|
|
, m_tipsLabel(new TipsWidget)
|
2022-11-18 17:08:32 +08:00
|
|
|
|
, m_powerManagerInter(new DBusPowerManager("org.deepin.dde.PowerManager1", "/org/deepin/dde/PowerManager1", QDBusConnection::systemBus(), this))
|
2021-03-20 12:10:45 +08:00
|
|
|
|
, m_gsettings(Utils::ModuleSettingsPtr("shutdown", QByteArray(), this))
|
2021-08-17 13:17:40 +08:00
|
|
|
|
, m_sessionShellGsettings(Utils::SettingsPtr("com.deepin.dde.session-shell", "/com/deepin/dde/session-shell/", this))
|
2018-10-28 15:50:20 +08:00
|
|
|
|
{
|
|
|
|
|
m_tipsLabel->setVisible(false);
|
2020-06-04 14:52:58 +08:00
|
|
|
|
m_tipsLabel->setAccessibleName("shutdown");
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString ShutdownPlugin::pluginName() const
|
|
|
|
|
{
|
|
|
|
|
return "shutdown";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString ShutdownPlugin::pluginDisplayName() const
|
|
|
|
|
{
|
2019-03-12 13:17:24 +08:00
|
|
|
|
return tr("Power");
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *ShutdownPlugin::itemWidget(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
return m_shutdownWidget.data();
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *ShutdownPlugin::itemTipsWidget(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2019-01-22 19:59:21 +08:00
|
|
|
|
// reset text every time to avoid size of LabelWidget not change after
|
|
|
|
|
// font size be changed in ControlCenter
|
2019-03-12 13:17:24 +08:00
|
|
|
|
m_tipsLabel->setText(tr("Power"));
|
2019-01-22 19:59:21 +08:00
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
return m_tipsLabel.data();
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShutdownPlugin::init(PluginProxyInterface *proxyInter)
|
|
|
|
|
{
|
|
|
|
|
m_proxyInter = proxyInter;
|
|
|
|
|
|
2018-11-08 11:39:26 +08:00
|
|
|
|
// transfer config
|
|
|
|
|
QSettings settings("deepin", "dde-dock-shutdown");
|
|
|
|
|
if (QFile::exists(settings.fileName())) {
|
|
|
|
|
QFile::remove(settings.fileName());
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 15:50:20 +08:00
|
|
|
|
if (!pluginIsDisable()) {
|
|
|
|
|
loadPlugin();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShutdownPlugin::pluginStateSwitched()
|
|
|
|
|
{
|
2018-11-08 11:39:26 +08:00
|
|
|
|
m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, !m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool());
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ShutdownPlugin::pluginIsDisable()
|
|
|
|
|
{
|
2018-11-08 11:39:26 +08:00
|
|
|
|
return !m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool();
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString ShutdownPlugin::itemCommand(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2022-11-18 17:08:32 +08:00
|
|
|
|
return QString("dbus-send --print-reply --dest=org.deepin.dde.ShutdownFront1 /org/deepin/dde/ShutdownFront1 org.deepin.dde.ShutdownFront1.Show");
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString ShutdownPlugin::itemContextMenu(const QString &itemKey)
|
|
|
|
|
{
|
2021-12-31 15:09:12 +08:00
|
|
|
|
Q_UNUSED(itemKey);
|
|
|
|
|
|
2018-10-28 15:50:20 +08:00
|
|
|
|
QList<QVariant> items;
|
|
|
|
|
items.reserve(6);
|
|
|
|
|
|
|
|
|
|
QMap<QString, QVariant> shutdown;
|
2021-03-20 12:10:45 +08:00
|
|
|
|
if (!m_gsettings || (m_gsettings->keys().contains(GSETTING_SHOW_SHUTDOWN) && m_gsettings->get(GSETTING_SHOW_SHUTDOWN).toBool())) {
|
2021-02-27 15:50:24 +08:00
|
|
|
|
shutdown["itemId"] = "Shutdown";
|
|
|
|
|
shutdown["itemText"] = tr("Shut down");
|
|
|
|
|
shutdown["isActive"] = true;
|
|
|
|
|
items.push_back(shutdown);
|
|
|
|
|
}
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
|
|
|
|
QMap<QString, QVariant> reboot;
|
|
|
|
|
reboot["itemId"] = "Restart";
|
2020-06-01 14:04:06 +08:00
|
|
|
|
reboot["itemText"] = tr("Reboot");
|
2018-10-28 15:50:20 +08:00
|
|
|
|
reboot["isActive"] = true;
|
|
|
|
|
items.push_back(reboot);
|
|
|
|
|
|
2019-03-25 13:37:58 +08:00
|
|
|
|
#ifndef DISABLE_POWER_OPTIONS
|
2020-02-17 17:28:42 +08:00
|
|
|
|
|
|
|
|
|
QProcessEnvironment enviromentVar = QProcessEnvironment::systemEnvironment();
|
|
|
|
|
bool can_sleep = enviromentVar.contains("POWER_CAN_SLEEP") ? QVariant(enviromentVar.value("POWER_CAN_SLEEP")).toBool()
|
2020-06-05 16:16:59 +08:00
|
|
|
|
: valueByQSettings<bool>("Power", "sleep", true) &&
|
2020-10-30 15:25:37 +08:00
|
|
|
|
m_powerManagerInter->CanSuspend();
|
2020-06-05 16:16:59 +08:00
|
|
|
|
;
|
|
|
|
|
if (can_sleep) {
|
2019-12-03 13:27:04 +08:00
|
|
|
|
QMap<QString, QVariant> suspend;
|
2021-03-20 12:10:45 +08:00
|
|
|
|
if (!m_gsettings || (m_gsettings->keys().contains(GSETTING_SHOW_SUSPEND) && m_gsettings->get(GSETTING_SHOW_SUSPEND).toBool())) {
|
2021-02-27 15:50:24 +08:00
|
|
|
|
suspend["itemId"] = "Suspend";
|
|
|
|
|
suspend["itemText"] = tr("Suspend");
|
|
|
|
|
suspend["isActive"] = true;
|
|
|
|
|
items.push_back(suspend);
|
|
|
|
|
}
|
2019-12-03 13:27:04 +08:00
|
|
|
|
}
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
2020-02-17 17:28:42 +08:00
|
|
|
|
bool can_hibernate = enviromentVar.contains("POWER_CAN_HIBERNATE") ? QVariant(enviromentVar.value("POWER_CAN_HIBERNATE")).toBool()
|
2020-10-30 15:25:37 +08:00
|
|
|
|
: checkSwap() && m_powerManagerInter->CanHibernate();
|
2020-02-21 12:06:26 +08:00
|
|
|
|
|
2020-02-17 17:28:42 +08:00
|
|
|
|
if (can_hibernate) {
|
2019-01-15 09:54:45 +08:00
|
|
|
|
QMap<QString, QVariant> hibernate;
|
2021-03-20 12:10:45 +08:00
|
|
|
|
if (!m_gsettings || (m_gsettings->keys().contains(GSETTING_SHOW_HIBERNATE) && m_gsettings->get(GSETTING_SHOW_HIBERNATE).toBool())) {
|
2021-02-27 15:50:24 +08:00
|
|
|
|
hibernate["itemId"] = "Hibernate";
|
|
|
|
|
hibernate["itemText"] = tr("Hibernate");
|
|
|
|
|
hibernate["isActive"] = true;
|
|
|
|
|
items.push_back(hibernate);
|
|
|
|
|
}
|
2019-01-15 09:54:45 +08:00
|
|
|
|
}
|
2020-02-17 17:28:42 +08:00
|
|
|
|
|
2019-03-25 13:37:58 +08:00
|
|
|
|
#endif
|
2019-01-15 09:54:45 +08:00
|
|
|
|
|
2018-10-28 15:50:20 +08:00
|
|
|
|
QMap<QString, QVariant> lock;
|
2021-03-20 12:10:45 +08:00
|
|
|
|
if (!m_gsettings || (m_gsettings->keys().contains(GSETTING_SHOW_LOCK) && m_gsettings->get(GSETTING_SHOW_LOCK).toBool())) {
|
2021-02-27 15:50:24 +08:00
|
|
|
|
lock["itemId"] = "Lock";
|
|
|
|
|
lock["itemText"] = tr("Lock");
|
|
|
|
|
lock["isActive"] = true;
|
|
|
|
|
items.push_back(lock);
|
|
|
|
|
}
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
|
|
|
|
QMap<QString, QVariant> logout;
|
|
|
|
|
logout["itemId"] = "Logout";
|
|
|
|
|
logout["itemText"] = tr("Log out");
|
|
|
|
|
logout["isActive"] = true;
|
|
|
|
|
items.push_back(logout);
|
|
|
|
|
|
2020-08-19 14:13:35 +08:00
|
|
|
|
if (!QFile::exists(ICBC_CONF_FILE)) {
|
2022-11-18 17:08:32 +08:00
|
|
|
|
// com.deepin.dde.session-shell切换用户配置项
|
2021-08-17 13:17:40 +08:00
|
|
|
|
enum SwitchUserConfig {
|
|
|
|
|
AlwaysShow = 0,
|
|
|
|
|
OnDemand,
|
|
|
|
|
Disabled
|
|
|
|
|
} switchUserConfig = OnDemand;
|
|
|
|
|
|
|
|
|
|
if (m_sessionShellGsettings && m_sessionShellGsettings->keys().contains("switchuser")) {
|
|
|
|
|
switchUserConfig = SwitchUserConfig(m_sessionShellGsettings->get("switchuser").toInt());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 和登录锁屏界面的逻辑保持一致
|
|
|
|
|
if (AlwaysShow == switchUserConfig ||
|
|
|
|
|
(OnDemand == switchUserConfig &&
|
|
|
|
|
(DBusAccount().userList().count() > 1 || DSysInfo::uosType() == DSysInfo::UosType::UosServer))) {
|
2020-08-19 14:13:35 +08:00
|
|
|
|
QMap<QString, QVariant> switchUser;
|
|
|
|
|
switchUser["itemId"] = "SwitchUser";
|
|
|
|
|
switchUser["itemText"] = tr("Switch account");
|
|
|
|
|
switchUser["isActive"] = true;
|
|
|
|
|
items.push_back(switchUser);
|
|
|
|
|
}
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
2019-03-25 13:37:58 +08:00
|
|
|
|
#ifndef DISABLE_POWER_OPTIONS
|
2020-08-19 14:13:35 +08:00
|
|
|
|
QMap<QString, QVariant> power;
|
|
|
|
|
power["itemId"] = "power";
|
|
|
|
|
power["itemText"] = tr("Power settings");
|
|
|
|
|
power["isActive"] = true;
|
|
|
|
|
items.push_back(power);
|
2019-03-25 13:37:58 +08:00
|
|
|
|
#endif
|
2020-08-19 14:13:35 +08:00
|
|
|
|
}
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
|
|
|
|
QMap<QString, QVariant> menu;
|
|
|
|
|
menu["items"] = items;
|
|
|
|
|
menu["checkableMenu"] = false;
|
|
|
|
|
menu["singleCheck"] = false;
|
|
|
|
|
|
|
|
|
|
return QJsonDocument::fromVariant(menu).toJson();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShutdownPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey)
|
|
|
|
|
Q_UNUSED(checked)
|
|
|
|
|
|
2020-10-10 11:03:12 +08:00
|
|
|
|
// 使得下面逻辑代码延迟200ms执行,保证线程不阻塞
|
|
|
|
|
QTime dieTime = QTime::currentTime().addMSecs(200);
|
2021-08-17 13:17:40 +08:00
|
|
|
|
while (QTime::currentTime() < dieTime)
|
2020-10-10 11:03:12 +08:00
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 200);
|
|
|
|
|
|
2020-12-10 17:10:59 +08:00
|
|
|
|
if (menuId == "power") {
|
|
|
|
|
DDBusSender()
|
2022-08-16 10:29:10 +00:00
|
|
|
|
.service("org.deepin.dde.ControlCenter1")
|
|
|
|
|
.interface("org.deepin.dde.ControlCenter1")
|
|
|
|
|
.path("/org/deepin/dde/ControlCenter1")
|
2022-06-14 10:46:42 +00:00
|
|
|
|
.method(QString("ShowPage"))
|
2020-12-10 17:10:59 +08:00
|
|
|
|
.arg(QString("power"))
|
|
|
|
|
.call();
|
2021-08-17 13:17:40 +08:00
|
|
|
|
} else if (menuId == "Lock") {
|
2020-08-19 14:13:35 +08:00
|
|
|
|
if (QFile::exists(ICBC_CONF_FILE)) {
|
2022-11-18 17:08:32 +08:00
|
|
|
|
QDBusMessage send = QDBusMessage::createMethodCall("org.deepin.dde.LockFront1", "/org/deepin/dde/LockFront1", "org.deepin.dde.LockFront1", "SwitchTTYAndShow");
|
2021-08-17 13:17:40 +08:00
|
|
|
|
QDBusConnection conn = QDBusConnection::connectToBus("unix:path=/run/user/1000/bus", "unix:path=/run/user/1000/bus");
|
2020-08-19 14:13:35 +08:00
|
|
|
|
QDBusMessage reply = conn.call(send);
|
|
|
|
|
#ifdef QT_DEBUG
|
2021-08-17 13:17:40 +08:00
|
|
|
|
qInfo() << "----------" << reply;
|
2020-08-19 14:13:35 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
} else {
|
2020-12-10 17:10:59 +08:00
|
|
|
|
DDBusSender()
|
2022-11-18 17:08:32 +08:00
|
|
|
|
.service("org.deepin.dde.LockFront1")
|
|
|
|
|
.interface("org.deepin.dde.LockFront1")
|
|
|
|
|
.path("/org/deepin/dde/LockFront1")
|
2020-12-10 17:10:59 +08:00
|
|
|
|
.method(QString("Show"))
|
|
|
|
|
.call();
|
2020-08-19 14:13:35 +08:00
|
|
|
|
}
|
2021-08-17 13:17:40 +08:00
|
|
|
|
} else
|
2020-12-10 17:10:59 +08:00
|
|
|
|
DDBusSender()
|
2022-11-18 17:08:32 +08:00
|
|
|
|
.service("org.deepin.dde.ShutdownFront1")
|
|
|
|
|
.interface("org.deepin.dde.ShutdownFront1")
|
|
|
|
|
.path("/org/deepin/dde/ShutdownFront1")
|
2020-12-10 17:10:59 +08:00
|
|
|
|
.method(QString(menuId))
|
|
|
|
|
.call();
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShutdownPlugin::displayModeChanged(const Dock::DisplayMode displayMode)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(displayMode);
|
|
|
|
|
|
2018-11-02 18:00:36 +08:00
|
|
|
|
if (!pluginIsDisable()) {
|
|
|
|
|
m_shutdownWidget->update();
|
|
|
|
|
}
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ShutdownPlugin::itemSortKey(const QString &itemKey)
|
|
|
|
|
{
|
2020-01-11 10:35:29 +08:00
|
|
|
|
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
|
2020-07-23 14:00:44 +08:00
|
|
|
|
return m_proxyInter->getValue(this, key, 5).toInt();
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShutdownPlugin::setSortKey(const QString &itemKey, const int order)
|
|
|
|
|
{
|
2020-01-11 10:35:29 +08:00
|
|
|
|
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
|
2018-11-08 11:39:26 +08:00
|
|
|
|
m_proxyInter->saveValue(this, key, order);
|
2018-10-28 15:50:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 15:41:34 +08:00
|
|
|
|
QIcon ShutdownPlugin::icon(const DockPart &dockPart, DGuiApplicationHelper::ColorType themeType)
|
2022-12-01 12:38:38 +08:00
|
|
|
|
{
|
|
|
|
|
if (dockPart == DockPart::DCCSetting) {
|
2023-12-07 16:04:05 +08:00
|
|
|
|
QString iconFile(":/icons/resources/icons/dcc_shutdown.svg");
|
|
|
|
|
auto pixmap = ImageUtil::loadSvg(iconFile, QSize(18, 18));
|
2022-12-01 12:38:38 +08:00
|
|
|
|
if (themeType == DGuiApplicationHelper::ColorType::LightType)
|
2023-12-07 16:04:05 +08:00
|
|
|
|
return pixmap;
|
2022-12-01 12:38:38 +08:00
|
|
|
|
|
|
|
|
|
QPainter pa(&pixmap);
|
|
|
|
|
pa.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
|
|
|
|
pa.fillRect(pixmap.rect(), Qt::white);
|
|
|
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString iconName = "system-shutdown";
|
|
|
|
|
|
|
|
|
|
if (themeType == DGuiApplicationHelper::LightType)
|
|
|
|
|
iconName.append(PLUGIN_MIN_ICON_NAME);
|
|
|
|
|
|
|
|
|
|
const auto ratio = qApp->devicePixelRatio();
|
|
|
|
|
QPixmap pixmap;
|
|
|
|
|
pixmap = QIcon::fromTheme(iconName, QIcon::fromTheme(":/icons/resources/icons/system-shutdown.svg")).pixmap(QSize(PLUGIN_ICON_MAX_SIZE, PLUGIN_ICON_MAX_SIZE) * ratio);
|
|
|
|
|
pixmap.setDevicePixelRatio(ratio);
|
|
|
|
|
return pixmap;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 14:37:54 +08:00
|
|
|
|
PluginFlags ShutdownPlugin::flags() const
|
2019-01-30 18:00:46 +08:00
|
|
|
|
{
|
2022-11-28 14:37:54 +08:00
|
|
|
|
return PluginFlag::Type_System | PluginFlag::Attribute_CanSetting;
|
2019-01-30 18:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 15:50:20 +08:00
|
|
|
|
void ShutdownPlugin::loadPlugin()
|
|
|
|
|
{
|
|
|
|
|
if (m_pluginLoaded) {
|
|
|
|
|
qDebug() << "shutdown plugin has been loaded! return";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_pluginLoaded = true;
|
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
m_shutdownWidget.reset(new ShutdownWidget);
|
2018-10-28 15:50:20 +08:00
|
|
|
|
|
2018-11-08 11:39:26 +08:00
|
|
|
|
m_proxyInter->itemAdded(this, pluginName());
|
2018-10-28 15:50:20 +08:00
|
|
|
|
displayModeChanged(displayMode());
|
|
|
|
|
}
|
2019-01-15 09:54:45 +08:00
|
|
|
|
|
2019-12-03 13:27:04 +08:00
|
|
|
|
std::pair<bool, qint64> ShutdownPlugin::checkIsPartitionType(const QStringList &list)
|
|
|
|
|
{
|
|
|
|
|
std::pair<bool, qint64> result{ false, -1 };
|
|
|
|
|
|
|
|
|
|
if (list.length() != 5) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString type{ list[1] };
|
|
|
|
|
const QString size{ list[2] };
|
|
|
|
|
|
|
|
|
|
result.first = type == "partition";
|
|
|
|
|
result.second = size.toLongLong() * 1024.0f;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 ShutdownPlugin::get_power_image_size()
|
|
|
|
|
{
|
|
|
|
|
qint64 size{ 0 };
|
|
|
|
|
QFile file("/sys/power/image_size");
|
|
|
|
|
|
|
|
|
|
if (file.open(QIODevice::Text | QIODevice::ReadOnly)) {
|
|
|
|
|
size = file.readAll().trimmed().toLongLong();
|
|
|
|
|
file.close();
|
2023-04-18 16:31:09 +08:00
|
|
|
|
} else{
|
|
|
|
|
qWarning() << "open /sys/power/image_size failed! please check permission!!!";
|
2019-12-03 13:27:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 09:54:45 +08:00
|
|
|
|
bool ShutdownPlugin::checkSwap()
|
2019-01-30 18:00:46 +08:00
|
|
|
|
{
|
2022-02-15 17:25:44 +08:00
|
|
|
|
if (!valueByQSettings<bool>("Power", "hibernate", true))
|
2019-12-03 13:27:04 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool hasSwap = false;
|
2019-01-15 09:54:45 +08:00
|
|
|
|
QFile file("/proc/swaps");
|
|
|
|
|
if (file.open(QIODevice::Text | QIODevice::ReadOnly)) {
|
|
|
|
|
const QString &body = file.readAll();
|
2019-12-03 13:27:04 +08:00
|
|
|
|
QTextStream stream(body.toUtf8());
|
|
|
|
|
while (!stream.atEnd()) {
|
|
|
|
|
const std::pair<bool, qint64> result =
|
2022-11-04 06:29:03 +00:00
|
|
|
|
checkIsPartitionType(stream.readLine().simplified().split(" ", Qt::SkipEmptyParts));
|
2019-12-03 13:27:04 +08:00
|
|
|
|
qint64 image_size{ get_power_image_size() };
|
|
|
|
|
|
|
|
|
|
if (result.first) {
|
|
|
|
|
hasSwap = image_size < result.second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hasSwap) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-01-30 18:00:46 +08:00
|
|
|
|
}
|
2019-12-03 13:27:04 +08:00
|
|
|
|
|
2019-01-15 09:54:45 +08:00
|
|
|
|
file.close();
|
2019-12-03 13:27:04 +08:00
|
|
|
|
} else {
|
2023-04-18 16:31:09 +08:00
|
|
|
|
qWarning() << "open /proc/swaps failed! please check permission!!!";
|
2019-01-30 18:00:46 +08:00
|
|
|
|
}
|
2019-01-15 09:54:45 +08:00
|
|
|
|
|
2019-12-03 13:27:04 +08:00
|
|
|
|
return hasSwap;
|
2019-01-30 18:00:46 +08:00
|
|
|
|
}
|