• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Engines

powermanagementengine.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2007 Aaron Seigo <aseigo@kde.org>
00003  *   Copyright 2007-2008 Sebastian Kuegler <sebas@kde.org>
00004  *   CopyRight 2007 Maor Vanmak <mvanmak1@gmail.com>
00005  *   Copyright 2008 Dario Freddi <drf54321@gmail.com>
00006  *
00007  *   This program is free software; you can redistribute it and/or modify
00008  *   it under the terms of the GNU Library General Public License version 2 as
00009  *   published by the Free Software Foundation
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this program; if not, write to the
00018  *   Free Software Foundation, Inc.,
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  */
00021 
00022 #include "powermanagementengine.h"
00023 
00024 //solid specific includes
00025 #include <solid/devicenotifier.h>
00026 #include <solid/device.h>
00027 #include <solid/deviceinterface.h>
00028 #include <solid/battery.h>
00029 #include <solid/powermanagement.h>
00030 #include <solid/control/powermanager.h>
00031 
00032 #include <KDebug>
00033 #include <KLocale>
00034 
00035 #include <QtDBus/QDBusInterface>
00036 #include <QtDBus/QDBusReply>
00037 
00038 #include <Plasma/DataContainer>
00039 
00040 PowermanagementEngine::PowermanagementEngine(QObject* parent, const QVariantList& args)
00041         : Plasma::DataEngine(parent, args)
00042         , m_acadapter(0)
00043         , m_sources(0)
00044         , m_dbus(QDBusConnection::sessionBus())
00045 {
00046     Q_UNUSED(args)
00047 
00048     m_sources << "Battery" << "AC Adapter" << "Sleepstates" << "PowerDevil";
00049 
00050     // This following call can be removed, but if we do, the
00051     // data is not shown in the plasmaengineexplorer.
00052     // sourceRequestEvent("Battery");
00053 }
00054 
00055 PowermanagementEngine::~PowermanagementEngine()
00056 {}
00057 
00058 void PowermanagementEngine::init()
00059 {
00060     connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(QString)),
00061             this,                              SLOT(deviceRemoved(QString)));
00062     connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(QString)),
00063             this,                              SLOT(deviceAdded(QString)));
00064     connect(Solid::Control::PowerManager::notifier(), SIGNAL(batteryRemainingTimeChanged(int)),
00065             this,                                     SLOT(batteryRemainingTimeChanged(int)));
00066 
00067     QStringList modules;
00068     QDBusInterface kdedInterface("org.kde.kded", "/kded", "org.kde.kded");
00069     QDBusReply<QStringList> reply = kdedInterface.call("loadedModules");
00070 
00071     if (!reply.isValid()) {
00072         return;
00073     }
00074 
00075     modules = reply.value();
00076 
00077     if (modules.contains("powerdevil")) {
00078 
00079         if (!m_dbus.connect("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil",
00080                           "profileChanged", this,
00081                            SLOT(profilesChanged(const QString&, const QStringList&)))) {
00082             kDebug() << "error!";
00083         }
00084 
00085         QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded", "/modules/powerdevil",
00086                            "org.kde.PowerDevil", "streamData");
00087         m_dbus.call(msg);
00088 
00089     }
00090 }
00091 
00092 QStringList PowermanagementEngine::sources() const
00093 {
00094     return m_sources + m_batterySources.values();
00095 }
00096 
00097 bool PowermanagementEngine::sourceRequestEvent(const QString &name)
00098 {
00099     if (name == "Battery") {
00100         QList<Solid::Device> list_battery =
00101                         Solid::Device::listFromType(Solid::DeviceInterface::Battery, QString());
00102         if (list_battery.count() == 0) {
00103             setData("Battery", "has Battery", false);
00104             return true;
00105         }
00106 
00107         uint index = 0;
00108         QStringList battery_sources;
00109 
00110         foreach (const Solid::Device &device_battery, list_battery) {
00111             const Solid::Battery* battery = device_battery.as<Solid::Battery>();
00112 
00113             if(battery != 0) {
00114                 if(battery->type() == Solid::Battery::PrimaryBattery) {
00115                     QString source = QString("Battery%1").arg(index++);
00116 
00117                     battery_sources<<source;
00118 
00119                     m_batterySources[device_battery.udi()] = source;
00120 
00121                     connect(battery, SIGNAL(chargeStateChanged(int, const QString &)), this,
00122                             SLOT(updateBatteryChargeState(int, const QString &)));
00123                     connect(battery, SIGNAL(chargePercentChanged(int, const QString &)), this,
00124                             SLOT(updateBatteryChargePercent(int, const QString &)));
00125                     connect(battery, SIGNAL(plugStateChanged(bool, const QString &)), this,
00126                             SLOT(updateBatteryPlugState(bool, const QString &)));
00127 
00128                     // Set initial values
00129                     updateBatteryChargeState(battery->chargeState(), device_battery.udi());
00130                     updateBatteryChargePercent(battery->chargePercent(), device_battery.udi());
00131                     updateBatteryPlugState(battery->isPlugged(), device_battery.udi());
00132                 }
00133             }
00134         }
00135 
00136         if (battery_sources.count() > 0) {
00137             setData("Battery", "has Battery", true);
00138             setData("Battery", "sources", battery_sources);
00139             setData("Battery", "Remaining msec", Solid::Control::PowerManager::batteryRemainingTime());
00140         }
00141     } else if (name == "AC Adapter") {
00142         // AC Adapter handling
00143         QList<Solid::Device> list_ac =
00144                         Solid::Device::listFromType(Solid::DeviceInterface::AcAdapter, QString());
00145         foreach (Solid::Device device_ac, list_ac) {
00146             m_acadapter = device_ac.as<Solid::AcAdapter>();
00147             updateAcPlugState(m_acadapter->isPlugged());
00148             connect(m_acadapter, SIGNAL(plugStateChanged(bool, const QString &)), this,
00149                     SLOT(updateAcPlugState(bool)));
00150         }
00151     } else if (name == "Sleepstates") {
00152         QSet<Solid::PowerManagement::SleepState> sleepstates =
00153                                 Solid::PowerManagement::supportedSleepStates();
00154         // We first set all possible sleepstates to false, then enable the ones that are available
00155         setData("Sleepstates", "Standby", false);
00156         setData("Sleepstates", "Suspend", false);
00157         setData("Sleepstates", "Hibernate", false);
00158 
00159         foreach (const Solid::PowerManagement::SleepState &sleepstate, sleepstates) {
00160             if (sleepstate == Solid::PowerManagement::StandbyState) {
00161                 setData("Sleepstates", "Supports standby", true);
00162             } else if (sleepstate == Solid::PowerManagement::SuspendState) {
00163                 setData("Sleepstates", "Supports suspend", true);
00164             } else if (sleepstate == Solid::PowerManagement::HibernateState) {
00165                 setData("Sleepstates", "Supports hibernate", true);
00166             }
00167             kDebug() << "Sleepstate \"" << sleepstate << "\" supported.";
00168         }
00169     } else if (name == "PowerDevil") {
00170         QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded", "/modules/powerdevil",
00171                            "org.kde.PowerDevil", "streamData");
00172         m_dbus.call(msg);
00173     } else {
00174         kDebug() << "Data for '" << name << "' not found";
00175     }
00176     return true;
00177 }
00178 
00179 void PowermanagementEngine::updateBatteryChargeState(int newState, const QString& udi)
00180 {
00181     QString state;
00182     if (newState == Solid::Battery::NoCharge) {
00183         state = "NoCharge";
00184     } else if (newState == Solid::Battery::Charging) {
00185         state = "Charging";
00186     } else if (newState == Solid::Battery::Discharging) {
00187         state = "Discharging";
00188     } else {
00189         state = "Could not determine battery status. Something is fishy here. :o";
00190     }
00191     const QString& source = m_batterySources[udi];
00192     setData(source, "State", state);
00193     scheduleSourcesUpdated();
00194 }
00195 
00196 void PowermanagementEngine::updateBatteryPlugState(bool newState, const QString& udi)
00197 {
00198     const QString& source = m_batterySources[udi];
00199     setData(source, "Plugged in", newState);
00200     scheduleSourcesUpdated();
00201 }
00202 
00203 void PowermanagementEngine::updateBatteryChargePercent(int newValue, const QString& udi)
00204 {
00205     const QString& source = m_batterySources[udi];
00206     setData(source, "Percent", newValue);
00207     scheduleSourcesUpdated();
00208 }
00209 
00210 void PowermanagementEngine::updateAcPlugState(bool newState)
00211 {
00212     setData("AC Adapter", "Plugged in", newState);
00213     scheduleSourcesUpdated();
00214 }
00215 
00216 void PowermanagementEngine::deviceRemoved(const QString& udi)
00217 {
00218     if (m_batterySources.contains(udi)) {
00219         QString source = m_batterySources[udi];
00220         m_batterySources.remove(udi);
00221         removeSource(source);
00222 
00223         QStringList sourceNames(m_batterySources.values());
00224         sourceNames.removeAll(source);
00225         setData("Battery", "sources", sourceNames);
00226     }
00227 }
00228 
00229 void PowermanagementEngine::deviceAdded(const QString& udi)
00230 {
00231     Solid::Device device(udi);
00232     if (device.isValid()) {
00233         const Solid::Battery* battery = device.as<Solid::Battery>();
00234 
00235         if (battery != 0) {
00236             int index = 0;
00237             QStringList sourceNames(m_batterySources.values());
00238             while (sourceNames.contains(QString("Battery%1").arg(index))) {
00239                 index++;
00240             }
00241 
00242             QString source = QString("Battery%1").arg(index);
00243             sourceNames << source;
00244             m_batterySources[device.udi()] = source;
00245 
00246             connect(battery, SIGNAL(chargeStateChanged(int, const QString &)), this,
00247                     SLOT(updateBatteryChargeState(int, const QString &)));
00248             connect(battery, SIGNAL(chargePercentChanged(int, const QString &)), this,
00249                     SLOT(updateBatteryChargePercent(int, const QString &)));
00250             connect(battery, SIGNAL(plugStateChanged(bool, const QString &)), this,
00251                     SLOT(updateBatteryPlugState(bool, const QString &)));
00252 
00253             // Set initial values
00254             updateBatteryChargeState(battery->chargeState(), device.udi());
00255             updateBatteryChargePercent(battery->chargePercent(), device.udi());
00256             updateBatteryPlugState(battery->isPlugged(), device.udi());
00257 
00258             setData("Battery", "sources", sourceNames);
00259         }
00260     }
00261 }
00262 
00263 void PowermanagementEngine::profilesChanged( const QString &current, const QStringList &profiles )
00264 {
00265     setData("PowerDevil", "currentProfile", current);
00266     setData("PowerDevil", "availableProfiles", profiles);
00267     scheduleSourcesUpdated();
00268 }
00269 
00270 void PowermanagementEngine::batteryRemainingTimeChanged(int time)
00271 {
00272     //kDebug() << "Remaining Time changed:" << time;
00273     setData("Battery0", "Remaining msec", time);
00274     scheduleSourcesUpdated();
00275 }
00276 
00277 #include "powermanagementengine.moc"

Engines

Skip menu "Engines"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal