giomodule.c 17.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

23
#include "config.h"
24

25 26
#include <string.h>

27
#include "giomodule.h"
Alexander Larsson's avatar
Alexander Larsson committed
28
#include "giomodule-priv.h"
29 30 31 32
#include "glocalfilemonitor.h"
#include "glocaldirectorymonitor.h"
#include "gnativevolumemonitor.h"
#include "gvfs.h"
33 34 35
#ifdef G_OS_UNIX
#include "gdesktopappinfo.h"
#endif
36 37
#include "gioalias.h"

38 39 40
/**
 * SECTION:giomodule
 * @short_description: Loadable GIO Modules
Matthias Clasen's avatar
Matthias Clasen committed
41
 * @include: gio/gio.h
42 43
 *
 * Provides an interface and default functions for loading and unloading 
Matthias Clasen's avatar
Matthias Clasen committed
44 45
 * modules. This is used internally to make GIO extensible, but can also
 * be used by others to implement module loading.
46 47 48
 * 
 **/

Matthias Clasen's avatar
Matthias Clasen committed
49 50 51 52
/**
 * SECTION:extensionpoints
 * @short_description: Extension Points
 * @include: gio.h
Matthias Clasen's avatar
Matthias Clasen committed
53
 * @see_also: <link linkend="extending-gio">Extending GIO</link>
Matthias Clasen's avatar
Matthias Clasen committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
 *
 * #GIOExtensionPoint provides a mechanism for modules to extend the
 * functionality of the library or application that loaded it in an 
 * organized fashion.  
 *
 * An extension point is identified by a name, and it may optionally
 * require that any implementation must by of a certain type (or derived
 * thereof). Use g_io_extension_point_register() to register an
 * extension point, and g_io_extension_point_set_required_type() to
 * set a required type.
 *
 * A module can implement an extension point by specifying the #GType 
 * that implements the functionality. Additionally, each implementation
 * of an extension point has a name, and a priority. Use
 * g_io_extension_point_implement() to implement an extension point.
 * 
 *  |[
 *  GIOExtensionPoint *ep;
 *
 *  /&ast; Register an extension point &ast;/
 *  ep = g_io_extension_point_register ("my-extension-point");
 *  g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
 *  ]|
 *
 *  |[
 *  /&ast; Implement an extension point &ast;/
 *  G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
 *  g_io_extension_point_implement ("my-extension-point",
 *                                  my_example_impl_get_type (),
 *                                  "my-example",
 *                                  10);
 *  ]|
 *
 *  It is up to the code that registered the extension point how
 *  it uses the implementations that have been associated with it.
 *  Depending on the use case, it may use all implementations, or
 *  only the one with the highest priority, or pick a specific
 *  one by name. 
 */
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
struct _GIOModule {
  GTypeModule parent_instance;
  
  gchar       *filename;
  GModule     *library;
  
  void (* load)   (GIOModule *module);
  void (* unload) (GIOModule *module);
};

struct _GIOModuleClass
{
  GTypeModuleClass parent_class;

};

static void      g_io_module_finalize      (GObject      *object);
static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
static void      g_io_module_unload_module (GTypeModule  *gmodule);

G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);

static void
g_io_module_class_init (GIOModuleClass *class)
{
  GObjectClass     *object_class      = G_OBJECT_CLASS (class);
  GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);

  object_class->finalize     = g_io_module_finalize;

  type_module_class->load    = g_io_module_load_module;
  type_module_class->unload  = g_io_module_unload_module;
}

static void
g_io_module_init (GIOModule *module)
{
}

static void
g_io_module_finalize (GObject *object)
{
  GIOModule *module = G_IO_MODULE (object);

  g_free (module->filename);

  G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
}

