File indexing completed on 2024-12-22 03:41:45

0001 /*
0002     SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #ifndef KABSTRACTCONFIGMODULE_H
0007 #define KABSTRACTCONFIGMODULE_H
0008 
0009 #include "kcmutilscore_export.h"
0010 
0011 #include <QObject>
0012 
0013 #include <memory>
0014 
0015 class KPluginMetaData;
0016 class KAbstractConfigModulePrivate;
0017 
0018 /**
0019  * Base class for QML and QWidgets config modules.
0020  *
0021  * @author Alexander Lohnau
0022  * @since 6.0
0023  */
0024 class KCMUTILSCORE_EXPORT KAbstractConfigModule : public QObject
0025 {
0026     Q_OBJECT
0027 
0028     Q_PROPERTY(KAbstractConfigModule::Buttons buttons READ buttons WRITE setButtons NOTIFY buttonsChanged)
0029     Q_PROPERTY(bool defaultsIndicatorsVisible READ defaultsIndicatorsVisible WRITE setDefaultsIndicatorsVisible NOTIFY defaultsIndicatorsVisibleChanged)
0030     Q_PROPERTY(bool needsAuthorization READ needsAuthorization NOTIFY authActionNameChanged)
0031     Q_PROPERTY(bool representsDefaults READ representsDefaults WRITE setRepresentsDefaults NOTIFY representsDefaultsChanged)
0032     Q_PROPERTY(bool needsSave READ needsSave WRITE setNeedsSave NOTIFY needsSaveChanged)
0033     Q_PROPERTY(QString name READ name CONSTANT)
0034     Q_PROPERTY(QString description READ description CONSTANT)
0035 public:
0036     /**
0037      * An enumeration type for the buttons used by this module.
0038      * You should only use Help, Default and Apply. The rest is obsolete.
0039      * NoAdditionalButton can be used when we do not want have other button that Ok Cancel
0040      *
0041      * @see ConfigModule::buttons @see ConfigModule::setButtons
0042      */
0043     enum Button {
0044         NoAdditionalButton = 0,
0045         Help = 1,
0046         Default = 2,
0047         Apply = 4,
0048         Export = 8,
0049     };
0050     Q_ENUM(Button)
0051     Q_DECLARE_FLAGS(Buttons, Button)
0052     Q_FLAG(Buttons)
0053 
0054     explicit KAbstractConfigModule(QObject *parent, const KPluginMetaData &metaData);
0055 
0056     ~KAbstractConfigModule() override;
0057 
0058     /**
0059      * @brief Set if the module's save() method requires authorization to be executed
0060      *
0061      * It will still have to execute the action itself using the KAuth library, so
0062      * this method is not technically needed to perform the action, but
0063      * using this method will ensure that hosting
0064      * applications like System Settings or kcmshell behave correctly.
0065      *
0066      * @param action the action that will be used by this ConfigModule
0067      */
0068     void setAuthActionName(const QString &action);
0069 
0070     /**
0071      * Returns the action previously set with setAuthActionName(). By default this will be a empty string.
0072      *
0073      * @return The action that has to be authorized to execute the save() method.
0074      */
0075     QString authActionName() const;
0076 
0077     /**
0078      * The auth action name has changed
0079      */
0080     Q_SIGNAL void authActionNameChanged();
0081 
0082     /**
0083      * Set this property to true when the user changes something in the module,
0084      * signaling that a save (such as user pressing Ok or Apply) is needed.
0085      */
0086     void setNeedsSave(bool needs);
0087 
0088     /**
0089      * True when the module has something changed and needs save.
0090      */
0091     bool needsSave() const;
0092 
0093     /**
0094      * Indicate that the state of the modules contents has changed.
0095      *
0096      * This signal is emitted whenever the state of the configuration
0097      * shown in the module changes. It allows the module container to
0098      * keep track of unsaved changes.
0099      */
0100     Q_SIGNAL void needsSaveChanged();
0101 
0102     /**
0103      * Set this property to true when the user sets the state of the module
0104      * to the default settings (e.g. clicking Defaults would do nothing).
0105      */
0106     void setRepresentsDefaults(bool defaults);
0107 
0108     /**
0109      * True when the module state represents the default settings.
0110      */
0111     bool representsDefaults() const;
0112 
0113     /**
0114      * Indicate that the state of the modules contents has changed
0115      * in a way that it might represents the defaults settings, or
0116      * stopped representing them.
0117      */
0118     Q_SIGNAL void representsDefaultsChanged();
0119 
0120     /**
0121      * Sets the buttons to display.
0122      *
0123      * Help: shows a "Help" button.
0124      *
0125      * Default: shows a "Use Defaults" button.
0126      *
0127      * Apply: shows an "Ok", "Apply" and "Cancel" button.
0128      *
0129      * If Apply is not specified, kcmshell will show a "Close" button.
0130      *
0131      * @see ConfigModule::buttons
0132      */
0133     void setButtons(const Buttons btn);
0134 
0135     /**
0136      * Indicate which buttons will be used.
0137      *
0138      * The return value is a value or'ed together from
0139      * the Button enumeration type.
0140      *
0141      * @see ConfigModule::setButtons
0142      */
0143     Buttons buttons() const;
0144 
0145     /**
0146      * Buttons to display changed.
0147      */
0148     Q_SIGNAL void buttonsChanged();
0149 
0150     /**
0151      * @return true, if the authActionName is not empty
0152      * @sa setAuthActionName
0153      */
0154     bool needsAuthorization() const;
0155 
0156     /**
0157      * @returns the name of the config module
0158      */
0159     QString name() const;
0160 
0161     /**
0162      * @returns the description of the config module
0163      */
0164     QString description() const;
0165 
0166     /**
0167      * Change defaultness indicator visibility
0168      * @param visible
0169      */
0170     void setDefaultsIndicatorsVisible(bool visible);
0171 
0172     /**
0173      * @returns defaultness indicator visibility
0174      */
0175     bool defaultsIndicatorsVisible() const;
0176 
0177     /**
0178      * Emitted when kcm need to display indicators for field with non default value
0179      */
0180     Q_SIGNAL void defaultsIndicatorsVisibleChanged();
0181 
0182     /**
0183      * Returns the metaData that was used when instantiating the plugin
0184      */
0185     KPluginMetaData metaData() const;
0186 
0187     /**
0188      * This signal will be emited by a single-instance application (such as
0189      * System Settings) to request activation and update arguments to a module
0190      * that is already running
0191      *
0192      * The module should connect to this signal when it needs to handle
0193      * the activation request and specially the new arguments
0194      *
0195      * @param args A list of arguments that get passed to the module
0196      */
0197     Q_SIGNAL void activationRequested(const QVariantList &args);
0198 
0199     /**
0200      * Load the configuration data into the module.
0201      *
0202      * The load method sets the user interface elements of the
0203      * module to reflect the current settings stored in the
0204      * configuration files.
0205      *
0206      * This method is invoked whenever the module should read its configuration
0207      * (most of the times from a config file) and update the user interface.
0208      * This happens when the user clicks the "Reset" button in the control
0209      * center, to undo all of his changes and restore the currently valid
0210      * settings. It is also called right after construction.
0211      */
0212     virtual void load();
0213 
0214     /**
0215      * The save method stores the config information as shown
0216      * in the user interface in the config files.
0217      *
0218      * This method is called when the user clicks "Apply" or "Ok".
0219      *
0220      */
0221     virtual void save();
0222 
0223     /**
0224      * Sets the configuration to default values.
0225      *
0226      * This method is called when the user clicks the "Default"
0227      * button.
0228      */
0229     virtual void defaults();
0230 
0231 private:
0232     const std::unique_ptr<KAbstractConfigModulePrivate> d;
0233 };
0234 
0235 Q_DECLARE_OPERATORS_FOR_FLAGS(KAbstractConfigModule::Buttons)
0236 
0237 #endif // KABSTRACTCONFIGMODULE_H