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

/*
4 5 6
 *  GtkPlacesSidebar - sidebar widget for places in the filesystem
 *
 *  This code comes from Nautilus, GNOME's file manager.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU 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
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Authors : Mr Jamie McCracken (jamiemcc at blueyonder dot co dot uk)
 *            Cosimo Cecchi <cosimoc@gnome.org>
24
 *            Federico Mena Quintero <federico@gnome.org>
25 26
 *
 */
27

28 29 30 31 32 33 34 35
/* TODO:
 *
 * * Fix instances of "#if DO_NOT_COMPILE"
 *
 * * Fix instances of "#if 0"
 *
 * * Fix FIXMEs
 *
36
 * * Grep for "NULL-GError" and see if they should be taken care of
37 38 39 40 41 42
 *
 * * Although we do g_mount_unmount_with_operation(), Nautilus used to do
 *   nautilus_file_operations_unmount_mount_full() to unmount a volume.  With
 *   that, Nautilus does the "volume has trash, empty it first?" dance.  Cosimo
 *   suggests that this logic should be part of GtkMountOperation, which can
 *   have Unix-specific code for emptying trash.
43 44 45 46 47 48 49 50 51 52 53 54
 *
 * * Grep for the comment containing "machinery" in this file, and copy that machinery
 *   from Nautilus - it's to get symbolic icons for everything.
 *
 * * Add getters for all the setters.
 *
 * * Add GObject properties.
 *
 * * Add docstrings for signals.
 *
 * * Write reference docs.
 *
55 56 57 58
 * * Sync nautilus commit 17a85b78acc78b573c2e1776b348ed348e19adb7
 * * Sync nautilus commit 1f307c367ddd8193a38f500e731b7ca0f9b2f98e
 * * Sync nautilus commit 1dcd2d816a1278f7eec1d074ec5d9134af34a803
 * * Sync nautilus commit 71ad0d3adc2553ef5bcd06ec32600302e9169765
59
 *
60 61
 */

62
#include "config.h"
63

64 65
#include <gio/gio.h>

66
#include "gdk/gdkkeysyms.h"
67
#include "gtkbookmarksmanager.h"
68 69 70
#include "gtkcelllayout.h"
#include "gtkcellrenderertext.h"
#include "gtkcellrendererpixbuf.h"
71
#include "gtkfilechooserprivate.h"
72 73 74 75
#include "gtkicontheme.h"
#include "gtkimagemenuitem.h"
#include "gtkintl.h"
#include "gtkmain.h"
76
#include "gtkmarshalers.h"
77 78
#include "gtkmenuitem.h"
#include "gtkmountoperation.h"
79 80
#include "gtkplacessidebar.h"
#include "gtkscrolledwindow.h"
81
#include "gtkseparatormenuitem.h"
82 83
#include "gtksettings.h"
#include "gtkstock.h"
84
#include "gtktrashmonitor.h"
85 86
#include "gtktreeselection.h"
#include "gtktreednd.h"
87
#include "gtktypebuiltins.h"
88
#include "gtkwindow.h"
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * SECTION:gtkplacessidebar
 * @Short_description: Sidebar that displays frequently-used places in the file system
 * @Title: GtkPlacesSidebar
 * @See_also: #GtkFileChooser
 *
 * #GtkPlacesSidebar is a widget that displays a list of frequently-used places in the
 * file system:  the user's home directory, the user's bookmarks, and volumes and drives.
 * This widget is used as a sidebar in #GtkFileChooser and may be used by file managers
 * and similar programs.
 *
 * The places sidebar displays drives and volumes, and will automatically mount
 * or unmount them when the user selects them.
 *
 * Applications can hook to various signals in the places sidebar to customize
 * its behavior.  For example, they can add extra commands to the context menu
 * of the sidebar.
 *
 * While bookmarks are completely in control of the user, the places sidebar also
 * allows individual applications to provide extra shortcut folders that are unique
 * to each application.  For example, a Paint program may want to add a shortcut
111
 * for a Clipart folder.  You can do this with gtk_places_sidebar_add_shortcut().
112 113 114 115 116 117 118 119
 *
 * To make use of the places sidebar, an application at least needs to connect
 * to the #GtkPlacesSidebar::open-location signal.  This is emitted when the
 * user selects in the sidebar a location to open.  The application should also
 * call gtk_places_sidebar_set_location() when it changes the currently-viewed
 * location.
 */

120 121 122
#define EJECT_BUTTON_XPAD 6
#define ICON_CELL_XPAD 6

123 124
#define DO_NOT_COMPILE 0

125
struct _GtkPlacesSidebar {
126 127 128 129 130 131 132 133 134 135
	GtkScrolledWindow parent;

	GtkTreeView		*tree_view;
	GtkCellRenderer		*eject_icon_cell_renderer;
	GtkListStore		*store;
	GtkBookmarksManager	*bookmarks_manager;
	GVolumeMonitor		*volume_monitor;
	GtkTrashMonitor		*trash_monitor;

	gulong trash_monitor_changed_id;
136 137 138 139 140 141 142 143 144 145 146 147 148 149

	gboolean devices_header_added;
	gboolean bookmarks_header_added;

	/* DnD */
	GList     *drag_list;
	gboolean  drag_data_received;
	int       drag_data_info;
	gboolean  drop_occured;

	GtkWidget *popup_menu;

	/* volume mounting - delayed open process */
	gboolean mounting;
150
	GtkPlacesOpenFlags go_to_after_mount_open_flags;
151

152 153
	GSList *shortcuts;

154
	GDBusProxy *hostnamed_proxy;
155
	GCancellable *hostnamed_cancellable;
156
	char *hostname;
157

158 159
	GtkPlacesOpenFlags open_flags;

160
	guint show_desktop : 1;
161
	guint accept_uri_drops : 1;
162
};
163

164
struct _GtkPlacesSidebarClass {
165
	GtkScrolledWindowClass parent;
166

167
	void (* open_location)         (GtkPlacesSidebar *sidebar,
168
				        GFile            *location,
169
				        GtkPlacesOpenFlags open_flags);
170 171 172
	void (* populate_popup)        (GtkPlacesSidebar *sidebar,
					GtkMenu          *menu,
					GFile            *selected_item);
173 174 175
	void (* show_error_message)    (GtkPlacesSidebar *sidebar,
				        const char       *primary,
				        const char       *secondary);
176 177 178 179 180
	void (* drag_action_requested) (GtkPlacesSidebar *sidebar,
					GdkDragContext   *context,
					const char       *uri,
					GList            *uri_list,
					int              *action);
181 182
	GdkDragAction (* drag_action_ask) (GtkPlacesSidebar *sidebar,
					   GdkDragAction     actions);
183 184 185 186
	void (* drag_perform_drop) (GtkPlacesSidebar     *sidebar,
				    GList                *uris,
				    const char           *drop_uri,
				    GdkDragAction         action);
187
};
188 189 190 191 192 193 194 195

enum {
	PLACES_SIDEBAR_COLUMN_ROW_TYPE,
	PLACES_SIDEBAR_COLUMN_URI,
	PLACES_SIDEBAR_COLUMN_DRIVE,
	PLACES_SIDEBAR_COLUMN_VOLUME,
	PLACES_SIDEBAR_COLUMN_MOUNT,
	PLACES_SIDEBAR_COLUMN_NAME,
196
	PLACES_SIDEBAR_COLUMN_GICON,
197 198 199 200 201 202 203 204 205 206 207 208 209
	PLACES_SIDEBAR_COLUMN_INDEX,
	PLACES_SIDEBAR_COLUMN_EJECT,
	PLACES_SIDEBAR_COLUMN_NO_EJECT,
	PLACES_SIDEBAR_COLUMN_BOOKMARK,
	PLACES_SIDEBAR_COLUMN_TOOLTIP,
	PLACES_SIDEBAR_COLUMN_SECTION_TYPE,
	PLACES_SIDEBAR_COLUMN_HEADING_TEXT,

