1 | /* |
2 | * BluezQt - Asynchronous Bluez wrapper library |
3 | * |
4 | * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com> |
5 | * |
6 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
7 | */ |
8 | |
9 | #include "obexmanager.h" |
10 | #include "debug.h" |
11 | #include "initobexmanagerjob.h" |
12 | #include "obexagent.h" |
13 | #include "obexagentadaptor.h" |
14 | #include "obexmanager_p.h" |
15 | #include "obexsession.h" |
16 | #include "pendingcall.h" |
17 | #include "utils.h" |
18 | |
19 | #include <QDBusServiceWatcher> |
20 | |
21 | namespace BluezQt |
22 | { |
23 | ObexManager::ObexManager(QObject *parent) |
24 | : QObject(parent) |
25 | , d(new ObexManagerPrivate(this)) |
26 | { |
27 | Instance::setObexManager(this); |
28 | } |
29 | |
30 | ObexManager::~ObexManager() = default; |
31 | |
32 | InitObexManagerJob *ObexManager::init() |
33 | { |
34 | return new InitObexManagerJob(this); |
35 | } |
36 | |
37 | bool ObexManager::isInitialized() const |
38 | { |
39 | return d->m_initialized; |
40 | } |
41 | |
42 | bool ObexManager::isOperational() const |
43 | { |
44 | return d->m_initialized && d->m_obexRunning && d->m_loaded; |
45 | } |
46 | |
47 | QList<ObexSessionPtr> ObexManager::sessions() const |
48 | { |
49 | return d->m_sessions.values(); |
50 | } |
51 | |
52 | ObexSessionPtr ObexManager::sessionForPath(const QDBusObjectPath &path) const |
53 | { |
54 | for (ObexSessionPtr session : std::as_const(t&: d->m_sessions)) { |
55 | if (path.path().startsWith(s: session->objectPath().path())) { |
56 | return session; |
57 | } |
58 | } |
59 | |
60 | return ObexSessionPtr(); |
61 | } |
62 | |
63 | PendingCall *ObexManager::startService() |
64 | { |
65 | QDBusMessage msg = QDBusMessage::createMethodCall(destination: Strings::orgFreedesktopDBus(), |
66 | QStringLiteral("/org/freedesktop/DBus" ), |
67 | interface: Strings::orgFreedesktopDBus(), |
68 | QStringLiteral("StartServiceByName" )); |
69 | msg << Strings::orgBluezObex(); |
70 | msg << quint32(0); |
71 | |
72 | return new PendingCall(DBusConnection::orgBluezObex().asyncCall(message: msg), PendingCall::ReturnUint32); |
73 | } |
74 | |
75 | PendingCall *ObexManager::registerAgent(ObexAgent *agent) |
76 | { |
77 | Q_ASSERT(agent); |
78 | |
79 | if (!d->m_obexAgentManager) { |
80 | return new PendingCall(PendingCall::InternalError, QStringLiteral("ObexManager not operational!" )); |
81 | } |
82 | |
83 | new ObexAgentAdaptor(agent, this); |
84 | |
85 | if (!DBusConnection::orgBluezObex().registerObject(path: agent->objectPath().path(), object: agent)) { |
86 | qCDebug(BLUEZQT) << "Cannot register object" << agent->objectPath().path(); |
87 | } |
88 | |
89 | return new PendingCall(d->m_obexAgentManager->RegisterAgent(agent: agent->objectPath()), PendingCall::ReturnVoid, this); |
90 | } |
91 | |
92 | PendingCall *ObexManager::unregisterAgent(ObexAgent *agent) |
93 | { |
94 | Q_ASSERT(agent); |
95 | |
96 | if (!d->m_obexAgentManager) { |
97 | return new PendingCall(PendingCall::InternalError, QStringLiteral("ObexManager not operational!" )); |
98 | } |
99 | |
100 | DBusConnection::orgBluezObex().unregisterObject(path: agent->objectPath().path()); |
101 | |
102 | return new PendingCall(d->m_obexAgentManager->UnregisterAgent(agent: agent->objectPath()), PendingCall::ReturnVoid, this); |
103 | } |
104 | |
105 | PendingCall *ObexManager::createSession(const QString &destination, const QVariantMap &args) |
106 | { |
107 | if (!d->m_obexClient) { |
108 | return new PendingCall(PendingCall::InternalError, QStringLiteral("ObexManager not operational!" )); |
109 | } |
110 | |
111 | return new PendingCall(d->m_obexClient->CreateSession(destination, args), PendingCall::ReturnObjectPath, this); |
112 | } |
113 | |
114 | PendingCall *ObexManager::removeSession(const QDBusObjectPath &session) |
115 | { |
116 | if (!d->m_obexClient) { |
117 | return new PendingCall(PendingCall::InternalError, QStringLiteral("ObexManager not operational!" )); |
118 | } |
119 | |
120 | return new PendingCall(d->m_obexClient->RemoveSession(session), PendingCall::ReturnVoid, this); |
121 | } |
122 | |
123 | } // namespace BluezQt |
124 | |
125 | #include "moc_obexmanager.cpp" |
126 | |