27 #include <QtCore/QCoreApplication> 32 : managerIface(QLatin1String("org.freedesktop.PowerManagement"),
33 QLatin1String("/org/freedesktop/PowerManagement"),
34 QDBusConnection::sessionBus()),
35 policyAgentIface(QLatin1String("org.kde.
Solid.PowerManagement.PolicyAgent"),
36 QLatin1String("/org/kde/
Solid/PowerManagement/PolicyAgent"),
37 QDBusConnection::sessionBus()),
38 inhibitIface(QLatin1String("org.freedesktop.PowerManagement.Inhibit"),
39 QLatin1String("/org/freedesktop/PowerManagement/Inhibit"),
40 QDBusConnection::sessionBus()),
41 serviceWatcher(QLatin1String("org.kde.
Solid.PowerManagement"),
42 QDBusConnection::sessionBus(),
43 QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration),
44 powerSaveStatus(false)
46 serviceWatcher.addWatchedService(QLatin1String(
"org.freedesktop.PowerManagement"));
48 connect(&managerIface, SIGNAL(CanSuspendChanged(
bool)),
49 this, SLOT(slotCanSuspendChanged(
bool)));
50 connect(&managerIface, SIGNAL(CanHibernateChanged(
bool)),
51 this, SLOT(slotCanHibernateChanged(
bool)));
52 connect(&managerIface, SIGNAL(CanHybridSuspendChanged(
bool)),
53 this, SLOT(slotCanHybridSuspendChanged(
bool)));
54 connect(&managerIface, SIGNAL(PowerSaveStatusChanged(
bool)),
55 this, SLOT(slotPowerSaveStatusChanged(
bool)));
56 connect(&serviceWatcher, SIGNAL(serviceRegistered(QString)),
57 this, SLOT(slotServiceRegistered(QString)));
58 connect(&serviceWatcher, SIGNAL(serviceUnregistered(QString)),
59 this, SLOT(slotServiceUnregistered(QString)));
62 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String(
"org.kde.Solid.PowerManagement"))) {
63 slotServiceRegistered(QLatin1String(
"org.kde.Solid.PowerManagement"));
65 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String(
"org.freedesktop.PowerManagement"))) {
66 slotServiceRegistered(QLatin1String(
"org.freedesktop.PowerManagement"));
80 return globalPowerManager->powerSaveStatus;
85 return globalPowerManager->supportedSleepStates;
93 if (!globalPowerManager->supportedSleepStates.contains(state)) {
101 globalPowerManager->managerIface.Suspend();
104 globalPowerManager->managerIface.Hibernate();
111 QDBusReply<uint> reply;
112 if (globalPowerManager->policyAgentIface.isValid()) {
113 reply = globalPowerManager->policyAgentIface.AddInhibition(
115 QCoreApplication::applicationName(), reason);
118 reply = globalPowerManager->inhibitIface.Inhibit(QCoreApplication::applicationName(), reason);
129 if (globalPowerManager->policyAgentIface.isValid()) {
130 return globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
133 return globalPowerManager->inhibitIface.UnInhibit(cookie).isValid();
139 if (globalPowerManager->policyAgentIface.isValid()) {
140 QDBusReply<uint> reply = globalPowerManager->policyAgentIface.AddInhibition(
142 QCoreApplication::applicationName(), reason);
144 if (reply.isValid()) {
145 QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String(
"org.freedesktop.ScreenSaver"),
146 QLatin1String(
"/ScreenSaver"),
147 QLatin1String(
"org.freedesktop.ScreenSaver"),
148 QLatin1String(
"Inhibit"));
149 message << QCoreApplication::applicationName();
152 QDBusPendingReply<uint> ssReply = QDBusConnection::sessionBus().asyncCall(message);
153 ssReply.waitForFinished();
154 if (ssReply.isValid()) {
155 globalPowerManager->screensaverCookiesForPowerDevilCookies.insert(reply, ssReply.value());
170 if (globalPowerManager->policyAgentIface.isValid()) {
171 bool result = globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
173 if (globalPowerManager->screensaverCookiesForPowerDevilCookies.contains(cookie)) {
174 QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String(
"org.freedesktop.ScreenSaver"),
175 QLatin1String(
"/ScreenSaver"),
176 QLatin1String(
"org.freedesktop.ScreenSaver"),
177 QLatin1String(
"UnInhibit"));
178 message << globalPowerManager->screensaverCookiesForPowerDevilCookies.take(cookie);
179 QDBusConnection::sessionBus().asyncCall(message);
191 return globalPowerManager;
245 if (serviceName == QLatin1String(
"org.freedesktop.PowerManagement")) {
247 QDBusPendingReply<bool> suspendReply =
managerIface.CanSuspend();
248 suspendReply.waitForFinished();
251 QDBusPendingReply<bool> hibernateReply =
managerIface.CanHibernate();
252 hibernateReply.waitForFinished();
255 QDBusPendingReply<bool> hybridSuspendReply =
managerIface.CanHybridSuspend();
256 hybridSuspendReply.waitForFinished();
259 QDBusPendingReply<bool> saveStatusReply =
managerIface.GetPowerSaveStatus();
260 saveStatusReply.waitForFinished();
264 QDBusMessage call = QDBusMessage::createMethodCall(QLatin1String(
"org.kde.Solid.PowerManagement"),
265 QLatin1String(
"/org/kde/Solid/PowerManagement"),
266 QLatin1String(
"org.kde.Solid.PowerManagement"),
267 QLatin1String(
"backendCapabilities"));
268 QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
269 reply.waitForFinished();
271 if (reply.isValid() && reply.value() > 0) {
273 QDBusConnection::sessionBus().connect(QLatin1String(
"org.kde.Solid.PowerManagement"),
274 QLatin1String(
"/org/kde/Solid/PowerManagement/Actions/SuspendSession"),
275 QLatin1String(
"org.kde.Solid.PowerManagement.Actions.SuspendSession"),
276 QLatin1String(
"resumingFromSuspend"),
285 if (serviceName == QLatin1String(
"org.freedesktop.PowerManagement")) {
293 QDBusConnection::sessionBus().disconnect(QLatin1String(
"org.kde.Solid.PowerManagement"),
294 QLatin1String(
"/org/kde/Solid/PowerManagement"),
295 QLatin1String(
"org.kde.Solid.PowerManagement"),
296 QLatin1String(
"resumingFromSuspend"),
302 #include "powermanagement_p.moc" 303 #include "powermanagement.moc" SOLID_EXPORT void requestSleep(SleepState state, QObject *receiver, const char *member)
Requests that the system go to sleep.
SOLID_EXPORT bool stopSuppressingScreenPowerManagement(int cookie)
Tell the power management that a particular screen power management suppression is no longer needed...
SOLID_EXPORT bool appShouldConserveResources()
Retrieves a high level indication of how applications should behave according to the power management...
#define SOLID_GLOBAL_STATIC(TYPE, NAME)
~PowerManagementPrivate()
void slotPowerSaveStatusChanged(bool newState)
QSet< Solid::PowerManagement::SleepState > supportedSleepStates
void slotCanHybridSuspendChanged(bool newState)
SleepState
This enum type defines the different suspend methods.
OrgFreedesktopPowerManagementInterface managerIface
SOLID_EXPORT QSet< SleepState > supportedSleepStates()
Retrieves the set of suspend methods supported by the system.
void resumingFromSuspend()
This signal is emitted whenever the system is resuming from suspend.
void slotCanHibernateChanged(bool newState)
void appShouldConserveResourcesChanged(bool newState)
This signal is emitted when the AC adapter is plugged or unplugged.
SOLID_EXPORT int beginSuppressingScreenPowerManagement(const QString &reason=QString())
Tell the power management subsystem to suppress automatic screen power management until further notic...
void slotCanSuspendChanged(bool newState)
SOLID_EXPORT int beginSuppressingSleep(const QString &reason=QString())
Tell the power management subsystem to suppress automatic system sleep until further notice...
void slotServiceRegistered(const QString &serviceName)
SOLID_EXPORT bool stopSuppressingSleep(int cookie)
Tell the power management that a particular sleep suppression is no longer needed.
SOLID_EXPORT Notifier * notifier()
void slotServiceUnregistered(const QString &serviceName)