File indexing completed on 2024-05-19 03:56:22

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0005     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0006     SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #ifndef KJOBUIDELEGATE_H
0012 #define KJOBUIDELEGATE_H
0013 
0014 #include <QObject>
0015 #include <kcoreaddons_export.h>
0016 #include <memory>
0017 
0018 class KJob;
0019 
0020 /**
0021  * @class KJobUiDelegate kjobuidelegate.h KJobUiDelegate
0022  *
0023  * The base class for all KJob UI delegate.
0024  *
0025  * A UI delegate is responsible for the events of a
0026  * job and provides a UI for them (an error message
0027  * box or warning etc.).
0028  *
0029  * @see KJob
0030  */
0031 class KCOREADDONS_EXPORT KJobUiDelegate : public QObject
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     /**
0037      * Flags for the constructor, to enable automatic handling of errors and/or warnings
0038      * @see Flags
0039      * @since 5.70
0040      */
0041     enum Flag {
0042         AutoHandlingDisabled = 0, ///< No automatic handling (default)
0043         AutoErrorHandlingEnabled = 1, ///< Equivalent to setAutoErrorHandlingEnabled(true)
0044         AutoWarningHandlingEnabled = 2, ///< Equivalent to setAutoWarningHandlingEnabled(true)
0045         AutoHandlingEnabled = AutoErrorHandlingEnabled | AutoWarningHandlingEnabled, ///< Enables both error and warning handling
0046     };
0047     /**
0048      * Stores a combination of #Flag values.
0049      */
0050     Q_DECLARE_FLAGS(Flags, Flag)
0051 
0052     /**
0053      * Constructs a new KJobUiDelegate with a flags argument.
0054      * @param flags allows to enable automatic error/warning handling
0055      * @since 5.70
0056      */
0057     explicit KJobUiDelegate(Flags flags = {KJobUiDelegate::AutoHandlingDisabled});
0058 
0059     /**
0060      * Destroys a KJobUiDelegate.
0061      */
0062     ~KJobUiDelegate() override;
0063 
0064 protected:
0065     /**
0066      * Attach this UI delegate to a job. Once attached it'll track the job events.
0067      *
0068      * @return @c true if this UI delegate was successfully attached to @p job, @c false otherwise
0069      *
0070      * @note if this UI delegate is already attached to a job, calling this method will return
0071      * @c false.
0072      */
0073     virtual bool setJob(KJob *job);
0074 
0075 protected:
0076     /**
0077      * Retrieves the current job this UI delegate is attached to.
0078      *
0079      * @return current job this UI delegate is attached to, or @c nullptr if
0080      * this UI delegate is not tracking any job
0081      */
0082     KJob *job() const;
0083 
0084     friend class KJob;
0085 
0086 public:
0087     /**
0088      * Display to the user the error given by this job.
0089      * The default implementation uses qWarning(). Subclasses
0090      * reimplement this to use something more user-visible such
0091      * as a message box.
0092      *
0093      * Only call this method if error is not 0, and only in the
0094      * slot connected to result.
0095      */
0096     virtual void showErrorMessage();
0097 
0098     /**
0099      * Enable or disable the automatic error handling. When automatic
0100      * error handling is enabled and an error occurs, then showErrorDialog()
0101      * is called, right before the emission of the result signal.
0102      *
0103      * The default is false.
0104      *
0105      * See also isAutoErrorHandlingEnabled , showErrorDialog
0106      *
0107      * @param enable enable or disable automatic error handling
0108      * @see isAutoErrorHandlingEnabled()
0109      */
0110     void setAutoErrorHandlingEnabled(bool enable);
0111 
0112     /**
0113      * Returns whether automatic error handling is enabled or disabled.
0114      * See also setAutoErrorHandlingEnabled .
0115      * @return true if automatic error handling is enabled
0116      * @see setAutoErrorHandlingEnabled()
0117      */
0118     bool isAutoErrorHandlingEnabled() const;
0119 
0120     /**
0121      * Enable or disable the automatic warning handling. When automatic
0122      * warning handling is enabled and an error occurs, then a message box
0123      * is displayed with the warning message
0124      *
0125      * The default is true.
0126      *
0127      * See also isAutoWarningHandlingEnabled , showErrorDialog
0128      *
0129      * @param enable enable or disable automatic warning handling
0130      * @see isAutoWarningHandlingEnabled()
0131      */
0132     void setAutoWarningHandlingEnabled(bool enable);
0133 
0134     /**
0135      * Returns whether automatic warning handling is enabled or disabled.
0136      * See also setAutoWarningHandlingEnabled .
0137      * @return true if automatic warning handling is enabled
0138      * @see setAutoWarningHandlingEnabled()
0139      */
0140     bool isAutoWarningHandlingEnabled() const;
0141 
0142 protected Q_SLOTS:
0143     virtual void slotWarning(KJob *job, const QString &message);
0144 
0145 private:
0146     KCOREADDONS_NO_EXPORT void connectJob(KJob *job);
0147 
0148 private:
0149     std::unique_ptr<class KJobUiDelegatePrivate> const d;
0150 };
0151 
0152 Q_DECLARE_OPERATORS_FOR_FLAGS(KJobUiDelegate::Flags)
0153 
0154 #endif // KJOBUIDELEGATE_H