Debian Codegraph @

autotests/fakebluez/objectmanager.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2014-2015 David Rosca <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7 #include "objectmanager.h"
8 #include "object.h"
9
10 #include <QDBusConnection>
11 #include <QDBusMetaType>
12
13 ObjectManager *s_instance = nullptr;
14
15 ObjectManager::ObjectManager(QObject *parent)
16 : QDBusAbstractAdaptor(parent)
17 {
18 s_instance = this;
19
20 qDBusRegisterMetaType<QVariantMapMap>();
21 qDBusRegisterMetaType<DBusManagerStruct>();
22
23 QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/bluez"), this);
24 }
25
26 ObjectManager::~ObjectManager()
27 {
28 s_instance = nullptr;
29 qDeleteAll(m_autoDeleteObjects);
30 }
31
32 void ObjectManager::addObject(Object *object)
33 {
34 m_objects.insert(object->path().path(), object);
35
36 QVariantMapMap interfaces;
37 interfaces.insert(object->name(), object->properties());
38 Q_EMIT InterfacesAdded(object->path(), interfaces);
39 }
40
41 void ObjectManager::removeObject(Object *object)
42 {
43 m_objects.remove(object->path().path());
44 m_autoDeleteObjects.removeOne(object->objectParent());
45
46 Q_EMIT InterfacesRemoved(object->path(), QStringList(object->name()));
47
48 delete object->objectParent();
49 }
50
51 void ObjectManager::addAutoDeleteObject(QObject *object)
52 {
53 m_autoDeleteObjects.append(object);
54 }
55
56 Object *ObjectManager::objectByPath(const QDBusObjectPath &path) const
57 {
58 return m_objects.value(path.path());
59 }
60
61 ObjectManager *ObjectManager::self()
62 {
63 return s_instance;
64 }
65
66 DBusManagerStruct ObjectManager::GetManagedObjects()
67 {
68 DBusManagerStruct objects;
69
70 for (Object *object : m_objects.values()) {
71 if (objects.value(object->path()).isEmpty()) {
72 objects[object->path()].insert(QStringLiteral("org.freedesktop.DBus.Introspectable"), QVariantMap());
73 if (object->haveProperties()) {
74 objects[object->path()].insert(QStringLiteral("org.freedesktop.DBus.Properties"), QVariantMap());
75 }
76 }
77 objects[object->path()].insert(object->name(), object->properties());
78 }
79
80 return objects;
81 }
82
83 #include "moc_objectmanager.cpp"