1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "glocalvfs.h"
25 #include "glocalfile.h"
26 #include <gio/gdummyfile.h>
27 #include <sys/types.h>
35 struct _GLocalVfsClass
37 GVfsClass parent_class;
41 G_DEFINE_TYPE (GLocalVfs, g_local_vfs, G_TYPE_VFS)
44 g_local_vfs_finalize (GObject *object)
47 G_OBJECT_CLASS (g_local_vfs_parent_class)->finalize (object);
51 g_local_vfs_init (GLocalVfs *vfs)
58 * Returns a new #GVfs handle for a local vfs.
60 * Returns: a new #GVfs handle.
63 g_local_vfs_new (void)
65 return g_object_new (G_TYPE_LOCAL_VFS, NULL);
69 g_local_vfs_get_file_for_path (GVfs *vfs,
72 return g_local_file_new (path);
76 g_local_vfs_get_file_for_uri (GVfs *vfs,
82 path = g_filename_from_uri (uri, NULL, NULL);
85 file = g_local_file_new (path);
87 file = g_dummy_file_new (uri);
94 static const gchar * const *
95 g_local_vfs_get_supported_uri_schemes (GVfs *vfs)
97 static const gchar * uri_schemes[] = { "file", NULL };
103 g_local_vfs_parse_name (GVfs *vfs,
104 const char *parse_name)
110 const char *user_start, *user_end;
112 struct passwd *passwd_file_entry;
114 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
115 g_return_val_if_fail (parse_name != NULL, NULL);
117 if (g_ascii_strncasecmp ("file:", parse_name, 5) == 0)
118 filename = g_filename_from_uri (parse_name, NULL, NULL);
121 if (*parse_name == '~')
124 user_start = parse_name;
126 while (*parse_name != 0 && *parse_name != '/')
129 user_end = parse_name;
131 if (user_end == user_start)
132 user_prefix = g_strdup (g_get_home_dir());
135 user_name = g_strndup (user_start, user_end - user_start);
136 passwd_file_entry = getpwnam (user_name);
139 if (passwd_file_entry != NULL &&
140 passwd_file_entry->pw_dir != NULL)
141 user_prefix = g_strdup (passwd_file_entry->pw_dir);
143 user_prefix = g_strdup (g_get_home_dir ());
148 rest = g_filename_from_utf8 (user_end, -1, NULL, NULL, NULL);
150 filename = g_build_filename (user_prefix, rest, NULL);
152 g_free (user_prefix);
155 filename = g_filename_from_utf8 (parse_name, -1, NULL, NULL, NULL);
158 if (filename == NULL)
159 filename = g_strdup (parse_name);
161 file = g_local_file_new (filename);
168 g_local_vfs_is_active (GVfs *vfs)
174 g_local_vfs_class_init (GLocalVfsClass *class)
176 GObjectClass *object_class;
177 GVfsClass *vfs_class;
179 object_class = (GObjectClass *) class;
181 object_class->finalize = g_local_vfs_finalize;
183 vfs_class = G_VFS_CLASS (class);
185 vfs_class->name = "local";
186 vfs_class->priority = 0;
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;