• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.9.5 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(QLatin1String("org.freedesktop.PowerManagement"),
00032                    QLatin1String("/org/freedesktop/PowerManagement"),
00033                    QDBusConnection::sessionBus()),
00034       policyAgentIface(QLatin1String("org.kde.Solid.PowerManagement.PolicyAgent"),
00035                        QLatin1String("/org/kde/Solid/PowerManagement/PolicyAgent"),
00036                        QDBusConnection::sessionBus()),
00037       inhibitIface(QLatin1String("org.freedesktop.PowerManagement.Inhibit"),
00038                    QLatin1String("/org/freedesktop/PowerManagement/Inhibit"),
00039                    QDBusConnection::sessionBus()),
00040       serviceWatcher(QLatin1String("org.kde.Solid.PowerManagement"),
00041                      QDBusConnection::sessionBus(),
00042                      QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration),
00043       powerSaveStatus(false)
00044 {
00045     serviceWatcher.addWatchedService(QLatin1String("org.freedesktop.PowerManagement"));
00046 
00047     connect(&managerIface, SIGNAL(CanSuspendChanged(bool)),
00048             this, SLOT(slotCanSuspendChanged(bool)));
00049     connect(&managerIface, SIGNAL(CanHibernateChanged(bool)),
00050             this, SLOT(slotCanHibernateChanged(bool)));
00051     connect(&managerIface, SIGNAL(PowerSaveStatusChanged(bool)),
00052             this, SLOT(slotPowerSaveStatusChanged(bool)));
00053     connect(&serviceWatcher, SIGNAL(serviceRegistered(QString)),
00054             this, SLOT(slotServiceRegistered(QString)));
00055     connect(&serviceWatcher, SIGNAL(serviceUnregistered(QString)),
00056             this, SLOT(slotServiceUnregistered(QString)));
00057 
00058     // If the service is registered, trigger the connection immediately
00059     if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.Solid.PowerManagement"))) {
00060         slotServiceRegistered(QLatin1String("org.kde.Solid.PowerManagement"));
00061     }
00062     if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"))) {
00063         slotServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"));
00064     }
00065 }
00066 
00067 Solid::PowerManagementPrivate::~PowerManagementPrivate()
00068 {
00069 }
00070 
00071 Solid::PowerManagement::Notifier::Notifier()
00072 {
00073 }
00074 
00075 bool Solid::PowerManagement::appShouldConserveResources()
00076 {
00077     return globalPowerManager->powerSaveStatus;
00078 }
00079 
00080 QSet<Solid::PowerManagement::SleepState> Solid::PowerManagement::supportedSleepStates()
00081 {
00082     return globalPowerManager->supportedSleepStates;
00083 }
00084 
00085 void Solid::PowerManagement::requestSleep(SleepState state, QObject *receiver, const char *member)
00086 {
00087     Q_UNUSED(receiver)
00088     Q_UNUSED(member)
00089 
00090     if (!globalPowerManager->supportedSleepStates.contains(state)) {
00091         return;
00092     }
00093 
00094     switch (state)
00095     {
00096     case StandbyState:
00097         break;
00098     case SuspendState:
00099         globalPowerManager->managerIface.Suspend();
00100         break;
00101     case HibernateState:
00102         globalPowerManager->managerIface.Hibernate();
00103         break;
00104     }
00105 }
00106 
00107 int Solid::PowerManagement::beginSuppressingSleep(const QString &reason)
00108 {
00109     QDBusReply<uint> reply;
00110     if (globalPowerManager->policyAgentIface.isValid()) {
00111         reply = globalPowerManager->policyAgentIface.AddInhibition(
00112             (uint)PowerManagementPrivate::InterruptSession,
00113             QCoreApplication::applicationName(), reason);
00114     } else {
00115         // Fallback to the fd.o Inhibit interface
00116         reply = globalPowerManager->inhibitIface.Inhibit(QCoreApplication::applicationName(), reason);
00117     }
00118 
00119     if (reply.isValid())
00120         return reply;
00121     else
00122         return -1;
00123 }
00124 
00125 bool Solid::PowerManagement::stopSuppressingSleep(int cookie)
00126 {
00127     if (globalPowerManager->policyAgentIface.isValid()) {
00128         return globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
00129     } else {
00130         // Fallback to the fd.o Inhibit interface
00131         return globalPowerManager->inhibitIface.UnInhibit(cookie).isValid();
00132     }
00133 }
00134 
00135 int Solid::PowerManagement::beginSuppressingScreenPowerManagement(const QString& reason)
00136 {
00137     if (globalPowerManager->policyAgentIface.isValid()) {
00138         QDBusReply<uint> reply = globalPowerManager->policyAgentIface.AddInhibition(
00139             (uint)PowerManagementPrivate::ChangeScreenSettings,
00140             QCoreApplication::applicationName(), reason);
00141 
00142         if (reply.isValid()) {
00143             QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.ScreenSaver"),
00144                                                                   QLatin1String("/ScreenSaver"),
00145                                                                   QLatin1String("org.freedesktop.ScreenSaver"),
00146                                                                   QLatin1String("Inhibit"));
00147             message << QCoreApplication::applicationName();
00148             message << reason;
00149 
00150             QDBusPendingReply<uint> ssReply = QDBusConnection::sessionBus().asyncCall(message);
00151             ssReply.waitForFinished();
00152             if (ssReply.isValid()) {
00153                 globalPowerManager->screensaverCookiesForPowerDevilCookies.insert(reply, ssReply.value());
00154             }
00155 
00156             return reply;
00157         } else {
00158             return -1;
00159         }
00160     } else {
00161         // No way to fallback on something, hence return failure
00162         return -1;
00163     }
00164 }
00165 
00166 bool Solid::PowerManagement::stopSuppressingScreenPowerManagement(int cookie)
00167 {
00168     if (globalPowerManager->policyAgentIface.isValid()) {
00169         bool result = globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid();
00170 
00171         if (globalPowerManager->screensaverCookiesForPowerDevilCookies.contains(cookie)) {
00172             QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.ScreenSaver"),
00173                                                                   QLatin1String("/ScreenSaver"),
00174                                                                   QLatin1String("org.freedesktop.ScreenSaver"),
00175                                                                   QLatin1String("UnInhibit"));
00176             message << globalPowerManager->screensaverCookiesForPowerDevilCookies.take(cookie);
00177             QDBusConnection::sessionBus().asyncCall(message);
00178         }
00179 
00180         return result;
00181     } else {
00182         // No way to fallback on something, hence return failure
00183         return false;
00184     }
00185 }
00186 
00187 Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier()
00188 {
00189     return globalPowerManager;
00190 }
00191 
00192 void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState)
00193 {
00194     if (supportedSleepStates.contains(Solid::PowerManagement::SuspendState) == newState) {
00195         return;
00196     }
00197 
00198     if (newState) {
00199         supportedSleepStates+= Solid::PowerManagement::SuspendState;
00200     } else {
00201         supportedSleepStates-= Solid::PowerManagement::SuspendState;
00202     }
00203 }
00204 
00205 void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState)
00206 {
00207     if (supportedSleepStates.contains(Solid::PowerManagement::HibernateState) == newState) {
00208         return;
00209     }
00210 
00211     if (newState) {
00212         supportedSleepStates+= Solid::PowerManagement::HibernateState;
00213     } else {
00214         supportedSleepStates-= Solid::PowerManagement::HibernateState;
00215     }
00216 }
00217 
00218 void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState)
00219 {
00220     if (powerSaveStatus == newState) {
00221         return;
00222     }
00223 
00224     powerSaveStatus = newState;
00225     emit appShouldConserveResourcesChanged(powerSaveStatus);
00226 }
00227 
00228 void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName)
00229 {
00230     if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
00231         // Load all the properties
00232         QDBusPendingReply<bool> suspendReply = managerIface.CanSuspend();
00233         suspendReply.waitForFinished();
00234         slotCanSuspendChanged(suspendReply.isValid() ? suspendReply.value() : false);
00235 
00236         QDBusPendingReply<bool> hibernateReply = managerIface.CanHibernate();
00237         hibernateReply.waitForFinished();
00238         slotCanHibernateChanged(hibernateReply.isValid() ? hibernateReply.value() : false);
00239 
00240         QDBusPendingReply<bool> saveStatusReply = managerIface.GetPowerSaveStatus();
00241         saveStatusReply.waitForFinished();
00242         slotPowerSaveStatusChanged(saveStatusReply.isValid() ? saveStatusReply.value() : false);
00243     } else {
00244         // Is the resume signal available?
00245         QDBusMessage call = QDBusMessage::createMethodCall(QLatin1String("org.kde.Solid.PowerManagement"),
00246                                                            QLatin1String("/org/kde/Solid/PowerManagement"),
00247                                                            QLatin1String("org.kde.Solid.PowerManagement"),
00248                                                            QLatin1String("backendCapabilities"));
00249         QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call);
00250         reply.waitForFinished();
00251 
00252         if (reply.isValid() && reply.value() > 0) {
00253                 // Connect the signal
00254                 QDBusConnection::sessionBus().connect(QLatin1String("org.kde.Solid.PowerManagement"),
00255                                                       QLatin1String("/org/kde/Solid/PowerManagement"),
00256                                                       QLatin1String("org.kde.Solid.PowerManagement"),
00257                                                       QLatin1String("resumingFromSuspend"),
00258                                                       this,
00259                                                       SIGNAL(resumingFromSuspend()));
00260         }
00261     }
00262 }
00263 
00264 void Solid::PowerManagementPrivate::slotServiceUnregistered(const QString &serviceName)
00265 {
00266     if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) {
00267         // Reset the values
00268         slotCanSuspendChanged(false);
00269         slotCanHibernateChanged(false);
00270         slotPowerSaveStatusChanged(false);
00271     } else {
00272         // Disconnect the signal
00273         QDBusConnection::sessionBus().disconnect(QLatin1String("org.kde.Solid.PowerManagement"),
00274                                                  QLatin1String("/org/kde/Solid/PowerManagement"),
00275                                                  QLatin1String("org.kde.Solid.PowerManagement"),
00276                                                  QLatin1String("resumingFromSuspend"),
00277                                                  this,
00278                                                  SIGNAL(resumingFromSuspend()));
00279     }
00280 }
00281 
00282 #include "powermanagement_p.moc"
00283 #include "powermanagement.moc"
00284 
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:31:23 by doxygen 1.7.5.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.9.5 API Reference

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