File: rb-plugins-engine.c

package info (click to toggle)
rhythmbox 0.11.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,232 kB
  • ctags: 11,434
  • sloc: ansic: 97,824; xml: 36,080; sh: 9,341; python: 4,193; makefile: 2,130; cpp: 153
file content (718 lines) | stat: -rw-r--r-- 16,620 bytes parent folder | download
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Plugin manager for Rhythmbox, based heavily on the code from gedit.
 *
 * Copyright (C) 2002-2005 Paolo Maggi
 *               2006 James Livingston  <doclivingston@gmail.com>
 *
 * 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.
 *
 *  The Rhythmbox authors hereby grants permission for non-GPL compatible
 *  GStreamer plugins to be used and distributed together with GStreamer
 *  and Rhythmbox. This permission is above and beyond the permissions granted
 *  by the GPL license by which Rhythmbox is covered. If you modify this code
 *  you may extend this exception to your version of the code, but you are not
 *  obligated to do so. If you do not wish to do so, delete this exception
 *  statement from your 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.
 */

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

#include <string.h>

#include <glib/gi18n.h>
#include <glib/gkeyfile.h>

#include "eel-gconf-extensions.h"
#include "rb-file-helpers.h"
#include "rb-preferences.h"
#include "rb-util.h"
#include "rb-plugin.h"
#include "rb-debug.h"
#include "rb-dialog.h"

#include "rb-module.h"
#ifdef ENABLE_PYTHON
#include "rb-python-module.h"
#endif

#include "rb-plugins-engine.h"

#define PLUGIN_EXT	".rb-plugin"

typedef enum
{
	RB_PLUGIN_LOADER_C,
	RB_PLUGIN_LOADER_PY,
} RBPluginLang;

struct _RBPluginInfo
{
	gchar        *file;

	gchar        *location;
	RBPluginLang lang;
	GTypeModule  *module;

	gchar        *name;
	gchar        *desc;
	gchar        **authors;
	gchar        *copyright;
	gchar        *website;

	gchar        *icon_name;
	GdkPixbuf    *icon_pixbuf;

	RBPlugin     *plugin;

	gboolean     active;
	gboolean     visible;
	guint        active_notification_id;
	guint        visible_notification_id;
};

static void rb_plugin_info_free (RBPluginInfo *info);
static void rb_plugins_engine_plugin_active_cb (GConfClient *client,
						guint cnxn_id,
						GConfEntry *entry,
						RBPluginInfo *info);
static void rb_plugins_engine_plugin_visible_cb (GConfClient *client,
						 guint cnxn_id,
						 GConfEntry *entry,
						 RBPluginInfo *info);
static gboolean rb_plugins_engine_activate_plugin_real (RBPluginInfo *info,
							RBShell *shell);
static void rb_plugins_engine_deactivate_plugin_real (RBPluginInfo *info,
						      RBShell *shell);

static GHashTable *rb_plugins = NULL;
guint garbage_collect_id = 0;
RBShell *rb_plugins_shell = NULL;

