gtkfilesystemunix.c 49.4 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
/* 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.
 */

21 22
#include <config.h>

23
#include "gtkalias.h"
Owen Taylor's avatar
Owen Taylor committed
24 25
#include "gtkfilesystem.h"
#include "gtkfilesystemunix.h"
Federico Mena Quintero's avatar
Federico Mena Quintero committed
26
#include "gtkicontheme.h"
27
#include "gtkintl.h"
Federico Mena Quintero's avatar
Federico Mena Quintero committed
28
#include "gtkstock.h"
Owen Taylor's avatar
Owen Taylor committed
29

30
#define XDG_PREFIX _gtk_xdg
Owen Taylor's avatar
Owen Taylor committed
31 32
#include "xdgmime/xdgmime.h"

Owen Taylor's avatar
Owen Taylor committed
33 34 35 36
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
Federico Mena Quintero's avatar
Federico Mena Quintero committed
37
#include <pwd.h>
38
#ifdef HAVE_UNISTD_H
Owen Taylor's avatar
Owen Taylor committed
39
#include <unistd.h>
40
#endif
41
#include <stdio.h>
42
#include <time.h>
43 44 45

#define BOOKMARKS_FILENAME ".gtk-bookmarks"
#define BOOKMARKS_TMP_FILENAME ".gtk-bookmarks-XXXXXX"
Owen Taylor's avatar
Owen Taylor committed
46

47 48
#define FOLDER_CACHE_LIFETIME 2 /* seconds */

Owen Taylor's avatar
Owen Taylor committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62
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;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
63 64

  GHashTable *folder_hash;
Owen Taylor's avatar
Owen Taylor committed
65 66
};

Federico Mena Quintero's avatar
Federico Mena Quintero committed
67 68 69
/* Icon type, supplemented by MIME type
 */
typedef enum {
Federico Mena Quintero's avatar
Federico Mena Quintero committed
70 71 72
  ICON_UNDECIDED, /* Only used while we have not yet computed the icon in a struct stat_info_entry */
  ICON_NONE,      /* "Could not compute the icon type" */
  ICON_REGULAR,	  /* Use mime type for icon */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
73 74 75 76 77 78 79 80 81
  ICON_BLOCK_DEVICE,
  ICON_BROKEN_SYMBOLIC_LINK,
  ICON_CHARACTER_DEVICE,
  ICON_DIRECTORY,
  ICON_EXECUTABLE,
  ICON_FIFO,
  ICON_SOCKET
} IconType;

82

Owen Taylor's avatar
Owen Taylor committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#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;

Federico Mena Quintero's avatar
Federico Mena Quintero committed
102
  GtkFileSystemUnix *system_unix;
Owen Taylor's avatar
Owen Taylor committed
103 104
  GtkFileInfoType types;
  gchar *filename;
105 106 107 108 109 110 111 112 113 114
  GHashTable *stat_info;
  unsigned int have_stat : 1;
  unsigned int have_mime_type : 1;
  time_t asof;
};

struct stat_info_entry {
  struct stat statbuf;
  char *mime_type;
  IconType icon_type;
Owen Taylor's avatar
Owen Taylor committed
115 116
};

117 118 119 120 121
static const GtkFileInfoType STAT_NEEDED_MASK = (GTK_FILE_INFO_IS_FOLDER |
						 GTK_FILE_INFO_IS_HIDDEN |
						 GTK_FILE_INFO_MODIFICATION_TIME |
						 GTK_FILE_INFO_SIZE);

Owen Taylor's avatar
Owen Taylor committed
122 123
static GObjectClass *system_parent_class;
static GObjectClass *folder_parent_class;
124

Owen Taylor's avatar
Owen Taylor committed
125 126 127 128 129
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);

130 131 132 133
static GSList *             gtk_file_system_unix_list_volumes        (GtkFileSystem     *file_system);
static GtkFileSystemVolume *gtk_file_system_unix_get_volume_for_path (GtkFileSystem     *file_system,
								      const GtkFilePath *path);

134 135 136 137 138 139 140
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);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

static void         gtk_file_system_unix_volume_free             (GtkFileSystem       *file_system,
								  GtkFileSystemVolume *volume);
static GtkFilePath *gtk_file_system_unix_volume_get_base_path    (GtkFileSystem       *file_system,
								  GtkFileSystemVolume *volume);
static gboolean     gtk_file_system_unix_volume_get_is_mounted   (GtkFileSystem       *file_system,
								  GtkFileSystemVolume *volume);
static gboolean     gtk_file_system_unix_volume_mount            (GtkFileSystem       *file_system,
								  GtkFileSystemVolume *volume,
								  GError             **error);
static gchar *      gtk_file_system_unix_volume_get_display_name (GtkFileSystem       *file_system,
								  GtkFileSystemVolume *volume);
static GdkPixbuf *  gtk_file_system_unix_volume_render_icon      (GtkFileSystem        *file_system,
								  GtkFileSystemVolume  *volume,
								  GtkWidget            *widget,
								  gint                  pixel_size,
								  GError              **error);

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
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
182

