8#include "upowermanager.h"
10#include "upowerdevice.h"
12#include <QDBusConnectionInterface>
13#include <QDBusMetaType>
17#include "../shared/rootdevice.h"
19using namespace Solid::Backends::UPower;
20using namespace Solid::Backends::Shared;
22UPowerManager::UPowerManager(
QObject *parent)
23 : Solid::Ifaces::DeviceManager(parent)
24 , m_supportedInterfaces({Solid::DeviceInterface::GenericInterface, Solid::DeviceInterface::Battery})
26 , m_knownDevices(udiPrefix())
28 qDBusRegisterMetaType<QList<QDBusObjectPath>>();
29 qDBusRegisterMetaType<QVariantMap>();
31 bool serviceFound = m_manager.isValid();
35 QStringLiteral(
"/org/freedesktop/DBus"),
36 QStringLiteral(
"org.freedesktop.DBus"),
37 QStringLiteral(
"ListActivatableNames"));
40 if (reply.
isValid() && reply.value().contains(QStringLiteral(UP_DBUS_SERVICE))) {
47 connect(&m_manager, &UPower::DBusInterface::DeviceAdded,
this, &UPowerManager::onDeviceAdded);
48 connect(&m_manager, &UPower::DBusInterface::DeviceRemoved,
this, &UPowerManager::onDeviceRemoved);
52UPowerManager::~UPowerManager()
56QObject *UPowerManager::createDevice(
const QString &udi)
58 if (udi == udiPrefix()) {
59 RootDevice *root =
new RootDevice(udiPrefix());
61 root->setProduct(
tr(
"Power Management"));
62 root->setDescription(
tr(
"Batteries and other sources of power"));
63 root->setIcon(QStringLiteral(
"preferences-system-power-management"));
67 }
else if (m_knownDevices.contains(udi) || allDevices().contains(udi)) {
68 return new UPowerDevice(udi);
77 const QStringList allDev = allDevices();
81 for (
const QString &udi : allDev) {
82 if (udi == udiPrefix()) {
86 UPowerDevice device(udi);
87 if (device.queryDeviceInterface(type) && device.parentUdi() == parentUdi) {
93 }
else if (type != Solid::DeviceInterface::Unknown) {
94 for (
const QString &udi : allDev) {
95 if (udi == udiPrefix()) {
99 UPowerDevice device(udi);
100 if (device.queryDeviceInterface(type)) {
111QStringList UPowerManager::allDevices()
113 static bool initialized =
false;
115 return m_knownDevices;
117 auto reply = m_manager.EnumerateDevices();
118 reply.waitForFinished();
121 qWarning() << Q_FUNC_INFO <<
" error: " << reply.
error();
122 return QStringList();
124 const auto pathList = reply.value();
127 retList.
reserve(pathList.size() + m_knownDevices.size());
129 for (
const QDBusObjectPath &path : pathList) {
130 retList <<
path.path();
132 retList += m_knownDevices;
134 std::sort(retList.
begin(), retList.
end());
135 auto end = std::unique(retList.
begin(), retList.
end());
138 m_knownDevices = retList;
141 return m_knownDevices;
144QSet<Solid::DeviceInterface::Type> UPowerManager::supportedInterfaces()
const
146 return m_supportedInterfaces;
149QString UPowerManager::udiPrefix()
const
151 return QStringLiteral(UP_UDI_PREFIX);
154void UPowerManager::onDeviceAdded(
const QDBusObjectPath &path)
156 auto pathString =
path.path();
157 if (m_knownDevices.indexOf(pathString) < 0)
158 m_knownDevices.
append(pathString);
163void UPowerManager::onDeviceRemoved(
const QDBusObjectPath &path)
165 auto pathString =
path.path();
166 auto index = m_knownDevices.
indexOf(pathString);
168 m_knownDevices.removeAt(index);
173#include "moc_upowermanager.cpp"
Type
This enum type defines the type of device interface that a Device can have.
void deviceAdded(const QString &udi)
This signal is emitted when a new device appears in the system.
void deviceRemoved(const QString &udi)
This signal is emitted when a device disappears from the system.
QString path(const QString &relativePath)
const QList< QKeySequence > & end()
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const const
QDBusConnectionInterface * interface() const const
QDBusConnection systemBus()
QDBusReply< void > startService(const QString &name)
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
const QDBusError & error()
bool isValid() const const
iterator erase(const_iterator begin, const_iterator end)
void reserve(qsizetype size)
QString tr(const char *sourceText, const char *disambiguation, int n)
QString & append(QChar ch)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)