File indexing completed on 2024-04-28 04:49:23
0001 /* 0002 SPDX-FileCopyrightText: 1998-2010 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "k3bthreadjob.h" 0007 #include "k3bthread.h" 0008 #include "k3bprogressinfoevent.h" 0009 #include "k3bthreadjobcommunicationevent.h" 0010 0011 #include <QCoreApplication> 0012 #include <QDebug> 0013 #include <QSharedPointer> 0014 #include <QThread> 0015 0016 0017 class K3b::ThreadJob::Private 0018 { 0019 public: 0020 Private() 0021 : thread( 0 ), 0022 running( false ), 0023 canceled( false ) { 0024 } 0025 K3b::Thread* thread; 0026 bool running; 0027 bool canceled; 0028 }; 0029 0030 0031 K3b::ThreadJob::ThreadJob( K3b::JobHandler* jh, QObject* parent ) 0032 : K3b::Job( jh, parent ), 0033 d( new Private ) 0034 { 0035 d->thread = new K3b::Thread( this ); 0036 connect( d->thread, SIGNAL(finished()), 0037 this, SLOT(slotThreadFinished()) ); 0038 } 0039 0040 0041 K3b::ThreadJob::~ThreadJob() 0042 { 0043 delete d; 0044 } 0045 0046 0047 bool K3b::ThreadJob::active() const 0048 { 0049 return d->running; 0050 } 0051 0052 0053 void K3b::ThreadJob::start() 0054 { 0055 if( !d->running ) { 0056 d->canceled = false; 0057 d->running = true; 0058 jobStarted(); 0059 d->thread->start(); 0060 } 0061 else { 0062 qDebug() << "(K3b::ThreadJob) thread not finished yet."; 0063 } 0064 } 0065 0066 0067 void K3b::ThreadJob::slotThreadFinished() 0068 { 0069 d->running = false; 0070 if( canceled() ) 0071 emit canceled(); 0072 jobFinished( d->thread->success() ); 0073 } 0074 0075 0076 void K3b::ThreadJob::cancel() 0077 { 0078 d->canceled = true; 0079 d->thread->ensureDone(); 0080 } 0081 0082 0083 bool K3b::ThreadJob::canceled() const 0084 { 0085 return d->canceled; 0086 } 0087 0088 0089 K3b::Device::MediaType K3b::ThreadJob::waitForMedium( K3b::Device::Device* device, 0090 Device::MediaStates mediaState, 0091 Device::MediaTypes mediaType, 0092 const K3b::Msf& minMediaSize, 0093 const QString& message ) 0094 { 0095 K3b::ThreadJobCommunicationEvent* event = K3b::ThreadJobCommunicationEvent::waitForMedium( device, 0096 mediaState, 0097 mediaType, 0098 minMediaSize, 0099 message ); 0100 QSharedPointer<K3b::ThreadJobCommunicationEvent::Data> data( event->data() ); 0101 QCoreApplication::postEvent( this, event ); 0102 data->wait(); 0103 return (Device::MediaType)data->intResult(); 0104 } 0105 0106 0107 bool K3b::ThreadJob::questionYesNo( const QString& text, 0108 const QString& caption, 0109 const KGuiItem& buttonYes, 0110 const KGuiItem& buttonNo ) 0111 { 0112 K3b::ThreadJobCommunicationEvent* event = K3b::ThreadJobCommunicationEvent::questionYesNo( text, 0113 caption, 0114 buttonYes, 0115 buttonNo ); 0116 QSharedPointer<K3b::ThreadJobCommunicationEvent::Data> data( event->data() ); 0117 QCoreApplication::postEvent( this, event ); 0118 data->wait(); 0119 return data->boolResult(); 0120 } 0121 0122 0123 void K3b::ThreadJob::blockingInformation( const QString& text, 0124 const QString& caption ) 0125 { 0126 K3b::ThreadJobCommunicationEvent* event = K3b::ThreadJobCommunicationEvent::blockingInformation( text, 0127 caption ); 0128 QSharedPointer<K3b::ThreadJobCommunicationEvent::Data> data( event->data() ); 0129 QCoreApplication::postEvent( this, event ); 0130 data->wait(); 0131 } 0132 0133 0134 void K3b::ThreadJob::customEvent( QEvent* e ) 0135 { 0136 if( K3b::ThreadJobCommunicationEvent* ce = dynamic_cast<K3b::ThreadJobCommunicationEvent*>(e) ) { 0137 K3b::ThreadJobCommunicationEvent::Data* data = ce->data(); 0138 int result = 0; 0139 switch( ce->type() ) { 0140 case K3b::ThreadJobCommunicationEvent::WaitForMedium: 0141 result = K3b::Job::waitForMedium( data->device(), 0142 data->wantedMediaState(), 0143 data->wantedMediaType(), 0144 data->wantedMediaSize(), 0145 data->text() ); 0146 break; 0147 0148 case K3b::ThreadJobCommunicationEvent::QuestionYesNo: 0149 result = K3b::Job::questionYesNo( data->text(), 0150 data->caption(), 0151 data->buttonYes(), 0152 data->buttonNo() ) 0153 ? 1 : 0; 0154 break; 0155 0156 case K3b::ThreadJobCommunicationEvent::BlockingInfo: 0157 K3b::Job::blockingInformation( data->text(), data->caption() ); 0158 break; 0159 } 0160 data->done( result ); 0161 } 0162 } 0163 0164 0165 bool K3b::ThreadJob::wait( unsigned long time ) 0166 { 0167 return d->thread->wait( time ); 0168 } 0169 0170 #include "moc_k3bthreadjob.cpp"