• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.2 API Reference
  • KDE Home
  • Contact Us
 

Solid

  • solid
  • solid
powermanagement.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2007 Kevin Ottens <ervin@kde.org>
3  Copyright 2013 Lukas Tinkl <ltinkl@redhat.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) version 3, or any
9  later version accepted by the membership of KDE e.V. (or its
10  successor approved by the membership of KDE e.V.), which shall
11  act as a proxy defined in Section 6 of version 3 of the license.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "powermanagement.h"
23 #include "powermanagement_p.h"
24 
25 #include "soliddefs_p.h"
26 
27 #include <QtCore/QCoreApplication>
28 
29 SOLID_GLOBAL_STATIC(Solid::PowerManagementPrivate, globalPowerManager)
30 
31 Solid::PowerManagementPrivate::PowerManagementPrivate()
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)
45 {
46  serviceWatcher.addWatchedService(QLatin1String("org.freedesktop.PowerManagement"));
47 
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)));
60 
61  // If the service is registered, trigger the connection immediately
62  if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.Solid.PowerManagement"))) {
63  slotServiceRegistered(QLatin1String("org.kde.Solid.PowerManagement"));
64  }
65  if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"))) {
66  slotServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"));
67  }
68 }
69 
70 Solid::PowerManagementPrivate::~PowerManagementPrivate()
71 {
72 }
73 
74 Solid::PowerManagement::Notifier::Notifier()
75 {
76 }
77 
78 bool Solid::PowerManagement::appShouldConserveResources()
79 {
80  return globalPowerManager->powerSaveStatus;
81 }
82 
83 QSet<Solid::PowerManagement::SleepState> Solid::PowerManagement::supportedSleepStates()
84 {
85  return globalPowerManager->supportedSleepStates;
86 }
87 
88 void Solid::PowerManagement::requestSleep(SleepState state, QObject *receiver, const char *member)
89 {
90  Q_UNUSED(receiver)
91  Q_UNUSED(member)
92 
93  if (!globalPowerManager->supportedSleepStates.contains(state)) {
94  return;
95  }
96 
97  switch (state)
98  {
99  case StandbyState:
100  case SuspendState:
101  globalPowerManager->managerIface.Suspend();
102  break;
103  case HibernateState:
104  globalPowerManager->managerIface.Hibernate();
105  break;
106  case HybridSuspendState:
107  globalPowerManager->managerIface.HybridSuspend();
108  break;
109  }
110 }
111 
112 int Solid::PowerManagement::beginSuppressingSleep(const QString &reason)
113 {
114  QDBusReply<uint> reply;
115  if (globalPowerManager->policyAgentIface.isValid()) {
116  reply = globalPowerManager->policyAgentIface.AddInhibition(
117  (uint)PowerManagementPrivate::InterruptSession,
118  QCoreApplication::applicationName(), reason);
119  } else {
120  // Fallback to the fd.o Inhibit interface
121  reply = globalPowerManager->inhibitIface.Inhibit(QCoreApplication::applicationName(), reason);
122  }
123 
124  if (reply.isValid())
125  return reply;
126  else
127  return -1;
128 }
129 
130 bool Solid::PowerManagement::stopSuppressingSleep(int cookie)
131 {
132  if (globalPowerManager->policyAgentIface.isValid()) {
133  return globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
134  } else {
135  // Fallback to the fd.o Inhibit interface
136  return globalPowerManager->inhibitIface.UnInhibit(cookie).isValid();
137  }
138 }
139 
140 int Solid::PowerManagement::beginSuppressingScreenPowerManagement(const QString& reason)
141 {
142  if (globalPowerManager->policyAgentIface.isValid()) {
143  QDBusReply<uint> reply = globalPowerManager->policyAgentIface.AddInhibition(
144  (uint)PowerManagementPrivate::ChangeScreenSettings,
145  QCoreApplication::applicationName(), reason);
146 
147  if (reply.isValid()) {
148  QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.ScreenSaver"),
149  QLatin1String("/ScreenSaver"),
150  QLatin1String("org.freedesktop.ScreenSaver"),
151  QLatin1String("Inhibit"));
152  message << QCoreApplication::applicationName();
153  message << reason;
154 
155  QDBusPendingReply<uint> ssReply = QDBusConnection::sessionBus().asyncCall(message);
156  ssReply.waitForFinished();
157  if (ssReply.isValid()) {
158  globalPowerManager->screensaverCookiesForPowerDevilCookies.insert(reply, ssReply.value());
159  }
160 
161  return reply;
162  } else {
163  return -1;
164  }
165  } else {
166  // No way to fallback on something, hence return failure
167  return -1;
168  }
169 }
170 
171 bool Solid::PowerManagement::stopSuppressingScreenPowerManagement(int cookie)
172 {
173  if (globalPowerManager->policyAgentIface.isValid()) {
174  bool result = globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
175 
176  if (globalPowerManager->screensaverCookiesForPowerDevilCookies.contains(cookie)) {
177  QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.ScreenSaver"),
178  QLatin1String("/ScreenSaver"),
179  QLatin1String("org.freedesktop.ScreenSaver"),
180  QLatin1String("UnInhibit"));
181  message << globalPowerManager->screensaverCookiesForPowerDevilCookies.take(cookie);
182  QDBusConnection::sessionBus().asyncCall(message);
183  }
184 
185  return result;
186  } else {
187  // No way to fallback on something, hence return failure
188  return false;
189  }
190 }
191 
192 Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier()
193 {
194  return globalPowerManager;
195 }
196 
197 void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState)
198 {
199  if (supportedSleepStates.contains(Solid::PowerManagement::SuspendState) == newState) {
200  return;
201  }
202 
203  if (newState) {
204  supportedSleepStates+= Solid::PowerManagement::SuspendState;
205  } else {
206  supportedSleepStates-= Solid::PowerManagement::SuspendState;
207  }
208 }
209 
210 void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState)
211 {
212  if (supportedSleepStates.contains(Solid::PowerManagement::HibernateState) == newState) {
213  return;
214  }
215 
216  if (newState) {
217  supportedSleepStates+= Solid::PowerManagement::HibernateState;
218  } else {
219  supportedSleepStates-= Solid::PowerManagement::HibernateState;
220  }
221 }
222 
223 void Solid::PowerManagementPrivate::slotCanHybridSuspendChanged(bool newState)
224 {
225  if (supportedSleepStates.contains(Solid::PowerManagement::HybridSuspendState) == newState) {
226  return;
227  }
228 
229  if (newState) {
230  supportedSleepStates+= Solid::PowerManagement::HybridSuspendState;
231  } else {
232  supportedSleepStates-= Solid::PowerManagement::HybridSuspendState;
233  }
234 }
235 
236 void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState)
237 {
238  if (powerSaveStatus == newState) {
239  return;
240  }
241 
242  powerSaveStatus = newState;
243  emit appShouldConserveResourcesChanged(powerSaveStatus);
244 }
245 
246 void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName)
247 {
248  if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
249  // Load all the properties
250  QDBusPendingReply<bool> suspendReply = managerIface.CanSuspend();
251  suspendReply.waitForFinished();
252  slotCanSuspendChanged(suspendReply.isValid() ? suspendReply.value() : false);
253 
254  QDBusPendingReply<bool> hibernateReply = managerIface.CanHibernate();
255  hibernateReply.waitForFinished();
256  slotCanHibernateChanged(hibernateReply.isValid() ? hibernateReply.value() : false);
257 
258  QDBusPendingReply<bool> hybridSuspendReply = managerIface.CanHybridSuspend();
259  hybridSuspendReply.waitForFinished();
260  slotCanHybridSuspendChanged(hybridSuspendReply.isValid() ? hybridSuspendReply.value() : false);
261 
262  QDBusPendingReply<bool> saveStatusReply = managerIface.GetPowerSaveStatus();
263  saveStatusReply.waitForFinished();
264  slotPowerSaveStatusChanged(saveStatusReply.isValid() ? saveStatusReply.value() : false);
265  } else {
266  // Is the resume signal available?
267  QDBusMessage call = QDBusMessage::createMethodCall(QLatin1String("org.kde.Solid.PowerManagement"),
268  QLatin1String("/org/kde/Solid/PowerManagement"),
269  QLatin1String("org.kde.Solid.PowerManagement"),
270  QLatin1String("backendCapabilities"));
271  QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
272  reply.waitForFinished();
273 
274  if (reply.isValid() && reply.value() > 0) {
275  // Connect the signal
276  QDBusConnection::sessionBus().connect(QLatin1String("org.kde.Solid.PowerManagement"),
277  QLatin1String("/org/kde/Solid/PowerManagement"),
278  QLatin1String("org.kde.Solid.PowerManagement"),
279  QLatin1String("resumingFromSuspend"),
280  this,
281  SIGNAL(resumingFromSuspend()));
282  }
283  }
284 }
285 
286 void Solid::PowerManagementPrivate::slotServiceUnregistered(const QString &serviceName)
287 {
288  if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
289  // Reset the values
290  slotCanSuspendChanged(false);
291  slotCanHibernateChanged(false);
292  slotCanHybridSuspendChanged(false);
293  slotPowerSaveStatusChanged(false);
294  } else {
295  // Disconnect the signal
296  QDBusConnection::sessionBus().disconnect(QLatin1String("org.kde.Solid.PowerManagement"),
297  QLatin1String("/org/kde/Solid/PowerManagement"),
298  QLatin1String("org.kde.Solid.PowerManagement"),
299  QLatin1String("resumingFromSuspend"),
300  this,
301  SIGNAL(resumingFromSuspend()));
302  }
303 }
304 
305 #include "powermanagement_p.moc"
306 #include "powermanagement.moc"
307 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Apr 16 2013 20:59:32 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Solid

Skip menu "Solid"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.10.2 API Reference

Skip menu "kdelibs-4.10.2 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal