1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include "upowerdevice.h"
#include "power.h"
#include <QDebug>
#include <QStringList>
#include <QDBusReply>
UPowerDevice::UPowerDevice(const QString &udi, QObject *parent)
: QObject(parent)
, m_device(UP_DBUS_SERVICE,
udi,
UP_DBUS_INTERFACE_DEVICE,
QDBusConnection::systemBus())
, m_udi(udi)
{
if (m_device.isValid()) {
if (m_device.metaObject()->indexOfSignal("Changed()") != -1) {
connect(&m_device, SIGNAL(Changed()), this, SLOT(slotChanged()));
} else {
// for UPower >= 0.99.0, missing Changed() signal
QDBusConnection::systemBus().connect(UP_DBUS_SERVICE, m_udi, "org.freedesktop.DBus.Properties",
"PropertiesChanged", this,
SLOT(onPropertiesChanged(QString, QVariantMap, QStringList)));
}
// TODO port this to Solid::Power, we can't link against kdelibs4support for this signal
// older upower versions not affected
QDBusConnection::systemBus().connect("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "PrepareForSleep",
this, SLOT(login1Resuming(bool)));
}
}
bool UPowerDevice::queryDeviceInterface(UPowerDevice::Type type) const
{
const uint uptype = prop("Type").toUInt();
switch (type) {
case GenericInterface:
return true;
case Battery:
switch (uptype) {
case UP_DEVICE_KIND_BATTERY:
case UP_DEVICE_KIND_UPS:
case UP_DEVICE_KIND_MOUSE:
case UP_DEVICE_KIND_KEYBOARD:
case UP_DEVICE_KIND_PDA:
case UP_DEVICE_KIND_PHONE:
case UP_DEVICE_KIND_GAMING_INPUT:
return true;
case UP_DEVICE_KIND_UNKNOWN:
// There is currently no "Bluetooth battery" type, so check if it comes from Bluez
if (prop("NativePath").toString().startsWith(QLatin1String("/org/bluez/"))) {
return true;
}
return false;
case UP_DEVICE_KIND_LINE_POWER:
case UP_DEVICE_KIND_MONITOR:
case UP_DEVICE_KIND_MEDIA_PLAYER:
case UP_DEVICE_KIND_TABLET:
case UP_DEVICE_KIND_COMPUTER:
case UP_DEVICE_KIND_LAST:
return false;
}
default:
return false;
}
}
QString UPowerDevice::description() const
{
if (queryDeviceInterface(Battery)) {
return tr("%1 Battery", "%1 is battery technology").arg(batteryTechnology());
} else {
QString result = prop("Model").toString();
if (result.isEmpty()) {
return vendor();
}
return result;
}
}
QString UPowerDevice::batteryTechnology() const
{
const uint tech = prop("Technology").toUInt();
switch (tech) {
case 1:
return tr("Lithium-ion", "battery technology");
case 2:
return tr("Lithium Polymer", "battery technology");
case 3:
return tr("Lithium Iron Phosphate", "battery technology");
case 4:
return tr("Lead Acid", "battery technology");
case 5:
return tr("Nickel Cadmium", "battery technology");
case 6:
return tr("Nickel Metal Hydride", "battery technology");
default:
return tr("Unknown", "battery technology");
}
}
QString UPowerDevice::product() const
{
QString result = prop("Model").toString();
if (result.isEmpty()) {
result = description();
}
return result;
}
QString UPowerDevice::vendor() const
{
return prop("Vendor").toString();
}
QString UPowerDevice::udi() const
{
return m_udi;
}
QVariant UPowerDevice::prop(const QString &key) const
{
checkCache(key);
return m_cache.value(key);
}
bool UPowerDevice::propertyExists(const QString &key) const
{
checkCache(key);
return m_cache.contains(key);
}
QMap<QString, QVariant> UPowerDevice::allProperties() const
{
QDBusMessage call = QDBusMessage::createMethodCall(m_device.service(), m_device.path(),
"org.freedesktop.DBus.Properties", "GetAll");
call << m_device.interface();
QDBusPendingReply< QVariantMap > reply = QDBusConnection::systemBus().asyncCall(call);
reply.waitForFinished();
if (reply.isValid()) {
m_cache = reply.value();
} else {
m_cache.clear();
}
return m_cache;
}
void UPowerDevice::onPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps)
{
Q_UNUSED(changedProps);
Q_UNUSED(invalidatedProps);
if (ifaceName == UP_DBUS_INTERFACE_DEVICE) {
slotChanged(); // TODO maybe process the properties separately?
}
}
void UPowerDevice::login1Resuming(bool active)
{
if (!active) {
QDBusReply<void> refreshCall = m_device.asyncCall("Refresh");
if (refreshCall.isValid()) {
slotChanged();
}
}
}
void UPowerDevice::slotChanged()
{
m_cache.clear();
emit changed();
}
void UPowerDevice::checkCache(const QString &key) const
{
if (m_cache.isEmpty()) { // recreate the cache
allProperties();
}
if (m_cache.contains(key)) {
return;
}
QVariant reply = m_device.property(key.toUtf8());
if (reply.isValid()) {
m_cache[key] = reply;
} else {
m_cache[key] = QVariant();
}
}
|