static gboolean
g_io_module_load_module (GTypeModule *gmodule)
{
  GIOModule *module = G_IO_MODULE (gmodule);

  if (!module->filename)
    {
      g_warning ("GIOModule path not set");
      return FALSE;
    }

  module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);

  if (!module->library)
    {
      g_printerr ("%s\n", g_module_error ());
      return FALSE;
    }

  /* Make sure that the loaded library contains the required methods */
  if (! g_module_symbol (module->library,
                         "g_io_module_load",
Alexander Larsson's avatar
Alexander Larsson committed
164
                         (gpointer) &module->load) ||
165 166
      ! g_module_symbol (module->library,
                         "g_io_module_unload",
Alexander Larsson's avatar
Alexander Larsson committed
167
                         (gpointer) &module->unload))
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    {
      g_printerr ("%s\n", g_module_error ());
      g_module_close (module->library);

      return FALSE;
    }

  /* Initialize the loaded module */
  module->load (module);

  return TRUE;
}

static void
g_io_module_unload_module (GTypeModule *gmodule)
{
  GIOModule *module = G_IO_MODULE (gmodule);

  module->unload (module);

  g_module_close (module->library);
  module->library = NULL;

  module->load   = NULL;
  module->unload = NULL;
}

/**
 * g_io_module_new:
Alexander Larsson's avatar
Alexander Larsson committed
197
 * @filename: filename of the shared library module.
198
 * 
Alexander Larsson's avatar
Alexander Larsson committed
199 200
 * Creates a new GIOModule that will load the specific
 * shared library when in use.
201 202
 * 
 * Returns: a #GIOModule from given @filename, 
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
 * or %NULL on error.
 **/
GIOModule *
g_io_module_new (const gchar *filename)
{
  GIOModule *module;

  g_return_val_if_fail (filename != NULL, NULL);

  module = g_object_new (G_IO_TYPE_MODULE, NULL);
  module->filename = g_strdup (filename);

  return module;
}

static gboolean
is_valid_module_name (const gchar *basename)
{
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
  return
    g_str_has_prefix (basename, "lib") &&
    g_str_has_suffix (basename, ".so");
#else
  return g_str_has_suffix (basename, ".dll");
#endif
}

Alexander Larsson's avatar
Alexander Larsson committed
230 231 232
/**
 * g_io_modules_load_all_in_directory:
 * @dirname: pathname for a directory containing modules to load.
233 234 235
 *
 * Loads all the modules in the specified directory.
 *
236 237 238 239 240
 * Returns: a list of #GIOModules loaded from the directory,
 *      All the modules are loaded into memory, if you want to
 *      unload them (enabling on-demand loading) you must call
 *      g_type_module_unuse() on all the modules. Free the list
 *      with g_list_free().
Alexander Larsson's avatar
Alexander Larsson committed
241 242 243
 **/
GList *
g_io_modules_load_all_in_directory (const char *dirname)
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
{
  const gchar *name;
  GDir        *dir;
  GList *modules;

  if (!g_module_supported ())
    return NULL;

  dir = g_dir_open (dirname, 0, NULL);
  if (!dir)
    return NULL;

  modules = NULL;
  while ((name = g_dir_read_name (dir)))
    {
      if (is_valid_module_name (name))
        {
          GIOModule *module;
          gchar     *path;

          path = g_build_filename (dirname, name, NULL);
          module = g_io_module_new (path);

          if (!g_type_module_use (G_TYPE_MODULE (module)))
            {
              g_printerr ("Failed to load module: %s\n", path);
              g_object_unref (module);
              g_free (path);
              continue;
            }
	  
          g_free (path);

          modules = g_list_prepend (modules, module);
        }
    }
  
  g_dir_close (dir);

  return modules;
}

G_LOCK_DEFINE_STATIC (loaded_dirs);

288 289
extern GType _g_fen_directory_monitor_get_type (void);
extern GType _g_fen_file_monitor_get_type (void);
290 291 292 293 294
extern GType _g_inotify_directory_monitor_get_type (void);
extern GType _g_inotify_file_monitor_get_type (void);
extern GType _g_unix_volume_monitor_get_type (void);
extern GType _g_local_vfs_get_type (void);

