This macro simply unifies two KDE macros that allow us the exportation of the plugin.

This macro simply unifies two KDE macros that allow us the exportation of the plugin.

−

== The plugin loading stuff ==

+

== Loading Plugins ==

Now let's get prepared to make our application find and load every compatible plugin. We'll make use of a simple helper that uses standard KDE methods in order to locate and load plugins. Here follows header and implementation

Now let's get prepared to make our application find and load every compatible plugin. We'll make use of a simple helper that uses standard KDE methods in order to locate and load plugins. Here follows header and implementation

Abstract

Before starting with the code let's see which advantages we can undertake
developing a plugin structured application. First of all an application capable
of managing plugins can extend its features with apparently no limits. Anyway, rather than developing a plugin structured application because "it's so coool!!" the developer should think about the real advantages of this kind of development. For instance we can think about an archive manager application: the main program can delegate the creation/extraction/editing of each type of archive to specific plugins. This way we give modularity to the application so that supporting brand new archive types in the future will simply consist in writing the specific plugin and not rewriting the whole application. Ok, then let's stop talking.. and start coding =).

The Text Editor Example

So, even if an archiving application would be nicer to look at, here we will examine a simple text editing application capable of plugin loading.
Each plugin will simply give a specific text editing feature.

Creating a Plugin Interface Class

The main step in order to create a plugin structure is to define its base class.
This class will be inherited by every plugin and should give access to the key resources of the main application. In this example the main plugin class will inherit a KXMLGUIClient since every plugin will give access to its features through a gui element (a KAction in this case).

This base class will give the useful pointers needed by each plugin in order to manipulate the main application data. In this case we only give access to the main editor interface through editorInterface().

Where plugin.h is the header file seen before.
The setEditorInterface allows the main application to set the pointer to the main KTextEditor. This way each programmer will be able to access the main text editing interface and interact with it as well.

The Plugin Factory Macro

Every plugin will be made "visible and available" through the use of a macro.
So we better put this macro in our own myplugin_macros.h as follows.

This macro simply unifies two KDE macros that allow us the exportation of the plugin.

Loading Plugins

Now let's get prepared to make our application find and load every compatible plugin. We'll make use of a simple helper that uses standard KDE methods in order to locate and load plugins. Here follows header and implementation

The code shown simply queries SyCoCa for a plugin for the TextEditor. When creating a plugin a .desktop file is provided with the Service Type the plugin is designed for. We'll see later how to set the proper Service Type for our custom plugin. We emit the pluginLoaded signal when the plugin is properly loaded so that the application will integrate it in the GUI.

Handling plugins by the Main Application

I'll just show pieces of code that handle plugin loading in the main window.
Let's assume we have a pointer to our KTextEdit simply called editor.

So with this piece of code we ask PluginLoader to load all plugins and call the slot pluginLoaded we'll see now to implement the plugins in the interface.
The loadPlugins method is supposed to be called at the beginning of the application in order to load everything before the user can start using it. Of course you can decide what to do with your plugins and ignore this suggestion :).

Since our plugin inherits KXMLGuiClient we can provide any kind of gui client supported and KDE will automagically load it into our application. So this code snippet simply integrates our plugin with the main window.