gtkfilesystemunix.c 24 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* GTK - The GIMP Toolkit
 * gtkfilesystemunix.c: Default implementation of GtkFileSystem for UNIX-like systems
 * Copyright (C) 2003, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gtkfilesystem.h"
#include "gtkfilesystemunix.h"
23
#include "gtkintl.h"
Owen Taylor's avatar
Owen Taylor committed
24

25
#define XDG_PREFIX _gtk_xdg
Owen Taylor's avatar
Owen Taylor committed
26
27
#include "xdgmime/xdgmime.h"

Owen Taylor's avatar
Owen Taylor committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct _GtkFileSystemUnixClass GtkFileSystemUnixClass;

#define GTK_FILE_SYSTEM_UNIX_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_UNIX, GtkFileSystemUnixClass))
#define GTK_IS_FILE_SYSTEM_UNIX_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_UNIX))
#define GTK_FILE_SYSTEM_UNIX_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_UNIX, GtkFileSystemUnixClass))

struct _GtkFileSystemUnixClass
{
  GObjectClass parent_class;
};

struct _GtkFileSystemUnix
{
  GObject parent_instance;
};

#define GTK_TYPE_FILE_FOLDER_UNIX             (gtk_file_folder_unix_get_type ())
#define GTK_FILE_FOLDER_UNIX(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnix))
#define GTK_IS_FILE_FOLDER_UNIX(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_FOLDER_UNIX))
#define GTK_FILE_FOLDER_UNIX_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnixClass))
#define GTK_IS_FILE_FOLDER_UNIX_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_FOLDER_UNIX))
#define GTK_FILE_FOLDER_UNIX_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_FOLDER_UNIX, GtkFileFolderUnixClass))

typedef struct _GtkFileFolderUnix      GtkFileFolderUnix;
typedef struct _GtkFileFolderUnixClass GtkFileFolderUnixClass;

struct _GtkFileFolderUnixClass
{
  GObjectClass parent_class;
};

struct _GtkFileFolderUnix
{
  GObject parent_instance;

  GtkFileInfoType types;
  gchar *filename;
};

Owen Taylor's avatar
Owen Taylor committed
73
74
static GObjectClass *system_parent_class;
static GObjectClass *folder_parent_class;
75

Owen Taylor's avatar
Owen Taylor committed
76
77
78
79
80
static void gtk_file_system_unix_class_init   (GtkFileSystemUnixClass *class);
static void gtk_file_system_unix_iface_init   (GtkFileSystemIface     *iface);
static void gtk_file_system_unix_init         (GtkFileSystemUnix      *impl);
static void gtk_file_system_unix_finalize     (GObject                *object);

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
static GSList *       gtk_file_system_unix_list_roots    (GtkFileSystem      *file_system);
static GtkFileInfo *  gtk_file_system_unix_get_root_info (GtkFileSystem      *file_system,
							  const GtkFilePath  *path,
							  GtkFileInfoType     types,
							  GError            **error);
static GtkFileFolder *gtk_file_system_unix_get_folder    (GtkFileSystem      *file_system,
							  const GtkFilePath  *path,
							  GtkFileInfoType     types,
							  GError            **error);
static gboolean       gtk_file_system_unix_create_folder (GtkFileSystem      *file_system,
							  const GtkFilePath  *path,
							  GError            **error);
static gboolean       gtk_file_system_unix_get_parent    (GtkFileSystem      *file_system,
							  const GtkFilePath  *path,
							  GtkFilePath       **parent,
							  GError            **error);
static GtkFilePath *  gtk_file_system_unix_make_path     (GtkFileSystem      *file_system,
							  const GtkFilePath  *base_path,
							  const gchar        *display_name,
							  GError            **error);
static gboolean       gtk_file_system_unix_parse         (GtkFileSystem      *file_system,
							  const GtkFilePath  *base_path,
							  const gchar        *str,
							  GtkFilePath       **folder,
							  gchar             **file_part,
							  GError            **error);

static gchar *      gtk_file_system_unix_path_to_uri      (GtkFileSystem     *file_system,
							   const GtkFilePath *path);
static gchar *      gtk_file_system_unix_path_to_filename (GtkFileSystem     *file_system,
							   const GtkFilePath *path);
static GtkFilePath *gtk_file_system_unix_uri_to_path      (GtkFileSystem     *file_system,
							   const gchar       *uri);
