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 | 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("org.kde.Solid.PowerManagement")) { 00060 slotServiceRegistered("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->managerIface.GetPowerSaveStatus(); 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("org.freedesktop.ScreenSaver", "/ScreenSaver", 00144 "org.freedesktop.ScreenSaver", "Inhibit"); 00145 message << QCoreApplication::applicationName(); 00146 message << reason; 00147 00148 QDBusPendingReply<uint> ssReply = QDBusConnection::sessionBus().asyncCall(message); 00149 ssReply.waitForFinished(); 00150 if (ssReply.isValid()) { 00151 globalPowerManager->screensaverCookiesForPowerDevilCookies.insert(reply, ssReply.value()); 00152 } 00153 00154 return reply; 00155 } else { 00156 return -1; 00157 } 00158 } else { 00159 // No way to fallback on something, hence return failure 00160 return -1; 00161 } 00162 } 00163 00164 bool Solid::PowerManagement::stopSuppressingScreenPowerManagement(int cookie) 00165 { 00166 if (globalPowerManager->policyAgentIface.isValid()) { 00167 bool result = globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid(); 00168 00169 if (globalPowerManager->screensaverCookiesForPowerDevilCookies.contains(cookie)) { 00170 QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.ScreenSaver", "/ScreenSaver", 00171 "org.freedesktop.ScreenSaver", "UnInhibit"); 00172 message << globalPowerManager->screensaverCookiesForPowerDevilCookies.take(cookie); 00173 QDBusConnection::sessionBus().asyncCall(message); 00174 } 00175 00176 return result; 00177 } else { 00178 // No way to fallback on something, hence return failure 00179 return false; 00180 } 00181 } 00182 00183 Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier() 00184 { 00185 return globalPowerManager; 00186 } 00187 00188 void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState) 00189 { 00190 if (supportedSleepStates.contains(Solid::PowerManagement::SuspendState) == newState) { 00191 return; 00192 } 00193 00194 if (newState) { 00195 supportedSleepStates+= Solid::PowerManagement::SuspendState; 00196 } else { 00197 supportedSleepStates-= Solid::PowerManagement::SuspendState; 00198 } 00199 } 00200 00201 void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState) 00202 { 00203 if (supportedSleepStates.contains(Solid::PowerManagement::HibernateState) == newState) { 00204 return; 00205 } 00206 00207 if (newState) { 00208 supportedSleepStates+= Solid::PowerManagement::HibernateState; 00209 } else { 00210 supportedSleepStates-= Solid::PowerManagement::HibernateState; 00211 } 00212 } 00213 00214 void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState) 00215 { 00216 if (powerSaveStatus == newState) { 00217 return; 00218 } 00219 00220 powerSaveStatus = newState; 00221 emit appShouldConserveResourcesChanged(powerSaveStatus); 00222 } 00223 00224 void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName) 00225 { 00226 if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) { 00227 // Load all the properties 00228 QDBusPendingReply<bool> suspendReply = managerIface.CanSuspend(); 00229 suspendReply.waitForFinished(); 00230 slotCanSuspendChanged(suspendReply.isValid() ? suspendReply.value() : false); 00231 00232 QDBusPendingReply<bool> hibernateReply = managerIface.CanHibernate(); 00233 hibernateReply.waitForFinished(); 00234 slotCanHibernateChanged(hibernateReply.isValid() ? hibernateReply.value() : false); 00235 00236 QDBusPendingReply<bool> saveStatusReply = managerIface.GetPowerSaveStatus(); 00237 saveStatusReply.waitForFinished(); 00238 slotPowerSaveStatusChanged(saveStatusReply.isValid() ? saveStatusReply.value() : false); 00239 } else { 00240 // Is the resume signal available? 00241 QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", 00242 "/org/kde/Solid/PowerManagement", 00243 "org.kde.Solid.PowerManagement", 00244 "backendCapabilities"); 00245 QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call); 00246 reply.waitForFinished(); 00247 00248 if (reply.isValid() && reply.value() > 0) { 00249 // Connect the signal 00250 QDBusConnection::sessionBus().connect("org.kde.Solid.PowerManagement", 00251 "/org/kde/Solid/PowerManagement", 00252 "org.kde.Solid.PowerManagement", 00253 "resumingFromSuspend", 00254 this, 00255 SIGNAL(resumingFromSuspend())); 00256 } 00257 } 00258 } 00259 00260 void Solid::PowerManagementPrivate::slotServiceUnregistered(const QString &serviceName) 00261 { 00262 if (serviceName == QLatin1String("org.freedesktop.PowerManagement")) { 00263 // Reset the values 00264 slotCanSuspendChanged(false); 00265 slotCanHibernateChanged(false); 00266 slotPowerSaveStatusChanged(false); 00267 } else { 00268 // Disconnect the signal 00269 QDBusConnection::sessionBus().disconnect(QLatin1String("org.kde.Solid.PowerManagement"), 00270 QLatin1String("/org/kde/Solid/PowerManagement"), 00271 QLatin1String("org.kde.Solid.PowerManagement"), 00272 QLatin1String("resumingFromSuspend"), 00273 this, 00274 SIGNAL(resumingFromSuspend())); 00275 } 00276 } 00277 00278 #include "powermanagement_p.moc" 00279 #include "powermanagement.moc" 00280
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 09:47:06 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 09:47:06 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.