Warning, file /frameworks/kdeclarative/src/quickaddons/configmodule.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-FileCopyrightText: 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
0005     SPDX-FileCopyrightText: 2001 Michael Goffioul <kdeprint@swing.be>
0006     SPDX-FileCopyrightText: 2004 Frans Englich <frans.englich@telia.com>
0007     SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
0008     SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0009 
0010     SPDX-License-Identifier: LGPL-2.0-or-later
0011 */
0012 
0013 #ifndef CONFIGMODULE_H
0014 #define CONFIGMODULE_H
0015 
0016 #include "quickaddons_export.h"
0017 
0018 #include <QObject>
0019 #include <QStringList>
0020 #include <QVariant>
0021 #include <QtQml>
0022 
0023 #include <KPluginMetaData>
0024 
0025 class KAboutData;
0026 class QQuickItem;
0027 class QQmlEngine;
0028 
0029 namespace KQuickAddons
0030 {
0031 class ConfigModulePrivate;
0032 
0033 /**
0034  * @class KQuickAddons::ConfigModule configmodule.h KQuickAddons/ConfigModule
0035  *
0036  * The base class for configuration modules.
0037  *
0038  * Configuration modules are realized as plugins that are loaded only when
0039  * needed.
0040  *
0041  * The module in principle is a simple widget displaying the
0042  * item to be changed. The module has a very small interface.
0043  *
0044  * All the necessary glue logic and the GUI bells and whistles
0045  * are provided by the control center and must not concern
0046  * the module author.
0047  *
0048  * To write a config module, you have to create a C++ library
0049  * and an accompaning QML user interface.
0050  * The library must contain a factory function like the following:
0051  *
0052  * \code
0053  * #include <KPluginFactory>
0054  *
0055  * K_PLUGIN_CLASS_WITH_JSON(MyConfigModule, "myconfigmodule.json")
0056  * \endcode
0057  *
0058  * The constructor of the ConfigModule then looks like this:
0059  * \code
0060  * MyConfigModule::MyConfigModule(QObject* parent, const KPluginMetaData &metaData, const QVariantList &args)
0061  *   : ConfigModule(parent, metaData, args)
0062  * {
0063  *   .
0064  *   .
0065  *   .
0066  * }
0067  * \endcode
0068  *
0069  * The QML part must be in the KPackage format, installed under share/kpackage/kcms.
0070  * @see KPackage::Package
0071  *
0072  * The package must have the same name as the C++ plugin, to be installed
0073  * by CMake with the command:
0074  * \code
0075  * kpackage_install_package(packagedir kcm_componentName kcms)
0076  * \endcode
0077  * The "packagedir" is the subdirectory in the source tree where the package sources are
0078  * located, and "kcm_componentName" is the name of the C++ plugin. Finally "kcms" is the literal string "kcms",
0079  * so that the package is
0080  * installed as a configuration module (and not some other kind of package).
0081  * The main config dialog UI will be the file
0082  * ui/main.qml from the package (or what X-KPackage-MainScript value is in the
0083  * package metadata desktop file).
0084  *
0085  * The QML part can access all the properties of ConfigModule (together with the properties
0086  * defined in its subclass) by accessing to the global object "kcm", or with the
0087  * import of "org.kde.kcm 1.0" the ConfigModule attached property.
0088  *
0089  * \code
0090  * import QtQuick 2.1
0091  * import QtQuick.Controls 1.0 as QtControls
0092  * import org.kde.kcm 1.0
0093  * import org.kde.plasma.core 2.0 as PlasmaCore
0094  *
0095  * Item {
0096  *     //implicitWidth and implicitHeight will be used as initial size
0097  *     //when loaded in kcmshell5
0098  *     implicitWidth: units.gridUnit * 20
0099  *     implicitHeight: units.gridUnit * 20
0100  *
0101  *     ConfigModule.buttons: ConfigModule.Help|ConfigModule.Apply
0102  *     Label {
0103  *         text: kcm.needsSave
0104  *     }
0105  * }
0106  * \endcode
0107  *
0108  * See https://develop.kde.org/docs/extend/kcm/
0109  * for more detailed documentation.
0110  *
0111  */
0112 class QUICKADDONS_EXPORT ConfigModule : public QObject
0113 {
0114     Q_OBJECT
0115 
0116     Q_PROPERTY(QQuickItem *mainUi READ mainUi CONSTANT)
0117     Q_PROPERTY(KQuickAddons::ConfigModule::Buttons buttons READ buttons WRITE setButtons NOTIFY buttonsChanged)
0118     Q_PROPERTY(bool needsSave READ needsSave WRITE setNeedsSave NOTIFY needsSaveChanged)
0119     Q_PROPERTY(bool representsDefaults READ representsDefaults WRITE setRepresentsDefaults NOTIFY representsDefaultsChanged)
0120     Q_PROPERTY(QString name READ name CONSTANT)
0121     Q_PROPERTY(QString description READ description CONSTANT)
0122     Q_PROPERTY(QString quickHelp READ quickHelp WRITE setQuickHelp NOTIFY quickHelpChanged)
0123     Q_PROPERTY(QString rootOnlyMessage READ rootOnlyMessage WRITE setRootOnlyMessage NOTIFY rootOnlyMessageChanged)
0124     Q_PROPERTY(bool useRootOnlyMessage READ useRootOnlyMessage WRITE setUseRootOnlyMessage NOTIFY useRootOnlyMessageChanged)
0125     Q_PROPERTY(bool needsAuthorization READ needsAuthorization WRITE setNeedsAuthorization NOTIFY needsAuthorizationChanged)
0126     Q_PROPERTY(int columnWidth READ columnWidth WRITE setColumnWidth NOTIFY columnWidthChanged)
0127     Q_PROPERTY(int depth READ depth NOTIFY depthChanged)
0128     Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
0129     Q_PROPERTY(int defaultsIndicatorsVisible READ defaultsIndicatorsVisible WRITE setDefaultsIndicatorsVisible NOTIFY defaultsIndicatorsVisibleChanged)
0130 
0131 public:
0132     /**
0133      * An enumeration type for the buttons used by this module.
0134      * You should only use Help, Default and Apply. The rest is obsolete.
0135      * NoAdditionalButton can be used when we do not want have other button that Ok Cancel
0136      *
0137      * @see ConfigModule::buttons @see ConfigModule::setButtons
0138      */
0139     enum Button {
0140         NoAdditionalButton = 0,
0141         Help = 1,
0142         Default = 2,
0143         Apply = 4,
0144     };
0145     Q_ENUM(Button)
0146     Q_DECLARE_FLAGS(Buttons, Button)
0147     Q_FLAG(Buttons)
0148 
0149 #if QUICKADDONS_ENABLE_DEPRECATED_SINCE(5, 88)
0150     /**
0151      * Base class for all KControlModules.
0152      *
0153      * @note do not emit changed signals here, since they are not yet connected
0154      *       to any slot.
0155      * @param aboutData becomes owned by the ConfigModule
0156      * @deprecated since 5.88, use ConfigModule(QObject*, KPluginMetaData, QVariantList)
0157      */
0158     QUICKADDONS_DEPRECATED_VERSION(5, 88, "Use ConfigModule(QObject*, KPluginMetaData, QVariantList)")
0159     explicit ConfigModule(const KAboutData *aboutData, QObject *parent = nullptr, const QVariantList &args = QVariantList());
0160 #endif
0161 
0162 #if QUICKADDONS_ENABLE_DEPRECATED_SINCE(5, 88)
0163     /**
0164      * @note do not emit changed signals here, since they are not yet connected
0165      *       to any slot.
0166      * @param metaData description for the plugin: it will generate a KAboutData from that
0167      * @since 5.11
0168      * @deprecated since 5.88, use ConfigModule(QObject*, KPluginMetaData, QVariantList)
0169      */
0170     QUICKADDONS_DEPRECATED_VERSION(5, 88, "Use ConfigModule(QObject*, KPluginMetaData, QVariantList)")
0171     explicit ConfigModule(const KPluginMetaData &metaData, QObject *parent = nullptr, const QVariantList &args = QVariantList());
0172 #endif
0173 
0174     /**
0175      * Base class for all KControlModules.
0176      *
0177      * @note do not emit changed signals here, since they are not yet connected
0178      *       to any slot.
0179      * @since 5.88
0180      */
0181     explicit ConfigModule(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args = QVariantList());
0182 
0183 #if QUICKADDONS_ENABLE_DEPRECATED_SINCE(5, 104)
0184     /**
0185      * Base class for all KControlModules.
0186      *
0187      * @note do not emit changed signals here, since they are not yet connected
0188      *       to any slot.
0189      *
0190      * @deprecated since 5.104, use ConfigModule(QObject*, KPluginMetaData, QVariantList)
0191      */
0192     QUICKADDONS_DEPRECATED_VERSION(5, 104, "Use ConfigModule(QObject*, KPluginMetaData, QVariantList)")
0193     explicit ConfigModule(QObject *parent = nullptr, const QVariantList &args = QVariantList());
0194 #endif
0195 
0196     /**
0197      * Destroys the module.
0198      */
0199     ~ConfigModule() override;
0200 
0201 #if QUICKADDONS_ENABLE_DEPRECATED_SINCE(5, 88)
0202     /**
0203      * This is generally only called for the KBugReport.
0204      * If you override you should  have it return a pointer to a constant.
0205      *
0206      *
0207      * @returns the KAboutData for this module
0208      * @deprecated since 5.88. Use the KPluginMetaData the ConfigModule was instantiated from.
0209      */
0210     QUICKADDONS_DEPRECATED_VERSION(5, 88, "Use the KPluginMetaData the ConfigModule was instantiated from")
0211     const KAboutData *aboutData() const;
0212 #endif
0213 
0214 #if QUICKADDONS_ENABLE_DEPRECATED_SINCE(5, 104)
0215     /**
0216      * This sets the KAboutData returned by aboutData()
0217      * The about data is now owned by ConfigModule.
0218      *
0219      * @deprecated since 5.104. Use the ConfigModule(QObject *, const KPluginMetaData &, const QVariantList &) constructor.
0220      * Then the metaData is taken from the plugin's JSON file.
0221      */
0222     QUICKADDONS_DEPRECATED_VERSION(5, 104, "See API docs.")
0223     void setAboutData(const KAboutData *about);
0224 #endif
0225 
0226     /**
0227      * @brief Set if the module's save() method requires authorization to be executed
0228      *
0229      * It will still have to execute the action itself using the KAuth library, so
0230      * this method is not technically needed to perform the action, but
0231      * using this method will ensure that hosting
0232      * applications like System Settings or kcmshell behave correctly.
0233      *
0234      * @param action the action that will be used by this ConfigModule
0235      */
0236     void setAuthActionName(const QString &action);
0237 
0238     /**
0239      * Returns the action previously set with setAuthActionName(). By default its an invalid action.
0240      *
0241      * @return The action that has to be authorized to execute the save() method.
0242      */
0243     QString authActionName() const;
0244 
0245     /**
0246      * @return the qml engine that built the main config UI
0247      */
0248     QQmlEngine *engine() const;
0249 
0250     /**
0251      * The status of the mainUi component.
0252      * @since 5.64
0253      */
0254     QQmlComponent::Status status() const;
0255 
0256     /**
0257      * The error string in case the mainUi failed to load.
0258      * @return 5.64
0259      */
0260     QString errorString() const;
0261 
0262     // QML property accessors
0263 
0264     /**
0265      * @return The main UI for this configuration module. It's a QQuickItem coming from
0266      * the QML package named the same as the KAboutData's component name for
0267      * this config module
0268      */
0269     QQuickItem *mainUi();
0270 
0271     /*
0272      * @return a subpage at a given depth
0273      * @note This does not include the mainUi. i.e a depth of 2 is a mainUi and one subPage
0274      * at index 0
0275      */
0276     QQuickItem *subPage(int index) const;
0277 
0278     /**
0279      * Sets the quick help.
0280      */
0281     void setQuickHelp(const QString &help);
0282 
0283     /**
0284      * Return a quick-help text.
0285      *
0286      * This method is called when the module is docked.
0287      * The quick-help text should contain a short description of the module and
0288      * links to the module's help files. You can use HTML formatting tags in the text.
0289      *
0290      * @note make sure the quick help text gets translated (use i18n()).
0291      */
0292     QString quickHelp() const;
0293 
0294     /**
0295      * Set this property to true when the user changes something in the module,
0296      * signaling that a save (such as user pressing Ok or Apply) is needed.
0297      */
0298     void setNeedsSave(bool needs);
0299 
0300     /**
0301      * True when the module has something changed and needs save.
0302      */
0303     bool needsSave();
0304 
0305     /**
0306      * Set this property to true when the user sets the state of the module
0307      * to the default settings (e.g. clicking Defaults would do nothing).
0308      */
0309     void setRepresentsDefaults(bool defaults);
0310 
0311     /**
0312      * True when the module state represents the default settings.
0313      */
0314     bool representsDefaults();
0315 
0316     /**
0317      * Sets the buttons to display.
0318      *
0319      * Help: shows a "Help" button.
0320      *
0321      * Default: shows a "Use Defaults" button.
0322      *
0323      * Apply: in kcontrol this will show an "Apply" and "Reset" button,
0324      *        in kcmshell this will show an "Ok", "Apply" and "Cancel" button.
0325      *
0326      * If Apply is not specified, kcmshell will show a "Close" button.
0327      *
0328      * @see ConfigModule::buttons
0329      */
0330     void setButtons(const Buttons btn);
0331 
0332     /**
0333      * Indicate which buttons will be used.
0334      *
0335      * The return value is a value or'ed together from
0336      * the Button enumeration type.
0337      *
0338      * @see ConfigModule::setButtons
0339      */
0340     Buttons buttons() const;
0341 
0342     /**
0343      * Sets the RootOnly message.
0344      *
0345      * This message will be shown at the top of the module if useRootOnlyMessage is
0346      * set. If no message is set, a default one will be used.
0347      *
0348      * @see ConfigModule::rootOnlyMessage
0349      */
0350     void setRootOnlyMessage(const QString &message);
0351 
0352     /**
0353      * Get the RootOnly message for this module.
0354      *
0355      * When the module must be run as root, or acts differently
0356      * for root and a normal user, it is sometimes useful to
0357      * customize the message that appears at the top of the module
0358      * when used as a normal user. This function returns this
0359      * customized message. If none has been set, a default message
0360      * will be used.
0361      *
0362      * @see ConfigModule::setRootOnlyMessage
0363      */
0364     QString rootOnlyMessage() const;
0365 
0366     /**
0367      * Change whether or not the RootOnly message should be shown.
0368      *
0369      * Following the value of @p on, the RootOnly message will be
0370      * shown or not.
0371      *
0372      * @see ConfigModule::useRootOnlyMessage
0373      */
0374     void setUseRootOnlyMessage(bool on);
0375 
0376     /**
0377      * Tell if KControl should show a RootOnly message when run as
0378      * a normal user.
0379      *
0380      * In some cases, the module don't want a RootOnly message to
0381      * appear (for example if it has already one). This function
0382      * tells KControl if a RootOnly message should be shown
0383      *
0384      * @see ConfigModule::setUseRootOnlyMessage
0385      */
0386     bool useRootOnlyMessage() const;
0387 
0388     /**
0389      * @brief Set if the module's save() method requires authorization to be executed.
0390      *
0391      * The module can set this property to @c true if it requires authorization.
0392      * It will still have to execute the action itself using the KAuth library, so
0393      * this method is not technically needed to perform the action, but
0394      * using this and/or the setAuthActionName() method will ensure that hosting
0395      * applications like System Settings or kcmshell behave correctly.
0396      *
0397      * Called with @c true, this method will set the action to  "org.kde.kcontrol.name.save" where
0398      * "name" is aboutData()->appName() return value. This default action won't be set if
0399      * the aboutData() object is not valid.
0400      *
0401      * Note that called with @c false, this method will reset the action name set with setAuthActionName().
0402      *
0403      * @param needsAuth Tells if the module's save() method requires authorization to be executed.
0404      */
0405     void setNeedsAuthorization(bool needsAuth);
0406 
0407     /**
0408      * Returns the value previously set with setNeedsAuthorization() or setAuthActionName(). By default it's @c false.
0409      *
0410      * @return @c true if the module's save() method requires authorization, @c false otherwise
0411      */
0412     bool needsAuthorization() const;
0413 
0414     /**
0415      * @returns the name of the config module
0416      * @since 5.41
0417      */
0418     QString name() const;
0419 
0420     /**
0421      * @returns the description of the config module
0422      * @since 5.41
0423      */
0424     QString description() const;
0425 
0426     /**
0427      * returns the width the kcm wants in column mode.
0428      * If a columnWidth is valid ( > 0 ) and less than the systemsettings' view width,
0429      * more than one will be visible at once, and the first page will be a sidebar to the last page pushed.
0430      * As default, this is -1 which will make the shell always show only one page at a time.
0431      * @since 5.50
0432      */
0433     int columnWidth() const;
0434 
0435     /**
0436      * Sets the column width we want.
0437      * @since 5.50
0438      */
0439     void setColumnWidth(int width);
0440 
0441     /**
0442      * @returns how many pages this kcm has.
0443      * It is guaranteed to be at least 1 (the main ui) plus how many times a new page has been pushed without pop
0444      */
0445     int depth() const;
0446 
0447     /**
0448      * Sets the current page index this kcm should display
0449      * @since 5.53
0450      */
0451     void setCurrentIndex(int index);
0452 
0453     /**
0454      * @returns the index of the page this kcm should display
0455      * @since 5.53
0456      */
0457     int currentIndex() const;
0458 
0459     static ConfigModule *qmlAttachedProperties(QObject *object);
0460 
0461     /**
0462      * @returns defaultness indicator visibility
0463      * @since 5.73
0464      */
0465     bool defaultsIndicatorsVisible() const;
0466 
0467 public Q_SLOTS:
0468     /**
0469      * Load the configuration data into the module.
0470      *
0471      * The load method sets the user interface elements of the
0472      * module to reflect the current settings stored in the
0473      * configuration files.
0474      *
0475      * This method is invoked whenever the module should read its configuration
0476      * (most of the times from a config file) and update the user interface.
0477      * This happens when the user clicks the "Reset" button in the control
0478      * center, to undo all of his changes and restore the currently valid
0479      * settings. It is also called right after construction.
0480      */
0481     virtual void load();
0482 
0483     /**
0484      * Save the configuration data.
0485      *
0486      * The save method stores the config information as shown
0487      * in the user interface in the config files.
0488      *
0489      * If necessary, this method also updates the running system,
0490      * e.g. by restarting applications. This normally does not apply for
0491      * KSettings::Dialog modules where the updating is taken care of by
0492      * KSettings::Dispatcher.
0493      *
0494      * save is called when the user clicks "Apply" or "Ok".
0495      *
0496      */
0497     virtual void save();
0498 
0499     /**
0500      * Sets the configuration to sensible default values.
0501      *
0502      * This method is called when the user clicks the "Default"
0503      * button. It should set the display to useful values.
0504      */
0505     virtual void defaults();
0506 
0507     /**
0508      * Push a new sub page in the KCM hierarchy: pages will be seen as a Kirigami PageRow
0509      * @since 5.50
0510      */
0511     void push(const QString &fileName, const QVariantMap &propertyMap = QVariantMap());
0512 
0513     /**
0514      *
0515      */
0516     void push(QQuickItem *item);
0517 
0518     /**
0519      * pop the last page of the KCM hierarchy, the page is destroyed
0520      * @since 5.50
0521      */
0522     void pop();
0523 
0524     /**
0525      * remove and return the last page of the KCM hierarchy:
0526      * the popped page won't be deleted, it's the caller's responsibility to manage the lifetime of the returned item
0527      * @returns the last page if any, nullptr otherwise
0528      * @since 5.89
0529      */
0530     QQuickItem *takeLast();
0531 
0532     /**
0533      * Ask the shell to show a passive notification
0534      * @param message The message text to display
0535      * @param timeout (optional) the timeout, either in milliseconds or the strings "short" and "long"
0536      * @param actionText (optional) The notification can have a button with this text
0537      * @param callBack (optional) If actionText is set and callBack is a JavaScript function, it will be called when the use clicks the button.
0538      * @since 5.68
0539      */
0540     void showPassiveNotification(const QString &message,
0541                                  const QVariant &timeout = QVariant(),
0542                                  const QString &actionText = QString(),
0543                                  const QJSValue &callBack = QJSValue());
0544 
0545     /**
0546      * Change defaultness indicator visibility
0547      * @param visible
0548      * @since 5.73
0549      */
0550     void setDefaultsIndicatorsVisible(bool visible);
0551 
0552 Q_SIGNALS:
0553 
0554     /**
0555      * The auth action name has changed: this signal will relay it to the actual KCM
0556      */
0557     void authActionNameChanged();
0558 
0559     // QML NOTIFY signaling
0560     /**
0561      * Buttons to display changed.
0562      */
0563     void buttonsChanged();
0564 
0565     /**
0566      * Indicate that the module's quickhelp has changed.
0567      *
0568      * Emit this signal whenever the module's quickhelp changes.
0569      * Modules implemented as tabbed dialogs might want to implement
0570      * per-tab quickhelp for example.
0571      *
0572      */
0573     void quickHelpChanged();
0574 
0575     /**
0576      * Indicate that the state of the modules contents has changed.
0577      *
0578      * This signal is emitted whenever the state of the configuration
0579      * shown in the module changes. It allows the module container to
0580      * keep track of unsaved changes.
0581      */
0582     void needsSaveChanged();
0583 
0584     /**
0585      * Indicate that the state of the modules contents has changed
0586      * in a way that it might represents the defaults settings, or
0587      * stopped representing them.
0588      */
0589     void representsDefaultsChanged();
0590 
0591     /**
0592      * Emits this signal whenever the need for root authorization to save changes.
0593      */
0594     void needsAuthorizationChanged();
0595 
0596     /**
0597      * Indicate that the module's root message has changed.
0598      *
0599      * Emits this signal whenever the module's root message changes.
0600      *
0601      */
0602     void rootOnlyMessageChanged();
0603 
0604     /**
0605      * Emits this signal whenever the root only message gets used or discarded.
0606      */
0607     void useRootOnlyMessageChanged();
0608 
0609     /**
0610      * Emitted when a new sub page is pushed
0611      * @since 5.50
0612      */
0613     void pagePushed(QQuickItem *page);
0614 
0615     /**
0616      * Emitted when a sub page is popped
0617      * @since 5.50
0618      */
0619     // RFC: page argument?
0620     void pageRemoved();
0621 
0622     /**
0623      * Emitted when the wanted column width of the kcm changes
0624      * @since 5.50
0625      */
0626     void columnWidthChanged(int width);
0627 
0628     /**
0629      * Emitted when the current page changed
0630      * @since 5.53
0631      */
0632     void currentIndexChanged(int index);
0633 
0634     /**
0635      * Emitted when the number of pages changed
0636      * @since 5.53
0637      */
0638     void depthChanged(int index);
0639 
0640     /**
0641      * Emitted when the kcm wants the shell to display a passive notification
0642      * @since 5.68
0643      */
0644     void passiveNotificationRequested(const QString &message, const QVariant &timeout, const QString &actionText, const QJSValue &callBack);
0645 
0646     /**
0647      * Emitted when kcm need to display indicators for field with non default value
0648      * @since 5.73
0649      */
0650     void defaultsIndicatorsVisibleChanged();
0651 
0652 private:
0653     ConfigModulePrivate *const d;
0654 };
0655 
0656 }
0657 
0658 Q_DECLARE_OPERATORS_FOR_FLAGS(KQuickAddons::ConfigModule::Buttons)
0659 QML_DECLARE_TYPEINFO(KQuickAddons::ConfigModule, QML_HAS_ATTACHED_PROPERTIES)
0660 
0661 #endif // ConfigModule_H