183 184 185 186 187 188
static GdkPixbuf *gtk_file_system_unix_render_icon (GtkFileSystem     *file_system,
						    const GtkFilePath *path,
						    GtkWidget         *widget,
						    gint               pixel_size,
						    GError           **error);

189
static gboolean gtk_file_system_unix_insert_bookmark (GtkFileSystem     *file_system,
190
						      const GtkFilePath *path,
191
						      gint               position,
192 193 194 195 196
						      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);
197

Owen Taylor's avatar
Owen Taylor committed
198 199 200 201 202 203 204
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,
205
							const GtkFilePath    *path,
Owen Taylor's avatar
Owen Taylor committed
206 207 208 209 210
							GError        **error);
static gboolean     gtk_file_folder_unix_list_children (GtkFileFolder  *folder,
							GSList        **children,
							GError        **error);

Federico Mena Quintero's avatar
Federico Mena Quintero committed
211 212
static gboolean     gtk_file_folder_unix_is_finished_loading (GtkFileFolder *folder);

213 214
static GtkFilePath *filename_to_path   (const gchar       *filename);

Owen Taylor's avatar
Owen Taylor committed
215
static gboolean     filename_is_root  (const char       *filename);
216 217 218 219 220

static gboolean fill_in_names (GtkFileFolderUnix *folder_unix, GError **error);
static gboolean fill_in_stats (GtkFileFolderUnix *folder_unix, GError **error);
static gboolean fill_in_mime_type (GtkFileFolderUnix *folder_unix, GError **error);

221
static char *       get_parent_dir    (const char       *filename);
Owen Taylor's avatar
Owen Taylor committed
222 223 224 225 226

/*
 * GtkFileSystemUnix
 */
GType
Owen Taylor's avatar
Owen Taylor committed
227
gtk_file_system_unix_get_type (void)
Owen Taylor's avatar
Owen Taylor committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
{
  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,
      };
245

Owen Taylor's avatar
Owen Taylor committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      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;
}

264
/**
Owen Taylor's avatar
Owen Taylor committed
265
 * gtk_file_system_unix_new:
266
 *
267 268 269
 * Creates a new #GtkFileSystemUnix object. #GtkFileSystemUnix
 * implements the #GtkFileSystem interface with direct access to
 * the filesystem using Unix/Linux API calls
270
 *
271 272 273
 * Return value: the new #GtkFileSystemUnix object
 **/
GtkFileSystem *
Owen Taylor's avatar
Owen Taylor committed
274
gtk_file_system_unix_new (void)
275 276 277 278
{
  return g_object_new (GTK_TYPE_FILE_SYSTEM_UNIX, NULL);
}

Owen Taylor's avatar
Owen Taylor committed
279 280 281 282
static void
gtk_file_system_unix_class_init (GtkFileSystemUnixClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
283 284

  system_parent_class = g_type_class_peek_parent (class);
285

Owen Taylor's avatar
Owen Taylor committed
286 287 288 289 290 291
  gobject_class->finalize = gtk_file_system_unix_finalize;
}

static void
gtk_file_system_unix_iface_init   (GtkFileSystemIface *iface)
{
292
  iface->list_volumes = gtk_file_system_unix_list_volumes;
293
  iface->get_volume_for_path = gtk_file_system_unix_get_volume_for_path;
Owen Taylor's avatar
Owen Taylor committed
294 295
  iface->get_folder = gtk_file_system_unix_get_folder;
  iface->create_folder = gtk_file_system_unix_create_folder;
296 297 298 299 300 301
  iface->volume_free = gtk_file_system_unix_volume_free;
  iface->volume_get_base_path = gtk_file_system_unix_volume_get_base_path;
  iface->volume_get_is_mounted = gtk_file_system_unix_volume_get_is_mounted;
  iface->volume_mount = gtk_file_system_unix_volume_mount;
  iface->volume_get_display_name = gtk_file_system_unix_volume_get_display_name;
  iface->volume_render_icon = gtk_file_system_unix_volume_render_icon;
Owen Taylor's avatar
Owen Taylor committed
302
  iface->get_parent = gtk_file_system_unix_get_parent;
303
  iface->make_path = gtk_file_system_unix_make_path;
304
  iface->parse = gtk_file_system_unix_parse;
305 306 307 308
  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;
309
  iface->render_icon = gtk_file_system_unix_render_icon;
310
  iface->insert_bookmark = gtk_file_system_unix_insert_bookmark;
311
  iface->remove_bookmark = gtk_file_system_unix_remove_bookmark;
312
  iface->list_bookmarks = gtk_file_system_unix_list_bookmarks;
Owen Taylor's avatar
Owen Taylor committed
313 314 315 316 317
}

static void
gtk_file_system_unix_init (GtkFileSystemUnix *system_unix)
{
Federico Mena Quintero's avatar
Federico Mena Quintero committed
318
  system_unix->folder_hash = g_hash_table_new (g_str_hash, g_str_equal);
Owen Taylor's avatar
Owen Taylor committed
319 320 321 322 323
}

