]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilesystemwin32.c
Add a ::group-changed signal emitted when the radio button/menu item is
[~andy/gtk] / gtk / gtkfilesystemwin32.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilesystemwin32.c: Default implementation of GtkFileSystem for Windows
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General 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.
19  */
20
21 #include "gtkfilesystem.h"
22 #include "gtkfilesystemwin32.h"
23 #include "gtkintl.h"
24 #include "gtkstock.h"
25 #include "gtkiconfactory.h"
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
31
32 #ifdef G_OS_WIN32
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
35 #include <shellapi.h> /* ExtractAssociatedIcon */
36 #include <direct.h>
37 #include <io.h>
38 #define mkdir(p,m) _mkdir(p)
39 #include <gdk/win32/gdkwin32.h> /* gdk_win32_hdc_get */
40 #else
41 #error "The implementation is win32 only yet."
42 #endif /* G_OS_WIN32 */
43
44 typedef struct _GtkFileSystemWin32Class GtkFileSystemWin32Class;
45
46 #define GTK_FILE_SYSTEM_WIN32_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_WIN32, GtkFileSystemWin32Class))
47 #define GTK_IS_FILE_SYSTEM_WIN32_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_WIN32))
48 #define GTK_FILE_SYSTEM_WIN32_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_WIN32, GtkFileSystemWin32Class))
49
50 struct _GtkFileSystemWin32Class
51 {
52   GObjectClass parent_class;
53 };
54
55 struct _GtkFileSystemWin32
56 {
57   GObject parent_instance;
58 };
59
60 #define GTK_TYPE_FILE_FOLDER_WIN32             (gtk_file_folder_win32_get_type ())
61 #define GTK_FILE_FOLDER_WIN32(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_FOLDER_WIN32, GtkFileFolderWin32))
62 #define GTK_IS_FILE_FOLDER_WIN32(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_FOLDER_WIN32))
63 #define GTK_FILE_FOLDER_WIN32_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_FOLDER_WIN32, GtkFileFolderWin32Class))
64 #define GTK_IS_FILE_FOLDER_WIN32_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_FOLDER_WIN32))
65 #define GTK_FILE_FOLDER_WIN32_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_FOLDER_WIN32, GtkFileFolderWin32Class))
66
67 typedef struct _GtkFileFolderWin32      GtkFileFolderWin32;
68 typedef struct _GtkFileFolderWin32Class GtkFileFolderWin32Class;
69
70 struct _GtkFileFolderWin32Class
71 {
72   GObjectClass parent_class;
73 };
74
75 struct _GtkFileFolderWin32
76 {
77   GObject parent_instance;
78
79   GtkFileInfoType types;
80   gchar *filename;
81 };
82
83 static GObjectClass *system_parent_class;
84 static GObjectClass *folder_parent_class;
85
86 static void           gtk_file_system_win32_class_init       (GtkFileSystemWin32Class  *class);
87 static void           gtk_file_system_win32_iface_init       (GtkFileSystemIface       *iface);
88 static void           gtk_file_system_win32_init             (GtkFileSystemWin32       *impl);
89 static void           gtk_file_system_win32_finalize         (GObject                  *object);
90
91 static GSList *       gtk_file_system_win32_list_volumes     (GtkFileSystem      *file_system);
92 static GtkFileSystemVolume *gtk_file_system_win32_get_volume_for_path (GtkFileSystem     *file_system,
93                                                                        const GtkFilePath *path);
94
95 static GtkFileFolder *gtk_file_system_win32_get_folder       (GtkFileSystem            *file_system,
96                                                               const GtkFilePath        *path,
97                                                               GtkFileInfoType           types,
98                                                               GError                  **error);
99 static gboolean       gtk_file_system_win32_create_folder    (GtkFileSystem            *file_system,
100                                                               const GtkFilePath        *path,
101                                                               GError                  **error);
102
103 static void         gtk_file_system_win32_volume_free             (GtkFileSystem       *file_system,
104                                                                    GtkFileSystemVolume *volume);
105 static GtkFilePath *gtk_file_system_win32_volume_get_base_path    (GtkFileSystem       *file_system,
106                                                                    GtkFileSystemVolume *volume);
107 static gboolean     gtk_file_system_win32_volume_get_is_mounted   (GtkFileSystem       *file_system,
108                                                                    GtkFileSystemVolume *volume);
109 static gboolean     gtk_file_system_win32_volume_mount            (GtkFileSystem       *file_system,
110                                                                    GtkFileSystemVolume *volume,
111                                                                    GError             **error);
112 static gchar *      gtk_file_system_win32_volume_get_display_name (GtkFileSystem       *file_system,
113                                                                    GtkFileSystemVolume *volume);
114 static GdkPixbuf *  gtk_file_system_win32_volume_render_icon      (GtkFileSystem        *file_system,
115                                                                    GtkFileSystemVolume  *volume,
116                                                                    GtkWidget            *widget,
117                                                                    gint                  pixel_size,
118                                                                    GError              **error);
119
120 static gboolean       gtk_file_system_win32_get_parent       (GtkFileSystem            *file_system,
121                                                               const GtkFilePath        *path,
122                                                               GtkFilePath             **parent,
123                                                               GError                  **error);
124 static GtkFilePath *  gtk_file_system_win32_make_path        (GtkFileSystem            *file_system,
125                                                               const GtkFilePath        *base_path,
126                                                               const gchar              *display_name,
127                                                               GError                  **error);
128 static gboolean       gtk_file_system_win32_parse            (GtkFileSystem            *file_system,
129                                                               const GtkFilePath        *base_path,
130                                                               const gchar              *str,
131                                                               GtkFilePath             **folder,
132                                                               gchar                   **file_part,
133                                                               GError                  **error);
134 static gchar *        gtk_file_system_win32_path_to_uri      (GtkFileSystem            *file_system,
135                                                               const GtkFilePath        *path);
136 static gchar *        gtk_file_system_win32_path_to_filename (GtkFileSystem            *file_system,
137                                                               const GtkFilePath        *path);
138 static GtkFilePath *  gtk_file_system_win32_uri_to_path      (GtkFileSystem            *file_system,
139                                                               const gchar              *uri);
140 static GtkFilePath *  gtk_file_system_win32_filename_to_path (GtkFileSystem            *file_system,
141                                                               const gchar              *filename);
142 static GdkPixbuf *gtk_file_system_win32_render_icon (GtkFileSystem     *file_system,
143                                                      const GtkFilePath *path,
144                                                      GtkWidget         *widget,
145                                                      gint               pixel_size,
146                                                      GError           **error);
147
148 static gboolean       gtk_file_system_win32_add_bookmark     (GtkFileSystem            *file_system,
149                                                               const GtkFilePath        *path,
150                                                               GError                  **error);
151 static gboolean       gtk_file_system_win32_remove_bookmark  (GtkFileSystem            *file_system,
152                                                               const GtkFilePath        *path,
153                                                               GError                  **error);
154 static GSList *       gtk_file_system_win32_list_bookmarks   (GtkFileSystem            *file_system);
155 static GType          gtk_file_folder_win32_get_type         (void);
156 static void           gtk_file_folder_win32_class_init       (GtkFileFolderWin32Class  *class);
157 static void           gtk_file_folder_win32_iface_init       (GtkFileFolderIface       *iface);
158 static void           gtk_file_folder_win32_init             (GtkFileFolderWin32       *impl);
159 static void           gtk_file_folder_win32_finalize         (GObject                  *object);
160 static GtkFileInfo *  gtk_file_folder_win32_get_info         (GtkFileFolder            *folder,
161                                                               const GtkFilePath        *path,
162                                                               GError                  **error);
163 static gboolean       gtk_file_folder_win32_list_children    (GtkFileFolder            *folder,
164                                                               GSList                  **children,
165                                                               GError                  **error);
166
167 static gchar *        filename_from_path                     (const GtkFilePath        *path);
168 static GtkFilePath *  filename_to_path                       (const gchar              *filename);
169
170 static gboolean       filename_is_root                       (const char               *filename);
171 static GtkFileInfo *  filename_get_info                      (const gchar              *filename,
172                                                               GtkFileInfoType           types,
173                                                               GError                  **error);
174
175 /* some info kept together for volumes */
176 struct _GtkFileSystemVolume
177 {
178   gchar    *drive;
179   gboolean  is_mounted;
180 };
181
182 /*
183  * GtkFileSystemWin32
184  */
185 GType
186 gtk_file_system_win32_get_type (void)
187 {
188   static GType file_system_win32_type = 0;
189
190   if (!file_system_win32_type)
191     {
192       static const GTypeInfo file_system_win32_info =
193       {
194         sizeof (GtkFileSystemWin32Class),
195         NULL,           /* base_init */
196         NULL,           /* base_finalize */
197         (GClassInitFunc) gtk_file_system_win32_class_init,
198         NULL,           /* class_finalize */
199         NULL,           /* class_data */
200         sizeof (GtkFileSystemWin32),
201         0,              /* n_preallocs */
202         (GInstanceInitFunc) gtk_file_system_win32_init,
203       };
204       
205       static const GInterfaceInfo file_system_info =
206       {
207         (GInterfaceInitFunc) gtk_file_system_win32_iface_init, /* interface_init */
208         NULL,                                                  /* interface_finalize */
209         NULL                                                   /* interface_data */
210       };
211
212       file_system_win32_type = g_type_register_static (G_TYPE_OBJECT,
213                                                        "GtkFileSystemWin32",
214                                                        &file_system_win32_info, 0);
215       g_type_add_interface_static (file_system_win32_type,
216                                    GTK_TYPE_FILE_SYSTEM,
217                                    &file_system_info);
218     }
219
220   return file_system_win32_type;
221 }
222
223 /**
224  * gtk_file_system_win32_new:
225  * 
226  * Creates a new #GtkFileSystemWin32 object. #GtkFileSystemWin32
227  * implements the #GtkFileSystem interface with direct access to
228  * the filesystem using Windows API calls
229  * 
230  * Return value: the new #GtkFileSystemWin32 object
231  **/
232 GtkFileSystem *
233 gtk_file_system_win32_new (void)
234 {
235   return g_object_new (GTK_TYPE_FILE_SYSTEM_WIN32, NULL);
236 }
237
238 static void
239 gtk_file_system_win32_class_init (GtkFileSystemWin32Class *class)
240 {
241   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
242
243   system_parent_class = g_type_class_peek_parent (class);
244   
245   gobject_class->finalize = gtk_file_system_win32_finalize;
246 }
247
248 static void
249 gtk_file_system_win32_iface_init (GtkFileSystemIface *iface)
250 {
251   iface->list_volumes = gtk_file_system_win32_list_volumes;
252   iface->get_volume_for_path = gtk_file_system_win32_get_volume_for_path;
253   iface->get_folder = gtk_file_system_win32_get_folder;
254   iface->create_folder = gtk_file_system_win32_create_folder;
255   iface->volume_free = gtk_file_system_win32_volume_free;
256   iface->volume_get_base_path = gtk_file_system_win32_volume_get_base_path;
257   iface->volume_get_is_mounted = gtk_file_system_win32_volume_get_is_mounted;
258   iface->volume_mount = gtk_file_system_win32_volume_mount;
259   iface->volume_get_display_name = gtk_file_system_win32_volume_get_display_name;
260   iface->volume_render_icon = gtk_file_system_win32_volume_render_icon;
261   iface->get_parent = gtk_file_system_win32_get_parent;
262   iface->make_path = gtk_file_system_win32_make_path;
263   iface->parse = gtk_file_system_win32_parse;
264   iface->path_to_uri = gtk_file_system_win32_path_to_uri;
265   iface->path_to_filename = gtk_file_system_win32_path_to_filename;
266   iface->uri_to_path = gtk_file_system_win32_uri_to_path;
267   iface->filename_to_path = gtk_file_system_win32_filename_to_path;
268   iface->render_icon = gtk_file_system_win32_render_icon;
269   iface->add_bookmark = gtk_file_system_win32_add_bookmark;
270   iface->remove_bookmark = gtk_file_system_win32_remove_bookmark;
271   iface->list_bookmarks = gtk_file_system_win32_list_bookmarks;
272 }
273
274 static void
275 gtk_file_system_win32_init (GtkFileSystemWin32 *system_win32)
276 {
277 }
278
279 static void
280 gtk_file_system_win32_finalize (GObject *object)
281 {
282   system_parent_class->finalize (object);
283 }
284
285 static GSList *
286 gtk_file_system_win32_list_volumes (GtkFileSystem *file_system)
287 {
288   gchar   drives[26*4];
289   guint   len;
290   gchar  *p;
291   GSList *list = NULL;
292
293   len = GetLogicalDriveStrings(sizeof(drives), drives);
294
295   if (len < 3)
296     g_warning("No drive strings available!");
297
298   p = drives;
299   while ((len = strlen(p)) != 0)
300     {
301       GtkFileSystemVolume *vol = g_new0 (GtkFileSystemVolume, 1);
302       if (p[0] == 'a' || p[0] == 'b')
303         vol->is_mounted = FALSE; /* skip floppy */
304       else
305         vol->is_mounted = TRUE;
306
307       /*FIXME: gtk_file_path_compare() is case sensitive, we are not*/
308       p[0] = toupper (p[0]);
309       vol->drive = g_strdup (p);
310
311       list = g_slist_append (list, vol);
312
313       p += len + 1;
314     }
315   return list;
316 }
317
318 static GtkFileSystemVolume *
319 gtk_file_system_win32_get_volume_for_path (GtkFileSystem     *file_system,
320                                            const GtkFilePath *path)
321 {
322   GtkFileSystemVolume *vol = g_new0 (GtkFileSystemVolume, 1);
323   gchar* p = g_strndup (path, 3);
324
325   g_return_val_if_fail (p != NULL, NULL);
326
327   /*FIXME: gtk_file_path_compare() is case sensitive, we are not*/
328   p[0] = toupper (p[0]);
329   vol->drive = p;
330   vol->is_mounted = (p[0] != 'a' && p[0] != 'b');
331
332   return vol;
333 }
334
335 static GtkFileFolder *
336 gtk_file_system_win32_get_folder (GtkFileSystem    *file_system,
337                                  const GtkFilePath *path,
338                                  GtkFileInfoType    types,
339                                  GError           **error)
340 {
341   GtkFileFolderWin32 *folder_win32;
342   gchar *filename;
343
344   filename = filename_from_path (path);
345   g_return_val_if_fail (filename != NULL, NULL);
346
347   folder_win32 = g_object_new (GTK_TYPE_FILE_FOLDER_WIN32, NULL);
348   folder_win32->filename = filename;
349   folder_win32->types = types;
350
351   return GTK_FILE_FOLDER (folder_win32);
352 }
353
354 static gboolean
355 gtk_file_system_win32_create_folder (GtkFileSystem     *file_system,
356                                      const GtkFilePath *path,
357                                      GError           **error)
358 {
359   gchar *filename;
360   gboolean result;
361
362   filename = filename_from_path (path);
363   g_return_val_if_fail (filename != NULL, FALSE);
364   
365   result = mkdir (filename, 0777) != 0;
366   
367   if (!result)
368     {
369       gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
370       g_set_error (error,
371                    GTK_FILE_SYSTEM_ERROR,
372                    GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
373                    _("error creating directory '%s': %s"),
374                    filename_utf8 ? filename_utf8 : "???",
375                    g_strerror (errno));
376       g_free (filename_utf8);
377     }
378   
379   g_free (filename);
380   
381   return result;
382 }
383
384 static void
385 gtk_file_system_win32_volume_free (GtkFileSystem        *file_system,
386                                    GtkFileSystemVolume  *volume)
387 {
388   g_free (volume->drive);
389   g_free (volume);
390 }
391
392 static GtkFilePath *
393 gtk_file_system_win32_volume_get_base_path (GtkFileSystem        *file_system,
394                                             GtkFileSystemVolume  *volume)
395 {
396   return (GtkFilePath *) g_strndup (volume->drive, 2);
397 }
398
399 static gboolean
400 gtk_file_system_win32_volume_get_is_mounted (GtkFileSystem        *file_system,
401                                              GtkFileSystemVolume  *volume)
402 {
403   return volume->is_mounted;
404 }
405
406 static gboolean
407 gtk_file_system_win32_volume_mount (GtkFileSystem        *file_system, 
408                                     GtkFileSystemVolume  *volume,
409                                     GError              **error)
410 {
411   g_set_error (error,
412                GTK_FILE_SYSTEM_ERROR,
413                GTK_FILE_SYSTEM_ERROR_FAILED,
414                _("This file system does not support mounting"));
415   return FALSE;
416 }
417
418 static gchar *
419 gtk_file_system_win32_volume_get_display_name (GtkFileSystem       *file_system,
420                                               GtkFileSystemVolume *volume)
421 {
422   gchar display_name[80];
423
424   if (GetVolumeInformation (volume->drive, 
425                             display_name, sizeof(display_name),
426                             NULL, /* serial number */
427                             NULL, /* max. component length */
428                             NULL, /* fs flags */
429                             NULL, 0)) /* fs type like FAT, NTFS */
430     {
431       gchar* real_display_name = g_strconcat (display_name, " (", volume->drive, ")", NULL);
432
433       return real_display_name;
434     }
435   else
436     return g_strdup (volume->drive);
437 }
438
439 static GdkPixbuf *
440 gtk_file_system_win32_volume_render_icon (GtkFileSystem        *file_system,
441                                          GtkFileSystemVolume  *volume,
442                                          GtkWidget            *widget,
443                                          gint                  pixel_size,
444                                          GError              **error)
445 {
446   GtkIconSet *icon_set = NULL;
447   DWORD dt = GetDriveType (volume->drive);
448
449   switch (dt)
450     {
451     case DRIVE_REMOVABLE :
452       icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_FLOPPY);
453       break;
454     case DRIVE_CDROM :
455       icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_CDROM);
456       break;
457     case DRIVE_REMOTE :
458       icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_NETWORK);
459       break;
460     case DRIVE_FIXED :
461       icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_HARDDISK);
462       break;
463     case DRIVE_RAMDISK :
464       /*FIXME: need a ram stock icon
465       gtk_file_info_set_icon_type (info, GTK_STOCK_OPEN);*/
466       break;
467     default :
468       g_assert_not_reached ();
469     }
470
471   return gtk_icon_set_render_icon (icon_set, 
472                                    widget->style,
473                                    gtk_widget_get_direction (widget),
474                                    GTK_STATE_NORMAL,
475                                    GTK_ICON_SIZE_BUTTON,
476                                    widget, NULL); 
477 }
478
479 static gboolean
480 gtk_file_system_win32_get_parent (GtkFileSystem     *file_system,
481                                   const GtkFilePath *path,
482                                   GtkFilePath      **parent,
483                                   GError           **error)
484 {
485   gchar *filename = filename_from_path (path);
486   g_return_val_if_fail (filename != NULL, FALSE);
487
488   if (filename_is_root (filename))
489     {
490       *parent = NULL;
491     }
492   else
493     {
494       gchar *parent_filename = g_path_get_dirname (filename);
495       *parent = filename_to_path (parent_filename);
496       g_free (parent_filename);
497     }
498
499   g_free (filename);
500
501   return TRUE;
502 }
503
504 static GtkFilePath *
505 gtk_file_system_win32_make_path (GtkFileSystem     *file_system,
506                                  const GtkFilePath *base_path,
507                                  const gchar       *display_name,
508                                  GError           **error)
509 {
510   gchar *base_filename;
511   gchar *filename;
512   gchar *full_filename;
513   GError *tmp_error = NULL;
514   GtkFilePath *result;
515   
516   base_filename = filename_from_path (base_path);
517   g_return_val_if_fail (base_filename != NULL, NULL);
518
519   filename = g_filename_from_utf8 (display_name, -1, NULL, NULL, &tmp_error);
520   if (!filename)
521     {
522       g_set_error (error,
523                    GTK_FILE_SYSTEM_ERROR,
524                    GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
525                    "%s",
526                    tmp_error->message);
527       
528       g_error_free (tmp_error);
529       g_free (base_filename);
530
531       return NULL;
532     }
533     
534   full_filename = g_build_filename (base_filename, filename, NULL);
535   result = filename_to_path (full_filename);
536   g_free (base_filename);
537   g_free (filename);
538   g_free (full_filename);
539   
540   return result;
541 }
542
543 /* If this was a publically exported function, it should return
544  * a dup'ed result, but we make it modify-in-place for efficiency
545  * here, and because it works for us.
546  */
547 static void
548 canonicalize_filename (gchar *filename)
549 {
550   gchar *p, *q;
551   gboolean last_was_slash = FALSE;
552
553   p = filename;
554   q = filename;
555
556   while (*p)
557     {
558       if (*p == G_DIR_SEPARATOR)
559         {
560           if (!last_was_slash)
561             *q++ = G_DIR_SEPARATOR;
562
563           last_was_slash = TRUE;
564         }
565       else
566         {
567           if (last_was_slash && *p == '.')
568             {
569               if (*(p + 1) == G_DIR_SEPARATOR ||
570                   *(p + 1) == '\0')
571                 {
572                   if (*(p + 1) == '\0')
573                     break;
574                   
575                   p += 1;
576                 }
577               else if (*(p + 1) == '.' &&
578                        (*(p + 2) == G_DIR_SEPARATOR ||
579                         *(p + 2) == '\0'))
580                 {
581                   if (q > filename + 1)
582                     {
583                       q--;
584                       while (q > filename + 1 &&
585                              *(q - 1) != G_DIR_SEPARATOR)
586                         q--;
587                     }
588
589                   if (*(p + 2) == '\0')
590                     break;
591                   
592                   p += 2;
593                 }
594               else
595                 {
596                   *q++ = *p;
597                   last_was_slash = FALSE;
598                 }
599             }
600           else
601             {
602               *q++ = *p;
603               last_was_slash = FALSE;
604             }
605         }
606
607       p++;
608     }
609
610   if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
611     q--;
612
613   *q = '\0';
614 }
615
616 static gboolean
617 gtk_file_system_win32_parse (GtkFileSystem     *file_system,
618                              const GtkFilePath *base_path,
619                              const gchar       *str,
620                              GtkFilePath      **folder,
621                              gchar            **file_part,
622                              GError           **error)
623 {
624   char *base_filename;
625   gchar *last_slash;
626   gboolean result = FALSE;
627
628   base_filename = filename_from_path (base_path);
629   g_return_val_if_fail (base_filename != NULL, FALSE);
630   
631   last_slash = strrchr (str, G_DIR_SEPARATOR);
632   if (!last_slash)
633     {
634       *folder = gtk_file_path_copy (base_path);
635       *file_part = g_strdup (str);
636       result = TRUE;
637     }
638   else
639     {
640       gchar *folder_part;
641       gchar *folder_path;
642       GError *tmp_error = NULL;
643
644       if (last_slash == str)
645         folder_part = g_strdup ("/");
646       else
647         folder_part = g_filename_from_utf8 (str, last_slash - str,
648                                             NULL, NULL, &tmp_error);
649
650       if (!folder_part)
651         {
652           g_set_error (error,
653                        GTK_FILE_SYSTEM_ERROR,
654                        GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
655                        "%s",
656                        tmp_error->message);
657           g_error_free (tmp_error);
658         }
659       else
660         {
661           if (folder_part[1] == ':')
662             folder_path = folder_part;
663           else
664             {
665               folder_path = g_build_filename (base_filename, folder_part, NULL);
666               g_free (folder_part);
667             }
668
669           canonicalize_filename (folder_path);
670           
671           *folder = filename_to_path (folder_path);
672           *file_part = g_strdup (last_slash + 1);
673
674           g_free (folder_path);
675
676           result = TRUE;
677         }
678     }
679
680   g_free (base_filename);
681   
682   return result;
683 }
684
685 static gchar *
686 gtk_file_system_win32_path_to_uri (GtkFileSystem     *file_system,
687                                    const GtkFilePath *path)
688 {
689   return g_filename_to_uri (gtk_file_path_get_string (path), NULL, NULL);
690 }
691
692 static gchar *
693 gtk_file_system_win32_path_to_filename (GtkFileSystem     *file_system,
694                                         const GtkFilePath *path)
695 {
696   return g_strdup (gtk_file_path_get_string (path));
697 }
698
699 static GtkFilePath *
700 gtk_file_system_win32_uri_to_path (GtkFileSystem     *file_system,
701                                    const gchar       *uri)
702 {
703   gchar *filename = g_filename_from_uri (uri, NULL, NULL);
704   if (filename)
705     return gtk_file_path_new_steal (filename);
706   else
707     return NULL;
708 }
709
710 static GtkFilePath *
711 gtk_file_system_win32_filename_to_path (GtkFileSystem *file_system,
712                                         const gchar   *filename)
713 {
714   return gtk_file_path_new_dup (filename);
715 }
716
717 static gboolean
718 bookmarks_serialize (GSList  **bookmarks,
719                      gchar    *uri,
720                      gboolean  add,
721                      GError  **error)
722 {
723   gchar   *filename;
724   gboolean ok = TRUE;
725   GSList  *list = *bookmarks;
726
727   filename = g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
728
729   if (filename)
730     {
731       gchar *contents = NULL;
732       gsize  len = 0;
733       GSList *entry;
734       FILE  *f;   
735        
736       if (g_file_test (filename, G_FILE_TEST_EXISTS))
737         {
738           if (g_file_get_contents (filename, &contents, &len, error))
739             {
740               gchar **lines = g_strsplit (contents, "\n", -1);
741               gint    i;
742
743               for (i = 0; lines[i] != NULL; i++)
744                 {
745                   if (lines[i][0] && !g_slist_find_custom (list, lines[i], strcmp))
746                     list = g_slist_append (list, g_strdup (lines[i]));
747                 }
748               g_strfreev (lines);
749             }
750           else
751             ok = FALSE;
752         }
753       if (ok && (f = fopen (filename, "wb")) != NULL)
754         {
755           for (entry = list; entry != NULL; entry = entry->next)
756             {
757               gchar *line = entry->data;
758
759               if (strcmp (line, uri) != 0)
760                 {
761                   fputs (line, f);
762                   fputs ("\n", f);
763                 }
764             }
765           if (add)
766             {
767               fputs (uri, f);
768               fputs ("\n", f);
769             }
770           fclose (f);
771         }
772       else if (ok && error)
773         {
774           g_set_error (error,
775                        GTK_FILE_SYSTEM_ERROR,
776                        GTK_FILE_SYSTEM_ERROR_FAILED,
777                        _("Bookmark saving failed (%s)"),
778                        g_strerror (errno));
779         }
780     }
781   *bookmarks = list;
782   return ok;
783 }
784
785 static GdkPixbuf*
786 extract_icon (const char* filename)
787 {
788   GdkPixbuf *pixbuf = NULL;
789   WORD iicon;
790   HICON hicon;
791   
792   if (!filename || !filename[0])
793     return NULL;
794
795   hicon = ExtractAssociatedIcon (GetModuleHandle (NULL), filename, &iicon);
796   if (hicon > (HICON)1)
797     {
798       ICONINFO ii;
799
800       if (GetIconInfo (hicon, &ii))
801         {
802           SIZE   size;
803           GdkPixmap *pixmap;
804           GdkGC *gc;
805           HDC    hdc;
806
807           if (!GetBitmapDimensionEx (ii.hbmColor, &size))
808             g_warning ("GetBitmapDimensionEx failed.");
809
810           if (size.cx < 1) size.cx = 32;
811           if (size.cy < 1) size.cy = 32;
812             
813           pixmap = gdk_pixmap_new (NULL, size.cx, size.cy, 
814                                    gdk_screen_get_system_visual (gdk_screen_get_default ())->depth);
815           gc = gdk_gc_new (pixmap);
816           hdc = gdk_win32_hdc_get (GDK_DRAWABLE (pixmap), gc, 0);
817
818           if (!DrawIcon (hdc, 0, 0, hicon))
819             g_warning ("DrawIcon failed");
820
821           gdk_win32_hdc_release (GDK_DRAWABLE (pixmap), gc, 0);
822
823           pixbuf = gdk_pixbuf_get_from_drawable (
824                      NULL, pixmap, 
825                      gdk_screen_get_system_colormap (gdk_screen_get_default ()),
826                      0, 0, 0, 0, size.cx, size.cy);
827           g_object_unref (pixmap);
828           g_object_unref (gc);
829         }
830       else
831         g_print ("GetIconInfo failed: %s\n", g_win32_error_message (GetLastError ())); 
832
833       if (!DestroyIcon (hicon))
834         g_warning ("DestroyIcon failed");
835     }
836   else
837     g_print ("ExtractAssociatedIcon failed: %s\n", g_win32_error_message (GetLastError ()));
838
839   return pixbuf;
840 }
841
842 static GtkIconSet *
843 win32_pseudo_mime_lookup (const char* name)
844 {
845   static GHashTable *mime_hash = NULL;
846   GtkIconSet *is = NULL;
847   char *p = strrchr(name, '.');
848   char *extension = p ? g_ascii_strdown (p, -1) : g_strdup ("");
849
850   if (!mime_hash)
851     mime_hash = g_hash_table_new (g_str_hash, g_str_equal);
852
853   /* do we already have it ? */
854   is = g_hash_table_lookup (mime_hash, extension);
855   if (is)
856     {
857       g_free (extension);
858       return is;
859     }
860   /* create icon and set */
861   {
862     GdkPixbuf *pixbuf = extract_icon (name);
863     if (pixbuf)
864       {
865         GtkIconSource* source = gtk_icon_source_new ();
866
867         is = gtk_icon_set_new_from_pixbuf (pixbuf);
868         gtk_icon_source_set_pixbuf (source, pixbuf);
869         gtk_icon_set_add_source (is, source);
870
871         gtk_icon_source_free (source);
872       }
873
874     g_hash_table_insert (mime_hash, extension, is);
875     return is;
876   }
877 }
878
879 static GdkPixbuf *
880 gtk_file_system_win32_render_icon (GtkFileSystem     *file_system,
881                                    const GtkFilePath *path,
882                                    GtkWidget         *widget,
883                                    gint               pixel_size,
884                                    GError           **error)
885 {
886   GtkIconSet *icon_set = NULL;
887   const char* filename = gtk_file_path_get_string (path);
888
889   /* handle drives with stock icons */
890   if (filename_is_root (filename))
891     {
892       gchar *filename2 = g_strconcat(filename, "\\", NULL);
893       DWORD dt = GetDriveType (filename2);
894
895       switch (dt)
896         {
897         case DRIVE_REMOVABLE :
898           icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_FLOPPY);
899           break;
900         case DRIVE_CDROM :
901           icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_CDROM);
902           break;
903         case DRIVE_FIXED : /* need a hard disk icon */
904           icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_HARDDISK);
905           break;
906         default :
907           break;
908         }
909       g_free (filename2);
910     }
911   else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
912     {
913       if (0 == strcmp (g_get_home_dir(), filename))
914         icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_HOME);
915       else
916         icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_OPEN);
917     }
918   else if (g_file_test (filename, G_FILE_TEST_IS_EXECUTABLE))
919     {
920       /* don't lookup all executable icons */
921       icon_set = gtk_style_lookup_icon_set (widget->style, GTK_STOCK_EXECUTE);
922     }
923   else if (g_file_test (filename, G_FILE_TEST_EXISTS))
924     {
925       icon_set = win32_pseudo_mime_lookup (filename);
926     }
927
928   if (!icon_set)
929     {
930        g_set_error (error,
931                   GTK_FILE_SYSTEM_ERROR,
932                   GTK_FILE_SYSTEM_ERROR_FAILED,
933                   _("This file system does not support icons for everything"));
934        return NULL;
935     }
936
937   // FIXME : I'd like to get from pixel_size (=20) back to
938   // icon size, which is an index, but there appears to be no way ?
939   return gtk_icon_set_render_icon (icon_set, 
940                                    widget->style,
941                                    gtk_widget_get_direction (widget),
942                                    GTK_STATE_NORMAL,
943                                    GTK_ICON_SIZE_BUTTON,
944                                    widget, NULL); 
945 }
946
947 static GSList *_bookmarks = NULL;
948
949 static gboolean
950 gtk_file_system_win32_add_bookmark (GtkFileSystem     *file_system,
951                                     const GtkFilePath *path,
952                                     GError           **error)
953 {
954   gchar *uri = gtk_file_system_win32_path_to_uri (file_system, path);
955   gboolean ret = bookmarks_serialize (&_bookmarks, uri, TRUE, error);
956   g_free (uri);
957   return ret;
958                                
959 }
960
961 static gboolean
962 gtk_file_system_win32_remove_bookmark (GtkFileSystem     *file_system,
963                                        const GtkFilePath *path,
964                                        GError           **error)
965 {
966   gchar *uri = gtk_file_system_win32_path_to_uri (file_system, path);
967   gboolean ret = bookmarks_serialize (&_bookmarks, uri, FALSE, error);
968   g_free (uri);
969   return ret;
970 }
971
972 static GSList *
973 gtk_file_system_win32_list_bookmarks (GtkFileSystem *file_system)
974 {
975   GSList *list = NULL;
976   GSList *entry;
977
978
979   if (bookmarks_serialize (&_bookmarks, "", FALSE, NULL))
980     {
981       for (entry = _bookmarks; entry != NULL; entry = entry->next)
982         {
983           GtkFilePath *path = gtk_file_system_win32_uri_to_path (
984                                 file_system, (gchar *)entry->data);
985
986           list = g_slist_append (list, path);
987         }
988     }
989
990   return list;
991 }
992
993 /*
994  * GtkFileFolderWin32
995  */
996 static GType
997 gtk_file_folder_win32_get_type (void)
998 {
999   static GType file_folder_win32_type = 0;
1000
1001   if (!file_folder_win32_type)
1002     {
1003       static const GTypeInfo file_folder_win32_info =
1004       {
1005         sizeof (GtkFileFolderWin32Class),
1006         NULL,           /* base_init */
1007         NULL,           /* base_finalize */
1008         (GClassInitFunc) gtk_file_folder_win32_class_init,
1009         NULL,           /* class_finalize */
1010         NULL,           /* class_data */
1011         sizeof (GtkFileFolderWin32),
1012         0,              /* n_preallocs */
1013         (GInstanceInitFunc) gtk_file_folder_win32_init,
1014       };
1015       
1016       static const GInterfaceInfo file_folder_info =
1017       {
1018         (GInterfaceInitFunc) gtk_file_folder_win32_iface_init, /* interface_init */
1019         NULL,                                                 /* interface_finalize */
1020         NULL                                                  /* interface_data */
1021       };
1022
1023       file_folder_win32_type = g_type_register_static (G_TYPE_OBJECT,
1024                                                       "GtkFileFolderWin32",
1025                                                       &file_folder_win32_info, 0);
1026       g_type_add_interface_static (file_folder_win32_type,
1027                                    GTK_TYPE_FILE_FOLDER,
1028                                    &file_folder_info);
1029     }
1030
1031   return file_folder_win32_type;
1032 }
1033
1034 static void
1035 gtk_file_folder_win32_class_init (GtkFileFolderWin32Class *class)
1036 {
1037   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1038
1039   folder_parent_class = g_type_class_peek_parent (class);
1040
1041   gobject_class->finalize = gtk_file_folder_win32_finalize;
1042 }
1043
1044 static void
1045 gtk_file_folder_win32_iface_init (GtkFileFolderIface *iface)
1046 {
1047   iface->get_info = gtk_file_folder_win32_get_info;
1048   iface->list_children = gtk_file_folder_win32_list_children;
1049 }
1050
1051 static void
1052 gtk_file_folder_win32_init (GtkFileFolderWin32 *impl)
1053 {
1054 }
1055
1056 static void
1057 gtk_file_folder_win32_finalize (GObject *object)
1058 {
1059   GtkFileFolderWin32 *folder_win32 = GTK_FILE_FOLDER_WIN32 (object);
1060
1061   g_free (folder_win32->filename);
1062   
1063   folder_parent_class->finalize (object);
1064 }
1065
1066 static GtkFileInfo *
1067 gtk_file_folder_win32_get_info (GtkFileFolder     *folder,
1068                                 const GtkFilePath *path,
1069                                 GError           **error)
1070 {
1071   GtkFileFolderWin32 *folder_win32 = GTK_FILE_FOLDER_WIN32 (folder);
1072   GtkFileInfo *info;
1073   gchar *dirname;
1074   gchar *filename;
1075   
1076   filename = filename_from_path (path);
1077   g_return_val_if_fail (filename != NULL, NULL);
1078
1079 #if 0
1080   dirname = g_path_get_dirname (filename);
1081   g_return_val_if_fail (strcmp (dirname, folder_win32->filename) == 0, NULL);
1082   g_free (dirname);
1083 #endif
1084
1085   info = filename_get_info (filename, folder_win32->types, error);
1086
1087   g_free (filename);
1088
1089   return info;
1090 }
1091
1092 static gboolean
1093 gtk_file_folder_win32_list_children (GtkFileFolder  *folder,
1094                                      GSList        **children,
1095                                      GError        **error)
1096 {
1097   GtkFileFolderWin32 *folder_win32 = GTK_FILE_FOLDER_WIN32 (folder);
1098   GError *tmp_error = NULL;
1099   GDir *dir;
1100
1101   *children = NULL;
1102
1103   dir = g_dir_open (folder_win32->filename, 0, &tmp_error);
1104   if (!dir)
1105     {
1106       g_set_error (error,
1107                    GTK_FILE_SYSTEM_ERROR,
1108                    GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
1109                    "%s",
1110                    tmp_error->message);
1111       
1112       g_error_free (tmp_error);
1113
1114       return FALSE;
1115     }
1116
1117   while (TRUE)
1118     {
1119       const gchar *filename = g_dir_read_name (dir);
1120       gchar *fullname;
1121
1122       if (!filename)
1123         break;
1124
1125       fullname = g_build_filename (folder_win32->filename, filename, NULL);
1126       *children = g_slist_prepend (*children, filename_to_path (fullname));
1127       g_free (fullname);
1128     }
1129
1130   g_dir_close (dir);
1131
1132   *children = g_slist_reverse (*children);
1133   
1134   return TRUE;
1135 }
1136
1137 static GtkFileInfo *
1138 filename_get_info (const gchar     *filename,
1139                    GtkFileInfoType  types,
1140                    GError         **error)
1141 {
1142   GtkFileInfo *info;
1143 #if 0 /* it's dead in GtkFileSystemUnix.c, too */
1144   GtkFileIconType icon_type = GTK_FILE_ICON_REGULAR;
1145 #endif
1146   WIN32_FILE_ATTRIBUTE_DATA wfad;
1147
1148   if (!GetFileAttributesEx (filename, GetFileExInfoStandard, &wfad))
1149     {
1150       gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
1151       g_set_error (error,
1152                    GTK_FILE_SYSTEM_ERROR,
1153                    GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
1154                    _("error getting information for '%s': %s"),
1155                    filename_utf8 ? filename_utf8 : "???",
1156                    g_win32_error_message (GetLastError ()));
1157       g_free (filename_utf8);
1158       
1159       return NULL;
1160     }
1161
1162   info = gtk_file_info_new ();
1163
1164   if (filename_is_root (filename))
1165     {
1166       if (types & GTK_FILE_INFO_DISPLAY_NAME)
1167         gtk_file_info_set_display_name (info, filename);
1168       
1169       if (types & GTK_FILE_INFO_IS_HIDDEN)
1170         gtk_file_info_set_is_hidden (info, FALSE);
1171     }
1172   else
1173     {
1174       gchar *basename = g_path_get_basename (filename);
1175   
1176       if (types & GTK_FILE_INFO_DISPLAY_NAME)
1177         {
1178           gchar *display_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
1179           if (!display_name)
1180             display_name = g_strescape (basename, NULL);
1181           
1182           gtk_file_info_set_display_name (info, display_name);
1183           
1184           g_free (display_name);
1185         }
1186       
1187       if (types & GTK_FILE_INFO_IS_HIDDEN)
1188         {
1189             /* win32 convention ... */
1190             gboolean is_hidden = basename[0] == '.';
1191             /* ... _and_ windoze attribute */
1192             is_hidden = is_hidden || !!(wfad.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
1193           gtk_file_info_set_is_hidden (info, is_hidden);
1194         }
1195
1196       g_free (basename);
1197     }
1198
1199   if (types & GTK_FILE_INFO_IS_FOLDER)
1200     {
1201       gtk_file_info_set_is_folder (info, !!(wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
1202    }
1203
1204 #if 0 /* it's dead in GtkFileSystemUnix.c, too */
1205   if (types & GTK_FILE_INFO_ICON)
1206     {
1207       if (wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1208         icon_type = GTK_FILE_ICON_DIRECTORY;
1209
1210       gtk_file_info_set_icon_type (info, icon_type);
1211     }
1212 #endif
1213
1214   if ((types & GTK_FILE_INFO_MIME_TYPE)
1215 #if 0 /* it's dead in GtkFileSystemUnix.c, too */
1216       || ((types & GTK_FILE_INFO_ICON) && icon_type == GTK_FILE_ICON_REGULAR)
1217 #endif
1218      )
1219     {
1220 #if 0
1221       const char *mime_type = xdg_mime_get_mime_type_for_file (filename);
1222       gtk_file_info_set_mime_type (info, mime_type);
1223
1224       if ((types & GTK_FILE_INFO_ICON) && icon_type == GTK_FILE_ICON_REGULAR &&
1225           (statbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) &&
1226           (strcmp (mime_type, XDG_MIME_TYPE_UNKNOWN) == 0 ||
1227            strcmp (mime_type, "application/x-executable") == 0 ||
1228            strcmp (mime_type, "application/x-shellscript") == 0))
1229         gtk_file_info_set_icon_type (info, GTK_FILE_ICON_EXECUTABLE);
1230 #endif
1231     }
1232
1233   if (types & GTK_FILE_INFO_MODIFICATION_TIME)
1234     {
1235       GtkFileTime time = wfad.ftLastWriteTime.dwLowDateTime 
1236                        | ((guint64)wfad.ftLastWriteTime.dwHighDateTime) << 32;
1237       /* 100-nanosecond intervals since January 1, 1601, urgh! */
1238       time /= 10000000I64; /* now seconds */
1239       time -= 134774I64 * 24 * 3600; /* good old Unix time */
1240       gtk_file_info_set_modification_time (info, time);
1241     }
1242
1243   if (types & GTK_FILE_INFO_SIZE)
1244     {
1245       gint64 size = wfad.nFileSizeLow | ((guint64)wfad.nFileSizeHigh) << 32;
1246       gtk_file_info_set_size (info, size);
1247     }
1248   
1249   return info;
1250 }
1251
1252 static gchar *
1253 filename_from_path (const GtkFilePath *path)
1254 {
1255   return g_strdup (gtk_file_path_get_string (path));
1256 }
1257
1258 static GtkFilePath *
1259 filename_to_path (const char *filename)
1260 {
1261   return gtk_file_path_new_dup (filename);
1262 }
1263
1264 static gboolean
1265 filename_is_root (const char *filename)
1266 {
1267   guint len = strlen(filename);
1268
1269   /* accept both forms */
1270
1271   return (   (len == 2 && filename[1] == ':')
1272           || (len == 3 && filename[1] == ':' && filename[2] == '\\'));
1273 }
1274