static RBPluginInfo *
rb_plugins_engine_load (const gchar *file)
{
	RBPluginInfo *info;
	GKeyFile *plugin_file = NULL;
	gchar *str;

	g_return_val_if_fail (file != NULL, NULL);

	rb_debug ("Loading plugin: %s", file);

	info = g_new0 (RBPluginInfo, 1);
	info->file = g_strdup (file);

	plugin_file = g_key_file_new ();
	if (!g_key_file_load_from_file (plugin_file, file, G_KEY_FILE_NONE, NULL)) {
		g_warning ("Bad plugin file: %s", file);
		goto error;
	}

	if (!g_key_file_has_key (plugin_file,
			   	 "RB Plugin",
				 "IAge",
				 NULL))	{
		rb_debug ("IAge key does not exist in file: %s", file);
		goto error;
	}

	/* Check IAge=1 */
	if (g_key_file_get_integer (plugin_file,
				    "RB Plugin",
				    "IAge",
				    NULL) != 1)	{
		rb_debug ("Wrong IAge in file: %s", file);
		goto error;
	}

	/* Get Location */
	str = g_key_file_get_string (plugin_file,
				     "RB Plugin",
				     "Module",
				     NULL);
	if (str) {
		info->location = str;
	} else {
		g_warning ("Could not find 'Module' in %s", file);
		goto error;
	}

	/* Get the loader for this plugin */
	str = g_key_file_get_string (plugin_file,
				     "RB Plugin",
				     "Loader",
				     NULL);
	if (str && strcmp(str, "python") == 0) {
		info->lang = RB_PLUGIN_LOADER_PY;
#ifndef ENABLE_PYTHON
		rb_debug ("Cannot load python extension '%s', Rhythmbox was not "
					"compiled with python support", file);
		g_free (str);
		goto error;
#endif
	} else {
		info->lang = RB_PLUGIN_LOADER_C;
	}
	g_free (str);

	/* Get Name */
	str = g_key_file_get_locale_string (plugin_file,
					    "RB Plugin",
					    "Name",
					    NULL, NULL);
	if (str) {
		info->name = str;
	} else {
		g_warning ("Could not find 'Name' in %s", file);
		goto error;
	}

	/* Get Description */
	str = g_key_file_get_locale_string (plugin_file,
					    "RB Plugin",
					    "Description",
					    NULL, NULL);
	if (str) {
		info->desc = str;
	} else {
		rb_debug ("Could not find 'Description' in %s", file);
		info->desc = g_strdup ("");
	}

	/* Get icon name */
	str = g_key_file_get_string (plugin_file,
				     "RB Plugin",
				     "Icon",
				     NULL);
	if (str) {
		info->icon_name = str;
	} else {
		rb_debug ("Could not find 'Icon' in %s", file);
		info->icon_name = g_strdup ("");
	}

	/* Get Authors */
	info->authors = g_key_file_get_string_list (plugin_file,
						    "RB Plugin",
						    "Authors",
						    NULL, NULL);
	if (info->authors == NULL)
		rb_debug ("Could not find 'Authors' in %s", file);

	/* Get Copyright */
	str = g_key_file_get_string (plugin_file,
				     "RB Plugin",
				     "Copyright",
				     NULL);
	if (str) {
		info->copyright = str;
	} else {
		rb_debug ("Could not find 'Copyright' in %s", file);
		info->copyright = g_strdup ("");
	}

	/* Get Copyright */
	str = g_key_file_get_string (plugin_file,
				     "RB Plugin",
				     "Website",
				     NULL);
	if (str) {
		info->website = str;
	} else {
		rb_debug ("Could not find 'Website' in %s", file);
		info->website = g_strdup ("");
	}

	g_key_file_free (plugin_file);

	return info;

error:
	g_free (info->file);
	g_free (info->location);
	g_free (info->name);
	g_free (info);
	g_key_file_free (plugin_file);

	return NULL;
}

static gboolean
rb_plugins_engine_load_cb (const char *uri, gboolean dir, gpointer userdata)
{
	gchar *plugin_file;
	RBPluginInfo *info;
	char *key_name;
	gboolean activate;
	const char *sep;

	plugin_file = gnome_vfs_get_local_path_from_uri (uri);

	sep = strrchr (plugin_file, G_DIR_SEPARATOR);
	if (sep == NULL)
		sep = plugin_file;
	else
		sep += 1;
	
	/* don't look inside version control system directories.
	 * most are already covered by excluding hidden files/directories.
	 */
	if (dir && (g_str_has_prefix (sep, "_darcs") || g_str_has_prefix (sep, "CVS"))) {
		rb_debug ("not loading plugin from hidden/VCS directory %s", plugin_file);
		g_free (plugin_file);
		return FALSE;
	}

	if (dir || !g_str_has_suffix (uri, PLUGIN_EXT)) {
		g_free (plugin_file);
		return TRUE;
	}

	info = rb_plugins_engine_load (plugin_file);
	g_free (plugin_file);

	if (info == NULL)
		return TRUE;

	if (g_hash_table_lookup (rb_plugins, info->location)) {
		rb_plugin_info_free (info);
		return TRUE;
	}

	g_hash_table_insert (rb_plugins, info->location, info);
	rb_debug ("Plugin %s loaded", info->name);

	key_name = g_strdup_printf (CONF_PLUGIN_ACTIVE_KEY, info->location);
	info->active_notification_id = eel_gconf_notification_add (key_name,
								   (GConfClientNotifyFunc)rb_plugins_engine_plugin_active_cb,
								   info);
	activate = eel_gconf_get_boolean (key_name);
	g_free (key_name);

	key_name = g_strdup_printf (CONF_PLUGIN_HIDDEN_KEY, info->location);
	info->visible_notification_id = eel_gconf_notification_add (key_name,
								    (GConfClientNotifyFunc)rb_plugins_engine_plugin_visible_cb,
								    info);
	info->visible = !eel_gconf_get_boolean (key_name);
	g_free (key_name);

	if (activate)
		rb_plugins_engine_activate_plugin (info);
	return TRUE;	
}

static void
rb_plugins_engine_load_dir (const gchar *path)
{
	char *uri;

	uri = rb_uri_resolve_relative (path);
	rb_uri_handle_recursively (uri, rb_plugins_engine_load_cb, NULL, NULL);
	g_free (uri);
}

