nautilus-bookmark.c 20.8 KB
Newer Older
1 2 3
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-bookmark.c - implementation of individual bookmarks.
Cosimo Cecchi's avatar
Cosimo Cecchi committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Copyright (C) 1999, 2000 Eazel, Inc.
 * Copyright (C) 2011, Red Hat, Inc.
 *
 * The Gnome Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Gnome 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: John Sullivan <sullivan@eazel.com>
 *          Cosimo Cecchi <cosimoc@redhat.com>
 */
26

27
#include <config.h>
Cosimo Cecchi's avatar
Cosimo Cecchi committed
28

29
#include "nautilus-bookmark.h"
30

Ramiro Estrugo's avatar
Ramiro Estrugo committed
31
#include <eel/eel-vfs-extensions.h>
32
#include <gio/gio.h>
33 34
#include <glib/gi18n.h>
#include <gtk/gtk.h>
Cosimo Cecchi's avatar
Cosimo Cecchi committed
35

Alexander Larsson's avatar
Alexander Larsson committed
36
#include <libnautilus-private/nautilus-file.h>
Cosimo Cecchi's avatar
Cosimo Cecchi committed
37
#include <libnautilus-private/nautilus-file-utilities.h>
38
#include <libnautilus-private/nautilus-global-preferences.h>
Cosimo Cecchi's avatar
Cosimo Cecchi committed
39
#include <libnautilus-private/nautilus-icon-names.h>
40

Cosimo Cecchi's avatar
Cosimo Cecchi committed
41 42 43
#define DEBUG_FLAG NAUTILUS_DEBUG_BOOKMARKS
#include <libnautilus-private/nautilus-debug.h>

44
enum {
45
	CONTENTS_CHANGED,
46 47 48
	LAST_SIGNAL
};

Cosimo Cecchi's avatar
Cosimo Cecchi committed
49 50 51 52 53
enum {
	PROP_NAME = 1,
	PROP_CUSTOM_NAME,
	PROP_LOCATION,
	PROP_ICON,
54
	PROP_SYMBOLIC_ICON,
Cosimo Cecchi's avatar
Cosimo Cecchi committed
55 56 57
	NUM_PROPERTIES
};

58 59
#define ELLIPSISED_MENU_ITEM_MIN_CHARS  32

Cosimo Cecchi's avatar
Cosimo Cecchi committed
60
static GParamSpec* properties[NUM_PROPERTIES] = { NULL };
61
static guint signals[LAST_SIGNAL];
62

63
struct NautilusBookmarkDetails
64
{
65
	char *name;
66
	gboolean has_custom_name;
Alexander Larsson's avatar
Alexander Larsson committed
67 68
	GFile *location;
	GIcon *icon;
69
	GIcon *symbolic_icon;
70
	NautilusFile *file;
71 72
	
	char *scroll_file;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
73 74 75 76

	gboolean exists;
	guint exists_id;
	GCancellable *cancellable;
77 78
};

79
static void	  nautilus_bookmark_disconnect_file	  (NautilusBookmark	 *file);
80

Cosimo Cecchi's avatar
Cosimo Cecchi committed
81
G_DEFINE_TYPE (NautilusBookmark, nautilus_bookmark, G_TYPE_OBJECT);
82

Cosimo Cecchi's avatar
Cosimo Cecchi committed
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
static void
nautilus_bookmark_set_name_internal (NautilusBookmark *bookmark,
				     const char *new_name)
{
	if (g_strcmp0 (bookmark->details->name, new_name) != 0) {
		g_free (bookmark->details->name);
		bookmark->details->name = g_strdup (new_name);

		g_object_notify_by_pspec (G_OBJECT (bookmark), properties[PROP_NAME]);
	}
}

static void
bookmark_set_name_from_ready_file (NautilusBookmark *self,
				   NautilusFile *file)
{
	gchar *display_name;

	if (self->details->has_custom_name) {
		return;
	}

	display_name = nautilus_file_get_display_name (self->details->file);

	if (nautilus_file_is_home (self->details->file)) {
		nautilus_bookmark_set_name_internal (self, _("Home"));
	} else if (g_strcmp0 (self->details->name, display_name) != 0) {
		nautilus_bookmark_set_name_internal (self, display_name);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
111
		DEBUG ("%s: name changed to %s", nautilus_bookmark_get_name (self), display_name);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
112 113 114 115 116 117 118 119 120 121 122 123 124
	}

	g_free (display_name);
}