	PLACES_SIDEBAR_COLUMN_COUNT
};

typedef enum {
	PLACES_BUILT_IN,
210
	PLACES_XDG_DIR,
211 212 213 214 215 216 217 218 219 220 221 222
	PLACES_MOUNTED_VOLUME,
	PLACES_BOOKMARK,
	PLACES_HEADING,
} PlaceType;

typedef enum {
	SECTION_DEVICES,
	SECTION_BOOKMARKS,
	SECTION_COMPUTER,
	SECTION_NETWORK,
} SectionType;

223
enum {
224
	OPEN_LOCATION,
225
	POPULATE_POPUP,
226
	SHOW_ERROR_MESSAGE,
227
	DRAG_ACTION_REQUESTED,
228
	DRAG_ACTION_ASK,
229
	DRAG_PERFORM_DROP,
230 231 232
	LAST_SIGNAL,
};

233
/* Names for themed icons */
234
#define ICON_NAME_HOME		"user-home-symbolic"
235
#define ICON_NAME_DESKTOP	"user-desktop"
236
#define ICON_NAME_FILESYSTEM	"drive-harddisk-symbolic"
237
#define ICON_NAME_EJECT		"media-eject-symbolic"
238
#define ICON_NAME_NETWORK	"network-workgroup-symbolic"
239

240
#define ICON_NAME_FOLDER_DESKTOP	"user-desktop"
241 242 243 244 245 246 247 248
#define ICON_NAME_FOLDER_DOCUMENTS	"folder-documents-symbolic"
#define ICON_NAME_FOLDER_DOWNLOAD	"folder-download-symbolic"
#define ICON_NAME_FOLDER_MUSIC		"folder-music-symbolic"
#define ICON_NAME_FOLDER_PICTURES	"folder-pictures-symbolic"
#define ICON_NAME_FOLDER_PUBLIC_SHARE	"folder-publicshare-symbolic"
#define ICON_NAME_FOLDER_TEMPLATES	"folder-templates-symbolic"
#define ICON_NAME_FOLDER_VIDEOS		"folder-videos-symbolic"
#define ICON_NAME_FOLDER_SAVED_SEARCH	"folder-saved-search-symbolic"
249

250 251 252
/* Settings keys */
#define SETTINGS_KEY_STARTUP_MODE        "startup-mode"

253
static guint places_sidebar_signals [LAST_SIGNAL] = { 0 };
254

255
static void  open_selected_bookmark                    (GtkPlacesSidebar        *sidebar,
256 257 258
							GtkTreeModel            *model,
							GtkTreeIter             *iter,
							GtkPlacesOpenFlags       open_flags);
259
static void  gtk_places_sidebar_style_set         (GtkWidget                    *widget,
260
							GtkStyle                     *previous_style);
261
static gboolean eject_or_unmount_bookmark              (GtkPlacesSidebar *sidebar,
262
							GtkTreePath *path);
263
static gboolean eject_or_unmount_selection             (GtkPlacesSidebar *sidebar);
264 265 266 267 268 269 270 271
static void  check_unmount_and_eject                   (GMount *mount,
							GVolume *volume,
							GDrive *drive,
							gboolean *show_unmount,
							gboolean *show_eject);

/* Identifiers for target types */
enum {
272 273
	GTK_TREE_MODEL_ROW,
	TEXT_URI_LIST
274 275 276
};

/* Target types for dragging from the shortcuts list */
277
static const GtkTargetEntry dnd_source_targets[] = {
278 279 280 281
	{ "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
};

/* Target types for dropping into the shortcuts list */
282
static const GtkTargetEntry dnd_drop_targets [] = {
283 284 285 286 287 288
	{ "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
	{ "text/uri-list", 0, TEXT_URI_LIST }
};

/* Drag and drop interface declarations */
typedef struct {
289
	GtkListStore parent;
290

291 292
	GtkPlacesSidebar *sidebar;
} ShortcutsModel;
293 294

typedef struct {
295 296
	GtkListStoreClass parent_class;
} ShortcutsModelClass;
297

298 299
#define SHORTCUTS_MODEL_TYPE (shortcuts_model_get_type ())
#define SHORTCUTS_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_TYPE, ShortcutsModel))
300

301 302
static GType shortcuts_model_get_type (void);
static void shortcuts_model_drag_source_iface_init (GtkTreeDragSourceIface *iface);
303

304 305 306
G_DEFINE_TYPE_WITH_CODE (ShortcutsModel,
			 shortcuts_model,
			 GTK_TYPE_LIST_STORE,
307
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
308
						shortcuts_model_drag_source_iface_init));
309

310
static GtkListStore *shortcuts_model_new (GtkPlacesSidebar *sidebar);
311

312
G_DEFINE_TYPE (GtkPlacesSidebar, gtk_places_sidebar, GTK_TYPE_SCROLLED_WINDOW);
313

314
static void
315
emit_open_location (GtkPlacesSidebar *sidebar, GFile *location, GtkPlacesOpenFlags open_flags)
316
{
317 318 319
	if ((open_flags & sidebar->open_flags) == 0)
		open_flags = GTK_PLACES_OPEN_NORMAL;

320
	g_signal_emit (sidebar, places_sidebar_signals[OPEN_LOCATION], 0,
321
		       location, open_flags);
322 323
}

324 325 326 327 328 329 330
static void
emit_populate_popup (GtkPlacesSidebar *sidebar, GtkMenu *menu, GFile *selected_item)
{
	g_signal_emit (sidebar, places_sidebar_signals[POPULATE_POPUP], 0,
		       menu, selected_item);
}

331 332 333 334 335 336 337
static void
emit_show_error_message (GtkPlacesSidebar *sidebar, const char *primary, const char *secondary)
{
	g_signal_emit (sidebar, places_sidebar_signals[SHOW_ERROR_MESSAGE], 0,
		       primary, secondary);
}

338 339 340 341 342 343 344 345 346 347 348 349 350 351
static void
emit_drag_action_requested (GtkPlacesSidebar *sidebar,
			    GdkDragContext *context,
			    const char *uri,
			    GList *uri_list,
			    int *action)
{
	g_signal_emit (sidebar, places_sidebar_signals[DRAG_ACTION_REQUESTED], 0,
		       context,
		       uri,
		       uri_list,
		       action);
}

352 353 354 355 356 357 358 359 360 361 362 363 364 365
static GdkDragAction
emit_drag_action_ask (GtkPlacesSidebar *sidebar,
		      GdkDragAction actions)
{
	GdkDragAction ret_action;

	ret_action = 0;

	g_signal_emit (sidebar, places_sidebar_signals[DRAG_ACTION_ASK], 0,
		       actions,
		       &ret_action);
	return ret_action;
}

366 367 368 369 370 371 372 373 374 375 376 377
static void
emit_drag_perform_drop (GtkPlacesSidebar *sidebar,
			GList *uris,
			const char *drop_uri,
			GdkDragAction action)
{
	g_signal_emit (sidebar, places_sidebar_signals[DRAG_PERFORM_DROP], 0,
		       uris,
		       drop_uri,
		       action);
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391
static gint
get_icon_size (GtkPlacesSidebar *sidebar)
{
	GtkSettings *settings;
	gint width, height;

	settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (sidebar)));

	if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
		return MAX (width, height);
	else
		return 16;
}

392
static GtkTreeIter
393
add_heading (GtkPlacesSidebar *sidebar,
394 395 396
	     SectionType section_type,
	     const gchar *title)
{
397
	GtkTreeIter iter;
398 399 400 401

	gtk_list_store_append (sidebar->store, &iter);
	gtk_list_store_set (sidebar->store, &iter,
			    PLACES_SIDEBAR_COLUMN_ROW_TYPE, PLACES_HEADING,
402
			    PLACES_SIDEBAR_COLUMN_SECTION_TYPE, section_type,
403 404 405 406 407
			    PLACES_SIDEBAR_COLUMN_HEADING_TEXT, title,
			    PLACES_SIDEBAR_COLUMN_EJECT, FALSE,
			    PLACES_SIDEBAR_COLUMN_NO_EJECT, TRUE,
			    -1);

408
	return iter;
409 410 411
}

