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