static void
gtk_file_system_unix_finalize (GObject *object)
{
Federico Mena Quintero's avatar
Federico Mena Quintero committed
324 325 326 327 328 329 330
  GtkFileSystemUnix *system_unix;

  system_unix = GTK_FILE_SYSTEM_UNIX (object);

  /* FIXME: assert that the hash is empty? */
  g_hash_table_destroy (system_unix->folder_hash);

331
  system_parent_class->finalize (object);
Owen Taylor's avatar
Owen Taylor committed
332 333
}

334 335 336
/* Returns our single root volume */
static GtkFileSystemVolume *
get_root_volume (void)
337
{
338
  return (GtkFileSystemVolume *) gtk_file_path_new_dup ("/");
339 340
}

Owen Taylor's avatar
Owen Taylor committed
341
static GSList *
342
gtk_file_system_unix_list_volumes (GtkFileSystem *file_system)
Owen Taylor's avatar
Owen Taylor committed
343
{
344
  return g_slist_append (NULL, get_root_volume ());
Owen Taylor's avatar
Owen Taylor committed
345 346
}

347 348 349
static GtkFileSystemVolume *
gtk_file_system_unix_get_volume_for_path (GtkFileSystem     *file_system,
					  const GtkFilePath *path)
Owen Taylor's avatar
Owen Taylor committed
350
{
351
  return get_root_volume ();
Owen Taylor's avatar
Owen Taylor committed
352 353
}

354 355 356 357 358 359 360 361 362 363 364 365 366
static char *
remove_trailing_slash (const char *filename)
{
  int len;

  len = strlen (filename);

  if (len > 1 && filename[len - 1] == '/')
    return g_strndup (filename, len - 1);
  else
    return g_memdup (filename, len + 1);
}

Owen Taylor's avatar
Owen Taylor committed
367
static GtkFileFolder *
368 369 370 371
gtk_file_system_unix_get_folder (GtkFileSystem     *file_system,
				 const GtkFilePath *path,
				 GtkFileInfoType    types,
				 GError           **error)
Owen Taylor's avatar
Owen Taylor committed
372
{
Federico Mena Quintero's avatar
Federico Mena Quintero committed
373
  GtkFileSystemUnix *system_unix;
Owen Taylor's avatar
Owen Taylor committed
374
  GtkFileFolderUnix *folder_unix;
375
  const char *filename;
376
  char *filename_copy;
377
  time_t now = time (NULL);
Owen Taylor's avatar
Owen Taylor committed
378

Federico Mena Quintero's avatar
Federico Mena Quintero committed
379 380
  system_unix = GTK_FILE_SYSTEM_UNIX (file_system);

381
  filename = gtk_file_path_get_string (path);
382
  g_return_val_if_fail (filename != NULL, NULL);
383
  g_return_val_if_fail (g_path_is_absolute (filename), NULL);
Owen Taylor's avatar
Owen Taylor committed
384

385 386
  filename_copy = remove_trailing_slash (filename);
  folder_unix = g_hash_table_lookup (system_unix->folder_hash, filename_copy);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
387 388

  if (folder_unix)
389
    {
390
      g_free (filename_copy);
391 392 393
      if (now - folder_unix->asof >= FOLDER_CACHE_LIFETIME &&
	  folder_unix->stat_info)
	{
Morten Welinder's avatar
Morten Welinder committed
394
#if 0
395
	  g_print ("Cleaning out cached directory %s\n", filename);
Morten Welinder's avatar
Morten Welinder committed
396
#endif
397 398 399 400 401 402 403
	  g_hash_table_destroy (folder_unix->stat_info);
	  folder_unix->stat_info = NULL;
	  folder_unix->have_mime_type = FALSE;
	  folder_unix->have_stat = FALSE;
	}

      g_object_ref (folder_unix);
404
      folder_unix->types |= types;
405
      types = folder_unix->types;
406
    }
Federico Mena Quintero's avatar
Federico Mena Quintero committed
407 408
  else
    {
409 410 411 412
      if (!g_file_test (filename, G_FILE_TEST_IS_DIR))
	{
	  int save_errno = errno;
	  gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

	  /* If g_file_test() returned FALSE but not due to an error, it means
	   * that the filename is not a directory.
	   */
	  if (save_errno == 0)
	    /* ENOTDIR */
	    g_set_error (error,
			 GTK_FILE_SYSTEM_ERROR,
			 GTK_FILE_SYSTEM_ERROR_NOT_FOLDER,
			 _("%s: %s"),
			 filename_utf8 ? filename_utf8 : "???",
			 g_strerror (ENOTDIR));
	  else
	    g_set_error (error,
			 GTK_FILE_SYSTEM_ERROR,
			 GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
			 _("error getting information for '%s': %s"),
			 filename_utf8 ? filename_utf8 : "???",
			 g_strerror (save_errno));

433 434 435 436 437
	  g_free (filename_utf8);
	  g_free (filename_copy);
	  return NULL;
	}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
438 439
      folder_unix = g_object_new (GTK_TYPE_FILE_FOLDER_UNIX, NULL);
      folder_unix->system_unix = system_unix;
440
      folder_unix->filename = filename_copy;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
441
      folder_unix->types = types;
442 443 444 445 446 447 448 449 450
      folder_unix->stat_info = NULL;
      folder_unix->asof = now;
      folder_unix->have_mime_type = FALSE;
      folder_unix->have_stat = FALSE;

      g_hash_table_insert (system_unix->folder_hash,
			   folder_unix->filename,
			   folder_unix);
    }
Federico Mena Quintero's avatar
Federico Mena Quintero committed
451

452 453 454 455
  if ((types & STAT_NEEDED_MASK) && !fill_in_stats (folder_unix, error))
    {
      g_object_unref (folder_unix);
      return NULL;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
456
    }
457 458 459 460 461 462 463
  if ((types & GTK_FILE_INFO_MIME_TYPE) && !fill_in_mime_type (folder_unix, error))
    {
      g_object_unref (folder_unix);
      return NULL;
    }

  return GTK_FILE_FOLDER (folder_unix);
Owen Taylor's avatar
Owen Taylor committed
464 465 466
}