static void
412
check_heading_for_section (GtkPlacesSidebar *sidebar,
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
			   SectionType section_type)
{
	switch (section_type) {
	case SECTION_DEVICES:
		if (!sidebar->devices_header_added) {
			add_heading (sidebar, SECTION_DEVICES,
				     _("Devices"));
			sidebar->devices_header_added = TRUE;
		}

		break;
	case SECTION_BOOKMARKS:
		if (!sidebar->bookmarks_header_added) {
			add_heading (sidebar, SECTION_BOOKMARKS,
				     _("Bookmarks"));
			sidebar->bookmarks_header_added = TRUE;
		}

		break;
	default:
		break;
	}
}

437
static void
438
add_place (GtkPlacesSidebar *sidebar,
439 440 441 442 443 444 445 446 447 448 449
	   PlaceType place_type,
	   SectionType section_type,
	   const char *name,
	   GIcon *icon,
	   const char *uri,
	   GDrive *drive,
	   GVolume *volume,
	   GMount *mount,
	   const int index,
	   const char *tooltip)
{
450
	GtkTreeIter           iter;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	gboolean show_eject, show_unmount;
	gboolean show_eject_button;

	check_heading_for_section (sidebar, section_type);

	check_unmount_and_eject (mount, volume, drive,
				 &show_unmount, &show_eject);

	if (show_unmount || show_eject) {
		g_assert (place_type != PLACES_BOOKMARK);
	}

	if (mount == NULL) {
		show_eject_button = FALSE;
	} else {
		show_eject_button = (show_unmount || show_eject);
	}

	gtk_list_store_append (sidebar->store, &iter);
	gtk_list_store_set (sidebar->store, &iter,
471
			    PLACES_SIDEBAR_COLUMN_GICON, icon,
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
			    PLACES_SIDEBAR_COLUMN_NAME, name,
			    PLACES_SIDEBAR_COLUMN_URI, uri,
			    PLACES_SIDEBAR_COLUMN_DRIVE, drive,
			    PLACES_SIDEBAR_COLUMN_VOLUME, volume,
			    PLACES_SIDEBAR_COLUMN_MOUNT, mount,
			    PLACES_SIDEBAR_COLUMN_ROW_TYPE, place_type,
			    PLACES_SIDEBAR_COLUMN_INDEX, index,
			    PLACES_SIDEBAR_COLUMN_EJECT, show_eject_button,
			    PLACES_SIDEBAR_COLUMN_NO_EJECT, !show_eject_button,
			    PLACES_SIDEBAR_COLUMN_BOOKMARK, place_type != PLACES_BOOKMARK,
			    PLACES_SIDEBAR_COLUMN_TOOLTIP, tooltip,
			    PLACES_SIDEBAR_COLUMN_SECTION_TYPE, section_type,
			    -1);
}

