Basic JobQueue implementation

This commit is contained in:
Aurélien Gâteau 2014-07-08 17:04:39 +02:00
parent 191f37f5fa
commit 04743eb4be
7 changed files with 114 additions and 5 deletions

View File

@ -26,6 +26,7 @@
#include "modulesystem/ModuleManager.h" #include "modulesystem/ModuleManager.h"
#include "utils/CalamaresUtilsGui.h" #include "utils/CalamaresUtilsGui.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "JobQueue.h"
#include "Settings.h" #include "Settings.h"
#include "ViewManager.h" #include "ViewManager.h"
@ -165,5 +166,5 @@ CalamaresApplication::onPluginsReady()
void void
CalamaresApplication::initJobQueue() CalamaresApplication::initJobQueue()
{ {
new Calamares::JobQueue( this );
} }

View File

@ -17,9 +17,55 @@
*/ */
#include "JobQueue.h" #include "JobQueue.h"
#include "Job.h"
#include <QThread>
namespace Calamares namespace Calamares
{ {
class JobThread : public QThread
{
public:
JobThread( JobQueue* queue )
: QThread( queue )
, m_queue( queue )
{
}
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() );
job->exec();
++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 )
);
}
};
JobQueue* JobQueue::s_instance = nullptr; JobQueue* JobQueue::s_instance = nullptr;
@ -33,20 +79,35 @@ JobQueue::instance()
JobQueue::JobQueue( QObject* parent ) JobQueue::JobQueue( QObject* parent )
: QObject( parent ) : QObject( parent )
, m_thread( new JobThread( this ) )
{ {
Q_ASSERT( !s_instance );
s_instance = this;
}
void
JobQueue::start()
{
Q_ASSERT( !m_thread->isRunning() );
m_thread->setJobs( m_jobs );
m_thread->start();
} }
void void
JobQueue::enqueue( const Calamares::job_ptr& job ) JobQueue::enqueue( const Calamares::job_ptr& job )
{ {
Q_ASSERT( !m_thread->isRunning() );
m_jobs.append( job );
} }
void void
JobQueue::enqueue( const QList< job_ptr >& jobs ) JobQueue::enqueue( const QList< job_ptr >& jobs )
{ {
Q_ASSERT( !m_thread->isRunning() );
m_jobs.append( jobs );
} }
} // namespace Calamares } // namespace Calamares

View File

@ -27,6 +27,8 @@
namespace Calamares namespace Calamares
{ {
class JobThread;
class DLLEXPORT JobQueue : public QObject class DLLEXPORT JobQueue : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -37,13 +39,16 @@ public:
void enqueue( const Calamares::job_ptr& job ); void enqueue( const Calamares::job_ptr& job );
void enqueue( const QList< Calamares::job_ptr >& jobs ); void enqueue( const QList< Calamares::job_ptr >& jobs );
void start();
signals: signals:
void jobRunning( const Calamares::job_ptr& ); void progress( int current, int total, const QString& prettyName );
void jobFinished( const Calamares::job_ptr& );
private: private:
static JobQueue* s_instance; static JobQueue* s_instance;
QList< Calamares::job_ptr > m_jobs;
JobThread* m_thread;
}; };
} }

View File

@ -18,15 +18,26 @@
#include <InstallationViewStep.h> #include <InstallationViewStep.h>
#include <JobQueue.h>
#include <QLabel> #include <QLabel>
#include <QProgressBar>
#include <QVBoxLayout>
namespace Calamares namespace Calamares
{ {
InstallationViewStep::InstallationViewStep( QObject* parent ) InstallationViewStep::InstallationViewStep( QObject* parent )
: ViewStep( parent ) : ViewStep( parent )
, m_widget( new QLabel( "[Installation Progress]" ) ) , m_widget( new QWidget )
{ {
m_progressBar = new QProgressBar;
m_label = new QLabel;
QVBoxLayout* layout = new QVBoxLayout( m_widget );
layout->addWidget(m_progressBar);
layout->addWidget(m_label);
connect( JobQueue::instance(), &JobQueue::progress, this, &InstallationViewStep::updateFromJobQueue );
} }
QString QString
@ -76,4 +87,12 @@ InstallationViewStep::jobs() const
return QList< Calamares::job_ptr >(); return QList< Calamares::job_ptr >();
} }
void
InstallationViewStep::updateFromJobQueue( int current, int total, const QString& message )
{
m_progressBar->setMaximum( total );
m_progressBar->setValue( current );
m_label->setText( message );
}
} // namespace } // namespace

View File

@ -21,6 +21,9 @@
#include <viewpages/ViewStep.h> #include <viewpages/ViewStep.h>
class QLabel;
class QProgressBar;
namespace Calamares namespace Calamares
{ {
@ -45,6 +48,10 @@ public:
private: private:
QWidget* m_widget; QWidget* m_widget;
QProgressBar* m_progressBar;
QLabel* m_label;
void updateFromJobQueue( int current, int total, const QString& message );
}; };
} }

View File

@ -20,6 +20,7 @@
#include "viewpages/ViewStep.h" #include "viewpages/ViewStep.h"
#include "InstallationViewStep.h" #include "InstallationViewStep.h"
#include "JobQueue.h"
#include <QApplication> #include <QApplication>
#include <QLabel> #include <QLabel>
@ -152,6 +153,10 @@ ViewManager::next()
m_steps.at( m_currentStep )->onActivate(); m_steps.at( m_currentStep )->onActivate();
installing = m_steps.at( m_currentStep ) == m_installationViewStep; installing = m_steps.at( m_currentStep ) == m_installationViewStep;
emit currentStepChanged(); emit currentStepChanged();
if ( installing )
{
startInstallation();
}
} }
else else
{ {
@ -186,4 +191,14 @@ ViewManager::back()
m_back->setEnabled( false ); m_back->setEnabled( false );
} }
void
ViewManager::startInstallation()
{
for( ViewStep* step : m_prepareSteps )
{
JobQueue::instance()->enqueue( step->jobs() );
}
JobQueue::instance()->start();
}
} }

View File

@ -72,6 +72,7 @@ private:
QPushButton* m_quit; QPushButton* m_quit;
void insertViewStep( int before, ViewStep* step ); void insertViewStep( int before, ViewStep* step );
void startInstallation();
}; };
} }