summaryrefslogtreecommitdiff
path: root/src/plugins/totem-plugin.c
blob: 75cc7f92eb6841f5f7b96dd55f4f3eb98a1a9ffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * heavily based on code from Rhythmbox and Gedit
 *
 * Copyright (C) 2002-2005 Paolo Maggi
 * Copyright (C) 2007 Bastien Nocera <hadess@hadess.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301  USA.
 *
 * Sunday 13th May 2007: Bastien Nocera: Add exception clause.
 * See license_change file for details.
 *
 */

/**
 * SECTION:totem-plugin
 * @short_description: base plugin class and loading/unloading functions
 * @stability: Unstable
 * @include: totem-plugin.h
 *
 * #TotemPlugin is a general-purpose architecture for adding plugins to Totem, with
 * derived support for different programming languages.
 **/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <gconf/gconf-client.h>

#include "totem-plugin.h"
#include "totem-uri.h"
#include "totem-interface.h"

G_DEFINE_TYPE (TotemPlugin, totem_plugin, G_TYPE_OBJECT)

#define TOTEM_PLUGIN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TOTEM_TYPE_PLUGIN, TotemPluginPrivate))

static void totem_plugin_finalise (GObject *o);
static void totem_plugin_set_property (GObject *object,
				       guint prop_id,
				       const GValue *value,
				       GParamSpec *pspec);
static void totem_plugin_get_property (GObject *object,
				       guint prop_id,
				       GValue *value,
				       GParamSpec *pspec);

struct TotemPluginPrivate {
	char *name;
};

enum
{
	PROP_0,
	PROP_NAME
};

GQuark
totem_plugin_error_quark (void)
{
	static GQuark quark;

	if (!quark)
		quark = g_quark_from_static_string ("totem_plugin_error");

	return quark;
}

static gboolean
is_configurable (TotemPlugin *plugin)
{
	return (TOTEM_PLUGIN_GET_CLASS (plugin)->create_configure_dialog != NULL);
}

static void
totem_plugin_class_init (TotemPluginClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = totem_plugin_finalise;
	object_class->get_property = totem_plugin_get_property;
	object_class->set_property = totem_plugin_set_property;

	klass->activate = NULL;
	klass->deactivate = NULL;
	klass->create_configure_dialog = NULL;
	klass->is_configurable = is_configurable;

	/* FIXME: this should be a construction property, but due to the python plugin hack can't be */
	/**
	 * TotemPlugin:name:
	 *
	 * The plugin's name. It should be a construction property, but due to the Python plugin hack, it
	 * can't be: do not change the name after construction. Should be the same as used for naming plugin-
	 * specific resources.
	 **/
	g_object_class_install_property (object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
							      "name",
							      "name",
							      NULL,
							      G_PARAM_READWRITE /*| G_PARAM_CONSTRUCT_ONLY*/));

	g_type_class_add_private (klass, sizeof (TotemPluginPrivate));
}

static void
totem_plugin_init (TotemPlugin *plugin)
{
	/* Empty */
}

static void
totem_plugin_finalise (GObject *object)
{
	TotemPluginPrivate *priv = TOTEM_PLUGIN_GET_PRIVATE (object);

	g_free (priv->name);

	G_OBJECT_CLASS (totem_plugin_parent_class)->finalize (object);
}