487
static GIcon *
488
special_directory_get_gicon (GUserDirectory directory)
489
{
490 491 492
#define ICON_CASE(x)				\
	case G_USER_DIRECTORY_ ## x:					\
		return g_themed_icon_new (ICON_NAME_FOLDER_ ## x);
493 494

	switch (directory) {
495

496 497 498 499 500 501 502 503 504
		ICON_CASE (DOCUMENTS);
		ICON_CASE (DOWNLOAD);
		ICON_CASE (MUSIC);
		ICON_CASE (PICTURES);
		ICON_CASE (PUBLIC_SHARE);
		ICON_CASE (TEMPLATES);
		ICON_CASE (VIDEOS);

	default:
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
		return g_themed_icon_new ("folder-symbolic");
	}

#undef ICON_CASE
}

static gboolean
recent_is_supported (void)
{
	const char * const *supported;
	int i;

	supported = g_vfs_get_supported_uri_schemes (g_vfs_get_default ());
	if (!supported) {
		return FALSE;
520 521
	}

522 523 524 525 526 527
	for (i = 0; supported[i] != NULL; i++) {
		if (strcmp ("recent", supported[i]) == 0) {
			return TRUE;
		}
	}
	return FALSE;
528 529
}

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
static void
add_special_dirs (GtkPlacesSidebar *sidebar)
{
	GList *dirs;
	int index;

	dirs = NULL;
	for (index = 0; index < G_USER_N_DIRECTORIES; index++) {
		const char *path;
		GFile *root;
		GIcon *icon;
		char *name;
		char *mount_uri;
		char *tooltip;

545
		if (!_gtk_bookmarks_manager_get_is_xdg_dir_builtin (index)) {
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
			continue;
		}

		path = g_get_user_special_dir (index);

		/* xdg resets special dirs to the home directory in case
		 * it's not finiding what it expects. We don't want the home
		 * to be added multiple times in that weird configuration.
		 */
		if (path == NULL
		    || g_strcmp0 (path, g_get_home_dir ()) == 0
		    || g_list_find_custom (dirs, path, (GCompareFunc) g_strcmp0) != NULL) {
			continue;
		}

		root = g_file_new_for_path (path);
		name = g_file_get_basename (root);
		icon = special_directory_get_gicon (index);
		mount_uri = g_file_get_uri (root);
		tooltip = g_file_get_parse_name (root);

		add_place (sidebar, PLACES_XDG_DIR,
			   SECTION_COMPUTER,
			   name, icon, mount_uri,
			   NULL, NULL, NULL, 0,
			   tooltip);
		g_free (name);
		g_object_unref (root);
		g_object_unref (icon);
		g_free (mount_uri);
		g_free (tooltip);

		dirs = g_list_prepend (dirs, (char *)path);
	}

	g_list_free (dirs);
}

static char *
get_home_directory_uri (void)
{
	const char *home;

	home = g_get_home_dir ();
	if (!home)
		return NULL;

	return g_strconcat ("file://", home, NULL);
}

static char *
get_desktop_directory_uri (void)
598 599 600 601 602 603 604 605 606 607
{
	const char *name;

	name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
	/* "To disable a directory, point it to the homedir."
	 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
	 **/
	if (!g_strcmp0 (name, g_get_home_dir ())) {
		return NULL;
	} else {
608
		return g_strconcat ("file://", name, NULL);
609 610 611
	}
}

612 613 614 615 616 617 618 619 620 621 622 623 624
static void
add_application_shortcuts (GtkPlacesSidebar *sidebar)
{
	GSList *l;

	for (l = sidebar->shortcuts; l; l = l->next) {
		GFile *file;
		GFileInfo *info;

		file = G_FILE (l->data);

		/* FIXME: we are getting file info synchronously.  We may want to do it async at some point. */
		info = g_file_query_info (file,
625
					  "standard::display-name,standard::symbolic-icon",
626 627 628 629 630 631 632 633 634 635 636
					  G_FILE_QUERY_INFO_NONE,
					  NULL,
					  NULL); /* NULL-GError */

		if (info) {
			char *uri;
			char *tooltip;
			const char *name;
			GIcon *icon;

			name = g_file_info_get_display_name (info);
637
			icon = g_file_info_get_symbolic_icon (info);
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

			uri = g_file_get_uri (file);
			tooltip = g_file_get_parse_name (file);

			add_place (sidebar, PLACES_BUILT_IN,
				   SECTION_COMPUTER,
				   name, icon, uri,
				   NULL, NULL, NULL, 0,
				   tooltip);

			g_free (uri);
			g_free (tooltip);

			g_object_unref (info);
		}
	}
}

656 657 658 659 660 661 662 663 664 665 666
static gboolean
get_selected_iter (GtkPlacesSidebar *sidebar,
		   GtkTreeIter *iter)
{
	GtkTreeSelection *selection;

	selection = gtk_tree_view_get_selection (sidebar->tree_view);

	return gtk_tree_selection_get_selected (selection, NULL, iter);
}

667
static void
668
update_places (GtkPlacesSidebar *sidebar)
669
{
670
	GtkTreeIter iter;
671 672 673 674 675 676 677
	GVolumeMonitor *volume_monitor;
	GList *mounts, *l, *ll;
	GMount *mount;
	GList *drives;
	GDrive *drive;
	GList *volumes;
	GVolume *volume;
678 679
	GSList *bookmarks, *sl;
	int index;
680
	char *original_uri, *mount_uri, *name, *identifier;
681
	char *home_uri;
682
	char *bookmark_name;
683 684 685
	GIcon *icon;
	GFile *root;
	char *tooltip;
686
	GList *network_mounts, *network_volumes;
687

688 689 690 691 692 693 694
	/* save original selection */
	if (get_selected_iter (sidebar, &iter)) {
		gtk_tree_model_get (GTK_TREE_MODEL (sidebar->store),
				    &iter,
				    PLACES_SIDEBAR_COLUMN_URI, &original_uri, -1);
	} else
		original_uri = NULL;
695 696 697 698 699 700

	gtk_list_store_clear (sidebar->store);

	sidebar->devices_header_added = FALSE;
	sidebar->bookmarks_header_added = FALSE;

701
	network_mounts = network_volumes = NULL;
702 703
	volume_monitor = sidebar->volume_monitor;

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	/* add built in bookmarks */

	add_heading (sidebar, SECTION_COMPUTER,
		     _("Places"));

	if (recent_is_supported ()) {
		mount_uri = "recent:///"; /* No need to strdup */
		icon = g_themed_icon_new ("document-open-recent-symbolic");
		add_place (sidebar, PLACES_BUILT_IN,
			   SECTION_COMPUTER,
			   _("Recent"), icon, mount_uri,
			   NULL, NULL, NULL, 0,
			   _("Recent files"));
		g_object_unref (icon);
	}

	/* home folder */
721
	home_uri = get_home_directory_uri ();
722 723 724 725
	icon = g_themed_icon_new (ICON_NAME_HOME);
	add_place (sidebar, PLACES_BUILT_IN,
		   SECTION_COMPUTER,
		   _("Home"), icon,
726
		   home_uri, NULL, NULL, NULL, 0,
727 728
		   _("Open your personal folder"));
	g_object_unref (icon);
729
	g_free (home_uri);
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

	if (sidebar->show_desktop) {
		/* desktop */
		mount_uri = get_desktop_directory_uri ();
		icon = g_themed_icon_new (ICON_NAME_DESKTOP);
		add_place (sidebar, PLACES_BUILT_IN,
			   SECTION_COMPUTER,
			   _("Desktop"), icon,
			   mount_uri, NULL, NULL, NULL, 0,
			   _("Open the contents of your desktop in a folder"));
		g_object_unref (icon);
		g_free (mount_uri);
	}

	/* XDG directories */
	add_special_dirs (sidebar);

747 748 749 750 751 752 753 754 755
	/* Trash */
	mount_uri = "trash:///"; /* No need to strdup */
	icon = _gtk_trash_monitor_get_icon (sidebar->trash_monitor);
	add_place (sidebar, PLACES_BUILT_IN,
		   SECTION_COMPUTER,
		   _("Trash"), icon, mount_uri,
		   NULL, NULL, NULL, 0,
		   _("Open the trash"));
	g_object_unref (icon);
756

757 758 759
	/* Application-side shortcuts */
	add_application_shortcuts (sidebar);

760
	/* go through all connected drives */
761 762 763 764 765 766 767 768 769
	drives = g_volume_monitor_get_connected_drives (volume_monitor);

	for (l = drives; l != NULL; l = l->next) {
		drive = l->data;

		volumes = g_drive_get_volumes (drive);
		if (volumes != NULL) {
			for (ll = volumes; ll != NULL; ll = ll->next) {
				volume = ll->data;
770 771 772 773 774 775 776 777 778
				identifier = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_CLASS);

				if (g_strcmp0 (identifier, "network") == 0) {
					g_free (identifier);
					network_volumes = g_list_prepend (network_volumes, volume);
					continue;
				}
				g_free (identifier);

779 780 781
				mount = g_volume_get_mount (volume);
				if (mount != NULL) {
					/* Show mounted volume in the sidebar */
782
					icon = g_mount_get_symbolic_icon (mount);
783 784 785 786 787
					root = g_mount_get_default_location (mount);
					mount_uri = g_file_get_uri (root);
					name = g_mount_get_name (mount);
					tooltip = g_file_get_parse_name (root);

788 789 790 791
					add_place (sidebar, PLACES_MOUNTED_VOLUME,
						   SECTION_DEVICES,
						   name, icon, mount_uri,
						   drive, volume, mount, 0, tooltip);
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
					g_object_unref (root);
					g_object_unref (mount);
					g_object_unref (icon);
					g_free (tooltip);
					g_free (name);
					g_free (mount_uri);
				} else {
					/* Do show the unmounted volumes in the sidebar;
					 * this is so the user can mount it (in case automounting
					 * is off).
					 *
					 * Also, even if automounting is enabled, this gives a visual
					 * cue that the user should remember to yank out the media if
					 * he just unmounted it.
					 */
807
					icon = g_volume_get_symbolic_icon (volume);
808 809 810
					name = g_volume_get_name (volume);
					tooltip = g_strdup_printf (_("Mount and open %s"), name);

811 812 813 814
					add_place (sidebar, PLACES_MOUNTED_VOLUME,
						   SECTION_DEVICES,
						   name, icon, NULL,
						   drive, volume, NULL, 0, tooltip);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
					g_object_unref (icon);
					g_free (name);
					g_free (tooltip);
				}
				g_object_unref (volume);
			}
			g_list_free (volumes);
		} else {
			if (g_drive_is_media_removable (drive) && !g_drive_is_media_check_automatic (drive)) {
				/* If the drive has no mountable volumes and we cannot detect media change.. we
				 * display the drive in the sidebar so the user can manually poll the drive by
				 * right clicking and selecting "Rescan..."
				 *
				 * This is mainly for drives like floppies where media detection doesn't
				 * work.. but it's also for human beings who like to turn off media detection
				 * in the OS to save battery juice.
				 */
832
				icon = g_drive_get_symbolic_icon (drive);
833 834 835
				name = g_drive_get_name (drive);
				tooltip = g_strdup_printf (_("Mount and open %s"), name);

836 837 838 839
				add_place (sidebar, PLACES_BUILT_IN,
					   SECTION_DEVICES,
					   name, icon, NULL,
					   drive, NULL, NULL, 0, tooltip);
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
				g_object_unref (icon);
				g_free (tooltip);
				g_free (name);
			}
		}
		g_object_unref (drive);
	}
	g_list_free (drives);

	/* add all volumes that is not associated with a drive */
	volumes = g_volume_monitor_get_volumes (volume_monitor);
	for (l = volumes; l != NULL; l = l->next) {
		volume = l->data;
		drive = g_volume_get_drive (volume);
		if (drive != NULL) {
		    	g_object_unref (volume);
			g_object_unref (drive);
			continue;
		}
859 860 861 862 863 864 865 866 867 868

		identifier = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_CLASS);

		if (g_strcmp0 (identifier, "network") == 0) {
			g_free (identifier);
			network_volumes = g_list_prepend (network_volumes, volume);
			continue;
		}
		g_free (identifier);

869 870
		mount = g_volume_get_mount (volume);
		if (mount != NULL) {
871
			icon = g_mount_get_symbolic_icon (mount);
872 873 874 875 876
			root = g_mount_get_default_location (mount);
			mount_uri = g_file_get_uri (root);
			tooltip = g_file_get_parse_name (root);
			g_object_unref (root);
			name = g_mount_get_name (mount);
877 878 879 880
			add_place (sidebar, PLACES_MOUNTED_VOLUME,
				   SECTION_DEVICES,
				   name, icon, mount_uri,
				   NULL, volume, mount, 0, tooltip);
881 882 883 884 885 886 887
			g_object_unref (mount);
			g_object_unref (icon);
			g_free (name);
			g_free (tooltip);
			g_free (mount_uri);
		} else {
			/* see comment above in why we add an icon for an unmounted mountable volume */
888
			icon = g_volume_get_symbolic_icon (volume);
889
			name = g_volume_get_name (volume);
890 891 892 893
			add_place (sidebar, PLACES_MOUNTED_VOLUME,
				   SECTION_DEVICES,
				   name, icon, NULL,
				   NULL, volume, NULL, 0, name);
894 895 896 897 898 899
			g_object_unref (icon);
			g_free (name);
		}
		g_object_unref (volume);
	}
	g_list_free (volumes);
