Added Scottish Gaelic translation
[glib.git] / gio / giomodule.c
blobda7c16796bf02d9c0ae440bda89675c409792e07
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 "gnativevolumemonitor.h"
29 #include "gproxyresolver.h"
30 #include "gproxy.h"
31 #include "gsettingsbackendinternal.h"
32 #include "ghttpproxy.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 #include "ginitable.h"
40 #include "gnetworkmonitor.h"
41 #ifdef G_OS_WIN32
42 #include "gregistrysettingsbackend.h"
43 #endif
44 #include <glib/gstdio.h>
46 #ifdef G_OS_UNIX
47 #include "gdesktopappinfo.h"
48 #endif
50 /**
51 * SECTION:giomodule
52 * @short_description: Loadable GIO Modules
53 * @include: gio/gio.h
55 * Provides an interface and default functions for loading and unloading
56 * modules. This is used internally to make GIO extensible, but can also
57 * be used by others to implement module loading.
59 **/
61 /**
62 * SECTION:extensionpoints
63 * @short_description: Extension Points
64 * @include: gio.h
65 * @see_also: [Extending GIO][extending-gio]
67 * #GIOExtensionPoint provides a mechanism for modules to extend the
68 * functionality of the library or application that loaded it in an
69 * organized fashion.
71 * An extension point is identified by a name, and it may optionally
72 * require that any implementation must be of a certain type (or derived
73 * thereof). Use g_io_extension_point_register() to register an
74 * extension point, and g_io_extension_point_set_required_type() to
75 * set a required type.
77 * A module can implement an extension point by specifying the #GType
78 * that implements the functionality. Additionally, each implementation
79 * of an extension point has a name, and a priority. Use
80 * g_io_extension_point_implement() to implement an extension point.
82 * |[<!-- language="C" -->
83 * GIOExtensionPoint *ep;
85 * // Register an extension point
86 * ep = g_io_extension_point_register ("my-extension-point");
87 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
88 * ]|
90 * |[<!-- language="C" -->
91 * // Implement an extension point
92 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
93 * g_io_extension_point_implement ("my-extension-point",
94 * my_example_impl_get_type (),
95 * "my-example",
96 * 10);
97 * ]|
99 * It is up to the code that registered the extension point how
100 * it uses the implementations that have been associated with it.
101 * Depending on the use case, it may use all implementations, or
102 * only the one with the highest priority, or pick a specific
103 * one by name.
105 * To avoid opening all modules just to find out what extension
106 * points they implement, GIO makes use of a caching mechanism,
107 * see [gio-querymodules][gio-querymodules].
108 * You are expected to run this command after installing a
109 * GIO module.
111 * The `GIO_EXTRA_MODULES` environment variable can be used to
112 * specify additional directories to automatically load modules
113 * from. This environment variable has the same syntax as the
114 * `PATH`. If two modules have the same base name in different
115 * directories, then the latter one will be ignored. If additional
116 * directories are specified GIO will load modules from the built-in
117 * directory last.
121 * GIOModuleScope:
123 * Represents a scope for loading IO modules. A scope can be used for blocking
124 * duplicate modules, or blocking a module you don't want to load.
126 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
127 * or g_io_modules_scan_all_in_directory_with_scope().
129 * Since: 2.30
131 struct _GIOModuleScope {
132 GIOModuleScopeFlags flags;
133 GHashTable *basenames;
137 * g_io_module_scope_new:
138 * @flags: flags for the new scope
140 * Create a new scope for loading of IO modules. A scope can be used for
141 * blocking duplicate modules, or blocking a module you don't want to load.
143 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
144 * which have the same base name as a module that has already been seen
145 * in this scope.
147 * Returns: (transfer full): the new module scope
149 * Since: 2.30
151 GIOModuleScope *
152 g_io_module_scope_new (GIOModuleScopeFlags flags)
154 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
155 scope->flags = flags;
156 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
157 return scope;
161 * g_io_module_scope_free:
162 * @scope: a module loading scope
164 * Free a module scope.
166 * Since: 2.30
168 void
169 g_io_module_scope_free (GIOModuleScope *scope)
171 if (!scope)
172 return;
173 g_hash_table_destroy (scope->basenames);
174 g_free (scope);
178 * g_io_module_scope_block:
179 * @scope: a module loading scope
180 * @basename: the basename to block
182 * Block modules with the given @basename from being loaded when
183 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
184 * or g_io_modules_load_all_in_directory_with_scope().
186 * Since: 2.30
188 void
189 g_io_module_scope_block (GIOModuleScope *scope,
190 const gchar *basename)
192 gchar *key;
194 g_return_if_fail (scope != NULL);
195 g_return_if_fail (basename != NULL);
197 key = g_strdup (basename);
198 g_hash_table_insert (scope->basenames, key, key);
201 static gboolean
202 _g_io_module_scope_contains (GIOModuleScope *scope,
203 const gchar *basename)
205 return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE;
208 struct _GIOModule {
209 GTypeModule parent_instance;
211 gchar *filename;
212 GModule *library;
213 gboolean initialized; /* The module was loaded at least once */
215 void (* load) (GIOModule *module);
216 void (* unload) (GIOModule *module);
219 struct _GIOModuleClass
221 GTypeModuleClass parent_class;
225 static void g_io_module_finalize (GObject *object);
226 static gboolean g_io_module_load_module (GTypeModule *gmodule);
227 static void g_io_module_unload_module (GTypeModule *gmodule);
230 * GIOExtension:
232 * #GIOExtension is an opaque data structure and can only be accessed
233 * using the following functions.
235 struct _GIOExtension {
236 char *name;
237 GType type;
238 gint priority;
242 * GIOExtensionPoint:
244 * #GIOExtensionPoint is an opaque data structure and can only be accessed
245 * using the following functions.
247 struct _GIOExtensionPoint {
248 GType required_type;
249 char *name;
250 GList *extensions;
251 GList *lazy_load_modules;
254 static GHashTable *extension_points = NULL;
255 G_LOCK_DEFINE_STATIC(extension_points);
257 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
259 static void
260 g_io_module_class_init (GIOModuleClass *class)
262 GObjectClass *object_class = G_OBJECT_CLASS (class);
263 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
265 object_class->finalize = g_io_module_finalize;
267 type_module_class->load = g_io_module_load_module;
268 type_module_class->unload = g_io_module_unload_module;
271 static void
272 g_io_module_init (GIOModule *module)
276 static void
277 g_io_module_finalize (GObject *object)
279 GIOModule *module = G_IO_MODULE (object);
281 g_free (module->filename);
283 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
286 static gboolean
287 g_io_module_load_module (GTypeModule *gmodule)
289 GIOModule *module = G_IO_MODULE (gmodule);
291 if (!module->filename)
293 g_warning ("GIOModule path not set");
294 return FALSE;
297 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
299 if (!module->library)
301 g_printerr ("%s\n", g_module_error ());
302 return FALSE;
305 /* Make sure that the loaded library contains the required methods */
306 if (! g_module_symbol (module->library,
307 "g_io_module_load",
308 (gpointer) &module->load) ||
309 ! g_module_symbol (module->library,
310 "g_io_module_unload",
311 (gpointer) &module->unload))
313 g_printerr ("%s\n", g_module_error ());
314 g_module_close (module->library);
316 return FALSE;
319 /* Initialize the loaded module */
320 module->load (module);
321 module->initialized = TRUE;
323 return TRUE;
326 static void
327 g_io_module_unload_module (GTypeModule *gmodule)
329 GIOModule *module = G_IO_MODULE (gmodule);
331 module->unload (module);
333 g_module_close (module->library);
334 module->library = NULL;
336 module->load = NULL;
337 module->unload = NULL;
341 * g_io_module_new:
342 * @filename: filename of the shared library module.
344 * Creates a new GIOModule that will load the specific
345 * shared library when in use.
347 * Returns: a #GIOModule from given @filename,
348 * or %NULL on error.
350 GIOModule *
351 g_io_module_new (const gchar *filename)
353 GIOModule *module;
355 g_return_val_if_fail (filename != NULL, NULL);
357 module = g_object_new (G_IO_TYPE_MODULE, NULL);
358 module->filename = g_strdup (filename);
360 return module;
363 static gboolean
364 is_valid_module_name (const gchar *basename,
365 GIOModuleScope *scope)
367 gboolean result;
369 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
370 if (!g_str_has_prefix (basename, "lib") ||
371 !g_str_has_suffix (basename, ".so"))
372 return FALSE;
373 #else
374 if (!g_str_has_suffix (basename, ".dll"))
375 return FALSE;
376 #endif
378 result = TRUE;
379 if (scope)
381 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
382 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
383 g_io_module_scope_block (scope, basename);
386 return result;
391 * g_io_modules_scan_all_in_directory_with_scope:
392 * @dirname: pathname for a directory containing modules to scan.
393 * @scope: a scope to use when scanning the modules
395 * Scans all the modules in the specified directory, ensuring that
396 * any extension point implemented by a module is registered.
398 * This may not actually load and initialize all the types in each
399 * module, some modules may be lazily loaded and initialized when
400 * an extension point it implementes is used with e.g.
401 * g_io_extension_point_get_extensions() or
402 * g_io_extension_point_get_extension_by_name().
404 * If you need to guarantee that all types are loaded in all the modules,
405 * use g_io_modules_load_all_in_directory().
407 * Since: 2.30
409 void
410 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
411 GIOModuleScope *scope)
413 const gchar *name;
414 char *filename;
415 GDir *dir;
416 GStatBuf statbuf;
417 char *data;
418 time_t cache_mtime;
419 GHashTable *cache;
421 if (!g_module_supported ())
422 return;
424 dir = g_dir_open (dirname, 0, NULL);
425 if (!dir)
426 return;
428 filename = g_build_filename (dirname, "giomodule.cache", NULL);
430 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
431 g_free, (GDestroyNotify)g_strfreev);
433 cache_mtime = 0;
434 if (g_stat (filename, &statbuf) == 0 &&
435 g_file_get_contents (filename, &data, NULL, NULL))
437 char **lines;
438 int i;
440 /* Cache mtime is the time the cache file was created, any file
441 * that has a ctime before this was created then and not modified
442 * since then (userspace can't change ctime). Its possible to change
443 * the ctime forward without changing the file content, by e.g.
444 * chmoding the file, but this is uncommon and will only cause us
445 * to not use the cache so will not cause bugs.
447 cache_mtime = statbuf.st_mtime;
449 lines = g_strsplit (data, "\n", -1);
450 g_free (data);
452 for (i = 0; lines[i] != NULL; i++)
454 char *line = lines[i];
455 char *file;
456 char *colon;
457 char **extension_points;
459 if (line[0] == '#')
460 continue;
462 colon = strchr (line, ':');
463 if (colon == NULL || line == colon)
464 continue; /* Invalid line, ignore */
466 *colon = 0; /* terminate filename */
467 file = g_strdup (line);
468 colon++; /* after colon */
470 while (g_ascii_isspace (*colon))
471 colon++;
473 extension_points = g_strsplit (colon, ",", -1);
474 g_hash_table_insert (cache, file, extension_points);
476 g_strfreev (lines);
479 while ((name = g_dir_read_name (dir)))
481 if (is_valid_module_name (name, scope))
483 GIOExtensionPoint *extension_point;
484 GIOModule *module;
485 gchar *path;
486 char **extension_points;
487 int i;
489 path = g_build_filename (dirname, name, NULL);
490 module = g_io_module_new (path);
492 extension_points = g_hash_table_lookup (cache, name);
493 if (extension_points != NULL &&
494 g_stat (path, &statbuf) == 0 &&
495 statbuf.st_ctime <= cache_mtime)
497 /* Lazy load/init the library when first required */
498 for (i = 0; extension_points[i] != NULL; i++)
500 extension_point =
501 g_io_extension_point_register (extension_points[i]);
502 extension_point->lazy_load_modules =
503 g_list_prepend (extension_point->lazy_load_modules,
504 module);
507 else
509 /* Try to load and init types */
510 if (g_type_module_use (G_TYPE_MODULE (module)))
511 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
512 else
513 { /* Failure to load */
514 g_printerr ("Failed to load module: %s\n", path);
515 g_object_unref (module);
516 g_free (path);
517 continue;
521 g_free (path);
525 g_dir_close (dir);
527 g_hash_table_destroy (cache);
529 g_free (filename);
533 * g_io_modules_scan_all_in_directory:
534 * @dirname: pathname for a directory containing modules to scan.
536 * Scans all the modules in the specified directory, ensuring that
537 * any extension point implemented by a module is registered.
539 * This may not actually load and initialize all the types in each
540 * module, some modules may be lazily loaded and initialized when
541 * an extension point it implementes is used with e.g.
542 * g_io_extension_point_get_extensions() or
543 * g_io_extension_point_get_extension_by_name().
545 * If you need to guarantee that all types are loaded in all the modules,
546 * use g_io_modules_load_all_in_directory().
548 * Since: 2.24
550 void
551 g_io_modules_scan_all_in_directory (const char *dirname)
553 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
557 * g_io_modules_load_all_in_directory_with_scope:
558 * @dirname: pathname for a directory containing modules to load.
559 * @scope: a scope to use when scanning the modules.
561 * Loads all the modules in the specified directory.
563 * If don't require all modules to be initialized (and thus registering
564 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
565 * which allows delayed/lazy loading of modules.
567 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
568 * from the directory,
569 * All the modules are loaded into memory, if you want to
570 * unload them (enabling on-demand loading) you must call
571 * g_type_module_unuse() on all the modules. Free the list
572 * with g_list_free().
574 * Since: 2.30
576 GList *
577 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
578 GIOModuleScope *scope)
580 const gchar *name;
581 GDir *dir;
582 GList *modules;
584 if (!g_module_supported ())
585 return NULL;
587 dir = g_dir_open (dirname, 0, NULL);
588 if (!dir)
589 return NULL;
591 modules = NULL;
592 while ((name = g_dir_read_name (dir)))
594 if (is_valid_module_name (name, scope))
596 GIOModule *module;
597 gchar *path;
599 path = g_build_filename (dirname, name, NULL);
600 module = g_io_module_new (path);
602 if (!g_type_module_use (G_TYPE_MODULE (module)))
604 g_printerr ("Failed to load module: %s\n", path);
605 g_object_unref (module);
606 g_free (path);
607 continue;
610 g_free (path);
612 modules = g_list_prepend (modules, module);
616 g_dir_close (dir);
618 return modules;
622 * g_io_modules_load_all_in_directory:
623 * @dirname: pathname for a directory containing modules to load.
625 * Loads all the modules in the specified directory.
627 * If don't require all modules to be initialized (and thus registering
628 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
629 * which allows delayed/lazy loading of modules.
631 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
632 * from the directory,
633 * All the modules are loaded into memory, if you want to
634 * unload them (enabling on-demand loading) you must call
635 * g_type_module_unuse() on all the modules. Free the list
636 * with g_list_free().
638 GList *
639 g_io_modules_load_all_in_directory (const char *dirname)
641 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
644 static gpointer
645 try_class (GIOExtension *extension,
646 guint is_supported_offset)
648 GType type = g_io_extension_get_type (extension);
649 typedef gboolean (*verify_func) (void);
650 gpointer class;
652 class = g_type_class_ref (type);
653 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
654 return class;
656 g_type_class_unref (class);
657 return NULL;
661 * _g_io_module_get_default_type:
662 * @extension_point: the name of an extension point
663 * @envvar: (allow-none): the name of an environment variable to
664 * override the default implementation.
665 * @is_supported_offset: a vtable offset, or zero
667 * Retrieves the default class implementing @extension_point.
669 * If @envvar is not %NULL, and the environment variable with that
670 * name is set, then the implementation it specifies will be tried
671 * first. After that, or if @envvar is not set, all other
672 * implementations will be tried in order of decreasing priority.
674 * If @is_supported_offset is non-zero, then it is the offset into the
675 * class vtable at which there is a function that takes no arguments and
676 * returns a boolean. This function will be called on each candidate
677 * implementation to check if it is actually usable or not.
679 * The result is cached after it is generated the first time, and
680 * the function is thread-safe.
682 * Returns: (transfer none): an object implementing
683 * @extension_point, or %NULL if there are no usable
684 * implementations.
686 GType
687 _g_io_module_get_default_type (const gchar *extension_point,
688 const gchar *envvar,
689 guint is_supported_offset)
691 static GRecMutex default_modules_lock;
692 static GHashTable *default_modules;
693 const char *use_this;
694 GList *l;
695 GIOExtensionPoint *ep;
696 GIOExtension *extension, *preferred;
697 gpointer impl;
699 g_rec_mutex_lock (&default_modules_lock);
700 if (default_modules)
702 gpointer key;
704 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
706 g_rec_mutex_unlock (&default_modules_lock);
707 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
710 else
712 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
715 _g_io_modules_ensure_loaded ();
716 ep = g_io_extension_point_lookup (extension_point);
718 if (!ep)
720 g_warn_if_reached ();
721 g_rec_mutex_unlock (&default_modules_lock);
722 return G_TYPE_INVALID;
725 use_this = envvar ? g_getenv (envvar) : NULL;
726 if (use_this)
728 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
729 if (preferred)
731 impl = try_class (preferred, is_supported_offset);
732 if (impl)
733 goto done;
735 else
736 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
738 else
739 preferred = NULL;
741 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
743 extension = l->data;
744 if (extension == preferred)
745 continue;
747 impl = try_class (extension, is_supported_offset);
748 if (impl)
749 goto done;
752 impl = NULL;
754 done:
755 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
756 g_rec_mutex_unlock (&default_modules_lock);
758 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
761 static gpointer
762 try_implementation (GIOExtension *extension,
763 GIOModuleVerifyFunc verify_func)
765 GType type = g_io_extension_get_type (extension);
766 gpointer impl;
768 if (g_type_is_a (type, G_TYPE_INITABLE))
769 return g_initable_new (type, NULL, NULL, NULL);
770 else
772 impl = g_object_new (type, NULL);
773 if (!verify_func || verify_func (impl))
774 return impl;
776 g_object_unref (impl);
777 return NULL;
782 * _g_io_module_get_default:
783 * @extension_point: the name of an extension point
784 * @envvar: (allow-none): the name of an environment variable to
785 * override the default implementation.
786 * @verify_func: (allow-none): a function to call to verify that
787 * a given implementation is usable in the current environment.
789 * Retrieves the default object implementing @extension_point.
791 * If @envvar is not %NULL, and the environment variable with that
792 * name is set, then the implementation it specifies will be tried
793 * first. After that, or if @envvar is not set, all other
794 * implementations will be tried in order of decreasing priority.
796 * If an extension point implementation implements #GInitable, then
797 * that implementation will only be used if it initializes
798 * successfully. Otherwise, if @verify_func is not %NULL, then it will
799 * be called on each candidate implementation after construction, to
800 * check if it is actually usable or not.
802 * The result is cached after it is generated the first time, and
803 * the function is thread-safe.
805 * Returns: (transfer none): an object implementing
806 * @extension_point, or %NULL if there are no usable
807 * implementations.
809 gpointer
810 _g_io_module_get_default (const gchar *extension_point,
811 const gchar *envvar,
812 GIOModuleVerifyFunc verify_func)
814 static GRecMutex default_modules_lock;
815 static GHashTable *default_modules;
816 const char *use_this;
817 GList *l;
818 GIOExtensionPoint *ep;
819 GIOExtension *extension, *preferred;
820 gpointer impl;
822 g_rec_mutex_lock (&default_modules_lock);
823 if (default_modules)
825 gpointer key;
827 if (g_hash_table_lookup_extended (default_modules, extension_point,
828 &key, &impl))
830 g_rec_mutex_unlock (&default_modules_lock);
831 return impl;
834 else
836 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
839 _g_io_modules_ensure_loaded ();
840 ep = g_io_extension_point_lookup (extension_point);
842 if (!ep)
844 g_warn_if_reached ();
845 g_rec_mutex_unlock (&default_modules_lock);
846 return NULL;
849 use_this = envvar ? g_getenv (envvar) : NULL;
850 if (use_this)
852 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
853 if (preferred)
855 impl = try_implementation (preferred, verify_func);
856 if (impl)
857 goto done;
859 else
860 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
862 else
863 preferred = NULL;
865 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
867 extension = l->data;
868 if (extension == preferred)
869 continue;
871 impl = try_implementation (extension, verify_func);
872 if (impl)
873 goto done;
876 impl = NULL;
878 done:
879 g_hash_table_insert (default_modules,
880 g_strdup (extension_point),
881 impl ? g_object_ref (impl) : NULL);
882 g_rec_mutex_unlock (&default_modules_lock);
884 return impl;
887 G_LOCK_DEFINE_STATIC (registered_extensions);
888 G_LOCK_DEFINE_STATIC (loaded_dirs);
890 extern GType g_fen_file_monitor_get_type (void);
891 extern GType g_inotify_file_monitor_get_type (void);
892 extern GType g_kqueue_file_monitor_get_type (void);
893 extern GType g_win32_file_monitor_get_type (void);
895 extern GType _g_unix_volume_monitor_get_type (void);
896 extern GType _g_local_vfs_get_type (void);
898 extern GType _g_win32_volume_monitor_get_type (void);
899 extern GType _g_winhttp_vfs_get_type (void);
901 extern GType _g_dummy_proxy_resolver_get_type (void);
902 extern GType _g_dummy_tls_backend_get_type (void);
903 extern GType g_network_monitor_base_get_type (void);
904 #ifdef HAVE_NETLINK
905 extern GType _g_network_monitor_netlink_get_type (void);
906 extern GType _g_network_monitor_nm_get_type (void);
907 #endif
909 #ifdef G_OS_UNIX
910 extern GType g_fdo_notification_backend_get_type (void);
911 extern GType g_gtk_notification_backend_get_type (void);
912 #endif
914 #ifdef HAVE_COCOA
915 extern GType g_cocoa_notification_backend_get_type (void);
916 #endif
918 #ifdef G_PLATFORM_WIN32
920 #include <windows.h>
922 static HMODULE gio_dll = NULL;
924 #ifdef DLL_EXPORT
926 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
927 DWORD fdwReason,
928 LPVOID lpvReserved);
930 BOOL WINAPI
931 DllMain (HINSTANCE hinstDLL,
932 DWORD fdwReason,
933 LPVOID lpvReserved)
935 if (fdwReason == DLL_PROCESS_ATTACH)
936 gio_dll = hinstDLL;
938 return TRUE;
941 #endif
943 void *
944 _g_io_win32_get_module (void)
946 if (!gio_dll)
947 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
948 (const char *) _g_io_win32_get_module,
949 &gio_dll);
950 return gio_dll;
953 #endif
955 void
956 _g_io_modules_ensure_extension_points_registered (void)
958 static gboolean registered_extensions = FALSE;
959 GIOExtensionPoint *ep;
961 G_LOCK (registered_extensions);
963 if (!registered_extensions)
965 registered_extensions = TRUE;
967 #ifdef G_OS_UNIX
968 #if !GLIB_CHECK_VERSION (3, 0, 0)
969 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
970 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
971 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
972 G_GNUC_END_IGNORE_DEPRECATIONS
973 #endif
974 #endif
976 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
977 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
979 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
980 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
982 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
983 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
985 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
986 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
988 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
989 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
991 ep = g_io_extension_point_register ("gsettings-backend");
992 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
994 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
995 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
997 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
998 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1000 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1001 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1003 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1004 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1006 ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1007 g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1010 G_UNLOCK (registered_extensions);
1013 static gchar *
1014 get_gio_module_dir (void)
1016 gchar *module_dir;
1018 module_dir = g_strdup (g_getenv ("GIO_MODULE_DIR"));
1019 if (module_dir == NULL)
1021 #ifdef G_OS_WIN32
1022 gchar *install_dir;
1024 install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1025 #ifdef _MSC_VER
1026 /* On Visual Studio builds we have all the libraries and binaries in bin
1027 * so better load the gio modules from bin instead of lib
1029 module_dir = g_build_filename (install_dir,
1030 "bin", "gio", "modules",
1031 NULL);
1032 #else
1033 module_dir = g_build_filename (install_dir,
1034 "lib", "gio", "modules",
1035 NULL);
1036 #endif
1037 g_free (install_dir);
1038 #else
1039 module_dir = g_strdup (GIO_MODULE_DIR);
1040 #endif
1043 return module_dir;
1046 void
1047 _g_io_modules_ensure_loaded (void)
1049 static gboolean loaded_dirs = FALSE;
1050 const char *module_path;
1051 GIOModuleScope *scope;
1053 _g_io_modules_ensure_extension_points_registered ();
1055 G_LOCK (loaded_dirs);
1057 if (!loaded_dirs)
1059 gchar *module_dir;
1061 loaded_dirs = TRUE;
1062 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1064 /* First load any overrides, extras */
1065 module_path = g_getenv ("GIO_EXTRA_MODULES");
1066 if (module_path)
1068 gchar **paths;
1069 int i;
1071 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1073 for (i = 0; paths[i] != NULL; i++)
1075 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1078 g_strfreev (paths);
1081 /* Then load the compiled in path */
1082 module_dir = get_gio_module_dir ();
1084 g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1085 g_free (module_dir);
1087 g_io_module_scope_free (scope);
1089 /* Initialize types from built-in "modules" */
1090 g_type_ensure (g_null_settings_backend_get_type ());
1091 g_type_ensure (g_memory_settings_backend_get_type ());
1092 #if defined(HAVE_INOTIFY_INIT1)
1093 g_type_ensure (g_inotify_file_monitor_get_type ());
1094 #endif
1095 #if defined(HAVE_KQUEUE)
1096 g_type_ensure (g_kqueue_file_monitor_get_type ());
1097 #endif
1098 #if defined(HAVE_FEN)
1099 g_type_ensure (g_fen_file_monitor_get_type ());
1100 #endif
1101 #ifdef G_OS_WIN32
1102 g_type_ensure (_g_win32_volume_monitor_get_type ());
1103 g_type_ensure (g_win32_file_monitor_get_type ());
1104 g_type_ensure (g_registry_backend_get_type ());
1105 #endif
1106 #ifdef HAVE_COCOA
1107 g_nextstep_settings_backend_get_type ();
1108 #endif
1109 #ifdef G_OS_UNIX
1110 g_type_ensure (_g_unix_volume_monitor_get_type ());
1111 g_type_ensure (g_fdo_notification_backend_get_type ());
1112 g_type_ensure (g_gtk_notification_backend_get_type ());
1113 #endif
1114 #ifdef HAVE_COCOA
1115 g_type_ensure (g_cocoa_notification_backend_get_type ());
1116 #endif
1117 #ifdef G_OS_WIN32
1118 g_type_ensure (_g_winhttp_vfs_get_type ());
1119 #endif
1120 g_type_ensure (_g_local_vfs_get_type ());
1121 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1122 g_type_ensure (_g_http_proxy_get_type ());
1123 g_type_ensure (_g_https_proxy_get_type ());
1124 g_type_ensure (_g_socks4a_proxy_get_type ());
1125 g_type_ensure (_g_socks4_proxy_get_type ());
1126 g_type_ensure (_g_socks5_proxy_get_type ());
1127 g_type_ensure (_g_dummy_tls_backend_get_type ());
1128 g_type_ensure (g_network_monitor_base_get_type ());
1129 #ifdef HAVE_NETLINK
1130 g_type_ensure (_g_network_monitor_netlink_get_type ());
1131 g_type_ensure (_g_network_monitor_nm_get_type ());
1132 #endif
1135 G_UNLOCK (loaded_dirs);
1138 static void
1139 g_io_extension_point_free (GIOExtensionPoint *ep)
1141 g_free (ep->name);
1142 g_free (ep);
1146 * g_io_extension_point_register:
1147 * @name: The name of the extension point
1149 * Registers an extension point.
1151 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1152 * owned by GIO and should not be freed.
1154 GIOExtensionPoint *
1155 g_io_extension_point_register (const char *name)
1157 GIOExtensionPoint *ep;
1159 G_LOCK (extension_points);
1160 if (extension_points == NULL)
1161 extension_points = g_hash_table_new_full (g_str_hash,
1162 g_str_equal,
1163 NULL,
1164 (GDestroyNotify)g_io_extension_point_free);
1166 ep = g_hash_table_lookup (extension_points, name);
1167 if (ep != NULL)
1169 G_UNLOCK (extension_points);
1170 return ep;
1173 ep = g_new0 (GIOExtensionPoint, 1);
1174 ep->name = g_strdup (name);
1176 g_hash_table_insert (extension_points, ep->name, ep);
1178 G_UNLOCK (extension_points);
1180 return ep;
1184 * g_io_extension_point_lookup:
1185 * @name: the name of the extension point
1187 * Looks up an existing extension point.
1189 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1190 * is no registered extension point with the given name.
1192 GIOExtensionPoint *
1193 g_io_extension_point_lookup (const char *name)
1195 GIOExtensionPoint *ep;
1197 G_LOCK (extension_points);
1198 ep = NULL;
1199 if (extension_points != NULL)
1200 ep = g_hash_table_lookup (extension_points, name);
1202 G_UNLOCK (extension_points);
1204 return ep;
1209 * g_io_extension_point_set_required_type:
1210 * @extension_point: a #GIOExtensionPoint
1211 * @type: the #GType to require
1213 * Sets the required type for @extension_point to @type.
1214 * All implementations must henceforth have this type.
1216 void
1217 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1218 GType type)
1220 extension_point->required_type = type;
1224 * g_io_extension_point_get_required_type:
1225 * @extension_point: a #GIOExtensionPoint
1227 * Gets the required type for @extension_point.
1229 * Returns: the #GType that all implementations must have,
1230 * or #G_TYPE_INVALID if the extension point has no required type
1232 GType
1233 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1235 return extension_point->required_type;
1238 static void
1239 lazy_load_modules (GIOExtensionPoint *extension_point)
1241 GIOModule *module;
1242 GList *l;
1244 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1246 module = l->data;
1248 if (!module->initialized)
1250 if (g_type_module_use (G_TYPE_MODULE (module)))
1251 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1252 else
1253 g_printerr ("Failed to load module: %s\n",
1254 module->filename);
1260 * g_io_extension_point_get_extensions:
1261 * @extension_point: a #GIOExtensionPoint
1263 * Gets a list of all extensions that implement this extension point.
1264 * The list is sorted by priority, beginning with the highest priority.
1266 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1267 * #GIOExtensions. The list is owned by GIO and should not be
1268 * modified.
1270 GList *
1271 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1273 lazy_load_modules (extension_point);
1274 return extension_point->extensions;
1278 * g_io_extension_point_get_extension_by_name:
1279 * @extension_point: a #GIOExtensionPoint
1280 * @name: the name of the extension to get
1282 * Finds a #GIOExtension for an extension point by name.
1284 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1285 * given name, or %NULL if there is no extension with that name
1287 GIOExtension *
1288 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1289 const char *name)
1291 GList *l;
1293 g_return_val_if_fail (name != NULL, NULL);
1295 lazy_load_modules (extension_point);
1296 for (l = extension_point->extensions; l != NULL; l = l->next)
1298 GIOExtension *e = l->data;
1300 if (e->name != NULL &&
1301 strcmp (e->name, name) == 0)
1302 return e;
1305 return NULL;
1308 static gint
1309 extension_prio_compare (gconstpointer a,
1310 gconstpointer b)
1312 const GIOExtension *extension_a = a, *extension_b = b;
1314 if (extension_a->priority > extension_b->priority)
1315 return -1;
1317 if (extension_b->priority > extension_a->priority)
1318 return 1;
1320 return 0;
1324 * g_io_extension_point_implement:
1325 * @extension_point_name: the name of the extension point
1326 * @type: the #GType to register as extension
1327 * @extension_name: the name for the extension
1328 * @priority: the priority for the extension
1330 * Registers @type as extension for the extension point with name
1331 * @extension_point_name.
1333 * If @type has already been registered as an extension for this
1334 * extension point, the existing #GIOExtension object is returned.
1336 * Returns: (transfer none): a #GIOExtension object for #GType
1338 GIOExtension *
1339 g_io_extension_point_implement (const char *extension_point_name,
1340 GType type,
1341 const char *extension_name,
1342 gint priority)
1344 GIOExtensionPoint *extension_point;
1345 GIOExtension *extension;
1346 GList *l;
1348 g_return_val_if_fail (extension_point_name != NULL, NULL);
1350 extension_point = g_io_extension_point_lookup (extension_point_name);
1351 if (extension_point == NULL)
1353 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1354 return NULL;
1357 if (extension_point->required_type != 0 &&
1358 !g_type_is_a (type, extension_point->required_type))
1360 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1361 "Expected type is %s.",
1362 g_type_name (type),
1363 extension_point_name,
1364 g_type_name (extension_point->required_type));
1365 return NULL;
1368 /* It's safe to register the same type multiple times */
1369 for (l = extension_point->extensions; l != NULL; l = l->next)
1371 extension = l->data;
1372 if (extension->type == type)
1373 return extension;
1376 extension = g_slice_new0 (GIOExtension);
1377 extension->type = type;
1378 extension->name = g_strdup (extension_name);
1379 extension->priority = priority;
1381 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1382 extension, extension_prio_compare);
1384 return extension;
1388 * g_io_extension_ref_class:
1389 * @extension: a #GIOExtension
1391 * Gets a reference to the class for the type that is
1392 * associated with @extension.
1394 * Returns: (transfer full): the #GTypeClass for the type of @extension
1396 GTypeClass *
1397 g_io_extension_ref_class (GIOExtension *extension)
1399 return g_type_class_ref (extension->type);
1403 * g_io_extension_get_type:
1404 * @extension: a #GIOExtension
1406 * Gets the type associated with @extension.
1408 * Returns: the type of @extension
1410 GType
1411 g_io_extension_get_type (GIOExtension *extension)
1413 return extension->type;
1417 * g_io_extension_get_name:
1418 * @extension: a #GIOExtension
1420 * Gets the name under which @extension was registered.
1422 * Note that the same type may be registered as extension
1423 * for multiple extension points, under different names.
1425 * Returns: the name of @extension.
1427 const char *
1428 g_io_extension_get_name (GIOExtension *extension)
1430 return extension->name;
1434 * g_io_extension_get_priority:
1435 * @extension: a #GIOExtension
1437 * Gets the priority with which @extension was registered.
1439 * Returns: the priority of @extension
1441 gint
1442 g_io_extension_get_priority (GIOExtension *extension)
1444 return extension->priority;