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

/* nautilus-bookmark.c - implementation of individual bookmarks.

Elliot Lee's avatar
Elliot Lee committed
5
   Copyright (C) 1999, 2000 Eazel, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

   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>
*/

25
#include <config.h>
26
#include "nautilus-bookmark.h"
27

Darin Adler's avatar
Darin Adler committed
28 29 30
#include "nautilus-gtk-macros.h"
#include "nautilus-icon-factory.h"
#include "nautilus-string.h"
31
#include <gtk/gtkaccellabel.h>
32
#include <gtk/gtksignal.h>
33 34
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-util.h>
35
#include <libgnomeui/gtkpixmapmenuitem.h>
36 37
#include <libgnomevfs/gnome-vfs-types.h>
#include <libgnomevfs/gnome-vfs-uri.h>
Darin Adler's avatar
Darin Adler committed
38
#include <libgnomevfs/gnome-vfs-utils.h>
39
#include <libnautilus-extensions/nautilus-file-utilities.h>
40
#include <libnautilus-extensions/nautilus-gdk-pixbuf-extensions.h>
41
#include <libnautilus-extensions/nautilus-gtk-extensions.h>
42

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

49 50 51
#define GENERIC_BOOKMARK_ICON_NAME	"i-bookmark"
#define MISSING_BOOKMARK_ICON_NAME	"i-bookmark-missing"

52
static guint signals[LAST_SIGNAL];
53

54
struct NautilusBookmarkDetails
55
{
56 57
	char *name;
	char *uri;
58
	NautilusScalableIcon *icon;
59
	NautilusFile *file;
60 61
};

62 63 64 65 66
static void	  nautilus_bookmark_connect_file	  (NautilusBookmark	 *file);
static void	  nautilus_bookmark_disconnect_file	  (NautilusBookmark	 *file);
static void       nautilus_bookmark_initialize_class      (NautilusBookmarkClass *class);
static void       nautilus_bookmark_initialize            (NautilusBookmark      *bookmark);
static GtkWidget *create_pixmap_widget_for_bookmark       (NautilusBookmark 	 *bookmark);
67

68
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusBookmark, nautilus_bookmark, GTK_TYPE_OBJECT)
69 70 71 72 73 74 75 76

/* GtkObject methods.  */

static void
nautilus_bookmark_destroy (GtkObject *object)
{
	NautilusBookmark *bookmark;

77
	g_assert (NAUTILUS_IS_BOOKMARK (object));
78 79

	bookmark = NAUTILUS_BOOKMARK(object);
80

81 82
	nautilus_bookmark_disconnect_file (bookmark);	

83 84 85
	g_free (bookmark->details->name);
	g_free (bookmark->details->uri);
	g_free (bookmark->details);
86 87

	/* Chain up */
88
	NAUTILUS_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
89 90 91 92 93
}

/* Initialization.  */

static void
94
nautilus_bookmark_initialize_class (NautilusBookmarkClass *class)
95 96 97 98 99 100
{
	GtkObjectClass *object_class;

	object_class = GTK_OBJECT_CLASS (class);

	object_class->destroy = nautilus_bookmark_destroy;
101

102 103
	signals[APPEARANCE_CHANGED] =
		gtk_signal_new ("appearance_changed",
104 105
				GTK_RUN_LAST,
				object_class->type,
106 107 108 109 110 111 112 113 114
				GTK_SIGNAL_OFFSET (NautilusBookmarkClass, appearance_changed),
				gtk_marshal_NONE__NONE,
				GTK_TYPE_NONE, 0);

	signals[CONTENTS_CHANGED] =
		gtk_signal_new ("contents_changed",
				GTK_RUN_LAST,
				object_class->type,
				GTK_SIGNAL_OFFSET (NautilusBookmarkClass, contents_changed),
115 116 117 118 119
				gtk_marshal_NONE__NONE,
				GTK_TYPE_NONE, 0);

	gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
				
120 121 122
}

static void
123
nautilus_bookmark_initialize (NautilusBookmark *bookmark)
124
{
125
	bookmark->details = g_new0 (NautilusBookmarkDetails, 1);
126 127
}

128 129 130 131 132 133 134 135 136 137
/**
 * 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)
 **/
