File indexing completed on 2024-06-16 10:11:46

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "job.h"
0008 #include "job_p.h"
0009 
0010 #include <QEventLoop>
0011 
0012 using namespace Solid;
0013 JobPrivate::JobPrivate()
0014 {
0015     eventLoop = nullptr;
0016     error = Job::NoError;
0017 }
0018 
0019 Job::Job(QObject *parent)
0020     : QObject(parent)
0021     , d_ptr(new JobPrivate)
0022 {
0023     d_ptr->q_ptr = this;
0024 }
0025 
0026 Job::~Job()
0027 {
0028     delete d_ptr;
0029 }
0030 
0031 void Job::start()
0032 {
0033     QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection);
0034 }
0035 
0036 void Job::emitResult()
0037 {
0038     Q_D(Job);
0039     if (d->eventLoop) {
0040         d->eventLoop->quit();
0041     }
0042 
0043     Q_EMIT result(this);
0044     deleteLater();
0045 }
0046 
0047 int Job::error() const
0048 {
0049     return d_func()->error;
0050 }
0051 
0052 QString Job::errorText() const
0053 {
0054     return d_func()->errorText;
0055 }
0056 
0057 void Job::setError(int errorCode)
0058 {
0059     d_func()->error = errorCode;
0060 }
0061 
0062 void Job::setErrorText(const QString &errorText)
0063 {
0064     d_func()->errorText = errorText;
0065 }
0066 
0067 bool Job::exec()
0068 {
0069     Q_D(Job);
0070 
0071     Q_ASSERT(!d->eventLoop);
0072 
0073     QEventLoop loop(this);
0074     d->eventLoop = &loop;
0075 
0076     start();
0077     d->eventLoop->exec(QEventLoop::ExcludeUserInputEvents);
0078 
0079     return (d->error == NoError);
0080 }
0081 
0082 Job::Job(JobPrivate &dd, QObject *parent)
0083     : QObject(parent)
0084     , d_ptr(&dd)
0085 {
0086 }
0087 
0088 #include "moc_job.cpp"