File indexing completed on 2024-04-14 03:49:53

0001 /*
0002  * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "objectmanager.h"
0008 #include "object.h"
0009 
0010 #include <QDBusConnection>
0011 #include <QDBusMetaType>
0012 
0013 ObjectManager *s_instance = nullptr;
0014 
0015 ObjectManager::ObjectManager(QObject *parent)
0016     : QDBusAbstractAdaptor(parent)
0017 {
0018     s_instance = this;
0019 
0020     qDBusRegisterMetaType<QVariantMapMap>();
0021     qDBusRegisterMetaType<DBusManagerStruct>();
0022 
0023     QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/bluez"), this);
0024 }
0025 
0026 ObjectManager::~ObjectManager()
0027 {
0028     s_instance = nullptr;
0029     qDeleteAll(m_autoDeleteObjects);
0030 }
0031 
0032 void ObjectManager::addObject(Object *object)
0033 {
0034     m_objects.insert(object->path().path(), object);
0035 
0036     QVariantMapMap interfaces;
0037     interfaces.insert(object->name(), object->properties());
0038     Q_EMIT InterfacesAdded(object->path(), interfaces);
0039 }
0040 
0041 void ObjectManager::removeObject(Object *object)
0042 {
0043     m_objects.remove(object->path().path());
0044     m_autoDeleteObjects.removeOne(object->objectParent());
0045 
0046     Q_EMIT InterfacesRemoved(object->path(), QStringList(object->name()));
0047 
0048     delete object->objectParent();
0049 }
0050 
0051 void ObjectManager::addAutoDeleteObject(QObject *object)
0052 {
0053     m_autoDeleteObjects.append(object);
0054 }
0055 
0056 Object *ObjectManager::objectByPath(const QDBusObjectPath &path) const
0057 {
0058     return m_objects.value(path.path());
0059 }
0060 
0061 ObjectManager *ObjectManager::self()
0062 {
0063     return s_instance;
0064 }
0065 
0066 DBusManagerStruct ObjectManager::GetManagedObjects()
0067 {
0068     DBusManagerStruct objects;
0069 
0070     for (Object *object : m_objects.values()) {
0071         if (objects.value(object->path()).isEmpty()) {
0072             objects[object->path()].insert(QStringLiteral("org.freedesktop.DBus.Introspectable"), QVariantMap());
0073             if (object->haveProperties()) {
0074                 objects[object->path()].insert(QStringLiteral("org.freedesktop.DBus.Properties"), QVariantMap());
0075             }
0076         }
0077         objects[object->path()].insert(object->name(), object->properties());
0078     }
0079 
0080     return objects;
0081 }
0082 
0083 #include "moc_objectmanager.cpp"