File indexing completed on 2024-04-21 04:49:10

0001 /*
0002     SPDX-FileCopyrightText: 1998-2010 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 
0007 #ifndef _K3B_THREAD_JOB_H_
0008 #define _K3B_THREAD_JOB_H_
0009 
0010 #include "k3bjob.h"
0011 #include "k3b_export.h"
0012 #include <climits>
0013 
0014 
0015 namespace K3b {
0016 
0017     class Thread;
0018 
0019     /**
0020      * A Job that runs in a different thread. Instead of reimplementing
0021      * start() reimplement run() to perform all operations in a different
0022      * thread. Otherwise usage is the same as Job.
0023      */
0024     class LIBK3B_EXPORT ThreadJob : public Job
0025     {
0026         Q_OBJECT
0027 
0028     public:
0029         explicit ThreadJob( JobHandler*, QObject* parent = 0 );
0030         ~ThreadJob() override;
0031 
0032         /**
0033          * \reimplemented from Job
0034          *
0035          * \return true if the job has been started and has not yet
0036          * emitted the finished signal
0037          */
0038         bool active() const override;
0039 
0040         /**
0041          * reimplemented from JobHandler
0042          */
0043         Device::MediaType waitForMedium( Device::Device*,
0044                                                  Device::MediaStates mediaState = Device::STATE_EMPTY,
0045                                                  Device::MediaTypes mediaType = Device::MEDIA_WRITABLE_CD,
0046                                                  const K3b::Msf& minMediaSize = K3b::Msf(),
0047                                                  const QString& message = QString() ) override;
0048 
0049         /**
0050          * reimplemented from JobHandler
0051          */
0052         bool questionYesNo( const QString& text,
0053                                     const QString& caption = QString(),
0054                                     const KGuiItem& buttonYes = KStandardGuiItem::ok(),
0055                                     const KGuiItem& buttonNo = KStandardGuiItem::cancel() ) override;
0056 
0057         /**
0058          * reimplemented from JobHandler
0059          */
0060         void blockingInformation( const QString& text,
0061                                           const QString& caption = QString() ) override;
0062 
0063 
0064         /**
0065          * Call QThread::wait() on job's thread
0066          * \see QThread::wait()
0067          */
0068         bool wait( unsigned long time = ULONG_MAX );
0069 
0070     public Q_SLOTS:
0071         /**
0072          * Starts the job in a different thread. Emits the started()
0073          * signal.
0074          *
0075          * When reimplementing this method to perform housekeeping
0076          * operations in the GUI thread make sure to call the
0077          * parent implementation.
0078          *
0079          * \sa run()
0080          */
0081         void start() override;
0082 
0083         /**
0084          * Cancel the job. The method will give the thread a certain
0085          * time to actually cancel. After that the thread is terminated.
0086          *
0087          * \sa canceled()
0088          */
0089         void cancel() override;
0090 
0091     protected:
0092         /**
0093          * Implement this method to do the actual work in the thread.
0094          * Do not emit started(), finished(), and canceled() signals
0095          * in this method. ThreadJob will do that automatically.
0096          *
0097          * \return \p true on success.
0098          */
0099         virtual bool run() = 0;
0100 
0101         /**
0102          * Use to check if the job has been canceled.
0103          * \sa cancel()
0104          */
0105         bool canceled() const;
0106 
0107     private Q_SLOTS:
0108         /**
0109          * Called in the GUi thread once the job is done.
0110          * Emits the finished signal and performs some
0111          * housekeeping.
0112          */
0113         void slotThreadFinished();
0114 
0115     private:
0116         void customEvent( QEvent* ) override;
0117 
0118         class Private;
0119         Private* const d;
0120 
0121         friend class Thread;
0122     };
0123 }
0124 
0125 #endif
0126