Clean up some docs
[platform/upstream/glib.git] / gio / glocalvfs.c
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@redhat.com>
21  */
22
23 #include <config.h>
24 #include "glocalvfs.h"
25 #include "glocalfile.h"
26 #include <gio/gdummyfile.h>
27 #include <sys/types.h>
28 #include <pwd.h>
29
30 struct _GLocalVfs
31 {
32   GVfs parent;
33 };
34
35 struct _GLocalVfsClass
36 {
37   GVfsClass parent_class;
38   
39 };
40
41 G_DEFINE_TYPE (GLocalVfs, g_local_vfs, G_TYPE_VFS)
42  
43 static void
44 g_local_vfs_finalize (GObject *object)
45 {
46   /* must chain up */
47   G_OBJECT_CLASS (g_local_vfs_parent_class)->finalize (object);
48 }
49
50 static void
51 g_local_vfs_init (GLocalVfs *vfs)
52 {
53 }
54
55 /**
56  * g_local_vfs_new:
57  *
58  * Returns a new #GVfs handle for a local vfs.
59  *
60  * Returns: a new #GVfs handle.
61  **/
62 GVfs *
63 g_local_vfs_new (void)
64 {
65   return g_object_new (G_TYPE_LOCAL_VFS, NULL);
66 }
67
68 static GFile *
69 g_local_vfs_get_file_for_path  (GVfs       *vfs,
70                                 const char *path)
71 {
72   return g_local_file_new (path);
73 }
74
75 static GFile *
76 g_local_vfs_get_file_for_uri   (GVfs       *vfs,
77                                 const char *uri)
78 {
79   char *path;
80   GFile *file;
81
82   path = g_filename_from_uri (uri, NULL, NULL);
83
84   if (path != NULL)
85     file = g_local_file_new (path);
86   else
87     file = g_dummy_file_new (uri);
88
89   g_free (path);
90
91   return file;
92 }
93
94 static const gchar * const *
95 g_local_vfs_get_supported_uri_schemes (GVfs *vfs)
96 {
97   static const gchar * uri_schemes[] = { "file", NULL };
98
99   return uri_schemes;
100 }
101
102 static GFile *
103 g_local_vfs_parse_name (GVfs       *vfs,
104                         const char *parse_name)
105 {
106   GFile *file;
107   char *filename;
108   char *user_name;
109   char *user_prefix;
110   const char *user_start, *user_end;
111   char *rest;
112   struct passwd *passwd_file_entry;
113   
114   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
115   g_return_val_if_fail (parse_name != NULL, NULL);
116
117   if (g_ascii_strncasecmp ("file:", parse_name, 5) == 0)
118     filename = g_filename_from_uri (parse_name, NULL, NULL);
119   else
120     {
121       if (*parse_name == '~')
122         {
123           parse_name ++;
124           user_start = parse_name;
125           
126           while (*parse_name != 0 && *parse_name != '/')
127             parse_name++;
128           
129           user_end = parse_name;
130
131           if (user_end == user_start)
132             user_prefix = g_strdup (g_get_home_dir());
133           else
134             {
135               user_name = g_strndup (user_start, user_end - user_start);
136               passwd_file_entry = getpwnam (user_name);
137               g_free (user_name);
138               
139               if (passwd_file_entry != NULL &&
140                   passwd_file_entry->pw_dir != NULL)
141                 user_prefix = g_strdup (passwd_file_entry->pw_dir);
142               else
143                 user_prefix = g_strdup (g_get_home_dir ());
144             }
145
146           rest = NULL;
147           if (*user_end != 0)
148             rest = g_filename_from_utf8 (user_end, -1, NULL, NULL, NULL);
149           
150           filename = g_build_filename (user_prefix, rest, NULL);
151           g_free (rest);
152           g_free (user_prefix);
153         }
154       else
155         filename = g_filename_from_utf8 (parse_name, -1, NULL, NULL, NULL);
156     }
157   
158   if (filename == NULL)
159     filename = g_strdup (parse_name);
160     
161   file = g_local_file_new (filename);
162   g_free (filename);
163
164   return file;
165 }
166
167 static gboolean
168 g_local_vfs_is_active (GVfs *vfs)
169 {
170   return TRUE;
171 }
172
173 static void
174 g_local_vfs_class_init (GLocalVfsClass *class)
175 {
176   GObjectClass *object_class;
177   GVfsClass *vfs_class;
178   
179   object_class = (GObjectClass *) class;
180
181   object_class->finalize = g_local_vfs_finalize;
182
183   vfs_class = G_VFS_CLASS (class);
184
185   vfs_class->name = "local";
186   vfs_class->priority = 0;
187   
188   vfs_class->is_active = g_local_vfs_is_active;
189   vfs_class->get_file_for_path = g_local_vfs_get_file_for_path;
190   vfs_class->get_file_for_uri = g_local_vfs_get_file_for_uri;
191   vfs_class->get_supported_uri_schemes = g_local_vfs_get_supported_uri_schemes;
192   vfs_class->parse_name = g_local_vfs_parse_name;
193 }