59f417bacb416414544ee6090113c95273bb5789
[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 #ifdef G_OS_UNIX
34 #include "gdesktopappinfo.h"
35 #endif
36 #include "gioalias.h"
37
38 /**
39  * SECTION:giomodule
40  * @short_description: Loadable GIO Modules
41  * @include: gio/gio.h
42  *
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.
46  * 
47  **/
48
49 /**
50  * SECTION:extensionpoints
51  * @short_description: Extension Points
52  * @include: gio.h
53  * @see_also: <link linkend="extending-gio">Extending GIO</link>
54  *
55  * #GIOExtensionPoint provides a mechanism for modules to extend the
56  * functionality of the library or application that loaded it in an 
57  * organized fashion.  
58  *
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.
64  *
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.
69  * 
70  *  |[
71  *  GIOExtensionPoint *ep;
72  *
73  *  /&ast; Register an extension point &ast;/
74  *  ep = g_io_extension_point_register ("my-extension-point");
75  *  g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
76  *  ]|
77  *
78  *  |[
79  *  /&ast; Implement an extension point &ast;/
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 (),
83  *                                  "my-example",
84  *                                  10);
85  *  ]|
86  *
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
91  *  one by name. 
92  */
93 struct _GIOModule {
94   GTypeModule parent_instance;
95   
96   gchar       *filename;
97   GModule     *library;
98   
99   void (* load)   (GIOModule *module);
100   void (* unload) (GIOModule *module);
101 };
102
103 struct _GIOModuleClass
104 {
105   GTypeModuleClass parent_class;
106
107 };
108
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);
112
113 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
114
115 static void
116 g_io_module_class_init (GIOModuleClass *class)
117 {
118   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
119   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
120
121   object_class->finalize     = g_io_module_finalize;
122
123   type_module_class->load    = g_io_module_load_module;
124   type_module_class->unload  = g_io_module_unload_module;
125 }
126
127 static void
128 g_io_module_init (GIOModule *module)
129 {
130 }
131
132 static void
133 g_io_module_finalize (GObject *object)
134 {
135   GIOModule *module = G_IO_MODULE (object);
136
137   g_free (module->filename);
138
139   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
140 }
141
142 static gboolean
143 g_io_module_load_module (GTypeModule *gmodule)
144 {
145   GIOModule *module = G_IO_MODULE (gmodule);
146
147   if (!module->filename)
148     {
149       g_warning ("GIOModule path not set");
150       return FALSE;
151     }
152
153   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
154
155   if (!module->library)
156     {
157       g_printerr ("%s\n", g_module_error ());
158       return FALSE;
159     }
160
161   /* Make sure that the loaded library contains the required methods */
162   if (! g_module_symbol (module->library,
163                          "g_io_module_load",
164                          (gpointer) &module->load) ||
165       ! g_module_symbol (module->library,
166                          "g_io_module_unload",
167                          (gpointer) &module->unload))
168     {
169       g_printerr ("%s\n", g_module_error ());
170       g_module_close (module->library);
171
172       return FALSE;
173     }
174
175   /* Initialize the loaded module */
176   module->load (module);
177
178   return TRUE;
179 }
180
181 static void
182 g_io_module_unload_module (GTypeModule *gmodule)
183 {
184   GIOModule *module = G_IO_MODULE (gmodule);
185
186   module->unload (module);
187
188   g_module_close (module->library);
189   module->library = NULL;
190
191   module->load   = NULL;
192   module->unload = NULL;
193 }
194
195 /**
196  * g_io_module_new:
197  * @filename: filename of the shared library module.
198  * 
199  * Creates a new GIOModule that will load the specific
200  * shared library when in use.
201  * 
202  * Returns: a #GIOModule from given @filename, 
203  * or %NULL on error.
204  **/
205 GIOModule *
206 g_io_module_new (const gchar *filename)
207 {
208   GIOModule *module;
209
210   g_return_val_if_fail (filename != NULL, NULL);
211
212   module = g_object_new (G_IO_TYPE_MODULE, NULL);
213   module->filename = g_strdup (filename);
214
215   return module;
216 }
217
218 static gboolean
219 is_valid_module_name (const gchar *basename)
220 {
221 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
222   return
223     g_str_has_prefix (basename, "lib") &&
224     g_str_has_suffix (basename, ".so");
225 #else
226   return g_str_has_suffix (basename, ".dll");
227 #endif
228 }
229
230 /**
231  * g_io_modules_load_all_in_directory:
232  * @dirname: pathname for a directory containing modules to load.
233  *
234  * Loads all the modules in the specified directory.
235  *
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().
241  **/
242 GList *
243 g_io_modules_load_all_in_directory (const char *dirname)
244 {
245   const gchar *name;
246   GDir        *dir;
247   GList *modules;
248
249   if (!g_module_supported ())
250     return NULL;
251
252   dir = g_dir_open (dirname, 0, NULL);
253   if (!dir)
254     return NULL;
255
256   modules = NULL;
257   while ((name = g_dir_read_name (dir)))
258     {
259       if (is_valid_module_name (name))
260         {
261           GIOModule *module;
262           gchar     *path;
263
264           path = g_build_filename (dirname, name, NULL);
265           module = g_io_module_new (path);
266
267           if (!g_type_module_use (G_TYPE_MODULE (module)))
268             {
269               g_printerr ("Failed to load module: %s\n", path);
270               g_object_unref (module);
271               g_free (path);
272               continue;
273             }
274           
275           g_free (path);
276
277           modules = g_list_prepend (modules, module);
278         }
279     }
280   
281   g_dir_close (dir);
282
283   return modules;
284 }
285
286 G_LOCK_DEFINE_STATIC (loaded_dirs);
287
288 extern GType _g_fen_directory_monitor_get_type (void);
289 extern GType _g_fen_file_monitor_get_type (void);
290 extern GType _g_inotify_directory_monitor_get_type (void);
291 extern GType _g_inotify_file_monitor_get_type (void);
292 extern GType _g_unix_volume_monitor_get_type (void);
293 extern GType _g_local_vfs_get_type (void);
294
295 extern GType _g_win32_volume_monitor_get_type (void);
296 extern GType g_win32_directory_monitor_get_type (void);
297 extern GType _g_winhttp_vfs_get_type (void);
298
299 void
300 _g_io_modules_ensure_extension_points_registered (void)
301 {
302   static gboolean registered_extensions = FALSE;
303   GIOExtensionPoint *ep;
304
305   G_LOCK (loaded_dirs);
306   
307   if (!registered_extensions)
308     {
309       registered_extensions = TRUE;
310       
311 #ifdef G_OS_UNIX
312       ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
313       g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
314 #endif
315       
316       ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
317       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
318       
319       ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
320       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
321       
322       ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
323       g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
324       
325       ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
326       g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
327       
328       ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
329       g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
330     }
331   
332   G_UNLOCK (loaded_dirs);
333  }
334
335 void
336 _g_io_modules_ensure_loaded (void)
337 {
338   GList *modules, *l;
339   static gboolean loaded_dirs = FALSE;
340   const char *module_path;
341
342   _g_io_modules_ensure_extension_points_registered ();
343   
344   G_LOCK (loaded_dirs);
345
346   if (!loaded_dirs)
347     {
348       loaded_dirs = TRUE;
349
350       modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
351
352       module_path = g_getenv ("GIO_EXTRA_MODULES");
353
354       if (module_path)
355         {
356           int i = 0;
357           gchar **paths;
358           paths = g_strsplit (module_path, ":", 0);
359     
360           while (paths[i] != NULL)
361             { 
362               modules = g_list_concat (modules, g_io_modules_load_all_in_directory (paths[i]));
363               i++;
364             }
365
366           g_strfreev (paths);
367         }
368
369       /* Initialize types from built-in "modules" */
370 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
371       _g_inotify_directory_monitor_get_type ();
372       _g_inotify_file_monitor_get_type ();
373 #endif
374 #if defined(HAVE_FEN)
375       _g_fen_directory_monitor_get_type ();
376       _g_fen_file_monitor_get_type ();
377 #endif
378 #ifdef G_OS_WIN32
379       _g_win32_volume_monitor_get_type ();
380       g_win32_directory_monitor_get_type ();
381 #endif
382 #ifdef G_OS_UNIX
383       _g_unix_volume_monitor_get_type ();
384 #endif
385 #ifdef G_OS_WIN32
386       _g_winhttp_vfs_get_type ();
387 #endif
388       _g_local_vfs_get_type ();
389     
390       for (l = modules; l != NULL; l = l->next)
391         g_type_module_unuse (G_TYPE_MODULE (l->data));
392       
393       g_list_free (modules);
394     }
395
396   G_UNLOCK (loaded_dirs);
397 }
398
399 struct _GIOExtension {
400   char *name;
401   GType type;
402   gint priority;
403 };
404
405 struct _GIOExtensionPoint {
406   GType required_type;
407   char *name;
408   GList *extensions;
409 };
410
411 static GHashTable *extension_points = NULL;
412 G_LOCK_DEFINE_STATIC(extension_points);
413
414
415 static void
416 g_io_extension_point_free (GIOExtensionPoint *ep)
417 {
418   g_free (ep->name);
419   g_free (ep);
420 }
421
422 /**
423  * g_io_extension_point_register:
424  * @name: The name of the extension point
425  *
426  * Registers an extension point.
427  *
428  * Returns: the new #GIOExtensionPoint. This object is owned by GIO
429  *    and should not be freed
430  */
431 GIOExtensionPoint *
432 g_io_extension_point_register (const char *name)
433 {
434   GIOExtensionPoint *ep;
435   
436   G_LOCK (extension_points);
437   if (extension_points == NULL)
438     extension_points = g_hash_table_new_full (g_str_hash,
439                                               g_str_equal,
440                                               NULL,
441                                               (GDestroyNotify)g_io_extension_point_free);
442
443   if (g_hash_table_lookup (extension_points, name) != NULL)
444     {
445       g_warning ("Extension point %s registered multiple times", name);
446       G_UNLOCK (extension_points);
447       return NULL;
448     }
449
450   ep = g_new0 (GIOExtensionPoint, 1);
451   ep->name = g_strdup (name);
452   
453   g_hash_table_insert (extension_points, ep->name, ep);
454   
455   G_UNLOCK (extension_points);
456
457   return ep;
458 }
459
460 /**
461  * g_io_extension_point_lookup:
462  * @name: the name of the extension point
463  *
464  * Looks up an existing extension point.
465  *
466  * Returns: the #GIOExtensionPoint, or %NULL if there is no
467  *    registered extension point with the given name
468  */
469 GIOExtensionPoint *
470 g_io_extension_point_lookup (const char *name)
471 {
472   GIOExtensionPoint *ep;
473
474   G_LOCK (extension_points);
475   ep = NULL;
476   if (extension_points != NULL)
477     ep = g_hash_table_lookup (extension_points, name);
478   
479   G_UNLOCK (extension_points);
480
481   return ep;
482   
483 }
484
485 /**
486  * g_io_extension_point_set_required_type:
487  * @extension_point: a #GIOExtensionPoint
488  * @type: the #GType to require
489  *
490  * Sets the required type for @extension_point to @type. 
491  * All implementations must henceforth have this type.
492  */
493 void
494 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
495                                         GType              type)
496 {
497   extension_point->required_type = type;
498 }
499
500 /**
501  * g_io_extension_point_get_required_type:
502  * @extension_point: a #GIOExtensionPoint
503  *
504  * Gets the required type for @extension_point.
505  *
506  * Returns: the #GType that all implementations must have, 
507  *     or #G_TYPE_INVALID if the extension point has no required type
508  */
509 GType
510 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
511 {
512   return extension_point->required_type;
513 }
514
515 /**
516  * g_io_extension_point_get_extensions:
517  * @extension_point: a #GIOExtensionPoint
518  *
519  * Gets a list of all extensions that implement this extension point.
520  * The list is sorted by priority, beginning with the highest priority.
521  * 
522  * Returns: a #GList of #GIOExtension<!-- -->s. The list is owned by
523  *   GIO and should not be modified
524  */
525 GList *
526 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
527 {
528   return extension_point->extensions;
529 }
530
531 /**
532  * g_io_extension_point_get_extension_by_name:
533  * @extension_point: a #GIOExtensionPoint
534  * @name: the name of the extension to get
535  *
536  * Finds a #GIOExtension for an extension point by name.
537  *
538  * Returns: the #GIOExtension for @extension_point that has the
539  *    given name, or %NULL if there is no extension with that name
540  */
541 GIOExtension *
542 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
543                                             const char        *name)
544 {
545   GList *l;
546
547   for (l = extension_point->extensions; l != NULL; l = l->next)
548     {
549       GIOExtension *e = l->data;
550
551       if (e->name != NULL &&
552           strcmp (e->name, name) == 0)
553         return e;
554     }
555   
556   return NULL;
557 }
558
559 static gint
560 extension_prio_compare (gconstpointer  a,
561                         gconstpointer  b)
562 {
563   const GIOExtension *extension_a = a, *extension_b = b;
564
565   return extension_b->priority - extension_a->priority;
566 }
567
568 /**
569  * g_io_extension_point_implement:
570  * @extension_point_name: the name of the extension point
571  * @type: the #GType to register as extension 
572  * @extension_name: the name for the extension
573  * @priority: the priority for the extension
574  *
575  * Registers @type as extension for the extension point with name
576  * @extension_point_name. 
577  *
578  * If @type has already been registered as an extension for this 
579  * extension point, the existing #GIOExtension object is returned.
580  *
581  * Returns: a #GIOExtension object for #GType
582  */
583 GIOExtension *
584 g_io_extension_point_implement (const char *extension_point_name,
585                                 GType       type,
586                                 const char *extension_name,
587                                 gint        priority)
588 {
589   GIOExtensionPoint *extension_point;
590   GIOExtension *extension;
591   GList *l;
592
593   g_return_val_if_fail (extension_point_name != NULL, NULL);
594
595   extension_point = g_io_extension_point_lookup (extension_point_name);
596   if (extension_point == NULL)
597     {
598       g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
599       return NULL;
600     }
601   
602   if (extension_point->required_type != 0 &&
603       !g_type_is_a (type, extension_point->required_type))
604     {
605       g_warning ("Tried to register an extension of the type %s to extension point %s. "
606                  "Expected type is %s.",
607                  g_type_name (type),
608                  extension_point_name, 
609                  g_type_name (extension_point->required_type));
610       return NULL;
611     }      
612
613   /* It's safe to register the same type multiple times */
614   for (l = extension_point->extensions; l != NULL; l = l->next)
615     {
616       extension = l->data;
617       if (extension->type == type)
618         return extension;
619     }
620   
621   extension = g_slice_new0 (GIOExtension);
622   extension->type = type;
623   extension->name = g_strdup (extension_name);
624   extension->priority = priority;
625   
626   extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
627                                                       extension, extension_prio_compare);
628   
629   return extension;
630 }
631
632 /**
633  * g_io_extension_ref_class:
634  * @extension: a #GIOExtension
635  *
636  * Gets a reference to the class for the type that is 
637  * associated with @extension.
638  *
639  * Returns: the #GTypeClass for the type of @extension
640  */
641 GTypeClass *
642 g_io_extension_ref_class (GIOExtension *extension)
643 {
644   return g_type_class_ref (extension->type);
645 }
646
647 /**
648  * g_io_extension_get_type:
649  * @extension: a #GIOExtension
650  *
651  * Gets the type associated with @extension.
652  *
653  * Returns: the type of @extension
654  */
655 GType
656 g_io_extension_get_type (GIOExtension *extension)
657 {
658   return extension->type;
659 }
660
661 /**
662  * g_io_extension_get_name:
663  * @extension: a #GIOExtension
664  *
665  * Gets the name under which @extension was registered.
666  *
667  * Note that the same type may be registered as extension
668  * for multiple extension points, under different names.
669  *
670  * Returns: the name of @extension.
671  */
672 const char *
673 g_io_extension_get_name (GIOExtension *extension)
674 {
675   return extension->name;
676 }
677
678 /**
679  * g_io_extension_get_priority:
680  * @extension: a #GIOExtension
681  *
682  * Gets the priority with which @extension was registered.
683  *
684  * Returns: the priority of @extension
685  */
686 gint
687 g_io_extension_get_priority (GIOExtension *extension)
688 {
689   return extension->priority;
690 }
691
692 #define __G_IO_MODULE_C__
693 #include "gioaliasdef.c"