BluezQt

job.cpp
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;
21 running = false;
22 finished = false;
23 killed = false;
24}
25
27 : QObject(parent)
28 , d_ptr(new JobPrivate)
29{
30 d_ptr->q_ptr = this;
31}
32
33Job::~Job() = default;
34
36{
37 d_func()->running = true;
39}
40
42{
43 Q_D(Job);
44 Q_ASSERT(!d->eventLoop);
45
46 d->running = false;
47 d->finished = true;
48 d->killed = true;
50}
51
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;
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
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"
This class represents an asynchronous job performed by BluezQt, it is usually not used directly but i...
Job(QObject *parent=nullptr)
Creates a new Job object.
Definition job.cpp:26
void setError(int errorCode)
Sets the error code.
Definition job.cpp:90
void kill()
Kills the job.
Definition job.cpp:41
virtual void doEmitResult()=0
Implementation for emitting the result signal.
void emitResult()
Utility function to emit the result signal, and remove this job.
Definition job.cpp:52
bool isFinished() const
Returns whether the job have already finished.
Definition job.cpp:85
~Job() override
Destroys a Job object.
bool exec()
Executes the job synchronously.
Definition job.cpp:100
bool isRunning() const
Returns whether the job is currently running.
Definition job.cpp:80
void setErrorText(const QString &errorText)
Sets the error text.
Definition job.cpp:95
void start()
Starts the job asynchronously.
Definition job.cpp:35
@ NoError
Indicates there is no error.
Definition job.h:74
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
void deleteLater()
QueuedConnection
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:46:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.