900

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
	/* file system root */

 	mount_uri = "file:///"; /* No need to strdup */
	icon = g_themed_icon_new (ICON_NAME_FILESYSTEM);
	add_place (sidebar, PLACES_BUILT_IN,
		   SECTION_DEVICES,
		   sidebar->hostname, icon,
		   mount_uri, NULL, NULL, NULL, 0,
		   _("Open the contents of the File System"));
	g_object_unref (icon);

	/* add mounts that has no volume (/etc/mtab mounts, ftp, sftp,...) */
	mounts = g_volume_monitor_get_mounts (volume_monitor);

	for (l = mounts; l != NULL; l = l->next) {
		mount = l->data;
		if (g_mount_is_shadowed (mount)) {
			g_object_unref (mount);
			continue;
		}
		volume = g_mount_get_volume (mount);
		if (volume != NULL) {
		    	g_object_unref (volume);
			g_object_unref (mount);
			continue;
		}
		root = g_mount_get_default_location (mount);

		if (!g_file_is_native (root)) {
			network_mounts = g_list_prepend (network_mounts, mount);
931
			g_object_unref (root);
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
			continue;
		}

		icon = g_mount_get_symbolic_icon (mount);
		mount_uri = g_file_get_uri (root);
		name = g_mount_get_name (mount);
		tooltip = g_file_get_parse_name (root);
		add_place (sidebar, PLACES_MOUNTED_VOLUME,
			   SECTION_COMPUTER,
			   name, icon, mount_uri,
			   NULL, NULL, mount, 0, tooltip);
		g_object_unref (root);
		g_object_unref (mount);
		g_object_unref (icon);
		g_free (name);
		g_free (mount_uri);
		g_free (tooltip);
	}
	g_list_free (mounts);

952 953
	/* add bookmarks */

954
	bookmarks = _gtk_bookmarks_manager_list_bookmarks (sidebar->bookmarks_manager);
955

956
	for (sl = bookmarks, index = 0; sl; sl = sl->next, index++) {
957 958
		GFileInfo *info;

959 960 961 962
		root = sl->data;

#if 0
		/* FIXME: remove this?  If we *do* show bookmarks for nonexistent files, the user will eventually clean them up */
963
		if (!nautilus_bookmark_get_exists (bookmark)) {
964 965
			continue;
		}
966
#endif
967

968
		if (_gtk_bookmarks_manager_get_is_builtin (sidebar->bookmarks_manager, root)) {
969 970 971
			continue;
		}

972 973
		/* FIXME: we are getting file info synchronously.  We may want to do it async at some point. */
		info = g_file_query_info (root,
974
					  "standard::display-name,standard::symbolic-icon",
975 976 977
					  G_FILE_QUERY_INFO_NONE,
					  NULL,
					  NULL); /* NULL-GError */
978

979 980
		if (info) {
			bookmark_name = _gtk_bookmarks_manager_get_bookmark_label (sidebar->bookmarks_manager, root);
981

982 983
			if (bookmark_name == NULL)
				bookmark_name = g_strdup (g_file_info_get_display_name (info));
984

985
			icon = g_file_info_get_symbolic_icon (info);
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001

			mount_uri = g_file_get_uri (root);
			tooltip = g_file_get_parse_name (root);

			add_place (sidebar, PLACES_BOOKMARK,
				   SECTION_BOOKMARKS,
				   bookmark_name, icon, mount_uri,
				   NULL, NULL, NULL, index,
				   tooltip);

			g_free (mount_uri);
			g_free (tooltip);
			g_free (bookmark_name);

			g_object_unref (info);
		}
1002 1003
	}

1004 1005 1006
	g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
	g_slist_free (bookmarks);

1007 1008 1009
	/* network */
	add_heading (sidebar, SECTION_NETWORK,
		     _("Network"));
1010

1011 1012 1013 1014 1015 1016 1017 1018
 	mount_uri = "network:///"; /* No need to strdup */
	icon = g_themed_icon_new (ICON_NAME_NETWORK);
	add_place (sidebar, PLACES_BUILT_IN,
		   SECTION_NETWORK,
		   _("Browse Network"), icon,
		   mount_uri, NULL, NULL, NULL, 0,
		   _("Browse the contents of the network"));
	g_object_unref (icon);
1019

1020 1021 1022 1023
	network_volumes = g_list_reverse (network_volumes);
	for (l = network_volumes; l != NULL; l = l->next) {
		volume = l->data;
		mount = g_volume_get_mount (volume);
1024

1025 1026
		if (mount != NULL) {
			network_mounts = g_list_prepend (network_mounts, mount);
1027
			continue;
1028 1029 1030 1031
		} else {
			icon = g_volume_get_symbolic_icon (volume);
			name = g_volume_get_name (volume);
			tooltip = g_strdup_printf (_("Mount and open %s"), name);
1032

1033 1034 1035 1036 1037 1038 1039
			add_place (sidebar, PLACES_MOUNTED_VOLUME,
				   SECTION_NETWORK,
				   name, icon, NULL,
				   NULL, volume, NULL, 0, tooltip);
			g_object_unref (icon);
			g_free (name);
			g_free (tooltip);
1040 1041 1042
		}
	}

1043
	g_list_free_full (network_volumes, g_object_unref);
1044 1045 1046 1047 1048

	network_mounts = g_list_reverse (network_mounts);
	for (l = network_mounts; l != NULL; l = l->next) {
		mount = l->data;
		root = g_mount_get_default_location (mount);
1049
		icon = g_mount_get_symbolic_icon (mount);
1050 1051 1052
		mount_uri = g_file_get_uri (root);
		name = g_mount_get_name (mount);
		tooltip = g_file_get_parse_name (root);
1053 1054 1055 1056
		add_place (sidebar, PLACES_MOUNTED_VOLUME,
			   SECTION_NETWORK,
			   name, icon, mount_uri,
			   NULL, NULL, mount, 0, tooltip);
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		g_object_unref (root);
		g_object_unref (mount);
		g_object_unref (icon);
		g_free (name);
		g_free (mount_uri);
		g_free (tooltip);
	}

	g_list_free_full (network_mounts, g_object_unref);

1067 1068 1069 1070 1071 1072 1073
	/* restore original selection */
	if (original_uri) {
		GFile *restore;

		restore = g_file_new_for_uri (original_uri);
		gtk_places_sidebar_set_location (sidebar, restore);
		g_object_unref (restore);
1074

1075 1076
		g_free (original_uri);
	}
1077 1078 1079 1080 1081
}

static void
mount_added_callback (GVolumeMonitor *volume_monitor,
		      GMount *mount,
1082
		      GtkPlacesSidebar *sidebar)
1083 1084 1085 1086 1087 1088 1089
{
	update_places (sidebar);
}

static void
mount_removed_callback (GVolumeMonitor *volume_monitor,
			GMount *mount,
1090
			GtkPlacesSidebar *sidebar)
1091 1092 1093 1094 1095 1096 1097
{
	update_places (sidebar);
}

static void
mount_changed_callback (GVolumeMonitor *volume_monitor,
			GMount *mount,
1098
			GtkPlacesSidebar *sidebar)
1099 1100 1101 1102 1103 1104 1105
{
	update_places (sidebar);
}