138
int		    
139
nautilus_bookmark_compare_with (gconstpointer a, gconstpointer b)
140
{
141 142
	NautilusBookmark *bookmark_a;
	NautilusBookmark *bookmark_b;
143

144 145
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (a), 1);
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (b), 1);
146

147 148
	bookmark_a = NAUTILUS_BOOKMARK (a);
	bookmark_b = NAUTILUS_BOOKMARK (b);
149

150 151
	if (strcmp (bookmark_a->details->name,
		    bookmark_b->details->name) != 0) {
152 153
		return 1;
	}
154 155 156

	if (!nautilus_uris_match (bookmark_a->details->uri,
		    		  bookmark_b->details->uri)) {
157 158 159 160
		return 1;
	}
	
	return 0;
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
/**
 * nautilus_bookmark_compare_uris:
 *
 * Check whether the uris of two bookmarks are for the same location.
 * @a: first NautilusBookmark*.
 * @b: second NautilusBookmark*.
 * 
 * Return value: 0 if @a and @b have matching uri, 1 otherwise 
 * (GCompareFunc style)
 **/
int		    
nautilus_bookmark_compare_uris (gconstpointer a, gconstpointer b)
{
	NautilusBookmark *bookmark_a;
	NautilusBookmark *bookmark_b;

	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (a), 1);
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (b), 1);

	bookmark_a = NAUTILUS_BOOKMARK (a);
	bookmark_b = NAUTILUS_BOOKMARK (b);

	return !nautilus_uris_match (bookmark_a->details->uri,
		    		     bookmark_b->details->uri);
}

189
NautilusBookmark *
190
nautilus_bookmark_copy (NautilusBookmark *bookmark)
191
{
192
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (bookmark), NULL);
193

194 195 196 197
	return nautilus_bookmark_new_with_icon (
			bookmark->details->uri,
			bookmark->details->name,
			bookmark->details->icon);
198 199
}

200 201
char *
nautilus_bookmark_get_name (NautilusBookmark *bookmark)
202
{
203 204
	g_return_val_if_fail(NAUTILUS_IS_BOOKMARK (bookmark), NULL);

205
	return g_strdup (bookmark->details->name);
206 207 208
}

gboolean	    
209
nautilus_bookmark_get_pixmap_and_mask (NautilusBookmark *bookmark,
210 211 212 213
				       guint icon_size,
				       GdkPixmap **pixmap_return,
				       GdkBitmap **mask_return)
{
214
	GdkPixbuf *pixbuf;
215

216 217
	pixbuf = nautilus_bookmark_get_pixbuf (bookmark, icon_size);
	if (pixbuf == NULL) {
218
		return FALSE;
219
	}
220

221
	gdk_pixbuf_render_pixmap_and_mask (pixbuf, pixmap_return, mask_return, NAUTILUS_STANDARD_ALPHA_THRESHHOLD);
222 223 224
	gdk_pixbuf_unref (pixbuf);

	return TRUE;
225 226
}

227
GdkPixbuf *	    
228
nautilus_bookmark_get_pixbuf (NautilusBookmark *bookmark,
229
			      guint icon_size)
230
{
231 232 233
	GdkPixbuf *result;
	NautilusScalableIcon *icon;
	
234
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (bookmark), NULL);
235

236 237
	icon = nautilus_bookmark_get_icon (bookmark);
	if (icon == NULL) {
238
		return NULL;
239
	}
240
	
241
	result = nautilus_icon_factory_get_pixbuf_for_icon
242
		(icon, icon_size, icon_size, icon_size, icon_size, NULL, TRUE);
243 244 245
	nautilus_scalable_icon_unref (icon);
	
	return result;
246
}
247

248
NautilusScalableIcon *
249
nautilus_bookmark_get_icon (NautilusBookmark *bookmark)
250 251
{
	g_return_val_if_fail (NAUTILUS_IS_BOOKMARK (bookmark), NULL);
252

253 254 255
	/* Try to connect a file in case file exists now but didn't earlier. */
	nautilus_bookmark_connect_file (bookmark);

256 257 258 259
	if (bookmark->details->icon != NULL) {
		nautilus_scalable_icon_ref (bookmark->details->icon);
	}
	return bookmark->details->icon;
260 261
}

