File indexing completed on 2024-05-05 05:48:52

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2008 Laurent Montel <montel@kde.org>
0004     SPDX-FileCopyrightText: 2014-2018 Andrius Štikonas <andrius@stikonas.eu>
0005     SPDX-FileCopyrightText: 2015 Chris Campbell <c.j.campbell@ed.ac.uk>
0006 
0007     SPDX-License-Identifier: GPL-3.0-or-later
0008 */
0009 
0010 #ifndef KPMCORE_OPERATION_H
0011 #define KPMCORE_OPERATION_H
0012 
0013 #include "util/libpartitionmanagerexport.h"
0014 
0015 #include <QObject>
0016 #include <QList>
0017 #include <QtGlobal>
0018 
0019 #include <memory>
0020 
0021 class Partition;
0022 class Device;
0023 class Job;
0024 class OperationPrivate;
0025 class OperationStack;
0026 class OperationRunner;
0027 class Report;
0028 
0029 class QString;
0030 class QIcon;
0031 
0032 /** Base class of all Operations.
0033 
0034     An Operation serves two purposes: It is responsible for modifying the device preview to show the
0035     user a state as if the Operation had already been applied and it is made up of Jobs to actually
0036     perform what the Operation is supposed to do.
0037 
0038     Most Operations just run a list of Jobs and for that reason do not even overwrite
0039     Operation::execute(). The more complex Operations, however, need to perform some
0040     extra tasks in between running Jobs (most notably RestoreOperation and CopyOperation). These do
0041     overwrite Operation::execute().
0042 
0043     Operations own the objects they deal with in most cases, usually Partitions. But as soon as
0044     an Operation has been successfully executed, it no longer owns anything, because the
0045     OperationStack then takes over ownership.
0046 
0047     Some rules for creating new operations that inherit the Operation class:
0048 
0049     <ol>
0050         <li>
0051             Don't modify anything in the ctor. The ctor runs before merging operations. If you
0052             modify anything there, undo and merging will break. Just remember what you're
0053             supposed to do in the ctor and perform modifications in preview().
0054         </li>
0055         <li>
0056             Do not access the preview partitions and devices in description(). If you do,
0057             the operation descriptions will be wrong.
0058         </li>
0059         <li>
0060             Don't create or delete objects in preview() or undo() since these will be called
0061             more than once. Create and delete objects in the ctor and dtor.
0062         </li>
0063     </ol>
0064 
0065     @author Volker Lanz <vl@fidra.de>
0066 */
0067 class LIBKPMCORE_EXPORT Operation : public QObject
0068 {
0069     Q_OBJECT
0070     Q_DISABLE_COPY(Operation)
0071 
0072     friend class OperationStack;
0073     friend class OperationRunner;
0074 
0075 public:
0076     /** Status of this Operation */
0077     enum OperationStatus {
0078         StatusNone = 0,             /**< None yet, can be merged */
0079         StatusPending,              /**< Pending, can be undone */
0080         StatusRunning,              /**< Currently running */
0081         StatusFinishedSuccess,      /**< Successfully finished */
0082         StatusFinishedWarning,      /**< Finished with warnings */
0083         StatusError                 /**< Finished with errors */
0084     };
0085 
0086 protected:
0087     Operation();
0088     ~Operation() override;
0089 
0090 Q_SIGNALS:
0091     void progress(int);
0092     void jobStarted(Job*, Operation*);
0093     void jobFinished(Job*, Operation*);
0094 
0095 public:
0096     virtual QString iconName() const = 0; /**< @return name of the icon for the Operation */
0097     virtual QString description() const = 0; /**< @return the Operation's description */
0098     virtual void preview() = 0; /**< Apply the Operation to the current preview */
0099     virtual void undo() = 0; /**< Undo applying the Operation to the current preview */
0100     virtual bool execute(Report& parent);
0101 
0102     virtual bool targets(const Device&) const = 0;
0103     virtual bool targets(const Partition&) const = 0;
0104 
0105     /**< @return the current status */
0106     virtual OperationStatus status() const;
0107 
0108     virtual QString statusText() const;
0109     virtual QString statusIcon() const;
0110 
0111     /**< @param s the new status */
0112     virtual void setStatus(OperationStatus s);
0113 
0114     qint32 totalProgress() const;
0115 
0116 protected:
0117     void onJobStarted();
0118     void onJobFinished();
0119 
0120     void insertPreviewPartition(Device& targetDevice, Partition& newPartition);
0121     void removePreviewPartition(Device& device, Partition& p);
0122 
0123     void addJob(Job* job);
0124 
0125     QList<Job*>& jobs();
0126     const QList<Job*>& jobs() const;
0127 
0128     void setProgressBase(qint32 i);
0129     qint32 progressBase() const;
0130 
0131 private:
0132     std::unique_ptr<OperationPrivate> d;
0133 };
0134 
0135 #endif