calamares/src/libcalamares/JobQueue.cpp

136 lines
3.0 KiB
C++
Raw Normal View History

2014-06-27 15:34:05 +02:00
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calamares is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include "JobQueue.h"
2014-07-08 17:04:39 +02:00
#include "Job.h"
#include "CalamaresConfig.h"
#ifdef WITH_PYTHON
#include "PythonHelper.h"
#endif
2014-07-08 17:04:39 +02:00
#include <QThread>
2014-06-27 15:34:05 +02:00
namespace Calamares
{
2014-07-08 17:04:39 +02:00
class JobThread : public QThread
{
public:
JobThread( JobQueue* queue )
: QThread( queue )
, m_queue( queue )
{
#ifdef WITH_PYTHON
2014-07-18 11:59:12 +02:00
new CalamaresPython::Helper( this );
#endif
2014-07-08 17:04:39 +02:00
}
void setJobs( const QList< Calamares::job_ptr >& jobs )
{
m_jobs = jobs;
}
void run() override
{
int total = m_jobs.size();
int current = 0;
for( auto job : m_jobs )
{
emitProgress( current, total, job->prettyName() );
2014-07-10 14:46:08 +02:00
JobResult result = job->exec();
if ( !result )
{
emitFailed( result.message(), result.details() );
return;
}
2014-07-08 17:04:39 +02:00
++current;
}
emitProgress( total, total, QString() );
}
private:
QList< Calamares::job_ptr > m_jobs;
JobQueue* m_queue;
void emitProgress( int current, int total, const QString& prettyName )
{
QMetaObject::invokeMethod( m_queue, "progress", Qt::QueuedConnection,
Q_ARG( int, current ),
Q_ARG( int, total ),
Q_ARG( QString, prettyName )
);
}
2014-07-10 14:46:08 +02:00
void emitFailed( const QString& message, const QString& details )
{
QMetaObject::invokeMethod( m_queue, "failed", Qt::QueuedConnection,
Q_ARG( QString, message ),
Q_ARG( QString, details )
);
}
2014-07-08 17:04:39 +02:00
};
JobQueue* JobQueue::s_instance = nullptr;
JobQueue*
JobQueue::instance()
{
return s_instance;
}
2014-06-27 15:34:05 +02:00
JobQueue::JobQueue( QObject* parent )
: QObject( parent )
2014-07-08 17:04:39 +02:00
, m_thread( new JobThread( this ) )
2014-06-27 15:34:05 +02:00
{
2014-07-08 17:04:39 +02:00
Q_ASSERT( !s_instance );
s_instance = this;
}
void
JobQueue::start()
{
Q_ASSERT( !m_thread->isRunning() );
m_thread->setJobs( m_jobs );
m_jobs.clear();
2014-07-08 17:04:39 +02:00
m_thread->start();
2014-06-27 15:34:05 +02:00
}
void
JobQueue::enqueue( const Calamares::job_ptr& job )
{
2014-07-08 17:04:39 +02:00
Q_ASSERT( !m_thread->isRunning() );
m_jobs.append( job );
}
void
JobQueue::enqueue( const QList< job_ptr >& jobs )
{
2014-07-08 17:04:39 +02:00
Q_ASSERT( !m_thread->isRunning() );
m_jobs.append( jobs );
}
2014-06-27 15:34:05 +02:00
} // namespace Calamares