• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 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("org.freedesktop.PowerManagement",
32  "/org/freedesktop/PowerManagement",
33  QDBusConnection::sessionBus()),
34  policyAgentIface("org.kde.Solid.PowerManagement.PolicyAgent",
35  "/org/kde/Solid/PowerManagement/PolicyAgent",
36  QDBusConnection::sessionBus()),
37  inhibitIface("org.freedesktop.PowerManagement.Inhibit",
38  "/org/freedesktop/PowerManagement/Inhibit",
39  QDBusConnection::sessionBus()),
40  serviceWatcher("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("org.kde.Solid.PowerManagement")) {
60  slotServiceRegistered("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->managerIface.GetPowerSaveStatus();
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("org.freedesktop.ScreenSaver", "/ScreenSaver",
144  "org.freedesktop.ScreenSaver", "Inhibit");
145  message << QCoreApplication::applicationName();
146  message << reason;
147 
148  QDBusPendingReply<uint> ssReply = QDBusConnection::sessionBus().asyncCall(message);
149  ssReply.waitForFinished();
150  if (ssReply.isValid()) {
151  globalPowerManager->screensaverCookiesForPowerDevilCookies.insert(reply, ssReply.value());
152  }
153 
154  return reply;
155  } else {
156  return -1;
157  }
158  } else {
159  // No way to fallback on something, hence return failure
160  return -1;
161  }
162 }
163 
164 bool Solid::PowerManagement::stopSuppressingScreenPowerManagement(int cookie)
165 {
166  if (globalPowerManager->policyAgentIface.isValid()) {
167  bool result = globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
168 
169  if (globalPowerManager->screensaverCookiesForPowerDevilCookies.contains(cookie)) {
170  QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.ScreenSaver", "/ScreenSaver",
171  "org.freedesktop.ScreenSaver", "UnInhibit");
172  message << globalPowerManager->screensaverCookiesForPowerDevilCookies.take(cookie);
173  QDBusConnection::sessionBus().asyncCall(message);
174  }
175 
176  return result;
177  } else {
178  // No way to fallback on something, hence return failure
179  return false;
180  }
181 }
182 
183 Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier()
184 {
185  return globalPowerManager;
186 }
187 
188 void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState)
189 {
190  if (supportedSleepStates.contains(Solid::PowerManagement::SuspendState) == newState) {
191  return;
192  }
193 
194  if (newState) {
195  supportedSleepStates+= Solid::PowerManagement::SuspendState;
196  } else {
197  supportedSleepStates-= Solid::PowerManagement::SuspendState;
198  }
199 }
200 
201 void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState)
202 {
203  if (supportedSleepStates.contains(Solid::PowerManagement::HibernateState) == newState) {
204  return;
205  }
206 
207  if (newState) {
208  supportedSleepStates+= Solid::PowerManagement::HibernateState;
209  } else {
210  supportedSleepStates-= Solid::PowerManagement::HibernateState;
211  }
212 }
213 
214 void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState)
215 {
216  if (powerSaveStatus == newState) {
217  return;
218  }
219 
220  powerSaveStatus = newState;
221  emit appShouldConserveResourcesChanged(powerSaveStatus);
222 }
223 
224 void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName)
225 {
226  if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
227  // Load all the properties
228  QDBusPendingReply<bool> suspendReply = managerIface.CanSuspend();
229  suspendReply.waitForFinished();
230  slotCanSuspendChanged(suspendReply.isValid() ? suspendReply.value() : false);
231 
232  QDBusPendingReply<bool> hibernateReply = managerIface.CanHibernate();
233  hibernateReply.waitForFinished();
234  slotCanHibernateChanged(hibernateReply.isValid() ? hibernateReply.value() : false);
235 
236  QDBusPendingReply<bool> saveStatusReply = managerIface.GetPowerSaveStatus();
237  saveStatusReply.waitForFinished();
238  slotPowerSaveStatusChanged(saveStatusReply.isValid() ? saveStatusReply.value() : false);
239  } else {
240  // Is the resume signal available?
241  QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
242  "/org/kde/Solid/PowerManagement",
243  "org.kde.Solid.PowerManagement",
244  "backendCapabilities");
245  QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
246  reply.waitForFinished();
247 
248  if (reply.isValid() && reply.value() > 0) {
249  // Connect the signal
250  QDBusConnection::sessionBus().connect("org.kde.Solid.PowerManagement",
251  "/org/kde/Solid/PowerManagement",
252  "org.kde.Solid.PowerManagement",
253  "resumingFromSuspend",
254  this,
255  SIGNAL(resumingFromSuspend()));
256  }
257  }
258 }
259 
260 void Solid::PowerManagementPrivate::slotServiceUnregistered(const QString &serviceName)
261 {
262  if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
263  // Reset the values
264  slotCanSuspendChanged(false);
265  slotCanHibernateChanged(false);
266  slotPowerSaveStatusChanged(false);
267  } else {
268  // Disconnect the signal
269  QDBusConnection::sessionBus().disconnect(QLatin1String("org.kde.Solid.PowerManagement"),
270  QLatin1String("/org/kde/Solid/PowerManagement"),
271  QLatin1String("org.kde.Solid.PowerManagement"),
272  QLatin1String("resumingFromSuspend"),
273  this,
274  SIGNAL(resumingFromSuspend()));
275  }
276 }
277 
278 #include "powermanagement_p.moc"
279 #include "powermanagement.moc"
280 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Fri Nov 16 2012 14:57:46 by doxygen 1.8.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.8.5 API Reference

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