File indexing completed on 2024-05-05 04:33:15

0001 /*
0002     SPDX-FileCopyrightText: 2004-2018 Gilles Caulier <caulier dot gilles at gmail dot com>
0003     SPDX-FileCopyrightText: 2012 Victor Dodon <dodonvictor at gmail dot com>
0004     SPDX-FileCopyrightText: 2004-2005 Renchi Raju <renchi dot raju at gmail dot com>
0005     SPDX-FileCopyrightText: 2004-2005 Jesper K. Pedersen <blackie at kde dot org>
0006     SPDX-FileCopyrightText: 2004-2005 Aurelien Gateau <aurelien dot gateau at free dot fr>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef KIPI_PLUGIN_H
0012 #define KIPI_PLUGIN_H
0013 
0014 // Std includes
0015 
0016 #include <memory>
0017 
0018 // Qt includes
0019 
0020 #include <QObject>
0021 #include <QList>
0022 #include <QHash>
0023 #include <QDomElement>
0024 #include <QDomNode>
0025 #include <QDomDocument>
0026 
0027 // KF includes
0028 
0029 #include <KXMLGUIClient>
0030 
0031 // Local includes
0032 
0033 #include "libkipi_export.h"
0034 
0035 class QAction;
0036 
0037 namespace KIPI
0038 {
0039 
0040 class Interface;
0041 
0042 /**
0043  * The Category enum.
0044  */
0045 enum Category
0046 {
0047     InvalidCategory = -1,
0048     ImagesPlugin    = 0,
0049     ToolsPlugin,
0050     ImportPlugin,
0051     ExportPlugin,
0052     BatchPlugin,
0053     CollectionsPlugin
0054 };
0055 
0056 /**
0057  * @class Plugin plugin.h <KIPI/Plugin>
0058  *
0059  * @short Base class for the KIPI plugins
0060  *
0061  */
0062 class LIBKIPI_EXPORT Plugin : public QObject, public KXMLGUIClient
0063 {
0064     Q_OBJECT
0065 
0066     typedef QList<QDomElement>                        QDomElemList;
0067     typedef QHash<QString, QDomElemList>              QHashPath;
0068     typedef QMap<QWidget*, QMap<QAction*, Category> > ActionCategoryMap;
0069 
0070 public:
0071 
0072     /**
0073      * Constructs a plugin
0074      *
0075      * @param parent the parent of this object
0076      * @param name the name of the plugin
0077      */
0078     Plugin(QObject* const parent, const char* name);
0079 
0080     /**
0081      * Standard destructor
0082      *
0083      * All the actions in the actionCollection are deleted before the plugin is
0084      * deleted.
0085      */
0086     ~Plugin() override;
0087 
0088     /**
0089      * Returns the plugin actions associated with passed @p widget, or with
0090      * the default widget, if @p widget is a nullptr. The actions are in
0091      * the same order as added to the plugin.
0092      */
0093     QList<QAction*> actions(QWidget* const widget = nullptr) const;
0094 
0095     /**
0096      * Returns the KIPI::Interface
0097      */
0098     Interface* interface() const;
0099 
0100     /**
0101      * Virtual method that must be overridden by the non abstract descendants and
0102      * must be called before any actions are added.
0103      *
0104      * @param widget The widget which holds the plugin. It will be set as the default widget.
0105      */
0106     virtual void setup(QWidget* const widget) = 0;
0107 
0108     /**
0109      * Returns the category of the specified plugin action, or InvalidCategory
0110      * if the action is not recognised
0111      */
0112     Category category(QAction* const action) const;
0113 
0114     /**
0115      * Force the plugin to reread and to reload its XML file
0116      */
0117     void rebuild();
0118 
0119 protected:
0120 
0121     /**
0122      * Register an action to the plugin instance and add it to the action collection.
0123      *
0124      * The action is added only if the action name is not in the disabled actions
0125      * list of the PluginLoader singleton class.
0126      *
0127      * @param name the name by which the action will be added to the action collection
0128      * @param action the action to add
0129      *
0130      * @note It just calls addAction(const QString&, QAction* const, Category)
0131      * with the default category, so the default
0132      * category must be set using setDefaultCategory() before you use this method
0133      */
0134     void addAction(const QString& name, QAction* const action);
0135 
0136     /**
0137      * Register action to the plugin instance and add it to the action collection
0138      *
0139      * The action is added only if the action name is not in the disabled actions
0140      * list of the PluginLoader singleton class.
0141      *
0142      * @param name the name by which the action will be added to the action collection
0143      * @param action the action to add
0144      * @param cat the category of the action
0145      */
0146     void addAction(const QString& name, QAction* const action, Category cat);
0147 
0148     /**
0149      * Sets the default category of the plugin actions
0150      *
0151      * \sa defaultCategory()
0152      */
0153     void setDefaultCategory(Category cat);
0154 
0155     /**
0156      * Returns the default category of the plugin actions
0157      *
0158      * \sa setDefaultCategory()
0159      */
0160     Category defaultCategory() const;
0161 
0162     /**
0163      * Sets the name of the XML file associated with this KXMLGUIClient. You must
0164      * provide only the filename without slashes.
0165      *
0166      * The default XML file must be installed in the dir ${KDE_INSTALL_KXMLGUIDIR}/kipi,
0167      * modifications will be stored in the local config dir.
0168      *
0169      * \sa uiBaseName()
0170      */
0171     void setUiBaseName(const char* name);
0172 
0173     /**
0174      * Return the base name of the XML file associated with this KXMLGUIClient
0175      *
0176      * \sa setUiBaseName()
0177      */
0178     QString uiBaseName() const;
0179 
0180     /**
0181      * Adapt the XML file of the plugin with the one of the KXmlGuiWindow main window.
0182      * It's recommended to call it on every creation of the plugin.
0183      *
0184      * @note the XML file of the plugin must be set using setUiBaseName()
0185      */
0186     void setupXML();
0187 
0188 private:
0189 
0190     /** For internal uses only
0191       */
0192     void addAction(QAction* const action);
0193     void addAction(QAction* const action, Category cat);
0194 
0195     void mergeXMLFile(KXMLGUIClient* const host);
0196     void clearActions();
0197 
0198 private:
0199 
0200     class Private;
0201     std::unique_ptr<Private> const d;
0202 };
0203 
0204 } // namespace KIPI
0205 
0206 #endif  // KIPI_PLUGIN_H