00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "powermanagementengine.h"
00022
00023
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
00045
00046
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
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
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
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"