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 "initmanagerjob.h" |
10 | #include "debug.h" |
11 | #include "manager.h" |
12 | #include "manager_p.h" |
13 | |
14 | namespace BluezQt |
15 | { |
16 | class InitManagerJobPrivate : public QObject |
17 | { |
18 | public: |
19 | explicit InitManagerJobPrivate(InitManagerJob *q, Manager *manager); |
20 | |
21 | void doStart(); |
22 | void initError(const QString &errorText); |
23 | void initFinished(); |
24 | |
25 | InitManagerJob *q; |
26 | Manager *m_manager; |
27 | }; |
28 | |
29 | InitManagerJobPrivate::InitManagerJobPrivate(InitManagerJob *q, Manager *manager) |
30 | : QObject(q) |
31 | , q(q) |
32 | , m_manager(manager) |
33 | { |
34 | } |
35 | |
36 | void InitManagerJobPrivate::doStart() |
37 | { |
38 | if (m_manager->d->m_initialized) { |
39 | qCWarning(BLUEZQT) << "InitManagerJob: Manager already initialized!" ; |
40 | q->emitResult(); |
41 | return; |
42 | } |
43 | |
44 | m_manager->d->init(); |
45 | connect(sender: m_manager->d.get(), signal: &ManagerPrivate::initError, context: this, slot: &InitManagerJobPrivate::initError); |
46 | connect(sender: m_manager->d.get(), signal: &ManagerPrivate::initFinished, context: this, slot: &InitManagerJobPrivate::initFinished); |
47 | } |
48 | |
49 | void InitManagerJobPrivate::initError(const QString &errorText) |
50 | { |
51 | qCWarning(BLUEZQT) << "GetManagerJob Error:" << errorText; |
52 | |
53 | q->setError(InitManagerJob::UserDefinedError); |
54 | q->setErrorText(errorText); |
55 | q->emitResult(); |
56 | } |
57 | |
58 | void InitManagerJobPrivate::initFinished() |
59 | { |
60 | q->emitResult(); |
61 | } |
62 | |
63 | InitManagerJob::InitManagerJob(Manager *manager) |
64 | : Job(manager) |
65 | , d(new InitManagerJobPrivate(this, manager)) |
66 | { |
67 | } |
68 | |
69 | InitManagerJob::~InitManagerJob() |
70 | { |
71 | if (isRunning()) { |
72 | qCWarning(BLUEZQT) << "InitManagerJob Error: Job was deleted before finished!" ; |
73 | |
74 | setError(UserDefinedError); |
75 | setErrorText(QStringLiteral("Job was deleted before finished." )); |
76 | emitResult(); |
77 | } |
78 | } |
79 | |
80 | Manager *InitManagerJob::manager() const |
81 | { |
82 | return d->m_manager; |
83 | } |
84 | |
85 | void InitManagerJob::doStart() |
86 | { |
87 | d->doStart(); |
88 | } |
89 | |
90 | void InitManagerJob::doEmitResult() |
91 | { |
92 | Q_EMIT result(job: this); |
93 | } |
94 | |
95 | } // namespace BluezQt |
96 | |
97 | #include "moc_initmanagerjob.cpp" |
98 | |