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 other to implement module loading.
50 GTypeModule parent_instance;
55 void (* load) (GIOModule *module);
56 void (* unload) (GIOModule *module);
59 struct _GIOModuleClass
61 GTypeModuleClass parent_class;
65 static void g_io_module_finalize (GObject *object);
66 static gboolean g_io_module_load_module (GTypeModule *gmodule);
67 static void g_io_module_unload_module (GTypeModule *gmodule);
69 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
72 g_io_module_class_init (GIOModuleClass *class)
74 GObjectClass *object_class = G_OBJECT_CLASS (class);
75 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
77 object_class->finalize = g_io_module_finalize;
79 type_module_class->load = g_io_module_load_module;
80 type_module_class->unload = g_io_module_unload_module;
84 g_io_module_init (GIOModule *module)
89 g_io_module_finalize (GObject *object)
91 GIOModule *module = G_IO_MODULE (object);
93 g_free (module->filename);
95 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
99 g_io_module_load_module (GTypeModule *gmodule)
101 GIOModule *module = G_IO_MODULE (gmodule);
103 if (!module->filename)
105 g_warning ("GIOModule path not set");
109 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
111 if (!module->library)
113 g_printerr ("%s\n", g_module_error ());
117 /* Make sure that the loaded library contains the required methods */
118 if (! g_module_symbol (module->library,
120 (gpointer) &module->load) ||
121 ! g_module_symbol (module->library,
122 "g_io_module_unload",
123 (gpointer) &module->unload))
125 g_printerr ("%s\n", g_module_error ());
126 g_module_close (module->library);
131 /* Initialize the loaded module */
132 module->load (module);
138 g_io_module_unload_module (GTypeModule *gmodule)
140 GIOModule *module = G_IO_MODULE (gmodule);
142 module->unload (module);
144 g_module_close (module->library);
145 module->library = NULL;
148 module->unload = NULL;
153 * @filename: filename of the shared library module.
155 * Creates a new GIOModule that will load the specific
156 * shared library when in use.
158 * Returns: a #GIOModule from given @filename,
162 g_io_module_new (const gchar *filename)
166 g_return_val_if_fail (filename != NULL, NULL);
168 module = g_object_new (G_IO_TYPE_MODULE, NULL);
169 module->filename = g_strdup (filename);
175 is_valid_module_name (const gchar *basename)
177 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
179 g_str_has_prefix (basename, "lib") &&
180 g_str_has_suffix (basename, ".so");
182 return g_str_has_suffix (basename, ".dll");
187 * g_io_modules_load_all_in_directory:
188 * @dirname: pathname for a directory containing modules to load.
190 * Loads all the modules in the the specified directory.
192 * Returns: a list of #GIOModules loaded from the directory,
193 * All the modules are loaded into memory, if you want to
194 * unload them (enabling on-demand loading) you must call
195 * g_type_module_unuse() on all the modules. Free the list
196 * with g_list_free().
199 g_io_modules_load_all_in_directory (const char *dirname)
205 if (!g_module_supported ())
208 dir = g_dir_open (dirname, 0, NULL);
213 while ((name = g_dir_read_name (dir)))
215 if (is_valid_module_name (name))
220 path = g_build_filename (dirname, name, NULL);
221 module = g_io_module_new (path);
223 if (!g_type_module_use (G_TYPE_MODULE (module)))
225 g_printerr ("Failed to load module: %s\n", path);
226 g_object_unref (module);
233 modules = g_list_prepend (modules, module);
242 G_LOCK_DEFINE_STATIC (loaded_dirs);
244 extern GType _g_inotify_directory_monitor_get_type (void);
245 extern GType _g_inotify_file_monitor_get_type (void);
246 extern GType _g_unix_volume_monitor_get_type (void);
247 extern GType _g_local_vfs_get_type (void);
250 _g_io_modules_ensure_loaded (void)
253 static gboolean loaded_dirs = FALSE;
254 GIOExtensionPoint *ep;
256 G_LOCK (loaded_dirs);
263 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
264 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
267 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
268 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
270 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
271 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
273 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
274 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
276 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
277 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
279 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
280 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
282 modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
284 /* Initialize types from built-in "modules" */
285 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
286 _g_inotify_directory_monitor_get_type ();
287 _g_inotify_file_monitor_get_type ();
290 _g_unix_volume_monitor_get_type ();
292 _g_local_vfs_get_type ();
294 for (l = modules; l != NULL; l = l->next)
295 g_type_module_unuse (G_TYPE_MODULE (l->data));
297 g_list_free (modules);
300 G_UNLOCK (loaded_dirs);
303 struct _GIOExtension {
309 struct _GIOExtensionPoint {
315 static GHashTable *extension_points = NULL;
316 G_LOCK_DEFINE_STATIC(extension_points);
320 g_io_extension_point_free (GIOExtensionPoint *ep)
327 g_io_extension_point_register (const char *name)
329 GIOExtensionPoint *ep;
331 G_LOCK (extension_points);
332 if (extension_points == NULL)
333 extension_points = g_hash_table_new_full (g_str_hash,
336 (GDestroyNotify)g_io_extension_point_free);
338 if (g_hash_table_lookup (extension_points, name) != NULL)
340 g_warning ("Extension point %s registered multiple times", name);
341 G_UNLOCK (extension_points);
345 ep = g_new0 (GIOExtensionPoint, 1);
346 ep->name = g_strdup (name);
348 g_hash_table_insert (extension_points, ep->name, ep);
350 G_UNLOCK (extension_points);
356 g_io_extension_point_lookup (const char *name)
358 GIOExtensionPoint *ep;
360 G_LOCK (extension_points);
362 if (extension_points != NULL)
363 ep = g_hash_table_lookup (extension_points, name);
365 G_UNLOCK (extension_points);
372 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
375 extension_point->required_type = type;
379 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
381 return extension_point->required_type;
385 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
387 return extension_point->extensions;
391 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
396 for (l = extension_point->extensions; l != NULL; l = l->next)
398 GIOExtension *e = l->data;
400 if (e->name != NULL &&
401 strcmp (e->name, name) == 0)
409 extension_prio_compare (gconstpointer a,
412 const GIOExtension *extension_a = a, *extension_b = b;
414 return extension_b->priority - extension_a->priority;
418 g_io_extension_point_implement (const char *extension_point_name,
420 const char *extension_name,
423 GIOExtensionPoint *extension_point;
424 GIOExtension *extension;
427 g_return_val_if_fail (extension_point_name != NULL, NULL);
429 extension_point = g_io_extension_point_lookup (extension_point_name);
430 if (extension_point == NULL)
432 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
436 if (extension_point->required_type != 0 &&
437 !g_type_is_a (type, extension_point->required_type))
439 g_warning ("Tried to register an extension of the type %s to extension point %s. "
440 "Expected type is %s.",
442 extension_point_name,
443 g_type_name (extension_point->required_type));
447 /* Its safe to register the same type multiple times */
448 for (l = extension_point->extensions; l != NULL; l = l->next)
451 if (extension->type == type)
455 extension = g_slice_new0 (GIOExtension);
456 extension->type = type;
457 extension->name = g_strdup (extension_name);
458 extension->priority = priority;
460 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
461 extension, extension_prio_compare);
467 g_io_extension_ref_class (GIOExtension *extension)
469 return g_type_class_ref (extension->type);
474 g_io_extension_get_type (GIOExtension *extension)
476 return extension->type;
480 g_io_extension_get_name (GIOExtension *extension)
482 return extension->name;
486 g_io_extension_get_priority (GIOExtension *extension)
488 return extension->priority;
491 #define __G_IO_MODULE_C__
492 #include "gioaliasdef.c"