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"
34 #include "gdesktopappinfo.h"
40 * @short_description: Loadable GIO Modules
43 * Provides an interface and default functions for loading and unloading
44 * modules. This is used internally to make GIO extensible, but can also
45 * be used by others to implement module loading.
50 * SECTION:extensionpoints
51 * @short_description: Extension Points
53 * @see_also: <link linkend="extending-gio">Extending GIO</link>
55 * #GIOExtensionPoint provides a mechanism for modules to extend the
56 * functionality of the library or application that loaded it in an
59 * An extension point is identified by a name, and it may optionally
60 * require that any implementation must by of a certain type (or derived
61 * thereof). Use g_io_extension_point_register() to register an
62 * extension point, and g_io_extension_point_set_required_type() to
63 * set a required type.
65 * A module can implement an extension point by specifying the #GType
66 * that implements the functionality. Additionally, each implementation
67 * of an extension point has a name, and a priority. Use
68 * g_io_extension_point_implement() to implement an extension point.
71 * GIOExtensionPoint *ep;
73 * /* Register an extension point */
74 * ep = g_io_extension_point_register ("my-extension-point");
75 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
79 * /* Implement an extension point */
80 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
81 * g_io_extension_point_implement ("my-extension-point",
82 * my_example_impl_get_type (),
87 * It is up to the code that registered the extension point how
88 * it uses the implementations that have been associated with it.
89 * Depending on the use case, it may use all implementations, or
90 * only the one with the highest priority, or pick a specific
94 GTypeModule parent_instance;
99 void (* load) (GIOModule *module);
100 void (* unload) (GIOModule *module);
103 struct _GIOModuleClass
105 GTypeModuleClass parent_class;
109 static void g_io_module_finalize (GObject *object);
110 static gboolean g_io_module_load_module (GTypeModule *gmodule);
111 static void g_io_module_unload_module (GTypeModule *gmodule);
113 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
116 g_io_module_class_init (GIOModuleClass *class)
118 GObjectClass *object_class = G_OBJECT_CLASS (class);
119 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
121 object_class->finalize = g_io_module_finalize;
123 type_module_class->load = g_io_module_load_module;
124 type_module_class->unload = g_io_module_unload_module;
128 g_io_module_init (GIOModule *module)
133 g_io_module_finalize (GObject *object)
135 GIOModule *module = G_IO_MODULE (object);
137 g_free (module->filename);
139 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
143 g_io_module_load_module (GTypeModule *gmodule)
145 GIOModule *module = G_IO_MODULE (gmodule);
147 if (!module->filename)
149 g_warning ("GIOModule path not set");
153 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
155 if (!module->library)
157 g_printerr ("%s\n", g_module_error ());
161 /* Make sure that the loaded library contains the required methods */
162 if (! g_module_symbol (module->library,
164 (gpointer) &module->load) ||
165 ! g_module_symbol (module->library,
166 "g_io_module_unload",
167 (gpointer) &module->unload))
169 g_printerr ("%s\n", g_module_error ());
170 g_module_close (module->library);
175 /* Initialize the loaded module */
176 module->load (module);
182 g_io_module_unload_module (GTypeModule *gmodule)
184 GIOModule *module = G_IO_MODULE (gmodule);
186 module->unload (module);
188 g_module_close (module->library);
189 module->library = NULL;
192 module->unload = NULL;
197 * @filename: filename of the shared library module.
199 * Creates a new GIOModule that will load the specific
200 * shared library when in use.
202 * Returns: a #GIOModule from given @filename,
206 g_io_module_new (const gchar *filename)
210 g_return_val_if_fail (filename != NULL, NULL);
212 module = g_object_new (G_IO_TYPE_MODULE, NULL);
213 module->filename = g_strdup (filename);
219 is_valid_module_name (const gchar *basename)
221 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
223 g_str_has_prefix (basename, "lib") &&
224 g_str_has_suffix (basename, ".so");
226 return g_str_has_suffix (basename, ".dll");
231 * g_io_modules_load_all_in_directory:
232 * @dirname: pathname for a directory containing modules to load.
234 * Loads all the modules in the specified directory.
236 * Returns: a list of #GIOModules loaded from the directory,
237 * All the modules are loaded into memory, if you want to
238 * unload them (enabling on-demand loading) you must call
239 * g_type_module_unuse() on all the modules. Free the list
240 * with g_list_free().
243 g_io_modules_load_all_in_directory (const char *dirname)
249 if (!g_module_supported ())
252 dir = g_dir_open (dirname, 0, NULL);
257 while ((name = g_dir_read_name (dir)))
259 if (is_valid_module_name (name))
264 path = g_build_filename (dirname, name, NULL);
265 module = g_io_module_new (path);
267 if (!g_type_module_use (G_TYPE_MODULE (module)))
269 g_printerr ("Failed to load module: %s\n", path);
270 g_object_unref (module);
277 modules = g_list_prepend (modules, module);
286 G_LOCK_DEFINE_STATIC (registered_extensions);
287 G_LOCK_DEFINE_STATIC (loaded_dirs);
289 extern GType _g_fen_directory_monitor_get_type (void);
290 extern GType _g_fen_file_monitor_get_type (void);
291 extern GType _g_inotify_directory_monitor_get_type (void);
292 extern GType _g_inotify_file_monitor_get_type (void);
293 extern GType _g_unix_volume_monitor_get_type (void);
294 extern GType _g_local_vfs_get_type (void);
296 extern GType _g_win32_volume_monitor_get_type (void);
297 extern GType g_win32_directory_monitor_get_type (void);
298 extern GType _g_winhttp_vfs_get_type (void);
301 _g_io_modules_ensure_extension_points_registered (void)
303 static gboolean registered_extensions = FALSE;
304 GIOExtensionPoint *ep;
306 G_LOCK (registered_extensions);
308 if (!registered_extensions)
310 registered_extensions = TRUE;
313 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
314 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
317 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
318 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
320 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
321 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
323 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
324 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
326 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
327 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
329 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
330 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
332 ep = g_io_extension_point_register ("gsettings-backend");
333 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
336 G_UNLOCK (registered_extensions);
340 _g_io_modules_ensure_loaded (void)
343 static gboolean loaded_dirs = FALSE;
344 const char *module_path;
346 _g_io_modules_ensure_extension_points_registered ();
348 G_LOCK (loaded_dirs);
354 modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
356 module_path = g_getenv ("GIO_EXTRA_MODULES");
362 paths = g_strsplit (module_path, ":", 0);
364 while (paths[i] != NULL)
366 modules = g_list_concat (modules, g_io_modules_load_all_in_directory (paths[i]));
373 /* Initialize types from built-in "modules" */
374 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
375 _g_inotify_directory_monitor_get_type ();
376 _g_inotify_file_monitor_get_type ();
378 #if defined(HAVE_FEN)
379 _g_fen_directory_monitor_get_type ();
380 _g_fen_file_monitor_get_type ();
383 _g_win32_volume_monitor_get_type ();
384 g_win32_directory_monitor_get_type ();
387 _g_unix_volume_monitor_get_type ();
390 _g_winhttp_vfs_get_type ();
392 _g_local_vfs_get_type ();
394 for (l = modules; l != NULL; l = l->next)
395 g_type_module_unuse (G_TYPE_MODULE (l->data));
397 g_list_free (modules);
400 G_UNLOCK (loaded_dirs);
403 struct _GIOExtension {
409 struct _GIOExtensionPoint {
415 static GHashTable *extension_points = NULL;
416 G_LOCK_DEFINE_STATIC(extension_points);
420 g_io_extension_point_free (GIOExtensionPoint *ep)
427 * g_io_extension_point_register:
428 * @name: The name of the extension point
430 * Registers an extension point.
432 * Returns: the new #GIOExtensionPoint. This object is owned by GIO
433 * and should not be freed
436 g_io_extension_point_register (const char *name)
438 GIOExtensionPoint *ep;
440 G_LOCK (extension_points);
441 if (extension_points == NULL)
442 extension_points = g_hash_table_new_full (g_str_hash,
445 (GDestroyNotify)g_io_extension_point_free);
447 if (g_hash_table_lookup (extension_points, name) != NULL)
449 g_warning ("Extension point %s registered multiple times", name);
450 G_UNLOCK (extension_points);
454 ep = g_new0 (GIOExtensionPoint, 1);
455 ep->name = g_strdup (name);
457 g_hash_table_insert (extension_points, ep->name, ep);
459 G_UNLOCK (extension_points);
465 * g_io_extension_point_lookup:
466 * @name: the name of the extension point
468 * Looks up an existing extension point.
470 * Returns: the #GIOExtensionPoint, or %NULL if there is no
471 * registered extension point with the given name
474 g_io_extension_point_lookup (const char *name)
476 GIOExtensionPoint *ep;
478 G_LOCK (extension_points);
480 if (extension_points != NULL)
481 ep = g_hash_table_lookup (extension_points, name);
483 G_UNLOCK (extension_points);
490 * g_io_extension_point_set_required_type:
491 * @extension_point: a #GIOExtensionPoint
492 * @type: the #GType to require
494 * Sets the required type for @extension_point to @type.
495 * All implementations must henceforth have this type.
498 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
501 extension_point->required_type = type;
505 * g_io_extension_point_get_required_type:
506 * @extension_point: a #GIOExtensionPoint
508 * Gets the required type for @extension_point.
510 * Returns: the #GType that all implementations must have,
511 * or #G_TYPE_INVALID if the extension point has no required type
514 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
516 return extension_point->required_type;
520 * g_io_extension_point_get_extensions:
521 * @extension_point: a #GIOExtensionPoint
523 * Gets a list of all extensions that implement this extension point.
524 * The list is sorted by priority, beginning with the highest priority.
526 * Returns: a #GList of #GIOExtension<!-- -->s. The list is owned by
527 * GIO and should not be modified
530 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
532 return extension_point->extensions;
536 * g_io_extension_point_get_extension_by_name:
537 * @extension_point: a #GIOExtensionPoint
538 * @name: the name of the extension to get
540 * Finds a #GIOExtension for an extension point by name.
542 * Returns: the #GIOExtension for @extension_point that has the
543 * given name, or %NULL if there is no extension with that name
546 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
551 for (l = extension_point->extensions; l != NULL; l = l->next)
553 GIOExtension *e = l->data;
555 if (e->name != NULL &&
556 strcmp (e->name, name) == 0)
564 extension_prio_compare (gconstpointer a,
567 const GIOExtension *extension_a = a, *extension_b = b;
569 return extension_b->priority - extension_a->priority;
573 * g_io_extension_point_implement:
574 * @extension_point_name: the name of the extension point
575 * @type: the #GType to register as extension
576 * @extension_name: the name for the extension
577 * @priority: the priority for the extension
579 * Registers @type as extension for the extension point with name
580 * @extension_point_name.
582 * If @type has already been registered as an extension for this
583 * extension point, the existing #GIOExtension object is returned.
585 * Returns: a #GIOExtension object for #GType
588 g_io_extension_point_implement (const char *extension_point_name,
590 const char *extension_name,
593 GIOExtensionPoint *extension_point;
594 GIOExtension *extension;
597 g_return_val_if_fail (extension_point_name != NULL, NULL);
599 extension_point = g_io_extension_point_lookup (extension_point_name);
600 if (extension_point == NULL)
602 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
606 if (extension_point->required_type != 0 &&
607 !g_type_is_a (type, extension_point->required_type))
609 g_warning ("Tried to register an extension of the type %s to extension point %s. "
610 "Expected type is %s.",
612 extension_point_name,
613 g_type_name (extension_point->required_type));
617 /* It's safe to register the same type multiple times */
618 for (l = extension_point->extensions; l != NULL; l = l->next)
621 if (extension->type == type)
625 extension = g_slice_new0 (GIOExtension);
626 extension->type = type;
627 extension->name = g_strdup (extension_name);
628 extension->priority = priority;
630 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
631 extension, extension_prio_compare);
637 * g_io_extension_ref_class:
638 * @extension: a #GIOExtension
640 * Gets a reference to the class for the type that is
641 * associated with @extension.
643 * Returns: the #GTypeClass for the type of @extension
646 g_io_extension_ref_class (GIOExtension *extension)
648 return g_type_class_ref (extension->type);
652 * g_io_extension_get_type:
653 * @extension: a #GIOExtension
655 * Gets the type associated with @extension.
657 * Returns: the type of @extension
660 g_io_extension_get_type (GIOExtension *extension)
662 return extension->type;
666 * g_io_extension_get_name:
667 * @extension: a #GIOExtension
669 * Gets the name under which @extension was registered.
671 * Note that the same type may be registered as extension
672 * for multiple extension points, under different names.
674 * Returns: the name of @extension.
677 g_io_extension_get_name (GIOExtension *extension)
679 return extension->name;
683 * g_io_extension_get_priority:
684 * @extension: a #GIOExtension
686 * Gets the priority with which @extension was registered.
688 * Returns: the priority of @extension
691 g_io_extension_get_priority (GIOExtension *extension)
693 return extension->priority;
696 #define __G_IO_MODULE_C__
697 #include "gioaliasdef.c"