File: bonobo-storage-plugin.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (130 lines) | stat: -rw-r--r-- 2,966 bytes parent folder | download | duplicates (2)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Author:
 *   Dietmar Maurer (dietmar@maurer-it.com)
 *
 * Copyright 2000 Maurer IT Systeml´┐Żsungen (http://www.maurer-it.com)
 */

#include <config.h>
#include <glib.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-util.h>
#include "bonobo-storage-plugin.h"

#define PLUGIN_PREFIX "libstorage_"

GList *storage_plugin_list = NULL;

static void
plugin_load (gchar *path)
{
	StoragePlugin *plugin;
	GModule *handle;
	StoragePluginInitFn init_plugin = NULL;

	if (!path) return;
        if (!(handle = g_module_open (path, G_MODULE_BIND_LAZY))) {
                g_warning ("Can't load storage plugin `%s': %s", path,
                           g_module_error ());
                return;
        }
        if (!g_module_symbol (handle, "init_storage_plugin",
                              (gpointer *) &init_plugin)) {
                g_warning ("Can't initialize storage plugin `%s': %s", path,
                           g_module_error ());
                return;
        }

	plugin = g_new0 (StoragePlugin, 1);
	plugin->handle = handle;
	plugin->filename = g_strdup (path);
	if (init_plugin (plugin) == 0) {
		storage_plugin_list = g_list_prepend (storage_plugin_list,
						      plugin);
		return;
	}

	g_module_close (plugin->handle);
	g_free (plugin->filename);
	g_free (plugin);
}

void 
bonobo_storage_load_plugins (void)
{
	DIR *dir;
	struct dirent *de;
	gchar *plugin_name, *path;
	gchar **plugin_dir;
	gchar *bonobo_plugin_path;
	gint len, i = 0;

	if (!g_module_supported ()) 
		return;

	if (storage_plugin_list) /* already loaded */
		return; 

	if ((bonobo_plugin_path = getenv ("BONOBO_PLUGIN_PATH")))
		bonobo_plugin_path = g_strconcat (bonobo_plugin_path, ":",  
						  PLUGIN_DIR, NULL);
	else 
		bonobo_plugin_path = PLUGIN_DIR;
	
	plugin_dir = g_strsplit (bonobo_plugin_path, ":", 100);

	while ((path = plugin_dir[i++])) {

		if ((dir = opendir (path)) == NULL)  
			continue;

		while ((de = readdir (dir)) != NULL){
			len = strlen (de->d_name);

			if (len > (strlen (PLUGIN_PREFIX) + 3) &&
			    strncmp (de->d_name, PLUGIN_PREFIX, 
				     strlen (PLUGIN_PREFIX)) == 0 &&
			    strncmp (de->d_name + len - 3, ".so", 3) == 0) {
				plugin_name = g_concat_dir_and_file (path, de->d_name);
				plugin_load (plugin_name);
				g_free (plugin_name);
			}
		}
		closedir (dir);
	}

	g_strfreev (plugin_dir);
}


StoragePlugin*
bonobo_storage_plugin_find (const gchar *name)
{
	GList *l;
	StoragePlugin *p;

	g_return_val_if_fail (name != NULL, NULL);

	if (!storage_plugin_list) bonobo_storage_load_plugins ();

	if (!storage_plugin_list) return NULL;

	l = storage_plugin_list;

	while (l) {
		p = (StoragePlugin *) l->data;
		if (!strcmp (p->name, name) &&
		    !strcmp (p->version, BONOBO_STORAGE_VERSION)) {
			return p;
		}
		l = l->next;
	}

	return NULL;
}