source: calamares/trunk/fuentes/src/libcalamares/JobQueue.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 3.8 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "JobQueue.h"
20
21#include "Job.h"
22#include "GlobalStorage.h"
23#include "utils/Logger.h"
24
25#include "CalamaresConfig.h"
26#ifdef WITH_PYTHON
27#include "PythonHelper.h"
28#endif
29
30#include <QThread>
31
32namespace Calamares
33{
34
35class JobThread : public QThread
36{
37    Q_OBJECT
38public:
39    JobThread( JobQueue* queue )
40        : QThread( queue )
41        , m_queue( queue )
42        , m_jobIndex( 0 )
43    {
44    }
45
46    void setJobs( const JobList& jobs )
47    {
48        m_jobs = jobs;
49    }
50
51    void run() override
52    {
53        m_jobIndex = 0;
54        for( auto job : m_jobs )
55        {
56            emitProgress();
57            cDebug() << "Starting job" << job->prettyName();
58            connect( job.data(), &Job::progress, this, &JobThread::emitProgress );
59            JobResult result = job->exec();
60            if ( !result )
61            {
62                emitFailed( result.message(), result.details() );
63                emitFinished();
64                return;
65            }
66            ++m_jobIndex;
67        }
68        emitProgress();
69        emitFinished();
70    }
71
72private:
73    JobList m_jobs;
74    JobQueue* m_queue;
75    int m_jobIndex;
76
77    void emitProgress( qreal jobPercent = 0 )
78    {
79        // Make sure jobPercent is reasonable, in case a job messed up its
80        // percentage computations.
81        jobPercent = qBound( qreal( 0 ), jobPercent, qreal( 1 ) );
82
83        int jobCount = m_jobs.size();
84        QString message = m_jobIndex < jobCount
85            ? m_jobs.at( m_jobIndex )->prettyStatusMessage()
86            : tr( "Done" );
87
88        qreal percent = ( m_jobIndex + jobPercent ) / qreal( jobCount );
89
90        QMetaObject::invokeMethod( m_queue, "progress", Qt::QueuedConnection,
91            Q_ARG( qreal, percent ),
92            Q_ARG( QString, message )
93        );
94    }
95
96    void emitFailed( const QString& message, const QString& details )
97    {
98        QMetaObject::invokeMethod( m_queue, "failed", Qt::QueuedConnection,
99            Q_ARG( QString, message ),
100            Q_ARG( QString, details )
101        );
102    }
103
104    void emitFinished()
105    {
106        QMetaObject::invokeMethod( m_queue, "finished", Qt::QueuedConnection );
107    }
108};
109
110
111JobQueue* JobQueue::s_instance = nullptr;
112
113
114JobQueue*
115JobQueue::instance()
116{
117    return s_instance;
118}
119
120
121GlobalStorage*
122JobQueue::globalStorage() const
123{
124    return m_storage;
125}
126
127
128JobQueue::JobQueue( QObject* parent )
129    : QObject( parent )
130    , m_thread( new JobThread( this ) )
131    , m_storage( new GlobalStorage() )
132{
133    Q_ASSERT( !s_instance );
134    s_instance = this;
135}
136
137
138JobQueue::~JobQueue()
139{
140    delete m_storage;
141}
142
143
144void
145JobQueue::start()
146{
147    Q_ASSERT( !m_thread->isRunning() );
148    m_thread->setJobs( m_jobs );
149    m_jobs.clear();
150    m_thread->start();
151}
152
153
154void
155JobQueue::enqueue( const job_ptr& job )
156{
157    Q_ASSERT( !m_thread->isRunning() );
158    m_jobs.append( job );
159    emit queueChanged( m_jobs );
160}
161
162
163void
164JobQueue::enqueue( const JobList& jobs )
165{
166    Q_ASSERT( !m_thread->isRunning() );
167    m_jobs.append( jobs );
168    emit queueChanged( m_jobs );
169}
170
171} // namespace Calamares
172
173#include "JobQueue.moc"
Note: See TracBrowser for help on using the repository browser.