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