static void
volume_added_callback (GVolumeMonitor *volume_monitor,
		       GVolume *volume,
1106
		       GtkPlacesSidebar *sidebar)
1107 1108 1109 1110 1111 1112 1113
{
	update_places (sidebar);
}

static void
volume_removed_callback (GVolumeMonitor *volume_monitor,
			 GVolume *volume,
1114
			 GtkPlacesSidebar *sidebar)
1115 1116 1117 1118 1119 1120 1121
{
	update_places (sidebar);
}

static void
volume_changed_callback (GVolumeMonitor *volume_monitor,
			 GVolume *volume,
1122
			 GtkPlacesSidebar *sidebar)
1123 1124 1125 1126 1127 1128 1129
{
	update_places (sidebar);
}

static void
drive_disconnected_callback (GVolumeMonitor *volume_monitor,
			     GDrive         *drive,
1130
			     GtkPlacesSidebar *sidebar)
1131 1132 1133 1134 1135 1136 1137
{
	update_places (sidebar);
}

static void
drive_connected_callback (GVolumeMonitor *volume_monitor,
			  GDrive         *drive,
1138
			  GtkPlacesSidebar *sidebar)
1139 1140 1141 1142 1143 1144 1145
{
	update_places (sidebar);
}

static void
drive_changed_callback (GVolumeMonitor *volume_monitor,
			GDrive         *drive,
1146
			GtkPlacesSidebar *sidebar)
1147 1148 1149 1150 1151
{
	update_places (sidebar);
}

static gboolean
1152
over_eject_button (GtkPlacesSidebar *sidebar,
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
		   gint x,
		   gint y,
		   GtkTreePath **path)
{
	GtkTreeViewColumn *column;
	int width, x_offset, hseparator;
	int eject_button_size;
	gboolean show_eject;
	GtkTreeIter iter;
	GtkTreeModel *model;

	*path = NULL;
	model = gtk_tree_view_get_model (sidebar->tree_view);

	if (gtk_tree_view_get_path_at_pos (sidebar->tree_view,
					   x, y,
					   path, &column, NULL, NULL)) {

		gtk_tree_model_get_iter (model, &iter, *path);
		gtk_tree_model_get (model, &iter,
				    PLACES_SIDEBAR_COLUMN_EJECT, &show_eject,
				    -1);

		if (!show_eject) {
			goto out;
		}


		gtk_widget_style_get (GTK_WIDGET (sidebar->tree_view),
				      "horizontal-separator", &hseparator,
				      NULL);

		/* Reload cell attributes for this particular row */
		gtk_tree_view_column_cell_set_cell_data (column,
							 model, &iter, FALSE, FALSE);

		gtk_tree_view_column_cell_get_position (column,
							sidebar->eject_icon_cell_renderer,
							&x_offset, &width);

1193
		eject_button_size = get_icon_size (sidebar);
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215

		/* This is kinda weird, but we have to do it to workaround gtk+ expanding
		 * the eject cell renderer (even thought we told it not to) and we then
		 * had to set it right-aligned */
		x_offset += width - hseparator - EJECT_BUTTON_XPAD - eject_button_size;

		if (x - x_offset >= 0 &&
		    x - x_offset <= eject_button_size) {
			return TRUE;
		}
	}

 out:
	if (*path != NULL) {
		gtk_tree_path_free (*path);
		*path = NULL;
	}

	return FALSE;
}

static gboolean
1216
clicked_eject_button (GtkPlacesSidebar *sidebar,
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
		      GtkTreePath **path)
{
	GdkEvent *event = gtk_get_current_event ();
	GdkEventButton *button_event = (GdkEventButton *) event;

	if ((event->type == GDK_BUTTON_PRESS || event->type == GDK_BUTTON_RELEASE) &&
	     over_eject_button (sidebar, button_event->x, button_event->y, path)) {
		return TRUE;
	}

	return FALSE;
}

/* Computes the appropriate row and position for dropping */
static gboolean
1232
compute_drop_position (GtkTreeView             *tree_view,
1233 1234 1235 1236
		       int                      x,
		       int                      y,
		       GtkTreePath            **path,
		       GtkTreeViewDropPosition *pos,
1237
		       GtkPlacesSidebar        *sidebar)
1238 1239 1240 1241 1242 1243 1244
{
	GtkTreeModel *model;
	GtkTreeIter iter;
	PlaceType place_type;
	SectionType section_type;

	if (!gtk_tree_view_get_dest_row_at_pos (tree_view,
1245 1246
						x, y,
						path, pos)) {
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
		return FALSE;
	}

	model = gtk_tree_view_get_model (tree_view);

	gtk_tree_model_get_iter (model, &iter, *path);
	gtk_tree_model_get (model, &iter,
			    PLACES_SIDEBAR_COLUMN_ROW_TYPE, &place_type,
			    PLACES_SIDEBAR_COLUMN_SECTION_TYPE, &section_type,
			    -1);

1258 1259
	if (section_type != SECTION_BOOKMARKS &&
	    place_type == PLACES_HEADING) {
1260
		/* never drop on headings, but special case the bookmarks heading,
1261 1262
		 * so we can drop bookmarks in between it and the first item when
		 * reordering.
1263 1264 1265 1266
		 */

		gtk_tree_path_free (*path);
		*path = NULL;
1267

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
		return FALSE;
	}

	if (section_type != SECTION_BOOKMARKS &&
	    sidebar->drag_data_received &&
	    sidebar->drag_data_info == GTK_TREE_MODEL_ROW) {
		/* don't allow dropping bookmarks into non-bookmark areas */
		gtk_tree_path_free (*path);
		*path = NULL;

		return FALSE;
	}

1281 1282 1283
	if (sidebar->drag_data_received &&
	    sidebar->drag_data_info == GTK_TREE_MODEL_ROW) {
		/* bookmark rows can only be reordered */
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
		*pos = GTK_TREE_VIEW_DROP_AFTER;
	} else {
		*pos = GTK_TREE_VIEW_DROP_INTO_OR_BEFORE;
	}

	return TRUE;
}

static gboolean
get_drag_data (GtkTreeView *tree_view,
1294
	       GdkDragContext *context,
1295 1296 1297 1298
	       unsigned int time)
{
	GdkAtom target;

1299 1300
	target = gtk_drag_dest_find_target (GTK_WIDGET (tree_view),
					    context,
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
					    NULL);

	if (target == GDK_NONE) {
		return FALSE;
	}

	gtk_drag_get_data (GTK_WIDGET (tree_view),
			   context, target, time);

	return TRUE;
}

static void
1314
free_drag_data (GtkPlacesSidebar *sidebar)
1315 1316 1317 1318
{
	sidebar->drag_data_received = FALSE;

	if (sidebar->drag_list) {
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1319
		g_list_free_full (sidebar->drag_list, g_free);
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
		sidebar->drag_list = NULL;
	}
}

static gboolean
drag_motion_callback (GtkTreeView *tree_view,
		      GdkDragContext *context,
		      int x,
		      int y,
		      unsigned int time,
1330
		      GtkPlacesSidebar *sidebar)
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
{
	GtkTreePath *path;
	GtkTreeViewDropPosition pos;
	int action;
	GtkTreeIter iter;
	char *uri;
	gboolean res;

	if (!sidebar->drag_data_received) {
		if (!get_drag_data (tree_view, context, time)) {
			return FALSE;
		}
	}

	path = NULL;
	res = compute_drop_position (tree_view, x, y, &path, &pos, sidebar);

	if (!res) {
		goto out;
	}

1352
	if (pos == GTK_TREE_VIEW_DROP_AFTER) {
1353 1354 1355 1356 1357 1358 1359
		if (sidebar->drag_data_received &&
		    sidebar->drag_data_info == GTK_TREE_MODEL_ROW) {
			action = GDK_ACTION_MOVE;
		} else {
			action = 0;
		}
	} else {
1360
		action = 0;
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
		if (sidebar->accept_uri_drops) {
			if (sidebar->drag_list != NULL) {
				gtk_tree_model_get_iter (GTK_TREE_MODEL (sidebar->store),
							 &iter, path);
				gtk_tree_model_get (GTK_TREE_MODEL (sidebar->store),
						    &iter,
						    PLACES_SIDEBAR_COLUMN_URI, &uri,
						    -1);
				emit_drag_action_requested (sidebar, context, uri, sidebar->drag_list, &action);
				g_free (uri);
			}
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
		}
	}

	if (action != 0) {
		gtk_tree_view_set_drag_dest_row (tree_view, path, pos);
	}

	if (path != NULL) {
		gtk_tree_path_free (path);
	}

 out:
	g_signal_stop_emission_by_name (tree_view, "drag-motion");

1386
	gdk_drag_status (context, action, time);
1387 1388 1389 1390 1391 1392 1393 1394

	return TRUE;
}