295
extern GType _g_win32_volume_monitor_get_type (void);
Tor Lillqvist's avatar
Tor Lillqvist committed
296
extern GType g_win32_directory_monitor_get_type (void);
297
extern GType _g_winhttp_vfs_get_type (void);
Tor Lillqvist's avatar
Tor Lillqvist committed
298

299
void
Alexander Larsson's avatar
Alexander Larsson committed
300
_g_io_modules_ensure_loaded (void)
301
{
302
  GList *modules, *l;
Alexander Larsson's avatar
Alexander Larsson committed
303
  static gboolean loaded_dirs = FALSE;
304
  GIOExtensionPoint *ep;
305
  const char *module_path;
306

307 308
  G_LOCK (loaded_dirs);

Alexander Larsson's avatar
Alexander Larsson committed
309
  if (!loaded_dirs)
310
    {
Alexander Larsson's avatar
Alexander Larsson committed
311
      loaded_dirs = TRUE;
312

313 314 315 316 317
#ifdef G_OS_UNIX
      ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
#endif
      
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
      ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
      
      ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);

      ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
      
      ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
      
      ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
      
      modules = g_io_modules_load_all_in_directory (GIO_MODULE_DIR);
334

335 336 337 338 339 340 341 342 343 344
      module_path = g_getenv ("GIO_EXTRA_MODULES");

      if (module_path)
        {
          int i = 0;
          gchar **paths;
          paths = g_strsplit (module_path, ":", 0);
    
          while (paths[i] != NULL)
            { 
345
              modules = g_list_concat (modules, g_io_modules_load_all_in_directory (paths[i]));
346 347 348 349 350 351
              i++;
            }

          g_strfreev (paths);
        }

352 353 354 355
      /* Initialize types from built-in "modules" */
#if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
      _g_inotify_directory_monitor_get_type ();
      _g_inotify_file_monitor_get_type ();
Alexander Larsson's avatar
Alexander Larsson committed
356 357
#endif
#if defined(HAVE_FEN)
358 359
      _g_fen_directory_monitor_get_type ();
      _g_fen_file_monitor_get_type ();
Tor Lillqvist's avatar
Tor Lillqvist committed
360
#endif
Alexander Larsson's avatar
Alexander Larsson committed
361
#ifdef G_OS_WIN32
362
      _g_win32_volume_monitor_get_type ();
Alexander Larsson's avatar
Alexander Larsson committed
363 364
      g_win32_directory_monitor_get_type ();
#endif
365 366
#ifdef G_OS_UNIX
      _g_unix_volume_monitor_get_type ();
367 368 369
#endif
#ifdef G_OS_WIN32
      _g_winhttp_vfs_get_type ();
370 371
#endif
      _g_local_vfs_get_type ();
372
    
373 374 375 376
      for (l = modules; l != NULL; l = l->next)
	g_type_module_unuse (G_TYPE_MODULE (l->data));
      
      g_list_free (modules);
377
    }
378

379 380
  G_UNLOCK (loaded_dirs);
}
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
struct _GIOExtension {
  char *name;
  GType type;
  gint priority;
};

struct _GIOExtensionPoint {
  GType required_type;
  char *name;
  GList *extensions;
};

static GHashTable *extension_points = NULL;
G_LOCK_DEFINE_STATIC(extension_points);


static void
g_io_extension_point_free (GIOExtensionPoint *ep)
{
  g_free (ep->name);
  g_free (ep);
}

