1 /*
2  * SPDX-FileCopyrightText: 2018-2018 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #include "pipeline.h"
8 
9 namespace fcitx {
10 
Pipeline(QObject * parent)11 Pipeline::Pipeline(QObject *parent) : QObject(parent) {}
12 
addJob(PipelineJob * job)13 void Pipeline::addJob(PipelineJob *job) {
14     job->setParent(this);
15     jobs_.push_back(job);
16     connect(job, &PipelineJob::finished, this, [this](bool success) {
17         if (success) {
18             startNext();
19         } else {
20             emitFinished(false);
21         }
22     });
23 }
24 
abort()25 void Pipeline::abort() {
26     if (index_ < 0) {
27         return;
28     }
29     jobs_[index_]->abort();
30     index_ = -1;
31 }
32 
reset()33 void Pipeline::reset() {
34     abort();
35     for (auto *job : jobs_) {
36         delete job;
37     }
38     jobs_.clear();
39 }
40 
start()41 void Pipeline::start() {
42     Q_ASSERT(!jobs_.isEmpty());
43 
44     index_ = -1;
45     startNext();
46 }
47 
startNext()48 void Pipeline::startNext() {
49     if (index_ + 1 == jobs_.size()) {
50         emitFinished(true);
51         return;
52     }
53     index_ += 1;
54     jobs_[index_]->start();
55 }
56 
emitFinished(bool result)57 void Pipeline::emitFinished(bool result) {
58     for (auto *job : jobs_) {
59         job->cleanUp();
60     }
61     Q_EMIT finished(result);
62 }
63 
64 } // namespace fcitx
65