static gboolean
467 468 469
gtk_file_system_unix_create_folder (GtkFileSystem     *file_system,
				    const GtkFilePath *path,
				    GError           **error)
Owen Taylor's avatar
Owen Taylor committed
470
{
Federico Mena Quintero's avatar
Federico Mena Quintero committed
471
  GtkFileSystemUnix *system_unix;
472
  const char *filename;
Owen Taylor's avatar
Owen Taylor committed
473
  gboolean result;
474
  char *parent, *tmp;
475

Federico Mena Quintero's avatar
Federico Mena Quintero committed
476
  system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
Owen Taylor's avatar
Owen Taylor committed
477

478
  filename = gtk_file_path_get_string (path);
479
  g_return_val_if_fail (filename != NULL, FALSE);
480
  g_return_val_if_fail (g_path_is_absolute (filename), FALSE);
481

482 483 484
  tmp = remove_trailing_slash (filename);
  result = mkdir (tmp, 0777) == 0;
  g_free (tmp);
485

Owen Taylor's avatar
Owen Taylor committed
486 487
  if (!result)
    {
Morten Welinder's avatar
Morten Welinder committed
488
      int save_errno = errno;
Owen Taylor's avatar
Owen Taylor committed
489 490 491
      gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
      g_set_error (error,
		   GTK_FILE_SYSTEM_ERROR,
492
		   GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
493
		   _("error creating directory '%s': %s"),
Owen Taylor's avatar
Owen Taylor committed
494
		   filename_utf8 ? filename_utf8 : "???",
Morten Welinder's avatar
Morten Welinder committed
495
		   g_strerror (save_errno));
Owen Taylor's avatar
Owen Taylor committed
496
      g_free (filename_utf8);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
497
      return FALSE;
Owen Taylor's avatar
Owen Taylor committed
498
    }
499

Federico Mena Quintero's avatar
Federico Mena Quintero committed
500 501 502
  if (filename_is_root (filename))
    return TRUE; /* hmmm, but with no notification */

503
  parent = get_parent_dir (filename);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
504 505 506 507 508 509 510
  if (parent)
    {
      GtkFileFolderUnix *folder_unix;

      folder_unix = g_hash_table_lookup (system_unix->folder_hash, parent);
      if (folder_unix)
	{
511 512
	  GtkFileInfoType types;
	  GtkFilePath *parent_path;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
513
	  GSList *paths;
514
	  GtkFileFolder *folder;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
515

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
	  /* This is sort of a hack.  We re-get the folder, to ensure that the
	   * newly-created directory gets read into the folder's info hash table.
	   */

	  types = folder_unix->types;

	  parent_path = gtk_file_path_new_dup (parent);
	  folder = gtk_file_system_get_folder (file_system, parent_path, types, NULL);
	  gtk_file_path_free (parent_path);

	  if (folder)
	    {
	      paths = g_slist_append (NULL, (GtkFilePath *) path);
	      g_signal_emit_by_name (folder, "files-added", paths);
	      g_slist_free (paths);
	      g_object_unref (folder);
	    }
Federico Mena Quintero's avatar
Federico Mena Quintero committed
533
	}
534 535

      g_free (parent);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
536 537 538
    }

  return TRUE;
Owen Taylor's avatar
Owen Taylor committed
539 540
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
static void
gtk_file_system_unix_volume_free (GtkFileSystem        *file_system,
				  GtkFileSystemVolume  *volume)
{
  GtkFilePath *path;

  path = (GtkFilePath *) volume;
  gtk_file_path_free (path);
}

static GtkFilePath *
gtk_file_system_unix_volume_get_base_path (GtkFileSystem        *file_system,
					   GtkFileSystemVolume  *volume)
{
  return gtk_file_path_new_dup ("/");
}

static gboolean
gtk_file_system_unix_volume_get_is_mounted (GtkFileSystem        *file_system,
					    GtkFileSystemVolume  *volume)
{
  return TRUE;
}

static gboolean
566
gtk_file_system_unix_volume_mount (GtkFileSystem        *file_system,
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
				   GtkFileSystemVolume  *volume,
				   GError              **error)
{
  g_set_error (error,
	       GTK_FILE_SYSTEM_ERROR,
	       GTK_FILE_SYSTEM_ERROR_FAILED,
	       _("This file system does not support mounting"));
  return FALSE;
}

static gchar *
gtk_file_system_unix_volume_get_display_name (GtkFileSystem       *file_system,
					      GtkFileSystemVolume *volume)
{
  return g_strdup (_("Filesystem")); /* Same as Nautilus */
}

584 585 586 587 588 589 590 591 592 593 594
static IconType
get_icon_type_from_stat (struct stat *statp)
{
  if (S_ISBLK (statp->st_mode))
    return ICON_BLOCK_DEVICE;
  else if (S_ISLNK (statp->st_mode))
    return ICON_BROKEN_SYMBOLIC_LINK; /* See get_icon_type */
  else if (S_ISCHR (statp->st_mode))
    return ICON_CHARACTER_DEVICE;
  else if (S_ISDIR (statp->st_mode))
    return ICON_DIRECTORY;
595
#ifdef S_ISFIFO
596 597
  else if (S_ISFIFO (statp->st_mode))
    return  ICON_FIFO;
598 599
#endif
#ifdef S_ISSOCK
600 601
  else if (S_ISSOCK (statp->st_mode))
    return ICON_SOCKET;
602
#endif
603 604 605 606
  else
    return ICON_REGULAR;
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
607 608 609 610 611 612 613 614
static IconType
get_icon_type (const char *filename,
	       GError    **error)
{
  struct stat statbuf;

  /* If stat fails, try to fall back to lstat to catch broken links
   */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
615
  if (stat (filename, &statbuf) != 0)
Federico Mena Quintero's avatar
Federico Mena Quintero committed
616
    {
Federico Mena Quintero's avatar
Federico Mena Quintero committed
617 618 619 620 621 622 623 624 625 626 627
      if (errno != ENOENT || lstat (filename, &statbuf) != 0)
	{
	  int save_errno = errno;
	  gchar *filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
	  g_set_error (error,
		       GTK_FILE_SYSTEM_ERROR,
		       GTK_FILE_SYSTEM_ERROR_NONEXISTENT,
		       _("error getting information for '%s': %s"),
		       filename_utf8 ? filename_utf8 : "???",
		       g_strerror (save_errno));
	  g_free (filename_utf8);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
628

Federico Mena Quintero's avatar
Federico Mena Quintero committed
629 630
	  return ICON_NONE;
	}
Federico Mena Quintero's avatar
Federico Mena Quintero committed
631 632
    }

633
  return get_icon_type_from_stat (&statbuf);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
}

typedef struct
{
  gint size;
  GdkPixbuf *pixbuf;
} IconCacheElement;

static void
icon_cache_element_free (IconCacheElement *element)
{
  if (element->pixbuf)
    g_object_unref (element->pixbuf);
  g_free (element);
}

static void
icon_theme_changed (GtkIconTheme *icon_theme)
{
  GHashTable *cache;
654

Federico Mena Quintero's avatar
Federico Mena Quintero committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
  /* Difference from the initial creation is that we don't
   * reconnect the signal
   */
  cache = g_hash_table_new_full (g_str_hash, g_str_equal,
				 (GDestroyNotify)g_free,
				 (GDestroyNotify)icon_cache_element_free);
  g_object_set_data_full (G_OBJECT (icon_theme), "gtk-file-icon-cache",
			  cache, (GDestroyNotify)g_hash_table_destroy);
}

static GdkPixbuf *
get_cached_icon (GtkWidget   *widget,
		 const gchar *name,
		 gint         pixel_size)
{
  GtkIconTheme *icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
  GHashTable *cache = g_object_get_data (G_OBJECT (icon_theme), "gtk-file-icon-cache");
  IconCacheElement *element;
673

Federico Mena Quintero's avatar
Federico Mena Quintero committed
674 675 676 677 678
  if (!cache)
    {
      cache = g_hash_table_new_full (g_str_hash, g_str_equal,
				     (GDestroyNotify)g_free,
				     (GDestroyNotify)icon_cache_element_free);
679

Federico Mena Quintero's avatar
Federico Mena Quintero committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
      g_object_set_data_full (G_OBJECT (icon_theme), "gtk-file-icon-cache",
			      cache, (GDestroyNotify)g_hash_table_destroy);
      g_signal_connect (icon_theme, "changed",
			G_CALLBACK (icon_theme_changed), NULL);
    }

  element = g_hash_table_lookup (cache, name);
  if (!element)
    {
      element = g_new0 (IconCacheElement, 1);
      g_hash_table_insert (cache, g_strdup (name), element);
    }

  if (element->size != pixel_size)
    {
      if (element->pixbuf)
	g_object_unref (element->pixbuf);
      element->size = pixel_size;
      element->pixbuf = gtk_icon_theme_load_icon (icon_theme, name,
						  pixel_size, 0, NULL);
    }

  return element->pixbuf ? g_object_ref (element->pixbuf) : NULL;
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
/* Renders a fallback icon from the stock system */
static GdkPixbuf *
get_fallback_icon (GtkWidget *widget,
		   IconType   icon_type,
		   GError   **error)
{
  const char *stock_name;
  GdkPixbuf *pixbuf;

  switch (icon_type)
    {
    case ICON_BLOCK_DEVICE:
      stock_name = GTK_STOCK_HARDDISK;
      break;

    case ICON_DIRECTORY:
      stock_name = GTK_STOCK_DIRECTORY;
      break;

    case ICON_EXECUTABLE:
      stock_name = GTK_STOCK_EXECUTE;
      break;

    default:
      stock_name = GTK_STOCK_FILE;
      break;
    }

  pixbuf = gtk_widget_render_icon (widget, stock_name, GTK_ICON_SIZE_SMALL_TOOLBAR, NULL);
  if (!pixbuf)
    g_set_error (error,
		 GTK_FILE_SYSTEM_ERROR,
		 GTK_FILE_SYSTEM_ERROR_FAILED,
		 _("Could not get a stock icon for %s"),
		 stock_name);

  return pixbuf;
}

744 745 746 747 748 749 750
static GdkPixbuf *
gtk_file_system_unix_volume_render_icon (GtkFileSystem        *file_system,
					 GtkFileSystemVolume  *volume,
					 GtkWidget            *widget,
					 gint                  pixel_size,
					 GError              **error)
{
Federico Mena Quintero's avatar
Federico Mena Quintero committed
751 752 753 754 755 756 757 758 759 760
  GdkPixbuf *pixbuf;

  pixbuf = get_cached_icon (widget, "gnome-fs-blockdev", pixel_size);
  if (pixbuf)
    return pixbuf;

  pixbuf = get_fallback_icon (widget, ICON_BLOCK_DEVICE, error);
  g_assert (pixbuf != NULL);

  return pixbuf;
761 762
}

763 764 765 766 767 768
static char *
get_parent_dir (const char *filename)
{
  int len;

  len = strlen (filename);
769

770
  /* Ignore trailing slashes */
771
  if (len > 1 && filename[len - 1] == '/')
772 773
    {
      char *tmp, *parent;
774

775 776 777 778 779 780 781 782 783 784 785
      tmp = g_strndup (filename, len - 1);

      parent = g_path_get_dirname (tmp);
      g_free (tmp);

      return parent;
    }
  else
    return g_path_get_dirname (filename);
}

Owen Taylor's avatar
Owen Taylor committed
786
static gboolean
787 788 789 790
gtk_file_system_unix_get_parent (GtkFileSystem     *file_system,
				 const GtkFilePath *path,
				 GtkFilePath      **parent,
				 GError           **error)
Owen Taylor's avatar
Owen Taylor committed
791
{
792
  const char *filename;
793

794
  filename = gtk_file_path_get_string (path);
795
  g_return_val_if_fail (filename != NULL, FALSE);
796
  g_return_val_if_fail (g_path_is_absolute (filename), FALSE);
Owen Taylor's avatar
Owen Taylor committed
797 798 799 800 801 802 803

  if (filename_is_root (filename))
    {
      *parent = NULL;
    }
  else
    {
804
      gchar *parent_filename = get_parent_dir (filename);
805
      *parent = filename_to_path (parent_filename);
Owen Taylor's avatar
Owen Taylor committed
806 807 808 809 810 811
      g_free (parent_filename);
    }

  return TRUE;
}

812 813 814 815 816
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
817
{
818
  const char *base_filename;
Owen Taylor's avatar
Owen Taylor committed
819 820 821
  gchar *filename;
  gchar *full_filename;
  GError *tmp_error = NULL;
822
  GtkFilePath *result;
823

824
  base_filename = gtk_file_path_get_string (base_path);
825
  g_return_val_if_fail (base_filename != NULL, NULL);
826
  g_return_val_if_fail (g_path_is_absolute (base_filename), NULL);
Owen Taylor's avatar
Owen Taylor committed
827

828 829 830 831 832 833 834 835 836 837 838 839
  if (strchr (display_name, G_DIR_SEPARATOR))
    {
      g_set_error (error,
		   GTK_FILE_SYSTEM_ERROR,
		   GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
		   _("The name \"%s\" is not valid because it contains the character \"%s\". "
		     "Please use a different name."),
		   display_name,
		   G_DIR_SEPARATOR_S);
      return NULL;
    }

Owen Taylor's avatar
Owen Taylor committed
840 841 842 843 844 845 846 847
  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);
848

Owen Taylor's avatar
Owen Taylor committed
849 850
      g_error_free (tmp_error);

851
      return NULL;
Owen Taylor's avatar
Owen Taylor committed
852
    }
853

Owen Taylor's avatar
Owen Taylor committed
854
  full_filename = g_build_filename (base_filename, filename, NULL);
855
  result = filename_to_path (full_filename);
Owen Taylor's avatar
Owen Taylor committed
856 857
  g_free (filename);
  g_free (full_filename);
858

859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
  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;
893

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
		  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;
910

911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
		  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';
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
/* Takes a user-typed filename and expands a tilde at the beginning of the string */
static char *
expand_tilde (const char *filename)
{
  const char *notilde;
  const char *slash;
  const char *home;

  if (filename[0] != '~')
    return g_strdup (filename);

  notilde = filename + 1;

  slash = strchr (notilde, G_DIR_SEPARATOR);
  if (!slash)
    return NULL;

  if (slash == notilde)
    {
      home = g_get_home_dir ();

      if (!home)
	return g_strdup (filename);
    }
  else
    {
      char *username;
      struct passwd *passwd;

      username = g_strndup (notilde, slash - notilde);
      passwd = getpwnam (username);
      g_free (username);

      if (!passwd)
	return g_strdup (filename);

      home = passwd->pw_dir;
    }

  return g_build_filename (home, G_DIR_SEPARATOR_S, slash + 1, NULL);
}

977
static gboolean
978 979 980 981 982 983
gtk_file_system_unix_parse (GtkFileSystem     *file_system,
			    const GtkFilePath *base_path,
			    const gchar       *str,
			    GtkFilePath      **folder,
			    gchar            **file_part,
			    GError           **error)
984
{
985
  const char *base_filename;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
986
  gchar *filename;
987 988 989
  gchar *last_slash;
  gboolean result = FALSE;

990
  base_filename = gtk_file_path_get_string (base_path);
991
  g_return_val_if_fail (base_filename != NULL, FALSE);
992
  g_return_val_if_fail (g_path_is_absolute (base_filename), FALSE);
993

Federico Mena Quintero's avatar
Federico Mena Quintero committed
994 995 996 997 998 999 1000 1001 1002 1003 1004
  filename = expand_tilde (str);
  if (!filename)
    {
      g_set_error (error,
		   GTK_FILE_SYSTEM_ERROR,
		   GTK_FILE_SYSTEM_ERROR_BAD_FILENAME,
		   "%s", ""); /* nothing for now, as we are string-frozen */
      return FALSE;
    }

  last_slash = strrchr (filename, G_DIR_SEPARATOR);
1005 1006
  if (!last_slash)
    {
1007
      *folder = gtk_file_path_copy (base_path);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1008
      *file_part = g_strdup (filename);
1009 1010 1011 1012 1013 1014 1015 1016
      result = TRUE;
    }
  else
    {
      gchar *folder_part;
      gchar *folder_path;
      GError *tmp_error = NULL;

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1017
      if (last_slash == filename)
1018 1019
	folder_part = g_strdup ("/");
      else
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1020
	folder_part = g_filename_from_utf8 (filename, last_slash - filename,
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
					    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);
1043

1044
	  *folder = filename_to_path (folder_path);
1045 1046 1047 1048 1049 1050 1051 1052
	  *file_part = g_strdup (last_slash + 1);

	  g_free (folder_path);

	  result = TRUE;
	}
    }

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1053 1054
  g_free (filename);

Owen Taylor's avatar
Owen Taylor committed
1055 1056 1057
  return result;
}

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
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)
{
1076
  GtkFilePath *path;
1077
  gchar *filename = g_filename_from_uri (uri, NULL, NULL);
1078

1079
  if (filename)
1080 1081 1082 1083
    {
      path = filename_to_path (filename);
      g_free (filename);
    }
1084
  else
1085 1086 1087
    path = NULL;

  return path;
1088 1089 1090 1091 1092 1093
}

