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.
21 /* #define this if you want the program to crash when a file system gets
22 * finalized while async handles are still outstanding.
24 #undef HANDLE_ME_HARDER
28 #include "gtkfilesystem.h"
29 #include "gtkfilesystemunix.h"
30 #include "gtkicontheme.h"
35 #define XDG_PREFIX _gtk_xdg
36 #include "xdgmime/xdgmime.h"
41 #include <sys/types.h>
49 #define BOOKMARKS_FILENAME ".gtk-bookmarks"
51 #define HIDDEN_FILENAME ".hidden"
53 #define FOLDER_CACHE_LIFETIME 2 /* seconds */
55 typedef struct _GtkFileSystemUnixClass GtkFileSystemUnixClass;
57 #define GTK_FILE_SYSTEM_UNIX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_UNIX, GtkFileSystemUnixClass))
58 #define GTK_IS_FILE_SYSTEM_UNIX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_UNIX))
59 #define GTK_FILE_SYSTEM_UNIX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_UNIX, GtkFileSystemUnixClass))
61 struct _GtkFileSystemUnixClass
63 GObjectClass parent_class;
66 struct _GtkFileSystemUnix
68 GObject parent_instance;
70 GHashTable *folder_hash;
72 /* For /afs and /net */
73 struct stat afs_statbuf;
74 struct stat net_statbuf;
78 guint execute_callbacks_idle_id;
85 /* Icon type, supplemented by MIME type
88 ICON_UNDECIDED, /* Only used while we have not yet computed the icon in a struct stat_info_entry */
89 ICON_NONE, /* "Could not compute the icon type" */
90 ICON_REGULAR, /* Use mime type for icon */
92 ICON_BROKEN_SYMBOLIC_LINK,
93 ICON_CHARACTER_DEVICE,
101 #define GTK_TYPE_FILE_FOLDER_UNIX (_gtk_file_folder_unix_get_type ())
102 #define GTK_FILE_FOLDER_UNIX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnix))
103 #define GTK_IS_FILE_FOLDER_UNIX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_FOLDER_UNIX))
104 #define GTK_FILE_FOLDER_UNIX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnixClass))
105 #define GTK_IS_FILE_FOLDER_UNIX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_FOLDER_UNIX))
106 #define GTK_FILE_FOLDER_UNIX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnixClass))
108 typedef struct _GtkFileFolderUnix GtkFileFolderUnix;
109 typedef struct _GtkFileFolderUnixClass GtkFileFolderUnixClass;
111 struct _GtkFileFolderUnixClass
113 GObjectClass parent_class;
116 struct _GtkFileFolderUnix
118 GObject parent_instance;
120 GtkFileSystemUnix *system_unix;
121 GtkFileInfoType types;
123 GHashTable *stat_info;
124 guint load_folder_id;
126 guint have_mime_type : 1;
127 guint is_network_dir : 1;
128 guint have_hidden : 1;
129 guint is_finished_loading : 1;
133 struct stat_info_entry {
140 static const GtkFileInfoType STAT_NEEDED_MASK = (GTK_FILE_INFO_IS_FOLDER |
141 GTK_FILE_INFO_MODIFICATION_TIME |
145 static void gtk_file_system_unix_iface_init (GtkFileSystemIface *iface);
146 static void gtk_file_system_unix_dispose (GObject *object);
147 static void gtk_file_system_unix_finalize (GObject *object);
149 static GSList * gtk_file_system_unix_list_volumes (GtkFileSystem *file_system);
150 static GtkFileSystemVolume *gtk_file_system_unix_get_volume_for_path (GtkFileSystem *file_system,
151 const GtkFilePath *path);
153 static GtkFileSystemHandle *gtk_file_system_unix_get_folder (GtkFileSystem *file_system,
154 const GtkFilePath *path,
155 GtkFileInfoType types,
156 GtkFileSystemGetFolderCallback callback,
158 static GtkFileSystemHandle *gtk_file_system_unix_get_info (GtkFileSystem *file_system,
159 const GtkFilePath *path,
160 GtkFileInfoType types,
161 GtkFileSystemGetInfoCallback callback,
163 static GtkFileSystemHandle *gtk_file_system_unix_create_folder (GtkFileSystem *file_system,
164 const GtkFilePath *path,
165 GtkFileSystemCreateFolderCallback callback,
167 static void gtk_file_system_unix_cancel_operation (GtkFileSystemHandle *handle);
169 static void gtk_file_system_unix_volume_free (GtkFileSystem *file_system,
170 GtkFileSystemVolume *volume);
171 static GtkFilePath *gtk_file_system_unix_volume_get_base_path (GtkFileSystem *file_system,
172 GtkFileSystemVolume *volume);
173 static gboolean gtk_file_system_unix_volume_get_is_mounted (GtkFileSystem *file_system,
174 GtkFileSystemVolume *volume);
175 static GtkFileSystemHandle *gtk_file_system_unix_volume_mount (GtkFileSystem *file_system,
176 GtkFileSystemVolume *volume,
177 GtkFileSystemVolumeMountCallback callback,
179 static gchar * gtk_file_system_unix_volume_get_display_name (GtkFileSystem *file_system,
180 GtkFileSystemVolume *volume);
181 static gchar * gtk_file_system_unix_volume_get_icon_name (GtkFileSystem *file_system,
182 GtkFileSystemVolume *volume,
185 static gboolean gtk_file_system_unix_get_parent (GtkFileSystem *file_system,
186 const GtkFilePath *path,
187 GtkFilePath **parent,
189 static GtkFilePath * gtk_file_system_unix_make_path (GtkFileSystem *file_system,
190 const GtkFilePath *base_path,
191 const gchar *display_name,
193 static gboolean gtk_file_system_unix_parse (GtkFileSystem *file_system,
194 const GtkFilePath *base_path,
196 GtkFilePath **folder,
200 static gchar * gtk_file_system_unix_path_to_uri (GtkFileSystem *file_system,
201 const GtkFilePath *path);
202 static gchar * gtk_file_system_unix_path_to_filename (GtkFileSystem *file_system,
203 const GtkFilePath *path);
204 static GtkFilePath *gtk_file_system_unix_uri_to_path (GtkFileSystem *file_system,
206 static GtkFilePath *gtk_file_system_unix_filename_to_path (GtkFileSystem *file_system,
207 const gchar *filename);
210 static gboolean gtk_file_system_unix_insert_bookmark (GtkFileSystem *file_system,
211 const GtkFilePath *path,
214 static gboolean gtk_file_system_unix_remove_bookmark (GtkFileSystem *file_system,
215 const GtkFilePath *path,
217 static GSList * gtk_file_system_unix_list_bookmarks (GtkFileSystem *file_system);
218 static gchar * gtk_file_system_unix_get_bookmark_label (GtkFileSystem *file_system,
219 const GtkFilePath *path);
220 static void gtk_file_system_unix_set_bookmark_label (GtkFileSystem *file_system,
221 const GtkFilePath *path,
224 static void gtk_file_folder_unix_iface_init (GtkFileFolderIface *iface);
225 static void gtk_file_folder_unix_finalize (GObject *object);
227 static GtkFileInfo *gtk_file_folder_unix_get_info (GtkFileFolder *folder,
228 const GtkFilePath *path,
230 static gboolean gtk_file_folder_unix_list_children (GtkFileFolder *folder,
234 static gboolean gtk_file_folder_unix_is_finished_loading (GtkFileFolder *folder);
236 static GtkFilePath *filename_to_path (const gchar *filename);
238 static gboolean filename_is_root (const char *filename);
240 static gboolean get_is_hidden_for_file (const char *filename,
241 const char *basename);
242 static gboolean file_is_hidden (GtkFileFolderUnix *folder_unix,
243 const char *basename);
245 static GtkFileInfo *file_info_for_root_with_error (const char *root_name,
247 static gboolean stat_with_error (const char *filename,
248 struct stat *statbuf,
250 static GtkFileInfo *create_file_info (GtkFileFolderUnix *folder_unix,
251 const char *filename,
252 const char *basename,
253 GtkFileInfoType types,
254 struct stat *statbuf,
255 const char *mime_type);
257 static gboolean execute_callbacks_idle (gpointer data);
259 static gboolean fill_in_names (GtkFileFolderUnix *folder_unix,
261 static void fill_in_stats (GtkFileFolderUnix *folder_unix);
262 static void fill_in_mime_type (GtkFileFolderUnix *folder_unix);
263 static void fill_in_hidden (GtkFileFolderUnix *folder_unix);
265 static gboolean cb_fill_in_stats (gpointer key,
268 static gboolean cb_fill_in_mime_type (gpointer key,
272 static char * get_parent_dir (const char *filename);
277 G_DEFINE_TYPE_WITH_CODE (GtkFileSystemUnix, gtk_file_system_unix, G_TYPE_OBJECT,
278 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_SYSTEM,
279 gtk_file_system_unix_iface_init))
284 G_DEFINE_TYPE_WITH_CODE (GtkFileFolderUnix, _gtk_file_folder_unix, G_TYPE_OBJECT,
285 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_FOLDER,
286 gtk_file_folder_unix_iface_init))
290 * gtk_file_system_unix_new:
292 * Creates a new #GtkFileSystemUnix object. #GtkFileSystemUnix
293 * implements the #GtkFileSystem interface with direct access to
294 * the filesystem using Unix/Linux API calls
296 * Return value: the new #GtkFileSystemUnix object
299 gtk_file_system_unix_new (void)
301 return g_object_new (GTK_TYPE_FILE_SYSTEM_UNIX, NULL);
305 gtk_file_system_unix_class_init (GtkFileSystemUnixClass *class)
307 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
309 gobject_class->dispose = gtk_file_system_unix_dispose;
310 gobject_class->finalize = gtk_file_system_unix_finalize;
314 gtk_file_system_unix_iface_init (GtkFileSystemIface *iface)
316 iface->list_volumes = gtk_file_system_unix_list_volumes;
317 iface->get_volume_for_path = gtk_file_system_unix_get_volume_for_path;
318 iface->get_folder = gtk_file_system_unix_get_folder;
319 iface->get_info = gtk_file_system_unix_get_info;
320 iface->create_folder = gtk_file_system_unix_create_folder;
321 iface->cancel_operation = gtk_file_system_unix_cancel_operation;
322 iface->volume_free = gtk_file_system_unix_volume_free;
323 iface->volume_get_base_path = gtk_file_system_unix_volume_get_base_path;
324 iface->volume_get_is_mounted = gtk_file_system_unix_volume_get_is_mounted;
325 iface->volume_mount = gtk_file_system_unix_volume_mount;
326 iface->volume_get_display_name = gtk_file_system_unix_volume_get_display_name;
327 iface->volume_get_icon_name = gtk_file_system_unix_volume_get_icon_name;
328 iface->get_parent = gtk_file_system_unix_get_parent;
329 iface->make_path = gtk_file_system_unix_make_path;
330 iface->parse = gtk_file_system_unix_parse;
331 iface->path_to_uri = gtk_file_system_unix_path_to_uri;
332 iface->path_to_filename = gtk_file_system_unix_path_to_filename;
333 iface->uri_to_path = gtk_file_system_unix_uri_to_path;
334 iface->filename_to_path = gtk_file_system_unix_filename_to_path;
335 iface->insert_bookmark = gtk_file_system_unix_insert_bookmark;
336 iface->remove_bookmark = gtk_file_system_unix_remove_bookmark;
337 iface->list_bookmarks = gtk_file_system_unix_list_bookmarks;
338 iface->get_bookmark_label = gtk_file_system_unix_get_bookmark_label;
339 iface->set_bookmark_label = gtk_file_system_unix_set_bookmark_label;
343 gtk_file_system_unix_init (GtkFileSystemUnix *system_unix)
345 system_unix->folder_hash = g_hash_table_new (g_str_hash, g_str_equal);
347 if (stat ("/afs", &system_unix->afs_statbuf) == 0)
348 system_unix->have_afs = TRUE;
350 system_unix->have_afs = FALSE;
352 if (stat ("/net", &system_unix->net_statbuf) == 0)
353 system_unix->have_net = TRUE;
355 system_unix->have_net = FALSE;
357 system_unix->handles = g_hash_table_new (g_direct_hash, g_direct_equal);
359 system_unix->execute_callbacks_idle_id = 0;
360 system_unix->callbacks = NULL;
364 check_handle_fn (gpointer key, gpointer value, gpointer data)
366 GtkFileSystemHandle *handle;
367 int *num_live_handles;
370 num_live_handles = data;
372 (*num_live_handles)++;
374 g_warning ("file_system_unix=%p still has handle=%p at finalization which is %s!",
377 handle->cancelled ? "CANCELLED" : "NOT CANCELLED");
381 check_handles_at_finalization (GtkFileSystemUnix *system_unix)
383 int num_live_handles;
385 num_live_handles = 0;
387 g_hash_table_foreach (system_unix->handles, check_handle_fn, &num_live_handles);
388 #ifdef HANDLE_ME_HARDER
389 g_assert (num_live_handles == 0);
392 g_hash_table_destroy (system_unix->handles);
393 system_unix->handles = NULL;
396 #define GTK_TYPE_FILE_SYSTEM_HANDLE_UNIX (_gtk_file_system_handle_unix_get_type ())
398 typedef struct _GtkFileSystemHandle GtkFileSystemHandleUnix;
399 typedef struct _GtkFileSystemHandleClass GtkFileSystemHandleUnixClass;
401 G_DEFINE_TYPE (GtkFileSystemHandleUnix, _gtk_file_system_handle_unix, GTK_TYPE_FILE_SYSTEM_HANDLE)
404 _gtk_file_system_handle_unix_init (GtkFileSystemHandleUnix *handle)
409 _gtk_file_system_handle_unix_finalize (GObject *object)
411 GtkFileSystemHandleUnix *handle;
412 GtkFileSystemUnix *system_unix;
414 handle = (GtkFileSystemHandleUnix *)object;
416 system_unix = GTK_FILE_SYSTEM_UNIX (GTK_FILE_SYSTEM_HANDLE (handle)->file_system);
418 g_assert (g_hash_table_lookup (system_unix->handles, handle) != NULL);
419 g_hash_table_remove (system_unix->handles, handle);
421 if (G_OBJECT_CLASS (_gtk_file_system_handle_unix_parent_class)->finalize)
422 G_OBJECT_CLASS (_gtk_file_system_handle_unix_parent_class)->finalize (object);
426 _gtk_file_system_handle_unix_class_init (GtkFileSystemHandleUnixClass *class)
428 GObjectClass *gobject_class = (GObjectClass *) class;
430 gobject_class->finalize = _gtk_file_system_handle_unix_finalize;
434 gtk_file_system_unix_dispose (GObject *object)
436 GtkFileSystemUnix *system_unix;
438 system_unix = GTK_FILE_SYSTEM_UNIX (object);
440 if (system_unix->execute_callbacks_idle_id)
442 g_source_remove (system_unix->execute_callbacks_idle_id);
443 system_unix->execute_callbacks_idle_id = 0;
445 /* call pending callbacks */
446 execute_callbacks_idle (system_unix);
449 G_OBJECT_CLASS (gtk_file_system_unix_parent_class)->dispose (object);
453 gtk_file_system_unix_finalize (GObject *object)
455 GtkFileSystemUnix *system_unix;
457 system_unix = GTK_FILE_SYSTEM_UNIX (object);
459 check_handles_at_finalization (system_unix);
461 /* FIXME: assert that the hash is empty? */
462 g_hash_table_destroy (system_unix->folder_hash);
464 G_OBJECT_CLASS (gtk_file_system_unix_parent_class)->finalize (object);
467 /* Returns our single root volume */
468 static GtkFileSystemVolume *
469 get_root_volume (void)
471 return (GtkFileSystemVolume *) gtk_file_path_new_dup ("/");
475 gtk_file_system_unix_list_volumes (GtkFileSystem *file_system)
477 return g_slist_append (NULL, get_root_volume ());
480 static GtkFileSystemVolume *
481 gtk_file_system_unix_get_volume_for_path (GtkFileSystem *file_system,
482 const GtkFilePath *path)
484 return get_root_volume ();
488 remove_trailing_slash (const char *filename)
492 len = strlen (filename);
494 if (len > 1 && filename[len - 1] == '/')
495 return g_strndup (filename, len - 1);
497 return g_memdup (filename, len + 1);
500 /* Delay callback dispatching
507 CALLBACK_CREATE_FOLDER,
508 CALLBACK_VOLUME_MOUNT
511 static void queue_callback (GtkFileSystemUnix *system_unix, enum callback_types type, gpointer data);
513 struct get_info_callback
515 GtkFileSystemGetInfoCallback callback;
516 GtkFileSystemHandle *handle;
517 GtkFileInfo *file_info;
523 dispatch_get_info_callback (struct get_info_callback *info)
525 (* info->callback) (info->handle, info->file_info, info->error, info->data);
528 gtk_file_info_free (info->file_info);
531 g_error_free (info->error);
533 g_object_unref (info->handle);
539 queue_get_info_callback (GtkFileSystemGetInfoCallback callback,
540 GtkFileSystemHandle *handle,
541 GtkFileInfo *file_info,
545 struct get_info_callback *info;
547 info = g_new (struct get_info_callback, 1);
548 info->callback = callback;
549 info->handle = handle;
550 info->file_info = file_info;
554 queue_callback (GTK_FILE_SYSTEM_UNIX (handle->file_system), CALLBACK_GET_INFO, info);
558 struct get_folder_callback
560 GtkFileSystemGetFolderCallback callback;
561 GtkFileSystemHandle *handle;
562 GtkFileFolder *folder;
568 dispatch_get_folder_callback (struct get_folder_callback *info)
570 (* info->callback) (info->handle, info->folder, info->error, info->data);
573 g_error_free (info->error);
575 g_object_unref (info->handle);
581 queue_get_folder_callback (GtkFileSystemGetFolderCallback callback,
582 GtkFileSystemHandle *handle,
583 GtkFileFolder *folder,
587 struct get_folder_callback *info;
589 info = g_new (struct get_folder_callback, 1);
590 info->callback = callback;
591 info->handle = handle;
592 info->folder = folder;
596 queue_callback (GTK_FILE_SYSTEM_UNIX (handle->file_system), CALLBACK_GET_FOLDER, info);
600 struct create_folder_callback
602 GtkFileSystemCreateFolderCallback callback;
603 GtkFileSystemHandle *handle;
610 dispatch_create_folder_callback (struct create_folder_callback *info)
612 (* info->callback) (info->handle, info->path, info->error, info->data);
615 g_error_free (info->error);
618 gtk_file_path_free (info->path);
620 g_object_unref (info->handle);
626 queue_create_folder_callback (GtkFileSystemCreateFolderCallback callback,
627 GtkFileSystemHandle *handle,
628 const GtkFilePath *path,
632 struct create_folder_callback *info;
634 info = g_new (struct create_folder_callback, 1);
635 info->callback = callback;
636 info->handle = handle;
637 info->path = gtk_file_path_copy (path);
641 queue_callback (GTK_FILE_SYSTEM_UNIX (handle->file_system), CALLBACK_CREATE_FOLDER, info);
645 struct volume_mount_callback
647 GtkFileSystemVolumeMountCallback callback;
648 GtkFileSystemHandle *handle;
649 GtkFileSystemVolume *volume;
655 dispatch_volume_mount_callback (struct volume_mount_callback *info)
657 (* info->callback) (info->handle, info->volume, info->error, info->data);
660 g_error_free (info->error);
662 g_object_unref (info->handle);
668 queue_volume_mount_callback (GtkFileSystemVolumeMountCallback callback,
669 GtkFileSystemHandle *handle,
670 GtkFileSystemVolume *volume,
674 struct volume_mount_callback *info;
676 info = g_new (struct volume_mount_callback, 1);
677 info->callback = callback;
678 info->handle = handle;
679 info->volume = volume;
683 queue_callback (GTK_FILE_SYSTEM_UNIX (handle->file_system), CALLBACK_VOLUME_MOUNT, info);
689 enum callback_types type;
693 struct get_info_callback *get_info;
694 struct get_folder_callback *get_folder;
695 struct create_folder_callback *create_folder;
696 struct volume_mount_callback *volume_mount;
703 execute_callbacks_idle (gpointer data)
706 gboolean unref_file_system = TRUE;
707 GtkFileSystemUnix *system_unix = GTK_FILE_SYSTEM_UNIX (data);
709 GDK_THREADS_ENTER ();
711 if (!system_unix->execute_callbacks_idle_id)
712 unref_file_system = FALSE;
714 g_object_ref (system_unix);
716 for (l = system_unix->callbacks; l; l = l->next)
718 struct callback_info *info = l->data;
722 case CALLBACK_GET_INFO:
723 dispatch_get_info_callback (info->info.get_info);
726 case CALLBACK_GET_FOLDER:
727 dispatch_get_folder_callback (info->info.get_folder);
730 case CALLBACK_CREATE_FOLDER:
731 dispatch_create_folder_callback (info->info.create_folder);
734 case CALLBACK_VOLUME_MOUNT:
735 dispatch_volume_mount_callback (info->info.volume_mount);
742 g_slist_free (system_unix->callbacks);
743 system_unix->callbacks = NULL;
745 if (unref_file_system)
746 g_object_unref (system_unix);
748 system_unix->execute_callbacks_idle_id = 0;
750 GDK_THREADS_LEAVE ();
756 queue_callback (GtkFileSystemUnix *system_unix,
757 enum callback_types type,
760 struct callback_info *info;
762 info = g_new (struct callback_info, 1);
767 case CALLBACK_GET_INFO:
768 info->info.get_info = data;
771 case CALLBACK_GET_FOLDER:
772 info->info.get_folder = data;
775 case CALLBACK_CREATE_FOLDER:
776 info->info.create_folder = data;
779 case CALLBACK_VOLUME_MOUNT:
780 info->info.volume_mount = data;
784 system_unix->callbacks = g_slist_append (system_unix->callbacks, info);
786 if (!system_unix->execute_callbacks_idle_id)
787 system_unix->execute_callbacks_idle_id = g_idle_add (execute_callbacks_idle, system_unix);
790 static GtkFileSystemHandle *
791 create_handle (GtkFileSystem *file_system)
793 GtkFileSystemUnix *system_unix;
794 GtkFileSystemHandle *handle;
796 system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
798 handle = g_object_new (GTK_TYPE_FILE_SYSTEM_HANDLE_UNIX, NULL);
799 handle->file_system = file_system;
801 g_assert (g_hash_table_lookup (system_unix->handles, handle) == NULL);
802 g_hash_table_insert (system_unix->handles, handle, handle);
809 static GtkFileSystemHandle *
810 gtk_file_system_unix_get_info (GtkFileSystem *file_system,
811 const GtkFilePath *path,
812 GtkFileInfoType types,
813 GtkFileSystemGetInfoCallback callback,
816 GError *error = NULL;
817 GtkFileSystemUnix *system_unix;
818 GtkFileSystemHandle *handle;
819 const char *filename;
823 const char *mime_type;
825 system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
826 handle = create_handle (file_system);
828 filename = gtk_file_path_get_string (path);
829 g_return_val_if_fail (filename != NULL, FALSE);
830 g_return_val_if_fail (g_path_is_absolute (filename), FALSE);
832 if (!stat_with_error (filename, &statbuf, &error))
834 g_object_ref (handle);
835 queue_get_info_callback (callback, handle, NULL, error, data);
839 if ((types & GTK_FILE_INFO_MIME_TYPE) != 0)
840 mime_type = xdg_mime_get_mime_type_for_file (filename, &statbuf);
844 basename = g_path_get_basename (filename);
846 info = create_file_info (NULL, filename, basename, types, &statbuf,
849 g_object_ref (handle);
850 queue_get_info_callback (callback, handle, info, NULL, data);
856 load_folder (gpointer data)
858 GtkFileFolderUnix *folder_unix = data;
861 GDK_THREADS_ENTER ();
863 if ((folder_unix->types & STAT_NEEDED_MASK) != 0)
864 fill_in_stats (folder_unix);
866 if ((folder_unix->types & GTK_FILE_INFO_MIME_TYPE) != 0)
867 fill_in_mime_type (folder_unix);
869 if (gtk_file_folder_unix_list_children (GTK_FILE_FOLDER (folder_unix), &children, NULL))
871 folder_unix->is_finished_loading = TRUE;
872 g_signal_emit_by_name (folder_unix, "files-added", children);
873 gtk_file_paths_free (children);
876 folder_unix->load_folder_id = 0;
878 g_signal_emit_by_name (folder_unix, "finished-loading", 0);
880 GDK_THREADS_LEAVE ();
885 static GtkFileSystemHandle *
886 gtk_file_system_unix_get_folder (GtkFileSystem *file_system,
887 const GtkFilePath *path,
888 GtkFileInfoType types,
889 GtkFileSystemGetFolderCallback callback,
892 GError *error = NULL;
893 GtkFileSystemUnix *system_unix;
894 GtkFileFolderUnix *folder_unix;
895 GtkFileSystemHandle *handle;
896 const char *filename;
898 gboolean set_asof = FALSE;
900 system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
902 filename = gtk_file_path_get_string (path);
903 g_return_val_if_fail (filename != NULL, NULL);
904 g_return_val_if_fail (g_path_is_absolute (filename), NULL);
906 handle = create_handle (file_system);
908 filename_copy = remove_trailing_slash (filename);
909 folder_unix = g_hash_table_lookup (system_unix->folder_hash, filename_copy);
913 g_free (filename_copy);
914 if (folder_unix->stat_info &&
915 time (NULL) - folder_unix->asof >= FOLDER_CACHE_LIFETIME)
918 g_print ("Cleaning out cached directory %s\n", filename);
920 g_hash_table_destroy (folder_unix->stat_info);
921 folder_unix->stat_info = NULL;
922 folder_unix->have_mime_type = FALSE;
923 folder_unix->have_stat = FALSE;
924 folder_unix->have_hidden = FALSE;
928 g_object_ref (folder_unix);
929 folder_unix->types |= types;
930 types = folder_unix->types;
939 code = my_errno = 0; /* shut up GCC */
941 result = stat (filename, &statbuf);
945 if (!S_ISDIR (statbuf.st_mode))
948 code = GTK_FILE_SYSTEM_ERROR_NOT_FOLDER;
956 if (my_errno == ENOENT)
957 code = GTK_FILE_SYSTEM_ERROR_NONEXISTENT;
959 code = GTK_FILE_SYSTEM_ERROR_FAILED;
964 gchar *display_name = g_filename_display_name (filename);
966 GTK_FILE_SYSTEM_ERROR,
968 _("Error getting information for '%s': %s"),
970 g_strerror (my_errno));
972 g_object_ref (handle);
973 queue_get_folder_callback (callback, handle, NULL, error, data);
975 g_free (display_name);
976 g_free (filename_copy);
980 folder_unix = g_object_new (GTK_TYPE_FILE_FOLDER_UNIX, NULL);
981 folder_unix->system_unix = system_unix;
982 folder_unix->filename = filename_copy;
983 folder_unix->types = types;
984 folder_unix->stat_info = NULL;
985 folder_unix->load_folder_id = 0;
986 folder_unix->have_mime_type = FALSE;
987 folder_unix->have_stat = FALSE;
988 folder_unix->have_hidden = FALSE;
989 folder_unix->is_finished_loading = FALSE;
992 if ((system_unix->have_afs &&
993 system_unix->afs_statbuf.st_dev == statbuf.st_dev &&
994 system_unix->afs_statbuf.st_ino == statbuf.st_ino) ||
995 (system_unix->have_net &&
996 system_unix->net_statbuf.st_dev == statbuf.st_dev &&
997 system_unix->net_statbuf.st_ino == statbuf.st_ino))
998 folder_unix->is_network_dir = TRUE;
1000 folder_unix->is_network_dir = FALSE;
1002 g_hash_table_insert (system_unix->folder_hash,
1003 folder_unix->filename,
1008 folder_unix->asof = time (NULL);
1010 g_object_ref (handle);
1011 queue_get_folder_callback (callback, handle, GTK_FILE_FOLDER (folder_unix), NULL, data);
1013 /* Start loading the folder contents in an idle */
1014 if (!folder_unix->load_folder_id)
1015 folder_unix->load_folder_id =
1016 g_idle_add ((GSourceFunc) load_folder, folder_unix);
1021 static GtkFileSystemHandle *
1022 gtk_file_system_unix_create_folder (GtkFileSystem *file_system,
1023 const GtkFilePath *path,
1024 GtkFileSystemCreateFolderCallback callback,
1027 GError *error = NULL;
1028 GtkFileSystemUnix *system_unix;
1029 GtkFileSystemHandle *handle;
1030 const char *filename;
1033 int save_errno = errno;
1035 system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
1037 filename = gtk_file_path_get_string (path);
1038 g_return_val_if_fail (filename != NULL, FALSE);
1039 g_return_val_if_fail (g_path_is_absolute (filename), FALSE);
1041 handle = create_handle (file_system);
1043 tmp = remove_trailing_slash (filename);
1045 result = mkdir (tmp, 0777) == 0;
1051 gchar *display_name = g_filename_display_name (filename);
1052 g_set_error (&error,
1053 GTK_FILE_SYSTEM_ERROR,
1054 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
1055 _("Error creating directory '%s': %s"),
1057 g_strerror (save_errno));
1059 g_object_ref (handle);
1060 queue_create_folder_callback (callback, handle, path, error, data);
1062 g_free (display_name);
1066 g_object_ref (handle);
1067 queue_create_folder_callback (callback, handle, path, NULL, data);
1069 parent = get_parent_dir (filename);
1072 GtkFileFolderUnix *folder_unix;
1074 folder_unix = g_hash_table_lookup (system_unix->folder_hash, parent);
1079 struct stat_info_entry *entry;
1081 /* Make sure the new folder exists in the parent's folder */
1082 entry = g_new0 (struct stat_info_entry, 1);
1083 if (folder_unix->is_network_dir)
1085 entry->statbuf.st_mode = S_IFDIR;
1086 entry->mime_type = g_strdup ("x-directory/normal");
1089 basename = g_path_get_basename (filename);
1090 g_hash_table_insert (folder_unix->stat_info,
1094 if (folder_unix->have_stat)
1097 if ((folder_unix->types & STAT_NEEDED_MASK) != 0)
1098 cb_fill_in_stats (basename, entry, folder_unix);
1100 if ((folder_unix->types & GTK_FILE_INFO_MIME_TYPE) != 0)
1101 cb_fill_in_mime_type (basename, entry, folder_unix);
1104 paths = g_slist_append (NULL, (GtkFilePath *) path);
1105 g_signal_emit_by_name (folder_unix, "files-added", paths);
1106 g_slist_free (paths);
1116 gtk_file_system_unix_cancel_operation (GtkFileSystemHandle *handle)
1118 /* We don't set "cancelled" to TRUE here, since the actual operation
1119 * is executed in the function itself and not in a callback. So
1120 * the operations can never be cancelled (since they will be already
1121 * completed at this point.
1126 gtk_file_system_unix_volume_free (GtkFileSystem *file_system,
1127 GtkFileSystemVolume *volume)
1131 path = (GtkFilePath *) volume;
1132 gtk_file_path_free (path);
1135 static GtkFilePath *
1136 gtk_file_system_unix_volume_get_base_path (GtkFileSystem *file_system,
1137 GtkFileSystemVolume *volume)
1139 return gtk_file_path_new_dup ("/");
1143 gtk_file_system_unix_volume_get_is_mounted (GtkFileSystem *file_system,
1144 GtkFileSystemVolume *volume)
1149 static GtkFileSystemHandle *
1150 gtk_file_system_unix_volume_mount (GtkFileSystem *file_system,
1151 GtkFileSystemVolume *volume,
1152 GtkFileSystemVolumeMountCallback callback,
1155 GError *error = NULL;
1156 GtkFileSystemHandle *handle = create_handle (file_system);
1158 g_set_error (&error,
1159 GTK_FILE_SYSTEM_ERROR,
1160 GTK_FILE_SYSTEM_ERROR_FAILED,
1161 _("This file system does not support mounting"));
1163 g_object_ref (handle);
1164 queue_volume_mount_callback (callback, handle, volume, error, data);
1170 gtk_file_system_unix_volume_get_display_name (GtkFileSystem *file_system,
1171 GtkFileSystemVolume *volume)
1173 return g_strdup (_("File System")); /* Same as Nautilus */
1177 get_icon_type_from_stat (struct stat *statp)
1179 if (S_ISBLK (statp->st_mode))
1180 return ICON_BLOCK_DEVICE;
1181 else if (S_ISLNK (statp->st_mode))
1182 return ICON_BROKEN_SYMBOLIC_LINK; /* See get_icon_type */
1183 else if (S_ISCHR (statp->st_mode))
1184 return ICON_CHARACTER_DEVICE;
1185 else if (S_ISDIR (statp->st_mode))
1186 return ICON_DIRECTORY;
1188 else if (S_ISFIFO (statp->st_mode))
1192 else if (S_ISSOCK (statp->st_mode))
1196 return ICON_REGULAR;
1200 get_icon_type (const char *filename,
1203 struct stat statbuf;
1205 /* If stat fails, try to fall back to lstat to catch broken links
1207 if (stat (filename, &statbuf) != 0)
1209 if (errno != ENOENT || lstat (filename, &statbuf) != 0)
1211 int save_errno = errno;
1212 gchar *display_name = g_filename_display_name (filename);
1214 GTK_FILE_SYSTEM_ERROR,
1215 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
1216 _("Error getting information for '%s': %s"),
1218 g_strerror (save_errno));
1219 g_free (display_name);
1225 return get_icon_type_from_stat (&statbuf);
1228 /* Renders a fallback icon from the stock system */
1229 static const gchar *
1230 get_fallback_icon_name (IconType icon_type)
1232 const char *stock_name;
1236 case ICON_BLOCK_DEVICE:
1237 stock_name = GTK_STOCK_HARDDISK;
1240 case ICON_DIRECTORY:
1241 stock_name = GTK_STOCK_DIRECTORY;
1244 case ICON_EXECUTABLE:
1245 stock_name = GTK_STOCK_EXECUTE;
1249 stock_name = GTK_STOCK_FILE;
1257 gtk_file_system_unix_volume_get_icon_name (GtkFileSystem *file_system,
1258 GtkFileSystemVolume *volume,
1261 /* FIXME: maybe we just always want to return GTK_STOCK_HARDDISK here?
1262 * or the new tango icon name?
1264 return g_strdup ("gnome-dev-harddisk");
1268 get_parent_dir (const char *filename)
1272 len = strlen (filename);
1274 /* Ignore trailing slashes */
1275 if (len > 1 && filename[len - 1] == '/')
1279 tmp = g_strndup (filename, len - 1);
1281 parent = g_path_get_dirname (tmp);
1287 return g_path_get_dirname (filename);
1291 gtk_file_system_unix_get_parent (GtkFileSystem *file_system,
1292 const GtkFilePath *path,
1293 GtkFilePath **parent,
1296 const char *filename;
1298 filename = gtk_file_path_get_string (path);
1299 g_return_val_if_fail (filename != NULL, FALSE);
1300 g_return_val_if_fail (g_path_is_absolute (filename), FALSE);
1302 if (filename_is_root (filename))
1308 gchar *parent_filename = get_parent_dir (filename);
1309 *parent = filename_to_path (parent_filename);
1310 g_free (parent_filename);
1316 static GtkFilePath *
1317 gtk_file_system_unix_make_path (GtkFileSystem *file_system,
1318 const GtkFilePath *base_path,
1319 const gchar *display_name,
1322 const char *base_filename;
1324 gchar *full_filename;
1325 GError *tmp_error = NULL;
1326 GtkFilePath *result;
1328 base_filename = gtk_file_path_get_string (base_path);
1329 g_return_val_if_fail (base_filename != NULL, NULL);
1330 g_return_val_if_fail (g_path_is_absolute (base_filename), NULL);
1332 if (strchr (display_name, G_DIR_SEPARATOR))
1335 GTK_FILE_SYSTEM_ERROR,
1336 GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
1337 _("The name \"%s\" is not valid because it contains the character \"%s\". "
1338 "Please use a different name."),
1344 filename = g_filename_from_utf8 (display_name, -1, NULL, NULL, &tmp_error);
1348 GTK_FILE_SYSTEM_ERROR,
1349 GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
1351 tmp_error->message);
1353 g_error_free (tmp_error);
1358 full_filename = g_build_filename (base_filename, filename, NULL);
1359 result = filename_to_path (full_filename);
1361 g_free (full_filename);
1366 /* If this was a publically exported function, it should return
1367 * a dup'ed result, but we make it modify-in-place for efficiency
1368 * here, and because it works for us.
1371 canonicalize_filename (gchar *filename)
1374 gboolean last_was_slash = FALSE;
1381 if (*p == G_DIR_SEPARATOR)
1383 if (!last_was_slash)
1384 *q++ = G_DIR_SEPARATOR;
1386 last_was_slash = TRUE;
1390 if (last_was_slash && *p == '.')
1392 if (*(p + 1) == G_DIR_SEPARATOR ||
1395 if (*(p + 1) == '\0')
1400 else if (*(p + 1) == '.' &&
1401 (*(p + 2) == G_DIR_SEPARATOR ||
1404 if (q > filename + 1)
1407 while (q > filename + 1 &&
1408 *(q - 1) != G_DIR_SEPARATOR)
1412 if (*(p + 2) == '\0')
1420 last_was_slash = FALSE;
1426 last_was_slash = FALSE;
1433 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
1439 /* Takes a user-typed filename and expands a tilde at the beginning of the string */
1441 expand_tilde (const char *filename)
1443 const char *notilde;
1447 if (filename[0] != '~')
1448 return g_strdup (filename);
1450 notilde = filename + 1;
1452 slash = strchr (notilde, G_DIR_SEPARATOR);
1456 if (slash == notilde)
1458 home = g_get_home_dir ();
1461 return g_strdup (filename);
1466 struct passwd *passwd;
1468 username = g_strndup (notilde, slash - notilde);
1469 passwd = getpwnam (username);
1473 return g_strdup (filename);
1475 home = passwd->pw_dir;
1478 return g_build_filename (home, G_DIR_SEPARATOR_S, slash + 1, NULL);
1482 gtk_file_system_unix_parse (GtkFileSystem *file_system,
1483 const GtkFilePath *base_path,
1485 GtkFilePath **folder,
1489 const char *base_filename;
1492 gboolean result = FALSE;
1494 base_filename = gtk_file_path_get_string (base_path);
1495 g_return_val_if_fail (base_filename != NULL, FALSE);
1496 g_return_val_if_fail (g_path_is_absolute (base_filename), FALSE);
1498 filename = expand_tilde (str);
1502 GTK_FILE_SYSTEM_ERROR,
1503 GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
1504 "%s", ""); /* nothing for now, as we are string-frozen */
1508 last_slash = strrchr (filename, G_DIR_SEPARATOR);
1511 *folder = gtk_file_path_copy (base_path);
1512 *file_part = g_strdup (filename);
1519 GError *tmp_error = NULL;
1521 if (last_slash == filename)
1522 folder_part = g_strdup ("/");
1524 folder_part = g_filename_from_utf8 (filename, last_slash - filename,
1525 NULL, NULL, &tmp_error);
1530 GTK_FILE_SYSTEM_ERROR,
1531 GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
1533 tmp_error->message);
1534 g_error_free (tmp_error);
1538 if (folder_part[0] == G_DIR_SEPARATOR)
1539 folder_path = folder_part;
1542 folder_path = g_build_filename (base_filename, folder_part, NULL);
1543 g_free (folder_part);
1546 canonicalize_filename (folder_path);
1548 *folder = filename_to_path (folder_path);
1549 *file_part = g_strdup (last_slash + 1);
1551 g_free (folder_path);
1563 gtk_file_system_unix_path_to_uri (GtkFileSystem *file_system,
1564 const GtkFilePath *path)
1566 return g_filename_to_uri (gtk_file_path_get_string (path), NULL, NULL);
1570 gtk_file_system_unix_path_to_filename (GtkFileSystem *file_system,
1571 const GtkFilePath *path)
1573 return g_strdup (gtk_file_path_get_string (path));
1576 static GtkFilePath *
1577 gtk_file_system_unix_uri_to_path (GtkFileSystem *file_system,
1581 gchar *filename = g_filename_from_uri (uri, NULL, NULL);
1585 path = filename_to_path (filename);
1594 static GtkFilePath *
1595 gtk_file_system_unix_filename_to_path (GtkFileSystem *file_system,
1596 const gchar *filename)
1598 return filename_to_path (filename);
1601 /* Returns the name of the icon to be used for a path which is known to be a
1602 * directory. This can vary for Home, Desktop, etc.
1605 get_icon_name_for_directory (const char *path)
1607 static char *desktop_path = NULL;
1609 if (!g_get_home_dir ())
1610 return "gnome-fs-directory";
1613 desktop_path = g_build_filename (g_get_home_dir (), "Desktop", NULL);
1615 if (strcmp (g_get_home_dir (), path) == 0)
1616 return "gnome-fs-home";
1617 else if (strcmp (desktop_path, path) == 0)
1618 return "gnome-fs-desktop";
1620 return "gnome-fs-directory";
1625 /* Computes our internal icon type based on a path name; also returns the MIME
1626 * type in case we come up with ICON_REGULAR.
1629 get_icon_type_from_path (GtkFileFolderUnix *folder_unix,
1630 struct stat *statbuf,
1631 const char *filename,
1632 const char **mime_type)
1638 if (folder_unix && folder_unix->have_stat)
1641 struct stat_info_entry *entry;
1643 g_assert (folder_unix->stat_info != NULL);
1645 basename = g_path_get_basename (filename);
1646 entry = g_hash_table_lookup (folder_unix->stat_info, basename);
1650 if (entry->icon_type == ICON_UNDECIDED)
1652 entry->icon_type = get_icon_type_from_stat (&entry->statbuf);
1653 g_assert (entry->icon_type != ICON_UNDECIDED);
1655 icon_type = entry->icon_type;
1656 if (icon_type == ICON_REGULAR)
1658 fill_in_mime_type (folder_unix);
1659 *mime_type = entry->mime_type;
1667 return get_icon_type_from_stat (statbuf);
1669 icon_type = get_icon_type (filename, NULL);
1670 if (icon_type == ICON_REGULAR)
1671 *mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
1676 /* Renders an icon for a non-ICON_REGULAR file */
1677 static const gchar *
1678 get_special_icon_name (IconType icon_type,
1679 const gchar *filename)
1683 g_assert (icon_type != ICON_REGULAR);
1687 case ICON_BLOCK_DEVICE:
1688 name = "gnome-fs-blockdev";
1690 case ICON_BROKEN_SYMBOLIC_LINK:
1691 name = "gnome-fs-symlink";
1693 case ICON_CHARACTER_DEVICE:
1694 name = "gnome-fs-chardev";
1696 case ICON_DIRECTORY:
1697 /* get_icon_name_for_directory() returns a dupped string */
1698 return get_icon_name_for_directory (filename);
1699 case ICON_EXECUTABLE:
1700 name ="gnome-fs-executable";
1703 name = "gnome-fs-fifo";
1706 name = "gnome-fs-socket";
1709 g_assert_not_reached ();
1717 get_icon_name_for_mime_type (const char *mime_type)
1720 const char *separator;
1726 separator = strchr (mime_type, '/');
1728 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
1730 /* FIXME: we default to the gnome icon naming for now. Some question
1731 * as below, how are we going to handle a second attempt?
1734 icon_name = g_string_new ("");
1735 g_string_append_len (icon_name, mime_type, separator - mime_type);
1736 g_string_append_c (icon_name, '-');
1737 g_string_append (icon_name, separator + 1);
1738 pixbuf = get_cached_icon (widget, icon_name->str, pixel_size);
1739 g_string_free (icon_name, TRUE);
1743 icon_name = g_string_new ("");
1744 g_string_append_len (icon_name, mime_type, separator - mime_type);
1745 g_string_append (icon_name, "-x-generic");
1746 pixbuf = get_cached_icon (widget, icon_name->str, pixel_size);
1747 g_string_free (icon_name, TRUE);
1752 icon_name = g_string_new ("gnome-mime-");
1753 g_string_append_len (icon_name, mime_type, separator - mime_type);
1754 g_string_append_c (icon_name, '-');
1755 g_string_append (icon_name, separator + 1);
1756 name = icon_name->str;
1757 g_string_free (icon_name, FALSE);
1761 /* FIXME: how are we going to implement a second attempt? */
1766 icon_name = g_string_new ("gnome-mime-");
1767 g_string_append_len (icon_name, mime_type, separator - mime_type);
1768 pixbuf = get_cached_icon (widget, icon_name->str, pixel_size);
1769 g_string_free (icon_name, TRUE);
1776 bookmark_list_free (GSList *list)
1780 for (l = list; l; l = l->next)
1783 g_slist_free (list);
1786 /* Returns whether a URI is a local file:// */
1788 is_local_uri (const char *uri)
1794 /* This is rather crude, but hey */
1795 filename = g_filename_from_uri (uri, &hostname, NULL);
1797 result = (filename && !hostname);
1806 bookmark_get_filename (void)
1810 filename = g_build_filename (g_get_home_dir (),
1811 BOOKMARKS_FILENAME, NULL);
1812 g_assert (filename != NULL);
1817 bookmark_list_read (GSList **bookmarks, GError **error)
1821 gboolean result = FALSE;
1823 filename = bookmark_get_filename ();
1826 if (g_file_get_contents (filename, &contents, NULL, error))
1828 gchar **lines = g_strsplit (contents, "\n", -1);
1832 table = g_hash_table_new (g_str_hash, g_str_equal);
1834 for (i = 0; lines[i]; i++)
1836 if (lines[i][0] && !g_hash_table_lookup (table, lines[i]))
1838 *bookmarks = g_slist_prepend (*bookmarks, g_strdup (lines[i]));
1839 g_hash_table_insert (table, lines[i], lines[i]);
1844 g_hash_table_destroy (table);
1847 *bookmarks = g_slist_reverse (*bookmarks);
1857 bookmark_list_write (GSList *bookmarks,
1863 GError *tmp_error = NULL;
1866 string = g_string_new ("");
1868 for (l = bookmarks; l; l = l->next)
1870 g_string_append (string, l->data);
1871 g_string_append_c (string, '\n');
1874 filename = bookmark_get_filename ();
1876 result = g_file_set_contents (filename, string->str, -1, &tmp_error);
1879 g_string_free (string, TRUE);
1884 GTK_FILE_SYSTEM_ERROR,
1885 GTK_FILE_SYSTEM_ERROR_FAILED,
1886 _("Bookmark saving failed: %s"),
1887 tmp_error->message);
1889 g_error_free (tmp_error);
1896 gtk_file_system_unix_insert_bookmark (GtkFileSystem *file_system,
1897 const GtkFilePath *path,
1909 if (!bookmark_list_read (&bookmarks, &err) && err->code != G_FILE_ERROR_NOENT)
1911 g_propagate_error (error, err);
1915 num_bookmarks = g_slist_length (bookmarks);
1916 g_return_val_if_fail (position >= -1 && position <= num_bookmarks, FALSE);
1920 uri = gtk_file_system_unix_path_to_uri (file_system, path);
1922 for (l = bookmarks; l; l = l->next)
1924 char *bookmark, *space;
1928 space = strchr (bookmark, ' ');
1931 if (strcmp (bookmark, uri) != 0)
1939 GTK_FILE_SYSTEM_ERROR,
1940 GTK_FILE_SYSTEM_ERROR_ALREADY_EXISTS,
1941 _("'%s' already exists in the bookmarks list"),
1947 bookmarks = g_slist_insert (bookmarks, g_strdup (uri), position);
1948 if (bookmark_list_write (bookmarks, error))
1951 g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
1957 bookmark_list_free (bookmarks);
1963 gtk_file_system_unix_remove_bookmark (GtkFileSystem *file_system,
1964 const GtkFilePath *path,
1972 if (!bookmark_list_read (&bookmarks, error))
1977 uri = gtk_file_system_path_to_uri (file_system, path);
1979 for (l = bookmarks; l; l = l->next)
1981 char *bookmark, *space;
1983 bookmark = (char *)l->data;
1984 space = strchr (bookmark, ' ');
1988 if (strcmp (bookmark, uri) != 0)
1996 bookmarks = g_slist_remove_link (bookmarks, l);
1999 if (bookmark_list_write (bookmarks, error))
2003 g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
2011 GTK_FILE_SYSTEM_ERROR,
2012 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
2013 _("'%s' does not exist in the bookmarks list"),
2019 bookmark_list_free (bookmarks);
2025 gtk_file_system_unix_list_bookmarks (GtkFileSystem *file_system)
2031 if (!bookmark_list_read (&bookmarks, NULL))
2036 for (l = bookmarks; l; l = l->next)
2038 char *bookmark, *space;
2040 bookmark = (char *)l->data;
2041 space = strchr (bookmark, ' ');
2045 if (is_local_uri (bookmark))
2046 result = g_slist_prepend (result, gtk_file_system_unix_uri_to_path (file_system, bookmark));
2049 bookmark_list_free (bookmarks);
2051 result = g_slist_reverse (result);
2056 gtk_file_system_unix_get_bookmark_label (GtkFileSystem *file_system,
2057 const GtkFilePath *path)
2062 char *bookmark, *space, *uri;
2067 uri = gtk_file_system_path_to_uri (file_system, path);
2068 bookmark_list_read (&labels, NULL);
2070 for (l = labels; l && !label; l = l->next)
2072 bookmark = (char *)l->data;
2073 space = strchr (bookmark, ' ');
2079 if (strcmp (uri, bookmark) == 0)
2080 label = g_strdup (space + 1);
2083 bookmark_list_free (labels);
2090 gtk_file_system_unix_set_bookmark_label (GtkFileSystem *file_system,
2091 const GtkFilePath *path,
2096 gchar *bookmark, *space, *uri;
2101 uri = gtk_file_system_path_to_uri (file_system, path);
2102 bookmark_list_read (&labels, NULL);
2105 for (l = labels; l && !found; l = l->next)
2107 bookmark = (gchar *)l->data;
2108 space = strchr (bookmark, ' ');
2112 if (strcmp (bookmark, uri) != 0)
2121 if (label && *label)
2122 l->data = g_strdup_printf ("%s %s", uri, label);
2124 l->data = g_strdup (uri);
2133 if (bookmark_list_write (labels, NULL))
2134 g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
2137 bookmark_list_free (labels);
2142 _gtk_file_folder_unix_class_init (GtkFileFolderUnixClass *class)
2144 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
2146 gobject_class->finalize = gtk_file_folder_unix_finalize;
2150 gtk_file_folder_unix_iface_init (GtkFileFolderIface *iface)
2152 iface->get_info = gtk_file_folder_unix_get_info;
2153 iface->list_children = gtk_file_folder_unix_list_children;
2154 iface->is_finished_loading = gtk_file_folder_unix_is_finished_loading;
2158 _gtk_file_folder_unix_init (GtkFileFolderUnix *impl)
2163 gtk_file_folder_unix_finalize (GObject *object)
2165 GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (object);
2167 if (folder_unix->load_folder_id)
2169 g_source_remove (folder_unix->load_folder_id);
2170 folder_unix->load_folder_id = 0;
2173 g_hash_table_remove (folder_unix->system_unix->folder_hash, folder_unix->filename);
2175 if (folder_unix->stat_info)
2178 g_print ("Releasing information for directory %s\n", folder_unix->filename);
2180 g_hash_table_destroy (folder_unix->stat_info);
2183 g_free (folder_unix->filename);
2185 G_OBJECT_CLASS (_gtk_file_folder_unix_parent_class)->finalize (object);
2188 /* Creates a GtkFileInfo for "/" by stat()ing it */
2189 static GtkFileInfo *
2190 file_info_for_root_with_error (const char *root_name,
2193 struct stat statbuf;
2196 if (stat (root_name, &statbuf) != 0)
2200 saved_errno = errno;
2202 GTK_FILE_SYSTEM_ERROR,
2203 GTK_FILE_SYSTEM_ERROR_FAILED,
2204 _("Error getting information for '%s': %s"),
2205 "/", g_strerror (saved_errno));
2210 info = gtk_file_info_new ();
2211 gtk_file_info_set_display_name (info, "/");
2212 gtk_file_info_set_is_folder (info, TRUE);
2213 gtk_file_info_set_is_hidden (info, FALSE);
2214 gtk_file_info_set_mime_type (info, "x-directory/normal");
2215 gtk_file_info_set_modification_time (info, statbuf.st_mtime);
2216 gtk_file_info_set_size (info, statbuf.st_size);
2222 stat_with_error (const char *filename,
2223 struct stat *statbuf,
2226 if (stat (filename, statbuf) == -1 &&
2227 (errno != ENOENT || lstat (filename, statbuf) == -1))
2233 saved_errno = errno;
2235 if (saved_errno == ENOENT)
2236 code = GTK_FILE_SYSTEM_ERROR_NONEXISTENT;
2238 code = GTK_FILE_SYSTEM_ERROR_FAILED;
2240 display_name = g_filename_display_name (filename);
2242 GTK_FILE_SYSTEM_ERROR,
2244 _("Error getting information for '%s': %s"),
2246 g_strerror (saved_errno));
2248 g_free (display_name);
2255 /* Creates a new GtkFileInfo from the specified data */
2256 static GtkFileInfo *
2257 create_file_info (GtkFileFolderUnix *folder_unix,
2258 const char *filename,
2259 const char *basename,
2260 GtkFileInfoType types,
2261 struct stat *statbuf,
2262 const char *mime_type)
2266 info = gtk_file_info_new ();
2268 if (types & GTK_FILE_INFO_DISPLAY_NAME)
2270 gchar *display_name = g_filename_display_basename (filename);
2271 gtk_file_info_set_display_name (info, display_name);
2272 g_free (display_name);
2275 if (types & GTK_FILE_INFO_IS_HIDDEN)
2279 if (file_is_hidden (folder_unix, basename))
2280 gtk_file_info_set_is_hidden (info, TRUE);
2284 if (get_is_hidden_for_file (filename, basename))
2285 gtk_file_info_set_is_hidden (info, TRUE);
2289 if (types & GTK_FILE_INFO_IS_FOLDER)
2290 gtk_file_info_set_is_folder (info, S_ISDIR (statbuf->st_mode));
2292 if (types & GTK_FILE_INFO_MIME_TYPE)
2293 gtk_file_info_set_mime_type (info, mime_type);
2295 if (types & GTK_FILE_INFO_MODIFICATION_TIME)
2296 gtk_file_info_set_modification_time (info, statbuf->st_mtime);
2298 if (types & GTK_FILE_INFO_SIZE)
2299 gtk_file_info_set_size (info, (gint64) statbuf->st_size);
2301 if (types & GTK_FILE_INFO_ICON)
2304 gboolean free_icon_name = FALSE;
2305 const char *icon_name;
2306 const char *icon_mime_type;
2308 icon_type = get_icon_type_from_path (folder_unix, statbuf, filename, &icon_mime_type);
2313 icon_name = get_fallback_icon_name (icon_type);
2317 free_icon_name = TRUE;
2319 icon_name = get_icon_name_for_mime_type (icon_mime_type);
2321 icon_name = get_icon_name_for_mime_type (mime_type);
2325 icon_name = get_special_icon_name (icon_type, filename);
2329 gtk_file_info_set_icon_name (info, icon_name);
2332 g_free ((char *) icon_name);
2338 static struct stat_info_entry *
2339 create_stat_info_entry_and_emit_add (GtkFileFolderUnix *folder_unix,
2340 const char *filename,
2341 const char *basename,
2342 struct stat *statbuf)
2346 struct stat_info_entry *entry;
2348 entry = g_new0 (struct stat_info_entry, 1);
2350 if ((folder_unix->types & STAT_NEEDED_MASK) != 0)
2351 entry->statbuf = *statbuf;
2353 if ((folder_unix->types & GTK_FILE_INFO_MIME_TYPE) != 0)
2354 entry->mime_type = g_strdup (xdg_mime_get_mime_type_for_file (filename, statbuf));
2356 g_hash_table_insert (folder_unix->stat_info,
2357 g_strdup (basename),
2360 path = gtk_file_path_new_dup (filename);
2361 paths = g_slist_append (NULL, path);
2362 g_signal_emit_by_name (folder_unix, "files-added", paths);
2363 gtk_file_path_free (path);
2364 g_slist_free (paths);
2369 static GtkFileInfo *
2370 gtk_file_folder_unix_get_info (GtkFileFolder *folder,
2371 const GtkFilePath *path,
2374 GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (folder);
2376 gchar *dirname, *basename;
2377 const char *filename;
2378 GtkFileInfoType types;
2379 struct stat statbuf;
2380 const char *mime_type;
2382 /* Get_info for "/" */
2385 g_return_val_if_fail (filename_is_root (folder_unix->filename), NULL);
2386 return file_info_for_root_with_error (folder_unix->filename, error);
2389 /* Get_info for normal files */
2391 filename = gtk_file_path_get_string (path);
2392 g_return_val_if_fail (filename != NULL, NULL);
2393 g_return_val_if_fail (g_path_is_absolute (filename), NULL);
2395 dirname = get_parent_dir (filename);
2396 g_return_val_if_fail (strcmp (dirname, folder_unix->filename) == 0, NULL);
2399 basename = g_path_get_basename (filename);
2400 types = folder_unix->types;
2402 if (folder_unix->have_stat)
2404 struct stat_info_entry *entry;
2406 g_assert (folder_unix->stat_info != NULL);
2407 entry = g_hash_table_lookup (folder_unix->stat_info, basename);
2411 if (!stat_with_error (filename, &statbuf, error))
2417 entry = create_stat_info_entry_and_emit_add (folder_unix, filename, basename, &statbuf);
2420 info = create_file_info (folder_unix, filename, basename, types, &entry->statbuf, entry->mime_type);
2426 if (!stat_with_error (filename, &statbuf, error))
2432 if ((types & GTK_FILE_INFO_MIME_TYPE) != 0)
2433 mime_type = xdg_mime_get_mime_type_for_file (filename, &statbuf);
2437 info = create_file_info (folder_unix, filename, basename, types, &statbuf, mime_type);
2445 cb_list_children (gpointer key, gpointer value, gpointer user_data)
2447 GSList **children = user_data;
2448 *children = g_slist_prepend (*children, key);
2452 gtk_file_folder_unix_list_children (GtkFileFolder *folder,
2456 GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (folder);
2461 /* Get the list of basenames. */
2462 if (folder_unix->stat_info)
2463 g_hash_table_foreach (folder_unix->stat_info, cb_list_children, children);
2465 /* Turn basenames into GFilePaths. */
2466 for (l = *children; l; l = l->next)
2468 const char *basename = l->data;
2469 char *fullname = g_build_filename (folder_unix->filename, basename, NULL);
2470 l->data = filename_to_path (fullname);
2478 gtk_file_folder_unix_is_finished_loading (GtkFileFolder *folder)
2480 return GTK_FILE_FOLDER_UNIX (folder)->is_finished_loading;
2484 free_stat_info_entry (struct stat_info_entry *entry)
2486 g_free (entry->mime_type);
2491 fill_in_names (GtkFileFolderUnix *folder_unix, GError **error)
2495 if (folder_unix->stat_info)
2498 dir = g_dir_open (folder_unix->filename, 0, error);
2502 folder_unix->stat_info = g_hash_table_new_full (g_str_hash, g_str_equal,
2503 (GDestroyNotify) g_free,
2504 (GDestroyNotify) free_stat_info_entry);
2508 struct stat_info_entry *entry;
2509 const gchar *basename;
2511 basename = g_dir_read_name (dir);
2515 entry = g_new0 (struct stat_info_entry, 1);
2516 if (folder_unix->is_network_dir)
2518 entry->statbuf.st_mode = S_IFDIR;
2519 entry->mime_type = g_strdup ("x-directory/normal");
2522 g_hash_table_insert (folder_unix->stat_info,
2523 g_strdup (basename),
2529 folder_unix->asof = time (NULL);
2534 cb_fill_in_stats (gpointer key, gpointer value, gpointer user_data)
2536 const char *basename = key;
2537 struct stat_info_entry *entry = value;
2538 GtkFileFolderUnix *folder_unix = user_data;
2539 char *fullname = g_build_filename (folder_unix->filename, basename, NULL);
2542 if (stat (fullname, &entry->statbuf) == -1 &&
2543 (errno != ENOENT || lstat (fullname, &entry->statbuf) == -1))
2544 result = TRUE; /* Couldn't stat -- remove from hash. */
2554 fill_in_stats (GtkFileFolderUnix *folder_unix)
2556 if (folder_unix->have_stat)
2559 if (!fill_in_names (folder_unix, NULL))
2562 if (!folder_unix->is_network_dir)
2563 g_hash_table_foreach_remove (folder_unix->stat_info,
2567 folder_unix->have_stat = TRUE;
2572 cb_fill_in_mime_type (gpointer key, gpointer value, gpointer user_data)
2574 const char *basename = key;
2575 struct stat_info_entry *entry = value;
2576 GtkFileFolderUnix *folder_unix = user_data;
2577 char *fullname = g_build_filename (folder_unix->filename, basename, NULL);
2578 struct stat *statbuf = NULL;
2579 const char *mime_type;
2581 if (folder_unix->have_stat)
2582 statbuf = &entry->statbuf;
2584 mime_type = xdg_mime_get_mime_type_for_file (fullname, statbuf);
2585 entry->mime_type = g_strdup (mime_type);
2593 fill_in_mime_type (GtkFileFolderUnix *folder_unix)
2595 if (folder_unix->have_mime_type)
2598 if (!folder_unix->have_stat)
2601 g_assert (folder_unix->stat_info != NULL);
2603 if (!folder_unix->is_network_dir)
2604 g_hash_table_foreach_remove (folder_unix->stat_info,
2605 cb_fill_in_mime_type,
2608 folder_unix->have_mime_type = TRUE;
2612 read_hidden_file (const char *dirname)
2614 gchar **lines = NULL;
2618 hidden_file = g_build_filename (dirname, HIDDEN_FILENAME, NULL);
2620 if (g_file_get_contents (hidden_file, &contents, NULL, NULL))
2622 lines = g_strsplit (contents, "\n", -1);
2626 g_free (hidden_file);
2632 fill_in_hidden (GtkFileFolderUnix *folder_unix)
2636 if (folder_unix->have_hidden)
2639 lines = read_hidden_file (folder_unix->filename);
2645 for (i = 0; lines[i]; i++)
2649 struct stat_info_entry *entry;
2651 entry = g_hash_table_lookup (folder_unix->stat_info, lines[i]);
2653 entry->hidden = TRUE;
2660 folder_unix->have_hidden = TRUE;
2663 static GtkFilePath *
2664 filename_to_path (const char *filename)
2668 tmp = remove_trailing_slash (filename);
2669 return gtk_file_path_new_steal (tmp);
2673 filename_is_root (const char *filename)
2675 const gchar *after_root;
2677 after_root = g_path_skip_root (filename);
2679 return (after_root != NULL && *after_root == '\0');
2683 get_is_hidden_for_file (const char *filename,
2684 const char *basename)
2688 gboolean hidden = FALSE;
2690 dirname = g_path_get_dirname (filename);
2691 lines = read_hidden_file (dirname);
2698 for (i = 0; lines[i]; i++)
2700 if (lines[i][0] && strcmp (lines[i], basename) == 0)
2714 file_is_hidden (GtkFileFolderUnix *folder_unix,
2715 const char *basename)
2717 struct stat_info_entry *entry;
2719 if (basename[0] == '.' || basename[strlen (basename) - 1] == '~')
2722 if (folder_unix->have_stat)
2724 fill_in_hidden (folder_unix);
2726 entry = g_hash_table_lookup (folder_unix->stat_info, basename);
2729 return entry->hidden;
2735 #define __GTK_FILE_SYSTEM_UNIX_C__
2736 #include "gtkaliasdef.c"