File indexing completed on 2024-04-28 05:45:54

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2016-2018 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2018 Huzaifa Faruqui <huzaifafaruqui@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-3.0-or-later
0007 */
0008 
0009 #ifndef KPMCORE_JOB_H
0010 #define KPMCORE_JOB_H
0011 
0012 #include "fs/filesystem.h"
0013 
0014 #include "util/libpartitionmanagerexport.h"
0015 
0016 #include <QObject>
0017 #include <QtGlobal>
0018 
0019 class QString;
0020 class QIcon;
0021 
0022 class CopySource;
0023 class CopyTarget;
0024 class Report;
0025 
0026 /** Base class for all Jobs.
0027 
0028     Each Operation is made up of one or more Jobs. Usually, an Operation will run each Job it is
0029     made up of and only complete successfully if each Job could be run without error. Jobs are
0030     all-or-nothing and try to be as atomic as possible: A Job is either successfully run or not, there
0031     is no case where a Job finishes with a warning.
0032 
0033     @author Volker Lanz <vl@fidra.de>
0034 */
0035 class LIBKPMCORE_EXPORT Job : public QObject
0036 {
0037     Q_OBJECT
0038     Q_DISABLE_COPY(Job)
0039 
0040 public:
0041     /** Status of this Job */
0042     enum class Status : int {
0043         Pending,        /**< Pending, not yet run */
0044         Success,        /**< Successfully run */
0045         Error           /**< Running generated an error */
0046     };
0047 
0048 protected:
0049     Job();
0050 
0051 public:
0052     ~Job() override {}
0053 
0054 Q_SIGNALS:
0055     void started();
0056     void progress(int);
0057     void finished();
0058 
0059 public:
0060     virtual qint32 numSteps() const {
0061         return 1;    /**< @return the number of steps the job takes to complete */
0062     }
0063     virtual QString description() const = 0; /**< @return the Job's description */
0064     virtual bool run(Report& parent) = 0; /**< @param parent parent Report to add new child to for this Job @return true if successfully run */
0065 
0066     virtual QString statusIcon() const;
0067     virtual QString statusText() const;
0068 
0069     Status status() const {
0070         return m_Status;    /**< @return the Job's current status */
0071     }
0072 
0073     void emitProgress(int i);
0074     void updateReport(const QString& report);
0075 
0076 protected:
0077     bool copyBlocks(Report& report, CopyTarget& target, CopySource& source);
0078     bool rollbackCopyBlocks(Report& report, CopyTarget& origTarget, CopySource& origSource);
0079 
0080     Report* jobStarted(Report& parent);
0081     void jobFinished(Report& report, bool b);
0082 
0083     void setStatus(Status s) {
0084         m_Status = s;
0085     }
0086 
0087 private:
0088     Report *m_Report;
0089     Status m_Status;
0090 };
0091 
0092 #endif