File indexing completed on 2025-01-19 06:44:36
0001 /* 0002 * BluezQt - Asynchronous Bluez wrapper library 0003 * 0004 * SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org> 0005 * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com> 0006 * 0007 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0008 */ 0009 0010 #include "job.h" 0011 #include "job_p.h" 0012 0013 #include <QEventLoop> 0014 0015 namespace BluezQt 0016 { 0017 JobPrivate::JobPrivate() 0018 { 0019 eventLoop = nullptr; 0020 error = Job::NoError; 0021 running = false; 0022 finished = false; 0023 killed = false; 0024 } 0025 0026 Job::Job(QObject *parent) 0027 : QObject(parent) 0028 , d_ptr(new JobPrivate) 0029 { 0030 d_ptr->q_ptr = this; 0031 } 0032 0033 Job::~Job() = default; 0034 0035 void Job::start() 0036 { 0037 d_func()->running = true; 0038 QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection); 0039 } 0040 0041 void Job::kill() 0042 { 0043 Q_D(Job); 0044 Q_ASSERT(!d->eventLoop); 0045 0046 d->running = false; 0047 d->finished = true; 0048 d->killed = true; 0049 deleteLater(); 0050 } 0051 0052 void Job::emitResult() 0053 { 0054 Q_D(Job); 0055 0056 if (d->killed) { 0057 return; 0058 } 0059 0060 if (d->eventLoop) { 0061 d->eventLoop->quit(); 0062 } 0063 0064 d->running = false; 0065 d->finished = true; 0066 doEmitResult(); 0067 deleteLater(); 0068 } 0069 0070 int Job::error() const 0071 { 0072 return d_func()->error; 0073 } 0074 0075 QString Job::errorText() const 0076 { 0077 return d_func()->errorText; 0078 } 0079 0080 bool Job::isRunning() const 0081 { 0082 return d_func()->running; 0083 } 0084 0085 bool Job::isFinished() const 0086 { 0087 return d_func()->finished; 0088 } 0089 0090 void Job::setError(int errorCode) 0091 { 0092 d_func()->error = errorCode; 0093 } 0094 0095 void Job::setErrorText(const QString &errorText) 0096 { 0097 d_func()->errorText = errorText; 0098 } 0099 0100 bool Job::exec() 0101 { 0102 Q_D(Job); 0103 0104 Q_ASSERT(!d->eventLoop); 0105 0106 QEventLoop loop(this); 0107 d->eventLoop = &loop; 0108 0109 start(); 0110 d->eventLoop->exec(QEventLoop::ExcludeUserInputEvents); 0111 d->running = false; 0112 d->finished = true; 0113 0114 return d->error == NoError; 0115 } 0116 0117 } // namespace BluezQt 0118 0119 #include "moc_job.cpp"