File indexing completed on 2024-05-19 04:03:21

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 "inhibitionjob.h"
0008 #include "backends/abstractinhibitionjob.h"
0009 #include "inhibitionjob_p.h"
0010 #include "powerbackendloader.h"
0011 
0012 #include "inhibition.h"
0013 
0014 #include <QDebug>
0015 #include <qglobal.h>
0016 
0017 using namespace Solid;
0018 
0019 InhibitionJobPrivate::InhibitionJobPrivate()
0020     : inhibitions(Power::None)
0021     , inhibition(nullptr)
0022     , backendJob(nullptr)
0023 {
0024 }
0025 
0026 InhibitionJob::InhibitionJob(QObject *parent)
0027     : Job(*new InhibitionJobPrivate(), parent)
0028 {
0029 }
0030 
0031 Inhibition *InhibitionJob::inhibition() const
0032 {
0033     Q_ASSERT_X(d_func()->inhibition, "addInhibitionJob", "::inhibition() called before result() has been emitted");
0034 
0035     if (d_func()->inhibition) {
0036         return d_func()->inhibition;
0037     }
0038 
0039     qWarning() << "result() has not been emitted yet, job not finished";
0040     return nullptr;
0041 }
0042 
0043 void InhibitionJob::doStart()
0044 {
0045     Q_D(InhibitionJob);
0046 
0047     if (!d->inhibitions) {
0048         setError(InvalidInhibitions);
0049         emitResult();
0050         return;
0051     }
0052     if (d->description.isEmpty()) {
0053         setError(EmptyDescription);
0054         emitResult();
0055         return;
0056     }
0057 
0058     d->backendJob = PowerBackendLoader::addInhibitionJob(d->inhibitions, d->description);
0059     connect(d->backendJob, &AbstractInhibitionJob::result, [this, d]() {
0060         d_func()->inhibition = d->backendJob->inhibition();
0061         emitResult();
0062     });
0063 
0064     d->backendJob->start();
0065 }
0066 
0067 void InhibitionJob::setInhibitions(Power::InhibitionTypes inhibitions)
0068 {
0069     Q_D(InhibitionJob);
0070     d->inhibitions = inhibitions;
0071 }
0072 
0073 Power::InhibitionTypes InhibitionJob::inhibitions() const
0074 {
0075     return d_func()->inhibitions;
0076 }
0077 
0078 void InhibitionJob::setDescription(const QString &description)
0079 {
0080     Q_D(InhibitionJob);
0081     d->description = description;
0082 }
0083 
0084 QString InhibitionJob::description() const
0085 {
0086     return d_func()->description;
0087 }
0088 
0089 #include "moc_inhibitionjob.cpp"