static void
rb_plugins_engine_load_all (void)
{
	GList *paths;

	paths = rb_get_plugin_paths ();
	while (paths != NULL) {
		rb_plugins_engine_load_dir (paths->data);
		g_free (paths->data);
		paths = g_list_delete_link (paths, paths);
	}
}

static gboolean
garbage_collect_cb (gpointer data)
{
	rb_plugins_engine_garbage_collect ();
	return TRUE;
}

gboolean
rb_plugins_engine_init (RBShell *shell)
{
	g_return_val_if_fail (rb_plugins == NULL, FALSE);

	if (!g_module_supported ())
	{
		g_warning ("rb is not able to initialize the plugins engine.");
		return FALSE;
	}
	rb_profile_start ("plugins engine init");

	rb_plugins = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify)rb_plugin_info_free);

	rb_plugins_shell = shell;
	g_object_ref (G_OBJECT (rb_plugins_shell));
#ifdef ENABLE_PYTHON
	rb_python_module_init_python ();
#endif

	rb_plugins_engine_load_all ();

	garbage_collect_id = g_timeout_add_full (G_PRIORITY_LOW, 20000, garbage_collect_cb, NULL, NULL);

	rb_profile_end ("plugins engine init");

	return TRUE;
}

void
rb_plugins_engine_garbage_collect (void)
{
#ifdef ENABLE_PYTHON
	rb_python_garbage_collect ();
#endif
}

static void
rb_plugin_info_free (RBPluginInfo *info)
{
	if (info->active)
		rb_plugins_engine_deactivate_plugin_real (info, rb_plugins_shell);

	if (info->plugin != NULL) {
	       	rb_debug ("Unref plugin %s", info->name);

		g_object_unref (info->plugin);

		/* info->module must not be unref since it is not possible to finalize
		 * a type module */
	}

	eel_gconf_notification_remove (info->active_notification_id);
	eel_gconf_notification_remove (info->visible_notification_id);

	g_free (info->file);
	g_free (info->location);
	g_free (info->name);
	g_free (info->desc);
	g_free (info->website);
	g_free (info->copyright);
	g_free (info->icon_name);

	if (info->icon_pixbuf)
		g_object_unref (info->icon_pixbuf);
	g_strfreev (info->authors);

	g_free (info);
}

void
rb_plugins_engine_shutdown (void)
{
	g_hash_table_destroy (rb_plugins);
	rb_plugins = NULL;

	g_object_unref (rb_plugins_shell);
	rb_plugins_shell = NULL;

	g_source_remove (garbage_collect_id);
	rb_plugins_engine_garbage_collect ();

#ifdef ENABLE_PYTHON
	rb_python_shutdown ();
#endif
}

GList *
rb_plugins_engine_get_plugins_list (void)
{
	return rb_collate_hash_table_values (rb_plugins);
}

static gboolean
load_plugin_module (RBPluginInfo *info)
{
	gchar *path;
	gchar *dirname;

	g_return_val_if_fail (info != NULL, FALSE);
	g_return_val_if_fail (info->file != NULL, FALSE);
	g_return_val_if_fail (info->location != NULL, FALSE);
	g_return_val_if_fail (info->plugin == NULL, FALSE);

	switch (info->lang) {
		case RB_PLUGIN_LOADER_C:
			dirname = g_path_get_dirname (info->file);
			g_return_val_if_fail (dirname != NULL, FALSE);

			path = g_module_build_path (dirname, info->location);
#ifdef SHARE_UNINSTALLED_DIR
			if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
				char *temp;

				g_free (path);
				temp = g_build_filename (dirname, ".libs", NULL);

				path = g_module_build_path (temp, info->location);
				g_free (temp);
			}
#endif

			g_free (dirname);
			g_return_val_if_fail (path != NULL, FALSE);

			info->module = G_TYPE_MODULE (rb_module_new (path, info->location));
			g_free (path);
			break;
		case RB_PLUGIN_LOADER_PY:
#ifdef ENABLE_PYTHON
			info->module = G_TYPE_MODULE (rb_python_module_new (info->file, info->location));
#else
			rb_debug ("cannot load plugin %s, python plugin support is disabled", info->location);
#endif
			break;
	}

	if (g_type_module_use (info->module) == FALSE) {
		g_warning ("Could not load plugin %s\n", info->location);

		g_object_unref (G_OBJECT (info->module));
		info->module = NULL;

		return FALSE;
	}

	switch (info->lang) {
		case RB_PLUGIN_LOADER_C:
			info->plugin = RB_PLUGIN (rb_module_new_object (RB_MODULE (info->module)));
			break;
		case RB_PLUGIN_LOADER_PY:
#ifdef ENABLE_PYTHON
			info->plugin = RB_PLUGIN (rb_python_module_new_object (RB_PYTHON_MODULE (info->module)));
#endif
			break;
	}

	return TRUE;
}

