1 /* GTK - The GIMP Toolkit
2 * gtkfilesystemunix.c: Default implementation of GtkFileSystem for UNIX-like systems
3 * Copyright (C) 2003, 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 Public
16 * 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.
23 #include "gtkfilesystem.h"
24 #include "gtkfilesystemunix.h"
25 #include "gtkicontheme.h"
28 #define XDG_PREFIX _gtk_xdg
29 #include "xdgmime/xdgmime.h"
34 #include <sys/types.h>
38 #define BOOKMARKS_FILENAME ".gtk-bookmarks"
39 #define BOOKMARKS_TMP_FILENAME ".gtk-bookmarks-XXXXXX"
41 typedef struct _GtkFileSystemUnixClass GtkFileSystemUnixClass;
43 #define GTK_FILE_SYSTEM_UNIX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_UNIX, GtkFileSystemUnixClass))
44 #define GTK_IS_FILE_SYSTEM_UNIX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_UNIX))
45 #define GTK_FILE_SYSTEM_UNIX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_UNIX, GtkFileSystemUnixClass))
47 struct _GtkFileSystemUnixClass
49 GObjectClass parent_class;
52 struct _GtkFileSystemUnix
54 GObject parent_instance;
56 GHashTable *folder_hash;
59 /* Icon type, supplemented by MIME type
63 ICON_REGULAR, /* Use mime type for icon */
65 ICON_BROKEN_SYMBOLIC_LINK,
66 ICON_CHARACTER_DEVICE,
74 #define GTK_TYPE_FILE_FOLDER_UNIX (gtk_file_folder_unix_get_type ())
75 #define GTK_FILE_FOLDER_UNIX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnix))
76 #define GTK_IS_FILE_FOLDER_UNIX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_FOLDER_UNIX))
77 #define GTK_FILE_FOLDER_UNIX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnixClass))
78 #define GTK_IS_FILE_FOLDER_UNIX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_FOLDER_UNIX))
79 #define GTK_FILE_FOLDER_UNIX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnixClass))
81 typedef struct _GtkFileFolderUnix GtkFileFolderUnix;
82 typedef struct _GtkFileFolderUnixClass GtkFileFolderUnixClass;
84 struct _GtkFileFolderUnixClass
86 GObjectClass parent_class;
89 struct _GtkFileFolderUnix
91 GObject parent_instance;
93 GtkFileSystemUnix *system_unix;
94 GtkFileInfoType types;
98 static GObjectClass *system_parent_class;
99 static GObjectClass *folder_parent_class;
101 static void gtk_file_system_unix_class_init (GtkFileSystemUnixClass *class);
102 static void gtk_file_system_unix_iface_init (GtkFileSystemIface *iface);
103 static void gtk_file_system_unix_init (GtkFileSystemUnix *impl);
104 static void gtk_file_system_unix_finalize (GObject *object);
106 static GSList * gtk_file_system_unix_list_volumes (GtkFileSystem *file_system);
107 static GtkFileSystemVolume *gtk_file_system_unix_get_volume_for_path (GtkFileSystem *file_system,
108 const GtkFilePath *path);
110 static GtkFileFolder *gtk_file_system_unix_get_folder (GtkFileSystem *file_system,
111 const GtkFilePath *path,
112 GtkFileInfoType types,
114 static gboolean gtk_file_system_unix_create_folder (GtkFileSystem *file_system,
115 const GtkFilePath *path,
118 static void gtk_file_system_unix_volume_free (GtkFileSystem *file_system,
119 GtkFileSystemVolume *volume);
120 static GtkFilePath *gtk_file_system_unix_volume_get_base_path (GtkFileSystem *file_system,
121 GtkFileSystemVolume *volume);
122 static gboolean gtk_file_system_unix_volume_get_is_mounted (GtkFileSystem *file_system,
123 GtkFileSystemVolume *volume);
124 static gboolean gtk_file_system_unix_volume_mount (GtkFileSystem *file_system,
125 GtkFileSystemVolume *volume,
127 static gchar * gtk_file_system_unix_volume_get_display_name (GtkFileSystem *file_system,
128 GtkFileSystemVolume *volume);
129 static GdkPixbuf * gtk_file_system_unix_volume_render_icon (GtkFileSystem *file_system,
130 GtkFileSystemVolume *volume,
135 static gboolean gtk_file_system_unix_get_parent (GtkFileSystem *file_system,
136 const GtkFilePath *path,
137 GtkFilePath **parent,
139 static GtkFilePath * gtk_file_system_unix_make_path (GtkFileSystem *file_system,
140 const GtkFilePath *base_path,
141 const gchar *display_name,
143 static gboolean gtk_file_system_unix_parse (GtkFileSystem *file_system,
144 const GtkFilePath *base_path,
146 GtkFilePath **folder,
150 static gchar * gtk_file_system_unix_path_to_uri (GtkFileSystem *file_system,
151 const GtkFilePath *path);
152 static gchar * gtk_file_system_unix_path_to_filename (GtkFileSystem *file_system,
153 const GtkFilePath *path);
154 static GtkFilePath *gtk_file_system_unix_uri_to_path (GtkFileSystem *file_system,
156 static GtkFilePath *gtk_file_system_unix_filename_to_path (GtkFileSystem *file_system,
157 const gchar *filename);
159 static GdkPixbuf *gtk_file_system_unix_render_icon (GtkFileSystem *file_system,
160 const GtkFilePath *path,
165 static gboolean gtk_file_system_unix_insert_bookmark (GtkFileSystem *file_system,
166 const GtkFilePath *path,
169 static gboolean gtk_file_system_unix_remove_bookmark (GtkFileSystem *file_system,
170 const GtkFilePath *path,
172 static GSList * gtk_file_system_unix_list_bookmarks (GtkFileSystem *file_system);
174 static GType gtk_file_folder_unix_get_type (void);
175 static void gtk_file_folder_unix_class_init (GtkFileFolderUnixClass *class);
176 static void gtk_file_folder_unix_iface_init (GtkFileFolderIface *iface);
177 static void gtk_file_folder_unix_init (GtkFileFolderUnix *impl);
178 static void gtk_file_folder_unix_finalize (GObject *object);
180 static GtkFileInfo *gtk_file_folder_unix_get_info (GtkFileFolder *folder,
181 const GtkFilePath *path,
183 static gboolean gtk_file_folder_unix_list_children (GtkFileFolder *folder,
187 static GtkFilePath *filename_to_path (const gchar *filename);
189 static gboolean filename_is_root (const char *filename);
190 static GtkFileInfo *filename_get_info (const gchar *filename,
191 GtkFileInfoType types,
198 gtk_file_system_unix_get_type (void)
200 static GType file_system_unix_type = 0;
202 if (!file_system_unix_type)
204 static const GTypeInfo file_system_unix_info =
206 sizeof (GtkFileSystemUnixClass),
207 NULL, /* base_init */
208 NULL, /* base_finalize */
209 (GClassInitFunc) gtk_file_system_unix_class_init,
210 NULL, /* class_finalize */
211 NULL, /* class_data */
212 sizeof (GtkFileSystemUnix),
214 (GInstanceInitFunc) gtk_file_system_unix_init,
217 static const GInterfaceInfo file_system_info =
219 (GInterfaceInitFunc) gtk_file_system_unix_iface_init, /* interface_init */
220 NULL, /* interface_finalize */
221 NULL /* interface_data */
224 file_system_unix_type = g_type_register_static (G_TYPE_OBJECT,
226 &file_system_unix_info, 0);
227 g_type_add_interface_static (file_system_unix_type,
228 GTK_TYPE_FILE_SYSTEM,
232 return file_system_unix_type;
236 * gtk_file_system_unix_new:
238 * Creates a new #GtkFileSystemUnix object. #GtkFileSystemUnix
239 * implements the #GtkFileSystem interface with direct access to
240 * the filesystem using Unix/Linux API calls
242 * Return value: the new #GtkFileSystemUnix object
245 gtk_file_system_unix_new (void)
247 return g_object_new (GTK_TYPE_FILE_SYSTEM_UNIX, NULL);
251 gtk_file_system_unix_class_init (GtkFileSystemUnixClass *class)
253 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
255 system_parent_class = g_type_class_peek_parent (class);
257 gobject_class->finalize = gtk_file_system_unix_finalize;
261 gtk_file_system_unix_iface_init (GtkFileSystemIface *iface)
263 iface->list_volumes = gtk_file_system_unix_list_volumes;
264 iface->get_volume_for_path = gtk_file_system_unix_get_volume_for_path;
265 iface->get_folder = gtk_file_system_unix_get_folder;
266 iface->create_folder = gtk_file_system_unix_create_folder;
267 iface->volume_free = gtk_file_system_unix_volume_free;
268 iface->volume_get_base_path = gtk_file_system_unix_volume_get_base_path;
269 iface->volume_get_is_mounted = gtk_file_system_unix_volume_get_is_mounted;
270 iface->volume_mount = gtk_file_system_unix_volume_mount;
271 iface->volume_get_display_name = gtk_file_system_unix_volume_get_display_name;
272 iface->volume_render_icon = gtk_file_system_unix_volume_render_icon;
273 iface->get_parent = gtk_file_system_unix_get_parent;
274 iface->make_path = gtk_file_system_unix_make_path;
275 iface->parse = gtk_file_system_unix_parse;
276 iface->path_to_uri = gtk_file_system_unix_path_to_uri;
277 iface->path_to_filename = gtk_file_system_unix_path_to_filename;
278 iface->uri_to_path = gtk_file_system_unix_uri_to_path;
279 iface->filename_to_path = gtk_file_system_unix_filename_to_path;
280 iface->render_icon = gtk_file_system_unix_render_icon;
281 iface->insert_bookmark = gtk_file_system_unix_insert_bookmark;
282 iface->remove_bookmark = gtk_file_system_unix_remove_bookmark;
283 iface->list_bookmarks = gtk_file_system_unix_list_bookmarks;
287 gtk_file_system_unix_init (GtkFileSystemUnix *system_unix)
289 system_unix->folder_hash = g_hash_table_new (g_str_hash, g_str_equal);
293 gtk_file_system_unix_finalize (GObject *object)
295 GtkFileSystemUnix *system_unix;
297 system_unix = GTK_FILE_SYSTEM_UNIX (object);
299 /* FIXME: assert that the hash is empty? */
300 g_hash_table_destroy (system_unix->folder_hash);
302 system_parent_class->finalize (object);
305 /* Returns our single root volume */
306 static GtkFileSystemVolume *
307 get_root_volume (void)
309 return (GtkFileSystemVolume *) gtk_file_path_new_dup ("/");
313 gtk_file_system_unix_list_volumes (GtkFileSystem *file_system)
315 return g_slist_append (NULL, get_root_volume ());
318 static GtkFileSystemVolume *
319 gtk_file_system_unix_get_volume_for_path (GtkFileSystem *file_system,
320 const GtkFilePath *path)
322 return get_root_volume ();
325 static GtkFileFolder *
326 gtk_file_system_unix_get_folder (GtkFileSystem *file_system,
327 const GtkFilePath *path,
328 GtkFileInfoType types,
331 GtkFileSystemUnix *system_unix;
332 GtkFileFolderUnix *folder_unix;
333 const char *filename;
335 system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
337 filename = gtk_file_path_get_string (path);
338 g_return_val_if_fail (filename != NULL, NULL);
339 g_return_val_if_fail (g_path_is_absolute (filename), NULL);
341 folder_unix = g_hash_table_lookup (system_unix->folder_hash, filename);
345 folder_unix->types |= types;
346 return g_object_ref (folder_unix);
350 folder_unix = g_object_new (GTK_TYPE_FILE_FOLDER_UNIX, NULL);
351 folder_unix->system_unix = system_unix;
352 folder_unix->filename = g_strdup (filename);
353 folder_unix->types = types;
355 g_hash_table_insert (system_unix->folder_hash, folder_unix->filename, folder_unix);
357 return GTK_FILE_FOLDER (folder_unix);
362 gtk_file_system_unix_create_folder (GtkFileSystem *file_system,
363 const GtkFilePath *path,
366 GtkFileSystemUnix *system_unix;
367 const char *filename;
371 system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
373 filename = gtk_file_path_get_string (path);
374 g_return_val_if_fail (filename != NULL, FALSE);
375 g_return_val_if_fail (g_path_is_absolute (filename), FALSE);
377 result = mkdir (filename, 0777) == 0;
381 gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
383 GTK_FILE_SYSTEM_ERROR,
384 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
385 _("error creating directory '%s': %s"),
386 filename_utf8 ? filename_utf8 : "???",
388 g_free (filename_utf8);
392 if (filename_is_root (filename))
393 return TRUE; /* hmmm, but with no notification */
395 parent = g_path_get_dirname (filename);
398 GtkFileFolderUnix *folder_unix;
400 folder_unix = g_hash_table_lookup (system_unix->folder_hash, parent);
405 paths = g_slist_append (NULL, (GtkFilePath *) path);
406 g_signal_emit_by_name (folder_unix, "files-added", paths);
407 g_slist_free (paths);
415 gtk_file_system_unix_volume_free (GtkFileSystem *file_system,
416 GtkFileSystemVolume *volume)
420 path = (GtkFilePath *) volume;
421 gtk_file_path_free (path);
425 gtk_file_system_unix_volume_get_base_path (GtkFileSystem *file_system,
426 GtkFileSystemVolume *volume)
428 return gtk_file_path_new_dup ("/");
432 gtk_file_system_unix_volume_get_is_mounted (GtkFileSystem *file_system,
433 GtkFileSystemVolume *volume)
439 gtk_file_system_unix_volume_mount (GtkFileSystem *file_system,
440 GtkFileSystemVolume *volume,
444 GTK_FILE_SYSTEM_ERROR,
445 GTK_FILE_SYSTEM_ERROR_FAILED,
446 _("This file system does not support mounting"));
451 gtk_file_system_unix_volume_get_display_name (GtkFileSystem *file_system,
452 GtkFileSystemVolume *volume)
454 return g_strdup (_("Filesystem")); /* Same as Nautilus */
458 get_icon_type (const char *filename,
464 /* If stat fails, try to fall back to lstat to catch broken links
466 if (stat (filename, &statbuf) != 0 &&
467 lstat (filename, &statbuf) != 0)
469 gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
471 GTK_FILE_SYSTEM_ERROR,
472 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
473 _("error getting information for '%s': %s"),
474 filename_utf8 ? filename_utf8 : "???",
476 g_free (filename_utf8);
481 if (S_ISBLK (statbuf.st_mode))
482 icon_type = ICON_BLOCK_DEVICE;
483 else if (S_ISLNK (statbuf.st_mode))
484 icon_type = ICON_BROKEN_SYMBOLIC_LINK; /* See above */
485 else if (S_ISCHR (statbuf.st_mode))
486 icon_type = ICON_CHARACTER_DEVICE;
487 else if (S_ISDIR (statbuf.st_mode))
488 icon_type = ICON_DIRECTORY;
489 else if (S_ISFIFO (statbuf.st_mode))
490 icon_type = ICON_FIFO;
491 else if (S_ISSOCK (statbuf.st_mode))
492 icon_type = ICON_SOCKET;
495 icon_type = ICON_REGULAR;
498 if ((types & GTK_FILE_INFO_ICON) && icon_type == GTK_FILE_ICON_REGULAR &&
499 (statbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) &&
500 (strcmp (mime_type, XDG_MIME_TYPE_UNKNOWN) == 0 ||
501 strcmp (mime_type, "application/x-executable") == 0 ||
502 strcmp (mime_type, "application/x-shellscript") == 0))
503 gtk_file_info_set_icon_type (info, GTK_FILE_ICON_EXECUTABLE);
517 icon_cache_element_free (IconCacheElement *element)
520 g_object_unref (element->pixbuf);
525 icon_theme_changed (GtkIconTheme *icon_theme)
529 /* Difference from the initial creation is that we don't
530 * reconnect the signal
532 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
533 (GDestroyNotify)g_free,
534 (GDestroyNotify)icon_cache_element_free);
535 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-file-icon-cache",
536 cache, (GDestroyNotify)g_hash_table_destroy);
540 get_cached_icon (GtkWidget *widget,
544 GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
545 GHashTable *cache = g_object_get_data (G_OBJECT (icon_theme), "gtk-file-icon-cache");
546 IconCacheElement *element;
550 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
551 (GDestroyNotify)g_free,
552 (GDestroyNotify)icon_cache_element_free);
554 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-file-icon-cache",
555 cache, (GDestroyNotify)g_hash_table_destroy);
556 g_signal_connect (icon_theme, "changed",
557 G_CALLBACK (icon_theme_changed), NULL);
560 element = g_hash_table_lookup (cache, name);
563 element = g_new0 (IconCacheElement, 1);
564 g_hash_table_insert (cache, g_strdup (name), element);
567 if (element->size != pixel_size)
570 g_object_unref (element->pixbuf);
571 element->size = pixel_size;
572 element->pixbuf = gtk_icon_theme_load_icon (icon_theme, name,
573 pixel_size, 0, NULL);
576 return element->pixbuf ? g_object_ref (element->pixbuf) : NULL;
580 gtk_file_system_unix_volume_render_icon (GtkFileSystem *file_system,
581 GtkFileSystemVolume *volume,
586 /* FIXME: set the GError if we can't load the icon */
587 return get_cached_icon (widget, "gnome-fs-blockdev", pixel_size);
591 gtk_file_system_unix_get_parent (GtkFileSystem *file_system,
592 const GtkFilePath *path,
593 GtkFilePath **parent,
596 const char *filename;
598 filename = gtk_file_path_get_string (path);
599 g_return_val_if_fail (filename != NULL, FALSE);
600 g_return_val_if_fail (g_path_is_absolute (filename), FALSE);
602 if (filename_is_root (filename))
608 gchar *parent_filename = g_path_get_dirname (filename);
609 *parent = filename_to_path (parent_filename);
610 g_free (parent_filename);
617 gtk_file_system_unix_make_path (GtkFileSystem *file_system,
618 const GtkFilePath *base_path,
619 const gchar *display_name,
622 const char *base_filename;
624 gchar *full_filename;
625 GError *tmp_error = NULL;
628 base_filename = gtk_file_path_get_string (base_path);
629 g_return_val_if_fail (base_filename != NULL, NULL);
630 g_return_val_if_fail (g_path_is_absolute (base_filename), NULL);
632 filename = g_filename_from_utf8 (display_name, -1, NULL, NULL, &tmp_error);
636 GTK_FILE_SYSTEM_ERROR,
637 GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
641 g_error_free (tmp_error);
646 full_filename = g_build_filename (base_filename, filename, NULL);
647 result = filename_to_path (full_filename);
649 g_free (full_filename);
654 /* If this was a publically exported function, it should return
655 * a dup'ed result, but we make it modify-in-place for efficiency
656 * here, and because it works for us.
659 canonicalize_filename (gchar *filename)
662 gboolean last_was_slash = FALSE;
669 if (*p == G_DIR_SEPARATOR)
672 *q++ = G_DIR_SEPARATOR;
674 last_was_slash = TRUE;
678 if (last_was_slash && *p == '.')
680 if (*(p + 1) == G_DIR_SEPARATOR ||
683 if (*(p + 1) == '\0')
688 else if (*(p + 1) == '.' &&
689 (*(p + 2) == G_DIR_SEPARATOR ||
692 if (q > filename + 1)
695 while (q > filename + 1 &&
696 *(q - 1) != G_DIR_SEPARATOR)
700 if (*(p + 2) == '\0')
708 last_was_slash = FALSE;
714 last_was_slash = FALSE;
721 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
728 gtk_file_system_unix_parse (GtkFileSystem *file_system,
729 const GtkFilePath *base_path,
731 GtkFilePath **folder,
735 const char *base_filename;
737 gboolean result = FALSE;
739 base_filename = gtk_file_path_get_string (base_path);
740 g_return_val_if_fail (base_filename != NULL, FALSE);
741 g_return_val_if_fail (g_path_is_absolute (base_filename), FALSE);
743 last_slash = strrchr (str, G_DIR_SEPARATOR);
746 *folder = gtk_file_path_copy (base_path);
747 *file_part = g_strdup (str);
754 GError *tmp_error = NULL;
756 if (last_slash == str)
757 folder_part = g_strdup ("/");
759 folder_part = g_filename_from_utf8 (str, last_slash - str,
760 NULL, NULL, &tmp_error);
765 GTK_FILE_SYSTEM_ERROR,
766 GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
769 g_error_free (tmp_error);
773 if (folder_part[0] == G_DIR_SEPARATOR)
774 folder_path = folder_part;
777 folder_path = g_build_filename (base_filename, folder_part, NULL);
778 g_free (folder_part);
781 canonicalize_filename (folder_path);
783 *folder = filename_to_path (folder_path);
784 *file_part = g_strdup (last_slash + 1);
786 g_free (folder_path);
796 gtk_file_system_unix_path_to_uri (GtkFileSystem *file_system,
797 const GtkFilePath *path)
799 return g_filename_to_uri (gtk_file_path_get_string (path), NULL, NULL);
803 gtk_file_system_unix_path_to_filename (GtkFileSystem *file_system,
804 const GtkFilePath *path)
806 return g_strdup (gtk_file_path_get_string (path));
810 gtk_file_system_unix_uri_to_path (GtkFileSystem *file_system,
813 gchar *filename = g_filename_from_uri (uri, NULL, NULL);
815 return gtk_file_path_new_steal (filename);
821 gtk_file_system_unix_filename_to_path (GtkFileSystem *file_system,
822 const gchar *filename)
824 return gtk_file_path_new_dup (filename);
828 gtk_file_system_unix_render_icon (GtkFileSystem *file_system,
829 const GtkFilePath *path,
834 const char *filename;
836 const char *mime_type;
838 filename = gtk_file_path_get_string (path);
839 icon_type = get_icon_type (filename, error);
841 /* FIXME: this function should not return NULL without setting the GError; we
842 * should perhaps provide a "never fails" generic stock icon for when all else
846 if (icon_type == ICON_NONE)
849 if (icon_type != ICON_REGULAR)
855 case ICON_BLOCK_DEVICE:
856 name = "gnome-fs-blockdev";
858 case ICON_BROKEN_SYMBOLIC_LINK:
859 name = "gnome-fs-symlink";
861 case ICON_CHARACTER_DEVICE:
862 name = "gnome-fs-chardev";
865 name = "gnome-fs-directory";
867 case ICON_EXECUTABLE:
868 name ="gnome-fs-executable";
871 name = "gnome-fs-fifo";
874 name = "gnome-fs-socket";
877 g_assert_not_reached ();
881 return get_cached_icon (widget, name, pixel_size);
884 mime_type = xdg_mime_get_mime_type_for_file (filename);
887 const char *separator;
891 separator = strchr (mime_type, '/');
895 icon_name = g_string_new ("gnome-mime-");
896 g_string_append_len (icon_name, mime_type, separator - mime_type);
897 g_string_append_c (icon_name, '-');
898 g_string_append (icon_name, separator + 1);
899 pixbuf = get_cached_icon (widget, icon_name->str, pixel_size);
900 g_string_free (icon_name, TRUE);
904 icon_name = g_string_new ("gnome-mime-");
905 g_string_append_len (icon_name, mime_type, separator - mime_type);
906 pixbuf = get_cached_icon (widget, icon_name->str, pixel_size);
907 g_string_free (icon_name, TRUE);
912 return get_cached_icon (widget, "gnome-fs-regular", pixel_size);
916 bookmark_list_free (GSList *list)
920 for (l = list; l; l = l->next)
926 /* Returns whether a URI is a local file:// */
928 is_local_uri (const char *uri)
934 /* This is rather crude, but hey */
935 filename = g_filename_from_uri (uri, &hostname, NULL);
937 result = (filename && !hostname);
946 bookmark_get_filename (gboolean tmp_file)
950 filename = g_build_filename (g_get_home_dir (),
951 tmp_file ? BOOKMARKS_TMP_FILENAME : BOOKMARKS_FILENAME,
953 g_assert (filename != NULL);
958 bookmark_list_read (GSList **bookmarks, GError **error)
962 gboolean result = FALSE;
964 filename = bookmark_get_filename (FALSE);
967 if (g_file_get_contents (filename, &contents, NULL, error))
969 gchar **lines = g_strsplit (contents, "\n", -1);
973 table = g_hash_table_new (g_str_hash, g_str_equal);
975 for (i = 0; lines[i]; i++)
977 if (lines[i][0] && !g_hash_table_lookup (table, lines[i]))
979 *bookmarks = g_slist_prepend (*bookmarks, g_strdup (lines[i]));
980 g_hash_table_insert (table, lines[i], lines[i]);
985 g_hash_table_destroy (table);
988 *bookmarks = g_slist_reverse (*bookmarks);
998 bookmark_list_write (GSList *bookmarks, GError **error)
1002 gboolean result = TRUE;
1007 /* First, write a temporary file */
1009 tmp_filename = bookmark_get_filename (TRUE);
1010 filename = bookmark_get_filename (FALSE);
1012 fd = g_mkstemp (tmp_filename);
1015 saved_errno = errno;
1019 if ((file = fdopen (fd, "w")) != NULL)
1023 for (l = bookmarks; l; l = l->next)
1024 if (fputs (l->data, file) == EOF
1025 || fputs ("\n", file) == EOF)
1027 saved_errno = errno;
1031 if (fclose (file) == EOF)
1033 saved_errno = errno;
1037 if (rename (tmp_filename, filename) == -1)
1039 saved_errno = errno;
1048 saved_errno = errno;
1050 /* fdopen() failed, so we can't do much error checking here anyway */
1057 GTK_FILE_SYSTEM_ERROR,
1058 GTK_FILE_SYSTEM_ERROR_FAILED,
1059 _("Bookmark saving failed (%s)"),
1060 g_strerror (saved_errno));
1064 unlink (tmp_filename); /* again, not much error checking we can do here */
1069 g_free (tmp_filename);
1075 gtk_file_system_unix_insert_bookmark (GtkFileSystem *file_system,
1076 const GtkFilePath *path,
1088 if (!bookmark_list_read (&bookmarks, &err) && err->code != G_FILE_ERROR_NOENT)
1090 g_propagate_error (error, err);
1094 num_bookmarks = g_slist_length (bookmarks);
1095 g_return_val_if_fail (position >= -1 && position <= num_bookmarks, FALSE);
1099 uri = gtk_file_system_unix_path_to_uri (file_system, path);
1101 for (l = bookmarks; l; l = l->next)
1103 const char *bookmark;
1106 if (strcmp (bookmark, uri) == 0)
1109 GTK_FILE_SYSTEM_ERROR,
1110 GTK_FILE_SYSTEM_ERROR_ALREADY_EXISTS,
1111 "%s already exists in the bookmarks list",
1117 bookmarks = g_slist_insert (bookmarks, g_strdup (uri), position);
1118 if (bookmark_list_write (bookmarks, error))
1121 g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
1127 bookmark_list_free (bookmarks);
1133 gtk_file_system_unix_remove_bookmark (GtkFileSystem *file_system,
1134 const GtkFilePath *path,
1142 if (!bookmark_list_read (&bookmarks, error))
1147 uri = gtk_file_system_path_to_uri (file_system, path);
1149 for (l = bookmarks; l; l = l->next)
1151 const char *bookmark;
1154 if (strcmp (bookmark, uri) == 0)
1157 bookmarks = g_slist_remove_link (bookmarks, l);
1160 if (bookmark_list_write (bookmarks, error))
1163 g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
1171 GTK_FILE_SYSTEM_ERROR,
1172 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
1173 "%s does not exist in the bookmarks list",
1179 bookmark_list_free (bookmarks);
1185 gtk_file_system_unix_list_bookmarks (GtkFileSystem *file_system)
1191 if (!bookmark_list_read (&bookmarks, NULL))
1196 for (l = bookmarks; l; l = l->next)
1202 if (is_local_uri (name))
1203 result = g_slist_prepend (result, gtk_file_system_unix_uri_to_path (file_system, name));
1206 bookmark_list_free (bookmarks);
1208 result = g_slist_reverse (result);
1216 gtk_file_folder_unix_get_type (void)
1218 static GType file_folder_unix_type = 0;
1220 if (!file_folder_unix_type)
1222 static const GTypeInfo file_folder_unix_info =
1224 sizeof (GtkFileFolderUnixClass),
1225 NULL, /* base_init */
1226 NULL, /* base_finalize */
1227 (GClassInitFunc) gtk_file_folder_unix_class_init,
1228 NULL, /* class_finalize */
1229 NULL, /* class_data */
1230 sizeof (GtkFileFolderUnix),
1231 0, /* n_preallocs */
1232 (GInstanceInitFunc) gtk_file_folder_unix_init,
1235 static const GInterfaceInfo file_folder_info =
1237 (GInterfaceInitFunc) gtk_file_folder_unix_iface_init, /* interface_init */
1238 NULL, /* interface_finalize */
1239 NULL /* interface_data */
1242 file_folder_unix_type = g_type_register_static (G_TYPE_OBJECT,
1243 "GtkFileFolderUnix",
1244 &file_folder_unix_info, 0);
1245 g_type_add_interface_static (file_folder_unix_type,
1246 GTK_TYPE_FILE_FOLDER,
1250 return file_folder_unix_type;
1254 gtk_file_folder_unix_class_init (GtkFileFolderUnixClass *class)
1256 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1258 folder_parent_class = g_type_class_peek_parent (class);
1260 gobject_class->finalize = gtk_file_folder_unix_finalize;
1264 gtk_file_folder_unix_iface_init (GtkFileFolderIface *iface)
1266 iface->get_info = gtk_file_folder_unix_get_info;
1267 iface->list_children = gtk_file_folder_unix_list_children;
1271 gtk_file_folder_unix_init (GtkFileFolderUnix *impl)
1276 gtk_file_folder_unix_finalize (GObject *object)
1278 GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (object);
1280 g_hash_table_remove (folder_unix->system_unix->folder_hash, folder_unix->filename);
1282 g_free (folder_unix->filename);
1284 folder_parent_class->finalize (object);
1287 static GtkFileInfo *
1288 gtk_file_folder_unix_get_info (GtkFileFolder *folder,
1289 const GtkFilePath *path,
1292 GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (folder);
1295 const char *filename;
1297 filename = gtk_file_path_get_string (path);
1298 g_return_val_if_fail (filename != NULL, NULL);
1299 g_return_val_if_fail (g_path_is_absolute (filename), NULL);
1301 dirname = g_path_get_dirname (filename);
1302 g_return_val_if_fail (strcmp (dirname, folder_unix->filename) == 0, NULL);
1305 info = filename_get_info (filename, folder_unix->types, error);
1311 gtk_file_folder_unix_list_children (GtkFileFolder *folder,
1315 GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (folder);
1316 GError *tmp_error = NULL;
1321 dir = g_dir_open (folder_unix->filename, 0, &tmp_error);
1325 GTK_FILE_SYSTEM_ERROR,
1326 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
1328 tmp_error->message);
1330 g_error_free (tmp_error);
1337 const gchar *filename = g_dir_read_name (dir);
1343 fullname = g_build_filename (folder_unix->filename, filename, NULL);
1344 *children = g_slist_prepend (*children, filename_to_path (fullname));
1350 *children = g_slist_reverse (*children);
1355 static GtkFileInfo *
1356 filename_get_info (const gchar *filename,
1357 GtkFileInfoType types,
1361 struct stat statbuf;
1362 gboolean do_stat = (types & (GTK_FILE_INFO_IS_FOLDER |
1363 GTK_FILE_INFO_IS_HIDDEN |
1364 GTK_FILE_INFO_MODIFICATION_TIME |
1365 GTK_FILE_INFO_SIZE));
1367 /* If stat fails, try to fall back to lstat to catch broken links
1370 stat (filename, &statbuf) != 0 &&
1371 lstat (filename, &statbuf) != 0)
1373 gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
1375 GTK_FILE_SYSTEM_ERROR,
1376 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
1377 _("error getting information for '%s': %s"),
1378 filename_utf8 ? filename_utf8 : "???",
1379 g_strerror (errno));
1380 g_free (filename_utf8);
1385 info = gtk_file_info_new ();
1387 if (filename_is_root (filename))
1389 if (types & GTK_FILE_INFO_DISPLAY_NAME)
1390 gtk_file_info_set_display_name (info, "/");
1392 if (types & GTK_FILE_INFO_IS_HIDDEN)
1393 gtk_file_info_set_is_hidden (info, FALSE);
1397 gchar *basename = g_path_get_basename (filename);
1399 if (types & GTK_FILE_INFO_DISPLAY_NAME)
1401 gchar *display_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1403 display_name = g_strescape (basename, NULL);
1405 gtk_file_info_set_display_name (info, display_name);
1407 g_free (display_name);
1410 if (types & GTK_FILE_INFO_IS_HIDDEN)
1412 gtk_file_info_set_is_hidden (info, basename[0] == '.');
1418 if (types & GTK_FILE_INFO_IS_FOLDER)
1420 gtk_file_info_set_is_folder (info, S_ISDIR (statbuf.st_mode));
1423 if (types & GTK_FILE_INFO_MIME_TYPE)
1425 const char *mime_type = xdg_mime_get_mime_type_for_file (filename);
1426 gtk_file_info_set_mime_type (info, mime_type);
1429 if (types & GTK_FILE_INFO_MODIFICATION_TIME)
1431 gtk_file_info_set_modification_time (info, statbuf.st_mtime);
1434 if (types & GTK_FILE_INFO_SIZE)
1436 gtk_file_info_set_size (info, (gint64)statbuf.st_size);
1442 static GtkFilePath *
1443 filename_to_path (const char *filename)
1445 return gtk_file_path_new_dup (filename);
1449 filename_is_root (const char *filename)
1451 const gchar *after_root;
1453 after_root = g_path_skip_root (filename);
1455 return (after_root != NULL && *after_root == '\0');