1 /* 2 For general Scribus (>=1.3.2) copyright and licensing information please refer 3 to the COPYING file provided with the program. Following this notice may exist 4 a copyright and/or license notice that predates the release of Scribus 1.3.2 5 for which a new license (GPL+exception) is in place. 6 */ 7 8 #include <QTimer> 9 #include "deferredtask.h" 10 DeferredTask(QObject * parent)11DeferredTask::DeferredTask(QObject* parent) : QObject(parent) 12 { 13 init(); 14 } 15 ~DeferredTask()16DeferredTask::~DeferredTask() 17 { 18 cleanup(); 19 } 20 init()21void DeferredTask::init() 22 { 23 m_status = Status_NotStarted, 24 m_lastError.clear(); 25 // Build the timer we'll use to access the event loop 26 m_timer = new QTimer(this); 27 Q_CHECK_PTR(m_timer); 28 // and hook up to it. 29 connect(m_timer, SIGNAL(timeout()), SLOT(next()) ); 30 } 31 cleanup()32void DeferredTask::cleanup() 33 { 34 // We're being destroyed while the search is running. 35 if (m_status == Status_Running) 36 { 37 m_status = Status_Failed; 38 // Report a failure. This error generally indicates a program bug, 39 // non-translatable because it should never be user visible. 40 m_lastError = "DeferredTask unexpectedly deleted"; 41 emit aborted(false); 42 } 43 // Our timer, if we have one, is automatically destroyed when 44 // we are so don't worry about it. 45 } 46 isFinished() const47bool DeferredTask::isFinished() const 48 { 49 return m_status == Status_Finished; 50 } 51 lastError() const52const QString& DeferredTask::lastError() const 53 { 54 Q_ASSERT(m_status == Status_Cancelled || m_status == Status_Failed || m_status == Status_Finished); 55 Q_ASSERT(!m_lastError.isNull()); 56 return m_lastError; 57 } 58 start()59void DeferredTask::start() 60 { 61 m_status = Status_Running; 62 // Start the timer to do our search in the event loop's idle time 63 m_timer->setSingleShot(false); 64 m_timer->start(0); 65 } 66 cancel()67void DeferredTask::cancel() 68 { 69 Q_ASSERT(m_status == Status_Running); 70 m_status = Status_Cancelled; 71 m_timer->stop(); 72 // Cancelled by request 73 m_lastError = tr("Cancelled by user"); 74 emit aborted(true); 75 } 76 done()77void DeferredTask::done() 78 { 79 Q_ASSERT(m_status == Status_Running); 80 m_status = Status_Finished; 81 m_timer->stop(); 82 emit finished(); 83 } 84 runUntilFinished()85void DeferredTask::runUntilFinished() 86 { 87 Q_ASSERT(m_status == Status_Running); 88 while (m_status == Status_Running) 89 next(); 90 } 91