262 263
char *
nautilus_bookmark_get_uri (NautilusBookmark *bookmark)
264
{
265 266
	g_return_val_if_fail(NAUTILUS_IS_BOOKMARK (bookmark), NULL);

267 268 269 270 271 272 273 274
	/* 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);

275
	return g_strdup (bookmark->details->uri);
276 277
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291

/**
 * nautilus_bookmark_set_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_name (NautilusBookmark *bookmark, const char *new_name)
{
	g_return_if_fail(NAUTILUS_IS_BOOKMARK (bookmark));
	g_return_if_fail (new_name != NULL);

292 293 294 295
	if (strcmp (new_name, bookmark->details->name) == 0) {
		return;
	}

296 297
	g_free (bookmark->details->name);
	bookmark->details->name = g_strdup (new_name);
298

299
	gtk_signal_emit (GTK_OBJECT (bookmark), signals[APPEARANCE_CHANGED]);
300 301
}

302 303 304
static gboolean
nautilus_bookmark_icon_is_different (NautilusBookmark *bookmark,
		   		     NautilusScalableIcon *new_icon)
305
{
306 307 308 309 310 311 312 313 314 315 316 317
	char *new_uri, *new_name;
	char *old_uri, *old_name;
	gboolean result;

	g_assert (NAUTILUS_IS_BOOKMARK (bookmark));
	g_assert (new_icon != NULL);

	/* Bookmarks only care about the uri and name. */
	nautilus_scalable_icon_get_text_pieces 
		(new_icon, &new_uri, &new_name, NULL, NULL);

	if (bookmark->details->icon == NULL) {
318 319
		result = !nautilus_str_is_empty (new_uri)
			|| !nautilus_str_is_empty (new_name);
320 321 322 323
	} else {
		nautilus_scalable_icon_get_text_pieces 
			(bookmark->details->icon, &old_uri, &old_name, NULL, NULL);

324 325
		result = nautilus_strcmp (old_uri, new_uri) != 0
			|| nautilus_strcmp (old_name, new_name) != 0;
326

327 328
		g_free (old_uri);
		g_free (old_name);
329 330
	}

331 332
	g_free (new_uri);
	g_free (new_name);
333

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	return result;
}

/**
 * Update icon if there's a better one available.
 * Return TRUE if the icon changed.
 */
static gboolean
nautilus_bookmark_update_icon (NautilusBookmark *bookmark)
{
	NautilusScalableIcon *new_icon;

	g_assert (NAUTILUS_IS_BOOKMARK (bookmark));

	if (bookmark->details->file == NULL) {
		return FALSE;
	}

	if (nautilus_icon_factory_is_icon_ready_for_file (bookmark->details->file)) {
353 354
		new_icon = nautilus_icon_factory_get_icon_for_file (bookmark->details->file,
								    NULL, FALSE);
355 356 357 358 359 360 361
		if (nautilus_bookmark_icon_is_different (bookmark, new_icon)) {
			if (bookmark->details->icon != NULL) {
				nautilus_scalable_icon_unref (bookmark->details->icon);
			}
			bookmark->details->icon = new_icon;
			return TRUE;
		}
362
		nautilus_scalable_icon_unref (new_icon);
363 364 365 366 367 368 369 370
	}

	return FALSE;
}