static void
drag_leave_callback (GtkTreeView *tree_view,
		     GdkDragContext *context,
		     unsigned int time,
1395
		     GtkPlacesSidebar *sidebar)
1396 1397
{
	free_drag_data (sidebar);
1398
	gtk_tree_view_set_drag_dest_row (tree_view, NULL, 0);
1399 1400 1401
	g_signal_stop_emission_by_name (tree_view, "drag-leave");
}

1402 1403
/* Takes an array of URIs and turns it into a list of string URIs */
static GList *
1404
build_uri_list (const char **uris)
1405 1406 1407 1408 1409 1410
{
	GList *result;
	int i;

	result = NULL;
	for (i = 0; uris[i]; i++) {
1411
		char *uri;
1412

1413 1414 1415
		uri = g_strdup (uris[i]);
		result = g_list_prepend (result, uri);
	}
1416 1417 1418 1419 1420 1421

	return g_list_reverse (result);
}

/* Reorders the selected bookmark to the specified position */
static void
1422
reorder_bookmarks (GtkPlacesSidebar *sidebar,
1423
		   int               new_position)
1424 1425
{
	GtkTreeIter iter;
1426 1427
	char *uri;
	GFile *file;
1428 1429

	/* Get the selected path */
1430 1431 1432
	if (!get_selected_iter (sidebar, &iter)) {
		return;
	}
1433

1434
	gtk_tree_model_get (GTK_TREE_MODEL (sidebar->store), &iter,
1435
			    PLACES_SIDEBAR_COLUMN_URI, &uri,
1436 1437
			    -1);

1438 1439
	file = g_file_new_for_uri (uri);
	_gtk_bookmarks_manager_reorder_bookmark (sidebar->bookmarks_manager, file, new_position, NULL); /* NULL-GError */
1440

1441 1442
	g_object_unref (file);
	g_free (uri);
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
}

static void
drag_data_received_callback (GtkWidget *widget,
			     GdkDragContext *context,
			     int x,
			     int y,
			     GtkSelectionData *selection_data,
			     unsigned int info,
			     unsigned int time,
1453
			     GtkPlacesSidebar *sidebar)
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
{
	GtkTreeView *tree_view;
	GtkTreePath *tree_path;
	GtkTreeViewDropPosition tree_pos;
	GtkTreeIter iter;
	int position;
	GtkTreeModel *model;
	PlaceType place_type;
	SectionType section_type;
	gboolean success;

	tree_view = GTK_TREE_VIEW (widget);

	if (!sidebar->drag_data_received) {
		if (gtk_selection_data_get_target (selection_data) != GDK_NONE &&
		    info == TEXT_URI_LIST) {
1470 1471 1472
			char **uris;

			uris = gtk_selection_data_get_uris (selection_data);
1473
			sidebar->drag_list = build_uri_list ((const char **) uris);
1474
			g_strfreev (uris);
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
		} else {
			sidebar->drag_list = NULL;
		}
		sidebar->drag_data_received = TRUE;
		sidebar->drag_data_info = info;
	}

	g_signal_stop_emission_by_name (widget, "drag-data-received");

	if (!sidebar->drop_occured) {
		return;
	}

	/* Compute position */
1489 1490 1491 1492
	success = compute_drop_position (tree_view, x, y, &tree_path, &tree_pos, sidebar);
	if (!success) {
		goto out;
	}
1493 1494 1495

	success = FALSE;

1496
	if (tree_pos == GTK_TREE_VIEW_DROP_AFTER) {
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
		model = gtk_tree_view_get_model (tree_view);

		if (!gtk_tree_model_get_iter (model, &iter, tree_path)) {
			goto out;
		}

		gtk_tree_model_get (model, &iter,
				    PLACES_SIDEBAR_COLUMN_SECTION_TYPE, &section_type,
    				    PLACES_SIDEBAR_COLUMN_ROW_TYPE, &place_type,
				    PLACES_SIDEBAR_COLUMN_INDEX, &position,
				    -1);

		if (section_type != SECTION_BOOKMARKS) {
			goto out;
		}

		if (tree_pos == GTK_TREE_VIEW_DROP_AFTER && place_type != PLACES_HEADING) {
			/* heading already has position 0 */
			position++;
		}

		switch (info) {
		case GTK_TREE_MODEL_ROW:
			reorder_bookmarks (sidebar, position);
			success = TRUE;
			break;
		default:
			g_assert_not_reached ();
			break;
		}
	} else {
		GdkDragAction real_action;
1529 1530
		char **uris;
		GList *uri_list;
1531
		char *drop_uri;
1532 1533 1534 1535

		/* file transfer requested */
		real_action = gdk_drag_context_get_selected_action (context);

1536 1537
		if (real_action == GDK_ACTION_ASK)
			real_action = emit_drag_action_ask (sidebar, gdk_drag_context_get_actions (context));
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548

		if (real_action > 0) {
			model = gtk_tree_view_get_model (tree_view);

			gtk_tree_model_get_iter (model, &iter, tree_path);
			gtk_tree_model_get (model, &iter,
					    PLACES_SIDEBAR_COLUMN_URI, &drop_uri,
					    -1);

			switch (info) {
			case TEXT_URI_LIST:
1549 1550 1551 1552 1553
				uris = gtk_selection_data_get_uris (selection_data);
				uri_list = build_uri_list ((const char **) uris);
				emit_drag_perform_drop (sidebar, uri_list, drop_uri, real_action);
				g_list_free_full (uri_list, g_free);
				g_strfreev (uris);
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
				success = TRUE;
				break;
			case GTK_TREE_MODEL_ROW:
				success = FALSE;
				break;
			default:
				g_assert_not_reached ();
				break;
			}

			g_free (drop_uri);
		}
	}

out:
	sidebar->drop_occured = FALSE;
	free_drag_data (sidebar);
	gtk_drag_finish (context, success, FALSE, time);

	gtk_tree_path_free (tree_path);
}

static gboolean
drag_drop_callback (GtkTreeView *tree_view,
		    GdkDragContext *context,
		    int x,
		    int y,
		    unsigned int time,
1582
		    GtkPlacesSidebar *sidebar)
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
{
	gboolean retval = FALSE;
	sidebar->drop_occured = TRUE;
	retval = get_drag_data (tree_view, context, time);
	g_signal_stop_emission_by_name (tree_view, "drag-drop");
	return retval;
}

/* Callback used when the file list's popup menu is detached */
static void
bookmarks_popup_menu_detach_cb (GtkWidget *attach_widget,
				GtkMenu   *menu)
{
1596 1597 1598 1599
	GtkPlacesSidebar *sidebar;

	sidebar = GTK_PLACES_SIDEBAR (attach_widget);

1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
	sidebar->popup_menu = NULL;
}
static void
check_unmount_and_eject (GMount *mount,
			 GVolume *volume,
			 GDrive *drive,
			 gboolean *show_unmount,
			 gboolean *show_eject)
{
	*show_unmount = FALSE;
	*show_eject = FALSE;

	if (drive != NULL) {
		*show_eject = g_drive_can_eject (drive);
	}

	if (volume != NULL) {
		*show_eject |= g_volume_can_eject (volume);
	}
	if (mount != NULL) {
		*show_eject |= g_mount_can_eject (mount);
		*show_unmount = g_mount_can_unmount (mount) && !*show_eject;
	}
}