static GtkFilePath *
gtk_file_system_unix_filename_to_path (GtkFileSystem *file_system,
				       const gchar   *filename)
{
1094
  return filename_to_path (filename);
1095 1096
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1097 1098 1099
/* Returns the name of the icon to be used for a path which is known to be a
 * directory.  This can vary for Home, Desktop, etc.
 */
1100
static const char *
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1101
get_icon_name_for_directory (const char *path)
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
{
  static char *desktop_path = NULL;

  if (!g_get_home_dir ())
    return "gnome-fs-directory";

  if (!desktop_path)
      desktop_path = g_build_filename (g_get_home_dir (), "Desktop", NULL);

  if (strcmp (g_get_home_dir (), path) == 0)
    return "gnome-fs-home";
  else if (strcmp (desktop_path, path) == 0)
    return "gnome-fs-desktop";
  else
    return "gnome-fs-directory";
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1119 1120 1121 1122 1123 1124 1125
/* Computes our internal icon type based on a path name; also returns the MIME
 * type in case we come up with ICON_REGULAR.
 */
static IconType
get_icon_type_from_path (GtkFileSystemUnix *system_unix,
			 const GtkFilePath *path,
			 const char       **mime_type)
1126
{
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1127
  const char *filename;
1128 1129
  char *dirname;
  GtkFileFolderUnix *folder_unix;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1130
  IconType icon_type;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1131 1132

  filename = gtk_file_path_get_string (path);
1133 1134 1135 1136
  dirname = g_path_get_dirname (filename);
  folder_unix = g_hash_table_lookup (system_unix->folder_hash, dirname);
  g_free (dirname);

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1137 1138
  *mime_type = NULL;

1139 1140 1141 1142 1143
  if (folder_unix)
    {
      char *basename;
      struct stat_info_entry *entry;

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1144 1145
      if (!fill_in_stats (folder_unix, NULL))
	return ICON_NONE;
1146 1147 1148 1149 1150 1151 1152

      basename = g_path_get_basename (filename);
      entry = g_hash_table_lookup (folder_unix->stat_info, basename);
      g_free (basename);
      if (entry)
	{
	  if (entry->icon_type == ICON_UNDECIDED)
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1153 1154 1155 1156
	    {
	      entry->icon_type = get_icon_type_from_stat (&entry->statbuf);
	      g_assert (entry->icon_type != ICON_UNDECIDED);
	    }
1157 1158 1159 1160
	  icon_type = entry->icon_type;
	  if (icon_type == ICON_REGULAR)
	    {
	      (void)fill_in_mime_type (folder_unix, NULL);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1161
	      *mime_type = entry->mime_type;
1162 1163 1164 1165 1166 1167 1168
	    }
	}
      else
	icon_type = ICON_NONE;
    }
  else
    {
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1169
      icon_type = get_icon_type (filename, NULL);
1170
      if (icon_type == ICON_REGULAR)
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1171
	*mime_type = xdg_mime_get_mime_type_for_file (filename);
1172 1173
    }

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1174 1175
  return icon_type;
}
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1176

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1177 1178 1179 1180 1181 1182 1183 1184
/* Renders an icon for a non-ICON_REGULAR file */
static GdkPixbuf *
get_special_icon (IconType           icon_type,
		  const GtkFilePath *path,
		  GtkWidget         *widget,
		  gint               pixel_size)
{
  const char *name;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1185

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1186
  g_assert (icon_type != ICON_REGULAR);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1187

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1188
  switch (icon_type)
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1189
    {
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    case ICON_BLOCK_DEVICE:
      name = "gnome-fs-blockdev";
      break;
    case ICON_BROKEN_SYMBOLIC_LINK:
      name = "gnome-fs-symlink";
      break;
    case ICON_CHARACTER_DEVICE:
      name = "gnome-fs-chardev";
      break;
    case ICON_DIRECTORY: {
      const char *filename;

      filename = gtk_file_path_get_string (path);
      name = get_icon_name_for_directory (filename);
      break;
      }
    case ICON_EXECUTABLE:
      name ="gnome-fs-executable";
      break;
    case ICON_FIFO:
      name = "gnome-fs-fifo";
      break;
    case ICON_SOCKET:
      name = "gnome-fs-socket";
      break;
    default:
      g_assert_not_reached ();
      return NULL;
    }
1219

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1220 1221
  return get_cached_icon (widget, name, pixel_size);
}
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1222

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1223 1224 1225 1226 1227 1228 1229 1230
static GdkPixbuf *
get_icon_for_mime_type (GtkWidget  *widget,
			const char *mime_type,
			gint        pixel_size)
{
  const char *separator;
  GString *icon_name;
  GdkPixbuf *pixbuf;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1231

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
  separator = strchr (mime_type, '/');
  if (!separator)
    return NULL; /* maybe we should return a GError with "invalid MIME-type" */

  icon_name = g_string_new ("gnome-mime-");
  g_string_append_len (icon_name, mime_type, separator - mime_type);
  g_string_append_c (icon_name, '-');
  g_string_append (icon_name, separator + 1);
  pixbuf = get_cached_icon (widget, icon_name->str, pixel_size);
  g_string_free (icon_name, TRUE);
  if (pixbuf)
    return pixbuf;

  icon_name = g_string_new ("gnome-mime-");
  g_string_append_len (icon_name, mime_type, separator - mime_type);
  pixbuf = get_cached_icon (widget, icon_name->str, pixel_size);
  g_string_free (icon_name, TRUE);

  return pixbuf;
}

static GdkPixbuf *
gtk_file_system_unix_render_icon (GtkFileSystem     *file_system,
				  const GtkFilePath *path,
				  GtkWidget         *widget,
				  gint               pixel_size,
				  GError           **error)
{
  GtkFileSystemUnix *system_unix;
  IconType icon_type;
  const char *mime_type;
  GdkPixbuf *pixbuf;

  system_unix = GTK_FILE_SYSTEM_UNIX (file_system);

  icon_type = get_icon_type_from_path (system_unix, path, &mime_type);

  switch (icon_type) {
  case ICON_NONE:
    goto fallback;

  case ICON_REGULAR:
    pixbuf = get_icon_for_mime_type (widget, mime_type, pixel_size);
    break;

  default:
    pixbuf = get_special_icon (icon_type, path, widget, pixel_size);
  }

  if (pixbuf)
    goto out;

 fallback:

  pixbuf = get_cached_icon (widget, "gnome-fs-regular", pixel_size);
  if (pixbuf)
    goto out;

  pixbuf = get_fallback_icon (widget, icon_type, error);

 out:
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1293

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1294
  return pixbuf;
1295 1296
}

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
static void
bookmark_list_free (GSList *list)
{
  GSList *l;

  for (l =