static void
bookmark_file_changed_callback (NautilusFile *file, NautilusBookmark *bookmark)
{
371
	char *file_uri;
372 373
	gboolean should_emit_appearance_changed_signal;
	gboolean should_emit_contents_changed_signal;
374

375 376 377 378
	g_assert (NAUTILUS_IS_FILE (file));
	g_assert (NAUTILUS_IS_BOOKMARK (bookmark));
	g_assert (file == bookmark->details->file);

379 380
	should_emit_appearance_changed_signal = FALSE;
	should_emit_contents_changed_signal = FALSE;
381 382
	file_uri = nautilus_file_get_uri (file);

383
	if (!nautilus_uris_match (bookmark->details->uri, file_uri)) {
384 385
		g_free (bookmark->details->uri);
		bookmark->details->uri = file_uri;
386
		should_emit_contents_changed_signal = TRUE;
387 388 389 390 391 392 393 394 395 396 397 398 399
	} else {
		g_free (file_uri);
	}

	if (nautilus_file_is_gone (file)) {
		/* The file we were monitoring has been deleted,
		 * or moved in a way that we didn't notice. Make 
		 * a spanking new NautilusFile object for this 
		 * location so if a new file appears in this place 
		 * we will notice.
		 */
		nautilus_bookmark_disconnect_file (bookmark);
		nautilus_bookmark_connect_file (bookmark);
400
		should_emit_appearance_changed_signal = TRUE;		
401 402 403 404
	} else if (nautilus_bookmark_update_icon (bookmark)) {
		/* File hasn't gone away, but it has changed
		 * in a way that affected its icon.
		 */
405 406 407 408 409
		should_emit_appearance_changed_signal = TRUE;
	}

	if (should_emit_appearance_changed_signal) {
		gtk_signal_emit (GTK_OBJECT (bookmark), signals[APPEARANCE_CHANGED]);
410 411
	}

412 413
	if (should_emit_contents_changed_signal) {
		gtk_signal_emit (GTK_OBJECT (bookmark), signals[CONTENTS_CHANGED]);
414 415 416
	}
}

417 418 419 420 421 422
/**
 * nautilus_bookmark_set_icon_to_default:
 * 
 * Reset the icon to either the missing bookmark icon or the generic
 * bookmark icon, depending on whether the file still exists.
 */
423 424 425
static void
nautilus_bookmark_set_icon_to_default (NautilusBookmark *bookmark)
{
426 427
	const char *icon_name;

428 429 430 431
	if (bookmark->details->icon != NULL) {
		nautilus_scalable_icon_unref (bookmark->details->icon);
	}

432 433 434 435 436 437
	if (nautilus_bookmark_uri_known_not_to_exist (bookmark)) {
		icon_name = MISSING_BOOKMARK_ICON_NAME;
	} else {
		icon_name = GENERIC_BOOKMARK_ICON_NAME;
	}
	bookmark->details->icon = nautilus_scalable_icon_new_from_text_pieces 
438
		(NULL, icon_name, NULL, NULL, FALSE);
439 440
}

441
/**
442
 * nautilus_bookmark_new:
443 444
 *
 * Create a new NautilusBookmark from a text uri and a display name.
445 446 447 448
 * The initial icon for the bookmark will be based on the information 
 * already available without any explicit action on NautilusBookmark's
 * part.
 * 
449 450 451 452 453 454
 * @uri: Any uri, even a malformed or non-existent one.
 * @name: A string to display to the user as the bookmark's name.
 * 
 * Return value: A newly allocated NautilusBookmark.
 * 
 **/