static void
bookmark_file_changed_callback (NautilusFile *file,
				NautilusBookmark *bookmark)
{
	GFile *location;

	g_assert (file == bookmark->details->file);

Cosimo Cecchi's avatar
Cosimo Cecchi committed
125 126
	DEBUG ("%s: file changed", nautilus_bookmark_get_name (bookmark));

Cosimo Cecchi's avatar
Cosimo Cecchi committed
127 128 129 130
	location = nautilus_file_get_location (file);

	if (!g_file_equal (bookmark->details->location, location) &&
	    !nautilus_file_is_in_trash (file)) {
Cosimo Cecchi's avatar
Cosimo Cecchi committed
131 132
		DEBUG ("%s: file got moved", nautilus_bookmark_get_name (bookmark));

Cosimo Cecchi's avatar
Cosimo Cecchi committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		g_object_unref (bookmark->details->location);
		bookmark->details->location = g_object_ref (location);

		g_object_notify_by_pspec (G_OBJECT (bookmark), properties[PROP_LOCATION]);
		g_signal_emit (bookmark, signals[CONTENTS_CHANGED], 0);
	}

	g_object_unref (location);

	if (nautilus_file_is_gone (file) ||
	    nautilus_file_is_in_trash (file)) {
		/* The file we were monitoring has been trashed, deleted,
		 * or moved in a way that we didn't notice. We should make 
		 * a spanking new NautilusFile object for this 
		 * location so if a new file appears in this place 
		 * we will notice. However, we can't immediately do so
		 * because creating a new NautilusFile directly as a result
		 * of noticing a file goes away may trigger i/o on that file
		 * again, noticeing it is gone, leading to a loop.
		 * So, the new NautilusFile is created when the bookmark
		 * is used again. However, this is not really a problem, as
		 * we don't want to change the icon or anything about the
		 * bookmark just because its not there anymore.
		 */
Cosimo Cecchi's avatar
Cosimo Cecchi committed
157
		DEBUG ("%s: trashed", nautilus_bookmark_get_name (bookmark));
Cosimo Cecchi's avatar
Cosimo Cecchi committed
158 159
		nautilus_bookmark_disconnect_file (bookmark);
	} else {
160
		bookmark_set_name_from_ready_file (bookmark, file);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
161 162 163 164
	}
}

static void
165 166
apply_warning_emblem (GIcon **base,
		      gboolean symbolic)
