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

Engines

powermanagementengine.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Aaron Seigo <aseigo@kde.org>
00003  *   Copyright (C) 2007 Sebastian Kuegler <sebas@kde.org>
00004  *   CopyRight (C) 2007 Maor Vanmak <mvanmak1@gmail.com>
00005  *
00006  *   This program is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU Library General Public License version 2 as
00008  *   published by the Free Software Foundation
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details
00014  *
00015  *   You should have received a copy of the GNU Library General Public
00016  *   License along with this program; if not, write to the
00017  *   Free Software Foundation, Inc.,
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 #include "powermanagementengine.h"
00022 
00023 //solid specific includes
00024 #include <solid/devicenotifier.h>
00025 #include <solid/device.h>
00026 #include <solid/deviceinterface.h>
00027 #include <solid/battery.h>
00028 #include <solid/powermanagement.h>
00029 
00030 #include <KDebug>
00031 #include <KLocale>
00032 
00033 #include "plasma/datacontainer.h"
00034 
00035 PowermanagementEngine::PowermanagementEngine(QObject* parent, const QVariantList& args)
00036         : Plasma::DataEngine(parent, args)
00037         , m_acadapter(0)
00038         , m_sources(0)
00039 {
00040     Q_UNUSED(args)
00041         
00042     m_sources << I18N_NOOP("Battery") << I18N_NOOP("AC Adapter") << I18N_NOOP("Sleepstates");
00043     
00044     // This following call can be removed, but if we do, the
00045     // data is not shown in the plasmaengineexplorer.
00046     // sourceRequestEvent("Battery");
00047 }
00048 
00049 PowermanagementEngine::~PowermanagementEngine()
00050 {}
00051 
00052 void PowermanagementEngine::init()
00053 {
00054 }
00055 
00056 QStringList PowermanagementEngine::sources() const 
00057 {
00058     return m_sources;
00059 }
00060 
00061 bool PowermanagementEngine::sourceRequestEvent(const QString &name)
00062 {
00063     if (name == I18N_NOOP("Battery")) {
00064         QList<Solid::Device> list_battery =
00065                         Solid::Device::listFromType(Solid::DeviceInterface::Battery, QString());
00066         if (list_battery.count() == 0) {
00067             setData(I18N_NOOP("Battery"), I18N_NOOP("has Battery"), false);
00068             return true;
00069         }
00070         
00071         uint index = 0;
00072         QStringList battery_sources;
00073         
00074         foreach (const Solid::Device &device_battery, list_battery) {
00075             const Solid::Battery* battery = device_battery.as<Solid::Battery>();
00076 
00077             if(battery != 0) {
00078                 QString source = QString(I18N_NOOP("Battery%1")).arg(index++);
00079 
00080                 battery_sources<<source;
00081 
00082                 m_batterySources[device_battery.udi()] = source;
00083 
00084                 connect(battery, SIGNAL(chargeStateChanged(int, const QString &)), this,
00085                         SLOT(updateBatteryChargeState(int, const QString &)));
00086                 connect(battery, SIGNAL(chargePercentChanged(int, const QString &)), this,
00087                         SLOT(updateBatteryChargePercent(int, const QString &)));
00088                 connect(battery, SIGNAL(plugStateChanged(bool, const QString &)), this,
00089                         SLOT(updateBatteryPlugState(bool, const QString &)));
00090 
00091                 // Set initial values
00092                 updateBatteryChargeState(battery->chargeState(), device_battery.udi());
00093                 updateBatteryChargePercent(battery->chargePercent(), device_battery.udi());
00094                 updateBatteryPlugState(battery->isPlugged(), device_battery.udi());
00095             }
00096         }
00097         
00098         if(battery_sources.count() > 0) {
00099             setData(I18N_NOOP("Battery"), I18N_NOOP("has Battery"), true);
00100             setData(I18N_NOOP("Battery"), I18N_NOOP("sources"), battery_sources);
00101         }
00102     } else if (name == I18N_NOOP("AC Adapter")) {
00103         // AC Adapter handling
00104         QList<Solid::Device> list_ac =
00105                         Solid::Device::listFromType(Solid::DeviceInterface::AcAdapter, QString());
00106         foreach (Solid::Device device_ac, list_ac) {
00107             m_acadapter = device_ac.as<Solid::AcAdapter>();
00108             updateAcPlugState(m_acadapter->isPlugged());
00109             connect(m_acadapter, SIGNAL(plugStateChanged(bool, const QString &)), this,
00110                     SLOT(updateAcPlugState(bool)));
00111         }
00112     } else if (name == I18N_NOOP("Sleepstates")) {
00113         QSet<Solid::PowerManagement::SleepState> sleepstates =
00114                                 Solid::PowerManagement::supportedSleepStates();
00115         // We first set all possible sleepstates to false, then enable the ones that are available
00116         setData(I18N_NOOP("Sleepstates"), I18N_NOOP("Standby"), false);
00117         setData(I18N_NOOP("Sleepstates"), I18N_NOOP("Suspend"), false);
00118         setData(I18N_NOOP("Sleepstates"), I18N_NOOP("Hibernate"), false);
00119 
00120         foreach (Solid::PowerManagement::SleepState sleepstate, sleepstates) {
00121             if (sleepstate == Solid::PowerManagement::StandbyState) {
00122                 setData(I18N_NOOP("Sleepstates"), I18N_NOOP("Supports standby"), true);
00123             } else if (sleepstate == Solid::PowerManagement::SuspendState) {
00124                 setData(I18N_NOOP("Sleepstates"), I18N_NOOP("Supports suspend"), true);
00125             } else if (sleepstate == Solid::PowerManagement::HibernateState) {
00126                 setData(I18N_NOOP("Sleepstates"), I18N_NOOP("Supports hibernate"), true);
00127             }
00128             kDebug() << "Sleepstate \"" << sleepstate << "\" supported.";
00129         }
00130     } else {
00131         kDebug() << "Data for '" << name << "' not found";
00132     }
00133     return true;
00134 }
00135 
00136 void PowermanagementEngine::updateBatteryChargeState(int newState, const QString& udi)
00137 {
00138     QString state;
00139     if (newState == Solid::Battery::NoCharge) {
00140         state = I18N_NOOP("NoCharge");
00141     } else if (newState == Solid::Battery::Charging) {
00142         state = I18N_NOOP("Charging");
00143     } else if (newState == Solid::Battery::Discharging) {
00144         state = I18N_NOOP("Discharging");
00145     } else {
00146         state = I18N_NOOP("Could not determine battery status. Something is fishy here. :o");
00147     }
00148     const QString& source = m_batterySources[udi];
00149     setData(source, I18N_NOOP("State"), state);
00150     scheduleSourcesUpdated();
00151 }
00152 
00153 void PowermanagementEngine::updateBatteryPlugState(bool newState, const QString& udi)
00154 {
00155     const QString& source = m_batterySources[udi];
00156     setData(source, I18N_NOOP("Plugged in"), newState);
00157     scheduleSourcesUpdated();
00158 }
00159 
00160 void PowermanagementEngine::updateBatteryChargePercent(int newValue, const QString& udi)
00161 {
00162     const QString& source = m_batterySources[udi];
00163     setData(source, I18N_NOOP("Percent"), newValue);
00164     scheduleSourcesUpdated();
00165 }
00166 
00167 void PowermanagementEngine::updateAcPlugState(bool newState)
00168 {
00169     setData(I18N_NOOP("AC Adapter"), I18N_NOOP("Plugged in"), newState);
00170     scheduleSourcesUpdated();
00171 }
00172 
00173 #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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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