static gboolean
rb_plugins_engine_activate_plugin_real (RBPluginInfo *info, RBShell *shell)
{
	gboolean res = TRUE;

	if (info->plugin == NULL)
		res = load_plugin_module (info);

	if (res)
		rb_plugin_activate (info->plugin, shell);
	else
		g_warning ("Error, impossible to activate plugin '%s'", info->name);

	return res;
}

gboolean
rb_plugins_engine_activate_plugin (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, FALSE);

	if (info->active)
		return TRUE;

	if (rb_plugins_engine_activate_plugin_real (info, rb_plugins_shell)) {
		char *key_name;

		key_name = g_strdup_printf (CONF_PLUGIN_ACTIVE_KEY, info->location);
		eel_gconf_set_boolean (key_name, TRUE);
		g_free (key_name);

		info->active = TRUE;

		return TRUE;
	}

	rb_error_dialog (NULL, _("Plugin Error"), _("Unable to activate plugin %s"), info->name);

	return FALSE;
}

static void
rb_plugins_engine_deactivate_plugin_real (RBPluginInfo *info, RBShell *shell)
{
	rb_plugin_deactivate (info->plugin, rb_plugins_shell);
}

gboolean
rb_plugins_engine_deactivate_plugin (RBPluginInfo *info)
{
	char *key_name;

	g_return_val_if_fail (info != NULL, FALSE);

	if (!info->active)
		return TRUE;

	rb_plugins_engine_deactivate_plugin_real (info, rb_plugins_shell);

	/* Update plugin state */
	info->active = FALSE;

	key_name = g_strdup_printf (CONF_PLUGIN_ACTIVE_KEY, info->location);
	eel_gconf_set_boolean (key_name, FALSE);
	g_free (key_name);

	return TRUE;
}

gboolean
rb_plugins_engine_plugin_is_active (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, FALSE);

	return info->active;
}

gboolean
rb_plugins_engine_plugin_is_visible (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, FALSE);

	return info->visible;
}

gboolean
rb_plugins_engine_plugin_is_configurable (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, FALSE);

	if ((info->plugin == NULL) || !info->active)
		return FALSE;

	return rb_plugin_is_configurable (info->plugin);
}

void
rb_plugins_engine_configure_plugin (RBPluginInfo *info,
				       GtkWindow       *parent)
{
	GtkWidget *conf_dlg;

	GtkWindowGroup *wg;

	g_return_if_fail (info != NULL);

	conf_dlg = rb_plugin_create_configure_dialog (info->plugin);
	g_return_if_fail (conf_dlg != NULL);
	gtk_window_set_transient_for (GTK_WINDOW (conf_dlg),
				      parent);

	wg = parent->group;
	if (wg == NULL)
	{
		wg = gtk_window_group_new ();
		gtk_window_group_add_window (wg, parent);
	}

	gtk_window_group_add_window (wg,
				     GTK_WINDOW (conf_dlg));

	gtk_window_set_modal (GTK_WINDOW (conf_dlg), TRUE);
	gtk_widget_show (conf_dlg);
}

static void
rb_plugins_engine_plugin_active_cb (GConfClient *client,
				    guint cnxn_id,
				    GConfEntry *entry,
				    RBPluginInfo *info)
{
	if (gconf_value_get_bool (entry->value)) {
		rb_plugins_engine_activate_plugin (info);
	} else {
		rb_plugins_engine_deactivate_plugin (info);
	}
}

static void
rb_plugins_engine_plugin_visible_cb (GConfClient *client,
				     guint cnxn_id,
				     GConfEntry *entry,
				     RBPluginInfo *info)
{
	info->visible = !gconf_value_get_bool (entry->value);
}

const gchar *
rb_plugins_engine_get_plugin_name (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, NULL);

	return info->name;
}

const gchar *
rb_plugins_engine_get_plugin_description (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, NULL);

	return info->desc;
}

const gchar **
rb_plugins_engine_get_plugin_authors (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, (const gchar **)NULL);

	return (const gchar **)info->authors;
}

const gchar *
rb_plugins_engine_get_plugin_website (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, NULL);

	return info->website;
}

const gchar *
rb_plugins_engine_get_plugin_copyright (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, NULL);

	return info->copyright;
}

GdkPixbuf *
rb_plugins_engine_get_plugin_icon (RBPluginInfo *info)
{
	g_return_val_if_fail (info != NULL, NULL);

	if (info->icon_name == NULL)
		return NULL;

	if (info->icon_pixbuf == NULL) {
		char *filename = NULL;
		char *dirname;

		dirname = g_path_get_dirname (info->file);
		filename = g_build_filename (dirname, info->icon_name, NULL);
		g_free (dirname);

		info->icon_pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
		g_free (filename);
	}

	return info->icon_pixbuf;
}