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
21namespace BluezQt
22{
23ObexManager::ObexManager(QObject *parent)
24 : QObject(parent)
25 , d(new ObexManagerPrivate(this))
26{
27 Instance::setObexManager(this);
28}
29
30ObexManager::~ObexManager() = default;
31
32InitObexManagerJob *ObexManager::init()
33{
34 return new InitObexManagerJob(this);
35}
36
37bool ObexManager::isInitialized() const
38{
39 return d->m_initialized;
40}
41
42bool ObexManager::isOperational() const
43{
44 return d->m_initialized && d->m_obexRunning && d->m_loaded;
45}
46
47QList<ObexSessionPtr> ObexManager::sessions() const
48{
49 return d->m_sessions.values();
50}
51
52ObexSessionPtr 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
63PendingCall *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
75PendingCall *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
92PendingCall *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
105PendingCall *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
114PendingCall *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

source code of bluez-qt/src/obexmanager.cpp