BluezQt

job.cpp
1 /*
2  * BluezQt - Asynchronous Bluez wrapper library
3  *
4  * SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <[email protected]>
5  * SPDX-FileCopyrightText: 2014 David Rosca <[email protected]>
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 
15 namespace BluezQt
16 {
17 JobPrivate::JobPrivate()
18 {
19  eventLoop = nullptr;
21  running = false;
22  finished = false;
23  killed = false;
24 }
25 
26 Job::Job(QObject *parent)
27  : QObject(parent)
28  , d_ptr(new JobPrivate)
29 {
30  d_ptr->q_ptr = this;
31 }
32 
34 {
35  delete d_ptr;
36 }
37 
38 void Job::start()
39 {
40  d_func()->running = true;
42 }
43 
44 void Job::kill()
45 {
46  Q_D(Job);
47  Q_ASSERT(!d->eventLoop);
48 
49  d->running = false;
50  d->finished = true;
51  d->killed = true;
52  deleteLater();
53 }
54 
56 {
57  Q_D(Job);
58 
59  if (d->killed) {
60  return;
61  }
62 
63  if (d->eventLoop) {
64  d->eventLoop->quit();
65  }
66 
67  d->running = false;
68  d->finished = true;
69  doEmitResult();
70  deleteLater();
71 }
72 
73 int Job::error() const
74 {
75  return d_func()->error;
76 }
77 
78 QString Job::errorText() const
79 {
80  return d_func()->errorText;
81 }
82 
83 bool Job::isRunning() const
84 {
85  return d_func()->running;
86 }
87 
88 bool Job::isFinished() const
89 {
90  return d_func()->finished;
91 }
92 
93 void Job::setError(int errorCode)
94 {
95  d_func()->error = errorCode;
96 }
97 
98 void Job::setErrorText(const QString &errorText)
99 {
100  d_func()->errorText = errorText;
101 }
102 
103 bool Job::exec()
104 {
105  Q_D(Job);
106 
107  Q_ASSERT(!d->eventLoop);
108 
109  QEventLoop loop(this);
110  d->eventLoop = &loop;
111 
112  start();
113  d->eventLoop->exec(QEventLoop::ExcludeUserInputEvents);
114  d->running = false;
115  d->finished = true;
116 
117  return d->error == NoError;
118 }
119 
120 } // namespace BluezQt
bool isRunning() const
Returns whether the job is currently running.
Definition: job.cpp:83
~Job() override
Destroys a Job object.
Definition: job.cpp:33
void setErrorText(const QString &errorText)
Sets the error text.
Definition: job.cpp:98
bool isFinished() const
Returns whether the job have already finished.
Definition: job.cpp:88
void deleteLater()
void kill()
Kills the job.
Definition: job.cpp:44
virtual void doEmitResult()=0
Implementation for emitting the result signal.
QueuedConnection
bool exec()
Executes the job synchronously.
Definition: job.cpp:103
void setError(int errorCode)
Sets the error code.
Definition: job.cpp:93
@ NoError
Indicates there is no error.
Definition: job.h:72
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
void start()
Starts the job asynchronously.
Definition: job.cpp:38
void emitResult()
Utility function to emit the result signal, and suicide this job.
Definition: job.cpp:55
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
Job(QObject *parent=nullptr)
Creates a new Job object.
Definition: job.cpp:26
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Sep 25 2022 04:19:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.