455
NautilusBookmark *
456
nautilus_bookmark_new (const char *uri, const char *name)
457
{
458
	return nautilus_bookmark_new_with_icon (uri, name, NULL);
459 460
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
static void
nautilus_bookmark_disconnect_file (NautilusBookmark *bookmark)
{
	g_assert (NAUTILUS_IS_BOOKMARK (bookmark));
	
	if (bookmark->details->file != NULL) {
		gtk_signal_disconnect_by_func (GTK_OBJECT (bookmark->details->file),
					       bookmark_file_changed_callback,
					       bookmark);
		nautilus_file_unref (bookmark->details->file);
		bookmark->details->file = NULL;
	}

	if (bookmark->details->icon != NULL) {
		nautilus_scalable_icon_unref (bookmark->details->icon);
		bookmark->details->icon = NULL;
	}
}

static void
nautilus_bookmark_connect_file (NautilusBookmark *bookmark)
{
	g_assert (NAUTILUS_IS_BOOKMARK (bookmark));
484 485 486 487 488 489 490 491 492 493 494 495 496 497

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

	if (!nautilus_bookmark_uri_known_not_to_exist (bookmark)) {
		bookmark->details->file = nautilus_file_get (bookmark->details->uri);
		g_assert (!nautilus_file_is_gone (bookmark->details->file));

		gtk_signal_connect (GTK_OBJECT (bookmark->details->file),
				    "changed",
				    bookmark_file_changed_callback,
				    bookmark);
	}	
498 499 500 501 502

	/* Set icon based on available information; don't force network i/o
	 * to get any currently unknown information. 
	 */
	if (!nautilus_bookmark_update_icon (bookmark)) {
503
		if (bookmark->details->icon == NULL || bookmark->details->file == NULL) {
504 505 506 507 508
			nautilus_bookmark_set_icon_to_default (bookmark);
		}
	}
}

509 510 511
NautilusBookmark *
nautilus_bookmark_new_with_icon (const char *uri, const char *name, 
				 NautilusScalableIcon *icon)
512 513 514
{
	NautilusBookmark *new_bookmark;

515 516 517
	new_bookmark = NAUTILUS_BOOKMARK (gtk_object_new (NAUTILUS_TYPE_BOOKMARK, NULL));
	gtk_object_ref (GTK_OBJECT (new_bookmark));
	gtk_object_sink (GTK_OBJECT (new_bookmark));
518

519 520
	new_bookmark->details->name = g_strdup (name);
	new_bookmark->details->uri = g_strdup (uri);
521

522 523 524 525 526
	if (icon != NULL) {
		nautilus_scalable_icon_ref (icon);
	}
	new_bookmark->details->icon = icon;

527
	nautilus_bookmark_connect_file (new_bookmark);
528

529
	return new_bookmark;
530
}				 
531

532
static GtkWidget *
533
create_pixmap_widget_for_bookmark (NautilusBookmark *bookmark)
534 535 536 537 538
{
	GdkPixmap *gdk_pixmap;
	GdkBitmap *mask;

	if (!nautilus_bookmark_get_pixmap_and_mask (bookmark, 
539
						    NAUTILUS_ICON_SIZE_FOR_MENUS,
540
					  	    &gdk_pixmap, 
541
					  	    &mask)) {
542 543 544 545 546 547 548
		return NULL;
	}

	return gtk_pixmap_new (gdk_pixmap, mask);
}

/**
549
 * nautilus_bookmarnuk_menu_item_new:
550 551 552
 * 
 * Return a menu item representing a bookmark.
 * @bookmark: The bookmark the menu item represents.
553
 * Return value: A newly-created bookmark, not yet shown.
554 555
 **/ 
GtkWidget *
556
nautilus_bookmark_menu_item_new (NautilusBookmark *bookmark)
557 558 559
{
	GtkWidget *menu_item;
	GtkWidget *pixmap_widget;
560 561
	GtkWidget *label;
	char *display_name;
562 563 564 565 566 567 568 569 570

	/* Could check gnome_preferences_get_menus_have_icons here, but these
	 * are more important than stock menu icons, since they're connected to
	 * user data. For now let's not let them be turn-offable and see if
	 * anyone objects strenuously.
	 */
	menu_item = gtk_pixmap_menu_item_new ();

	pixmap_widget = create_pixmap_widget_for_bookmark (bookmark);
571
	if (pixmap_widget != NULL) {
572 573 574
		gtk_widget_show (pixmap_widget);
		gtk_pixmap_menu_item_set_pixmap (GTK_PIXMAP_MENU_ITEM (menu_item), pixmap_widget);
	}
575 576 577 578
	display_name = nautilus_truncate_text_for_menu_item (bookmark->details->name);
	label = gtk_label_new (display_name);
	g_free (display_name);
	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
579

580 581
	gtk_container_add (GTK_CONTAINER (menu_item), label);
	gtk_widget_show (label);
582 583 584

	return menu_item;
}
585 586 587 588 589 590 591 592

gboolean
nautilus_bookmark_uri_known_not_to_exist (NautilusBookmark *bookmark)
{
	char *path_name;
	gboolean exists;

	/* Convert to a path, returning FALSE if not local. */
Darin Adler's avatar
Darin Adler committed
593
	path_name = gnome_vfs_get_local_path_from_uri (bookmark->details->uri);
594 595 596 597 598 599 600 601 602
	if (path_name == NULL) {
		return FALSE;
	}

	/* Now check if the file exists (sync. call OK because it is local). */
	exists = g_file_exists (path_name);
	g_free (path_name);
	return !exists;
}