File indexing completed on 2024-05-12 17:08:33

0001 /*
0002  *   SPDX-FileCopyrightText: 2016 Ivan Cukic <ivan.cukic(at)kde.org>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 //
0008 // W A R N I N G
0009 // -------------
0010 //
0011 // This file is not part of the AsynQt API. It exists purely as an
0012 // implementation detail. This header file may change from version to
0013 // version without notice, or even be removed.
0014 //
0015 // We mean it.
0016 //
0017 
0018 namespace AsynQt
0019 {
0020 namespace detail
0021 {
0022 template<typename _Result>
0023 class KJobFutureInterface : public QObject, public QFutureInterface<_Result>
0024 {
0025 public:
0026     KJobFutureInterface(KJob *job)
0027         : job(job)
0028     {
0029         job->setAutoDelete(false);
0030     }
0031 
0032     ~KJobFutureInterface() override
0033     {
0034     }
0035 
0036     void callFinished();
0037 
0038     QFuture<_Result> start()
0039     {
0040         auto onCallFinished = [this]() {
0041             callFinished();
0042         };
0043         QObject::connect(job, &KJob::result, this, onCallFinished, Qt::QueuedConnection);
0044 
0045         this->reportStarted();
0046 
0047         job->start();
0048 
0049         return this->future();
0050     }
0051 
0052 private:
0053     KJob *job;
0054 };
0055 
0056 template<typename _Result>
0057 void KJobFutureInterface<_Result>::callFinished()
0058 {
0059     this->reportResult(job);
0060     this->reportFinished();
0061 
0062     deleteLater();
0063 }
0064 
0065 template<>
0066 void KJobFutureInterface<void>::callFinished()
0067 {
0068     this->reportFinished();
0069 
0070     job->deleteLater();
0071     deleteLater();
0072 }
0073 
0074 } // namespace detail
0075 } // namespace AsynQt