static void
check_visibility (GMount           *mount,
		  GVolume          *volume,
		  GDrive           *drive,
		  gboolean         *show_mount,
		  gboolean         *show_unmount,
		  gboolean         *show_eject,
		  gboolean         *show_rescan,
		  gboolean         *show_start,
		  gboolean         *show_stop)
{
	*show_mount = FALSE;
	*show_rescan = FALSE;
	*show_start = FALSE;
	*show_stop = FALSE;

	check_unmount_and_eject (mount, volume, drive, show_unmount, show_eject);

	if (drive != NULL) {
		if (g_drive_is_media_removable (drive) &&
1645
		    !g_drive_is_media_check_automatic (drive) &&
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
		    g_drive_can_poll_for_media (drive))
			*show_rescan = TRUE;

		*show_start = g_drive_can_start (drive) || g_drive_can_start_degraded (drive);
		*show_stop  = g_drive_can_stop (drive);

		if (*show_stop)
			*show_unmount = FALSE;
	}

	if (volume != NULL) {
		if (mount == NULL)
			*show_mount = g_volume_can_mount (volume);
	}
}

1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
typedef struct {
	PlaceType type;
	GDrive *drive;
	GVolume *volume;
	GMount *mount;
	char *uri;
} SelectionInfo;

static void
get_selection_info (GtkPlacesSidebar *sidebar, SelectionInfo *info)
{
	GtkTreeIter iter;
	
	info->type   = PLACES_BUILT_IN;
	info->drive  = NULL;
	info->volume = NULL;
	info->mount  = NULL;
	info->uri    = NULL;

	if (get_selected_iter (sidebar, &iter)) {
		gtk_tree_model_get (GTK_TREE_MODEL (sidebar->store), &iter,
				    PLACES_SIDEBAR_COLUMN_ROW_TYPE, &info->type,
				    PLACES_SIDEBAR_COLUMN_DRIVE, &info->drive,
				    PLACES_SIDEBAR_COLUMN_VOLUME, &info->volume,
 				    PLACES_SIDEBAR_COLUMN_MOUNT, &info->mount,
				    PLACES_SIDEBAR_COLUMN_URI, &info->uri,
				    -1);
	}
}

static void
free_selection_info (SelectionInfo *info)
{
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1695 1696 1697
	g_clear_object (&info->drive);
	g_clear_object (&info->volume);
	g_clear_object (&info->mount);
1698

Cosimo Cecchi's avatar
Cosimo Cecchi committed
1699
	g_clear_pointer (&info->uri, g_free);
1700 1701
}

1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
typedef struct {
	GtkWidget *add_shortcut_item;
	GtkWidget *remove_item;
	GtkWidget *rename_item;
	GtkWidget *separator_item;
	GtkWidget *mount_item;
	GtkWidget *unmount_item;
	GtkWidget *eject_item;
	GtkWidget *rescan_item;
	GtkWidget *start_item;
	GtkWidget *stop_item;
} PopupMenuData;

static void
1716
check_popup_sensitivity (GtkPlacesSidebar *sidebar, PopupMenuData *data, SelectionInfo *info)
1717 1718 1719 1720 1721 1722 1723
{
	gboolean show_mount;
	gboolean show_unmount;
	gboolean show_eject;
	gboolean show_rescan;
	gboolean show_start;
	gboolean show_stop;
1724

1725
	gtk_widget_set_visible (data->add_shortcut_item, (info->type == PLACES_MOUNTED_VOLUME));
1726

1727 1728
	gtk_widget_set_sensitive (data->remove_item, (info->type == PLACES_BOOKMARK));
	gtk_widget_set_sensitive (data->rename_item, (info->type == PLACES_BOOKMARK));
1729

1730
 	check_visibility (info->mount, info->volume, info->drive,
1731 1732
 			  &show_mount, &show_unmount, &show_eject, &show_rescan, &show_start, &show_stop);

1733
	gtk_widget_set_visible (data->separator_item, show_mount || show_unmount || show_eject);
1734 1735 1736 1737 1738 1739
	gtk_widget_set_visible (data->mount_item, show_mount);
	gtk_widget_set_visible (data->unmount_item, show_unmount);
	gtk_widget_set_visible (data->eject_item, show_eject);
	gtk_widget_set_visible (data->rescan_item, show_rescan);
	gtk_widget_set_visible (data->start_item, show_start);
	gtk_widget_set_visible (data->stop_item, show_stop);
1740 1741

	/* Adjust start/stop items to reflect the type of the drive */
1742 1743
	gtk_menu_item_set_label (GTK_MENU_ITEM (data->start_item), _("_Start"));
	gtk_menu_item_set_label (GTK_MENU_ITEM (data->stop_item), _("_Stop"));
1744 1745
	if ((show_start || show_stop) && info->drive != NULL) {
		switch (g_drive_get_start_stop_type (info->drive)) {
1746 1747
		case G_DRIVE_START_STOP_TYPE_SHUTDOWN:
			/* start() for type G_DRIVE_START_STOP_TYPE_SHUTDOWN is normally not used */
1748 1749
			gtk_menu_item_set_label (GTK_MENU_ITEM (data->start_item), _("_Power On"));
			gtk_menu_item_set_label (GTK_MENU_ITEM (data->stop_item), _("_Safely Remove Drive"));
1750 1751
			break;
		case G_DRIVE_START_STOP_TYPE_NETWORK:
1752 1753
			gtk_menu_item_set_label (GTK_MENU_ITEM (data->start_item), _("_Connect Drive"));
			gtk_menu_item_set_label (GTK_MENU_ITEM (data->stop_item), _("_Disconnect Drive"));
1754 1755
			break;
		case G_DRIVE_START_STOP_TYPE_MULTIDISK:
1756 1757
			gtk_menu_item_set_label (GTK_MENU_ITEM (data->start_item), _("_Start Multi-disk Device"));
			gtk_menu_item_set_label (GTK_MENU_ITEM (data->stop_item), _("_Stop Multi-disk Device"));
1758 1759 1760
			break;
		case G_DRIVE_START_STOP_TYPE_PASSWORD:
			/* stop() for type G_DRIVE_START_STOP_TYPE_PASSWORD is normally not used */
1761 1762
			gtk_menu_item_set_label (GTK_MENU_ITEM (data->start_item), _("_Unlock Drive"));
			gtk_menu_item_set_label (GTK_MENU_ITEM (data->stop_item), _("_Lock Drive"));
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
			break;

		default:
		case G_DRIVE_START_STOP_TYPE_UNKNOWN:
			/* uses defaults set above */
			break;
		}
	}
}

static void
1774 1775 1776
drive_start_from_bookmark_cb (GObject      *source_object,
			      GAsyncResult *res,
			      gpointer      user_data)
1777
{
1778
	GtkPlacesSidebar *sidebar;
1779 1780 1781
	GError *error;
	char *primary;
	char *name;
1782

1783
	sidebar = GTK_PLACES_SIDEBAR (user_data);
1784

1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
	error = NULL;
	if (!g_drive_poll_for_media_finish (G_DRIVE (source_object), res, &error)) {
		if (error->code != G_IO_ERROR_FAILED_HANDLED) {
			name = g_drive_get_name (G_DRIVE (source_object));
			primary = g_strdup_printf (_("Unable to start %s"), name);
			g_free (name);
			emit_show_error_message (sidebar, primary, error->message);
			g_free (primary);
		}
		g_error_free (error);
	}
}

/* Callback from g_volume_mount() */
static void
volume_mount_cb (GObject *source_object, GAsyncResult *result, gpointer user_data)
{
	GtkPlacesSidebar *sidebar = GTK_PLACES_SIDEBAR (user_data);
	GVolume *volume;
	GError