## Writing Totem Plugins

Writing Totem Plugins — brief tutorial on writing Totem plugins

## Introduction

Totem is extensible by means of small, dynamically-loadable plugins, which add functionality wanted by some but not others.

### Locations

Totem plugins can either be installed in the system path (e.g. `/usr/lib/totem/plugins/`), or in a user's home directory (e.g. `~/.local/share/totem/plugins/`). In either case, each plugin resides in a subdirectory named after the plugin itself.

In addition, each plugin needs a `.totem-plugin` index file, residing inside the plugin directory. This gives the code name of the plugin, as well as some metadata about the plugin such as its human-readable name, description and author.

Example 1. Example Plugin Directory

A system-installed plugin called `subtitle-downloader` would reside in `/usr/lib/totem/plugins/subtitle-downloader`, and would (at a minimum) have the following files:

- `subtitle-downloader.totem-plugin`
- `libsubtitle-downloader.so`

If installed in a user's home directory, it would reside in `~/.local/share/totem/plugins/subtitle-downloader` and have the same files.

### The `.totem-plugin` File

The file should use the following template:

    [Totem Plugin]
    	Module=plugin-name
    	IAge=plugin interface age (starting at 1)
    	Builtin=true or false
    	Name=Human-Readable Plugin Name
    	Description=Simple sentence describing the plugin's functionality.
    	Authors=Plugin Author Name
    	Copyright=Copyright © year Copyright Holder
    	Website=http://plugin/website/

Most of the values in the template are fairly self-explanatory. One thing to note is that the plugin name should be in lowercase, and contain no spaces. The plugin interface age should start at `1`, and only be incremented when the binary interface of the plugin (as used by Totem) changes. If the plugin does not have its own website, Totem's website (`http://projects.gnome.org/totem/`) can be used.

The library file containing the plugin's code should be named `lib`plugin-name`.so` (for C, or other compiled-language, plugins) or ``plugin-name`.py` (for Python plugins).

### Writing a Plugin

Writing a plugin in C is a matter of creating a new GObject which inherits from TotemPlugin. The following code will create a simple plugin called `foobar`:

Example 2. Example Plugin Code

    #define TOTEM_TYPE_FOOBAR_PLUGIN		(totem_foobar_plugin_get_type ())
    #define TOTEM_FOOBAR_PLUGIN(o)			(G_TYPE_CHECK_INSTANCE_CAST ((o), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPlugin))
    #define TOTEM_FOOBAR_PLUGIN_CLASS(k)		(G_TYPE_CHECK_CLASS_CAST((k), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPluginClass))
    #define TOTEM_IS_FOOBAR_PLUGIN(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), TOTEM_TYPE_FOOBAR_PLUGIN))
    #define TOTEM_IS_FOOBAR_PLUGIN_CLASS(k)		(G_TYPE_CHECK_CLASS_TYPE ((k), TOTEM_TYPE_FOOBAR_PLUGIN))
    #define TOTEM_FOOBAR_PLUGIN_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPluginClass))

    typedef struct {
    	TotemPlugin parent;
    	/* plugin object members */
    } TotemFoobarPlugin;

    typedef struct {
    	TotemPluginClass parent_class;
    } TotemFoobarPluginClass;

    G_MODULE_EXPORT GType register_totem_plugin (GTypeModule *module);
    GType totem_foobar_plugin_get_type (void) G_GNUC_CONST;

    static gboolean impl_activate (TotemPlugin *plugin, TotemObject *totem, GError **error);
    static void impl_deactivate (TotemPlugin *plugin, TotemObject *totem);

    TOTEM_PLUGIN_REGISTER (TotemFoobarPlugin, totem_foobar_plugin)

    static void
    totem_foobar_plugin_class_init (TotemFoobarPluginClass *klass)
    {
    	TotemPluginClass *plugin_class = TOTEM_PLUGIN_CLASS (klass);

    	plugin_class->activate = impl_activate;
    	plugin_class->deactivate = impl_deactivate;
    }

    static void
    totem_foobar_plugin_init (TotemFoobarPlugin *plugin)
    {
    	/* Initialise resources, but only ones which should exist for the entire lifetime of Totem;
    	 * those which should only exist for the lifetime of the plugin (which may be short, and may
    	 * occur several times during one Totem session) should be created in impl_activate, and destroyed
    	 * in impl_deactivate. */
    }

    static gboolean
    impl_activate (TotemPlugin *plugin, TotemObject *totem, GError **error)
    {
    	TotemFoobarPlugin *self = TOTEM_FOOBAR_PLUGIN (plugin);

    	/* Initialise resources, connect to events, create menu items and UI, etc., here.
    	 * Note that impl_activate and impl_deactivate can be called multiple times in one
    	 * Totem instance, though impl_activate will always be followed by impl_deactivate before
    	 * it is called again. Similarly, impl_deactivate cannot be called twice in succession. */

    	return TRUE;
    }

    static void
    impl_deactivate	(TotemPlugin *plugin, TotemObject *totem)
    {
    	TotemFoobarPlugin *self = TOTEM_FOOBAR_PLUGIN (plugin);

    	/* Destroy resources created in impl_activate here. e.g. Disconnect from signals
    	 * and remove menu entries and UI. */
    }

Once resources have been created, and the plugin has been connected to Totem's UI in the `impl_activate` function, the plugin is free to go about its tasks as appropriate. If the user deactivates the plugin, or Totem decides to deactivate it, the `impl_deactivate` will be called. The plugin should free any resources grabbed or allocated in the `impl_activate` function, and remove itself from the Totem interface.

Note that plugins can be activated and deactivated (e.g. from Totem's plugin manager) many times during one Totem session, so the `impl_activate` and `impl_deactivate` functions must be able to cope with this.

Any of the API documented in the rest of the Totem API reference can be used by plugins, though the bindings for Python plugins are incomplete. Otherwise, Python plugins are written in the same way as C plugins, and are similarly implemented as classes derived from TotemPlugin.