static GtkFilePath *gtk_file_system_unix_filename_to_path (GtkFileSystem     *file_system,
							   const gchar       *filename);
Owen Taylor's avatar
Owen Taylor committed
116

117
118
119
120
121
122
static GdkPixbuf *gtk_file_system_unix_render_icon (GtkFileSystem     *file_system,
						    const GtkFilePath *path,
						    GtkWidget         *widget,
						    gint               pixel_size,
						    GError           **error);

123
124
125
126
127
128
129
static gboolean gtk_file_system_unix_add_bookmark    (GtkFileSystem     *file_system,
						      const GtkFilePath *path,
						      GError           **error);
static gboolean gtk_file_system_unix_remove_bookmark (GtkFileSystem     *file_system,
						      const GtkFilePath *path,
						      GError           **error);
static GSList * gtk_file_system_unix_list_bookmarks  (GtkFileSystem *file_system);
130

Owen Taylor's avatar
Owen Taylor committed
131
132
133
134
135
136
137
static GType gtk_file_folder_unix_get_type   (void);
static void  gtk_file_folder_unix_class_init (GtkFileFolderUnixClass *class);
static void  gtk_file_folder_unix_iface_init (GtkFileFolderIface     *iface);
static void  gtk_file_folder_unix_init       (GtkFileFolderUnix      *impl);
static void  gtk_file_folder_unix_finalize   (GObject                *object);

static GtkFileInfo *gtk_file_folder_unix_get_info      (GtkFileFolder  *folder,
138
							const GtkFilePath    *path,
Owen Taylor's avatar
Owen Taylor committed
139
140
141
142
143
							GError        **error);
static gboolean     gtk_file_folder_unix_list_children (GtkFileFolder  *folder,
							GSList        **children,
							GError        **error);

144
145
146
static gchar *      filename_from_path (const GtkFilePath *path);
static GtkFilePath *filename_to_path   (const gchar       *filename);

Owen Taylor's avatar
Owen Taylor committed
147
148
149
150
151
152
153
154
155
static gboolean     filename_is_root  (const char       *filename);
static GtkFileInfo *filename_get_info (const gchar      *filename,
				       GtkFileInfoType   types,
				       GError          **error);

/*
 * GtkFileSystemUnix
 */
GType
Owen Taylor's avatar
Owen Taylor committed
156
gtk_file_system_unix_get_type (void)
Owen Taylor's avatar
Owen Taylor committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{
  static GType file_system_unix_type = 0;

  if (!file_system_unix_type)
    {
      static const GTypeInfo file_system_unix_info =
      {
	sizeof (GtkFileSystemUnixClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_file_system_unix_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkFileSystemUnix),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_file_system_unix_init,
      };
      
      static const GInterfaceInfo file_system_info =
      {
	(GInterfaceInitFunc) gtk_file_system_unix_iface_init, /* interface_init */
	NULL,			                              /* interface_finalize */
	NULL			                              /* interface_data */
      };

      file_system_unix_type = g_type_register_static (G_TYPE_OBJECT,
						      "GtkFileSystemUnix",
						      &file_system_unix_info, 0);
      g_type_add_interface_static (file_system_unix_type,
				   GTK_TYPE_FILE_SYSTEM,
				   &file_system_info);
    }

  return file_system_unix_type;
}

193
/**
Owen Taylor's avatar
Owen Taylor committed
194
 * gtk_file_system_unix_new:
195
196
197
198
199
200
201
202
 * 
 * Creates a new #GtkFileSystemUnix object. #GtkFileSystemUnix
 * implements the #GtkFileSystem interface with direct access to
 * the filesystem using Unix/Linux API calls
 * 
 * Return value: the new #GtkFileSystemUnix object
 **/
GtkFileSystem *
Owen Taylor's avatar
Owen Taylor committed
203
gtk_file_system_unix_new (void)
204
205
206
207
{
  return g_object_new (GTK_TYPE_FILE_SYSTEM_UNIX, NULL);
}

Owen Taylor's avatar
Owen Taylor committed
208
209
210
211
static void
gtk_file_system_unix_class_init (GtkFileSystemUnixClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
212
213

  system_parent_class = g_type_class_peek_parent (class);
Owen Taylor's avatar
Owen Taylor committed
214
215
216
217
218
219
220
221
222
223
224
225
  
  gobject_class->finalize = gtk_file_system_unix_finalize;
}

