File indexing completed on 2024-12-01 12:29:48

0001 /*
0002  * BluezQt - Asynchronous Bluez wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "initmanagerjob.h"
0010 #include "debug.h"
0011 #include "manager_p.h"
0012 
0013 namespace BluezQt
0014 {
0015 class InitManagerJobPrivate : public QObject
0016 {
0017 public:
0018     explicit InitManagerJobPrivate(InitManagerJob *q, Manager *manager);
0019 
0020     void doStart();
0021     void initError(const QString &errorText);
0022     void initFinished();
0023 
0024     InitManagerJob *q;
0025     Manager *m_manager;
0026 };
0027 
0028 InitManagerJobPrivate::InitManagerJobPrivate(InitManagerJob *q, Manager *manager)
0029     : QObject(q)
0030     , q(q)
0031     , m_manager(manager)
0032 {
0033 }
0034 
0035 void InitManagerJobPrivate::doStart()
0036 {
0037     if (m_manager->d->m_initialized) {
0038         qCWarning(BLUEZQT) << "InitManagerJob: Manager already initialized!";
0039         q->emitResult();
0040         return;
0041     }
0042 
0043     m_manager->d->init();
0044     connect(m_manager->d, &ManagerPrivate::initError, this, &InitManagerJobPrivate::initError);
0045     connect(m_manager->d, &ManagerPrivate::initFinished, this, &InitManagerJobPrivate::initFinished);
0046 }
0047 
0048 void InitManagerJobPrivate::initError(const QString &errorText)
0049 {
0050     qCWarning(BLUEZQT) << "GetManagerJob Error:" << errorText;
0051 
0052     q->setError(InitManagerJob::UserDefinedError);
0053     q->setErrorText(errorText);
0054     q->emitResult();
0055 }
0056 
0057 void InitManagerJobPrivate::initFinished()
0058 {
0059     q->emitResult();
0060 }
0061 
0062 InitManagerJob::InitManagerJob(Manager *manager)
0063     : Job(manager)
0064     , d(new InitManagerJobPrivate(this, manager))
0065 {
0066 }
0067 
0068 InitManagerJob::~InitManagerJob()
0069 {
0070     if (isRunning()) {
0071         qCWarning(BLUEZQT) << "InitManagerJob Error: Job was deleted before finished!";
0072 
0073         setError(UserDefinedError);
0074         setErrorText(QStringLiteral("Job was deleted before finished."));
0075         emitResult();
0076     }
0077     delete d;
0078 }
0079 
0080 Manager *InitManagerJob::manager() const
0081 {
0082     return d->m_manager;
0083 }
0084 
0085 void InitManagerJob::doStart()
0086 {
0087     d->doStart();
0088 }
0089 
0090 void InitManagerJob::doEmitResult()
0091 {
0092     Q_EMIT result(this);
0093 }
0094 
0095 } // namespace BluezQt
0096 
0097 #include "moc_initmanagerjob.cpp"