1 /*
2     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
3     SPDX-FileCopyrightText: 2014-2018 Andrius Štikonas <andrius@stikonas.eu>
4     SPDX-FileCopyrightText: 2019 Albert Astals Cid <aacid@kde.org>
5 
6     SPDX-License-Identifier: GPL-3.0-or-later
7 */
8 
9 #ifndef KPMCORE_OPERATIONRUNNER_H
10 #define KPMCORE_OPERATIONRUNNER_H
11 
12 #include "util/libpartitionmanagerexport.h"
13 
14 #include <QThread>
15 #include <QMutex>
16 #include <QtGlobal>
17 
18 class Operation;
19 class OperationStack;
20 class Report;
21 
22 /** Thread to run the Operations in the OperationStack.
23 
24     Runs the OperationStack when the user applies operations.
25 
26     @author Volker Lanz <vl@fidra.de>
27 */
28 class LIBKPMCORE_EXPORT OperationRunner : public QThread
29 {
30     Q_OBJECT
31     Q_DISABLE_COPY(OperationRunner)
32 
33 public:
34     OperationRunner(QObject* parent, OperationStack& ostack);
35 
36 public:
37     void run() override;
38     qint32 numJobs() const;
39     qint32 numOperations() const;
40     qint32 numProgressSub() const;
isCancelling()41     bool isCancelling() const {
42         return m_Cancelling;    /**< @return if the user has requested cancelling */
43     }
cancel()44     void cancel() const {
45         m_Cancelling = true;    /**< Sets cancelling to true. */
46     }
suspendMutex()47     QMutex& suspendMutex() const {
48         return m_SuspendMutex;    /**< @return the QMutex used for syncing */
49     }
50     QString description(qint32 op) const;
setReport(Report * report)51     void setReport(Report* report) {
52         m_Report = report;    /**< @param report the Report to use while running */
53     }
54 
55 Q_SIGNALS:
56     void progressSub(int);
57     void opStarted(int, Operation*);
58     void opFinished(int, Operation*);
59     void finished();
60     void cancelled();
61     void error();
62 
63 protected:
operationStack()64     OperationStack& operationStack() {
65         return m_OperationStack;
66     }
operationStack()67     const OperationStack& operationStack() const {
68         return m_OperationStack;
69     }
setCancelling(bool b)70     void setCancelling(bool b) {
71         m_Cancelling = b;
72     }
report()73     Report& report() {
74         Q_ASSERT(m_Report);
75         return *m_Report;
76     }
77 
78 private:
79     OperationStack& m_OperationStack;
80     Report* m_Report;
81     mutable QMutex m_SuspendMutex;
82     mutable volatile bool m_Cancelling;
83 };
84 
85 #endif
86