static void
gtk_file_system_unix_iface_init   (GtkFileSystemIface *iface)
{
  iface->list_roots = gtk_file_system_unix_list_roots;
  iface->get_folder = gtk_file_system_unix_get_folder;
  iface->get_root_info = gtk_file_system_unix_get_root_info;
  iface->create_folder = gtk_file_system_unix_create_folder;
  iface->get_parent = gtk_file_system_unix_get_parent;
226
  iface->make_path = gtk_file_system_unix_make_path;
227
  iface->parse = gtk_file_system_unix_parse;
228
229
230
231
  iface->path_to_uri = gtk_file_system_unix_path_to_uri;
  iface->path_to_filename = gtk_file_system_unix_path_to_filename;
  iface->uri_to_path = gtk_file_system_unix_uri_to_path;
  iface->filename_to_path = gtk_file_system_unix_filename_to_path;
232
  iface->render_icon = gtk_file_system_unix_render_icon;
233
234
  iface->add_bookmark = gtk_file_system_unix_add_bookmark;
  iface->remove_bookmark = gtk_file_system_unix_remove_bookmark;
235
  iface->list_bookmarks = gtk_file_system_unix_list_bookmarks;
Owen Taylor's avatar
Owen Taylor committed
236
237
238
239
240
241
242
243
244
245
}

static void
gtk_file_system_unix_init (GtkFileSystemUnix *system_unix)
{
}

static void
gtk_file_system_unix_finalize (GObject *object)
{
246
  system_parent_class->finalize (object);
Owen Taylor's avatar
Owen Taylor committed
247
248
249
250
251
}

static GSList *
gtk_file_system_unix_list_roots (GtkFileSystem *file_system)
{
252
  return g_slist_append (NULL, gtk_file_path_new_dup ("/"));
Owen Taylor's avatar
Owen Taylor committed
253
254
255
256
}

static GtkFileInfo *
gtk_file_system_unix_get_root_info (GtkFileSystem    *file_system,
257
				    const GtkFilePath      *path,
Owen Taylor's avatar
Owen Taylor committed
258
259
260
				    GtkFileInfoType   types,
				    GError          **error)
{
261
262
263
  const gchar *filename = gtk_file_path_get_string (path);
  
  g_return_val_if_fail (strcmp (filename, "/") == 0, NULL);
Owen Taylor's avatar
Owen Taylor committed
264
265
266
267
268

  return filename_get_info ("/", types, error);
}

static GtkFileFolder *
269
270
271
272
gtk_file_system_unix_get_folder (GtkFileSystem     *file_system,
				 const GtkFilePath *path,
				 GtkFileInfoType    types,
				 GError           **error)
