Commit 9c30ec61 authored by Lukáš Tinkl's avatar Lukáš Tinkl
Browse files

Make sure we have valid values for powermanager, watch for

unregistration to reset values

(hand forward port of 05f1a192 by Daniel
Nicoletti)

CCBUG: 300786
parent 673c1ca2
......@@ -39,14 +39,10 @@ Solid::PowerManagementPrivate::PowerManagementPrivate()
QDBusConnection::sessionBus()),
serviceWatcher("org.kde.Solid.PowerManagement",
QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForRegistration)
QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration),
powerSaveStatus(false)
{
powerSaveStatus = managerIface.GetPowerSaveStatus();
if (managerIface.CanSuspend())
supportedSleepStates+= Solid::PowerManagement::SuspendState;
if (managerIface.CanHibernate())
supportedSleepStates+= Solid::PowerManagement::HibernateState;
serviceWatcher.addWatchedService(QLatin1String("org.freedesktop.PowerManagement"));
connect(&managerIface, SIGNAL(CanSuspendChanged(bool)),
this, SLOT(slotCanSuspendChanged(bool)));
......@@ -56,11 +52,16 @@ Solid::PowerManagementPrivate::PowerManagementPrivate()
this, SLOT(slotPowerSaveStatusChanged(bool)));
connect(&serviceWatcher, SIGNAL(serviceRegistered(QString)),
this, SLOT(slotServiceRegistered(QString)));
connect(&serviceWatcher, SIGNAL(serviceUnregistered(QString)),
this, SLOT(slotServiceUnregistered(QString)));
// If the service is registered, trigger the connection immediately
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
slotServiceRegistered("org.kde.Solid.PowerManagement");
}
if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"))) {
slotServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"));
}
}
Solid::PowerManagementPrivate::~PowerManagementPrivate()
......@@ -186,6 +187,10 @@ Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier()
void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState)
{
if (supportedSleepStates.contains(Solid::PowerManagement::SuspendState) == newState) {
return;
}
if (newState) {
supportedSleepStates+= Solid::PowerManagement::SuspendState;
} else {
......@@ -195,6 +200,10 @@ void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState)
void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState)
{
if (supportedSleepStates.contains(Solid::PowerManagement::HibernateState) == newState) {
return;
}
if (newState) {
supportedSleepStates+= Solid::PowerManagement::HibernateState;
} else {
......@@ -204,31 +213,64 @@ void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState)
void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState)
{
if (powerSaveStatus == newState) {
return;
}
powerSaveStatus = newState;
Q_EMIT appShouldConserveResourcesChanged(powerSaveStatus);
}
void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName)
{
Q_UNUSED(serviceName);
// Is the resume signal available?
QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
"/org/kde/Solid/PowerManagement",
"org.kde.Solid.PowerManagement",
"backendCapabilities");
QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
reply.waitForFinished();
if (reply.isValid() && reply.value() > 0) {
// Connect the signal
QDBusConnection::sessionBus().connect("org.kde.Solid.PowerManagement",
"/org/kde/Solid/PowerManagement",
"org.kde.Solid.PowerManagement",
"resumingFromSuspend",
this,
SIGNAL(resumingFromSuspend()));
if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
// Load all the properties
QDBusPendingReply<bool> suspendReply = managerIface.CanSuspend();
suspendReply.waitForFinished();
slotCanSuspendChanged(suspendReply.isValid() ? suspendReply.value() : false);
QDBusPendingReply<bool> hibernateReply = managerIface.CanHibernate();
hibernateReply.waitForFinished();
slotCanHibernateChanged(hibernateReply.isValid() ? hibernateReply.value() : false);
QDBusPendingReply<bool> saveStatusReply = managerIface.GetPowerSaveStatus();
saveStatusReply.waitForFinished();
slotPowerSaveStatusChanged(saveStatusReply.isValid() ? saveStatusReply.value() : false);
} else {
// Is the resume signal available?
QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
"/org/kde/Solid/PowerManagement",
"org.kde.Solid.PowerManagement",
"backendCapabilities");
QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
reply.waitForFinished();
if (reply.isValid() && reply.value() > 0) {
// Connect the signal
QDBusConnection::sessionBus().connect("org.kde.Solid.PowerManagement",
"/org/kde/Solid/PowerManagement",
"org.kde.Solid.PowerManagement",
"resumingFromSuspend",
this,
SIGNAL(resumingFromSuspend()));
}
}
}
void Solid::PowerManagementPrivate::slotServiceUnregistered(const QString &serviceName)
{
if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
// Reset the values
slotCanSuspendChanged(false);
slotCanHibernateChanged(false);
slotPowerSaveStatusChanged(false);
} else {
// Disconnect the signal
QDBusConnection::sessionBus().disconnect(QLatin1String("org.kde.Solid.PowerManagement"),
QLatin1String("/org/kde/Solid/PowerManagement"),
QLatin1String("org.kde.Solid.PowerManagement"),
QLatin1String("resumingFromSuspend"),
this,
SIGNAL(resumingFromSuspend()));
}
}
......@@ -50,6 +50,7 @@ namespace Solid
void slotCanHibernateChanged(bool newState);
void slotPowerSaveStatusChanged(bool newState);
void slotServiceRegistered(const QString &serviceName);
void slotServiceUnregistered(const QString &serviceName);
public:
OrgFreedesktopPowerManagementInterface managerIface;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment