1 /* GTK - The GIMP Toolkit
2 * gtkfilesystemwin32.c: Default implementation of GtkFileSystem for Windows
3 * Copyright (C) 2003, Red Hat, Inc.
4 * Copyright (C) 2004, Hans Breuer
5 * Copyright (C) 2007, Novell, Inc.
6 * Copyright (C) 2007, Mathias Hasselmann
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 /* #define this if you want the program to crash when a file system gets
25 * finalized while async handles are still outstanding.
27 #undef HANDLE_ME_HARDER
31 #include "gtkfilesystem.h"
32 #include "gtkfilesystemwin32.h"
33 #include "gtkicontheme.h"
36 #include "gtkiconfactory.h"
39 #include <glib/gstdio.h>
45 #include <sys/types.h>
47 #define WIN32_LEAN_AND_MEAN
49 #include "gdk/win32/gdkwin32.h"
55 #define BOOKMARKS_FILENAME ".gtk-bookmarks"
57 #define FOLDER_CACHE_LIFETIME 2 /* seconds */
59 typedef struct _GtkFileSystemWin32Class GtkFileSystemWin32Class;
61 #define GTK_FILE_SYSTEM_WIN32_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_WIN32, GtkFileSystemWin32Class))
62 #define GTK_IS_FILE_SYSTEM_WIN32_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_WIN32))
63 #define GTK_FILE_SYSTEM_WIN32_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_WIN32, GtkFileSystemWin32Class))
65 struct _GtkFileSystemWin32Class
67 GObjectClass parent_class;
70 struct _GtkFileSystemWin32
72 GObject parent_instance;
74 guint32 drives; /* bitmask as returned by GetLogicalDrives() */
75 GHashTable *folder_hash;
80 guint execute_callbacks_idle_id;
84 /* Icon type, supplemented by MIME type
87 ICON_UNDECIDED, /* Only used while we have not yet computed the icon in a struct stat_info_entry */
88 ICON_NONE, /* "Could not compute the icon type" */
89 ICON_REGULAR, /* Use mime type for icon */
96 #define GTK_TYPE_FILE_FOLDER_WIN32 (_gtk_file_folder_win32_get_type ())
97 #define GTK_FILE_FOLDER_WIN32(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_FOLDER_WIN32, GtkFileFolderWin32))
98 #define GTK_IS_FILE_FOLDER_WIN32(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_FOLDER_WIN32))
99 #define GTK_FILE_FOLDER_WIN32_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_FOLDER_WIN32, GtkFileFolderWin32Class))
100 #define GTK_IS_FILE_FOLDER_WIN32_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_FOLDER_WIN32))
101 #define GTK_FILE_FOLDER_WIN32_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_FOLDER_WIN32, GtkFileFolderWin32Class))
103 typedef struct _GtkFileFolderWin32 GtkFileFolderWin32;
104 typedef struct _GtkFileFolderWin32Class GtkFileFolderWin32Class;
106 struct _GtkFileFolderWin32Class
108 GObjectClass parent_class;
111 struct _GtkFileFolderWin32
113 GObject parent_instance;
115 GtkFileSystemWin32 *system_win32;
116 GtkFileInfoType types;
118 GHashTable *stat_info;
119 guint load_folder_id;
121 guint have_mime_type : 1;
122 guint is_network_dir : 1;
123 guint is_finished_loading : 1;
127 struct stat_info_entry {
128 WIN32_FILE_ATTRIBUTE_DATA wfad;
134 static const GtkFileInfoType STAT_NEEDED_MASK = (GTK_FILE_INFO_IS_FOLDER |
135 GTK_FILE_INFO_MODIFICATION_TIME |
139 static void gtk_file_system_win32_iface_init (GtkFileSystemIface *iface);
140 static void gtk_file_system_win32_dispose (GObject *object);
141 static void gtk_file_system_win32_finalize (GObject *object);
143 static GSList * gtk_file_system_win32_list_volumes (GtkFileSystem *file_system);
144 static GtkFileSystemVolume *gtk_file_system_win32_get_volume_for_path (GtkFileSystem *file_system,
145 const GtkFilePath *path);
147 static GtkFileSystemHandle *gtk_file_system_win32_get_folder (GtkFileSystem *file_system,
148 const GtkFilePath *path,
149 GtkFileInfoType types,
150 GtkFileSystemGetFolderCallback callback,
152 static GtkFileSystemHandle *gtk_file_system_win32_get_info (GtkFileSystem *file_system,
153 const GtkFilePath *path,
154 GtkFileInfoType types,
155 GtkFileSystemGetInfoCallback callback,
157 static GtkFileSystemHandle *gtk_file_system_win32_create_folder (GtkFileSystem *file_system,
158 const GtkFilePath *path,
159 GtkFileSystemCreateFolderCallback callback,
161 static void gtk_file_system_win32_cancel_operation (GtkFileSystemHandle *handle);
163 static void gtk_file_system_win32_volume_free (GtkFileSystem *file_system,
164 GtkFileSystemVolume *volume);
165 static GtkFilePath *gtk_file_system_win32_volume_get_base_path (GtkFileSystem *file_system,
166 GtkFileSystemVolume *volume);
167 static gboolean gtk_file_system_win32_volume_get_is_mounted (GtkFileSystem *file_system,
168 GtkFileSystemVolume *volume);
169 static GtkFileSystemHandle *gtk_file_system_win32_volume_mount (GtkFileSystem *file_system,
170 GtkFileSystemVolume *volume,
171 GtkFileSystemVolumeMountCallback callback,
173 static gchar * gtk_file_system_win32_volume_get_display_name (GtkFileSystem *file_system,
174 GtkFileSystemVolume *volume);
175 static gchar * gtk_file_system_win32_volume_get_icon_name (GtkFileSystem *file_system,
176 GtkFileSystemVolume *volume,
179 static gboolean gtk_file_system_win32_get_parent (GtkFileSystem *file_system,
180 const GtkFilePath *path,
181 GtkFilePath **parent,
183 static GtkFilePath * gtk_file_system_win32_make_path (GtkFileSystem *file_system,
184 const GtkFilePath *base_path,
185 const gchar *display_name,
187 static gboolean gtk_file_system_win32_parse (GtkFileSystem *file_system,
188 const GtkFilePath *base_path,
190 GtkFilePath **folder,
194 static gchar * gtk_file_system_win32_path_to_uri (GtkFileSystem *file_system,
195 const GtkFilePath *path);
196 static gchar * gtk_file_system_win32_path_to_filename (GtkFileSystem *file_system,
197 const GtkFilePath *path);
198 static GtkFilePath *gtk_file_system_win32_uri_to_path (GtkFileSystem *file_system,
200 static GtkFilePath *gtk_file_system_win32_filename_to_path (GtkFileSystem *file_system,
201 const gchar *filename);
204 static gboolean gtk_file_system_win32_insert_bookmark (GtkFileSystem *file_system,
205 const GtkFilePath *path,
208 static gboolean gtk_file_system_win32_remove_bookmark (GtkFileSystem *file_system,
209 const GtkFilePath *path,
211 static GSList * gtk_file_system_win32_list_bookmarks (GtkFileSystem *file_system);
212 static gchar * gtk_file_system_win32_get_bookmark_label (GtkFileSystem *file_system,
213 const GtkFilePath *path);
214 static void gtk_file_system_win32_set_bookmark_label (GtkFileSystem *file_system,
215 const GtkFilePath *path,
218 static void gtk_file_folder_win32_iface_init (GtkFileFolderIface *iface);
219 static void gtk_file_folder_win32_finalize (GObject *object);
221 static GtkFileInfo *gtk_file_folder_win32_get_info (GtkFileFolder *folder,
222 const GtkFilePath *path,
224 static gboolean gtk_file_folder_win32_list_children (GtkFileFolder *folder,
228 static gboolean gtk_file_folder_win32_is_finished_loading (GtkFileFolder *folder);
230 static GtkFilePath *filename_to_path (const gchar *filename);
232 static gboolean filename_is_root (const char *filename);
234 static gboolean filename_is_drive_root (const char *filename);
235 static gboolean filename_is_some_root (const char *filename);
237 static gboolean stat_with_error (const char *filename,
238 WIN32_FILE_ATTRIBUTE_DATA *wfad,
240 static GtkFileInfo *create_file_info (GtkFileFolderWin32 *folder_win32,
241 const char *filename,
242 GtkFileInfoType types,
243 WIN32_FILE_ATTRIBUTE_DATA *wfad,
244 const char *mime_type);
246 static gboolean execute_callbacks (gpointer data);
248 static gboolean fill_in_names (GtkFileFolderWin32 *folder_win32,
250 static void fill_in_stats (GtkFileFolderWin32 *folder_win32);
251 static void fill_in_mime_type (GtkFileFolderWin32 *folder_win32);
253 static gboolean cb_fill_in_stats (gpointer key,
256 static gboolean cb_fill_in_mime_type (gpointer key,
260 /* some info kept together for volumes */
261 struct _GtkFileSystemVolume
270 G_DEFINE_TYPE_WITH_CODE (GtkFileSystemWin32, gtk_file_system_win32, G_TYPE_OBJECT,
271 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_SYSTEM,
272 gtk_file_system_win32_iface_init))
277 G_DEFINE_TYPE_WITH_CODE (GtkFileFolderWin32, _gtk_file_folder_win32, G_TYPE_OBJECT,
278 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_FOLDER,
279 gtk_file_folder_win32_iface_init))
282 * gtk_file_system_win32_new:
284 * Creates a new #GtkFileSystemWin32 object. #GtkFileSystemWin32
285 * implements the #GtkFileSystem interface with direct access to
286 * the filesystem using Windows API calls
288 * Return value: the new #GtkFileSystemWin32 object
291 gtk_file_system_win32_new (void)
293 return g_object_new (GTK_TYPE_FILE_SYSTEM_WIN32, NULL);
297 gtk_file_system_win32_class_init (GtkFileSystemWin32Class *class)
299 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
301 gobject_class->dispose = gtk_file_system_win32_dispose;
302 gobject_class->finalize = gtk_file_system_win32_finalize;
306 gtk_file_system_win32_iface_init (GtkFileSystemIface *iface)
308 iface->list_volumes = gtk_file_system_win32_list_volumes;
309 iface->get_volume_for_path = gtk_file_system_win32_get_volume_for_path;
310 iface->get_folder = gtk_file_system_win32_get_folder;
311 iface->get_info = gtk_file_system_win32_get_info;
312 iface->create_folder = gtk_file_system_win32_create_folder;
313 iface->cancel_operation = gtk_file_system_win32_cancel_operation;
314 iface->volume_free = gtk_file_system_win32_volume_free;
315 iface->volume_get_base_path = gtk_file_system_win32_volume_get_base_path;
316 iface->volume_get_is_mounted = gtk_file_system_win32_volume_get_is_mounted;
317 iface->volume_mount = gtk_file_system_win32_volume_mount;
318 iface->volume_get_display_name = gtk_file_system_win32_volume_get_display_name;
319 iface->volume_get_icon_name = gtk_file_system_win32_volume_get_icon_name;
320 iface->get_parent = gtk_file_system_win32_get_parent;
321 iface->make_path = gtk_file_system_win32_make_path;
322 iface->parse = gtk_file_system_win32_parse;
323 iface->path_to_uri = gtk_file_system_win32_path_to_uri;
324 iface->path_to_filename = gtk_file_system_win32_path_to_filename;
325 iface->uri_to_path = gtk_file_system_win32_uri_to_path;
326 iface->filename_to_path = gtk_file_system_win32_filename_to_path;
327 iface->insert_bookmark = gtk_file_system_win32_insert_bookmark;
328 iface->remove_bookmark = gtk_file_system_win32_remove_bookmark;
329 iface->list_bookmarks = gtk_file_system_win32_list_bookmarks;
330 iface->get_bookmark_label = gtk_file_system_win32_get_bookmark_label;
331 iface->set_bookmark_label = gtk_file_system_win32_set_bookmark_label;
335 * get_viewable_logical_drives:
337 * Returns the list of logical and viewable drives as defined by
338 * GetLogicalDrives() and the registry keys
339 * Software\Microsoft\Windows\CurrentVersion\Policies\Explorer under
340 * HKLM or HKCU. If neither key exists the result of
341 * GetLogicalDrives() is returned.
343 * Return value: bitmask with same meaning as returned by GetLogicalDrives()
346 get_viewable_logical_drives (void)
348 guint viewable_drives = GetLogicalDrives ();
351 DWORD var_type = REG_DWORD; //the value's a REG_DWORD type
352 DWORD no_drives_size = 4;
354 gboolean hklm_present = FALSE;
356 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
357 "Software\\Microsoft\\Windows\\"
358 "CurrentVersion\\Policies\\Explorer",
359 0, KEY_READ, &key) == ERROR_SUCCESS)
361 if (RegQueryValueEx (key, "NoDrives", NULL, &var_type,
362 (LPBYTE) &no_drives, &no_drives_size) == ERROR_SUCCESS)
364 /* We need the bits that are set in viewable_drives, and
365 * unset in no_drives.
367 viewable_drives = viewable_drives & ~no_drives;
373 /* If the key is present in HKLM then the one in HKCU should be ignored */
376 if (RegOpenKeyEx (HKEY_CURRENT_USER,
377 "Software\\Microsoft\\Windows\\"
378 "CurrentVersion\\Policies\\Explorer",
379 0, KEY_READ, &key) == ERROR_SUCCESS)
381 if (RegQueryValueEx (key, "NoDrives", NULL, &var_type,
382 (LPBYTE) &no_drives, &no_drives_size) == ERROR_SUCCESS)
384 viewable_drives = viewable_drives & ~no_drives;
390 return viewable_drives;
394 check_volumes (gpointer data)
396 GtkFileSystemWin32 *system_win32 = GTK_FILE_SYSTEM_WIN32 (data);
398 g_return_val_if_fail (system_win32, FALSE);
400 if (system_win32->drives != get_viewable_logical_drives ())
402 g_signal_emit_by_name (system_win32, "volumes-changed", 0);
409 casefolded_hash (gconstpointer v)
411 const gchar *p = (const gchar *) v;
416 h = (h << 5) - h + g_unichar_toupper (g_utf8_get_char (p));
417 p = g_utf8_next_char (p);
423 static gboolean casefolded_equal (gconstpointer v1,
426 return (_gtk_file_system_win32_path_compare ((const gchar *) v1, (const gchar *) v2) == 0);
430 gtk_file_system_win32_init (GtkFileSystemWin32 *system_win32)
432 system_win32->folder_hash = g_hash_table_new (casefolded_hash, casefolded_equal);
434 /* Set up an idle handler for volume changes. Once a second should
437 system_win32->timeout = gdk_threads_add_timeout_full (0, 1000, check_volumes, system_win32, NULL);
439 system_win32->handles = g_hash_table_new (g_direct_hash, g_direct_equal);
441 system_win32->execute_callbacks_idle_id = 0;
442 system_win32->callbacks = NULL;
446 check_handle_fn (gpointer key, gpointer value, gpointer data)
448 GtkFileSystemHandle *handle;
449 int *num_live_handles;
452 num_live_handles = data;
454 (*num_live_handles)++;
456 g_warning ("file_system_win32=%p still has handle=%p at finalization which is %s!",
459 handle->cancelled ? "CANCELLED" : "NOT CANCELLED");
463 check_handles_at_finalization (GtkFileSystemWin32 *system_win32)
465 int num_live_handles;
467 num_live_handles = 0;
469 g_hash_table_foreach (system_win32->handles, check_handle_fn, &num_live_handles);
470 #ifdef HANDLE_ME_HARDER
471 g_assert (num_live_handles == 0);
474 g_hash_table_destroy (system_win32->handles);
475 system_win32->handles = NULL;
478 #define GTK_TYPE_FILE_SYSTEM_HANDLE_WIN32 (_gtk_file_system_handle_win32_get_type ())
480 typedef struct _GtkFileSystemHandle GtkFileSystemHandleWin32;
481 typedef struct _GtkFileSystemHandleClass GtkFileSystemHandleWin32Class;
483 G_DEFINE_TYPE (GtkFileSystemHandleWin32, _gtk_file_system_handle_win32, GTK_TYPE_FILE_SYSTEM_HANDLE)
486 _gtk_file_system_handle_win32_init (GtkFileSystemHandleWin32 *handle)
491 _gtk_file_system_handle_win32_finalize (GObject *object)
493 GtkFileSystemHandleWin32 *handle;
494 GtkFileSystemWin32 *system_win32;
496 handle = (GtkFileSystemHandleWin32 *)object;
498 system_win32 = GTK_FILE_SYSTEM_WIN32 (GTK_FILE_SYSTEM_HANDLE (handle)->file_system);
500 g_assert (g_hash_table_lookup (system_win32->handles, handle) != NULL);
501 g_hash_table_remove (system_win32->handles, handle);
503 if (G_OBJECT_CLASS (_gtk_file_system_handle_win32_parent_class)->finalize)
504 G_OBJECT_CLASS (_gtk_file_system_handle_win32_parent_class)->finalize (object);
508 _gtk_file_system_handle_win32_class_init (GtkFileSystemHandleWin32Class *class)
510 GObjectClass *gobject_class = (GObjectClass *) class;
512 gobject_class->finalize = _gtk_file_system_handle_win32_finalize;
516 gtk_file_system_win32_dispose (GObject *object)
518 GtkFileSystemWin32 *system_win32;
520 system_win32 = GTK_FILE_SYSTEM_WIN32 (object);
522 if (system_win32->execute_callbacks_idle_id)
524 g_source_remove (system_win32->execute_callbacks_idle_id);
525 system_win32->execute_callbacks_idle_id = 0;
527 /* call pending callbacks */
528 execute_callbacks (system_win32);
531 G_OBJECT_CLASS (gtk_file_system_win32_parent_class)->dispose (object);
535 gtk_file_system_win32_finalize (GObject *object)
537 GtkFileSystemWin32 *system_win32;
539 system_win32 = GTK_FILE_SYSTEM_WIN32 (object);
541 g_source_remove (system_win32->timeout);
543 check_handles_at_finalization (system_win32);
545 /* FIXME: assert that the hash is empty? */
546 g_hash_table_destroy (system_win32->folder_hash);
548 G_OBJECT_CLASS (gtk_file_system_win32_parent_class)->finalize (object);
551 /* Lifted from GLib */
554 get_special_folder (int csidl)
558 wchar_t wc[MAX_PATH+1];
561 LPITEMIDLIST pidl = NULL;
563 gchar *retval = NULL;
565 hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
568 b = SHGetPathFromIDListW (pidl, path.wc);
570 retval = g_utf16_to_utf8 (path.wc, -1, NULL, NULL, NULL);
571 CoTaskMemFree (pidl);
577 _gtk_file_system_win32_get_desktop (void)
579 return get_special_folder (CSIDL_DESKTOPDIRECTORY);
583 gtk_file_system_win32_list_volumes (GtkFileSystem *file_system)
586 gchar drive[4] = "A:\\";
588 GtkFileSystemWin32 *system_win32 = (GtkFileSystemWin32 *)file_system;
590 drives = get_viewable_logical_drives ();
592 system_win32->drives = drives;
594 g_warning ("get_viewable_logical_drives failed.");
596 while (drives && drive[0] <= 'Z')
600 GtkFileSystemVolume *vol = g_new0 (GtkFileSystemVolume, 1);
601 vol->drive = g_strdup (drive);
602 vol->drive_type = GetDriveType (drive);
603 list = g_slist_append (list, vol);
611 static GtkFileSystemVolume *
612 gtk_file_system_win32_get_volume_for_path (GtkFileSystem *file_system,
613 const GtkFilePath *path)
615 GtkFileSystemVolume *vol = g_new0 (GtkFileSystemVolume, 1);
618 g_return_val_if_fail (path != NULL, NULL);
620 p = gtk_file_path_get_string (path);
622 if (!g_path_is_absolute (p))
624 if (g_ascii_isalpha (p[0]) && p[1] == ':')
625 vol->drive = g_strdup_printf ("%c:\\", p[0]);
627 vol->drive = g_strdup ("\\");
628 vol->drive_type = GetDriveType (vol->drive);
632 const gchar *q = g_path_skip_root (p);
633 vol->drive = g_strndup (p, q - p);
634 if (!G_IS_DIR_SEPARATOR (q[-1]))
636 /* Make sure "drive" always ends in a slash */
637 gchar *tem = vol->drive;
638 vol->drive = g_strconcat (vol->drive, "\\", NULL);
642 if (filename_is_drive_root (vol->drive))
644 vol->drive[0] = g_ascii_toupper (vol->drive[0]);
645 vol->drive_type = GetDriveType (vol->drive);
649 wchar_t *wdrive = g_utf8_to_utf16 (vol->drive, -1, NULL, NULL, NULL);
650 vol->drive_type = GetDriveTypeW (wdrive);
658 remove_trailing_slash (const char *filename)
662 len = strlen (filename);
664 if (g_path_is_absolute (filename))
665 root_len = g_path_skip_root (filename) - filename;
668 if (len > root_len && G_IS_DIR_SEPARATOR (filename[len - 1]))
669 return g_strndup (filename, len - 1);
671 return g_memdup (filename, len + 1);
674 /* Delay callback dispatching
681 CALLBACK_CREATE_FOLDER,
682 CALLBACK_VOLUME_MOUNT
685 static void queue_callback (GtkFileSystemWin32 *system_win32, enum callback_types type, gpointer data);
687 struct get_info_callback
689 GtkFileSystemGetInfoCallback callback;
690 GtkFileSystemHandle *handle;
691 GtkFileInfo *file_info;
697 dispatch_get_info_callback (struct get_info_callback *info)
699 (* info->callback) (info->handle, info->file_info, info->error, info->data);
702 gtk_file_info_free (info->file_info);
705 g_error_free (info->error);
707 g_object_unref (info->handle);
713 queue_get_info_callback (GtkFileSystemGetInfoCallback callback,
714 GtkFileSystemHandle *handle,
715 GtkFileInfo *file_info,
719 struct get_info_callback *info;
721 info = g_new (struct get_info_callback, 1);
722 info->callback = callback;
723 info->handle = handle;
724 info->file_info = file_info;
728 queue_callback (GTK_FILE_SYSTEM_WIN32 (handle->file_system), CALLBACK_GET_INFO, info);
732 struct get_folder_callback
734 GtkFileSystemGetFolderCallback callback;
735 GtkFileSystemHandle *handle;
736 GtkFileFolder *folder;
742 dispatch_get_folder_callback (struct get_folder_callback *info)
744 (* info->callback) (info->handle, info->folder, info->error, info->data);
747 g_error_free (info->error);
749 g_object_unref (info->handle);
755 queue_get_folder_callback (GtkFileSystemGetFolderCallback callback,
756 GtkFileSystemHandle *handle,
757 GtkFileFolder *folder,
761 struct get_folder_callback *info;
763 info = g_new (struct get_folder_callback, 1);
764 info->callback = callback;
765 info->handle = handle;
766 info->folder = folder;
770 queue_callback (GTK_FILE_SYSTEM_WIN32 (handle->file_system), CALLBACK_GET_FOLDER, info);
774 struct create_folder_callback
776 GtkFileSystemCreateFolderCallback callback;
777 GtkFileSystemHandle *handle;
784 dispatch_create_folder_callback (struct create_folder_callback *info)
786 (* info->callback) (info->handle, info->path, info->error, info->data);
789 g_error_free (info->error);
792 gtk_file_path_free (info->path);
794 g_object_unref (info->handle);
800 queue_create_folder_callback (GtkFileSystemCreateFolderCallback callback,
801 GtkFileSystemHandle *handle,
802 const GtkFilePath *path,
806 struct create_folder_callback *info;
808 info = g_new (struct create_folder_callback, 1);
809 info->callback = callback;
810 info->handle = handle;
811 info->path = gtk_file_path_copy (path);
815 queue_callback (GTK_FILE_SYSTEM_WIN32 (handle->file_system), CALLBACK_CREATE_FOLDER, info);
819 struct volume_mount_callback
821 GtkFileSystemVolumeMountCallback callback;
822 GtkFileSystemHandle *handle;
823 GtkFileSystemVolume *volume;
829 dispatch_volume_mount_callback (struct volume_mount_callback *info)
831 (* info->callback) (info->handle, info->volume, info->error, info->data);
834 g_error_free (info->error);
836 g_object_unref (info->handle);
842 queue_volume_mount_callback (GtkFileSystemVolumeMountCallback callback,
843 GtkFileSystemHandle *handle,
844 GtkFileSystemVolume *volume,
848 struct volume_mount_callback *info;
850 info = g_new (struct volume_mount_callback, 1);
851 info->callback = callback;
852 info->handle = handle;
853 info->volume = volume;
857 queue_callback (GTK_FILE_SYSTEM_WIN32 (handle->file_system), CALLBACK_VOLUME_MOUNT, info);
863 enum callback_types type;
867 struct get_info_callback *get_info;
868 struct get_folder_callback *get_folder;
869 struct create_folder_callback *create_folder;
870 struct volume_mount_callback *volume_mount;
877 execute_callbacks (gpointer data)
880 gboolean unref_file_system = TRUE;
881 GtkFileSystemWin32 *system_win32 = GTK_FILE_SYSTEM_WIN32 (data);
883 if (!system_win32->execute_callbacks_idle_id)
884 unref_file_system = FALSE;
886 g_object_ref (system_win32);
888 for (l = system_win32->callbacks; l; l = l->next)
890 struct callback_info *info = l->data;
894 case CALLBACK_GET_INFO:
895 dispatch_get_info_callback (info->info.get_info);
898 case CALLBACK_GET_FOLDER:
899 dispatch_get_folder_callback (info->info.get_folder);
902 case CALLBACK_CREATE_FOLDER:
903 dispatch_create_folder_callback (info->info.create_folder);
906 case CALLBACK_VOLUME_MOUNT:
907 dispatch_volume_mount_callback (info->info.volume_mount);
914 g_slist_free (system_win32->callbacks);
915 system_win32->callbacks = NULL;
917 if (unref_file_system)
918 g_object_unref (system_win32);
920 system_win32->execute_callbacks_idle_id = 0;
926 queue_callback (GtkFileSystemWin32 *system_win32,
927 enum callback_types type,
930 struct callback_info *info;
932 info = g_new (struct callback_info, 1);
937 case CALLBACK_GET_INFO:
938 info->info.get_info = data;
941 case CALLBACK_GET_FOLDER:
942 info->info.get_folder = data;
945 case CALLBACK_CREATE_FOLDER:
946 info->info.create_folder = data;
949 case CALLBACK_VOLUME_MOUNT:
950 info->info.volume_mount = data;
954 system_win32->callbacks = g_slist_append (system_win32->callbacks, info);
956 if (!system_win32->execute_callbacks_idle_id)
957 system_win32->execute_callbacks_idle_id = gdk_threads_add_idle (execute_callbacks, system_win32);
960 static GtkFileSystemHandle *
961 create_handle (GtkFileSystem *file_system)
963 GtkFileSystemWin32 *system_win32;
964 GtkFileSystemHandle *handle;
966 system_win32 = GTK_FILE_SYSTEM_WIN32 (file_system);
968 handle = g_object_new (GTK_TYPE_FILE_SYSTEM_HANDLE_WIN32, NULL);
969 handle->file_system = file_system;
971 g_assert (g_hash_table_lookup (system_win32->handles, handle) == NULL);
972 g_hash_table_insert (system_win32->handles, handle, handle);
978 get_mime_type_for_file (const char *filename,
979 const WIN32_FILE_ATTRIBUTE_DATA *wfad)
981 const char *extension;
983 DWORD type, nbytes = 0;
986 if (wfad->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
987 return g_strdup ("x-directory/normal");
989 extension = strrchr (filename, '.');
991 if (extension != NULL &&
992 (stricmp (extension, ".exe") == 0 ||
993 stricmp (extension, ".com") == 0))
994 return g_strdup ("application/x-executable");
996 if (extension != NULL &&
997 extension[1] != '\0' &&
998 RegOpenKeyEx (HKEY_CLASSES_ROOT, extension, 0,
999 KEY_QUERY_VALUE, &key) == ERROR_SUCCESS &&
1000 RegQueryValueEx (key, "Content Type", 0,
1001 &type, NULL, &nbytes) == ERROR_SUCCESS &&
1003 (value = g_try_malloc (nbytes + 1)) &&
1004 RegQueryValueEx (key, "Content Type", 0,
1005 &type, value, &nbytes) == ERROR_SUCCESS)
1007 value[nbytes] = '\0';
1010 value = g_strdup ("application/octet-stream");
1017 static GtkFileSystemHandle *
1018 gtk_file_system_win32_get_info (GtkFileSystem *file_system,
1019 const GtkFilePath *path,
1020 GtkFileInfoType types,
1021 GtkFileSystemGetInfoCallback callback,
1024 GError *error = NULL;
1025 GtkFileSystemWin32 *system_win32;
1026 GtkFileSystemHandle *handle;
1027 const char *filename;
1029 WIN32_FILE_ATTRIBUTE_DATA wfad;
1030 const char *mime_type;
1032 system_win32 = GTK_FILE_SYSTEM_WIN32 (file_system);
1033 handle = create_handle (file_system);
1035 filename = gtk_file_path_get_string (path);
1036 g_return_val_if_fail (filename != NULL, NULL);
1037 g_return_val_if_fail (g_path_is_absolute (filename), NULL);
1039 if (!stat_with_error (filename, &wfad, &error))
1041 g_object_ref (handle);
1042 queue_get_info_callback (callback, handle, NULL, error, data);
1046 if ((types & GTK_FILE_INFO_MIME_TYPE) != 0)
1047 mime_type = get_mime_type_for_file (filename, &wfad);
1051 info = create_file_info (NULL, filename, types, &wfad, mime_type);
1052 g_object_ref (handle);
1053 queue_get_info_callback (callback, handle, info, NULL, data);
1059 load_folder (gpointer data)
1061 GtkFileFolderWin32 *folder_win32 = data;
1064 if ((folder_win32->types & STAT_NEEDED_MASK) != 0)
1065 fill_in_stats (folder_win32);
1067 if ((folder_win32->types & GTK_FILE_INFO_MIME_TYPE) != 0)
1068 fill_in_mime_type (folder_win32);
1070 if (gtk_file_folder_win32_list_children (GTK_FILE_FOLDER (folder_win32), &children, NULL))
1072 folder_win32->is_finished_loading = TRUE;
1073 g_signal_emit_by_name (folder_win32, "files-added", children);
1074 gtk_file_paths_free (children);
1077 folder_win32->load_folder_id = 0;
1079 g_signal_emit_by_name (folder_win32, "finished-loading", 0);
1084 static GtkFileSystemHandle *
1085 gtk_file_system_win32_get_folder (GtkFileSystem *file_system,
1086 const GtkFilePath *path,
1087 GtkFileInfoType types,
1088 GtkFileSystemGetFolderCallback callback,
1091 GError *error = NULL;
1092 GtkFileSystemWin32 *system_win32;
1093 GtkFileFolderWin32 *folder_win32;
1094 GtkFileSystemHandle *handle;
1095 const gchar *filename;
1096 char *filename_copy;
1097 gboolean set_asof = FALSE;
1099 system_win32 = GTK_FILE_SYSTEM_WIN32 (file_system);
1101 filename = gtk_file_path_get_string (path);
1102 g_return_val_if_fail (filename != NULL, NULL);
1103 g_return_val_if_fail (g_path_is_absolute (filename), NULL);
1105 handle = create_handle (file_system);
1107 filename_copy = remove_trailing_slash (filename);
1108 folder_win32 = g_hash_table_lookup (system_win32->folder_hash, filename_copy);
1112 g_free (filename_copy);
1113 if (folder_win32->stat_info &&
1114 time (NULL) - folder_win32->asof >= FOLDER_CACHE_LIFETIME)
1117 g_print ("Cleaning out cached directory %s\n", filename);
1119 g_hash_table_destroy (folder_win32->stat_info);
1120 folder_win32->stat_info = NULL;
1121 folder_win32->have_mime_type = FALSE;
1122 folder_win32->have_stat = FALSE;
1126 g_object_ref (folder_win32);
1127 folder_win32->types |= types;
1128 types = folder_win32->types;
1132 WIN32_FILE_ATTRIBUTE_DATA wfad;
1134 if (!stat_with_error (filename, &wfad, &error))
1136 g_object_ref (handle);
1137 queue_get_folder_callback (callback, handle, NULL, error, data);
1139 g_free (filename_copy);
1143 if (!wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1145 gchar *display_name = g_filename_display_name (filename);
1147 g_set_error (&error,
1148 GTK_FILE_SYSTEM_ERROR,
1149 GTK_FILE_SYSTEM_ERROR_NOT_FOLDER,
1150 _("Path is not a folder: '%s'"),
1153 g_object_ref (handle);
1154 queue_get_folder_callback (callback, handle, NULL, error, data);
1156 g_free (display_name);
1157 g_free (filename_copy);
1161 folder_win32 = g_object_new (GTK_TYPE_FILE_FOLDER_WIN32, NULL);
1162 folder_win32->system_win32 = system_win32;
1163 folder_win32->filename = filename_copy;
1164 folder_win32->types = types;
1165 folder_win32->stat_info = NULL;
1166 folder_win32->load_folder_id = 0;
1167 folder_win32->have_mime_type = FALSE;
1168 folder_win32->have_stat = FALSE;
1169 folder_win32->is_finished_loading = FALSE;
1172 /* Browsing for shares not yet implemented */
1173 folder_win32->is_network_dir = FALSE;
1175 g_hash_table_insert (system_win32->folder_hash,
1176 folder_win32->filename,
1181 folder_win32->asof = time (NULL);
1183 g_object_ref (handle);
1184 queue_get_folder_callback (callback, handle, GTK_FILE_FOLDER (folder_win32), NULL, data);
1186 /* Start loading the folder contents in an idle */
1187 if (!folder_win32->load_folder_id)
1188 folder_win32->load_folder_id =
1189 gdk_threads_add_idle ((GSourceFunc) load_folder, folder_win32);
1194 static GtkFileSystemHandle *
1195 gtk_file_system_win32_create_folder (GtkFileSystem *file_system,
1196 const GtkFilePath *path,
1197 GtkFileSystemCreateFolderCallback callback,
1200 GError *error = NULL;
1201 GtkFileSystemWin32 *system_win32;
1202 GtkFileSystemHandle *handle;
1203 const char *filename;
1208 system_win32 = GTK_FILE_SYSTEM_WIN32 (file_system);
1210 filename = gtk_file_path_get_string (path);
1211 g_return_val_if_fail (filename != NULL, NULL);
1212 g_return_val_if_fail (g_path_is_absolute (filename), NULL);
1214 handle = create_handle (file_system);
1216 tmp = remove_trailing_slash (filename);
1217 result = g_mkdir (tmp, 0777) == 0;
1223 gchar *display_filename = g_filename_display_name (filename);
1224 g_set_error (&error,
1225 GTK_FILE_SYSTEM_ERROR,
1226 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
1227 _("Error creating folder '%s': %s"),
1229 g_strerror (save_errno));
1231 g_object_ref (handle);
1232 queue_create_folder_callback (callback, handle, path, error, data);
1234 g_free (display_filename);
1238 if (!filename_is_some_root (filename))
1240 parent = g_path_get_dirname (filename);
1243 GtkFileFolderWin32 *folder_win32;
1245 folder_win32 = g_hash_table_lookup (system_win32->folder_hash, parent);
1250 struct stat_info_entry *entry;
1252 /* Make sure the new folder exists in the parent's folder */
1253 entry = g_new0 (struct stat_info_entry, 1);
1254 if (folder_win32->is_network_dir)
1256 entry->wfad.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
1257 entry->mime_type = g_strdup ("x-directory/normal");
1260 basename = g_path_get_basename (filename);
1261 g_hash_table_insert (folder_win32->stat_info,
1265 if (folder_win32->have_stat)
1268 if ((folder_win32->types & STAT_NEEDED_MASK) != 0)
1269 cb_fill_in_stats (basename, entry, folder_win32);
1271 if ((folder_win32->types & GTK_FILE_INFO_MIME_TYPE) != 0)
1272 cb_fill_in_mime_type (basename, entry, folder_win32);
1275 paths = g_slist_append (NULL, (GtkFilePath *) path);
1276 g_signal_emit_by_name (folder_win32, "files-added", paths);
1277 g_slist_free (paths);
1288 gtk_file_system_win32_cancel_operation (GtkFileSystemHandle *handle)
1290 /* We don't set "cancelled" to TRUE here, since the actual operation
1291 * is executed in the function itself and not in a callback. So
1292 * the operations can never be cancelled (since they will be already
1293 * completed at this point.
1298 gtk_file_system_win32_volume_free (GtkFileSystem *file_system,
1299 GtkFileSystemVolume *volume)
1303 g_free (volume->drive);
1304 path = (GtkFilePath *) volume;
1305 gtk_file_path_free (path);
1308 static GtkFilePath *
1309 gtk_file_system_win32_volume_get_base_path (GtkFileSystem *file_system,
1310 GtkFileSystemVolume *volume)
1312 return (GtkFilePath *) g_strdup (volume->drive);
1316 gtk_file_system_win32_volume_get_is_mounted (GtkFileSystem *file_system,
1317 GtkFileSystemVolume *volume)
1322 static GtkFileSystemHandle *
1323 gtk_file_system_win32_volume_mount (GtkFileSystem *file_system,
1324 GtkFileSystemVolume *volume,
1325 GtkFileSystemVolumeMountCallback callback,
1328 GError *error = NULL;
1329 GtkFileSystemHandle *handle = create_handle (file_system);
1331 g_set_error (&error,
1332 GTK_FILE_SYSTEM_ERROR,
1333 GTK_FILE_SYSTEM_ERROR_FAILED,
1334 _("This file system does not support mounting"));
1336 g_object_ref (handle);
1337 queue_volume_mount_callback (callback, handle, volume, error, data);
1343 gtk_file_system_win32_volume_get_display_name (GtkFileSystem *file_system,
1344 GtkFileSystemVolume *volume)
1346 gchar *real_display_name;
1348 g_return_val_if_fail (volume->drive != NULL, NULL);
1350 if (filename_is_drive_root (volume->drive) &&
1351 volume->drive_type == DRIVE_REMOTE)
1352 real_display_name = g_strdup_printf (_("Network Drive (%s)"), volume->drive);
1353 else if ((filename_is_drive_root (volume->drive) && volume->drive[0] >= 'C') ||
1354 volume->drive_type != DRIVE_REMOVABLE)
1357 gunichar2 *wdrive = g_utf8_to_utf16 (volume->drive, -1, NULL, NULL, NULL);
1358 gunichar2 wname[80];
1360 if (GetVolumeInformationW (wdrive,
1361 wname, G_N_ELEMENTS(wname),
1362 NULL, /* serial number */
1363 NULL, /* max. component length */
1364 NULL, /* fs flags */
1365 NULL, 0) /* fs type like FAT, NTFS */ &&
1368 name = g_utf16_to_utf8 (wname, -1, NULL, NULL, NULL);
1374 real_display_name = g_strdup_printf (_("%s (%s)"), name, volume->drive);
1379 real_display_name = g_strdup (volume->drive);
1383 real_display_name = g_strdup (volume->drive);
1385 return real_display_name;
1389 get_icon_type_from_stat (WIN32_FILE_ATTRIBUTE_DATA *wfad)
1391 if (wfad->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1392 return ICON_DIRECTORY;
1394 return ICON_REGULAR;
1397 /* Computes our internal icon type based on a path name; also returns the MIME
1398 * type in case we come up with ICON_REGULAR.
1401 get_icon_type_from_path (GtkFileFolderWin32 *folder_win32,
1402 WIN32_FILE_ATTRIBUTE_DATA *wfad,
1403 const char *filename)
1407 if (folder_win32 && folder_win32->have_stat)
1410 struct stat_info_entry *entry;
1412 g_assert (folder_win32->stat_info != NULL);
1414 basename = g_path_get_basename (filename);
1415 entry = g_hash_table_lookup (folder_win32->stat_info, basename);
1419 if (entry->icon_type == ICON_UNDECIDED)
1421 entry->icon_type = get_icon_type_from_stat (&entry->wfad);
1422 g_assert (entry->icon_type != ICON_UNDECIDED);
1424 icon_type = entry->icon_type;
1425 if (icon_type == ICON_REGULAR)
1427 fill_in_mime_type (folder_win32);
1434 icon_type = get_icon_type_from_stat (wfad);
1439 static const gchar *
1440 get_fallback_icon_name (IconType icon_type)
1445 return GTK_STOCK_HARDDISK;
1447 case ICON_DIRECTORY:
1448 return GTK_STOCK_DIRECTORY;
1450 case ICON_EXECUTABLE:
1451 return GTK_STOCK_EXECUTE;
1454 return GTK_STOCK_FILE;
1459 get_icon_path (const gchar *filename,
1467 g_return_val_if_fail (NULL != filename, NULL);
1468 g_return_val_if_fail ('\0' != *filename, NULL);
1469 g_return_val_if_fail (NULL != index, NULL);
1471 wfn = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1473 if (SHGetFileInfoW (wfn, 0, &shfi, sizeof (shfi), SHGFI_ICONLOCATION))
1475 path = g_utf16_to_utf8 (shfi.szDisplayName, -1, NULL, NULL, NULL);
1476 *index = shfi.iIcon;
1484 create_builtin_icon (const gchar *filename,
1485 const gchar *icon_name,
1488 static const DWORD attributes[] =
1490 SHGFI_ICON | SHGFI_LARGEICON,
1491 SHGFI_ICON | SHGFI_SMALLICON,
1495 GdkPixbuf *pixbuf = NULL;
1501 g_return_val_if_fail (NULL != filename, FALSE);
1502 g_return_val_if_fail ('\0' != *filename, FALSE);
1504 wfn = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1506 for(i = 0; attributes[i]; ++i)
1508 rc = SHGetFileInfoW (wfn, 0, &shfi, sizeof (shfi), attributes[i]);
1510 if (rc && shfi.hIcon)
1512 pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (shfi.hIcon);
1514 if (!DestroyIcon (shfi.hIcon))
1515 g_warning (G_STRLOC ": DestroyIcon failed: %s\n",
1516 g_win32_error_message (GetLastError ()));
1518 gtk_icon_theme_add_builtin_icon (icon_name,
1519 gdk_pixbuf_get_height (pixbuf),
1521 g_object_unref (pixbuf);
1526 return (NULL != pixbuf); /* at least one icon was created */
1530 get_icon_name (const gchar *filename,
1533 gchar *icon_name = NULL;
1534 gchar *icon_path = NULL;
1535 gint icon_index = -1;
1537 icon_path = get_icon_path(filename, icon_type, &icon_index);
1540 icon_name = g_strdup_printf ("gtk-win32-shell-icon;%s;%d",
1541 icon_path, icon_index);
1543 icon_name = g_strdup_printf ("gtk-win32-shell-icon;%s",
1546 if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (), icon_name) &&
1547 !create_builtin_icon (filename, icon_name, icon_type))
1550 icon_name = g_strdup (get_fallback_icon_name (icon_type));
1558 gtk_file_system_win32_volume_get_icon_name (GtkFileSystem *file_system,
1559 GtkFileSystemVolume *volume,
1562 return get_icon_name (volume->drive, ICON_VOLUME);
1565 #if 0 /* Unused, see below */
1568 get_parent_dir (const char *filename)
1572 len = strlen (filename);
1574 /* Ignore trailing slashes */
1575 if (len > 1 && G_IS_DIR_SEPARATOR (filename[len - 1]))
1579 tmp = g_strndup (filename, len - 1);
1581 parent = g_path_get_dirname (tmp);
1587 return g_path_get_dirname (filename);
1593 gtk_file_system_win32_get_parent (GtkFileSystem *file_system,
1594 const GtkFilePath *path,
1595 GtkFilePath **parent,
1598 const char *filename;
1600 filename = gtk_file_path_get_string (path);
1601 g_return_val_if_fail (filename != NULL, FALSE);
1602 g_return_val_if_fail (g_path_is_absolute (filename), FALSE);
1604 if (filename_is_some_root (filename))
1610 gchar *parent_filename = g_path_get_dirname (filename);
1611 *parent = filename_to_path (parent_filename);
1612 g_free (parent_filename);
1614 #if DEBUGGING_OUTPUT
1615 printf ("%s: %s => %s\n", __FUNCTION__, (char*)path, (*parent)?(char*)*parent:"NULL"), fflush(stdout);
1620 static GtkFilePath *
1621 gtk_file_system_win32_make_path (GtkFileSystem *file_system,
1622 const GtkFilePath *base_path,
1623 const gchar *display_name,
1626 const char *base_filename;
1627 gchar *full_filename;
1628 GtkFilePath *result;
1631 base_filename = gtk_file_path_get_string (base_path);
1632 g_return_val_if_fail (base_filename != NULL, NULL);
1633 g_return_val_if_fail (g_path_is_absolute (base_filename), NULL);
1635 if ((p = strpbrk (display_name, "<>\"/\\|")))
1639 badchar[0] = *p; /* We know it is a single-byte char */
1642 GTK_FILE_SYSTEM_ERROR,
1643 GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
1644 _("The name \"%s\" is not valid because it contains the character \"%s\". "
1645 "Please use a different name."),
1651 full_filename = g_build_filename (base_filename, display_name, NULL);
1652 result = filename_to_path (full_filename);
1653 g_free (full_filename);
1658 /* If this was a publically exported function, it should return
1659 * a dup'ed result, but we make it modify-in-place for efficiency
1660 * here, and because it works for us.
1663 canonicalize_filename (gchar *filename)
1667 gboolean last_was_slash = FALSE;
1669 #if DEBUGGING_OUTPUT
1670 printf("%s: %s ", __FUNCTION__, filename), fflush (stdout);
1673 past_root = (gchar *) g_path_skip_root (filename);
1679 if (G_IS_DIR_SEPARATOR (*p))
1681 if (!last_was_slash)
1684 last_was_slash = TRUE;
1688 if (last_was_slash && *p == '.')
1690 if (G_IS_DIR_SEPARATOR (*(p + 1)) ||
1693 if (*(p + 1) == '\0')
1698 else if (*(p + 1) == '.' &&
1699 (G_IS_DIR_SEPARATOR (*(p + 2)) ||
1705 while (q > past_root &&
1706 !G_IS_DIR_SEPARATOR (*(q - 1)))
1710 if (*(p + 2) == '\0')
1718 last_was_slash = FALSE;
1724 last_was_slash = FALSE;
1731 if (q > past_root && G_IS_DIR_SEPARATOR (*(q - 1)))
1736 #if DEBUGGING_OUTPUT
1737 printf(" => %s\n", filename), fflush (stdout);
1742 gtk_file_system_win32_parse (GtkFileSystem *file_system,
1743 const GtkFilePath *base_path,
1745 GtkFilePath **folder,
1749 const char *base_filename;
1750 gchar *last_backslash, *last_slash;
1751 gboolean result = FALSE;
1753 #if DEBUGGING_OUTPUT
1754 printf("%s: base_path=%s str=%s\n",__FUNCTION__,(char*)base_path,str),fflush(stdout);
1757 base_filename = gtk_file_path_get_string (base_path);
1758 g_return_val_if_fail (base_filename != NULL, FALSE);
1759 g_return_val_if_fail (g_path_is_absolute (base_filename), FALSE);
1761 last_backslash = strrchr (str, '\\');
1762 last_slash = strrchr (str, '/');
1763 if (last_slash == NULL ||
1764 (last_backslash != NULL && last_backslash > last_slash))
1765 last_slash = last_backslash;
1769 *folder = gtk_file_path_copy (base_path);
1770 *file_part = g_strdup (str);
1778 if (last_slash == str)
1780 if (g_ascii_isalpha (base_filename[0]) &&
1781 base_filename[1] == ':')
1782 folder_part = g_strdup_printf ("%c:\\", base_filename[0]);
1784 folder_part = g_strdup ("\\");
1786 else if (g_ascii_isalpha (str[0]) &&
1788 last_slash == str + 2)
1789 folder_part = g_strndup (str, last_slash - str + 1);
1791 /* Hmm, what the heck was this case supposed to do? It splits up
1792 * \\server\share\foo\bar into folder_part
1793 * \\server\share\foo\bar and file_path bar. Not good. As far as
1794 * I can see, this isn't needed.
1796 else if (G_IS_DIR_SEPARATOR (str[0]) &&
1797 G_IS_DIR_SEPARATOR (str[1]) &&
1798 (!str[2] || !G_IS_DIR_SEPARATOR (str[2])))
1799 folder_part = g_strdup (str);
1802 folder_part = g_strndup (str, last_slash - str);
1804 g_assert (folder_part);
1806 if (g_path_is_absolute (folder_part))
1807 folder_path = folder_part;
1810 folder_path = g_build_filename (base_filename, folder_part, NULL);
1811 g_free (folder_part);
1814 canonicalize_filename (folder_path);
1816 *folder = filename_to_path (folder_path);
1817 *file_part = g_strdup (last_slash + 1);
1819 g_free (folder_path);
1824 #if DEBUGGING_OUTPUT
1825 printf ("%s:returning folder=%s file_part=%s\n", __FUNCTION__, (*folder?(char*)*folder:"NULL"), *file_part), fflush(stdout);
1832 gtk_file_system_win32_path_to_uri (GtkFileSystem *file_system,
1833 const GtkFilePath *path)
1835 return g_filename_to_uri (gtk_file_path_get_string (path), NULL, NULL);
1839 gtk_file_system_win32_path_to_filename (GtkFileSystem *file_system,
1840 const GtkFilePath *path)
1842 return g_strdup (gtk_file_path_get_string (path));
1845 static GtkFilePath *
1846 gtk_file_system_win32_uri_to_path (GtkFileSystem *file_system,
1850 gchar *filename = g_filename_from_uri (uri, NULL, NULL);
1852 #if DEBUGGING_OUTPUT
1853 printf ("%s: %s -> %s\n", __FUNCTION__, uri, filename?filename:"NULL"), fflush (stdout);
1858 path = filename_to_path (filename);
1867 static GtkFilePath *
1868 gtk_file_system_win32_filename_to_path (GtkFileSystem *file_system,
1869 const gchar *filename)
1871 return filename_to_path (filename);
1875 bookmark_list_free (GSList *list)
1879 for (l = list; l; l = l->next)
1882 g_slist_free (list);
1885 /* Returns whether a URI is a local file:// */
1887 is_local_uri (const char *uri)
1893 /* This is rather crude, but hey */
1894 filename = g_filename_from_uri (uri, &hostname, NULL);
1896 result = (filename && !hostname);
1905 bookmark_get_filename (void)
1909 filename = g_build_filename (g_get_home_dir (),
1910 BOOKMARKS_FILENAME, NULL);
1911 g_assert (filename != NULL);
1916 bookmark_list_read (GSList **bookmarks, GError **error)
1920 gboolean result = FALSE;
1922 filename = bookmark_get_filename ();
1925 if (g_file_get_contents (filename, &contents, NULL, error))
1927 gchar **lines = g_strsplit (contents, "\n", -1);
1931 table = g_hash_table_new (g_str_hash, g_str_equal);
1933 for (i = 0; lines[i]; i++)
1935 if (lines[i][0] && !g_hash_table_lookup (table, lines[i]))
1937 *bookmarks = g_slist_prepend (*bookmarks, g_strdup (lines[i]));
1938 g_hash_table_insert (table, lines[i], lines[i]);
1943 g_hash_table_destroy (table);
1946 *bookmarks = g_slist_reverse (*bookmarks);
1956 bookmark_list_write (GSList *bookmarks,
1962 GError *tmp_error = NULL;
1965 string = g_string_new ("");
1967 for (l = bookmarks; l; l = l->next)
1969 g_string_append (string, l->data);
1970 g_string_append_c (string, '\n');
1973 filename = bookmark_get_filename ();
1975 result = g_file_set_contents (filename, string->str, -1, &tmp_error);
1978 g_string_free (string, TRUE);
1983 GTK_FILE_SYSTEM_ERROR,
1984 GTK_FILE_SYSTEM_ERROR_FAILED,
1985 _("Bookmark saving failed: %s"),
1986 tmp_error->message);
1988 g_error_free (tmp_error);
1995 gtk_file_system_win32_insert_bookmark (GtkFileSystem *file_system,
1996 const GtkFilePath *path,
2008 if (!bookmark_list_read (&bookmarks, &err) && err->code != G_FILE_ERROR_NOENT)
2010 g_propagate_error (error, err);
2014 num_bookmarks = g_slist_length (bookmarks);
2015 g_return_val_if_fail (position >= -1 && position <= num_bookmarks, FALSE);
2019 uri = gtk_file_system_win32_path_to_uri (file_system, path);
2021 for (l = bookmarks; l; l = l->next)
2023 char *bookmark, *space;
2027 space = strchr (bookmark, ' ');
2030 if (strcmp (bookmark, uri) != 0)
2038 GTK_FILE_SYSTEM_ERROR,
2039 GTK_FILE_SYSTEM_ERROR_ALREADY_EXISTS,
2040 _("'%s' already exists in the bookmarks list"),
2046 bookmarks = g_slist_insert (bookmarks, g_strdup (uri), position);
2047 if (bookmark_list_write (bookmarks, error))
2050 g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
2056 bookmark_list_free (bookmarks);
2062 gtk_file_system_win32_remove_bookmark (GtkFileSystem *file_system,
2063 const GtkFilePath *path,
2071 if (!bookmark_list_read (&bookmarks, error))
2076 uri = gtk_file_system_path_to_uri (file_system, path);
2078 for (l = bookmarks; l; l = l->next)
2080 char *bookmark, *space;
2082 bookmark = (char *)l->data;
2083 space = strchr (bookmark, ' ');
2087 if (strcmp (bookmark, uri) != 0)
2095 bookmarks = g_slist_remove_link (bookmarks, l);
2098 if (bookmark_list_write (bookmarks, error))
2102 g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
2110 GTK_FILE_SYSTEM_ERROR,
2111 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
2112 _("'%s' does not exist in the bookmarks list"),
2118 bookmark_list_free (bookmarks);
2124 gtk_file_system_win32_list_bookmarks (GtkFileSystem *file_system)
2130 if (!bookmark_list_read (&bookmarks, NULL))
2135 for (l = bookmarks; l; l = l->next)
2137 char *bookmark, *space;
2139 bookmark = (char *)l->data;
2140 space = strchr (bookmark, ' ');
2144 if (is_local_uri (bookmark))
2145 result = g_slist_prepend (result, gtk_file_system_win32_uri_to_path (file_system, bookmark));
2148 bookmark_list_free (bookmarks);
2150 result = g_slist_reverse (result);
2155 gtk_file_system_win32_get_bookmark_label (GtkFileSystem *file_system,
2156 const GtkFilePath *path)
2161 char *bookmark, *space, *uri;
2166 uri = gtk_file_system_path_to_uri (file_system, path);
2167 bookmark_list_read (&labels, NULL);
2169 for (l = labels; l && !label; l = l->next)
2171 bookmark = (char *)l->data;
2172 space = strchr (bookmark, ' ');
2178 if (strcmp (uri, bookmark) == 0)
2179 label = g_strdup (space + 1);
2182 bookmark_list_free (labels);
2189 gtk_file_system_win32_set_bookmark_label (GtkFileSystem *file_system,
2190 const GtkFilePath *path,
2195 gchar *bookmark, *space, *uri;
2200 uri = gtk_file_system_path_to_uri (file_system, path);
2201 bookmark_list_read (&labels, NULL);
2204 for (l = labels; l && !found; l = l->next)
2206 bookmark = (gchar *)l->data;
2207 space = strchr (bookmark, ' ');
2211 if (strcmp (bookmark, uri) != 0)
2220 if (label && *label)
2221 l->data = g_strdup_printf ("%s %s", uri, label);
2223 l->data = g_strdup (uri);
2232 if (bookmark_list_write (labels, NULL))
2233 g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
2236 bookmark_list_free (labels);
2241 _gtk_file_folder_win32_class_init (GtkFileFolderWin32Class *class)
2243 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
2245 gobject_class->finalize = gtk_file_folder_win32_finalize;
2249 gtk_file_folder_win32_iface_init (GtkFileFolderIface *iface)
2251 iface->get_info = gtk_file_folder_win32_get_info;
2252 iface->list_children = gtk_file_folder_win32_list_children;
2253 iface->is_finished_loading = gtk_file_folder_win32_is_finished_loading;
2257 _gtk_file_folder_win32_init (GtkFileFolderWin32 *impl)
2262 gtk_file_folder_win32_finalize (GObject *object)
2264 GtkFileFolderWin32 *folder_win32 = GTK_FILE_FOLDER_WIN32 (object);
2266 if (folder_win32->load_folder_id)
2268 g_source_remove (folder_win32->load_folder_id);
2269 folder_win32->load_folder_id = 0;
2272 g_hash_table_remove (folder_win32->system_win32->folder_hash, folder_win32->filename);
2274 if (folder_win32->stat_info)
2277 g_print ("Releasing information for directory %s\n", folder_win32->filename);
2279 g_hash_table_destroy (folder_win32->stat_info);
2282 g_free (folder_win32->filename);
2284 G_OBJECT_CLASS (_gtk_file_folder_win32_parent_class)->finalize (object);
2287 /* Creates a GtkFileInfo for a volume root by stat()ing it */
2288 static GtkFileInfo *
2289 file_info_for_root_with_error (const char *root_name,
2292 struct stat statbuf;
2295 if (g_stat (root_name, &statbuf) != 0)
2300 saved_errno = errno;
2301 display_name = g_filename_display_name (root_name);
2303 GTK_FILE_SYSTEM_ERROR,
2304 GTK_FILE_SYSTEM_ERROR_FAILED,
2305 _("Error getting information for '%s': %s"),
2307 g_strerror (saved_errno));
2309 g_free (display_name);
2313 info = gtk_file_info_new ();
2314 gtk_file_info_set_display_name (info, root_name);
2315 gtk_file_info_set_is_folder (info, TRUE);
2316 gtk_file_info_set_is_hidden (info, FALSE);
2317 gtk_file_info_set_mime_type (info, "x-directory/normal");
2318 gtk_file_info_set_modification_time (info, statbuf.st_mtime);
2319 gtk_file_info_set_size (info, statbuf.st_size);
2325 stat_with_error (const char *filename,
2326 WIN32_FILE_ATTRIBUTE_DATA *wfad,
2329 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, error);
2332 if (wfilename == NULL)
2335 rc = GetFileAttributesExW (wfilename, GetFileExInfoStandard, wfad);
2341 int error_number = GetLastError ();
2342 char *emsg = g_win32_error_message (error_number);
2343 gchar *display_name = g_filename_display_name (filename);
2346 if (error_number == ERROR_FILE_NOT_FOUND ||
2347 error_number == ERROR_PATH_NOT_FOUND)
2348 code = GTK_FILE_SYSTEM_ERROR_NONEXISTENT;
2350 code = GTK_FILE_SYSTEM_ERROR_FAILED;
2353 GTK_FILE_SYSTEM_ERROR,
2355 _("Error getting information for '%s': %s"),
2358 g_free (display_name);
2369 /* Creates a new GtkFileInfo from the specified data */
2370 static GtkFileInfo *
2371 create_file_info (GtkFileFolderWin32 *folder_win32,
2372 const char *filename,
2373 GtkFileInfoType types,
2374 WIN32_FILE_ATTRIBUTE_DATA *wfad,
2375 const char *mime_type)
2379 info = gtk_file_info_new ();
2381 if (types & GTK_FILE_INFO_DISPLAY_NAME)
2383 gchar *display_name;
2385 if (filename_is_root (filename))
2386 display_name = g_filename_display_name (filename);
2388 display_name = g_filename_display_basename (filename);
2390 gtk_file_info_set_display_name (info, display_name);
2391 g_free (display_name);
2394 if (types & GTK_FILE_INFO_IS_HIDDEN)
2396 gboolean is_hidden = !!(wfad->dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
2397 gtk_file_info_set_is_hidden (info, is_hidden);
2400 if (types & GTK_FILE_INFO_IS_FOLDER)
2401 gtk_file_info_set_is_folder (info, !!(wfad->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
2403 if (types & GTK_FILE_INFO_MIME_TYPE)
2404 gtk_file_info_set_mime_type (info, mime_type);
2406 if (types & GTK_FILE_INFO_MODIFICATION_TIME)
2408 GtkFileTime time = (wfad->ftLastWriteTime.dwLowDateTime
2409 | ((guint64)wfad->ftLastWriteTime.dwHighDateTime) << 32);
2410 /* 100-nanosecond intervals since January 1, 1601, urgh! */
2411 time /= G_GINT64_CONSTANT (10000000); /* now seconds */
2412 time -= G_GINT64_CONSTANT (134774) * 24 * 3600; /* good old Unix time */
2413 gtk_file_info_set_modification_time (info, time);
2416 if (types & GTK_FILE_INFO_SIZE)
2418 gint64 size = wfad->nFileSizeLow | ((guint64)wfad->nFileSizeHigh) << 32;
2419 gtk_file_info_set_size (info, size);
2422 if (types & GTK_FILE_INFO_ICON)
2424 IconType icon_type = get_icon_type_from_path (folder_win32, wfad, filename);
2425 gchar *icon_name = get_icon_name (filename, icon_type);
2426 gtk_file_info_set_icon_name (info, icon_name);
2433 static struct stat_info_entry *
2434 create_stat_info_entry_and_emit_add (GtkFileFolderWin32 *folder_win32,
2435 const char *filename,
2436 const char *basename,
2437 WIN32_FILE_ATTRIBUTE_DATA *wfad)
2441 struct stat_info_entry *entry;
2443 entry = g_new0 (struct stat_info_entry, 1);
2445 if ((folder_win32->types & STAT_NEEDED_MASK) != 0)
2446 entry->wfad = *wfad;
2448 if ((folder_win32->types & GTK_FILE_INFO_MIME_TYPE) != 0)
2449 entry->mime_type = get_mime_type_for_file (filename, wfad);
2451 g_hash_table_insert (folder_win32->stat_info,
2452 g_strdup (basename),
2455 path = gtk_file_path_new_dup (filename);
2456 paths = g_slist_append (NULL, path);
2457 g_signal_emit_by_name (folder_win32, "files-added", paths);
2458 gtk_file_path_free (path);
2459 g_slist_free (paths);
2464 static GtkFileInfo *
2465 gtk_file_folder_win32_get_info (GtkFileFolder *folder,
2466 const GtkFilePath *path,
2469 GtkFileFolderWin32 *folder_win32 = GTK_FILE_FOLDER_WIN32 (folder);
2471 const char *filename;
2472 GtkFileInfoType types;
2473 WIN32_FILE_ATTRIBUTE_DATA wfad;
2474 const char *mime_type;
2476 /* Get_info for "/" */
2479 g_return_val_if_fail (filename_is_root (folder_win32->filename), NULL);
2480 return file_info_for_root_with_error (folder_win32->filename, error);
2483 /* Get_info for normal files */
2485 filename = gtk_file_path_get_string (path);
2486 g_return_val_if_fail (filename != NULL, NULL);
2487 g_return_val_if_fail (g_path_is_absolute (filename), NULL);
2490 /* Skip this sanity check, as it fails for server share roots, where
2491 * dirname gets set to \\server\share\ and folder_win32->filename is
2492 * \\server\share. Also, should we do a casefolded comparison here,
2496 gchar *dirname = get_parent_dir (filename);
2497 g_return_val_if_fail (strcmp (dirname, folder_win32->filename) == 0, NULL);
2502 types = folder_win32->types;
2504 if (folder_win32->have_stat)
2506 struct stat_info_entry *entry;
2509 g_assert (folder_win32->stat_info != NULL);
2511 basename = g_path_get_basename (filename);
2512 entry = g_hash_table_lookup (folder_win32->stat_info, basename);
2516 if (!stat_with_error (filename, &wfad, error))
2522 entry = create_stat_info_entry_and_emit_add (folder_win32, filename, basename, &wfad);
2526 info = create_file_info (folder_win32, filename, types, &entry->wfad, entry->mime_type);
2531 if (!stat_with_error (filename, &wfad, error))
2534 if ((types & GTK_FILE_INFO_MIME_TYPE) != 0)
2535 mime_type = get_mime_type_for_file (filename, &wfad);
2539 info = create_file_info (folder_win32, filename, types, &wfad, mime_type);
2546 cb_list_children (gpointer key, gpointer value, gpointer user_data)
2548 GSList **children = user_data;
2549 *children = g_slist_prepend (*children, key);
2553 gtk_file_folder_win32_list_children (GtkFileFolder *folder,
2557 GtkFileFolderWin32 *folder_win32 = GTK_FILE_FOLDER_WIN32 (folder);
2562 /* Get the list of basenames. */
2563 if (folder_win32->stat_info)
2564 g_hash_table_foreach (folder_win32->stat_info, cb_list_children, children);
2566 /* Turn basenames into GFilePaths. */
2567 for (l = *children; l; l = l->next)
2569 const char *basename = l->data;
2570 char *fullname = g_build_filename (folder_win32->filename, basename, NULL);
2571 l->data = filename_to_path (fullname);
2579 gtk_file_folder_win32_is_finished_loading (GtkFileFolder *folder)
2581 return GTK_FILE_FOLDER_WIN32 (folder)->is_finished_loading;
2585 free_stat_info_entry (struct stat_info_entry *entry)
2587 g_free (entry->mime_type);
2592 fill_in_names (GtkFileFolderWin32 *folder_win32, GError **error)
2596 if (folder_win32->stat_info)
2599 dir = g_dir_open (folder_win32->filename, 0, error);
2603 folder_win32->stat_info = g_hash_table_new_full (casefolded_hash, casefolded_equal,
2604 (GDestroyNotify) g_free,
2605 (GDestroyNotify) free_stat_info_entry);
2609 struct stat_info_entry *entry;
2610 const gchar *basename;
2612 basename = g_dir_read_name (dir);
2616 entry = g_new0 (struct stat_info_entry, 1);
2617 if (folder_win32->is_network_dir)
2619 g_assert_not_reached ();
2620 entry->wfad.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
2621 entry->mime_type = g_strdup ("x-directory/normal");
2624 g_hash_table_insert (folder_win32->stat_info,
2625 g_strdup (basename),
2631 folder_win32->asof = time (NULL);
2636 cb_fill_in_stats (gpointer key, gpointer value, gpointer user_data)
2638 const char *basename = key;
2639 struct stat_info_entry *entry = value;
2640 GtkFileFolderWin32 *folder_win32 = user_data;
2641 char *fullname = g_build_filename (folder_win32->filename, basename, NULL);
2644 if (!stat_with_error (fullname, &entry->wfad, NULL))
2645 result = TRUE; /* Couldn't stat -- remove from hash. */
2655 fill_in_stats (GtkFileFolderWin32 *folder_win32)
2657 if (folder_win32->have_stat)
2660 if (!fill_in_names (folder_win32, NULL))
2663 if (!folder_win32->is_network_dir)
2664 g_hash_table_foreach_remove (folder_win32->stat_info,
2668 folder_win32->have_stat = TRUE;
2672 cb_fill_in_mime_type (gpointer key, gpointer value, gpointer user_data)
2674 const char *basename = key;
2675 struct stat_info_entry *entry = value;
2676 GtkFileFolderWin32 *folder_win32 = user_data;
2677 char *fullname = g_build_filename (folder_win32->filename, basename, NULL);
2679 entry->mime_type = get_mime_type_for_file (fullname, &entry->wfad);
2687 fill_in_mime_type (GtkFileFolderWin32 *folder_win32)
2689 if (folder_win32->have_mime_type)
2692 if (!folder_win32->have_stat)
2695 g_assert (folder_win32->stat_info != NULL);
2697 if (!folder_win32->is_network_dir)
2698 g_hash_table_foreach_remove (folder_win32->stat_info,
2699 cb_fill_in_mime_type,
2702 folder_win32->have_mime_type = TRUE;
2705 static GtkFilePath *
2706 filename_to_path (const char *filename)
2710 tmp = remove_trailing_slash (filename);
2711 return gtk_file_path_new_steal (tmp);
2715 filename_is_root (const char *filename)
2717 const gchar *after_root;
2719 after_root = g_path_skip_root (filename);
2721 return (after_root != NULL && *after_root == '\0');
2725 filename_is_drive_root (const char *filename)
2727 guint len = strlen (filename);
2730 g_ascii_isalpha (filename[0]) &&
2731 filename[1] == ':' &&
2732 G_IS_DIR_SEPARATOR (filename[2]));
2736 filename_is_some_root (const char *filename)
2738 return (g_path_is_absolute (filename) &&
2739 *(g_path_skip_root (filename)) == '\0');
2743 _gtk_file_system_win32_path_compare (const gchar *path1,
2746 while (*path1 && *path2)
2748 gunichar c1 = g_utf8_get_char (path1);
2749 gunichar c2 = g_utf8_get_char (path2);
2751 (G_IS_DIR_SEPARATOR (c1) && G_IS_DIR_SEPARATOR (c1)) ||
2752 g_unichar_toupper (c1) == g_unichar_toupper (c2))
2754 path1 = g_utf8_next_char (path1);
2755 path2 = g_utf8_next_char (path2);
2760 if (!*path1 && !*path2)
2767 return g_unichar_toupper (g_utf8_get_char (path1)) - g_unichar_toupper (g_utf8_get_char (path2));
2770 #define __GTK_FILE_SYSTEM_WIN32_C__
2771 #include "gtkaliasdef.c"