Matthias Clasen's avatar
Matthias Clasen committed
405 406 407 408 409 410 411 412 413
/**
 * g_io_extension_point_register:
 * @name: The name of the extension point
 *
 * Registers an extension point.
 *
 * Returns: the new #GIOExtensionPoint. This object is owned by GIO
 *    and should not be freed
 */
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
GIOExtensionPoint *
g_io_extension_point_register (const char *name)
{
  GIOExtensionPoint *ep;
  
  G_LOCK (extension_points);
  if (extension_points == NULL)
    extension_points = g_hash_table_new_full (g_str_hash,
					      g_str_equal,
					      NULL,
					      (GDestroyNotify)g_io_extension_point_free);

  if (g_hash_table_lookup (extension_points, name) != NULL)
    {
      g_warning ("Extension point %s registered multiple times", name);
      G_UNLOCK (extension_points);
      return NULL;
    }

  ep = g_new0 (GIOExtensionPoint, 1);
  ep->name = g_strdup (name);
  
  g_hash_table_insert (extension_points, ep->name, ep);
  
  G_UNLOCK (extension_points);

  return ep;
}

Matthias Clasen's avatar
Matthias Clasen committed
443 444 445 446 447 448 449 450 451
/**
 * g_io_extension_point_lookup:
 * @name: the name of the extension point
 *
 * Looks up an existing extension point.
 *
 * Returns: the #GIOExtensionPoint, or %NULL if there is no
 *    registered extension point with the given name
 */
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
GIOExtensionPoint *
g_io_extension_point_lookup (const char *name)
{
  GIOExtensionPoint *ep;

  G_LOCK (extension_points);
  ep = NULL;
  if (extension_points != NULL)
    ep = g_hash_table_lookup (extension_points, name);
  
  G_UNLOCK (extension_points);

  return ep;
  
}

Matthias Clasen's avatar
Matthias Clasen committed
468 469 470 471 472 473 474 475
/**
 * g_io_extension_point_set_required_type:
 * @extension_point: a #GIOExtensionPoint
 * @type: the #GType to require
 *
 * Sets the required type for @extension_point to @type. 
 * All implementations must henceforth have this type.
 */
476 477 478 479 480 481 482
void
g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
					GType              type)
{
  extension_point->required_type = type;
}

Matthias Clasen's avatar
Matthias Clasen committed
483 484 485 486 487 488 489 490 491
/**
 * g_io_extension_point_get_required_type:
 * @extension_point: a #GIOExtensionPoint
 *
 * Gets the required type for @extension_point.
 *
 * Returns: the #GType that all implementations must have, 
 *     or #G_TYPE_INVALID if the extension point has no required type
 */
492 493 494 495 496 497
GType
g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
{
  return extension_point->required_type;
}

Matthias Clasen's avatar
Matthias Clasen committed
498 499 500 501 502 503 504 505 506 507
/**
 * g_io_extension_point_get_extensions:
 * @extension_point: a #GIOExtensionPoint
 *
 * Gets a list of all extensions that implement this extension point.
 * The list is sorted by priority, beginning with the highest priority.
 * 
 * Returns: a #GList of #GIOExtension<!-- -->s. The list is owned by
 *   GIO and should not be modified
 */
508 509 510 511 512 513
GList *
g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
{
  return extension_point->extensions;
}

Matthias Clasen's avatar
Matthias Clasen committed
514 515 516 517 518 519 520 521 522 523
/**
 * g_io_extension_point_get_extension_by_name:
 * @extension_point: a #GIOExtensionPoint
 * @name: the name of the extension to get
 *
 * Finds a #GIOExtension for an extension point by name.
 *
 * Returns: the #GIOExtension for @extension_point that has the
 *    given name, or %NULL if there is no extension with that name
 */
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
GIOExtension *
g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
					    const char        *name)
{
  GList *l;

  for (l = extension_point->extensions; l != NULL; l = l->next)
    {
      GIOExtension *e = l->data;

      if (e->name != NULL &&
	  strcmp (e->name, name) == 0)
	return e;
    }
  
  return NULL;
}

static gint
extension_prio_compare (gconstpointer  a,
			gconstpointer  b)
{
  const GIOExtension *extension_a = a, *extension_b = b;

  return extension_b->priority - extension_a->priority;
}

