add more spacing
[personal-kdebase.git] / workspace / plasma / dataengines / powermanagement / powermanagementengine.cpp
bloba4d4a1af82c8b51fa9874cd9a1cb65c33b73366b
1 /*
2 * Copyright 2007 Aaron Seigo <aseigo@kde.org>
3 * Copyright 2007-2008 Sebastian Kuegler <sebas@kde.org>
4 * CopyRight 2007 Maor Vanmak <mvanmak1@gmail.com>
5 * Copyright 2008 Dario Freddi <drf54321@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Library General Public License version 2 as
9 * published by the Free Software Foundation
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details
16 * You should have received a copy of the GNU Library General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "powermanagementengine.h"
24 //solid specific includes
25 #include <solid/devicenotifier.h>
26 #include <solid/device.h>
27 #include <solid/deviceinterface.h>
28 #include <solid/battery.h>
29 #include <solid/powermanagement.h>
30 #include <solid/control/powermanager.h>
32 #include <KDebug>
33 #include <KLocale>
35 #include <QtDBus/QDBusInterface>
36 #include <QtDBus/QDBusReply>
38 #include <Plasma/DataContainer>
40 PowermanagementEngine::PowermanagementEngine(QObject* parent, const QVariantList& args)
41 : Plasma::DataEngine(parent, args)
42 , m_acadapter(0)
43 , m_sources(0)
44 , m_dbus(QDBusConnection::sessionBus())
46 Q_UNUSED(args)
48 m_sources << "Battery" << "AC Adapter" << "Sleepstates" << "PowerDevil";
50 // This following call can be removed, but if we do, the
51 // data is not shown in the plasmaengineexplorer.
52 // sourceRequestEvent("Battery");
55 PowermanagementEngine::~PowermanagementEngine()
58 void PowermanagementEngine::init()
60 connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(QString)),
61 this, SLOT(deviceRemoved(QString)));
62 connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(QString)),
63 this, SLOT(deviceAdded(QString)));
64 connect(Solid::Control::PowerManager::notifier(), SIGNAL(batteryRemainingTimeChanged(int)),
65 this, SLOT(batteryRemainingTimeChanged(int)));
67 QStringList modules;
68 QDBusInterface kdedInterface("org.kde.kded", "/kded", "org.kde.kded");
69 QDBusReply<QStringList> reply = kdedInterface.call("loadedModules");
71 if (!reply.isValid()) {
72 return;
75 modules = reply.value();
77 if (modules.contains("powerdevil")) {
79 if (!m_dbus.connect("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil",
80 "profileChanged", this,
81 SLOT(profilesChanged(const QString&, const QStringList&)))) {
82 kDebug() << "error!";
85 QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded", "/modules/powerdevil",
86 "org.kde.PowerDevil", "streamData");
87 m_dbus.call(msg);
92 QStringList PowermanagementEngine::sources() const
94 return m_sources + m_batterySources.values();
97 bool PowermanagementEngine::sourceRequestEvent(const QString &name)
99 if (name == "Battery") {
100 QList<Solid::Device> list_battery =
101 Solid::Device::listFromType(Solid::DeviceInterface::Battery, QString());
102 if (list_battery.count() == 0) {
103 setData("Battery", "has Battery", false);
104 return true;
107 uint index = 0;
108 QStringList battery_sources;
110 foreach (const Solid::Device &device_battery, list_battery) {
111 const Solid::Battery* battery = device_battery.as<Solid::Battery>();
113 if(battery != 0) {
114 if(battery->type() == Solid::Battery::PrimaryBattery) {
115 QString source = QString("Battery%1").arg(index++);
117 battery_sources<<source;
119 m_batterySources[device_battery.udi()] = source;
121 connect(battery, SIGNAL(chargeStateChanged(int, const QString &)), this,
122 SLOT(updateBatteryChargeState(int, const QString &)));
123 connect(battery, SIGNAL(chargePercentChanged(int, const QString &)), this,
124 SLOT(updateBatteryChargePercent(int, const QString &)));
125 connect(battery, SIGNAL(plugStateChanged(bool, const QString &)), this,
126 SLOT(updateBatteryPlugState(bool, const QString &)));
128 // Set initial values
129 updateBatteryChargeState(battery->chargeState(), device_battery.udi());
130 updateBatteryChargePercent(battery->chargePercent(), device_battery.udi());
131 updateBatteryPlugState(battery->isPlugged(), device_battery.udi());
136 if (battery_sources.count() > 0) {
137 setData("Battery", "has Battery", true);
138 setData("Battery", "sources", battery_sources);
139 setData("Battery", "Remaining msec", Solid::Control::PowerManager::batteryRemainingTime());
141 } else if (name == "AC Adapter") {
142 // AC Adapter handling
143 QList<Solid::Device> list_ac =
144 Solid::Device::listFromType(Solid::DeviceInterface::AcAdapter, QString());
145 foreach (Solid::Device device_ac, list_ac) {
146 m_acadapter = device_ac.as<Solid::AcAdapter>();
147 updateAcPlugState(m_acadapter->isPlugged());
148 connect(m_acadapter, SIGNAL(plugStateChanged(bool, const QString &)), this,
149 SLOT(updateAcPlugState(bool)));
151 } else if (name == "Sleepstates") {
152 QSet<Solid::PowerManagement::SleepState> sleepstates =
153 Solid::PowerManagement::supportedSleepStates();
154 // We first set all possible sleepstates to false, then enable the ones that are available
155 setData("Sleepstates", "Standby", false);
156 setData("Sleepstates", "Suspend", false);
157 setData("Sleepstates", "Hibernate", false);
159 foreach (const Solid::PowerManagement::SleepState &sleepstate, sleepstates) {
160 if (sleepstate == Solid::PowerManagement::StandbyState) {
161 setData("Sleepstates", "Supports standby", true);
162 } else if (sleepstate == Solid::PowerManagement::SuspendState) {
163 setData("Sleepstates", "Supports suspend", true);
164 } else if (sleepstate == Solid::PowerManagement::HibernateState) {
165 setData("Sleepstates", "Supports hibernate", true);
167 kDebug() << "Sleepstate \"" << sleepstate << "\" supported.";
169 } else if (name == "PowerDevil") {
170 QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded", "/modules/powerdevil",
171 "org.kde.PowerDevil", "streamData");
172 m_dbus.call(msg);
173 } else {
174 kDebug() << "Data for '" << name << "' not found";
176 return true;
179 void PowermanagementEngine::updateBatteryChargeState(int newState, const QString& udi)
181 QString state;
182 if (newState == Solid::Battery::NoCharge) {
183 state = "NoCharge";
184 } else if (newState == Solid::Battery::Charging) {
185 state = "Charging";
186 } else if (newState == Solid::Battery::Discharging) {
187 state = "Discharging";
188 } else {
189 state = "Could not determine battery status. Something is fishy here. :o";
191 const QString& source = m_batterySources[udi];
192 setData(source, "State", state);
193 scheduleSourcesUpdated();
196 void PowermanagementEngine::updateBatteryPlugState(bool newState, const QString& udi)
198 const QString& source = m_batterySources[udi];
199 setData(source, "Plugged in", newState);
200 scheduleSourcesUpdated();
203 void PowermanagementEngine::updateBatteryChargePercent(int newValue, const QString& udi)
205 const QString& source = m_batterySources[udi];
206 setData(source, "Percent", newValue);
207 scheduleSourcesUpdated();
210 void PowermanagementEngine::updateAcPlugState(bool newState)
212 setData("AC Adapter", "Plugged in", newState);
213 scheduleSourcesUpdated();
216 void PowermanagementEngine::deviceRemoved(const QString& udi)
218 if (m_batterySources.contains(udi)) {
219 QString source = m_batterySources[udi];
220 m_batterySources.remove(udi);
221 removeSource(source);
223 QStringList sourceNames(m_batterySources.values());
224 sourceNames.removeAll(source);
225 setData("Battery", "sources", sourceNames);
229 void PowermanagementEngine::deviceAdded(const QString& udi)
231 Solid::Device device(udi);
232 if (device.isValid()) {
233 const Solid::Battery* battery = device.as<Solid::Battery>();
235 if (battery != 0) {
236 int index = 0;
237 QStringList sourceNames(m_batterySources.values());
238 while (sourceNames.contains(QString("Battery%1").arg(index))) {
239 index++;
242 QString source = QString("Battery%1").arg(index);
243 sourceNames << source;
244 m_batterySources[device.udi()] = source;
246 connect(battery, SIGNAL(chargeStateChanged(int, const QString &)), this,
247 SLOT(updateBatteryChargeState(int, const QString &)));
248 connect(battery, SIGNAL(chargePercentChanged(int, const QString &)), this,
249 SLOT(updateBatteryChargePercent(int, const QString &)));
250 connect(battery, SIGNAL(plugStateChanged(bool, const QString &)), this,
251 SLOT(updateBatteryPlugState(bool, const QString &)));
253 // Set initial values
254 updateBatteryChargeState(battery->chargeState(), device.udi());
255 updateBatteryChargePercent(battery->chargePercent(), device.udi());
256 updateBatteryPlugState(battery->isPlugged(), device.udi());
258 setData("Battery", "sources", sourceNames);
263 void PowermanagementEngine::profilesChanged( const QString &current, const QStringList &profiles )
265 setData("PowerDevil", "currentProfile", current);
266 setData("PowerDevil", "availableProfiles", profiles);
267 scheduleSourcesUpdated();
270 void PowermanagementEngine::batteryRemainingTimeChanged(int time)
272 //kDebug() << "Remaining Time changed:" << time;
273 setData("Battery0", "Remaining msec", time);
274 scheduleSourcesUpdated();
277 #include "powermanagementengine.moc"