unicode: Simplify width table generation
[glib.git] / gio / giomodule.c
blob510f652d884ab87e3a5b9587bbbaed3f348606b6
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
23 #include <string.h>
25 #include "giomodule.h"
26 #include "giomodule-priv.h"
27 #include "glocalfilemonitor.h"
28 #include "glocaldirectorymonitor.h"
29 #include "gnativevolumemonitor.h"
30 #include "gproxyresolver.h"
31 #include "gproxy.h"
32 #include "gsettingsbackendinternal.h"
33 #include "gsocks4proxy.h"
34 #include "gsocks4aproxy.h"
35 #include "gsocks5proxy.h"
36 #include "gtlsbackend.h"
37 #include "gvfs.h"
38 #include "gnotificationbackend.h"
39 #ifdef G_OS_WIN32
40 #include "gregistrysettingsbackend.h"
41 #endif
42 #include <glib/gstdio.h>
44 #ifdef G_OS_UNIX
45 #include "gdesktopappinfo.h"
46 #endif
48 /**
49 * SECTION:giomodule
50 * @short_description: Loadable GIO Modules
51 * @include: gio/gio.h
53 * Provides an interface and default functions for loading and unloading
54 * modules. This is used internally to make GIO extensible, but can also
55 * be used by others to implement module loading.
57 **/
59 /**
60 * SECTION:extensionpoints
61 * @short_description: Extension Points
62 * @include: gio.h
63 * @see_also: [Extending GIO][extending-gio]
65 * #GIOExtensionPoint provides a mechanism for modules to extend the
66 * functionality of the library or application that loaded it in an
67 * organized fashion.
69 * An extension point is identified by a name, and it may optionally
70 * require that any implementation must be of a certain type (or derived
71 * thereof). Use g_io_extension_point_register() to register an
72 * extension point, and g_io_extension_point_set_required_type() to
73 * set a required type.
75 * A module can implement an extension point by specifying the #GType
76 * that implements the functionality. Additionally, each implementation
77 * of an extension point has a name, and a priority. Use
78 * g_io_extension_point_implement() to implement an extension point.
80 * |[<!-- language="C" -->
81 * GIOExtensionPoint *ep;
83 * // Register an extension point
84 * ep = g_io_extension_point_register ("my-extension-point");
85 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
86 * ]|
88 * |[<!-- language="C" -->
89 * // Implement an extension point
90 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
91 * g_io_extension_point_implement ("my-extension-point",
92 * my_example_impl_get_type (),
93 * "my-example",
94 * 10);
95 * ]|
97 * It is up to the code that registered the extension point how
98 * it uses the implementations that have been associated with it.
99 * Depending on the use case, it may use all implementations, or
100 * only the one with the highest priority, or pick a specific
101 * one by name.
103 * To avoid opening all modules just to find out what extension
104 * points they implement, GIO makes use of a caching mechanism,
105 * see [gio-querymodules][gio-querymodules].
106 * You are expected to run this command after installing a
107 * GIO module.
109 * The `GIO_EXTRA_MODULES` environment variable can be used to
110 * specify additional directories to automatically load modules
111 * from. This environment variable has the same syntax as the
112 * `PATH`. If two modules have the same base name in different
113 * directories, then the latter one will be ignored. If additional
114 * directories are specified GIO will load modules from the built-in
115 * directory last.
119 * GIOModuleScope:
121 * Represents a scope for loading IO modules. A scope can be used for blocking
122 * duplicate modules, or blocking a module you don't want to load.
124 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
125 * or g_io_modules_scan_all_in_directory_with_scope().
127 * Since: 2.30
129 struct _GIOModuleScope {
130 GIOModuleScopeFlags flags;
131 GHashTable *basenames;
135 * g_io_module_scope_new:
136 * @flags: flags for the new scope
138 * Create a new scope for loading of IO modules. A scope can be used for
139 * blocking duplicate modules, or blocking a module you don't want to load.
141 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
142 * which have the same base name as a module that has already been seen
143 * in this scope.
145 * Returns: (transfer full): the new module scope
147 * Since: 2.30
149 GIOModuleScope *
150 g_io_module_scope_new (GIOModuleScopeFlags flags)
152 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
153 scope->flags = flags;
154 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
155 return scope;
159 * g_io_module_scope_free:
160 * @scope: a module loading scope
162 * Free a module scope.
164 * Since: 2.30
166 void
167 g_io_module_scope_free (GIOModuleScope *scope)
169 if (!scope)
170 return;
171 g_hash_table_destroy (scope->basenames);
172 g_free (scope);
176 * g_io_module_scope_block:
177 * @scope: a module loading scope
178 * @basename: the basename to block
180 * Block modules with the given @basename from being loaded when
181 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
182 * or g_io_modules_load_all_in_directory_with_scope().
184 * Since: 2.30
186 void
187 g_io_module_scope_block (GIOModuleScope *scope,
188 const gchar *basename)
190 gchar *key;
192 g_return_if_fail (scope != NULL);
193 g_return_if_fail (basename != NULL);
195 key = g_strdup (basename);
196 g_hash_table_insert (scope->basenames, key, key);
199 static gboolean
200 _g_io_module_scope_contains (GIOModuleScope *scope,
201 const gchar *basename)
203 return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE;
206 struct _GIOModule {
207 GTypeModule parent_instance;
209 gchar *filename;
210 GModule *library;
211 gboolean initialized; /* The module was loaded at least once */
213 void (* load) (GIOModule *module);
214 void (* unload) (GIOModule *module);
217 struct _GIOModuleClass
219 GTypeModuleClass parent_class;
223 static void g_io_module_finalize (GObject *object);
224 static gboolean g_io_module_load_module (GTypeModule *gmodule);
225 static void g_io_module_unload_module (GTypeModule *gmodule);
227 struct _GIOExtension {
228 char *name;
229 GType type;
230 gint priority;
233 struct _GIOExtensionPoint {
234 GType required_type;
235 char *name;
236 GList *extensions;
237 GList *lazy_load_modules;
240 static GHashTable *extension_points = NULL;
241 G_LOCK_DEFINE_STATIC(extension_points);
243 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
245 static void
246 g_io_module_class_init (GIOModuleClass *class)
248 GObjectClass *object_class = G_OBJECT_CLASS (class);
249 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
251 object_class->finalize = g_io_module_finalize;
253 type_module_class->load = g_io_module_load_module;
254 type_module_class->unload = g_io_module_unload_module;
257 static void
258 g_io_module_init (GIOModule *module)
262 static void
263 g_io_module_finalize (GObject *object)
265 GIOModule *module = G_IO_MODULE (object);
267 g_free (module->filename);
269 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
272 static gboolean
273 g_io_module_load_module (GTypeModule *gmodule)
275 GIOModule *module = G_IO_MODULE (gmodule);
277 if (!module->filename)
279 g_warning ("GIOModule path not set");
280 return FALSE;
283 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
285 if (!module->library)
287 g_printerr ("%s\n", g_module_error ());
288 return FALSE;
291 /* Make sure that the loaded library contains the required methods */
292 if (! g_module_symbol (module->library,
293 "g_io_module_load",
294 (gpointer) &module->load) ||
295 ! g_module_symbol (module->library,
296 "g_io_module_unload",
297 (gpointer) &module->unload))
299 g_printerr ("%s\n", g_module_error ());
300 g_module_close (module->library);
302 return FALSE;
305 /* Initialize the loaded module */
306 module->load (module);
307 module->initialized = TRUE;
309 return TRUE;
312 static void
313 g_io_module_unload_module (GTypeModule *gmodule)
315 GIOModule *module = G_IO_MODULE (gmodule);
317 module->unload (module);
319 g_module_close (module->library);
320 module->library = NULL;
322 module->load = NULL;
323 module->unload = NULL;
327 * g_io_module_new:
328 * @filename: filename of the shared library module.
330 * Creates a new GIOModule that will load the specific
331 * shared library when in use.
333 * Returns: a #GIOModule from given @filename,
334 * or %NULL on error.
336 GIOModule *
337 g_io_module_new (const gchar *filename)
339 GIOModule *module;
341 g_return_val_if_fail (filename != NULL, NULL);
343 module = g_object_new (G_IO_TYPE_MODULE, NULL);
344 module->filename = g_strdup (filename);
346 return module;
349 static gboolean
350 is_valid_module_name (const gchar *basename,
351 GIOModuleScope *scope)
353 gboolean result;
355 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
356 if (!g_str_has_prefix (basename, "lib") ||
357 !g_str_has_suffix (basename, ".so"))
358 return FALSE;
359 #else
360 if (!g_str_has_suffix (basename, ".dll"))
361 return FALSE;
362 #endif
364 result = TRUE;
365 if (scope)
367 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
368 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
369 g_io_module_scope_block (scope, basename);
372 return result;
377 * g_io_modules_scan_all_in_directory_with_scope:
378 * @dirname: pathname for a directory containing modules to scan.
379 * @scope: a scope to use when scanning the modules
381 * Scans all the modules in the specified directory, ensuring that
382 * any extension point implemented by a module is registered.
384 * This may not actually load and initialize all the types in each
385 * module, some modules may be lazily loaded and initialized when
386 * an extension point it implementes is used with e.g.
387 * g_io_extension_point_get_extensions() or
388 * g_io_extension_point_get_extension_by_name().
390 * If you need to guarantee that all types are loaded in all the modules,
391 * use g_io_modules_load_all_in_directory().
393 * Since: 2.30
395 void
396 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
397 GIOModuleScope *scope)
399 const gchar *name;
400 char *filename;
401 GDir *dir;
402 GStatBuf statbuf;
403 char *data;
404 time_t cache_mtime;
405 GHashTable *cache;
407 if (!g_module_supported ())
408 return;
410 dir = g_dir_open (dirname, 0, NULL);
411 if (!dir)
412 return;
414 filename = g_build_filename (dirname, "giomodule.cache", NULL);
416 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
417 g_free, (GDestroyNotify)g_strfreev);
419 cache_mtime = 0;
420 if (g_stat (filename, &statbuf) == 0 &&
421 g_file_get_contents (filename, &data, NULL, NULL))
423 char **lines;
424 int i;
426 /* Cache mtime is the time the cache file was created, any file
427 * that has a ctime before this was created then and not modified
428 * since then (userspace can't change ctime). Its possible to change
429 * the ctime forward without changing the file content, by e.g.
430 * chmoding the file, but this is uncommon and will only cause us
431 * to not use the cache so will not cause bugs.
433 cache_mtime = statbuf.st_mtime;
435 lines = g_strsplit (data, "\n", -1);
436 g_free (data);
438 for (i = 0; lines[i] != NULL; i++)
440 char *line = lines[i];
441 char *file;
442 char *colon;
443 char **extension_points;
445 if (line[0] == '#')
446 continue;
448 colon = strchr (line, ':');
449 if (colon == NULL || line == colon)
450 continue; /* Invalid line, ignore */
452 *colon = 0; /* terminate filename */
453 file = g_strdup (line);
454 colon++; /* after colon */
456 while (g_ascii_isspace (*colon))
457 colon++;
459 extension_points = g_strsplit (colon, ",", -1);
460 g_hash_table_insert (cache, file, extension_points);
462 g_strfreev (lines);
465 while ((name = g_dir_read_name (dir)))
467 if (is_valid_module_name (name, scope))
469 GIOExtensionPoint *extension_point;
470 GIOModule *module;
471 gchar *path;
472 char **extension_points;
473 int i;
475 path = g_build_filename (dirname, name, NULL);
476 module = g_io_module_new (path);
478 extension_points = g_hash_table_lookup (cache, name);
479 if (extension_points != NULL &&
480 g_stat (path, &statbuf) == 0 &&
481 statbuf.st_ctime <= cache_mtime)
483 /* Lazy load/init the library when first required */
484 for (i = 0; extension_points[i] != NULL; i++)
486 extension_point =
487 g_io_extension_point_register (extension_points[i]);
488 extension_point->lazy_load_modules =
489 g_list_prepend (extension_point->lazy_load_modules,
490 module);
493 else
495 /* Try to load and init types */
496 if (g_type_module_use (G_TYPE_MODULE (module)))
497 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
498 else
499 { /* Failure to load */
500 g_printerr ("Failed to load module: %s\n", path);
501 g_object_unref (module);
502 g_free (path);
503 continue;
507 g_free (path);
511 g_dir_close (dir);
513 g_hash_table_destroy (cache);
515 g_free (filename);
519 * g_io_modules_scan_all_in_directory:
520 * @dirname: pathname for a directory containing modules to scan.
522 * Scans all the modules in the specified directory, ensuring that
523 * any extension point implemented by a module is registered.
525 * This may not actually load and initialize all the types in each
526 * module, some modules may be lazily loaded and initialized when
527 * an extension point it implementes is used with e.g.
528 * g_io_extension_point_get_extensions() or
529 * g_io_extension_point_get_extension_by_name().
531 * If you need to guarantee that all types are loaded in all the modules,
532 * use g_io_modules_load_all_in_directory().
534 * Since: 2.24
536 void
537 g_io_modules_scan_all_in_directory (const char *dirname)
539 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
543 * g_io_modules_load_all_in_directory_with_scope:
544 * @dirname: pathname for a directory containing modules to load.
545 * @scope: a scope to use when scanning the modules.
547 * Loads all the modules in the specified directory.
549 * If don't require all modules to be initialized (and thus registering
550 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
551 * which allows delayed/lazy loading of modules.
553 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
554 * from the directory,
555 * All the modules are loaded into memory, if you want to
556 * unload them (enabling on-demand loading) you must call
557 * g_type_module_unuse() on all the modules. Free the list
558 * with g_list_free().
560 * Since: 2.30
562 GList *
563 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
564 GIOModuleScope *scope)
566 const gchar *name;
567 GDir *dir;
568 GList *modules;
570 if (!g_module_supported ())
571 return NULL;
573 dir = g_dir_open (dirname, 0, NULL);
574 if (!dir)
575 return NULL;
577 modules = NULL;
578 while ((name = g_dir_read_name (dir)))
580 if (is_valid_module_name (name, scope))
582 GIOModule *module;
583 gchar *path;
585 path = g_build_filename (dirname, name, NULL);
586 module = g_io_module_new (path);
588 if (!g_type_module_use (G_TYPE_MODULE (module)))
590 g_printerr ("Failed to load module: %s\n", path);
591 g_object_unref (module);
592 g_free (path);
593 continue;
596 g_free (path);
598 modules = g_list_prepend (modules, module);
602 g_dir_close (dir);
604 return modules;
608 * g_io_modules_load_all_in_directory:
609 * @dirname: pathname for a directory containing modules to load.
611 * Loads all the modules in the specified directory.
613 * If don't require all modules to be initialized (and thus registering
614 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
615 * which allows delayed/lazy loading of modules.
617 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
618 * from the directory,
619 * All the modules are loaded into memory, if you want to
620 * unload them (enabling on-demand loading) you must call
621 * g_type_module_unuse() on all the modules. Free the list
622 * with g_list_free().
624 GList *
625 g_io_modules_load_all_in_directory (const char *dirname)
627 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
630 static gpointer
631 try_class (GIOExtension *extension,
632 guint is_supported_offset)
634 GType type = g_io_extension_get_type (extension);
635 typedef gboolean (*verify_func) (void);
636 gpointer class;
638 class = g_type_class_ref (type);
639 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
640 return class;
642 g_type_class_unref (class);
643 return NULL;
647 * _g_io_module_get_default_type:
648 * @extension_point: the name of an extension point
649 * @envvar: (allow-none): the name of an environment variable to
650 * override the default implementation.
651 * @is_supported_offset: a vtable offset, or zero
653 * Retrieves the default class implementing @extension_point.
655 * If @envvar is not %NULL, and the environment variable with that
656 * name is set, then the implementation it specifies will be tried
657 * first. After that, or if @envvar is not set, all other
658 * implementations will be tried in order of decreasing priority.
660 * If @is_supported_offset is non-zero, then it is the offset into the
661 * class vtable at which there is a function that takes no arguments and
662 * returns a boolean. This function will be called on each candidate
663 * implementation to check if it is actually usable or not.
665 * The result is cached after it is generated the first time, and
666 * the function is thread-safe.
668 * Returns: (transfer none): an object implementing
669 * @extension_point, or %NULL if there are no usable
670 * implementations.
672 GType
673 _g_io_module_get_default_type (const gchar *extension_point,
674 const gchar *envvar,
675 guint is_supported_offset)
677 static GRecMutex default_modules_lock;
678 static GHashTable *default_modules;
679 const char *use_this;
680 GList *l;
681 GIOExtensionPoint *ep;
682 GIOExtension *extension, *preferred;
683 gpointer impl;
685 g_rec_mutex_lock (&default_modules_lock);
686 if (default_modules)
688 gpointer key;
690 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
692 g_rec_mutex_unlock (&default_modules_lock);
693 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
696 else
698 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
701 _g_io_modules_ensure_loaded ();
702 ep = g_io_extension_point_lookup (extension_point);
704 if (!ep)
706 g_warn_if_reached ();
707 g_rec_mutex_unlock (&default_modules_lock);
708 return G_TYPE_INVALID;
711 use_this = envvar ? g_getenv (envvar) : NULL;
712 if (use_this)
714 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
715 if (preferred)
717 impl = try_class (preferred, is_supported_offset);
718 if (impl)
719 goto done;
721 else
722 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
724 else
725 preferred = NULL;
727 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
729 extension = l->data;
730 if (extension == preferred)
731 continue;
733 impl = try_class (extension, is_supported_offset);
734 if (impl)
735 goto done;
738 impl = NULL;
740 done:
741 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
742 g_rec_mutex_unlock (&default_modules_lock);
744 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
747 static gpointer
748 try_implementation (GIOExtension *extension,
749 GIOModuleVerifyFunc verify_func)
751 GType type = g_io_extension_get_type (extension);
752 gpointer impl;
754 if (g_type_is_a (type, G_TYPE_INITABLE))
755 return g_initable_new (type, NULL, NULL, NULL);
756 else
758 impl = g_object_new (type, NULL);
759 if (!verify_func || verify_func (impl))
760 return impl;
762 g_object_unref (impl);
763 return NULL;
768 * _g_io_module_get_default:
769 * @extension_point: the name of an extension point
770 * @envvar: (allow-none): the name of an environment variable to
771 * override the default implementation.
772 * @verify_func: (allow-none): a function to call to verify that
773 * a given implementation is usable in the current environment.
775 * Retrieves the default object implementing @extension_point.
777 * If @envvar is not %NULL, and the environment variable with that
778 * name is set, then the implementation it specifies will be tried
779 * first. After that, or if @envvar is not set, all other
780 * implementations will be tried in order of decreasing priority.
782 * If an extension point implementation implements #GInitable, then
783 * that implementation will only be used if it initializes
784 * successfully. Otherwise, if @verify_func is not %NULL, then it will
785 * be called on each candidate implementation after construction, to
786 * check if it is actually usable or not.
788 * The result is cached after it is generated the first time, and
789 * the function is thread-safe.
791 * Returns: (transfer none): an object implementing
792 * @extension_point, or %NULL if there are no usable
793 * implementations.
795 gpointer
796 _g_io_module_get_default (const gchar *extension_point,
797 const gchar *envvar,
798 GIOModuleVerifyFunc verify_func)
800 static GRecMutex default_modules_lock;
801 static GHashTable *default_modules;
802 const char *use_this;
803 GList *l;
804 GIOExtensionPoint *ep;
805 GIOExtension *extension, *preferred;
806 gpointer impl;
808 g_rec_mutex_lock (&default_modules_lock);
809 if (default_modules)
811 gpointer key;
813 if (g_hash_table_lookup_extended (default_modules, extension_point,
814 &key, &impl))
816 g_rec_mutex_unlock (&default_modules_lock);
817 return impl;
820 else
822 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
825 _g_io_modules_ensure_loaded ();
826 ep = g_io_extension_point_lookup (extension_point);
828 if (!ep)
830 g_warn_if_reached ();
831 g_rec_mutex_unlock (&default_modules_lock);
832 return NULL;
835 use_this = envvar ? g_getenv (envvar) : NULL;
836 if (use_this)
838 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
839 if (preferred)
841 impl = try_implementation (preferred, verify_func);
842 if (impl)
843 goto done;
845 else
846 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
848 else
849 preferred = NULL;
851 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
853 extension = l->data;
854 if (extension == preferred)
855 continue;
857 impl = try_implementation (extension, verify_func);
858 if (impl)
859 goto done;
862 impl = NULL;
864 done:
865 g_hash_table_insert (default_modules,
866 g_strdup (extension_point),
867 impl ? g_object_ref (impl) : NULL);
868 g_rec_mutex_unlock (&default_modules_lock);
870 return impl;
873 G_LOCK_DEFINE_STATIC (registered_extensions);
874 G_LOCK_DEFINE_STATIC (loaded_dirs);
876 extern GType _g_fen_directory_monitor_get_type (void);
877 extern GType _g_fen_file_monitor_get_type (void);
878 extern GType _g_inotify_directory_monitor_get_type (void);
879 extern GType _g_inotify_file_monitor_get_type (void);
880 extern GType _g_kqueue_directory_monitor_get_type (void);
881 extern GType _g_kqueue_file_monitor_get_type (void);
882 extern GType _g_unix_volume_monitor_get_type (void);
883 extern GType _g_local_vfs_get_type (void);
885 extern GType _g_win32_volume_monitor_get_type (void);
886 extern GType g_win32_directory_monitor_get_type (void);
887 extern GType _g_winhttp_vfs_get_type (void);
889 extern GType _g_dummy_proxy_resolver_get_type (void);
890 extern GType _g_dummy_tls_backend_get_type (void);
891 extern GType g_network_monitor_base_get_type (void);
892 #ifdef HAVE_NETLINK
893 extern GType _g_network_monitor_netlink_get_type (void);
894 #endif
896 #ifdef G_OS_UNIX
897 extern GType g_fdo_notification_backend_get_type (void);
898 extern GType g_gtk_notification_backend_get_type (void);
899 #endif
901 #ifdef G_PLATFORM_WIN32
903 #include <windows.h>
905 static HMODULE gio_dll = NULL;
907 #ifdef DLL_EXPORT
909 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
910 DWORD fdwReason,
911 LPVOID lpvReserved);
913 BOOL WINAPI
914 DllMain (HINSTANCE hinstDLL,
915 DWORD fdwReason,
916 LPVOID lpvReserved)
918 if (fdwReason == DLL_PROCESS_ATTACH)
919 gio_dll = hinstDLL;
921 return TRUE;
924 #endif
926 void *
927 _g_io_win32_get_module (void)
929 if (!gio_dll)
930 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
931 (const char *) _g_io_win32_get_module,
932 &gio_dll);
933 return gio_dll;
936 #undef GIO_MODULE_DIR
938 /* GIO_MODULE_DIR is used only in code called just once,
939 * so no problem leaking this
941 #define GIO_MODULE_DIR \
942 g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
943 "lib/gio/modules", \
944 NULL)
946 #endif
948 void
949 _g_io_modules_ensure_extension_points_registered (void)
951 static gboolean registered_extensions = FALSE;
952 GIOExtensionPoint *ep;
954 G_LOCK (registered_extensions);
956 if (!registered_extensions)
958 registered_extensions = TRUE;
960 #ifdef G_OS_UNIX
961 #if !GLIB_CHECK_VERSION (3, 0, 0)
962 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
963 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
964 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
965 G_GNUC_END_IGNORE_DEPRECATIONS
966 #endif
967 #endif
969 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
970 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
972 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
973 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
975 ep = g_io_extension_point_register (G_NFS_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
976 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
978 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
979 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
981 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
982 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
984 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
985 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
987 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
988 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
990 ep = g_io_extension_point_register ("gsettings-backend");
991 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
993 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
994 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
996 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
997 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
999 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1000 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1002 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1003 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1005 ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1006 g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1009 G_UNLOCK (registered_extensions);
1012 void
1013 _g_io_modules_ensure_loaded (void)
1015 static gboolean loaded_dirs = FALSE;
1016 const char *module_path;
1017 GIOModuleScope *scope;
1018 const gchar *module_dir;
1020 _g_io_modules_ensure_extension_points_registered ();
1022 G_LOCK (loaded_dirs);
1024 if (!loaded_dirs)
1026 loaded_dirs = TRUE;
1027 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1029 /* First load any overrides, extras */
1030 module_path = g_getenv ("GIO_EXTRA_MODULES");
1031 if (module_path)
1033 gchar **paths;
1034 int i;
1036 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1038 for (i = 0; paths[i] != NULL; i++)
1040 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1043 g_strfreev (paths);
1046 /* Then load the compiled in path */
1047 module_dir = g_getenv ("GIO_MODULE_DIR");
1048 if (module_dir == NULL)
1049 module_dir = GIO_MODULE_DIR;
1051 g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1053 g_io_module_scope_free (scope);
1055 /* Initialize types from built-in "modules" */
1056 g_type_ensure (g_null_settings_backend_get_type ());
1057 g_type_ensure (g_memory_settings_backend_get_type ());
1058 #if defined(HAVE_INOTIFY_INIT1)
1059 g_type_ensure (_g_inotify_directory_monitor_get_type ());
1060 g_type_ensure (_g_inotify_file_monitor_get_type ());
1061 #endif
1062 #if defined(HAVE_KQUEUE)
1063 g_type_ensure (_g_kqueue_directory_monitor_get_type ());
1064 g_type_ensure (_g_kqueue_file_monitor_get_type ());
1065 #endif
1066 #if defined(HAVE_FEN)
1067 g_type_ensure (_g_fen_directory_monitor_get_type ());
1068 g_type_ensure (_g_fen_file_monitor_get_type ());
1069 #endif
1070 #ifdef G_OS_WIN32
1071 g_type_ensure (_g_win32_volume_monitor_get_type ());
1072 g_type_ensure (g_win32_directory_monitor_get_type ());
1073 g_type_ensure (g_registry_backend_get_type ());
1074 #endif
1075 #ifdef HAVE_COCOA
1076 g_nextstep_settings_backend_get_type ();
1077 #endif
1078 #ifdef G_OS_UNIX
1079 g_type_ensure (_g_unix_volume_monitor_get_type ());
1080 g_type_ensure (g_fdo_notification_backend_get_type ());
1081 g_type_ensure (g_gtk_notification_backend_get_type ());
1082 #endif
1083 #ifdef G_OS_WIN32
1084 g_type_ensure (_g_winhttp_vfs_get_type ());
1085 #endif
1086 g_type_ensure (_g_local_vfs_get_type ());
1087 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1088 g_type_ensure (_g_socks4a_proxy_get_type ());
1089 g_type_ensure (_g_socks4_proxy_get_type ());
1090 g_type_ensure (_g_socks5_proxy_get_type ());
1091 g_type_ensure (_g_dummy_tls_backend_get_type ());
1092 g_type_ensure (g_network_monitor_base_get_type ());
1093 #ifdef HAVE_NETLINK
1094 g_type_ensure (_g_network_monitor_netlink_get_type ());
1095 #endif
1098 G_UNLOCK (loaded_dirs);
1101 static void
1102 g_io_extension_point_free (GIOExtensionPoint *ep)
1104 g_free (ep->name);
1105 g_free (ep);
1109 * g_io_extension_point_register:
1110 * @name: The name of the extension point
1112 * Registers an extension point.
1114 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1115 * owned by GIO and should not be freed.
1117 GIOExtensionPoint *
1118 g_io_extension_point_register (const char *name)
1120 GIOExtensionPoint *ep;
1122 G_LOCK (extension_points);
1123 if (extension_points == NULL)
1124 extension_points = g_hash_table_new_full (g_str_hash,
1125 g_str_equal,
1126 NULL,
1127 (GDestroyNotify)g_io_extension_point_free);
1129 ep = g_hash_table_lookup (extension_points, name);
1130 if (ep != NULL)
1132 G_UNLOCK (extension_points);
1133 return ep;
1136 ep = g_new0 (GIOExtensionPoint, 1);
1137 ep->name = g_strdup (name);
1139 g_hash_table_insert (extension_points, ep->name, ep);
1141 G_UNLOCK (extension_points);
1143 return ep;
1147 * g_io_extension_point_lookup:
1148 * @name: the name of the extension point
1150 * Looks up an existing extension point.
1152 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1153 * is no registered extension point with the given name.
1155 GIOExtensionPoint *
1156 g_io_extension_point_lookup (const char *name)
1158 GIOExtensionPoint *ep;
1160 G_LOCK (extension_points);
1161 ep = NULL;
1162 if (extension_points != NULL)
1163 ep = g_hash_table_lookup (extension_points, name);
1165 G_UNLOCK (extension_points);
1167 return ep;
1172 * g_io_extension_point_set_required_type:
1173 * @extension_point: a #GIOExtensionPoint
1174 * @type: the #GType to require
1176 * Sets the required type for @extension_point to @type.
1177 * All implementations must henceforth have this type.
1179 void
1180 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1181 GType type)
1183 extension_point->required_type = type;
1187 * g_io_extension_point_get_required_type:
1188 * @extension_point: a #GIOExtensionPoint
1190 * Gets the required type for @extension_point.
1192 * Returns: the #GType that all implementations must have,
1193 * or #G_TYPE_INVALID if the extension point has no required type
1195 GType
1196 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1198 return extension_point->required_type;
1201 static void
1202 lazy_load_modules (GIOExtensionPoint *extension_point)
1204 GIOModule *module;
1205 GList *l;
1207 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1209 module = l->data;
1211 if (!module->initialized)
1213 if (g_type_module_use (G_TYPE_MODULE (module)))
1214 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1215 else
1216 g_printerr ("Failed to load module: %s\n",
1217 module->filename);
1223 * g_io_extension_point_get_extensions:
1224 * @extension_point: a #GIOExtensionPoint
1226 * Gets a list of all extensions that implement this extension point.
1227 * The list is sorted by priority, beginning with the highest priority.
1229 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1230 * #GIOExtensions. The list is owned by GIO and should not be
1231 * modified.
1233 GList *
1234 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1236 lazy_load_modules (extension_point);
1237 return extension_point->extensions;
1241 * g_io_extension_point_get_extension_by_name:
1242 * @extension_point: a #GIOExtensionPoint
1243 * @name: the name of the extension to get
1245 * Finds a #GIOExtension for an extension point by name.
1247 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1248 * given name, or %NULL if there is no extension with that name
1250 GIOExtension *
1251 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1252 const char *name)
1254 GList *l;
1256 g_return_val_if_fail (name != NULL, NULL);
1258 lazy_load_modules (extension_point);
1259 for (l = extension_point->extensions; l != NULL; l = l->next)
1261 GIOExtension *e = l->data;
1263 if (e->name != NULL &&
1264 strcmp (e->name, name) == 0)
1265 return e;
1268 return NULL;
1271 static gint
1272 extension_prio_compare (gconstpointer a,
1273 gconstpointer b)
1275 const GIOExtension *extension_a = a, *extension_b = b;
1277 if (extension_a->priority > extension_b->priority)
1278 return -1;
1280 if (extension_b->priority > extension_a->priority)
1281 return 1;
1283 return 0;
1287 * g_io_extension_point_implement:
1288 * @extension_point_name: the name of the extension point
1289 * @type: the #GType to register as extension
1290 * @extension_name: the name for the extension
1291 * @priority: the priority for the extension
1293 * Registers @type as extension for the extension point with name
1294 * @extension_point_name.
1296 * If @type has already been registered as an extension for this
1297 * extension point, the existing #GIOExtension object is returned.
1299 * Returns: (transfer none): a #GIOExtension object for #GType
1301 GIOExtension *
1302 g_io_extension_point_implement (const char *extension_point_name,
1303 GType type,
1304 const char *extension_name,
1305 gint priority)
1307 GIOExtensionPoint *extension_point;
1308 GIOExtension *extension;
1309 GList *l;
1311 g_return_val_if_fail (extension_point_name != NULL, NULL);
1313 extension_point = g_io_extension_point_lookup (extension_point_name);
1314 if (extension_point == NULL)
1316 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1317 return NULL;
1320 if (extension_point->required_type != 0 &&
1321 !g_type_is_a (type, extension_point->required_type))
1323 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1324 "Expected type is %s.",
1325 g_type_name (type),
1326 extension_point_name,
1327 g_type_name (extension_point->required_type));
1328 return NULL;
1331 /* It's safe to register the same type multiple times */
1332 for (l = extension_point->extensions; l != NULL; l = l->next)
1334 extension = l->data;
1335 if (extension->type == type)
1336 return extension;
1339 extension = g_slice_new0 (GIOExtension);
1340 extension->type = type;
1341 extension->name = g_strdup (extension_name);
1342 extension->priority = priority;
1344 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1345 extension, extension_prio_compare);
1347 return extension;
1351 * g_io_extension_ref_class:
1352 * @extension: a #GIOExtension
1354 * Gets a reference to the class for the type that is
1355 * associated with @extension.
1357 * Returns: (transfer full): the #GTypeClass for the type of @extension
1359 GTypeClass *
1360 g_io_extension_ref_class (GIOExtension *extension)
1362 return g_type_class_ref (extension->type);
1366 * g_io_extension_get_type:
1367 * @extension: a #GIOExtension
1369 * Gets the type associated with @extension.
1371 * Returns: the type of @extension
1373 GType
1374 g_io_extension_get_type (GIOExtension *extension)
1376 return extension->type;
1380 * g_io_extension_get_name:
1381 * @extension: a #GIOExtension
1383 * Gets the name under which @extension was registered.
1385 * Note that the same type may be registered as extension
1386 * for multiple extension points, under different names.
1388 * Returns: the name of @extension.
1390 const char *
1391 g_io_extension_get_name (GIOExtension *extension)
1393 return extension->name;
1397 * g_io_extension_get_priority:
1398 * @extension: a #GIOExtension
1400 * Gets the priority with which @extension was registered.
1402 * Returns: the priority of @extension
1404 gint
1405 g_io_extension_get_priority (GIOExtension *extension)
1407 return extension->priority;