1
<refentry id="totem-plugins">
3
<refentrytitle role="top_of_page" id="totem-plugins.top_of_page">Writing Totem Plugins</refentrytitle>
4
<manvolnum>3</manvolnum>
5
<refmiscinfo>Totem</refmiscinfo>
8
<refname>Writing Totem Plugins</refname>
9
<refpurpose>brief tutorial on writing Totem plugins</refpurpose>
13
<title>Introduction</title>
14
<para>Totem is extensible by means of small, dynamically-loadable plugins, which add functionality wanted by some but not others.</para>
17
<title>Locations</title>
18
<para>Totem plugins can either be installed in the system path
19
(e.g. <filename class="directory">/usr/lib/totem/plugins/</filename>), or in a user's home directory
20
(e.g. <filename class="directory">~/.local/share/totem/plugins/</filename>). In either case, each plugin resides in a
21
subdirectory named after the plugin itself.</para>
22
<para>In addition, each plugin needs a <filename class="extension">.totem-plugin</filename> index file, residing inside the plugin
23
directory. This gives the code name of the plugin, as well as some metadata about the plugin such as its human-readable
24
name, description and author.</para>
26
<title>Example Plugin Directory</title>
27
<para>A system-installed plugin called <literal>subtitle-downloader</literal> would reside in
28
<filename class="directory">/usr/lib/totem/plugins/subtitle-downloader</filename>, and would (at a
29
minimum) have the following files:
31
<listitem><filename>subtitle-downloader.totem-plugin</filename></listitem>
32
<listitem><filename>libsubtitle-downloader.so</filename></listitem>
35
<para>If installed in a user's home directory, it would reside in
36
<filename class="extension">~/.local/share/totem/plugins/subtitle-downloader</filename> and have the same
42
<title>The <filename class="extension">.totem-plugin</filename> File</title>
43
<para>The file should use the following template:
44
<programlisting>[Totem Plugin]
45
Module=<replaceable>plugin-name</replaceable>
46
IAge=<replaceable>plugin interface age (starting at 1)</replaceable>
47
Builtin=<replaceable><literal>true</literal> or <literal>false</literal></replaceable>
48
Name=<replaceable>Human-Readable Plugin Name</replaceable>
49
Description=<replaceable>Simple sentence describing the plugin's functionality.</replaceable>
50
Authors=<replaceable>Plugin Author Name</replaceable>
51
Copyright=Copyright © <replaceable>year</replaceable> <replaceable>Copyright Holder</replaceable>
52
Website=<replaceable>http://plugin/website/</replaceable></programlisting>
53
Most of the values in the template are fairly self-explanatory. One thing to note is that the plugin name should be
54
in lowercase, and contain no spaces. The plugin interface age should start at <literal>1</literal>, and only be
55
incremented when the binary interface of the plugin (as used by Totem) changes. If the plugin does not have its own
56
website, Totem's website (<literal>http://projects.gnome.org/totem/</literal>) can be used.</para>
57
<para>The library file containing the plugin's code should be named
58
<filename>lib<replaceable>plugin-name</replaceable>.so</filename> (for C, or other compiled-language, plugins) or
59
<filename><replaceable>plugin-name</replaceable>.py</filename> (for Python plugins).</para>
63
<title>Writing a Plugin</title>
64
<para>Writing a plugin in C is a matter of creating a new <type><link linkend="GObject">GObject</link></type> which inherits
65
from <type><link linkend="TotemPlugin">TotemPlugin</link></type>. The following code will create a simple plugin
66
called <literal>foobar</literal>:
68
<title>Example Plugin Code</title>
70
#define TOTEM_TYPE_FOOBAR_PLUGIN (totem_foobar_plugin_get_type ())
71
#define TOTEM_FOOBAR_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPlugin))
72
#define TOTEM_FOOBAR_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPluginClass))
73
#define TOTEM_IS_FOOBAR_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), TOTEM_TYPE_FOOBAR_PLUGIN))
74
#define TOTEM_IS_FOOBAR_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), TOTEM_TYPE_FOOBAR_PLUGIN))
75
#define TOTEM_FOOBAR_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPluginClass))
79
/* plugin object members */
83
TotemPluginClass parent_class;
84
} TotemFoobarPluginClass;
86
G_MODULE_EXPORT GType register_totem_plugin (GTypeModule *module);
87
GType totem_foobar_plugin_get_type (void) G_GNUC_CONST;
89
static gboolean impl_activate (TotemPlugin *plugin, TotemObject *totem, GError **error);
90
static void impl_deactivate (TotemPlugin *plugin, TotemObject *totem);
92
TOTEM_PLUGIN_REGISTER (TotemFoobarPlugin, totem_foobar_plugin)
95
totem_foobar_plugin_class_init (TotemFoobarPluginClass *klass)
97
TotemPluginClass *plugin_class = TOTEM_PLUGIN_CLASS (klass);
99
plugin_class->activate = impl_activate;
100
plugin_class->deactivate = impl_deactivate;
104
totem_foobar_plugin_init (TotemFoobarPlugin *plugin)
106
/* Initialise resources, but only ones which should exist for the entire lifetime of Totem;
107
* those which should only exist for the lifetime of the plugin (which may be short, and may
108
* occur several times during one Totem session) should be created in impl_activate, and destroyed
109
* in impl_deactivate. */
113
impl_activate (TotemPlugin *plugin, TotemObject *totem, GError **error)
115
TotemFoobarPlugin *self = TOTEM_FOOBAR_PLUGIN (plugin);
117
/* Initialise resources, connect to events, create menu items and UI, etc., here.
118
* Note that impl_activate and impl_deactivate can be called multiple times in one
119
* Totem instance, though impl_activate will always be followed by impl_deactivate before
120
* it is called again. Similarly, impl_deactivate cannot be called twice in succession. */
126
impl_deactivate (TotemPlugin *plugin, TotemObject *totem)
128
TotemFoobarPlugin *self = TOTEM_FOOBAR_PLUGIN (plugin);
130
/* Destroy resources created in impl_activate here. e.g. Disconnect from signals
131
* and remove menu entries and UI. */
134
<para>Once resources have been created, and the plugin has been connected to Totem's UI in the <function>impl_activate</function>
135
function, the plugin is free to go about its tasks as appropriate. If the user deactivates the plugin, or Totem decides
136
to deactivate it, the <function>impl_deactivate</function> will be called. The plugin should free any resources
137
grabbed or allocated in the <function>impl_activate</function> function, and remove itself from the Totem
139
<para>Note that plugins can be activated and deactivated (e.g. from Totem's plugin manager) many times during one Totem session,
140
so the <function>impl_activate</function> and <function>impl_deactivate</function> functions must be able to cope with
142
<para>Any of the API documented in the rest of the Totem API reference can be used by plugins, though the bindings for Python
143
plugins are incomplete. Otherwise, Python plugins are written in the same way as C plugins, and are similarly implemented
144
as classes derived from <type><link linkend="TotemPlugin">TotemPlugin</link></type>.</para>