File indexing completed on 2024-05-05 17:33:23

0001 /*
0002  *   SPDX-FileCopyrightText: 2012 Jonathan Thomas <echidnaman@kubuntu.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #pragma once
0008 
0009 // Qt includes
0010 #include <QObject>
0011 
0012 // Own includes
0013 #include "AddonList.h"
0014 
0015 #include "discovercommon_export.h"
0016 
0017 class AbstractResource;
0018 
0019 /**
0020  * \class Transaction  Transaction.h "Transaction.h"
0021  *
0022  * \brief This is the base class of all transactions.
0023  *
0024  * When there are transactions running inside Discover, the backends should
0025  * provide the corresponding Transaction objects with proper information.
0026  */
0027 class DISCOVERCOMMON_EXPORT Transaction : public QObject
0028 {
0029     Q_OBJECT
0030 
0031     Q_PROPERTY(QString name READ name CONSTANT)
0032     Q_PROPERTY(QVariant icon READ icon CONSTANT)
0033     Q_PROPERTY(AbstractResource *resource READ resource CONSTANT)
0034     Q_PROPERTY(Role role READ role CONSTANT)
0035     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
0036     Q_PROPERTY(bool isCancellable READ isCancellable NOTIFY cancellableChanged)
0037     Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
0038     Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
0039     Q_PROPERTY(quint64 downloadSpeed READ downloadSpeed WRITE setDownloadSpeed NOTIFY downloadSpeedChanged)
0040     Q_PROPERTY(QString downloadSpeedString READ downloadSpeedString NOTIFY downloadSpeedChanged)
0041     Q_PROPERTY(QString remainingTimeString READ remainingTimeString NOTIFY remainingTimeChanged)
0042     Q_PROPERTY(uint remainingTime READ remainingTime NOTIFY remainingTimeChanged)
0043 
0044 public:
0045     enum Status {
0046         /// Not queued, newly created
0047         SetupStatus = 0,
0048         /// Queued, but not yet run
0049         QueuedStatus,
0050         /// Transaction is in the downloading phase
0051         DownloadingStatus,
0052         /// Transaction is doing an installation/removal
0053         CommittingStatus,
0054         /// Transaction is done
0055         DoneStatus,
0056         /// Transaction is done, but there was an error during transaction
0057         DoneWithErrorStatus,
0058         /// Transaction was cancelled
0059         CancelledStatus,
0060     };
0061     Q_ENUM(Status)
0062 
0063     enum Role {
0064         /// The transaction is going to install a resource
0065         InstallRole = 0,
0066         /// The transaction is going to remove a resource
0067         RemoveRole,
0068         /// The transaction is going to change the addons of a resource
0069         ChangeAddonsRole,
0070     };
0071     Q_ENUM(Role)
0072 
0073     Transaction(QObject *parent, AbstractResource *resource, Transaction::Role role, const AddonList &addons = {});
0074 
0075     ~Transaction() override;
0076 
0077     /**
0078      * @returns the AbstractResource which this transaction works with
0079      */
0080     AbstractResource *resource() const;
0081     /**
0082      * @returns the role which this transaction executes
0083      */
0084     Role role() const;
0085     /**
0086      * @returns the current status
0087      */
0088     Status status() const;
0089     /**
0090      * @returns the addons which this transaction works on
0091      */
0092     AddonList addons() const;
0093     /**
0094      * @returns true when the transaction can be canceled
0095      */
0096     bool isCancellable() const;
0097     /**
0098      * @returns a percentage of how much the transaction is already done
0099      */
0100     int progress() const;
0101 
0102     /**
0103      * Sets the status of the transaction
0104      * @param status the new status
0105      */
0106     void setStatus(Status status);
0107     /**
0108      * Sets whether the transaction can be canceled or not
0109      * @param isCancellable should be true if the transaction can be canceled
0110      */
0111     void setCancellable(bool isCancellable);
0112     /**
0113      * Sets the progress of the transaction
0114      * @param progress this should be a percentage of how much of the transaction is already done
0115      */
0116     void setProgress(int progress);
0117 
0118     /**
0119      * Cancels the transaction
0120      */
0121     Q_SCRIPTABLE virtual void cancel() = 0;
0122 
0123     /**
0124      * @returns if the transaction is either downloading or committing
0125      */
0126     bool isActive() const;
0127 
0128     Q_SCRIPTABLE virtual void proceed()
0129     {
0130     }
0131 
0132     /** @returns a name that identifies the transaction */
0133     virtual QString name() const;
0134 
0135     /** @returns an icon that describes the transaction */
0136     virtual QVariant icon() const;
0137 
0138     bool isVisible() const;
0139     void setVisible(bool v);
0140 
0141     quint64 downloadSpeed() const
0142     {
0143         return m_downloadSpeed;
0144     }
0145     void setDownloadSpeed(quint64 downloadSpeed);
0146 
0147     uint remainingTime() const
0148     {
0149         return m_remainingTime;
0150     }
0151     void setRemainingTime(uint seconds);
0152 
0153     QString downloadSpeedString() const;
0154     QString remainingTimeString() const;
0155 
0156 private:
0157     AbstractResource *const m_resource;
0158     const Role m_role;
0159     Status m_status;
0160     const AddonList m_addons;
0161     bool m_isCancellable;
0162     int m_progress;
0163     bool m_visible = true;
0164     quint64 m_downloadSpeed = 0;
0165     uint m_remainingTime = 0;
0166 
0167 Q_SIGNALS:
0168     /**
0169      * This gets emitted when the status of the transaction changed
0170      */
0171     void statusChanged(Transaction::Status status);
0172     /**
0173      * This gets emitted when the ability to cancel the transaction or not changed
0174      */
0175     void cancellableChanged(bool cancellable);
0176     /**
0177      * This gets emitted when the transaction changed the percentage of how much of it is already done
0178      */
0179     void progressChanged(int progress);
0180 
0181     /**
0182      * Provides a message to be shown to the user
0183      *
0184      * The user gets to acknowledge and proceed or cancel the transaction.
0185      *
0186      * @sa proceed(), cancel()
0187      */
0188     void proceedRequest(const QString &title, const QString &description);
0189 
0190     void passiveMessage(const QString &message);
0191 
0192     /**
0193      * A fatal error was found on distro packaging. Provide a @p message to show
0194      * in a modal dialog that should lead the user towards reporting the problem..
0195      */
0196     void distroErrorMessage(const QString &message);
0197 
0198     void visibleChanged(bool visible);
0199 
0200     void downloadSpeedChanged(quint64 downloadSpeed);
0201 
0202     void remainingTimeChanged(uint remainingTime);
0203 
0204     void webflowStarted(const QUrl &url, int id);
0205     void webflowDone(int id);
0206 };