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