static void
totem_plugin_set_property (GObject *object,
			   guint prop_id,
			   const GValue *value,
			   GParamSpec *pspec)
{
	TotemPluginPrivate *priv = TOTEM_PLUGIN_GET_PRIVATE (object);

	switch (prop_id)
	{
	case PROP_NAME:
		g_free (priv->name);
		priv->name = g_value_dup_string (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
totem_plugin_get_property (GObject *object,
			   guint prop_id,
			   GValue *value,
			   GParamSpec *pspec)
{
	TotemPluginPrivate *priv = TOTEM_PLUGIN_GET_PRIVATE (object);

	switch (prop_id)
	{
	case PROP_NAME:
		g_value_set_string (value, priv->name);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

/**
 * totem_plugin_activate:
 * @plugin: a #TotemPlugin
 * @totem: a #TotemObject
 * @error: return location for a #GError, or %NULL
 *
 * Activates the passed @plugin by calling its activate method.
 *
 * Return value: %TRUE on success
 **/
gboolean
totem_plugin_activate (TotemPlugin *plugin,
		       TotemObject *totem,
		       GError **error)
{
	g_return_val_if_fail (TOTEM_IS_PLUGIN (plugin), FALSE);
	g_return_val_if_fail (TOTEM_IS_OBJECT (totem), FALSE);

	if (TOTEM_PLUGIN_GET_CLASS (plugin)->activate)
		return TOTEM_PLUGIN_GET_CLASS (plugin)->activate (plugin, totem, error);

	return TRUE;
}

/**
 * totem_plugin_deactivate:
 * @plugin: a #TotemPlugin
 * @totem: a #TotemObject
 *
 * Deactivates @plugin by calling its deactivate method.
 **/
void
totem_plugin_deactivate	(TotemPlugin *plugin,
			 TotemObject *totem)
{
	g_return_if_fail (TOTEM_IS_PLUGIN (plugin));
	g_return_if_fail (TOTEM_IS_OBJECT (totem));

	if (TOTEM_PLUGIN_GET_CLASS (plugin)->deactivate)
		TOTEM_PLUGIN_GET_CLASS (plugin)->deactivate (plugin, totem);
}

/**
 * totem_plugin_is_configurable:
 * @plugin: a #TotemPlugin
 *
 * Returns %TRUE if the plugin is configurable and has a
 * configuration dialog. It calls the plugin's
 * is_configurable method.
 *
 * Return value: %TRUE if the plugin is configurable
 **/
gboolean
totem_plugin_is_configurable (TotemPlugin *plugin)
{
	g_return_val_if_fail (TOTEM_IS_PLUGIN (plugin), FALSE);

	return TOTEM_PLUGIN_GET_CLASS (plugin)->is_configurable (plugin);
}

/**
 * totem_plugin_create_configure_dialog:
 * @plugin: a #TotemPlugin
 *
 * Returns the plugin's configuration dialog, as created by
 * the plugin's create_configure_dialog method.
 *
 * Return value: the configuration dialog, or %NULL
 **/
GtkWidget *
totem_plugin_create_configure_dialog (TotemPlugin *plugin)
{
	g_return_val_if_fail (TOTEM_IS_PLUGIN (plugin), NULL);

	if (TOTEM_PLUGIN_GET_CLASS (plugin)->create_configure_dialog)
		return TOTEM_PLUGIN_GET_CLASS (plugin)->create_configure_dialog (plugin);
	else
		return NULL;
}

#define UNINSTALLED_PLUGINS_LOCATION "plugins"

GList *
totem_get_plugin_paths (void)
{
	GList *paths;
	char  *path;
	GConfClient *client;

	paths = NULL;

	client = gconf_client_get_default ();
	if (gconf_client_get_bool (client, GCONF_PREFIX"/disable_user_plugins", NULL) == FALSE) {
		path = g_build_filename (totem_data_dot_dir (), "plugins", NULL);
		paths = g_list_prepend (paths, path);
	}

#ifdef TOTEM_RUN_IN_SOURCE_TREE
	path = g_build_filename (UNINSTALLED_PLUGINS_LOCATION, NULL);
	paths = g_list_prepend (paths, path);
#endif

	path = g_strdup (TOTEM_PLUGIN_DIR);
	paths = g_list_prepend (paths, path);

	paths = g_list_reverse (paths);

	return paths;
}

/**
 * totem_plugin_find_file:
 * @plugin: a #TotemPlugin
 * @file: the file to find
 *
 * Finds the specified @file by looking in the plugin paths
 * listed by totem_get_plugin_paths() and then in the system
 * Totem data directory.
 *
 * This should be used by plugins to find plugin-specific
 * resource files.
 *
 * Return value: a newly-allocated absolute path for the file, or %NULL
 **/
char *
totem_plugin_find_file (TotemPlugin *plugin,
			const char *file)
{
	TotemPluginPrivate *priv = TOTEM_PLUGIN_GET_PRIVATE (plugin);
	GList *paths;
	GList *l;
	char *ret = NULL;

	paths = totem_get_plugin_paths ();

	for (l = paths; l != NULL; l = l->next) {
		if (ret == NULL && priv->name) {
			char *tmp;

			tmp = g_build_filename (l->data, priv->name, file, NULL);

			if (g_file_test (tmp, G_FILE_TEST_EXISTS)) {
				ret = tmp;
				break;
			}
			g_free (tmp);
		}
	}

	g_list_foreach (paths, (GFunc)g_free, NULL);
	g_list_free (paths);

	/* global data files */
	if (ret == NULL)
		ret = totem_interface_get_full_path (file);

	/* ensure it's an absolute path, so doesn't confuse rb_glade_new et al */
	if (ret && ret[0] != '/') {
		char *pwd = g_get_current_dir ();
		char *path = g_strconcat (pwd, G_DIR_SEPARATOR_S, ret, NULL);
		g_free (ret);
		g_free (pwd);
		ret = path;
	}
	return ret;
}

/**
 * totem_plugin_load_interface:
 * @plugin: a #TotemPlugin
 * @name: interface filename
 * @fatal: %TRUE if it's a fatal error if the interface can't be loaded
 * @parent: the interface's parent #GtkWindow
 * @user_data: a pointer to be passed to each signal handler in the interface when they're called
 *
 * Loads an interface file (GtkBuilder UI file) for a plugin, given its filename and
 * assuming it's installed in the plugin's data directory.
 *
 * This should be used instead of attempting to load interfaces manually in plugins.
 *
 * Return value: the #GtkBuilder instance for the interface
 **/
GtkBuilder *
totem_plugin_load_interface (TotemPlugin *plugin, const char *name,
			     gboolean fatal, GtkWindow *parent,
			     gpointer user_data)
{
	GtkBuilder *builder = NULL;
	char *filename;

	filename = totem_plugin_find_file (plugin, name);
	builder = totem_interface_load_with_full_path (filename, fatal, parent,
						       user_data);
	g_free (filename);

	return builder;
}