8#include "upowerdevice.h"
10#include "upowerbattery.h"
11#include "upowerdeviceinterface.h"
12#include "upowergenericinterface.h"
14#include <solid/device.h>
15#include <solid/genericinterface.h>
17#include <QDBusConnection>
21using namespace Solid::Backends::UPower;
23UPowerDevice::UPowerDevice(
const QString &udi)
29 QStringLiteral(
"org.freedesktop.DBus.Properties"),
30 QStringLiteral(
"PropertiesChanged"),
37 QStringLiteral(
"/org/freedesktop/login1"),
38 QStringLiteral(
"org.freedesktop.login1.Manager"),
39 QStringLiteral(
"PrepareForSleep"),
41 SLOT(login1Resuming(
bool)));
44UPowerDevice::~UPowerDevice()
50 if (!queryDeviceInterface(type)) {
54 DeviceInterface *iface =
nullptr;
56 case Solid::DeviceInterface::GenericInterface:
57 iface =
new GenericInterface(
this);
59 case Solid::DeviceInterface::Battery:
60 iface =
new Battery(
this);
70 const UpDeviceKind uptype =
static_cast<UpDeviceKind
>(prop(QStringLiteral(
"Type")).
toUInt());
72 case Solid::DeviceInterface::GenericInterface:
74 case Solid::DeviceInterface::Battery:
76 case UP_DEVICE_KIND_BATTERY:
77 case UP_DEVICE_KIND_UPS:
78 case UP_DEVICE_KIND_MOUSE:
79 case UP_DEVICE_KIND_KEYBOARD:
80 case UP_DEVICE_KIND_PDA:
81 case UP_DEVICE_KIND_PHONE:
82 case UP_DEVICE_KIND_TABLET:
83 case UP_DEVICE_KIND_GAMING_INPUT:
84 case UP_DEVICE_KIND_SPEAKERS:
85 case UP_DEVICE_KIND_HEADSET:
86 case UP_DEVICE_KIND_HEADPHONES:
87 case UP_DEVICE_KIND_BLUETOOTH_GENERIC:
88 case UP_DEVICE_KIND_TOUCHPAD:
90 case UP_DEVICE_KIND_UNKNOWN:
96 case UP_DEVICE_KIND_LINE_POWER:
97 case UP_DEVICE_KIND_MONITOR:
98 case UP_DEVICE_KIND_MEDIA_PLAYER:
99 case UP_DEVICE_KIND_COMPUTER:
100 case UP_DEVICE_KIND_LAST:
114QString UPowerDevice::description()
const
116 if (queryDeviceInterface(Solid::DeviceInterface::Battery)) {
117 return tr(
"%1 Battery",
"%1 is battery technology").
arg(batteryTechnology());
127QString UPowerDevice::batteryTechnology()
const
129 const UpDeviceTechnology tech =
static_cast<UpDeviceTechnology
>(prop(QStringLiteral(
"Technology")).
toUInt());
131 case UP_DEVICE_TECHNOLOGY_UNKNOWN:
132 return tr(
"Unknown",
"battery technology");
133 case UP_DEVICE_TECHNOLOGY_LITHIUM_ION:
134 return tr(
"Lithium Ion",
"battery technology");
135 case UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER:
136 return tr(
"Lithium Polymer",
"battery technology");
137 case UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE:
138 return tr(
"Lithium Iron Phosphate",
"battery technology");
139 case UP_DEVICE_TECHNOLOGY_LEAD_ACID:
140 return tr(
"Lead Acid",
"battery technology");
141 case UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM:
142 return tr(
"Nickel Cadmium",
"battery technology");
143 case UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE:
144 return tr(
"Nickel Metal Hydride",
"battery technology");
145 case UP_DEVICE_TECHNOLOGY_LAST:
146 return tr(
"Unknown",
"battery technology");
148 return tr(
"Unknown",
"battery technology");
151QString UPowerDevice::icon()
const
153 if (queryDeviceInterface(Solid::DeviceInterface::Battery)) {
154 return QStringLiteral(
"battery");
160QString UPowerDevice::product()
const
165 result = description();
171QString UPowerDevice::vendor()
const
173 return prop(QStringLiteral(
"Vendor")).
toString();
176QString UPowerDevice::udi()
const
181QString UPowerDevice::parentUdi()
const
183 return QStringLiteral(UP_UDI_PREFIX);
186void UPowerDevice::checkCache(
const QString &key)
const
188 if (m_cache.contains(key) || m_negativeCache.
contains(key)) {
194 if (m_cache.contains(key)) {
200 call.
setArguments({QStringLiteral(UP_DBUS_INTERFACE_DEVICE), key});
204 m_cache[key] = reply.value();
206 m_negativeCache.
append(key);
213 return m_cache.value(key);
216bool UPowerDevice::propertyExists(
const QString &key)
const
219 return m_cache.contains(key);
222void UPowerDevice::loadCache()
const
225 QDBusMessage::createMethodCall(QStringLiteral(UP_DBUS_SERVICE), m_udi, QStringLiteral(
"org.freedesktop.DBus.Properties"), QStringLiteral(
"GetAll"));
226 call.
setArguments({QStringLiteral(UP_DBUS_INTERFACE_DEVICE)});
230 m_cache = reply.value();
231 m_cacheComplete =
true;
239 if (!m_cacheComplete) {
246void UPowerDevice::onPropertiesChanged(
const QString &ifaceName,
const QVariantMap &changedProps,
const QStringList &invalidatedProps)
248 if (ifaceName != QStringLiteral(UP_DBUS_INTERFACE_DEVICE))
252 for (
auto it = changedProps.begin(); it != changedProps.end(); ++it) {
253 m_cache[it.key()] = it.value();
255 changeMap.
insert(it.key(), Solid::GenericInterface::PropertyModified);
257 for (
const auto &propName : invalidatedProps) {
258 m_cache.remove(propName);
260 changeMap.
insert(propName, Solid::GenericInterface::PropertyModified);
261 m_cacheComplete =
false;
263 Q_EMIT propertyChanged(changeMap);
266void UPowerDevice::login1Resuming(
bool active)
273 for (
auto it = m_cache.begin(); it != m_cache.end(); ++it) {
274 if (it.value().isValid()) {
275 changeMap.
insert(it.key(), Solid::GenericInterface::PropertyModified);
279 m_negativeCache.
clear();
280 m_cacheComplete =
false;
281 Q_EMIT propertyChanged(changeMap);
284#include "moc_upowerdevice.cpp"
Type
This enum type defines the type of device interface that a Device can have.
This class allows applications to deal with devices available in the underlying system.
char * toString(const EngineQuery &query)
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const const
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
QDBusConnection systemBus()
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void setArguments(const QList< QVariant > &arguments)
bool isValid() const const
void append(QList< T > &&value)
bool removeOne(const AT &t)
iterator insert(const Key &key, const T &value)
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(Args &&... args) const const
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QString toString() const const
uint toUInt(bool *ok) const const