| 1 | /* |
|---|---|
| 2 | SPDX-FileCopyrightText: 2020 Nicolas Fella <nicolas.fella@gmx.de> |
| 3 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 | */ |
| 5 | |
| 6 | #include "jobcontroller.h" |
| 7 | |
| 8 | #include "configuration.h" |
| 9 | |
| 10 | namespace Purpose |
| 11 | { |
| 12 | void JobController::configure() |
| 13 | { |
| 14 | Q_ASSERT(m_model); |
| 15 | Q_ASSERT(m_index >= 0); |
| 16 | |
| 17 | m_configuration = m_model->configureJob(row: m_index); |
| 18 | m_configuration->setUseSeparateProcess(false); |
| 19 | Q_EMIT configChanged(); |
| 20 | |
| 21 | if (m_configuration->isReady()) { |
| 22 | startJob(); |
| 23 | } else { |
| 24 | m_state = Configuring; |
| 25 | Q_EMIT stateChanged(); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | void JobController::startJob() |
| 30 | { |
| 31 | m_job = m_configuration->createJob(); |
| 32 | Q_ASSERT(m_job); |
| 33 | Q_EMIT jobChanged(); |
| 34 | |
| 35 | connect(sender: m_job, signal: &KJob::result, context: this, slot: [this](KJob *job) { |
| 36 | if (job->error()) { |
| 37 | m_state = Error; |
| 38 | } else { |
| 39 | m_state = Finished; |
| 40 | } |
| 41 | |
| 42 | Q_EMIT stateChanged(); |
| 43 | }); |
| 44 | |
| 45 | m_job->start(); |
| 46 | |
| 47 | m_state = Running; |
| 48 | Q_EMIT stateChanged(); |
| 49 | } |
| 50 | |
| 51 | void JobController::cancel() |
| 52 | { |
| 53 | m_state = Cancelled; |
| 54 | Q_EMIT stateChanged(); |
| 55 | } |
| 56 | |
| 57 | AlternativesModel *JobController::model() const |
| 58 | { |
| 59 | return m_model; |
| 60 | } |
| 61 | |
| 62 | void JobController::setModel(AlternativesModel *model) |
| 63 | { |
| 64 | if (m_model != model) { |
| 65 | m_model = model; |
| 66 | Q_EMIT modelChanged(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | int JobController::index() const |
| 71 | { |
| 72 | return m_index; |
| 73 | } |
| 74 | |
| 75 | void JobController::setIndex(int index) |
| 76 | { |
| 77 | if (index != m_index) { |
| 78 | m_index = index; |
| 79 | Q_EMIT indexChanged(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | JobController::State JobController::state() const |
| 84 | { |
| 85 | return m_state; |
| 86 | } |
| 87 | |
| 88 | Configuration *JobController::config() const |
| 89 | { |
| 90 | return m_configuration; |
| 91 | } |
| 92 | |
| 93 | Job *JobController::job() const |
| 94 | { |
| 95 | return m_job; |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | #include "moc_jobcontroller.cpp" |
| 101 |
