Add registration hooks for extension points. Register the gio extension
[platform/upstream/glib.git] / gio / giomodule.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include "giomodule.h"
28 #include "giomodule-priv.h"
29 #include "glocalfilemonitor.h"
30 #include "glocaldirectorymonitor.h"
31 #include "gnativevolumemonitor.h"
32 #include "gvfs.h"
33
34 #include "gioalias.h"
35
36 /**
37  * SECTION:giomodule
38  * @short_description: Loadable GIO Modules
39  * @include: gio.h
40  *
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.
44  * 
45  **/
46
47 struct _GIOModule {
48   GTypeModule parent_instance;
49   
50   gchar       *filename;
51   GModule     *library;
52   
53   void (* load)   (GIOModule *module);
54   void (* unload) (GIOModule *module);
55 };
56
57 struct _GIOModuleClass
58 {
59   GTypeModuleClass parent_class;
60
61 };
62
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);
66
67 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
68
69 static void
70 g_io_module_class_init (GIOModuleClass *class)
71 {
72   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
73   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
74
75   object_class->finalize     = g_io_module_finalize;
76
77   type_module_class->load    = g_io_module_load_module;
78   type_module_class->unload  = g_io_module_unload_module;
79 }
80
81 static void
82 g_io_module_init (GIOModule *module)
83 {
84 }
85
86 static void
87 g_io_module_finalize (GObject *object)
88 {
89   GIOModule *module = G_IO_MODULE (object);
90
91   g_free (module->filename);
92
93   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
94 }
95
96 static gboolean
97 g_io_module_load_module (GTypeModule *gmodule)
98 {
99   GIOModule *module = G_IO_MODULE (gmodule);
100
101   if (!module->filename)
102     {
103       g_warning ("GIOModule path not set");
104       return FALSE;
105     }
106
107   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
108
109   if (!module->library)
110     {
111       g_printerr ("%s\n", g_module_error ());
112       return FALSE;
113     }
114
115   /* Make sure that the loaded library contains the required methods */
116   if (! g_module_symbol (module->library,
117                          "g_io_module_load",
118                          (gpointer) &module->load) ||
119       ! g_module_symbol (module->library,
120                          "g_io_module_unload",
121                          (gpointer) &module->unload))
122     {
123       g_printerr ("%s\n", g_module_error ());
124       g_module_close (module->library);
125
126       return FALSE;
127     }
128
129   /* Initialize the loaded module */
130   module->load (module);
131
132   return TRUE;
133 }
134
135 static void
136 g_io_module_unload_module (GTypeModule *gmodule)
137 {
138   GIOModule *module = G_IO_MODULE (gmodule);
139
140   module->unload (module);
141
142   g_module_close (module->library);
143   module->library = NULL;
144
145   module->load   = NULL;
146   module->unload = NULL;
147 }
148
149 /**
150  * g_io_module_new:
151  * @filename: filename of the shared library module.
152  * 
153  * Creates a new GIOModule that will load the specific
154  * shared library when in use.
155  * 
156  * Returns: a #GIOModule from given @filename, 
157  * or %NULL on error.
158  **/
159 GIOModule *
160 g_io_module_new (const gchar *filename)
161 {
162   GIOModule *module;
163
164   g_return_val_if_fail (filename != NULL, NULL);
165
166   module = g_object_new (G_IO_TYPE_MODULE, NULL);
167   module->filename = g_strdup (filename);
168
169   return module;
170 }
171
172 static gboolean
173 is_valid_module_name (const gchar *basename)
174 {
175 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
176   return
177     g_str_has_prefix (basename, "lib") &&
178     g_str_has_suffix (basename, ".so");
179 #else
180   return g_str_has_suffix (basename, ".dll");
181 #endif
182 }
183
184 /**
185  * g_io_modules_load_all_in_directory:
186  * @dirname: pathname for a directory containing modules to load.
187  * 
188  * Loads all the modules in the the specified directory. 
189  * 
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().
195  **/
196 GList *
197 g_io_modules_load_all_in_directory (const char *dirname)
198 {
199   const gchar *name;
200   GDir        *dir;
201   GList *modules;
202
203   if (!g_module_supported ())
204     return NULL;
205
206   dir = g_dir_open (dirname, 0, NULL);
207   if (!dir)
208     return NULL;
209
210   modules = NULL;
211   while ((name = g_dir_read_name (dir)))
212     {
213       if (is_valid_module_name (name))
214         {
215           GIOModule *module;
216           gchar     *path;
217
218           path = g_build_filename (dirname, name, NULL);
219           module = g_io_module_new (path);
220
221           if (!g_type_module_use (G_TYPE_MODULE (module)))
222             {
223               g_printerr ("Failed to load module: %s\n", path);
224               g_object_unref (module);
225               g_free (path);
226               continue;
227             }
228           
229           g_free (path);
230
231           modules = g_list_prepend (modules, module);
232         }
233     }
234   
235   g_dir_close (dir);
236
237   return modules;
238 }
239
240 G_LOCK_DEFINE_STATIC (loaded_dirs);
241
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);
246
247 void
248 _g_io_modules_ensure_loaded (void)
249 {
250   GList *modules, *l;
251   static gboolean loaded_dirs = FALSE;
252   GIOExtensionPoint *ep;
253
254   G_LOCK (loaded_dirs);
255
256   if (!loaded_dirs)
257     {
258       loaded_dirs = TRUE;
259
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);
262       
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);
265
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);
268       
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);
271       
272       ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
273       g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
274       
275       modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
276
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 ();
281 #endif
282 #ifdef G_OS_UNIX
283       _g_unix_volume_monitor_get_type ();
284 #endif
285       _g_local_vfs_get_type ();
286     
287       for (l = modules; l != NULL; l = l->next)
288         g_type_module_unuse (G_TYPE_MODULE (l->data));
289       
290       g_list_free (modules);
291     }
292
293   G_UNLOCK (loaded_dirs);
294 }
295
296 struct _GIOExtension {
297   char *name;
298   GType type;
299   gint priority;
300 };
301
302 struct _GIOExtensionPoint {
303   GType required_type;
304   char *name;
305   GList *extensions;
306 };
307
308 static GHashTable *extension_points = NULL;
309 G_LOCK_DEFINE_STATIC(extension_points);
310
311
312 static void
313 g_io_extension_point_free (GIOExtensionPoint *ep)
314 {
315   g_free (ep->name);
316   g_free (ep);
317 }
318
319 GIOExtensionPoint *
320 g_io_extension_point_register (const char *name)
321 {
322   GIOExtensionPoint *ep;
323   
324   G_LOCK (extension_points);
325   if (extension_points == NULL)
326     extension_points = g_hash_table_new_full (g_str_hash,
327                                               g_str_equal,
328                                               NULL,
329                                               (GDestroyNotify)g_io_extension_point_free);
330
331   if (g_hash_table_lookup (extension_points, name) != NULL)
332     {
333       g_warning ("Extension point %s registered multiple times", name);
334       G_UNLOCK (extension_points);
335       return NULL;
336     }
337
338   ep = g_new0 (GIOExtensionPoint, 1);
339   ep->name = g_strdup (name);
340   
341   g_hash_table_insert (extension_points, ep->name, ep);
342   
343   G_UNLOCK (extension_points);
344
345   return ep;
346 }
347
348 GIOExtensionPoint *
349 g_io_extension_point_lookup (const char *name)
350 {
351   GIOExtensionPoint *ep;
352
353   G_LOCK (extension_points);
354   ep = NULL;
355   if (extension_points != NULL)
356     ep = g_hash_table_lookup (extension_points, name);
357   
358   G_UNLOCK (extension_points);
359
360   return ep;
361   
362 }
363
364 void
365 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
366                                         GType              type)
367 {
368   extension_point->required_type = type;
369 }
370
371 GType
372 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
373 {
374   return extension_point->required_type;
375 }
376
377 GList *
378 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
379 {
380   return extension_point->extensions;
381 }
382
383 GIOExtension *
384 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
385                                             const char        *name)
386 {
387   GList *l;
388
389   for (l = extension_point->extensions; l != NULL; l = l->next)
390     {
391       GIOExtension *e = l->data;
392
393       if (e->name != NULL &&
394           strcmp (e->name, name) == 0)
395         return e;
396     }
397   
398   return NULL;
399 }
400
401 static gint
402 extension_prio_compare (gconstpointer  a,
403                         gconstpointer  b)
404 {
405   const GIOExtension *extension_a = a, *extension_b = b;
406
407   return extension_b->priority - extension_a->priority;
408 }
409
410 GIOExtension *
411 g_io_extension_point_implement (const char *extension_point_name,
412                                 GType type,
413                                 const char *extension_name,
414                                 gint priority)
415 {
416   GIOExtensionPoint *extension_point;
417   GIOExtension *extension;
418   GList *l;
419
420   g_return_val_if_fail (extension_point_name != NULL, NULL);
421
422   extension_point = g_io_extension_point_lookup (extension_point_name);
423   if (extension_point == NULL)
424     {
425       g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
426       return NULL;
427     }
428   
429   if (extension_point->required_type != 0 &&
430       !g_type_is_a (type, extension_point->required_type))
431     {
432       g_warning ("Tried to register an extension of the type %s to extension point %s. "
433                  "Expected type is %s.",
434                  g_type_name (type),
435                  extension_point_name, 
436                  g_type_name (extension_point->required_type));
437       return NULL;
438     }      
439
440   /* Its safe to register the same type multiple times */
441   for (l = extension_point->extensions; l != NULL; l = l->next)
442     {
443       extension = l->data;
444       if (extension->type == type)
445         return extension;
446     }
447   
448   extension = g_slice_new0 (GIOExtension);
449   extension->type = type;
450   extension->name = g_strdup (extension_name);
451   extension->priority = priority;
452   
453   extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
454                                                       extension, extension_prio_compare);
455   
456   return extension;
457 }
458
459 GTypeClass *
460 g_io_extension_ref_class (GIOExtension *extension)
461 {
462   return g_type_class_ref (extension->type);
463 }
464
465
466 GType
467 g_io_extension_get_type (GIOExtension *extension)
468 {
469   return extension->type;
470 }
471
472 const char *
473 g_io_extension_get_name (GIOExtension *extension)
474 {
475   return extension->name;
476 }
477
478 gint
479 g_io_extension_get_priority (GIOExtension *extension)
480 {
481   return extension->priority;
482 }
483
484 #define __G_IO_MODULE_C__
485 #include "gioaliasdef.c"