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 return reply; 00143 else 00144 return -1; 00145 } else { 00146 // No way to fallback on something, hence return failure 00147 return -1; 00148 } 00149 } 00150 00151 bool Solid::PowerManagement::stopSuppressingScreenPowerManagement(int cookie) 00152 { 00153 if (globalPowerManager->policyAgentIface.isValid()) { 00154 return globalPowerManager->policyAgentIface.ReleaseInhibition(cookie).isValid(); 00155 } else { 00156 // No way to fallback on something, hence return failure 00157 return false; 00158 } 00159 } 00160 00161 Solid::PowerManagement::Notifier *Solid::PowerManagement::notifier() 00162 { 00163 return globalPowerManager; 00164 } 00165 00166 void Solid::PowerManagementPrivate::slotCanSuspendChanged(bool newState) 00167 { 00168 if (newState) { 00169 supportedSleepStates+= Solid::PowerManagement::SuspendState; 00170 } else { 00171 supportedSleepStates-= Solid::PowerManagement::SuspendState; 00172 } 00173 } 00174 00175 void Solid::PowerManagementPrivate::slotCanHibernateChanged(bool newState) 00176 { 00177 if (newState) { 00178 supportedSleepStates+= Solid::PowerManagement::HibernateState; 00179 } else { 00180 supportedSleepStates-= Solid::PowerManagement::HibernateState; 00181 } 00182 } 00183 00184 void Solid::PowerManagementPrivate::slotPowerSaveStatusChanged(bool newState) 00185 { 00186 powerSaveStatus = newState; 00187 emit appShouldConserveResourcesChanged(powerSaveStatus); 00188 } 00189 00190 void Solid::PowerManagementPrivate::slotServiceRegistered(const QString &serviceName) 00191 { 00192 Q_UNUSED(serviceName); 00193 00194 // Is the resume signal available? 00195 QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", 00196 "/org/kde/Solid/PowerManagement", 00197 "org.kde.Solid.PowerManagement", 00198 "backendCapabilities"); 00199 QDBusPendingReply< uint > reply = QDBusConnection::sessionBus().asyncCall(call); 00200 reply.waitForFinished(); 00201 00202 if (reply.isValid() && reply.value() > 0) { 00203 // Connect the signal 00204 QDBusConnection::sessionBus().connect("org.kde.Solid.PowerManagement", 00205 "/org/kde/Solid/PowerManagement", 00206 "org.kde.Solid.PowerManagement", 00207 "resumingFromSuspend", 00208 this, 00209 SIGNAL(resumingFromSuspend())); 00210 } 00211 } 00212 00213 #include "powermanagement_p.moc" 00214 #include "powermanagement.moc" 00215
KDE 4.7 API Reference