Owen Taylor's avatar
Owen Taylor committed
273
274
275
276
{
  GtkFileFolderUnix *folder_unix;
  gchar *filename;

277
278
  filename = filename_from_path (path);
  g_return_val_if_fail (filename != NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
279
280
281
282
283
284
285
286
287

  folder_unix = g_object_new (GTK_TYPE_FILE_FOLDER_UNIX, NULL);
  folder_unix->filename = filename;
  folder_unix->types = types;

  return GTK_FILE_FOLDER (folder_unix);
}

static gboolean
288
289
290
gtk_file_system_unix_create_folder (GtkFileSystem     *file_system,
				    const GtkFilePath *path,
				    GError           **error)
Owen Taylor's avatar
Owen Taylor committed
291
292
293
294
{
  gchar *filename;
  gboolean result;

295
296
297
  filename = filename_from_path (path);
  g_return_val_if_fail (filename != NULL, FALSE);
  
Owen Taylor's avatar
Owen Taylor committed
298
299
300
301
302
303
304
  result = mkdir (filename, 0777) != 0;
  
  if (!result)
    {
      gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
      g_set_error (error,
		   GTK_FILE_SYSTEM_ERROR,
305
		   GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
306
		   _("error creating directory '%s': %s"),
Owen Taylor's avatar
Owen Taylor committed
307
308
309
310
311
312
313
314
315
316
317
		   filename_utf8 ? filename_utf8 : "???",
		   g_strerror (errno));
      g_free (filename_utf8);
    }
  
  g_free (filename);
  
  return result;
}

static gboolean
318
319
320
321
gtk_file_system_unix_get_parent (GtkFileSystem     *file_system,
				 const GtkFilePath *path,
				 GtkFilePath      **parent,
				 GError           **error)
Owen Taylor's avatar
Owen Taylor committed
322
{
323
324
  gchar *filename = filename_from_path (path);
  g_return_val_if_fail (filename != NULL, FALSE);
Owen Taylor's avatar
Owen Taylor committed
325
326
327
328
329
330
331
332

  if (filename_is_root (filename))
    {
      *parent = NULL;
    }
  else
    {
      gchar *parent_filename = g_path_get_dirname (filename);
333
      *parent = filename_to_path (parent_filename);
Owen Taylor's avatar
Owen Taylor committed
334
335
336
337
338
339
340
341
      g_free (parent_filename);
    }

  g_free (filename);

  return TRUE;
}

342
343
344
345
346
static GtkFilePath *
gtk_file_system_unix_make_path (GtkFileSystem    *file_system,
			       const GtkFilePath *base_path,
			       const gchar       *display_name,
			       GError           **error)
Owen Taylor's avatar
Owen Taylor committed
347
348
349
350
351
{
  gchar *base_filename;
  gchar *filename;
  gchar *full_filename;
  GError *tmp_error = NULL;
352
  GtkFilePath *result;
Owen Taylor's avatar
Owen Taylor committed
353
  
354
355
  base_filename = filename_from_path (base_path);
  g_return_val_if_fail (base_filename != NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

  filename = g_filename_from_utf8 (display_name, -1, NULL, NULL, &tmp_error);
  if (!filename)
    {
      g_set_error (error,
		   GTK_FILE_SYSTEM_ERROR,
		   GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
		   "%s",
		   tmp_error->message);
      
      g_error_free (tmp_error);
      g_free (base_filename);

      return FALSE;
    }
    
  full_filename = g_build_filename (base_filename, filename, NULL);
373
  result = filename_to_path (full_filename);
Owen Taylor's avatar
Owen Taylor committed
374
375
376
377
  g_free (base_filename);
  g_free (filename);
  g_free (full_filename);
  
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  return result;
}

/* If this was a publically exported function, it should return
 * a dup'ed result, but we make it modify-in-place for efficiency
 * here, and because it works for us.
 */
static void
canonicalize_filename (gchar *filename)
{
  gchar *p, *q;
  gboolean last_was_slash = FALSE;

  p = filename;
  q = filename;

  while (*p)
    {
      if (*p == G_DIR_SEPARATOR)
	{
	  if (!last_was_slash)
	    *q++ = G_DIR_SEPARATOR;

	  last_was_slash = TRUE;
	}
      else
	{
	  if (last_was_slash && *p == '.')
	    {
	      if (*(p + 1) == G_DIR_SEPARATOR ||
		  *(p + 1) == '\0')
		{
		  if (*(p + 1) == '\0')
		    break;
		  
		  p += 1;
		}
	      else if (*(p + 1) == '.' &&
		       (*(p + 2) == G_DIR_SEPARATOR ||
			*(p + 2) == '\0'))
		{
		  if (q > filename + 1)
		    {
		      q--;
		      while (q > filename + 1 &&
			     *(q - 1) != G_DIR_SEPARATOR)
			q--;
		    }

		  if (*(p + 2) == '\0')
		    break;
		  
		  p += 2;
		}
	      else
		{
		  *q++ = *p;
		  last_was_slash = FALSE;
		}
	    }
	  else
	    {
	      *q++ = *p;
	      last_was_slash = FALSE;
	    }
	}

      p++;
    }

  if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
    q--;

  *q = '\0';
}

static gboolean
455
456
457
458
459
460
gtk_file_system_unix_parse (GtkFileSystem     *file_system,
			    const GtkFilePath *base_path,
			    const gchar       *str,
			    GtkFilePath      **folder,
			    gchar            **file_part,
			    GError           **error)
461
462
463
464
465
{
  char *base_filename;
  gchar *last_slash;
  gboolean result = FALSE;

466
467
  base_filename = filename_from_path (base_path);
  g_return_val_if_fail (base_filename != NULL, FALSE);
468
469
470
471
  
  last_slash = strrchr (str, G_DIR_SEPARATOR);
  if (!last_slash)
    {
472
      *folder = gtk_file_path_copy (base_path);
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
      *file_part = g_strdup (str);
      result = TRUE;
    }
  else
    {
      gchar *folder_part;
      gchar *folder_path;
      GError *tmp_error = NULL;

      if (last_slash == str)
	folder_part = g_strdup ("/");
      else
	folder_part = g_filename_from_utf8 (str, last_slash - str,
					    NULL, NULL, &tmp_error);

      if (!folder_part)
	{
	  g_set_error (error,
		       GTK_FILE_SYSTEM_ERROR,
		       GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
		       "%s",
		       tmp_error->message);
	  g_error_free (tmp_error);
	}
      else
	{
	  if (folder_part[0] == G_DIR_SEPARATOR)
	    folder_path = folder_part;
	  else
	    {
	      folder_path = g_build_filename (base_filename, folder_part, NULL);
	      g_free (folder_part);
	    }

	  canonicalize_filename (folder_path);
	  
509
	  *folder = filename_to_path (folder_path);
510
511
512
513
514
515
516
517
518
519
	  *file_part = g_strdup (last_slash + 1);

	  g_free (folder_path);

	  result = TRUE;
	}
    }

  g_free (base_filename);
  
Owen Taylor's avatar
Owen Taylor committed
520
521
522
  return result;
}

523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
static gchar *
gtk_file_system_unix_path_to_uri (GtkFileSystem     *file_system,
				  const GtkFilePath *path)
{
  return g_filename_to_uri (gtk_file_path_get_string (path), NULL, NULL);
}

static gchar *
gtk_file_system_unix_path_to_filename (GtkFileSystem     *file_system,
				       const GtkFilePath *path)
{
  return g_strdup (gtk_file_path_get_string (path));
}

static GtkFilePath *
gtk_file_system_unix_uri_to_path (GtkFileSystem     *file_system,
				  const gchar       *uri)
{
  gchar *filename = g_filename_from_uri (uri, NULL, NULL);
  if (filename)
    return gtk_file_path_new_steal (filename);
  else
    return NULL;
}

static GtkFilePath *
gtk_file_system_unix_filename_to_path (GtkFileSystem *file_system,
				       const gchar   *filename)
{
  return gtk_file_path_new_dup (filename);
}

555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
static GdkPixbuf *
gtk_file_system_unix_render_icon (GtkFileSystem     *file_system,
				  const GtkFilePath *path,
				  GtkWidget         *widget,
				  gint               pixel_size,
				  GError           **error)
{
  /* FIXME: Implement this */
  g_set_error (error,
	       GTK_FILE_SYSTEM_ERROR,
	       GTK_FILE_SYSTEM_ERROR_FAILED,
	       _("This file system does not support icons"));
  return NULL;
}

570
static gboolean
571
572
573
gtk_file_system_unix_add_bookmark (GtkFileSystem     *file_system,
				   const GtkFilePath *path,
				   GError           **error)
574
{
575
  /* FIXME: Implement as a really simple ~/.gtk-bookmarks */
576
577
578
  g_set_error (error,
	       GTK_FILE_SYSTEM_ERROR,
	       GTK_FILE_SYSTEM_ERROR_FAILED,
579
	       _("This file system does not support bookmarks"));
580
581
582
  return FALSE;
}

583
584
585
586
static gboolean
gtk_file_system_unix_remove_bookmark (GtkFileSystem     *file_system,
				      const GtkFilePath *path,
				      GError           **error)
587
{
588
  /* FIXME: Implement as a really simple ~/.gtk-bookmarks */
589
590
591
  g_set_error (error,
	       GTK_FILE_SYSTEM_ERROR,
	       GTK_FILE_SYSTEM_ERROR_FAILED,
592
	       _("This file system does not support bookmarks"));
593
  return FALSE;
594
595
596
597
598
}

static GSList *
gtk_file_system_unix_list_bookmarks (GtkFileSystem *file_system)
{
599
  /* FIXME: Implement as a really simple ~/.gtk-bookmarks */
600
601
602
  return NULL;
}

Owen Taylor's avatar
Owen Taylor committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * GtkFileFolderUnix
 */
static GType
gtk_file_folder_unix_get_type (void)
{
  static GType file_folder_unix_type = 0;

  if (!file_folder_unix_type)
    {
      static const GTypeInfo file_folder_unix_info =
      {
	sizeof (GtkFileFolderUnixClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_file_folder_unix_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkFileFolderUnix),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_file_folder_unix_init,
      };
      
      static const GInterfaceInfo file_folder_info =
      {
	(GInterfaceInitFunc) gtk_file_folder_unix_iface_init, /* interface_init */
	NULL,			                              /* interface_finalize */
	NULL			                              /* interface_data */
      };

      file_folder_unix_type = g_type_register_static (G_TYPE_OBJECT,
						      "GtkFileFolderUnix",
						      &file_folder_unix_info, 0);
      g_type_add_interface_static (file_folder_unix_type,
				   GTK_TYPE_FILE_FOLDER,
				   &file_folder_info);
    }

  return file_folder_unix_type;
}

static void
gtk_file_folder_unix_class_init (GtkFileFolderUnixClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

649
650
  folder_parent_class = g_type_class_peek_parent (class);

Owen Taylor's avatar
Owen Taylor committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
  gobject_class->finalize = gtk_file_folder_unix_finalize;
}

static void
gtk_file_folder_unix_iface_init (GtkFileFolderIface *iface)
{
  iface->get_info = gtk_file_folder_unix_get_info;
  iface->list_children = gtk_file_folder_unix_list_children;
}

static void
gtk_file_folder_unix_init (GtkFileFolderUnix *impl)
{
}

static void
gtk_file_folder_unix_finalize (GObject *object)
{
  GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (object);

  g_free (folder_unix->filename);
672
673
  
  folder_parent_class->finalize (object);
Owen Taylor's avatar
Owen Taylor committed
674
675
676
677
}

static GtkFileInfo *
gtk_file_folder_unix_get_info (GtkFileFolder  *folder,
678
			       const GtkFilePath    *path,
Owen Taylor's avatar
Owen Taylor committed
679
680
681
682
683
684
685
			       GError        **error)
{
  GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (folder);
  GtkFileInfo *info;
  gchar *dirname;
  gchar *filename;
  
686
687
  filename = filename_from_path (path);
  g_return_val_if_fail (filename != NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715

  dirname = g_path_get_dirname (filename);
  g_return_val_if_fail (strcmp (dirname, folder_unix->filename) == 0, NULL);
  g_free (dirname);

  info = filename_get_info (filename, folder_unix->types, error);

  g_free (filename);

  return info;
}

static gboolean
gtk_file_folder_unix_list_children (GtkFileFolder  *folder,
				    GSList        **children,
				    GError        **error)
{
  GtkFileFolderUnix *folder_unix = GTK_FILE_FOLDER_UNIX (folder);
  GError *tmp_error = NULL;
  GDir *dir;

  *children = NULL;

  dir = g_dir_open (folder_unix->filename, 0, &tmp_error);
  if (!dir)
    {
      g_set_error (error,
		   GTK_FILE_SYSTEM_ERROR,
716
		   GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
Owen Taylor's avatar
Owen Taylor committed
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
		   "%s",
		   tmp_error->message);
      
      g_error_free (tmp_error);

      return FALSE;
    }

  while (TRUE)
    {
      const gchar *filename = g_dir_read_name (dir);
      gchar *fullname;

      if (!filename)
	break;

      fullname = g_build_filename (folder_unix->filename, filename, NULL);
734
      *children = g_slist_prepend (*children, filename_to_path (fullname));
Owen Taylor's avatar
Owen Taylor committed
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
      g_free (fullname);
    }

  g_dir_close (dir);

  *children = g_slist_reverse (*children);
  
  return TRUE;
}

static GtkFileInfo *
filename_get_info (const gchar     *filename,
		   GtkFileInfoType  types,
		   GError         **error)
{
  GtkFileInfo *info;
751
#if 0
Owen Taylor's avatar
Owen Taylor committed
752
  GtkFileIconType icon_type = GTK_FILE_ICON_REGULAR;
753
#endif
Owen Taylor's avatar
Owen Taylor committed
754
755
756
757
758
759
760
761
762
763
  struct stat statbuf;
  
  /* If stat fails, try to fall back to lstat to catch broken links
   */
  if (stat (filename, &statbuf) != 0 &&
      lstat (filename, &statbuf) != 0)
    {
      gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
      g_set_error (error,
		   GTK_FILE_SYSTEM_ERROR,
764
		   GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
765
		   _("error getting information for '%s': %s"),
Owen Taylor's avatar
Owen Taylor committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
		   filename_utf8 ? filename_utf8 : "???",
		   g_strerror (errno));
      g_free (filename_utf8);
      
      return NULL;
    }

  info = gtk_file_info_new ();

  if (filename_is_root (filename))
    {
      if (types & GTK_FILE_INFO_DISPLAY_NAME)
	gtk_file_info_set_display_name (info, "/");
      
      if (types & GTK_FILE_INFO_IS_HIDDEN)
	gtk_file_info_set_is_hidden (info, FALSE);
    }
  else
    {
      gchar *basename = g_path_get_basename (filename);
  
      if (types & GTK_FILE_INFO_DISPLAY_NAME)
	{
	  gchar *display_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
	  if (!display_name)
	    display_name = g_strescape (basename, NULL);
	  
	  gtk_file_info_set_display_name (info, display_name);
	  
	  g_free (display_name);
	}
      
      if (types & GTK_FILE_INFO_IS_HIDDEN)
	{
	  gtk_file_info_set_is_hidden (info, basename[0] == '.');
	}

      g_free (basename);
    }

  if (types & GTK_FILE_INFO_IS_FOLDER)
    {
      gtk_file_info_set_is_folder (info, S_ISDIR (statbuf.st_mode));
   }

811
#if 0
Owen Taylor's avatar
Owen Taylor committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
  if (types & GTK_FILE_INFO_ICON)
    {
      if (S_ISBLK (statbuf.st_mode))
	icon_type = GTK_FILE_ICON_BLOCK_DEVICE;
      else if (S_ISLNK (statbuf.st_mode))
	icon_type = GTK_FILE_ICON_BROKEN_SYMBOLIC_LINK;
      else if (S_ISCHR (statbuf.st_mode))
	icon_type = GTK_FILE_ICON_CHARACTER_DEVICE;
      else if (S_ISDIR (statbuf.st_mode))
	icon_type = GTK_FILE_ICON_DIRECTORY;
      else if (S_ISFIFO (statbuf.st_mode))
	icon_type =  GTK_FILE_ICON_FIFO;
      else if (S_ISSOCK (statbuf.st_mode))
	icon_type = GTK_FILE_ICON_SOCKET;

      gtk_file_info_set_icon_type (info, icon_type);
    }

  if ((types & GTK_FILE_INFO_MIME_TYPE) ||
      ((types & GTK_FILE_INFO_ICON) && icon_type == GTK_FILE_ICON_REGULAR))
832
833
834
#else
  if (types & GTK_FILE_INFO_MIME_TYPE)
#endif
Owen Taylor's avatar
Owen Taylor committed
835
    {
Owen Taylor's avatar
Owen Taylor committed
836
837
838
      const char *mime_type = xdg_mime_get_mime_type_for_file (filename);
      gtk_file_info_set_mime_type (info, mime_type);

839
#if 0
Owen Taylor's avatar
Owen Taylor committed
840
841
842
843
844
845
      if ((types & GTK_FILE_INFO_ICON) && icon_type == GTK_FILE_ICON_REGULAR &&
	  (statbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) &&
	  (strcmp (mime_type, XDG_MIME_TYPE_UNKNOWN) == 0 ||
	   strcmp (mime_type, "application/x-executable") == 0 ||
	   strcmp (mime_type, "application/x-shellscript") == 0))
	gtk_file_info_set_icon_type (info, GTK_FILE_ICON_EXECUTABLE);
846
#endif
Owen Taylor's avatar
Owen Taylor committed
847
848
849
850
851
852
853
854
855
    }

  if (types & GTK_FILE_INFO_MODIFICATION_TIME)
    {
      gtk_file_info_set_modification_time (info, statbuf.st_mtime);
    }

  if (types & GTK_FILE_INFO_SIZE)
    {
Owen Taylor's avatar
Owen Taylor committed
856
      gtk_file_info_set_size (info, (gint64)statbuf.st_size);
Owen Taylor's avatar
Owen Taylor committed
857
858
859
860
861
862
    }
  
  return info;
}

static gchar *
863
filename_from_path (const GtkFilePath *path)
Owen Taylor's avatar
Owen Taylor committed
864
{
865
  return g_strdup (gtk_file_path_get_string (path));
Owen Taylor's avatar
Owen Taylor committed
866
867
}

868
869
static GtkFilePath *
filename_to_path (const char *filename)
Owen Taylor's avatar
Owen Taylor committed
870
{
871
  return gtk_file_path_new_dup (filename);
Owen Taylor's avatar
Owen Taylor committed
872
873
874
875
876
877
878
879
880
881
882
}

static gboolean
filename_is_root (const char *filename)
{
  const gchar *after_root;
  
  after_root = g_path_skip_root (filename);
  
  return (*after_root == '\0');
}