Debian Codegraph @

gui/pinyindictmanager/pipeline.cpp

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