Home | History | Annotate | Download | only in gio
      1 /* GIO - GLib Input, Output and Streaming Library
      2  *
      3  * Copyright (C) 2006-2007 Red Hat, Inc.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Lesser General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Lesser General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Lesser General
     16  * Public License along with this library; if not, write to the
     17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
     18  * Boston, MA 02111-1307, USA.
     19  *
     20  * Author: Alexander Larsson <alexl (at) redhat.com>
     21  */
     22 
     23 #include "config.h"
     24 #include <string.h>
     25 #include "gvfs.h"
     26 #include "glocalvfs.h"
     27 #include "giomodule-priv.h"
     28 #include "glibintl.h"
     29 
     30 #include "gioalias.h"
     31 
     32 /**
     33  * SECTION:gvfs
     34  * @short_description: Virtual File System
     35  * @include: gio/gio.h
     36  *
     37  * Entry point for using GIO functionality.
     38  *
     39  **/
     40 
     41 G_DEFINE_TYPE (GVfs, g_vfs, G_TYPE_OBJECT);
     42 
     43 static void
     44 g_vfs_class_init (GVfsClass *klass)
     45 {
     46 }
     47 
     48 static void
     49 g_vfs_init (GVfs *vfs)
     50 {
     51 }
     52 
     53 /**
     54  * g_vfs_is_active:
     55  * @vfs: a #GVfs.
     56  *
     57  * Checks if the VFS is active.
     58  *
     59  * Returns: %TRUE if construction of the @vfs was successful and it is now active.
     60  **/
     61 gboolean
     62 g_vfs_is_active (GVfs *vfs)
     63 {
     64   GVfsClass *class;
     65 
     66   g_return_val_if_fail (G_IS_VFS (vfs), FALSE);
     67 
     68   class = G_VFS_GET_CLASS (vfs);
     69 
     70   return (* class->is_active) (vfs);
     71 }
     72 
     73 
     74 /**
     75  * g_vfs_get_file_for_path:
     76  * @vfs: a #GVfs.
     77  * @path: a string containing a VFS path.
     78  *
     79  * Gets a #GFile for @path.
     80  *
     81  * Returns: a #GFile.
     82  *     Free the returned object with g_object_unref().
     83  **/
     84 GFile *
     85 g_vfs_get_file_for_path (GVfs       *vfs,
     86 			 const char *path)
     87 {
     88   GVfsClass *class;
     89 
     90   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
     91   g_return_val_if_fail (path != NULL, NULL);
     92 
     93   class = G_VFS_GET_CLASS (vfs);
     94 
     95   return (* class->get_file_for_path) (vfs, path);
     96 }
     97 
     98 /**
     99  * g_vfs_get_file_for_uri:
    100  * @vfs: a#GVfs.
    101  * @uri: a string containing a URI
    102  *
    103  * Gets a #GFile for @uri.
    104  *
    105  * This operation never fails, but the returned object
    106  * might not support any I/O operation if the URI
    107  * is malformed or if the URI scheme is not supported.
    108  *
    109  * Returns: a #GFile.
    110  *     Free the returned object with g_object_unref().
    111  **/
    112 GFile *
    113 g_vfs_get_file_for_uri (GVfs       *vfs,
    114 			const char *uri)
    115 {
    116   GVfsClass *class;
    117 
    118   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
    119   g_return_val_if_fail (uri != NULL, NULL);
    120 
    121   class = G_VFS_GET_CLASS (vfs);
    122 
    123   return (* class->get_file_for_uri) (vfs, uri);
    124 }
    125 
    126 /**
    127  * g_vfs_get_supported_uri_schemes:
    128  * @vfs: a #GVfs.
    129  *
    130  * Gets a list of URI schemes supported by @vfs.
    131  *
    132  * Returns: a %NULL-terminated array of strings.
    133  *     The returned array belongs to GIO and must
    134  *     not be freed or modified.
    135  **/
    136 const gchar * const *
    137 g_vfs_get_supported_uri_schemes (GVfs *vfs)
    138 {
    139   GVfsClass *class;
    140 
    141   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
    142 
    143   class = G_VFS_GET_CLASS (vfs);
    144 
    145   return (* class->get_supported_uri_schemes) (vfs);
    146 }
    147 
    148 /**
    149  * g_vfs_parse_name:
    150  * @vfs: a #GVfs.
    151  * @parse_name: a string to be parsed by the VFS module.
    152  *
    153  * This operation never fails, but the returned object might
    154  * not support any I/O operations if the @parse_name cannot
    155  * be parsed by the #GVfs module.
    156  *
    157  * Returns: a #GFile for the given @parse_name.
    158  *     Free the returned object with g_object_unref().
    159  **/
    160 GFile *
    161 g_vfs_parse_name (GVfs       *vfs,
    162 		  const char *parse_name)
    163 {
    164   GVfsClass *class;
    165 
    166   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
    167   g_return_val_if_fail (parse_name != NULL, NULL);
    168 
    169   class = G_VFS_GET_CLASS (vfs);
    170 
    171   return (* class->parse_name) (vfs, parse_name);
    172 }
    173 
    174 static gpointer
    175 get_default_vfs (gpointer arg)
    176 {
    177   const char *use_this;
    178   GVfs *vfs;
    179   GList *l;
    180   GIOExtensionPoint *ep;
    181   GIOExtension *extension;
    182 
    183 
    184   use_this = g_getenv ("GIO_USE_VFS");
    185 
    186   /* Ensure vfs in modules loaded */
    187   _g_io_modules_ensure_loaded ();
    188 
    189   ep = g_io_extension_point_lookup (G_VFS_EXTENSION_POINT_NAME);
    190 
    191   if (use_this)
    192     {
    193       extension = g_io_extension_point_get_extension_by_name (ep, use_this);
    194       if (extension)
    195 	{
    196 	  vfs = g_object_new (g_io_extension_get_type (extension), NULL);
    197 
    198 	  if (g_vfs_is_active (vfs))
    199 	    return vfs;
    200 
    201 	  g_object_unref (vfs);
    202 	}
    203     }
    204 
    205   for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
    206     {
    207       extension = l->data;
    208 
    209       vfs = g_object_new (g_io_extension_get_type (extension), NULL);
    210 
    211       if (g_vfs_is_active (vfs))
    212 	return vfs;
    213 
    214       g_object_unref (vfs);
    215     }
    216 
    217 
    218   return NULL;
    219 }
    220 
    221 /**
    222  * g_vfs_get_default:
    223  *
    224  * Gets the default #GVfs for the system.
    225  *
    226  * Returns: a #GVfs.
    227  **/
    228 GVfs *
    229 g_vfs_get_default (void)
    230 {
    231   static GOnce once_init = G_ONCE_INIT;
    232 
    233   return g_once (&once_init, get_default_vfs, NULL);
    234 }
    235 
    236 /**
    237  * g_vfs_get_local:
    238  *
    239  * Gets the local #GVfs for the system.
    240  *
    241  * Returns: a #GVfs.
    242  **/
    243 GVfs *
    244 g_vfs_get_local (void)
    245 {
    246   static gsize vfs = 0;
    247 
    248   if (g_once_init_enter (&vfs))
    249     g_once_init_leave (&vfs, (gsize)_g_local_vfs_new ());
    250 
    251   return G_VFS (vfs);
    252 }
    253 
    254 #define __G_VFS_C__
    255 #include "gioaliasdef.c"
    256