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>
25 #include "giomodule.h"
31 * @short_description: Loadable GIO Modules
33 * Provides an interface and default functions for loading and unloading
39 GTypeModule parent_instance;
44 void (* load) (GIOModule *module);
45 void (* unload) (GIOModule *module);
48 struct _GIOModuleClass
50 GTypeModuleClass parent_class;
54 static void g_io_module_finalize (GObject *object);
55 static gboolean g_io_module_load_module (GTypeModule *gmodule);
56 static void g_io_module_unload_module (GTypeModule *gmodule);
58 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
61 g_io_module_class_init (GIOModuleClass *class)
63 GObjectClass *object_class = G_OBJECT_CLASS (class);
64 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
66 object_class->finalize = g_io_module_finalize;
68 type_module_class->load = g_io_module_load_module;
69 type_module_class->unload = g_io_module_unload_module;
73 g_io_module_init (GIOModule *module)
78 g_io_module_finalize (GObject *object)
80 GIOModule *module = G_IO_MODULE (object);
82 g_free (module->filename);
84 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
88 g_io_module_load_module (GTypeModule *gmodule)
90 GIOModule *module = G_IO_MODULE (gmodule);
92 if (!module->filename)
94 g_warning ("GIOModule path not set");
98 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
100 if (!module->library)
102 g_printerr ("%s\n", g_module_error ());
106 /* Make sure that the loaded library contains the required methods */
107 if (! g_module_symbol (module->library,
109 (gpointer *) &module->load) ||
110 ! g_module_symbol (module->library,
111 "g_io_module_unload",
112 (gpointer *) &module->unload))
114 g_printerr ("%s\n", g_module_error ());
115 g_module_close (module->library);
120 /* Initialize the loaded module */
121 module->load (module);
127 g_io_module_unload_module (GTypeModule *gmodule)
129 GIOModule *module = G_IO_MODULE (gmodule);
131 module->unload (module);
133 g_module_close (module->library);
134 module->library = NULL;
137 module->unload = NULL;
142 * @filename: filename of the module to load.
144 * Loads a new module into GIO.
146 * Returns: a #GIOModule from given @filename,
150 g_io_module_new (const gchar *filename)
154 g_return_val_if_fail (filename != NULL, NULL);
156 module = g_object_new (G_IO_TYPE_MODULE, NULL);
157 module->filename = g_strdup (filename);
163 is_valid_module_name (const gchar *basename)
165 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
167 g_str_has_prefix (basename, "lib") &&
168 g_str_has_suffix (basename, ".so");
170 return g_str_has_suffix (basename, ".dll");
175 load_modules (const char *dirname)
181 if (!g_module_supported ())
184 dir = g_dir_open (dirname, 0, NULL);
189 while ((name = g_dir_read_name (dir)))
191 if (is_valid_module_name (name))
196 path = g_build_filename (dirname, name, NULL);
197 module = g_io_module_new (path);
199 if (!g_type_module_use (G_TYPE_MODULE (module)))
201 g_printerr ("Failed to load module: %s\n", path);
202 g_object_unref (module);
209 g_type_module_unuse (G_TYPE_MODULE (module));
211 modules = g_list_prepend (modules, module);
220 G_LOCK_DEFINE_STATIC (loaded_dirs);
221 static GHashTable *loaded_dirs = NULL;
224 * g_io_modules_ensure_loaded:
225 * @directory: string containing a directory path.
227 * Loads all of the modules within the @directory.
230 g_io_modules_ensure_loaded (const char *directory)
234 g_return_if_fail (directory != NULL);
236 G_LOCK (loaded_dirs);
238 if (loaded_dirs == NULL)
239 loaded_dirs = g_hash_table_new (g_str_hash, g_str_equal);
241 if (!g_hash_table_lookup_extended (loaded_dirs, directory,
244 modules = load_modules (directory);
245 g_hash_table_insert (loaded_dirs,
246 g_strdup (directory),
250 G_UNLOCK (loaded_dirs);
253 #define __G_IO_MODULE_C__
254 #include "gioaliasdef.c"