1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "giomodule.h"
28 #include "giomodule-priv.h"
29 #include "glocalfilemonitor.h"
30 #include "glocaldirectorymonitor.h"
31 #include "gnativevolumemonitor.h"
38 * @short_description: Loadable GIO Modules
41 * Provides an interface and default functions for loading and unloading
42 * modules. This is used internally to make gio extensible, but can also
43 * be used by other to implement module loading.
48 GTypeModule parent_instance;
53 void (* load) (GIOModule *module);
54 void (* unload) (GIOModule *module);
57 struct _GIOModuleClass
59 GTypeModuleClass parent_class;
63 static void g_io_module_finalize (GObject *object);
64 static gboolean g_io_module_load_module (GTypeModule *gmodule);
65 static void g_io_module_unload_module (GTypeModule *gmodule);
67 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
70 g_io_module_class_init (GIOModuleClass *class)
72 GObjectClass *object_class = G_OBJECT_CLASS (class);
73 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
75 object_class->finalize = g_io_module_finalize;
77 type_module_class->load = g_io_module_load_module;
78 type_module_class->unload = g_io_module_unload_module;
82 g_io_module_init (GIOModule *module)
87 g_io_module_finalize (GObject *object)
89 GIOModule *module = G_IO_MODULE (object);
91 g_free (module->filename);
93 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
97 g_io_module_load_module (GTypeModule *gmodule)
99 GIOModule *module = G_IO_MODULE (gmodule);
101 if (!module->filename)
103 g_warning ("GIOModule path not set");
107 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
109 if (!module->library)
111 g_printerr ("%s\n", g_module_error ());
115 /* Make sure that the loaded library contains the required methods */
116 if (! g_module_symbol (module->library,
118 (gpointer) &module->load) ||
119 ! g_module_symbol (module->library,
120 "g_io_module_unload",
121 (gpointer) &module->unload))
123 g_printerr ("%s\n", g_module_error ());
124 g_module_close (module->library);
129 /* Initialize the loaded module */
130 module->load (module);
136 g_io_module_unload_module (GTypeModule *gmodule)
138 GIOModule *module = G_IO_MODULE (gmodule);
140 module->unload (module);
142 g_module_close (module->library);
143 module->library = NULL;
146 module->unload = NULL;
151 * @filename: filename of the shared library module.
153 * Creates a new GIOModule that will load the specific
154 * shared library when in use.
156 * Returns: a #GIOModule from given @filename,
160 g_io_module_new (const gchar *filename)
164 g_return_val_if_fail (filename != NULL, NULL);
166 module = g_object_new (G_IO_TYPE_MODULE, NULL);
167 module->filename = g_strdup (filename);
173 is_valid_module_name (const gchar *basename)
175 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
177 g_str_has_prefix (basename, "lib") &&
178 g_str_has_suffix (basename, ".so");
180 return g_str_has_suffix (basename, ".dll");
185 * g_io_modules_load_all_in_directory:
186 * @dirname: pathname for a directory containing modules to load.
188 * Loads all the modules in the the specified directory.
190 * Returns: a list of #GIOModules loaded from the directory,
191 * All the modules are loaded into memory, if you want to
192 * unload them (enabling on-demand loading) you must call
193 * g_type_module_unuse() on all the modules. Free the list
194 * with g_list_free().
197 g_io_modules_load_all_in_directory (const char *dirname)
203 if (!g_module_supported ())
206 dir = g_dir_open (dirname, 0, NULL);
211 while ((name = g_dir_read_name (dir)))
213 if (is_valid_module_name (name))
218 path = g_build_filename (dirname, name, NULL);
219 module = g_io_module_new (path);
221 if (!g_type_module_use (G_TYPE_MODULE (module)))
223 g_printerr ("Failed to load module: %s\n", path);
224 g_object_unref (module);
231 modules = g_list_prepend (modules, module);
240 G_LOCK_DEFINE_STATIC (loaded_dirs);
242 extern GType _g_inotify_directory_monitor_get_type (void);
243 extern GType _g_inotify_file_monitor_get_type (void);
244 extern GType _g_unix_volume_monitor_get_type (void);
245 extern GType _g_local_vfs_get_type (void);
248 _g_io_modules_ensure_loaded (void)
251 static gboolean loaded_dirs = FALSE;
252 GIOExtensionPoint *ep;
254 G_LOCK (loaded_dirs);
260 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
261 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
263 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
264 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
266 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
267 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
269 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
270 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
272 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
273 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
275 modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
277 /* Initialize types from built-in "modules" */
278 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
279 _g_inotify_directory_monitor_get_type ();
280 _g_inotify_file_monitor_get_type ();
283 _g_unix_volume_monitor_get_type ();
285 _g_local_vfs_get_type ();
287 for (l = modules; l != NULL; l = l->next)
288 g_type_module_unuse (G_TYPE_MODULE (l->data));
290 g_list_free (modules);
293 G_UNLOCK (loaded_dirs);
296 struct _GIOExtension {
302 struct _GIOExtensionPoint {
308 static GHashTable *extension_points = NULL;
309 G_LOCK_DEFINE_STATIC(extension_points);
313 g_io_extension_point_free (GIOExtensionPoint *ep)
320 g_io_extension_point_register (const char *name)
322 GIOExtensionPoint *ep;
324 G_LOCK (extension_points);
325 if (extension_points == NULL)
326 extension_points = g_hash_table_new_full (g_str_hash,
329 (GDestroyNotify)g_io_extension_point_free);
331 if (g_hash_table_lookup (extension_points, name) != NULL)
333 g_warning ("Extension point %s registered multiple times", name);
334 G_UNLOCK (extension_points);
338 ep = g_new0 (GIOExtensionPoint, 1);
339 ep->name = g_strdup (name);
341 g_hash_table_insert (extension_points, ep->name, ep);
343 G_UNLOCK (extension_points);
349 g_io_extension_point_lookup (const char *name)
351 GIOExtensionPoint *ep;
353 G_LOCK (extension_points);
355 if (extension_points != NULL)
356 ep = g_hash_table_lookup (extension_points, name);
358 G_UNLOCK (extension_points);
365 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
368 extension_point->required_type = type;
372 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
374 return extension_point->required_type;
378 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
380 return extension_point->extensions;
384 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
389 for (l = extension_point->extensions; l != NULL; l = l->next)
391 GIOExtension *e = l->data;
393 if (e->name != NULL &&
394 strcmp (e->name, name) == 0)
402 extension_prio_compare (gconstpointer a,
405 const GIOExtension *extension_a = a, *extension_b = b;
407 return extension_b->priority - extension_a->priority;
411 g_io_extension_point_implement (const char *extension_point_name,
413 const char *extension_name,
416 GIOExtensionPoint *extension_point;
417 GIOExtension *extension;
420 g_return_val_if_fail (extension_point_name != NULL, NULL);
422 extension_point = g_io_extension_point_lookup (extension_point_name);
423 if (extension_point == NULL)
425 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
429 if (extension_point->required_type != 0 &&
430 !g_type_is_a (type, extension_point->required_type))
432 g_warning ("Tried to register an extension of the type %s to extension point %s. "
433 "Expected type is %s.",
435 extension_point_name,
436 g_type_name (extension_point->required_type));
440 /* Its safe to register the same type multiple times */
441 for (l = extension_point->extensions; l != NULL; l = l->next)
444 if (extension->type == type)
448 extension = g_slice_new0 (GIOExtension);
449 extension->type = type;
450 extension->name = g_strdup (extension_name);
451 extension->priority = priority;
453 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
454 extension, extension_prio_compare);
460 g_io_extension_ref_class (GIOExtension *extension)
462 return g_type_class_ref (extension->type);
467 g_io_extension_get_type (GIOExtension *extension)
469 return extension->type;
473 g_io_extension_get_name (GIOExtension *extension)
475 return extension->name;
479 g_io_extension_get_priority (GIOExtension *extension)
481 return extension->priority;
484 #define __G_IO_MODULE_C__
485 #include "gioaliasdef.c"