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:
92 if (prop(QStringLiteral(
"NativePath")).
toString().startsWith(QLatin1String(
"/org/bluez/"))) {
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:
109QStringList UPowerDevice::emblems()
const
111 return QStringList();
114QString UPowerDevice::description()
const
116 if (queryDeviceInterface(Solid::DeviceInterface::Battery)) {
117 if (!batteryTechnology().isEmpty()) {
118 return tr(
"%1 Battery",
"%1 is battery technology").
arg(batteryTechnology());
121 QString result = prop(QStringLiteral(
"Model")).toString();
128QString UPowerDevice::batteryTechnology()
const
130 const UpDeviceTechnology tech =
static_cast<UpDeviceTechnology
>(prop(QStringLiteral(
"Technology")).toUInt());
132 case UP_DEVICE_TECHNOLOGY_UNKNOWN:
134 case UP_DEVICE_TECHNOLOGY_LITHIUM_ION:
135 return tr(
"Lithium Ion",
"battery technology");
136 case UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER:
137 return tr(
"Lithium Polymer",
"battery technology");
138 case UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE:
139 return tr(
"Lithium Iron Phosphate",
"battery technology");
140 case UP_DEVICE_TECHNOLOGY_LEAD_ACID:
141 return tr(
"Lead Acid",
"battery technology");
142 case UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM:
143 return tr(
"Nickel Cadmium",
"battery technology");
144 case UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE:
145 return tr(
"Nickel Metal Hydride",
"battery technology");
146 case UP_DEVICE_TECHNOLOGY_LAST:
152QString UPowerDevice::icon()
const
154 if (queryDeviceInterface(Solid::DeviceInterface::Battery)) {
155 return QStringLiteral(
"battery");
161QString UPowerDevice::product()
const
163 QString result = prop(QStringLiteral(
"Model")).toString();
166 result = description();
172QString UPowerDevice::vendor()
const
174 return prop(QStringLiteral(
"Vendor")).toString();
177QString UPowerDevice::udi()
const
182QString UPowerDevice::parentUdi()
const
184 return QStringLiteral(UP_UDI_PREFIX);
187void UPowerDevice::checkCache(
const QString &key)
const
189 if (m_cache.contains(key) || m_negativeCache.contains(key)) {
195 if (m_cache.contains(key)) {
201 call.
setArguments({QStringLiteral(UP_DBUS_INTERFACE_DEVICE), key});
205 m_cache[key] = reply.value();
207 m_negativeCache.append(key);
211QVariant UPowerDevice::prop(
const QString &key)
const
214 return m_cache.value(key);
217bool UPowerDevice::propertyExists(
const QString &key)
const
220 return m_cache.contains(key);
223void UPowerDevice::loadCache()
const
226 QDBusMessage::createMethodCall(QStringLiteral(UP_DBUS_SERVICE), m_udi, QStringLiteral(
"org.freedesktop.DBus.Properties"), QStringLiteral(
"GetAll"));
227 call.
setArguments({QStringLiteral(UP_DBUS_INTERFACE_DEVICE)});
231 m_cache = reply.value();
232 m_cacheComplete =
true;
238QMap<QString, QVariant> UPowerDevice::allProperties()
const
240 if (!m_cacheComplete) {
247void UPowerDevice::onPropertiesChanged(
const QString &ifaceName,
const QVariantMap &changedProps,
const QStringList &invalidatedProps)
249 if (ifaceName != QStringLiteral(UP_DBUS_INTERFACE_DEVICE))
252 QMap<QString, int> changeMap;
253 for (
auto it = changedProps.begin(); it != changedProps.end(); ++it) {
254 m_cache[it.key()] = it.value();
255 m_negativeCache.removeOne(it.key());
256 changeMap.
insert(it.key(), Solid::GenericInterface::PropertyModified);
258 for (
const auto &propName : invalidatedProps) {
259 m_cache.remove(propName);
260 m_negativeCache.removeOne(propName);
261 changeMap.
insert(propName, Solid::GenericInterface::PropertyModified);
262 m_cacheComplete =
false;
264 Q_EMIT propertyChanged(changeMap);
267void UPowerDevice::login1Resuming(
bool active)
273 QMap<QString, int> changeMap;
274 for (
auto it = m_cache.begin(); it != m_cache.end(); ++it) {
275 if (it.value().isValid()) {
276 changeMap.
insert(it.key(), Solid::GenericInterface::PropertyModified);
280 m_negativeCache.clear();
281 m_cacheComplete =
false;
282 Q_EMIT propertyChanged(changeMap);
285#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
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