Back to index

scribus-ng  1.3.4.dfsg+svn20071115
deferredtask.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "deferredtask.h"
00008 #include "qtimer.h"
00009 
00010 #include "deferredtask.moc"
00011 
00012 enum FileSearchStatus
00013 {
00014        Status_NotStarted,
00015        Status_Running,
00016        Status_Cancelled,
00017        Status_Failed,
00018        Status_Finished,
00019 };
00020 
00021 DeferredTask::DeferredTask(QObject* parent) : QObject(parent)
00022 {
00023        init();
00024 }
00025 
00026 DeferredTask::~DeferredTask()
00027 {
00028        cleanup();
00029 }
00030 
00031 void DeferredTask::init()
00032 {
00033        m_status = Status_NotStarted,
00034        m_lastError = QString::null;
00035        // Build the timer we'll use to access the event loop
00036        m_timer = new QTimer(this, "DeferredTask timer");
00037        Q_CHECK_PTR(m_timer);
00038        // and hook up to it.
00039        connect(m_timer, SIGNAL(timeout()), SLOT(next()) );
00040 }
00041 
00042 void DeferredTask::cleanup()
00043 {
00044        // We're being destroyed while the search is running.
00045        if (m_status == Status_Running)
00046        {
00047               m_status = Status_Failed;
00048               // Report a failure. This error generally indicates a program bug,
00049               // non-translatable because it should never be user visible.
00050               m_lastError = "DeferredTask unexpectedly deleted";
00051               emit aborted(false);
00052        }
00053        // Our timer, if we have one, is automatically destroyed when
00054        // we are so don't worry about it.
00055 }
00056 
00057 bool DeferredTask::isFinished() const
00058 {
00059        return m_status == Status_Finished;
00060 }
00061 
00062 const QString& DeferredTask::lastError() const
00063 {
00064        Q_ASSERT(m_status == Status_Cancelled || m_status == Status_Failed || m_status == Status_Finished);
00065        Q_ASSERT(!m_lastError.isNull());
00066        return m_lastError;
00067 }
00068 
00069 void DeferredTask::start()
00070 {
00071        m_status = Status_Running;
00072        // Start the timer to do our search in the event loop's idle time
00073        m_timer->start(0, false);
00074 }
00075 
00076 void DeferredTask::cancel()
00077 {
00078        Q_ASSERT(m_status == Status_Running);
00079        m_status = Status_Cancelled;
00080        m_timer->stop();
00081        // Cancelled by request
00082        m_lastError = tr("Cancelled by user");
00083        emit aborted(true);
00084 }
00085 
00086 void DeferredTask::done()
00087 {
00088        Q_ASSERT(m_status == Status_Running);
00089        m_status = Status_Finished;
00090        m_timer->stop();
00091        emit finished();
00092 }
00093 
00094 void DeferredTask::runUntilFinished()
00095 {
00096        Q_ASSERT(m_status == Status_Running);
00097        while (m_status == Status_Running)
00098               next();
00099 }