Cosimo Cecchi's avatar
Cosimo Cecchi committed
167
{
168
	GIcon *warning, *emblemed_icon;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
169
	GEmblem *emblem;
170 171 172 173

	if (symbolic) {
		warning = g_themed_icon_new ("dialog-warning-symbolic");
	} else {
174
		warning = g_themed_icon_new ("dialog-warning");
175 176 177 178 179 180 181 182 183 184 185 186
	}

	emblem = g_emblem_new (warning);
	emblemed_icon = g_emblemed_icon_new (*base, emblem);

	g_object_unref (emblem);
	g_object_unref (warning);
	g_object_unref (*base);

	*base = emblemed_icon;
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
gboolean
nautilus_bookmark_get_is_builtin (NautilusBookmark *bookmark)
{
	GUserDirectory xdg_type;

	/* if this is not an XDG dir, it's never builtin */
	if (!nautilus_bookmark_get_xdg_type (bookmark, &xdg_type)) {
		return FALSE;
	}

	/* exclude XDG locations which are not in our builtin list */
	if (xdg_type == G_USER_DIRECTORY_DESKTOP &&
	    !g_settings_get_boolean (gnome_background_preferences, NAUTILUS_PREFERENCES_SHOW_DESKTOP)) {
		return FALSE;
	}

	return (xdg_type != G_USER_DIRECTORY_TEMPLATES) && (xdg_type != G_USER_DIRECTORY_PUBLIC_SHARE);
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
gboolean
nautilus_bookmark_get_xdg_type (NautilusBookmark *bookmark,
				GUserDirectory   *directory)
{
	gboolean match;
	GFile *location;
	const gchar *path;
	GUserDirectory dir;

	match = FALSE;

	for (dir = 0; dir < G_USER_N_DIRECTORIES; dir++) {
		path = g_get_user_special_dir (dir);
		if (!path) {
			continue;
		}

		location = g_file_new_for_path (path);
		match = g_file_equal (location, bookmark->details->location);
		g_object_unref (location);

		if (match) {
			break;
		}
	}

	if (match && directory != NULL) {
		*directory = dir;
	}

	return match;
}

239 240 241 242
static GIcon *
get_native_icon (NautilusBookmark *bookmark,
		 gboolean symbolic)
{
243
	GUserDirectory xdg_type;
244 245 246 247 248 249
	GIcon *icon = NULL;

	if (bookmark->details->file == NULL) {
		goto out;
	}

250 251
	if (!nautilus_bookmark_get_xdg_type (bookmark, &xdg_type)) {
		goto out;
252 253
	}

254
	if (xdg_type < G_USER_N_DIRECTORIES) {
255
		if (symbolic) {
256
			icon = nautilus_special_directory_get_symbolic_icon (xdg_type);
257
		} else {
258
			icon = nautilus_special_directory_get_icon (xdg_type);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
		}
	}

 out:
	if (icon == NULL) {
		if (symbolic) {
			icon = g_themed_icon_new (NAUTILUS_ICON_FOLDER);
		} else {
			icon = g_themed_icon_new (NAUTILUS_ICON_FULLCOLOR_FOLDER);
		}
	}

	return icon;
}

274 275 276 277
static void
nautilus_bookmark_set_icon_to_default (NautilusBookmark *bookmark)
{
	GIcon *icon, *symbolic_icon;
278
	char *uri;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
279

Cosimo Cecchi's avatar
Cosimo Cecchi committed
280
	if (g_file_is_native (bookmark->details->location)) {
281 282
		symbolic_icon = get_native_icon (bookmark, TRUE);
		icon = get_native_icon (bookmark, FALSE);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
283
	} else {
284 285
		uri = nautilus_bookmark_get_uri (bookmark);
		if (g_str_has_prefix (uri, EEL_SEARCH_URI)) {
286 287
			symbolic_icon = g_themed_icon_new (NAUTILUS_ICON_FOLDER_SAVED_SEARCH);
			icon = g_themed_icon_new (NAUTILUS_ICON_FULLCOLOR_FOLDER_SAVED_SEARCH);
288
		} else {
289 290
			symbolic_icon = g_themed_icon_new (NAUTILUS_ICON_FOLDER_REMOTE);
			icon = g_themed_icon_new (NAUTILUS_ICON_FULLCOLOR_FOLDER_REMOTE);
291 292
		}
		g_free (uri);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
293
	}
Cosimo Cecchi's avatar
Cosimo Cecchi committed
294

Cosimo Cecchi's avatar
Cosimo Cecchi committed
295
	if (!bookmark->details->exists) {
Cosimo Cecchi's avatar
Cosimo Cecchi committed
296 297
		DEBUG ("%s: file does not exist, add emblem", nautilus_bookmark_get_name (bookmark));

298 299
		apply_warning_emblem (&icon, FALSE);
		apply_warning_emblem (&symbolic_icon, TRUE);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
300 301
	}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
302 303
	DEBUG ("%s: setting icon to default", nautilus_bookmark_get_name (bookmark));

Cosimo Cecchi's avatar
Cosimo Cecchi committed
304
	g_object_set (bookmark,
305 306
		      "icon", icon,
		      "symbolic-icon", symbolic_icon,
Cosimo Cecchi's avatar
Cosimo Cecchi committed
307 308
		      NULL);

309 310
	g_object_unref (icon);
	g_object_unref (symbolic_icon);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
311 312 313 314
}

static void
nautilus_bookmark_disconnect_file (NautilusBookmark *bookmark)
Cosimo Cecchi's avatar
Cosimo Cecchi committed
315 316 317 318 319 320 321 322 323 324
{
	if (bookmark->details->file != NULL) {
		DEBUG ("%s: disconnecting file",
		       nautilus_bookmark_get_name (bookmark));

		g_signal_handlers_disconnect_by_func (bookmark->details->file,
						      G_CALLBACK (bookmark_file_changed_callback),
						      bookmark);
		g_clear_object (&bookmark->details->file);
	}
Cosimo Cecchi's avatar
Cosimo Cecchi committed
325 326 327 328 329 330 331 332 333 334

	if (bookmark->details->cancellable != NULL) {
		g_cancellable_cancel (bookmark->details->cancellable);
		g_clear_object (&bookmark->details->cancellable);
	}

	if (bookmark->details->exists_id != 0) {
		g_source_remove (bookmark->details->exists_id);
		bookmark->details->exists_id = 0;
	}
Cosimo Cecchi's avatar
Cosimo Cecchi committed
335 336 337 338 339 340
}

static void
nautilus_bookmark_connect_file (NautilusBookmark *bookmark)
{
	if (bookmark->details->file != NULL) {
Cosimo Cecchi's avatar
Cosimo Cecchi committed
341 342
		DEBUG ("%s: file already connected, returning",
		       nautilus_bookmark_get_name (bookmark));
Cosimo Cecchi's avatar
Cosimo Cecchi committed
343 344 345
		return;
	}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
346
	if (bookmark->details->exists) {
Cosimo Cecchi's avatar
Cosimo Cecchi committed
347 348
		DEBUG ("%s: creating file", nautilus_bookmark_get_name (bookmark));

Cosimo Cecchi's avatar
Cosimo Cecchi committed
349 350 351 352 353 354 355
		bookmark->details->file = nautilus_file_get (bookmark->details->location);
		g_assert (!nautilus_file_is_gone (bookmark->details->file));

		g_signal_connect_object (bookmark->details->file, "changed",
					 G_CALLBACK (bookmark_file_changed_callback), bookmark, 0);
	}

356 357
	if (bookmark->details->icon == NULL ||
	    bookmark->details->symbolic_icon == NULL) {
Cosimo Cecchi's avatar
Cosimo Cecchi committed
358 359 360
		nautilus_bookmark_set_icon_to_default (bookmark);
	}

361 362
	if (bookmark->details->file != NULL &&
	    nautilus_file_check_if_ready (bookmark->details->file, NAUTILUS_FILE_ATTRIBUTE_INFO)) {
Cosimo Cecchi's avatar
Cosimo Cecchi committed
363 364 365 366 367 368 369 370
		bookmark_set_name_from_ready_file (bookmark, bookmark->details->file);
	}

	if (bookmark->details->name == NULL) {
		bookmark->details->name = nautilus_compute_title_for_location (bookmark->details->location);
	}
}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
371 372 373 374 375 376 377 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
static void
nautilus_bookmark_set_exists (NautilusBookmark *bookmark,
			      gboolean exists)
{
	if (bookmark->details->exists == exists) {
		return;
	}

	bookmark->details->exists = exists;
	DEBUG ("%s: setting bookmark to exist: %d\n",
	       nautilus_bookmark_get_name (bookmark), exists);

	/* refresh icon */
	nautilus_bookmark_set_icon_to_default (bookmark);
}

static gboolean
exists_non_native_idle_cb (gpointer user_data)
{
	NautilusBookmark *bookmark = user_data;
	nautilus_bookmark_set_exists (bookmark, FALSE);

	return FALSE;
}

static void
exists_query_info_ready_cb (GObject *source,
			    GAsyncResult *res,
			    gpointer user_data)
{
	GFileInfo *info;
	NautilusBookmark *bookmark;
	GError *error = NULL;
	gboolean exists = FALSE;

	info = g_file_query_info_finish (G_FILE (source), res, &error);
	if (!info && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		g_clear_error (&error);
		return;
	}

	g_clear_error (&error);
	bookmark = user_data;

	if (info) {
		exists = TRUE;

		g_object_unref (info);
		g_clear_object (&bookmark->details->cancellable);
	}

	nautilus_bookmark_set_exists (bookmark, exists);
}

static void
nautilus_bookmark_update_exists (NautilusBookmark *bookmark)
{
	/* Convert to a path, returning FALSE if not local. */
	if (!g_file_is_native (bookmark->details->location) &&
	    bookmark->details->exists_id == 0) {
		bookmark->details->exists_id =
			g_idle_add (exists_non_native_idle_cb, bookmark);
		return;
	}

	if (bookmark->details->cancellable != NULL) {
		return;
	}

	bookmark->details->cancellable = g_cancellable_new ();
	g_file_query_info_async (bookmark->details->location,
				 G_FILE_ATTRIBUTE_STANDARD_TYPE,
				 0, G_PRIORITY_DEFAULT,
				 bookmark->details->cancellable,
				 exists_query_info_ready_cb, bookmark);
}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
/* GObject methods */

static void
nautilus_bookmark_set_property (GObject *object,
				guint property_id,
				const GValue *value,
				GParamSpec *pspec)
{
	NautilusBookmark *self = NAUTILUS_BOOKMARK (object);
	GIcon *new_icon;

	switch (property_id) {
	case PROP_ICON:
		new_icon = g_value_get_object (value);

		if (new_icon != NULL && !g_icon_equal (self->details->icon, new_icon)) {
			g_clear_object (&self->details->icon);
			self->details->icon = g_object_ref (new_icon);
		}

468 469 470 471 472 473 474 475 476
		break;
	case PROP_SYMBOLIC_ICON:
		new_icon = g_value_get_object (value);

		if (new_icon != NULL && !g_icon_equal (self->details->symbolic_icon, new_icon)) {
			g_clear_object (&self->details->symbolic_icon);
			self->details->symbolic_icon = g_object_ref (new_icon);
		}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
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
		break;
	case PROP_LOCATION:
		self->details->location = g_value_dup_object (value);
		break;
	case PROP_CUSTOM_NAME:
		self->details->has_custom_name = g_value_get_boolean (value);
		break;
	case PROP_NAME:
		nautilus_bookmark_set_name_internal (self, g_value_get_string (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
nautilus_bookmark_get_property (GObject *object,
				guint property_id,
				GValue *value,
				GParamSpec *pspec)
{
	NautilusBookmark *self = NAUTILUS_BOOKMARK (object);

	switch (property_id) {
	case PROP_NAME:
		g_value_set_string (value, self->details->name);
		break;
	case PROP_ICON:
		g_value_set_object (value, self->details->icon);
		break;
508 509 510
	case PROP_SYMBOLIC_ICON:
		g_value_set_object (value, self->details->symbolic_icon);
		break;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
511 512 513 514 515 516 517 518 519 520 521
	case PROP_LOCATION:
		g_value_set_object (value, self->details->location);
		break;
	case PROP_CUSTOM_NAME:
		g_value_set_boolean (value, self->details->has_custom_name);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}
522 523

static void
524
nautilus_bookmark_finalize (GObject *object)
525 526 527
{
	NautilusBookmark *bookmark;

528
	g_assert (NAUTILUS_IS_BOOKMARK (object));
529

530
	bookmark = NAUTILUS_BOOKMARK (object);
531

532 533
	nautilus_bookmark_disconnect_file (bookmark);	

Alexander Larsson's avatar
Alexander Larsson committed
534
	g_object_unref (bookmark->details->location);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
535
	g_clear_object (&bookmark->details->icon);
536
	g_clear_object (&bookmark->details->symbolic_icon);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
537 538

	g_free (bookmark->details->name);
539
	g_free (bookmark->details->scroll_file);
540

541
	G_OBJECT_CLASS (nautilus_bookmark_parent_class)->finalize (object);
542 543
}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
544 545 546 547 548 549
static void
nautilus_bookmark_constructed (GObject *obj)
{
	NautilusBookmark *self = NAUTILUS_BOOKMARK (obj);

	nautilus_bookmark_connect_file (self);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
550
	nautilus_bookmark_update_exists (self);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
551
}
552 553

static void
554
nautilus_bookmark_class_init (NautilusBookmarkClass *class)
555
{
Cosimo Cecchi's avatar
Cosimo Cecchi committed
556
	GObjectClass *oclass = G_OBJECT_CLASS (class);
557

Cosimo Cecchi's avatar
Cosimo Cecchi committed
558 559 560 561
	oclass->finalize = nautilus_bookmark_finalize;
	oclass->get_property = nautilus_bookmark_get_property;
	oclass->set_property = nautilus_bookmark_set_property;
	oclass->constructed = nautilus_bookmark_constructed;
562 563

	signals[CONTENTS_CHANGED] =
Cosimo Cecchi's avatar
Cosimo Cecchi committed
564
		g_signal_new ("contents-changed",
565
		              G_TYPE_FROM_CLASS (class),
566 567 568
		              G_SIGNAL_RUN_LAST,
		              G_STRUCT_OFFSET (NautilusBookmarkClass, contents_changed),
		              NULL, NULL,
569
		              g_cclosure_marshal_VOID__VOID,
570
		              G_TYPE_NONE, 0);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
571 572 573 574 575 576

	properties[PROP_NAME] =
		g_param_spec_string ("name",
				     "Bookmark's name",
				     "The name of this bookmark",
				     NULL,
577
				     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
578 579 580 581 582 583

	properties[PROP_CUSTOM_NAME] =
		g_param_spec_boolean ("custom-name",
				      "Whether the bookmark has a custom name",
				      "Whether the bookmark has a custom name",
				      FALSE,
584
				      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

	properties[PROP_LOCATION] =
		g_param_spec_object ("location",
				     "Bookmark's location",
				     "The location of this bookmark",
				     G_TYPE_FILE,
				     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);

	properties[PROP_ICON] =
		g_param_spec_object ("icon",
				     "Bookmark's icon",
				     "The icon of this bookmark",
				     G_TYPE_ICON,
				     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

600 601 602 603 604 605 606
	properties[PROP_SYMBOLIC_ICON] =
		g_param_spec_object ("symbolic-icon",
				     "Bookmark's symbolic icon",
				     "The symbolic icon of this bookmark",
				     G_TYPE_ICON,
				     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

Cosimo Cecchi's avatar
Cosimo Cecchi committed
607 608 609
	g_object_class_install_properties (oclass, NUM_PROPERTIES, properties);

	g_type_class_add_private (class, sizeof (NautilusBookmarkDetails));
610 611 612
}

static void
613
nautilus_bookmark_init (NautilusBookmark *bookmark)
614
{
Cosimo Cecchi's avatar
Cosimo Cecchi committed
615 616
	bookmark->details = G_TYPE_INSTANCE_GET_PRIVATE (bookmark, NAUTILUS_TYPE_BOOKMARK,
							 NautilusBookmarkDetails);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
617 618

	bookmark->details->exists = TRUE;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
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 649 650 651 652 653 654 655 656
}

const gchar *
nautilus_bookmark_get_name (NautilusBookmark *bookmark)
{
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (bookmark), NULL);

	return bookmark->details->name;
}

gboolean
nautilus_bookmark_get_has_custom_name (NautilusBookmark *bookmark)
{
	g_return_val_if_fail(NAUTILUS_IS_BOOKMARK (bookmark), FALSE);

	return (bookmark->details->has_custom_name);
}

/**
 * nautilus_bookmark_set_custom_name:
 *
 * Change the user-displayed name of a bookmark.
 * @new_name: The new user-displayed name for this bookmark, mustn't be NULL.
 *
 **/
void
nautilus_bookmark_set_custom_name (NautilusBookmark *bookmark,
				   const char *new_name)
{
	g_return_if_fail (new_name != NULL);
	g_return_if_fail (NAUTILUS_IS_BOOKMARK (bookmark));

	g_object_set (bookmark,
		      "custom-name", TRUE,
		      "name", new_name,
		      NULL);

	g_signal_emit (bookmark, signals[CONTENTS_CHANGED], 0);
657 658
}

659 660 661 662 663 664 665 666 667 668
/**
 * nautilus_bookmark_compare_with:
 *
 * Check whether two bookmarks are considered identical.
 * @a: first NautilusBookmark*.
 * @b: second NautilusBookmark*.
 * 
 * Return value: 0 if @a and @b have same name and uri, 1 otherwise 
 * (GCompareFunc style)
 **/
669
int		    
670
nautilus_bookmark_compare_with (gconstpointer a, gconstpointer b)
671
{
672 673
	NautilusBookmark *bookmark_a;
	NautilusBookmark *bookmark_b;
674

675 676
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (a), 1);
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (b), 1);
677

678 679
	bookmark_a = NAUTILUS_BOOKMARK (a);
	bookmark_b = NAUTILUS_BOOKMARK (b);
680

681 682 683
	if (!g_file_equal (bookmark_a->details->location,
			   bookmark_b->details->location)) {
		return 1;
684 685
	}
	
Cosimo Cecchi's avatar
Cosimo Cecchi committed
686 687
	if (g_strcmp0 (bookmark_a->details->name,
		       bookmark_b->details->name) != 0) {
688 689 690
		return 1;
	}
	
691
	return 0;
692 693
}

694 695
GIcon *
nautilus_bookmark_get_symbolic_icon (NautilusBookmark *bookmark)
696
{
697
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (bookmark), NULL);
698

699 700 701 702 703 704 705
	/* Try to connect a file in case file exists now but didn't earlier. */
	nautilus_bookmark_connect_file (bookmark);

	if (bookmark->details->symbolic_icon) {
		return g_object_ref (bookmark->details->symbolic_icon);
	}
	return NULL;
706 707
}

Alexander Larsson's avatar
Alexander Larsson committed
708
GIcon *
709
nautilus_bookmark_get_icon (NautilusBookmark *bookmark)
710 711
{
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (bookmark), NULL);
712

713 714 715
	/* Try to connect a file in case file exists now but didn't earlier. */
	nautilus_bookmark_connect_file (bookmark);

Alexander Larsson's avatar
Alexander Larsson committed
716 717 718 719
	if (bookmark->details->icon) {
		return g_object_ref (bookmark->details->icon);
	}
	return NULL;
720 721
}

Alexander Larsson's avatar
Alexander Larsson committed
722 723
GFile *
nautilus_bookmark_get_location (NautilusBookmark *bookmark)
724
{
725 726
	g_return_val_if_fail(NAUTILUS_IS_BOOKMARK (bookmark), NULL);

727 728 729 730 731 732 733 734
	/* Try to connect a file in case file exists now but didn't earlier.
	 * This allows a bookmark to update its image properly in the case
	 * where a new file appears with the same URI as a previously-deleted
	 * file. Calling connect_file here means that attempts to activate the 
	 * bookmark will update its image if possible. 
	 */
	nautilus_bookmark_connect_file (bookmark);

Alexander Larsson's avatar
Alexander Larsson committed
735 736 737 738 739 740 741 742 743 744 745 746 747
	return g_object_ref (bookmark->details->location);
}

char *
nautilus_bookmark_get_uri (NautilusBookmark *bookmark)
{
	GFile *file;
	char *uri;

	file = nautilus_bookmark_get_location (bookmark);
	uri = g_file_get_uri (file);
	g_object_unref (file);
	return uri;
748 749
}

750
NautilusBookmark *
Cosimo Cecchi's avatar
Cosimo Cecchi committed
751
nautilus_bookmark_new (GFile *location,
752
		       const gchar *custom_name)
753 754 755
{
	NautilusBookmark *new_bookmark;

Cosimo Cecchi's avatar
Cosimo Cecchi committed
756 757 758 759 760
	new_bookmark = NAUTILUS_BOOKMARK (g_object_new (NAUTILUS_TYPE_BOOKMARK,
							"location", location,
							"name", custom_name,
							"custom-name", custom_name != NULL,
							NULL));
761

762
	return new_bookmark;
763
}				 
764

765
/**
766
 * nautilus_bookmark_menu_item_new:
767 768 769
 * 
 * Return a menu item representing a bookmark.
 * @bookmark: The bookmark the menu item represents.
770
 * Return value: A newly-created bookmark, not yet shown.
771 772
 **/ 
GtkWidget *
773
nautilus_bookmark_menu_item_new (NautilusBookmark *bookmark)
774 775
{
	GtkWidget *menu_item;
776
	GtkLabel *label;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
777
	const char *name;
778 779

	name = nautilus_bookmark_get_name (bookmark);
780
	menu_item = gtk_menu_item_new_with_label (name);
781
	label = GTK_LABEL (gtk_bin_get_child (GTK_BIN (menu_item)));
782 783 784
	gtk_label_set_use_underline (label, FALSE);
	gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
	gtk_label_set_max_width_chars (label, ELLIPSISED_MENU_ITEM_MIN_CHARS);
785 786 787

	return menu_item;
}
788

789 790 791 792 793 794 795 796 797 798 799 800 801
void
nautilus_bookmark_set_scroll_pos (NautilusBookmark      *bookmark,
				  const char            *uri)
{
	g_free (bookmark->details->scroll_file);
	bookmark->details->scroll_file = g_strdup (uri);
}

char *
nautilus_bookmark_get_scroll_pos (NautilusBookmark      *bookmark)
{
	return g_strdup (bookmark->details->scroll_file);
}
Cosimo Cecchi's avatar
Cosimo Cecchi committed
802 803 804 805 806 807

gboolean
nautilus_bookmark_get_exists (NautilusBookmark *bookmark)
{
	return bookmark->details->exists;
}