1/*
2 * BluezQt - Asynchronous Bluez wrapper library
3 *
4 * SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
5 * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com>
6 *
7 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 */
9
10#include "job.h"
11#include "job_p.h"
12
13#include <QEventLoop>
14
15namespace BluezQt
16{
17JobPrivate::JobPrivate()
18{
19 eventLoop = nullptr;
20 error = Job::NoError;
21 running = false;
22 finished = false;
23 killed = false;
24}
25
26Job::Job(QObject *parent)
27 : QObject(parent)
28 , d_ptr(new JobPrivate)
29{
30 d_ptr->q_ptr = this;
31}
32
33Job::~Job() = default;
34
35void Job::start()
36{
37 d_func()->running = true;
38 QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection);
39}
40
41void Job::kill()
42{
43 Q_D(Job);
44 Q_ASSERT(!d->eventLoop);
45
46 d->running = false;
47 d->finished = true;
48 d->killed = true;
49 deleteLater();
50}
51
52void Job::emitResult()
53{
54 Q_D(Job);
55
56 if (d->killed) {
57 return;
58 }
59
60 if (d->eventLoop) {
61 d->eventLoop->quit();
62 }
63
64 d->running = false;
65 d->finished = true;
66 doEmitResult();
67 deleteLater();
68}
69
70int Job::error() const
71{
72 return d_func()->error;
73}
74
75QString Job::errorText() const
76{
77 return d_func()->errorText;
78}
79
80bool Job::isRunning() const
81{
82 return d_func()->running;
83}
84
85bool Job::isFinished() const
86{
87 return d_func()->finished;
88}
89
90void Job::setError(int errorCode)
91{
92 d_func()->error = errorCode;
93}
94
95void Job::setErrorText(const QString &errorText)
96{
97 d_func()->errorText = errorText;
98}
99
100bool Job::exec()
101{
102 Q_D(Job);
103
104 Q_ASSERT(!d->eventLoop);
105
106 QEventLoop loop(this);
107 d->eventLoop = &loop;
108
109 start();
110 d->eventLoop->exec(QEventLoop::ExcludeUserInputEvents);
111 d->running = false;
112 d->finished = true;
113
114 return d->error == NoError;
115}
116
117} // namespace BluezQt
118
119#include "moc_job.cpp"
120

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