Add g_key_file_save_to_file()
[glib.git] / gio / giomodule.c
bloba394c600c205c820f5b49911f496077a5d52a6b5
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
25 #include <string.h>
27 #include "giomodule.h"
28 #include "giomodule-priv.h"
29 #include "glocalfilemonitor.h"
30 #include "glocaldirectorymonitor.h"
31 #include "gnativevolumemonitor.h"
32 #include "gproxyresolver.h"
33 #include "gproxy.h"
34 #include "gsettingsbackendinternal.h"
35 #include "gsocks4proxy.h"
36 #include "gsocks4aproxy.h"
37 #include "gsocks5proxy.h"
38 #include "gtlsbackend.h"
39 #include "gvfs.h"
40 #ifdef G_OS_WIN32
41 #include "gregistrysettingsbackend.h"
42 #endif
43 #include <glib/gstdio.h>
45 #ifdef G_OS_UNIX
46 #include "gdesktopappinfo.h"
47 #endif
49 /**
50 * SECTION:giomodule
51 * @short_description: Loadable GIO Modules
52 * @include: gio/gio.h
54 * Provides an interface and default functions for loading and unloading
55 * modules. This is used internally to make GIO extensible, but can also
56 * be used by others to implement module loading.
58 **/
60 /**
61 * SECTION:extensionpoints
62 * @short_description: Extension Points
63 * @include: gio.h
64 * @see_also: <link linkend="extending-gio">Extending GIO</link>
66 * #GIOExtensionPoint provides a mechanism for modules to extend the
67 * functionality of the library or application that loaded it in an
68 * organized fashion.
70 * An extension point is identified by a name, and it may optionally
71 * require that any implementation must by of a certain type (or derived
72 * thereof). Use g_io_extension_point_register() to register an
73 * extension point, and g_io_extension_point_set_required_type() to
74 * set a required type.
76 * A module can implement an extension point by specifying the #GType
77 * that implements the functionality. Additionally, each implementation
78 * of an extension point has a name, and a priority. Use
79 * g_io_extension_point_implement() to implement an extension point.
81 * |[
82 * GIOExtensionPoint *ep;
84 * /&ast; Register an extension point &ast;/
85 * ep = g_io_extension_point_register ("my-extension-point");
86 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
87 * ]|
89 * |[
90 * /&ast; Implement an extension point &ast;/
91 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
92 * g_io_extension_point_implement ("my-extension-point",
93 * my_example_impl_get_type (),
94 * "my-example",
95 * 10);
96 * ]|
98 * It is up to the code that registered the extension point how
99 * it uses the implementations that have been associated with it.
100 * Depending on the use case, it may use all implementations, or
101 * only the one with the highest priority, or pick a specific
102 * one by name.
104 * To avoid opening all modules just to find out what extension
105 * points they implement, GIO makes use of a caching mechanism,
106 * see <link linkend="gio-querymodules">gio-querymodules</link>.
107 * You are expected to run this command after installing a
108 * GIO module.
110 * The <envar>GIO_EXTRA_MODULES</envar> environment variable can be
111 * used to specify additional directories to automatically load modules
112 * from. This environment variable has the same syntax as the
113 * <envar>PATH</envar>. If two modules have the same base name in different
114 * directories, then the latter one will be ignored. If additional
115 * directories are specified GIO will load modules from the built-in
116 * directory last.
120 * GIOModuleScope:
122 * Represents a scope for loading IO modules. A scope can be used for blocking
123 * duplicate modules, or blocking a module you don't want to load.
125 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
126 * or g_io_modules_scan_all_in_directory_with_scope().
128 * Since: 2.30
130 struct _GIOModuleScope {
131 GIOModuleScopeFlags flags;
132 GHashTable *basenames;
136 * g_io_module_scope_new:
137 * @flags: flags for the new scope
139 * Create a new scope for loading of IO modules. A scope can be used for
140 * blocking duplicate modules, or blocking a module you don't want to load.
142 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
143 * which have the same base name as a module that has already been seen
144 * in this scope.
146 * Returns: (transfer full): the new module scope
148 * Since: 2.30
150 GIOModuleScope *
151 g_io_module_scope_new (GIOModuleScopeFlags flags)
153 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
154 scope->flags = flags;
155 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
156 return scope;
160 * g_io_module_scope_free:
161 * @scope: a module loading scope
163 * Free a module scope.
165 * Since: 2.30
167 void
168 g_io_module_scope_free (GIOModuleScope *scope)
170 if (!scope)
171 return;
172 g_hash_table_destroy (scope->basenames);
173 g_free (scope);
177 * g_io_module_scope_block:
178 * @scope: a module loading scope
179 * @basename: the basename to block
181 * Block modules with the given @basename from being loaded when
182 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
183 * or g_io_modules_load_all_in_directory_with_scope().
185 * Since: 2.30
187 void
188 g_io_module_scope_block (GIOModuleScope *scope,
189 const gchar *basename)
191 gchar *key;
193 g_return_if_fail (scope != NULL);
194 g_return_if_fail (basename != NULL);
196 key = g_strdup (basename);
197 g_hash_table_insert (scope->basenames, key, key);
200 static gboolean
201 _g_io_module_scope_contains (GIOModuleScope *scope,
202 const gchar *basename)
204 return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE;
207 struct _GIOModule {
208 GTypeModule parent_instance;
210 gchar *filename;
211 GModule *library;
212 gboolean initialized; /* The module was loaded at least once */
214 void (* load) (GIOModule *module);
215 void (* unload) (GIOModule *module);
218 struct _GIOModuleClass
220 GTypeModuleClass parent_class;
224 static void g_io_module_finalize (GObject *object);
225 static gboolean g_io_module_load_module (GTypeModule *gmodule);
226 static void g_io_module_unload_module (GTypeModule *gmodule);
228 struct _GIOExtension {
229 char *name;
230 GType type;
231 gint priority;
234 struct _GIOExtensionPoint {
235 GType required_type;
236 char *name;
237 GList *extensions;
238 GList *lazy_load_modules;
241 static GHashTable *extension_points = NULL;
242 G_LOCK_DEFINE_STATIC(extension_points);
244 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
246 static void
247 g_io_module_class_init (GIOModuleClass *class)
249 GObjectClass *object_class = G_OBJECT_CLASS (class);
250 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
252 object_class->finalize = g_io_module_finalize;
254 type_module_class->load = g_io_module_load_module;
255 type_module_class->unload = g_io_module_unload_module;
258 static void
259 g_io_module_init (GIOModule *module)
263 static void
264 g_io_module_finalize (GObject *object)
266 GIOModule *module = G_IO_MODULE (object);
268 g_free (module->filename);
270 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
273 static gboolean
274 g_io_module_load_module (GTypeModule *gmodule)
276 GIOModule *module = G_IO_MODULE (gmodule);
278 if (!module->filename)
280 g_warning ("GIOModule path not set");
281 return FALSE;
284 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
286 if (!module->library)
288 g_printerr ("%s\n", g_module_error ());
289 return FALSE;
292 /* Make sure that the loaded library contains the required methods */
293 if (! g_module_symbol (module->library,
294 "g_io_module_load",
295 (gpointer) &module->load) ||
296 ! g_module_symbol (module->library,
297 "g_io_module_unload",
298 (gpointer) &module->unload))
300 g_printerr ("%s\n", g_module_error ());
301 g_module_close (module->library);
303 return FALSE;
306 /* Initialize the loaded module */
307 module->load (module);
308 module->initialized = TRUE;
310 return TRUE;
313 static void
314 g_io_module_unload_module (GTypeModule *gmodule)
316 GIOModule *module = G_IO_MODULE (gmodule);
318 module->unload (module);
320 g_module_close (module->library);
321 module->library = NULL;
323 module->load = NULL;
324 module->unload = NULL;
328 * g_io_module_new:
329 * @filename: filename of the shared library module.
331 * Creates a new GIOModule that will load the specific
332 * shared library when in use.
334 * Returns: a #GIOModule from given @filename,
335 * or %NULL on error.
337 GIOModule *
338 g_io_module_new (const gchar *filename)
340 GIOModule *module;
342 g_return_val_if_fail (filename != NULL, NULL);
344 module = g_object_new (G_IO_TYPE_MODULE, NULL);
345 module->filename = g_strdup (filename);
347 return module;
350 static gboolean
351 is_valid_module_name (const gchar *basename,
352 GIOModuleScope *scope)
354 gboolean result;
356 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
357 if (!g_str_has_prefix (basename, "lib") ||
358 !g_str_has_suffix (basename, ".so"))
359 return FALSE;
360 #else
361 if (!g_str_has_suffix (basename, ".dll"))
362 return FALSE;
363 #endif
365 result = TRUE;
366 if (scope)
368 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
369 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
370 g_io_module_scope_block (scope, basename);
373 return result;
378 * g_io_modules_scan_all_in_directory_with_scope:
379 * @dirname: pathname for a directory containing modules to scan.
380 * @scope: a scope to use when scanning the modules
382 * Scans all the modules in the specified directory, ensuring that
383 * any extension point implemented by a module is registered.
385 * This may not actually load and initialize all the types in each
386 * module, some modules may be lazily loaded and initialized when
387 * an extension point it implementes is used with e.g.
388 * g_io_extension_point_get_extensions() or
389 * g_io_extension_point_get_extension_by_name().
391 * If you need to guarantee that all types are loaded in all the modules,
392 * use g_io_modules_load_all_in_directory().
394 * Since: 2.30
396 void
397 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
398 GIOModuleScope *scope)
400 const gchar *name;
401 char *filename;
402 GDir *dir;
403 GStatBuf statbuf;
404 char *data;
405 time_t cache_mtime;
406 GHashTable *cache;
408 if (!g_module_supported ())
409 return;
411 dir = g_dir_open (dirname, 0, NULL);
412 if (!dir)
413 return;
415 filename = g_build_filename (dirname, "giomodule.cache", NULL);
417 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
418 g_free, (GDestroyNotify)g_strfreev);
420 cache_mtime = 0;
421 if (g_stat (filename, &statbuf) == 0 &&
422 g_file_get_contents (filename, &data, NULL, NULL))
424 char **lines;
425 int i;
427 /* Cache mtime is the time the cache file was created, any file
428 * that has a ctime before this was created then and not modified
429 * since then (userspace can't change ctime). Its possible to change
430 * the ctime forward without changing the file content, by e.g.
431 * chmoding the file, but this is uncommon and will only cause us
432 * to not use the cache so will not cause bugs.
434 cache_mtime = statbuf.st_mtime;
436 lines = g_strsplit (data, "\n", -1);
437 g_free (data);
439 for (i = 0; lines[i] != NULL; i++)
441 char *line = lines[i];
442 char *file;
443 char *colon;
444 char **extension_points;
446 if (line[0] == '#')
447 continue;
449 colon = strchr (line, ':');
450 if (colon == NULL || line == colon)
451 continue; /* Invalid line, ignore */
453 *colon = 0; /* terminate filename */
454 file = g_strdup (line);
455 colon++; /* after colon */
457 while (g_ascii_isspace (*colon))
458 colon++;
460 extension_points = g_strsplit (colon, ",", -1);
461 g_hash_table_insert (cache, file, extension_points);
463 g_strfreev (lines);
466 while ((name = g_dir_read_name (dir)))
468 if (is_valid_module_name (name, scope))
470 GIOExtensionPoint *extension_point;
471 GIOModule *module;
472 gchar *path;
473 char **extension_points;
474 int i;
476 path = g_build_filename (dirname, name, NULL);
477 module = g_io_module_new (path);
479 extension_points = g_hash_table_lookup (cache, name);
480 if (extension_points != NULL &&
481 g_stat (path, &statbuf) == 0 &&
482 statbuf.st_ctime <= cache_mtime)
484 /* Lazy load/init the library when first required */
485 for (i = 0; extension_points[i] != NULL; i++)
487 extension_point =
488 g_io_extension_point_register (extension_points[i]);
489 extension_point->lazy_load_modules =
490 g_list_prepend (extension_point->lazy_load_modules,
491 module);
494 else
496 /* Try to load and init types */
497 if (g_type_module_use (G_TYPE_MODULE (module)))
498 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
499 else
500 { /* Failure to load */
501 g_printerr ("Failed to load module: %s\n", path);
502 g_object_unref (module);
503 g_free (path);
504 continue;
508 g_free (path);
512 g_dir_close (dir);
514 g_hash_table_destroy (cache);
516 g_free (filename);
520 * g_io_modules_scan_all_in_directory:
521 * @dirname: pathname for a directory containing modules to scan.
523 * Scans all the modules in the specified directory, ensuring that
524 * any extension point implemented by a module is registered.
526 * This may not actually load and initialize all the types in each
527 * module, some modules may be lazily loaded and initialized when
528 * an extension point it implementes is used with e.g.
529 * g_io_extension_point_get_extensions() or
530 * g_io_extension_point_get_extension_by_name().
532 * If you need to guarantee that all types are loaded in all the modules,
533 * use g_io_modules_load_all_in_directory().
535 * Since: 2.24
537 void
538 g_io_modules_scan_all_in_directory (const char *dirname)
540 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
544 * g_io_modules_load_all_in_directory_with_scope:
545 * @dirname: pathname for a directory containing modules to load.
546 * @scope: a scope to use when scanning the modules.
548 * Loads all the modules in the specified directory.
550 * If don't require all modules to be initialized (and thus registering
551 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
552 * which allows delayed/lazy loading of modules.
554 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
555 * from the directory,
556 * All the modules are loaded into memory, if you want to
557 * unload them (enabling on-demand loading) you must call
558 * g_type_module_unuse() on all the modules. Free the list
559 * with g_list_free().
561 * Since: 2.30
563 GList *
564 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
565 GIOModuleScope *scope)
567 const gchar *name;
568 GDir *dir;
569 GList *modules;
571 if (!g_module_supported ())
572 return NULL;
574 dir = g_dir_open (dirname, 0, NULL);
575 if (!dir)
576 return NULL;
578 modules = NULL;
579 while ((name = g_dir_read_name (dir)))
581 if (is_valid_module_name (name, scope))
583 GIOModule *module;
584 gchar *path;
586 path = g_build_filename (dirname, name, NULL);
587 module = g_io_module_new (path);
589 if (!g_type_module_use (G_TYPE_MODULE (module)))
591 g_printerr ("Failed to load module: %s\n", path);
592 g_object_unref (module);
593 g_free (path);
594 continue;
597 g_free (path);
599 modules = g_list_prepend (modules, module);
603 g_dir_close (dir);
605 return modules;
609 * g_io_modules_load_all_in_directory:
610 * @dirname: pathname for a directory containing modules to load.
612 * Loads all the modules in the specified directory.
614 * If don't require all modules to be initialized (and thus registering
615 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
616 * which allows delayed/lazy loading of modules.
618 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
619 * from the directory,
620 * All the modules are loaded into memory, if you want to
621 * unload them (enabling on-demand loading) you must call
622 * g_type_module_unuse() on all the modules. Free the list
623 * with g_list_free().
625 GList *
626 g_io_modules_load_all_in_directory (const char *dirname)
628 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
631 static gpointer
632 try_class (GIOExtension *extension,
633 guint is_supported_offset)
635 GType type = g_io_extension_get_type (extension);
636 typedef gboolean (*verify_func) (void);
637 gpointer class;
639 class = g_type_class_ref (type);
640 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
641 return class;
643 g_type_class_unref (class);
644 return NULL;
648 * _g_io_module_get_default_type:
649 * @extension_point: the name of an extension point
650 * @envvar: (allow-none): the name of an environment variable to
651 * override the default implementation.
652 * @is_supported_offset: a vtable offset, or zero
654 * Retrieves the default class implementing @extension_point.
656 * If @envvar is not %NULL, and the environment variable with that
657 * name is set, then the implementation it specifies will be tried
658 * first. After that, or if @envvar is not set, all other
659 * implementations will be tried in order of decreasing priority.
661 * If @is_supported_offset is non-zero, then it is the offset into the
662 * class vtable at which there is a function that takes no arguments and
663 * returns a boolean. This function will be called on each candidate
664 * implementation to check if it is actually usable or not.
666 * The result is cached after it is generated the first time, and
667 * the function is thread-safe.
669 * Return value: (transfer none): an object implementing
670 * @extension_point, or %NULL if there are no usable
671 * implementations.
673 GType
674 _g_io_module_get_default_type (const gchar *extension_point,
675 const gchar *envvar,
676 guint is_supported_offset)
678 static GRecMutex default_modules_lock;
679 static GHashTable *default_modules;
680 const char *use_this;
681 GList *l;
682 GIOExtensionPoint *ep;
683 GIOExtension *extension, *preferred;
684 gpointer impl;
686 g_rec_mutex_lock (&default_modules_lock);
687 if (default_modules)
689 gpointer key;
691 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
693 g_rec_mutex_unlock (&default_modules_lock);
694 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
697 else
699 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
702 _g_io_modules_ensure_loaded ();
703 ep = g_io_extension_point_lookup (extension_point);
705 if (!ep)
707 g_warn_if_reached ();
708 g_rec_mutex_unlock (&default_modules_lock);
709 return G_TYPE_INVALID;
712 use_this = envvar ? g_getenv (envvar) : NULL;
713 if (use_this)
715 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
716 if (preferred)
718 impl = try_class (preferred, is_supported_offset);
719 if (impl)
720 goto done;
722 else
723 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
725 else
726 preferred = NULL;
728 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
730 extension = l->data;
731 if (extension == preferred)
732 continue;
734 impl = try_class (extension, is_supported_offset);
735 if (impl)
736 goto done;
739 impl = NULL;
741 done:
742 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
743 g_rec_mutex_unlock (&default_modules_lock);
745 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
748 static gpointer
749 try_implementation (GIOExtension *extension,
750 GIOModuleVerifyFunc verify_func)
752 GType type = g_io_extension_get_type (extension);
753 gpointer impl;
755 if (g_type_is_a (type, G_TYPE_INITABLE))
756 return g_initable_new (type, NULL, NULL, NULL);
757 else
759 impl = g_object_new (type, NULL);
760 if (!verify_func || verify_func (impl))
761 return impl;
763 g_object_unref (impl);
764 return NULL;
769 * _g_io_module_get_default:
770 * @extension_point: the name of an extension point
771 * @envvar: (allow-none): the name of an environment variable to
772 * override the default implementation.
773 * @verify_func: (allow-none): a function to call to verify that
774 * a given implementation is usable in the current environment.
776 * Retrieves the default object implementing @extension_point.
778 * If @envvar is not %NULL, and the environment variable with that
779 * name is set, then the implementation it specifies will be tried
780 * first. After that, or if @envvar is not set, all other
781 * implementations will be tried in order of decreasing priority.
783 * If an extension point implementation implements #GInitable, then
784 * that implementation will only be used if it initializes
785 * successfully. Otherwise, if @verify_func is not %NULL, then it will
786 * be called on each candidate implementation after construction, to
787 * check if it is actually usable or not.
789 * The result is cached after it is generated the first time, and
790 * the function is thread-safe.
792 * Return value: (transfer none): an object implementing
793 * @extension_point, or %NULL if there are no usable
794 * implementations.
796 gpointer
797 _g_io_module_get_default (const gchar *extension_point,
798 const gchar *envvar,
799 GIOModuleVerifyFunc verify_func)
801 static GRecMutex default_modules_lock;
802 static GHashTable *default_modules;
803 const char *use_this;
804 GList *l;
805 GIOExtensionPoint *ep;
806 GIOExtension *extension, *preferred;
807 gpointer impl;
809 g_rec_mutex_lock (&default_modules_lock);
810 if (default_modules)
812 gpointer key;
814 if (g_hash_table_lookup_extended (default_modules, extension_point,
815 &key, &impl))
817 g_rec_mutex_unlock (&default_modules_lock);
818 return impl;
821 else
823 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
826 _g_io_modules_ensure_loaded ();
827 ep = g_io_extension_point_lookup (extension_point);
829 if (!ep)
831 g_warn_if_reached ();
832 g_rec_mutex_unlock (&default_modules_lock);
833 return NULL;
836 use_this = envvar ? g_getenv (envvar) : NULL;
837 if (use_this)
839 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
840 if (preferred)
842 impl = try_implementation (preferred, verify_func);
843 if (impl)
844 goto done;
846 else
847 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
849 else
850 preferred = NULL;
852 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
854 extension = l->data;
855 if (extension == preferred)
856 continue;
858 impl = try_implementation (extension, verify_func);
859 if (impl)
860 goto done;
863 impl = NULL;
865 done:
866 g_hash_table_insert (default_modules,
867 g_strdup (extension_point),
868 impl ? g_object_ref (impl) : NULL);
869 g_rec_mutex_unlock (&default_modules_lock);
871 return impl;
874 G_LOCK_DEFINE_STATIC (registered_extensions);
875 G_LOCK_DEFINE_STATIC (loaded_dirs);
877 extern GType _g_fen_directory_monitor_get_type (void);
878 extern GType _g_fen_file_monitor_get_type (void);
879 extern GType _g_inotify_directory_monitor_get_type (void);
880 extern GType _g_inotify_file_monitor_get_type (void);
881 extern GType _g_kqueue_directory_monitor_get_type (void);
882 extern GType _g_kqueue_file_monitor_get_type (void);
883 extern GType _g_unix_volume_monitor_get_type (void);
884 extern GType _g_local_vfs_get_type (void);
886 extern GType _g_win32_volume_monitor_get_type (void);
887 extern GType g_win32_directory_monitor_get_type (void);
888 extern GType _g_winhttp_vfs_get_type (void);
890 extern GType _g_dummy_proxy_resolver_get_type (void);
891 extern GType _g_dummy_tls_backend_get_type (void);
892 extern GType g_network_monitor_base_get_type (void);
893 #ifdef HAVE_NETLINK
894 extern GType _g_network_monitor_netlink_get_type (void);
895 #endif
897 #ifdef G_PLATFORM_WIN32
899 #include <windows.h>
901 static HMODULE gio_dll = NULL;
903 #ifdef DLL_EXPORT
905 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
906 DWORD fdwReason,
907 LPVOID lpvReserved);
909 BOOL WINAPI
910 DllMain (HINSTANCE hinstDLL,
911 DWORD fdwReason,
912 LPVOID lpvReserved)
914 if (fdwReason == DLL_PROCESS_ATTACH)
915 gio_dll = hinstDLL;
917 return TRUE;
920 #endif
922 void *
923 _g_io_win32_get_module (void)
925 if (!gio_dll)
926 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
927 (const char *) _g_io_win32_get_module,
928 &gio_dll);
929 return gio_dll;
932 #undef GIO_MODULE_DIR
934 /* GIO_MODULE_DIR is used only in code called just once,
935 * so no problem leaking this
937 #define GIO_MODULE_DIR \
938 g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
939 "lib/gio/modules", \
940 NULL)
942 #endif
944 void
945 _g_io_modules_ensure_extension_points_registered (void)
947 static gboolean registered_extensions = FALSE;
948 GIOExtensionPoint *ep;
950 G_LOCK (registered_extensions);
952 if (!registered_extensions)
954 registered_extensions = TRUE;
956 #ifdef G_OS_UNIX
957 #if !GLIB_CHECK_VERSION (3, 0, 0)
958 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
959 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
960 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
961 G_GNUC_END_IGNORE_DEPRECATIONS
962 #endif
963 #endif
965 ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
966 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
968 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
969 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
971 ep = g_io_extension_point_register (G_NFS_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
972 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
974 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
975 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
977 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
978 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
980 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
981 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
983 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
984 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
986 ep = g_io_extension_point_register ("gsettings-backend");
987 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
989 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
990 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
992 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
993 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
995 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
996 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
998 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
999 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1002 G_UNLOCK (registered_extensions);
1005 void
1006 _g_io_modules_ensure_loaded (void)
1008 static gboolean loaded_dirs = FALSE;
1009 const char *module_path;
1010 GIOModuleScope *scope;
1012 _g_io_modules_ensure_extension_points_registered ();
1014 G_LOCK (loaded_dirs);
1016 if (!loaded_dirs)
1018 loaded_dirs = TRUE;
1019 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1021 /* First load any overrides, extras */
1022 module_path = g_getenv ("GIO_EXTRA_MODULES");
1023 if (module_path)
1025 gchar **paths;
1026 int i;
1028 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1030 for (i = 0; paths[i] != NULL; i++)
1032 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1035 g_strfreev (paths);
1038 /* Then load the compiled in path */
1039 g_io_modules_scan_all_in_directory_with_scope (GIO_MODULE_DIR, scope);
1041 g_io_module_scope_free (scope);
1043 /* Initialize types from built-in "modules" */
1044 g_type_ensure (g_null_settings_backend_get_type ());
1045 g_type_ensure (g_memory_settings_backend_get_type ());
1046 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
1047 g_type_ensure (_g_inotify_directory_monitor_get_type ());
1048 g_type_ensure (_g_inotify_file_monitor_get_type ());
1049 #endif
1050 #if defined(HAVE_KQUEUE)
1051 g_type_ensure (_g_kqueue_directory_monitor_get_type ());
1052 g_type_ensure (_g_kqueue_file_monitor_get_type ());
1053 #endif
1054 #if defined(HAVE_FEN)
1055 g_type_ensure (_g_fen_directory_monitor_get_type ());
1056 g_type_ensure (_g_fen_file_monitor_get_type ());
1057 #endif
1058 #ifdef G_OS_WIN32
1059 g_type_ensure (_g_win32_volume_monitor_get_type ());
1060 g_type_ensure (g_win32_directory_monitor_get_type ());
1061 g_type_ensure (g_registry_backend_get_type ());
1062 #endif
1063 #ifdef HAVE_CARBON
1064 g_nextstep_settings_backend_get_type ();
1065 #endif
1066 #ifdef G_OS_UNIX
1067 g_type_ensure (_g_unix_volume_monitor_get_type ());
1068 #endif
1069 #ifdef G_OS_WIN32
1070 g_type_ensure (_g_winhttp_vfs_get_type ());
1071 #endif
1072 g_type_ensure (_g_local_vfs_get_type ());
1073 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1074 g_type_ensure (_g_socks4a_proxy_get_type ());
1075 g_type_ensure (_g_socks4_proxy_get_type ());
1076 g_type_ensure (_g_socks5_proxy_get_type ());
1077 g_type_ensure (_g_dummy_tls_backend_get_type ());
1078 g_type_ensure (g_network_monitor_base_get_type ());
1079 #ifdef HAVE_NETLINK
1080 g_type_ensure (_g_network_monitor_netlink_get_type ());
1081 #endif
1084 G_UNLOCK (loaded_dirs);
1087 static void
1088 g_io_extension_point_free (GIOExtensionPoint *ep)
1090 g_free (ep->name);
1091 g_free (ep);
1095 * g_io_extension_point_register:
1096 * @name: The name of the extension point
1098 * Registers an extension point.
1100 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1101 * owned by GIO and should not be freed.
1103 GIOExtensionPoint *
1104 g_io_extension_point_register (const char *name)
1106 GIOExtensionPoint *ep;
1108 G_LOCK (extension_points);
1109 if (extension_points == NULL)
1110 extension_points = g_hash_table_new_full (g_str_hash,
1111 g_str_equal,
1112 NULL,
1113 (GDestroyNotify)g_io_extension_point_free);
1115 ep = g_hash_table_lookup (extension_points, name);
1116 if (ep != NULL)
1118 G_UNLOCK (extension_points);
1119 return ep;
1122 ep = g_new0 (GIOExtensionPoint, 1);
1123 ep->name = g_strdup (name);
1125 g_hash_table_insert (extension_points, ep->name, ep);
1127 G_UNLOCK (extension_points);
1129 return ep;
1133 * g_io_extension_point_lookup:
1134 * @name: the name of the extension point
1136 * Looks up an existing extension point.
1138 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1139 * is no registered extension point with the given name.
1141 GIOExtensionPoint *
1142 g_io_extension_point_lookup (const char *name)
1144 GIOExtensionPoint *ep;
1146 G_LOCK (extension_points);
1147 ep = NULL;
1148 if (extension_points != NULL)
1149 ep = g_hash_table_lookup (extension_points, name);
1151 G_UNLOCK (extension_points);
1153 return ep;
1158 * g_io_extension_point_set_required_type:
1159 * @extension_point: a #GIOExtensionPoint
1160 * @type: the #GType to require
1162 * Sets the required type for @extension_point to @type.
1163 * All implementations must henceforth have this type.
1165 void
1166 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1167 GType type)
1169 extension_point->required_type = type;
1173 * g_io_extension_point_get_required_type:
1174 * @extension_point: a #GIOExtensionPoint
1176 * Gets the required type for @extension_point.
1178 * Returns: the #GType that all implementations must have,
1179 * or #G_TYPE_INVALID if the extension point has no required type
1181 GType
1182 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1184 return extension_point->required_type;
1187 static void
1188 lazy_load_modules (GIOExtensionPoint *extension_point)
1190 GIOModule *module;
1191 GList *l;
1193 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1195 module = l->data;
1197 if (!module->initialized)
1199 if (g_type_module_use (G_TYPE_MODULE (module)))
1200 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1201 else
1202 g_printerr ("Failed to load module: %s\n",
1203 module->filename);
1209 * g_io_extension_point_get_extensions:
1210 * @extension_point: a #GIOExtensionPoint
1212 * Gets a list of all extensions that implement this extension point.
1213 * The list is sorted by priority, beginning with the highest priority.
1215 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1216 * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
1217 * modified.
1219 GList *
1220 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1222 lazy_load_modules (extension_point);
1223 return extension_point->extensions;
1227 * g_io_extension_point_get_extension_by_name:
1228 * @extension_point: a #GIOExtensionPoint
1229 * @name: the name of the extension to get
1231 * Finds a #GIOExtension for an extension point by name.
1233 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1234 * given name, or %NULL if there is no extension with that name
1236 GIOExtension *
1237 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1238 const char *name)
1240 GList *l;
1242 lazy_load_modules (extension_point);
1243 for (l = extension_point->extensions; l != NULL; l = l->next)
1245 GIOExtension *e = l->data;
1247 if (e->name != NULL &&
1248 strcmp (e->name, name) == 0)
1249 return e;
1252 return NULL;
1255 static gint
1256 extension_prio_compare (gconstpointer a,
1257 gconstpointer b)
1259 const GIOExtension *extension_a = a, *extension_b = b;
1261 if (extension_a->priority > extension_b->priority)
1262 return -1;
1264 if (extension_b->priority > extension_a->priority)
1265 return 1;
1267 return 0;
1271 * g_io_extension_point_implement:
1272 * @extension_point_name: the name of the extension point
1273 * @type: the #GType to register as extension
1274 * @extension_name: the name for the extension
1275 * @priority: the priority for the extension
1277 * Registers @type as extension for the extension point with name
1278 * @extension_point_name.
1280 * If @type has already been registered as an extension for this
1281 * extension point, the existing #GIOExtension object is returned.
1283 * Returns: (transfer none): a #GIOExtension object for #GType
1285 GIOExtension *
1286 g_io_extension_point_implement (const char *extension_point_name,
1287 GType type,
1288 const char *extension_name,
1289 gint priority)
1291 GIOExtensionPoint *extension_point;
1292 GIOExtension *extension;
1293 GList *l;
1295 g_return_val_if_fail (extension_point_name != NULL, NULL);
1297 extension_point = g_io_extension_point_lookup (extension_point_name);
1298 if (extension_point == NULL)
1300 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1301 return NULL;
1304 if (extension_point->required_type != 0 &&
1305 !g_type_is_a (type, extension_point->required_type))
1307 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1308 "Expected type is %s.",
1309 g_type_name (type),
1310 extension_point_name,
1311 g_type_name (extension_point->required_type));
1312 return NULL;
1315 /* It's safe to register the same type multiple times */
1316 for (l = extension_point->extensions; l != NULL; l = l->next)
1318 extension = l->data;
1319 if (extension->type == type)
1320 return extension;
1323 extension = g_slice_new0 (GIOExtension);
1324 extension->type = type;
1325 extension->name = g_strdup (extension_name);
1326 extension->priority = priority;
1328 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1329 extension, extension_prio_compare);
1331 return extension;
1335 * g_io_extension_ref_class:
1336 * @extension: a #GIOExtension
1338 * Gets a reference to the class for the type that is
1339 * associated with @extension.
1341 * Returns: (transfer full): the #GTypeClass for the type of @extension
1343 GTypeClass *
1344 g_io_extension_ref_class (GIOExtension *extension)
1346 return g_type_class_ref (extension->type);
1350 * g_io_extension_get_type:
1351 * @extension: a #GIOExtension
1353 * Gets the type associated with @extension.
1355 * Returns: the type of @extension
1357 GType
1358 g_io_extension_get_type (GIOExtension *extension)
1360 return extension->type;
1364 * g_io_extension_get_name:
1365 * @extension: a #GIOExtension
1367 * Gets the name under which @extension was registered.
1369 * Note that the same type may be registered as extension
1370 * for multiple extension points, under different names.
1372 * Returns: the name of @extension.
1374 const char *
1375 g_io_extension_get_name (GIOExtension *extension)
1377 return extension->name;
1381 * g_io_extension_get_priority:
1382 * @extension: a #GIOExtension
1384 * Gets the priority with which @extension was registered.
1386 * Returns: the priority of @extension
1388 gint
1389 g_io_extension_get_priority (GIOExtension *extension)
1391 return extension->priority;