41 static bool available =
false;
42 static bool checked =
false;
48 QDBusInterface upower(QStringLiteral(
"org.freedesktop.UPower"),
49 QStringLiteral(
"/org/freedesktop/UPower"),
50 QStringLiteral(
"org.freedesktop.UPower"),
51 QDBusConnection::systemBus());
52 QDBusInterface consolekit(QStringLiteral(
"org.freedesktop.ConsoleKit"),
53 QStringLiteral(
"/org/freedesktop/ConsoleKit/Manager"),
54 QStringLiteral(
"org.freedesktop.ConsoleKit.Manager"),
55 QDBusConnection::systemBus());
57 available = upower.isValid() && consolekit.isValid();
60 LOG(VB_GENERAL, LOG_WARNING, QStringLiteral(
"UPower and ConsoleKit not available"));
69 m_devices(QMap<QString,int>()),
70 m_upowerInterface(QStringLiteral(
"org.freedesktop.UPower"),
71 QStringLiteral(
"/org/freedesktop/UPower"),
72 QStringLiteral(
"org.freedesktop.UPower"),
73 QDBusConnection::systemBus()),
74 m_consoleInterface(QStringLiteral(
"org.freedesktop.ConsoleKit"),
75 QStringLiteral(
"/org/freedesktop/ConsoleKit/Manager"),
76 QStringLiteral(
"org.freedesktop.ConsoleKit.Manager"),
77 QDBusConnection::systemBus())
79 if (m_consoleInterface.isValid())
81 QDBusReply<bool> shutdown = m_consoleInterface.call(QStringLiteral(
"CanStop"));
82 if (shutdown.isValid() && shutdown.value())
85 QDBusReply<bool> restart = m_consoleInterface.call(QStringLiteral(
"CanRestart"));
86 if (restart.isValid() && restart.value())
91 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to create ConsoleKit interface"));
95 if (m_upowerInterface.isValid())
97 QDBusReply<QList<QDBusObjectPath> > devicecheck = m_upowerInterface.call(QStringLiteral(
"EnumerateDevices"));
99 if (devicecheck.isValid())
100 foreach (
const QDBusObjectPath &device, devicecheck.value())
105 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to create UPower interface"));
109 if (!QDBusConnection::systemBus().connect(
110 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"/org/freedesktop/UPower"),
111 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"Sleeping"),
114 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to register for sleep events"));
117 if (!QDBusConnection::systemBus().connect(
118 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"/org/freedesktop/UPower"),
119 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"Resuming"),
122 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to register for resume events"));
125 if (!QDBusConnection::systemBus().connect(
126 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"/org/freedesktop/UPower"),
127 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"Changed"),
130 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to register for Changed"));
133 if (!QDBusConnection::systemBus().connect(
134 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"/org/freedesktop/UPower"),
135 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"DeviceChanged"), QStringLiteral(
"o"),
138 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to register for DeviceChanged"));
141 if (!QDBusConnection::systemBus().connect(
142 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"/org/freedesktop/UPower"),
143 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"DeviceAdded"), QStringLiteral(
"o"),
146 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to register for DeviceAdded"));
149 if (!QDBusConnection::systemBus().connect(
150 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"/org/freedesktop/UPower"),
151 QStringLiteral(
"org.freedesktop.UPower"), QStringLiteral(
"DeviceRemoved"), QStringLiteral(
"o"),
154 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to register for DeviceRemoved"));
168 QList<QVariant> dummy;
169 if (m_consoleInterface.callWithCallback(QStringLiteral(
"Stop"), dummy, (QObject*)
this, SLOT(
DBusCallback()), SLOT(
DBusError(QDBusError))))
176 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Shutdown call failed"));
187 QList<QVariant> dummy;
188 if (m_upowerInterface.callWithCallback(QStringLiteral(
"AboutToSleep"), dummy, (QObject*)
this, SLOT(
DBusCallback()), SLOT(
DBusError(QDBusError))))
190 if (m_upowerInterface.callWithCallback(QStringLiteral(
"Suspend"), dummy, (QObject*)
this, SLOT(
DBusCallback()), SLOT(
DBusError(QDBusError))))
198 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Suspend call failed"));
209 QList<QVariant> dummy;
210 if (m_upowerInterface.callWithCallback(QStringLiteral(
"AboutToSleep"), dummy, (QObject*)
this, SLOT(
DBusCallback()), SLOT(
DBusError(QDBusError))))
212 if (m_upowerInterface.callWithCallback(QStringLiteral(
"Hibernate"), dummy, (QObject*)
this, SLOT(
DBusCallback()), SLOT(
DBusError(QDBusError))))
220 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Hibernate call failed"));
231 QList<QVariant> dummy;
232 if (m_consoleInterface.callWithCallback(QStringLiteral(
"Restart"), dummy, (QObject*)
this, SLOT(
DBusCallback()), SLOT(
DBusError(QDBusError))))
239 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Restart call failed"));
250 if (m_devices.contains(Device.path()))
256 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Added UPower.Device '%1'").arg(Device.path()));
265 if (!m_devices.contains(Device.path()))
268 m_devices.remove(Device.path());
271 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Removed UPower.Device '%1'").arg(Device.path()));
280 if (!m_devices.contains(Device.path()))
291 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"DBus callback error: %1, %2")
292 .arg(Error.name(), Error.message().trimmed()));
305 void TorcPowerUnixDBus::UpdateBattery(
void)
313 QMap<QString,int>::const_iterator it = m_devices.constBegin();
314 for ( ; it != m_devices.constEnd(); ++it)
316 if (it.value() >= 0 && it.value() <= 100)
319 total += (qreal)it.value();
344 QDBusInterface interface(QStringLiteral(
"org.freedesktop.UPower"), Path, QStringLiteral(
"org.freedesktop.UPower.Device"),
345 QDBusConnection::systemBus());
347 if (interface.isValid())
349 QVariant battery = interface.property(
"IsRechargeable");
350 if (battery.isValid() && battery.toBool() ==
true)
352 QVariant percent = interface.property(
"Percentage");
353 if (percent.isValid())
355 int result = lround(percent.toFloat() * 100.0);
356 return qBound(0, result, 100);
368 void TorcPowerUnixDBus::UpdateProperties(
void)
376 if (m_upowerInterface.isValid())
378 QVariant cansuspend = m_upowerInterface.property(
"CanSuspend");
379 if (cansuspend.isValid() && cansuspend.toBool() ==
true)
382 QVariant canhibernate = m_upowerInterface.property(
"CanHibernate");
383 if (canhibernate.isValid() && canhibernate.toBool() ==
true)
386 QVariant onbattery = m_upowerInterface.property(
"OnBattery");
387 if (onbattery.isValid() && onbattery.toBool() ==
true)
394 void Score(
int &Score)
bool DoHibernate(void) override
void DeviceAdded(QDBusObjectPath Device)
void BatteryUpdated(int Level)
TorcSetting * m_canHibernate
void DeviceChanged(QDBusObjectPath Device)
TorcSetting * m_canSuspend
void DeviceRemoved(QDBusObjectPath Device)
static bool Available(void)
QReadWriteLock m_httpServiceLock
TorcSetting * m_canShutdown
TorcPowerFactoryUnixDBus TorcPowerFactoryUnixDBus
A generic power status class.
void DBusError(QDBusError Error)
bool SetValue(const QVariant &Value)
#define LOG(_MASK_, _LEVEL_, _STRING_)
bool DoSuspend(void) override
bool DoShutdown(void) override
int GetBatteryLevel(void)
TorcSetting * m_canRestart
bool DoRestart(void) override