• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.4 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)
43 {
44  powerSaveStatus = managerIface.GetPowerSaveStatus();
45 
46  if (managerIface.CanSuspend())
47  supportedSleepStates+= Solid::PowerManagement::SuspendState;
48  if (managerIface.CanHibernate())
49  supportedSleepStates+= Solid::PowerManagement::HibernateState;
50 
51  connect(&managerIface, SIGNAL(CanSuspendChanged(bool)),
52  this, SLOT(slotCanSuspendChanged(bool)));
53  connect(&managerIface, SIGNAL(CanHibernateChanged(bool)),
54  this, SLOT(slotCanHibernateChanged(bool)));
55  connect(&managerIface, SIGNAL(PowerSaveStatusChanged(bool)),
56  this, SLOT(slotPowerSaveStatusChanged(bool)));
57  connect(&serviceWatcher, SIGNAL(serviceRegistered(QString)),
58  this, SLOT(slotServiceRegistered(QString)));
59 
60  // If the service is registered, trigger the connection immediately
61  if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
62  slotServiceRegistered("org.kde.Solid.PowerManagement");
63  }
64 }
65 
66 Solid::PowerManagementPrivate::~PowerManagementPrivate()
67 {
68 }
69 
70 Solid::PowerManagement::Notifier::Notifier()
71 {
72 }
73 
74 bool Solid::PowerManagement::appShouldConserveResources()
75 {
76  return globalPowerManager->powerSaveStatus;
77 }
78 
79 QSet<Solid::PowerManagement::SleepState> Solid::PowerManagement::supportedSleepStates()
80 {
81  return globalPowerManager->supportedSleepStates;
82 }
83 
84 void Solid::PowerManagement::requestSleep(SleepState state, QObject *receiver, const char *member)
85 {
86  Q_UNUSED(receiver)
87  Q_UNUSED(member)
88 
89  if (!globalPowerManager->supportedSleepStates.contains(state)) {
90  return;
91  }
92 
93  switch (state)
94  {
95  case StandbyState:
96  break;
97  case SuspendState:
98  globalPowerManager->managerIface.Suspend();
99  break;
100  case HibernateState:
101  globalPowerManager->managerIface.Hibernate();
102  break;
103  }
104 }
105 
106 int Solid::PowerManagement::beginSuppressingSleep(const QString &reason)
107 {
108  QDBusReply<uint> reply;
109  if (globalPowerManager->policyAgentIface.isValid()) {
110  reply = globalPowerManager->policyAgentIface.AddInhibition(
111  (uint)PowerManagementPrivate::InterruptSession,
112  QCoreApplication::applicationName(), reason);
113  } else {
114  // Fallback to the fd.o Inhibit interface
115  reply = globalPowerManager->inhibitIface.Inhibit(QCoreApplication::applicationName(), reason);
116  }
117 
118  if (reply.isValid())
119  return reply;
120  else
121  return -1;
122 }
123 
124 bool Solid::PowerManagement::stopSuppressingSleep(int cookie)
125 {
126  if (globalPowerManager->policyAgentIface.isValid()) {
127  return globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
128  } else {
129  // Fallback to the fd.o Inhibit interface
130  return globalPowerManager->inhibitIface.UnInhibit(cookie).isValid();
131  }
132 }
133 
134 int Solid::PowerManagement::beginSuppressingScreenPowerManagement(const QString& reason)
135 {
136  if (globalPowerManager->policyAgentIface.isValid()) {
137  QDBusReply<uint> reply = globalPowerManager->policyAgentIface.AddInhibition(
138  (uint)PowerManagementPrivate::ChangeScreenSettings,
139  QCoreApplication::applicationName(), reason);
140 
141  if (reply.isValid()) {
142  QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.ScreenSaver", "/ScreenSaver",
143  "org.freedesktop.ScreenSaver", "Inhibit");
144  message << QCoreApplication::applicationName();
145  message << reason;
146 
147  QDBusPendingReply<uint> ssReply = QDBusConnection::sessionBus().asyncCall(message);
148  ssReply.waitForFinished();
149  if (ssReply.isValid()) {
150  globalPowerManager->screensaverCookiesForPowerDevilCookies.insert(reply, ssReply.value());
151  }
152 
153  return reply;
154  } else {
155  return -1;
156  }
157  } else {
158  // No way to fallback on something, hence return failure
159  return -1;
160  }
161 }
162 
163 bool Solid::PowerManagement::stopSuppressingScreenPowerManagement(int cookie)
164 {
165  if (globalPowerManager->policyAgentIface.isValid()) {
166  bool result = globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
167 
168  if (globalPowerManager->screensaverCookiesForPowerDevilCookies.contains(cookie)) {
169  QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.ScreenSaver", "/ScreenSaver",
170  "org.freedesktop.ScreenSaver", "UnInhibit");
171  message << globalPowerManager->screensaverCookiesForPowerDevilCookies.take(cookie);
172  QDBusConnection::sessionBus().asyncCall(message);
173  }
174 
175  return result;
176  } else {
177  // No way to fallback on something, hence return failure
178  return false;
179  }
180 }
181 
182 Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier()
183 {
184  return globalPowerManager;
185 }
186 
187 void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState)
188 {
189  if (newState) {
190  supportedSleepStates+= Solid::PowerManagement::SuspendState;
191  } else {
192  supportedSleepStates-= Solid::PowerManagement::SuspendState;
193  }
194 }
195 
196 void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState)
197 {
198  if (newState) {
199  supportedSleepStates+= Solid::PowerManagement::HibernateState;
200  } else {
201  supportedSleepStates-= Solid::PowerManagement::HibernateState;
202  }
203 }
204 
205 void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState)
206 {
207  powerSaveStatus = newState;
208  emit appShouldConserveResourcesChanged(powerSaveStatus);
209 }
210 
211 void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName)
212 {
213  Q_UNUSED(serviceName);
214 
215  // Is the resume signal available?
216  QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
217  "/org/kde/Solid/PowerManagement",
218  "org.kde.Solid.PowerManagement",
219  "backendCapabilities");
220  QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
221  reply.waitForFinished();
222 
223  if (reply.isValid() && reply.value() > 0) {
224  // Connect the signal
225  QDBusConnection::sessionBus().connect("org.kde.Solid.PowerManagement",
226  "/org/kde/Solid/PowerManagement",
227  "org.kde.Solid.PowerManagement",
228  "resumingFromSuspend",
229  this,
230  SIGNAL(resumingFromSuspend()));
231  }
232 }
233 
234 #include "powermanagement_p.moc"
235 #include "powermanagement.moc"
236 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Jul 17 2012 07:29:27 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.4 API Reference

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