gio/ docs/reference/gio Merged gio-standalone into glib.
[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.
59  **/
60 GVfs *
61 g_local_vfs_new (void)
62 {
63   return g_object_new (G_TYPE_LOCAL_VFS, NULL);
64 }
65
66 static GFile *
67 g_local_vfs_get_file_for_path  (GVfs       *vfs,
68                                 const char *path)
69 {
70   return g_local_file_new (path);
71 }
72
73 static GFile *
74 g_local_vfs_get_file_for_uri   (GVfs       *vfs,
75                                 const char *uri)
76 {
77   char *path;
78   GFile *file;
79
80   path = g_filename_from_uri (uri, NULL, NULL);
81
82   if (path != NULL)
83     file = g_local_file_new (path);
84   else
85     file = g_dummy_file_new (uri);
86
87   g_free (path);
88
89   return file;
90 }
91
92 static const gchar * const *
93 g_local_vfs_get_supported_uri_schemes (GVfs *vfs)
94 {
95   static const gchar * uri_schemes[] = { "file", NULL };
96
97   return uri_schemes;
98 }
99
100 static GFile *
101 g_local_vfs_parse_name (GVfs       *vfs,
102                         const char *parse_name)
103 {
104   GFile *file;
105   char *filename;
106   char *user_name;
107   char *user_prefix;
108   const char *user_start, *user_end;
109   char *rest;
110   struct passwd *passwd_file_entry;
111   
112   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
113   g_return_val_if_fail (parse_name != NULL, NULL);
114
115   if (g_ascii_strncasecmp ("file:", parse_name, 5) == 0)
116     filename = g_filename_from_uri (parse_name, NULL, NULL);
117   else
118     {
119       if (*parse_name == '~')
120         {
121           parse_name ++;
122           user_start = parse_name;
123           
124           while (*parse_name != 0 && *parse_name != '/')
125             parse_name++;
126           
127           user_end = parse_name;
128
129           if (user_end == user_start)
130             user_prefix = g_strdup (g_get_home_dir());
131           else
132             {
133               user_name = g_strndup (user_start, user_end - user_start);
134               passwd_file_entry = getpwnam (user_name);
135               g_free (user_name);
136               
137               if (passwd_file_entry != NULL &&
138                   passwd_file_entry->pw_dir != NULL)
139                 user_prefix = g_strdup (passwd_file_entry->pw_dir);
140               else
141                 user_prefix = g_strdup (g_get_home_dir ());
142             }
143
144           rest = NULL;
145           if (*user_end != 0)
146             rest = g_filename_from_utf8 (user_end, -1, NULL, NULL, NULL);
147           
148           filename = g_build_filename (user_prefix, rest, NULL);
149           g_free (rest);
150           g_free (user_prefix);
151         }
152       else
153         filename = g_filename_from_utf8 (parse_name, -1, NULL, NULL, NULL);
154     }
155   
156   if (filename == NULL)
157     filename = g_strdup (parse_name);
158     
159   file = g_local_file_new (filename);
160   g_free (filename);
161
162   return file;
163 }
164
165 static gboolean
166 g_local_vfs_is_active (GVfs *vfs)
167 {
168   return TRUE;
169 }
170
171 static void
172 g_local_vfs_class_init (GLocalVfsClass *class)
173 {
174   GObjectClass *object_class;
175   GVfsClass *vfs_class;
176   
177   object_class = (GObjectClass *) class;
178
179   object_class->finalize = g_local_vfs_finalize;
180
181   vfs_class = G_VFS_CLASS (class);
182
183   vfs_class->name = "local";
184   vfs_class->priority = 0;
185   
186   vfs_class->is_active = g_local_vfs_is_active;
187   vfs_class->get_file_for_path = g_local_vfs_get_file_for_path;
188   vfs_class->get_file_for_uri = g_local_vfs_get_file_for_uri;
189   vfs_class->get_supported_uri_schemes = g_local_vfs_get_supported_uri_schemes;
190   vfs_class->parse_name = g_local_vfs_parse_name;
191 }