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

Skip menu "kdelibs-4.10.1 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