1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
*
* Copyright: 2023, KylinSoft Co., Ltd.
* Copyright: 2012 Razor team
*
* Authors:
* Christian Surlykke <christian@surlykke.dk>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "ukuipower.h"
#include "powerprovider.h"
#include <QDebug>
#include <QDBusInterface>
#include <QDBusReply>
UkuiPower::UkuiPower(QObject *parent) : QObject(parent)
{
m_systemdProvider = new SystemdProvider(this);
}
UkuiPower::~UkuiPower()
{
}
bool UkuiPower::canAction(UkuiPower::Action action) const
{
//以下为代码结构调整
QString command;
switch (action) {
case PowerSwitchUser:
command = QLatin1String("canSwitch");
break;
case PowerHibernate:
command = QLatin1String("canHibernate");
break;
case PowerSuspend:
command = QLatin1String("canSuspend");
break;
case PowerMonitorOff:
command = QLatin1String("canLockscreen");
break;
case PowerLogout:
command = QLatin1String("canLogout");
break;
case PowerReboot:
command = QLatin1String("canReboot");
break;
case PowerShutdown:
command = QLatin1String("canPowerOff");
break;
default:
break;
}
QDBusInterface *sessionInterface = new QDBusInterface("org.gnome.SessionManager", "/org/gnome/SessionManager",
"org.gnome.SessionManager", QDBusConnection::sessionBus());
if (!sessionInterface->isValid()) {
qWarning() << "dbusCall: Session QDBusInterface is invalid";
return false;
}
QDBusReply<bool> testReply = sessionInterface->call(QLatin1String("canLockscreen"));
if (!testReply.isValid()) {
//解决老版本升级到新版本接口不兼容的问题,在session接口不存在的情况下,调用systemd的接口
QDBusError error = testReply.error();
if (error.type() == QDBusError::UnknownMethod) {
qDebug() <<"updating ! old ukui-session dose not have canAction method";
if (action == PowerLogout || action == PowerMonitorOff) {
return true;
}
return m_systemdProvider->canAction(action);
}
qDebug() <<"dbus error";
return false;
}
QDBusReply<bool> reply = sessionInterface->call(command);
return reply.value();
}
bool UkuiPower::doAction(UkuiPower::Action action)
{
QString command;
switch (action) {
case PowerSwitchUser:
command = QLatin1String("switchUser");
break;
case PowerHibernate:
command = QLatin1String("hibernate");
break;
case PowerSuspend:
command = QLatin1String("suspend");
break;
case PowerLogout:
command = QLatin1String("logout");
break;
case PowerReboot:
command = QLatin1String("reboot");
break;
case PowerShutdown:
command = QLatin1String("powerOff");
break;
default:
break;
}
QDBusInterface *sessionInterface = new QDBusInterface("org.gnome.SessionManager", "/org/gnome/SessionManager",
"org.gnome.SessionManager", QDBusConnection::sessionBus());
if (!sessionInterface->isValid()) {
qWarning() << "dbusCall: Session QDBusInterface is invalid";
return false;
}
QDBusMessage mes = sessionInterface->call(command);
if (!(mes.errorName().isEmpty())) {
//本来应该判断错误类别,考虑到运行效率,不做该判断
return m_systemdProvider->doAction(action);
}
return true;
}
|