Matthias Clasen's avatar
Matthias Clasen committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/**
 * g_io_extension_point_implement:
 * @extension_point_name: the name of the extension point
 * @type: the #GType to register as extension 
 * @extension_name: the name for the extension
 * @priority: the priority for the extension
 *
 * Registers @type as extension for the extension point with name
 * @extension_point_name. 
 *
 * If @type has already been registered as an extension for this 
 * extension point, the existing #GIOExtension object is returned.
 *
 * Returns: a #GIOExtension object for #GType
 */
566 567
GIOExtension *
g_io_extension_point_implement (const char *extension_point_name,
Matthias Clasen's avatar
Matthias Clasen committed
568
				GType       type,
569
				const char *extension_name,
Matthias Clasen's avatar
Matthias Clasen committed
570
				gint        priority)
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
{
  GIOExtensionPoint *extension_point;
  GIOExtension *extension;
  GList *l;

  g_return_val_if_fail (extension_point_name != NULL, NULL);

  extension_point = g_io_extension_point_lookup (extension_point_name);
  if (extension_point == NULL)
    {
      g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
      return NULL;
    }
  
  if (extension_point->required_type != 0 &&
      !g_type_is_a (type, extension_point->required_type))
    {
      g_warning ("Tried to register an extension of the type %s to extension point %s. "
		 "Expected type is %s.",
		 g_type_name (type),
		 extension_point_name, 
		 g_type_name (extension_point->required_type));
      return NULL;
    }      

596
  /* It's safe to register the same type multiple times */
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
  for (l = extension_point->extensions; l != NULL; l = l->next)
    {
      extension = l->data;
      if (extension->type == type)
	return extension;
    }
  
  extension = g_slice_new0 (GIOExtension);
  extension->type = type;
  extension->name = g_strdup (extension_name);
  extension->priority = priority;
  
  extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
						      extension, extension_prio_compare);
  
  return extension;
}

Matthias Clasen's avatar
Matthias Clasen committed
615 616 617 618 619 620 621 622 623
/**
 * g_io_extension_ref_class:
 * @extension: a #GIOExtension
 *
 * Gets a reference to the class for the type that is 
 * associated with @extension.
 *
 * Returns: the #GTypeClass for the type of @extension
 */
624 625 626 627 628 629
GTypeClass *
g_io_extension_ref_class (GIOExtension *extension)
{
  return g_type_class_ref (extension->type);
}

Matthias Clasen's avatar
Matthias Clasen committed
630 631 632 633 634 635 636 637
/**
 * g_io_extension_get_type:
 * @extension: a #GIOExtension
 *
 * Gets the type associated with @extension.
 *
 * Returns: the type of @extension
 */
638 639 640 641 642 643
GType
g_io_extension_get_type (GIOExtension *extension)
{
  return extension->type;
}

Matthias Clasen's avatar
Matthias Clasen committed
644 645 646 647 648 649 650 651 652 653 654
/**
 * g_io_extension_get_name:
 * @extension: a #GIOExtension
 *
 * Gets the name under which @extension was registered.
 *
 * Note that the same type may be registered as extension
 * for multiple extension points, under different names.
 *
 * Returns: the name of @extension.
 */
655 656 657 658 659 660
const char *
g_io_extension_get_name (GIOExtension *extension)
{
  return extension->name;
}

Matthias Clasen's avatar
Matthias Clasen committed
661 662 663 664 665 666 667 668
/**
 * g_io_extension_get_priority:
 * @extension: a #GIOExtension
 *
 * Gets the priority with which @extension was registered.
 *
 * Returns: the priority of @extension
 */
669 670 671 672 673 674
gint
g_io_extension_get_priority (GIOExtension *extension)
{
  return extension->priority;
}

675 676
#define __G_IO_MODULE_C__
#include "gioaliasdef.c"