source: trunk/third/bonobo/bonobo/bonobo-storage-plugin.c @ 15509

Revision 15509, 2.9 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15508, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2/*
3 * Author:
4 *   Dietmar Maurer (dietmar@maurer-it.com)
5 *
6 * Copyright 2000 Maurer IT Systemlösungen (http://www.maurer-it.com)
7 */
8
9#include <config.h>
10#include <glib.h>
11#include <sys/types.h>
12#include <dirent.h>
13#include <string.h>
14#include <stdlib.h>
15
16#include <libgnome/gnome-defs.h>
17#include <libgnome/gnome-util.h>
18#include "bonobo-storage-plugin.h"
19
20#define PLUGIN_PREFIX "libstorage_"
21
22GList *storage_plugin_list = NULL;
23
24static void
25plugin_load (gchar *path)
26{
27        StoragePlugin *plugin;
28        GModule *handle;
29        StoragePluginInitFn init_plugin = NULL;
30
31        if (!path) return;
32        if (!(handle = g_module_open (path, G_MODULE_BIND_LAZY))) {
33                g_warning ("Can't load storage plugin `%s': %s", path,
34                           g_module_error ());
35                return;
36        }
37        if (!g_module_symbol (handle, "init_storage_plugin",
38                              (gpointer *) &init_plugin)) {
39                g_warning ("Can't initialize storage plugin `%s': %s", path,
40                           g_module_error ());
41                return;
42        }
43
44        plugin = g_new0 (StoragePlugin, 1);
45        plugin->handle = handle;
46        plugin->filename = g_strdup (path);
47        if (init_plugin (plugin) == 0) {
48                storage_plugin_list = g_list_prepend (storage_plugin_list,
49                                                      plugin);
50                return;
51        }
52
53        g_module_close (plugin->handle);
54        g_free (plugin->filename);
55        g_free (plugin);
56}
57
58void
59bonobo_storage_load_plugins (void)
60{
61        DIR *dir;
62        struct dirent *de;
63        gchar *plugin_name, *path;
64        gchar **plugin_dir;
65        gchar *bonobo_plugin_path;
66        gint len, i = 0;
67
68        if (!g_module_supported ())
69                return;
70
71        if (storage_plugin_list) /* already loaded */
72                return;
73
74        if ((bonobo_plugin_path = getenv ("BONOBO_PLUGIN_PATH")))
75                bonobo_plugin_path = g_strconcat (bonobo_plugin_path, ":", 
76                                                  PLUGIN_DIR, NULL);
77        else
78                bonobo_plugin_path = PLUGIN_DIR;
79       
80        plugin_dir = g_strsplit (bonobo_plugin_path, ":", 100);
81
82        while ((path = plugin_dir[i++])) {
83
84                if ((dir = opendir (path)) == NULL) 
85                        continue;
86
87                while ((de = readdir (dir)) != NULL){
88                        len = strlen (de->d_name);
89
90                        if (len > (strlen (PLUGIN_PREFIX) + 3) &&
91                            strncmp (de->d_name, PLUGIN_PREFIX,
92                                     strlen (PLUGIN_PREFIX)) == 0 &&
93                            strncmp (de->d_name + len - 3, ".so", 3) == 0) {
94                                plugin_name = g_concat_dir_and_file (path, de->d_name);
95                                plugin_load (plugin_name);
96                                g_free (plugin_name);
97                        }
98                }
99                closedir (dir);
100        }
101
102        g_strfreev (plugin_dir);
103}
104
105
106StoragePlugin*
107bonobo_storage_plugin_find (const gchar *name)
108{
109        GList *l;
110        StoragePlugin *p;
111
112        g_return_val_if_fail (name != NULL, NULL);
113
114        if (!storage_plugin_list) bonobo_storage_load_plugins ();
115
116        if (!storage_plugin_list) return NULL;
117
118        l = storage_plugin_list;
119
120        while (l) {
121                p = (StoragePlugin *) l->data;
122                if (!strcmp (p->name, name) &&
123                    !strcmp (p->version, BONOBO_STORAGE_VERSION)) {
124                        return p;
125                }
126                l = l->next;
127        }
128
129        return NULL;
130}
Note: See TracBrowser for help on using the repository browser.