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

Solid

powermanagement.cpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2006-2007 Kevin Ottens <ervin@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Lesser General Public
00006     License as published by the Free Software Foundation; either
00007     version 2.1 of the License, or (at your option) version 3, or any
00008     later version accepted by the membership of KDE e.V. (or its
00009     successor approved by the membership of KDE e.V.), which shall
00010     act as a proxy defined in Section 6 of version 3 of the license.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library. If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "powermanagement.h"
00022 #include "powermanagement_p.h"
00023 
00024 #include "soliddefs_p.h"
00025 
00026 #include <QtCore/QCoreApplication>
00027 
00028 SOLID_GLOBAL_STATIC(Solid::PowerManagementPrivate, globalPowerManager)
00029 
00030 Solid::PowerManagementPrivate::PowerManagementPrivate()
00031     : managerIface("org.freedesktop.PowerManagement",
00032                    "/org/freedesktop/PowerManagement",
00033                    QDBusConnection::sessionBus()),
00034       policyAgentIface("org.kde.Solid.PowerManagement.PolicyAgent",
00035                        "/org/kde/Solid/PowerManagement/PolicyAgent",
00036                        QDBusConnection::sessionBus()),
00037       inhibitIface("org.freedesktop.PowerManagement.Inhibit",
00038                    "/org/freedesktop/PowerManagement/Inhibit",
00039                    QDBusConnection::sessionBus()),
00040       serviceWatcher("org.kde.Solid.PowerManagement",
00041                      QDBusConnection::sessionBus(),
00042                      QDBusServiceWatcher::WatchForRegistration)
00043 {
00044     powerSaveStatus = managerIface.GetPowerSaveStatus();
00045 
00046     if (managerIface.CanSuspend())
00047         supportedSleepStates+= Solid::PowerManagement::SuspendState;
00048     if (managerIface.CanHibernate())
00049         supportedSleepStates+= Solid::PowerManagement::HibernateState;
00050 
00051     connect(&managerIface, SIGNAL(CanSuspendChanged(bool)),
00052             this, SLOT(slotCanSuspendChanged(bool)));
00053     connect(&managerIface, SIGNAL(CanHibernateChanged(bool)),
00054             this, SLOT(slotCanHibernateChanged(bool)));
00055     connect(&managerIface, SIGNAL(PowerSaveStatusChanged(bool)),
00056             this, SLOT(slotPowerSaveStatusChanged(bool)));
00057     connect(&serviceWatcher, SIGNAL(serviceRegistered(QString)),
00058             this, SLOT(slotServiceRegistered(QString)));
00059 
00060     // If the service is registered, trigger the connection immediately
00061     if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
00062         slotServiceRegistered("org.kde.Solid.PowerManagement");
00063     }
00064 }
00065 
00066 Solid::PowerManagementPrivate::~PowerManagementPrivate()
00067 {
00068 }
00069 
00070 Solid::PowerManagement::Notifier::Notifier()
00071 {
00072 }
00073 
00074 bool Solid::PowerManagement::appShouldConserveResources()
00075 {
00076     return globalPowerManager->powerSaveStatus;
00077 }
00078 
00079 QSet<Solid::PowerManagement::SleepState> Solid::PowerManagement::supportedSleepStates()
00080 {
00081     return globalPowerManager->supportedSleepStates;
00082 }
00083 
00084 void Solid::PowerManagement::requestSleep(SleepState state, QObject *receiver, const char *member)
00085 {
00086     Q_UNUSED(receiver)
00087     Q_UNUSED(member)
00088 
00089     if (!globalPowerManager->supportedSleepStates.contains(state)) {
00090         return;
00091     }
00092 
00093     switch (state)
00094     {
00095     case StandbyState:
00096         break;
00097     case SuspendState:
00098         globalPowerManager->managerIface.Suspend();
00099         break;
00100     case HibernateState:
00101         globalPowerManager->managerIface.Hibernate();
00102         break;
00103     }
00104 }
00105 
00106 int Solid::PowerManagement::beginSuppressingSleep(const QString &reason)
00107 {
00108     QDBusReply<uint> reply;
00109     if (globalPowerManager->policyAgentIface.isValid()) {
00110         reply = globalPowerManager->policyAgentIface.AddInhibition(
00111             (uint)PowerManagementPrivate::InterruptSession,
00112             QCoreApplication::applicationName(), reason);
00113     } else {
00114         // Fallback to the fd.o Inhibit interface
00115         reply = globalPowerManager->inhibitIface.Inhibit(QCoreApplication::applicationName(), reason);
00116     }
00117 
00118     if (reply.isValid())
00119         return reply;
00120     else
00121         return -1;
00122 }
00123 
00124 bool Solid::PowerManagement::stopSuppressingSleep(int cookie)
00125 {
00126     if (globalPowerManager->policyAgentIface.isValid()) {
00127         return globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
00128     } else {
00129         // Fallback to the fd.o Inhibit interface
00130         return globalPowerManager->inhibitIface.UnInhibit(cookie).isValid();
00131     }
00132 }
00133 
00134 int Solid::PowerManagement::beginSuppressingScreenPowerManagement(const QString& reason)
00135 {
00136     if (globalPowerManager->policyAgentIface.isValid()) {
00137         QDBusReply<uint> reply = globalPowerManager->policyAgentIface.AddInhibition(
00138             (uint)PowerManagementPrivate::ChangeScreenSettings,
00139             QCoreApplication::applicationName(), reason);
00140 
00141         if (reply.isValid()) {
00142             QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.ScreenSaver", "/ScreenSaver",
00143                                                                   "org.freedesktop.ScreenSaver", "Inhibit");
00144             message << QCoreApplication::applicationName();
00145             message << reason;
00146 
00147             QDBusPendingReply<uint> ssReply = QDBusConnection::sessionBus().asyncCall(message);
00148             ssReply.waitForFinished();
00149             if (ssReply.isValid()) {
00150                 globalPowerManager->screensaverCookiesForPowerDevilCookies.insert(reply, ssReply.value());
00151             }
00152 
00153             return reply;
00154         } else {
00155             return -1;
00156         }
00157     } else {
00158         // No way to fallback on something, hence return failure
00159         return -1;
00160     }
00161 }
00162 
00163 bool Solid::PowerManagement::stopSuppressingScreenPowerManagement(int cookie)
00164 {
00165     if (globalPowerManager->policyAgentIface.isValid()) {
00166         bool result = globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
00167 
00168         if (globalPowerManager->screensaverCookiesForPowerDevilCookies.contains(cookie)) {
00169             QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.ScreenSaver", "/ScreenSaver",
00170                                                                   "org.freedesktop.ScreenSaver", "UnInhibit");
00171             message << globalPowerManager->screensaverCookiesForPowerDevilCookies.take(cookie);
00172             QDBusConnection::sessionBus().asyncCall(message);
00173         }
00174 
00175         return result;
00176     } else {
00177         // No way to fallback on something, hence return failure
00178         return false;
00179     }
00180 }
00181 
00182 Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier()
00183 {
00184     return globalPowerManager;
00185 }
00186 
00187 void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState)
00188 {
00189     if (newState) {
00190         supportedSleepStates+= Solid::PowerManagement::SuspendState;
00191     } else {
00192         supportedSleepStates-= Solid::PowerManagement::SuspendState;
00193     }
00194 }
00195 
00196 void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState)
00197 {
00198     if (newState) {
00199         supportedSleepStates+= Solid::PowerManagement::HibernateState;
00200     } else {
00201         supportedSleepStates-= Solid::PowerManagement::HibernateState;
00202     }
00203 }
00204 
00205 void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState)
00206 {
00207     powerSaveStatus = newState;
00208     emit appShouldConserveResourcesChanged(powerSaveStatus);
00209 }
00210 
00211 void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName)
00212 {
00213     Q_UNUSED(serviceName);
00214 
00215     // Is the resume signal available?
00216     QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
00217                                                        "/org/kde/Solid/PowerManagement",
00218                                                        "org.kde.Solid.PowerManagement",
00219                                                        "backendCapabilities");
00220     QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
00221     reply.waitForFinished();
00222 
00223     if (reply.isValid() && reply.value() > 0) {
00224         // Connect the signal
00225         QDBusConnection::sessionBus().connect("org.kde.Solid.PowerManagement",
00226                                               "/org/kde/Solid/PowerManagement",
00227                                               "org.kde.Solid.PowerManagement",
00228                                               "resumingFromSuspend",
00229                                               this,
00230                                               SIGNAL(resumingFromSuspend()));
00231     }
00232 }
00233 
00234 #include "powermanagement_p.moc"
00235 #include "powermanagement.moc"
00236 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 17:43:18 by doxygen 1.8.0 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.3 API Reference

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