File indexing completed on 2024-05-19 05:21:25

0001 /*
0002   This file is part of the KDE Kontact Plugin Interface Library.
0003 
0004   SPDX-FileCopyrightText: 2001 Matthias Hoelzer-Kluepfel <mhk@kde.org>
0005   SPDX-FileCopyrightText: 2002-2003 Daniel Molkentin <molkentin@kde.org>
0006   SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
0007 
0008   SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #pragma once
0012 
0013 #include "kontactinterface_export.h"
0014 
0015 #include <KPluginFactory>
0016 #include <KXMLGUIClient>
0017 
0018 #include <QList>
0019 #include <QObject>
0020 #include <QStringList>
0021 
0022 class KAboutData;
0023 class QAction;
0024 class KConfig;
0025 class KConfigGroup;
0026 class QDropEvent;
0027 class QMimeData;
0028 class QWidget;
0029 namespace KParts
0030 {
0031 class Part;
0032 }
0033 
0034 /**
0035   Increase this version number whenever you make a change in the API.
0036  */
0037 #define KONTACT_PLUGIN_VERSION 11
0038 
0039 /**
0040   Exports Kontact plugin.
0041   @param pluginclass the class to instantiate (must derive from KontactInterface::Plugin)
0042   @param jsonFile filename of the JSON file, generated from a .desktop file
0043  */
0044 #define EXPORT_KONTACT_PLUGIN_WITH_JSON(pluginclass, jsonFile)                                                                                                 \
0045     class Instance                                                                                                                                             \
0046     {                                                                                                                                                          \
0047     public:                                                                                                                                                    \
0048         static QObject *createInstance(QWidget *, QObject *parent, const KPluginMetaData &data, const QVariantList &list)                                      \
0049         {                                                                                                                                                      \
0050             return new pluginclass(static_cast<KontactInterface::Core *>(parent), data, list);                                                                 \
0051         }                                                                                                                                                      \
0052     };                                                                                                                                                         \
0053     K_PLUGIN_FACTORY_WITH_JSON(KontactPluginFactory, jsonFile, registerPlugin<pluginclass>(Instance::createInstance);)
0054 
0055 namespace KontactInterface
0056 {
0057 class Core;
0058 class Summary;
0059 /**
0060  * @short Base class for all Plugins in Kontact.
0061  *
0062  * Inherit from it to get a plugin. It can insert an icon into the sidepane,
0063  * add widgets to the widgetstack and add menu items via XMLGUI.
0064  */
0065 class KONTACTINTERFACE_EXPORT Plugin : public QObject, virtual public KXMLGUIClient
0066 {
0067     Q_OBJECT
0068 
0069 public:
0070     /**
0071      * Creates a new plugin.
0072      *
0073      * @param core The core object that manages the plugin.
0074      * @param parent The parent object.
0075      * @param appName The name of the application that
0076      *       provides the part. This is the name used for DBus registration.
0077      *       It's ok to have several plugins using the same application name.
0078      * @param pluginName The unique name of the plugin. Defaults to appName if not set.
0079      */
0080     Plugin(Core *core, QObject *parent, const KPluginMetaData &data, const char *appName, const char *pluginName = nullptr);
0081     /**
0082      * Destroys the plugin.
0083      */
0084     ~Plugin() override;
0085 
0086     /**
0087      * Sets the @p identifier of the plugin.
0088      */
0089     void setIdentifier(const QString &identifier);
0090 
0091     /**
0092      * Returns the identifier of the plugin.
0093      */
0094     [[nodiscard]] QString identifier() const;
0095 
0096     /**
0097      * Sets the localized @p title of the plugin.
0098      */
0099     void setTitle(const QString &title);
0100 
0101     /**
0102      * Returns the localized title of the plugin.
0103      */
0104     [[nodiscard]] QString title() const;
0105 
0106     /**
0107      * Sets the @p icon name that is used for the plugin.
0108      */
0109     void setIcon(const QString &icon);
0110 
0111     /**
0112      * Returns the icon name that is used for the plugin.
0113      */
0114     [[nodiscard]] QString icon() const;
0115 
0116     /**
0117      * Sets the @p name of executable (if existent).
0118      */
0119     void setExecutableName(const QString &name);
0120 
0121     /**
0122      * Returns the name of the executable (if existent).
0123      */
0124     [[nodiscard]] QString executableName() const;
0125 
0126     /**
0127      * Set @p name of library which contains the KPart used by this plugin.
0128      */
0129     void setPartLibraryName(const QByteArray &name);
0130 
0131     /**
0132      * Reimplement this method and return whether a standalone application
0133      * is still running. This is only required if your part is also available
0134      * as standalone application.
0135      */
0136     [[nodiscard]] virtual bool isRunningStandalone() const;
0137 
0138     /**
0139      * Reimplement this method if your application needs a different approach to be brought
0140      * in the foreground. The default behaviour is calling the binary.
0141      * This is only required if your part is also available as standalone application.
0142      */
0143     virtual void bringToForeground();
0144 
0145     /**
0146      * Reimplement this method if you want to add your credits to the Kontact
0147      * about dialog.
0148      */
0149     [[nodiscard]] virtual const KAboutData aboutData();
0150 
0151     /**
0152      * You can use this method if you need to access the current part. You can be
0153      * sure that you always get the same pointer as long as the part has not been
0154      * deleted.
0155      */
0156     [[nodiscard]] KParts::Part *part();
0157 
0158     /**
0159      * This function is called when the plugin is selected by the user before the
0160      * widget of the KPart belonging to the plugin is raised.
0161      */
0162     virtual void select();
0163 
0164     /**
0165      * Called by kontact when the plugin is selected by the user.
0166      * Calls the virtual method select(), but also handles some standard behavior
0167      * like "invisible toolbar actions".
0168      */
0169     void aboutToSelect();
0170 
0171     /**
0172      * This function is called whenever the config dialog has been closed
0173      * successfully.
0174      */
0175     virtual void configUpdated();
0176 
0177     /**
0178      * Reimplement this method if you want to add a widget for your application
0179      * to Kontact's summary page.
0180      *
0181      * @param parent The parent widget of the summary widget.
0182      */
0183     [[nodiscard]] virtual Summary *createSummaryWidget(QWidget *parent);
0184 
0185     /**
0186      * Returns whether the plugin provides a part that should be shown in the sidebar.
0187      */
0188     [[nodiscard]] virtual bool showInSideBar() const;
0189 
0190     /**
0191      * Set if the plugin provides a part that should be shown in the sidebar.
0192      * @param hasPart shows part in sidebar if set as @c true
0193      */
0194     void setShowInSideBar(bool hasPart);
0195 
0196     /**
0197      * Reimplement this method if you want to add checks before closing the
0198      * main kontact window. Return true if it's OK to close the window.
0199      * If any loaded plugin returns false from this method, then the
0200      * main kontact window will not close.
0201      */
0202     [[nodiscard]] virtual bool queryClose() const;
0203 
0204     /**
0205      * Registers the client at DBus and returns the dbus identifier.
0206      */
0207     QString registerClient();
0208 
0209     /**
0210      * Return the weight of the plugin. The higher the weight the lower it will
0211      * be displayed in the sidebar. The default implementation returns 0.
0212      */
0213     virtual int weight() const;
0214 
0215     /**
0216      * Inserts a custom "New" @p action.
0217      * @param action the new action to insert
0218      */
0219     void insertNewAction(QAction *action);
0220 
0221     /**
0222      * Inserts a custom "Sync" @p action.
0223      * @param action the custom Sync action to insert
0224      */
0225     void insertSyncAction(QAction *action);
0226 
0227     /**
0228      * Returns the list of custom "New" actions.
0229      */
0230     [[nodiscard]] QList<QAction *> newActions() const;
0231 
0232     /**
0233      * Returns the list of custom "Sync" actions.
0234      */
0235     [[nodiscard]] QList<QAction *> syncActions() const;
0236 
0237     /**
0238      * Returns a list of action names that shall be hidden in the main toolbar.
0239      */
0240     [[nodiscard]] virtual QStringList invisibleToolbarActions() const;
0241 
0242     /**
0243      * Returns whether the plugin can handle the drag object of the given mime type.
0244      */
0245     [[nodiscard]] virtual bool canDecodeMimeData(const QMimeData *data) const;
0246 
0247     /**
0248      * Process drop event.
0249      */
0250     virtual void processDropEvent(QDropEvent *);
0251 
0252     /**
0253      * Session management: read properties
0254      */
0255     virtual void readProperties(const KConfigGroup &);
0256 
0257     /**
0258      * Session management: save properties
0259      */
0260     virtual void saveProperties(KConfigGroup &);
0261 
0262     /**
0263      * Returns a pointer to the kontact core object.
0264      */
0265     [[nodiscard]] Core *core() const;
0266 
0267     /**
0268      * Sets whether the plugin shall be disabled.
0269      */
0270     void setDisabled(bool value);
0271 
0272     /**
0273      * Returns whether the plugin is disabled.
0274      */
0275     [[nodiscard]] bool disabled() const;
0276 
0277     /**
0278      * @since 4.13
0279      */
0280     virtual void shortcutChanged();
0281 
0282 public Q_SLOTS:
0283     /**
0284      * @internal usage
0285      *
0286      * This slot is called whenever the configuration has been changed.
0287      */
0288     void slotConfigUpdated();
0289 
0290 protected:
0291     /**
0292      * Reimplement and return the part here. Reimplementing createPart() is
0293      * mandatory!
0294      */
0295     virtual KParts::Part *createPart() = 0;
0296 
0297     /**
0298      * Returns the loaded part.
0299      */
0300     KParts::Part *loadPart();
0301 
0302     /**
0303      * Virtual hook for BC extension.
0304      */
0305     void virtual_hook(int id, void *data) override;
0306 
0307 private:
0308     //@cond PRIVATE
0309     class PluginPrivate;
0310     std::unique_ptr<PluginPrivate> const d;
0311     //@endcond
0312 };
0313 
0314 }