nautilus-window-manage-views.c 55.8 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4 5 6

/*
 *  Nautilus
 *
 *  Copyright (C) 1999, 2000 Red Hat, Inc.
7
 *  Copyright (C) 1999, 2000, 2001 Eazel, Inc.
8
 *
9
 *  Nautilus is free software; you can redistribute it and/or
10 11 12 13
 *  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.
 *
14
 *  Nautilus is distributed in the hope that it will be useful,
15 16 17 18 19
 *  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
20
 *  License along with this program; if not, write to the Free
21 22
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
23 24
 *  Authors: Elliot Lee <sopwith@redhat.com>
 *           John Sullivan <sullivan@eazel.com>
25
 *           Darin Adler <darin@bentspoon.com>
26
 */
27 28

#include <config.h>
29
#include "nautilus-window-manage-views.h"
30

Alexander Larsson's avatar
Alexander Larsson committed
31
#include "nautilus-actions.h"
32 33
#include "nautilus-application.h"
#include "nautilus-location-bar.h"
34
#include "nautilus-search-bar.h"
35
#include "nautilus-pathbar.h"
36
#include "nautilus-window-private.h"
37 38
#include "nautilus-window-slot.h"
#include "nautilus-navigation-window-slot.h"
39
#include "nautilus-trash-bar.h"
40
#include "nautilus-view-factory.h"
41
#include "nautilus-x-content-bar.h"
42
#include "nautilus-navigation-window-pane.h"
43
#include <eel/eel-accessibility.h>
Ramiro Estrugo's avatar
Ramiro Estrugo committed
44 45 46 47
#include <eel/eel-debug.h>
#include <eel/eel-gdk-extensions.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gtk-extensions.h>
48
#include <eel/eel-gtk-macros.h>
Ramiro Estrugo's avatar
Ramiro Estrugo committed
49 50
#include <eel/eel-stock-dialogs.h>
#include <eel/eel-string.h>
51
#include <gtk/gtk.h>
52
#include <gdk/gdkx.h>
53
#include <glib/gi18n.h>
54
#include <libnautilus-extension/nautilus-location-widget-provider.h>
55 56 57 58 59
#include <libnautilus-private/nautilus-file-attributes.h>
#include <libnautilus-private/nautilus-file-utilities.h>
#include <libnautilus-private/nautilus-file.h>
#include <libnautilus-private/nautilus-global-preferences.h>
#include <libnautilus-private/nautilus-metadata.h>
60
#include <libnautilus-private/nautilus-module.h>
61
#include <libnautilus-private/nautilus-monitor.h>
62
#include <libnautilus-private/nautilus-search-directory.h>
63

64 65 66
#define DEBUG_FLAG NAUTILUS_DEBUG_WINDOW
#include <libnautilus-private/nautilus-debug.h>

67
/* FIXME bugzilla.gnome.org 41243: 
John Sullivan's avatar
John Sullivan committed
68
 * We should use inheritance instead of these special cases
69 70 71 72
 * for the desktop window.
 */
#include "nautilus-desktop-window.h"

73 74 75 76
/* This number controls a maximum character count for a URL that is
 * displayed as part of a dialog. It's fairly arbitrary -- big enough
 * to allow most "normal" URIs to display in full, but small enough to
 * prevent the dialog from getting insanely wide.
77 78 79
 */
#define MAX_URI_IN_DIALOG_LENGTH 60

80
static void begin_location_change                     (NautilusWindowSlot         *slot,
Alexander Larsson's avatar
Alexander Larsson committed
81
						       GFile                      *location,
Alexander Larsson's avatar
Alexander Larsson committed
82 83 84
						       GList                      *new_selection,
						       NautilusLocationChangeType  type,
						       guint                       distance,
85 86 87
						       const char                 *scroll_pos,
						       NautilusWindowGoToCallback  callback,
						       gpointer                    user_data);
88 89 90
static void free_location_change                      (NautilusWindowSlot         *slot);
static void end_location_change                       (NautilusWindowSlot         *slot);
static void cancel_location_change                    (NautilusWindowSlot         *slot);
Alexander Larsson's avatar
Alexander Larsson committed
91 92
static void got_file_info_for_view_selection_callback (NautilusFile               *file,
						       gpointer                    callback_data);
93
static void create_content_view                       (NautilusWindowSlot         *slot,
Alexander Larsson's avatar
Alexander Larsson committed
94 95 96
						       const char                 *view_id);
static void display_view_selection_failure            (NautilusWindow             *window,
						       NautilusFile               *file,
Alexander Larsson's avatar
Alexander Larsson committed
97 98
						       GFile                      *location,
						       GError                     *error);
99
static void load_new_location                         (NautilusWindowSlot         *slot,
Alexander Larsson's avatar
Alexander Larsson committed
100
						       GFile                      *location,
Alexander Larsson's avatar
Alexander Larsson committed
101 102 103
						       GList                      *selection,
						       gboolean                    tell_current_content_view,
						       gboolean                    tell_new_content_view);
104 105
static void location_has_really_changed               (NautilusWindowSlot         *slot);
static void update_for_new_location                   (NautilusWindowSlot         *slot);
106

107
/* set_displayed_location:
108 109
 */
static void
110
set_displayed_location (NautilusWindowSlot *slot, GFile *location)
111
{
Alexander Larsson's avatar
Alexander Larsson committed
112
        GFile *bookmark_location;
113
        gboolean recreate;
114 115

        if (slot->current_location_bookmark == NULL || location == NULL) {
116 117
                recreate = TRUE;
        } else {
118
                bookmark_location = nautilus_bookmark_get_location (slot->current_location_bookmark);
Alexander Larsson's avatar
Alexander Larsson committed
119 120
                recreate = !g_file_equal (bookmark_location, location);
                g_object_unref (bookmark_location);
121
        }
122
        
123
        if (recreate) {
124
                /* We've changed locations, must recreate bookmark for current location. */
125 126
		if (slot->last_location_bookmark != NULL)  {
			g_object_unref (slot->last_location_bookmark);
127
                }
128 129
		slot->last_location_bookmark = slot->current_location_bookmark;
		slot->current_location_bookmark = (location == NULL) ? NULL
130
                        : nautilus_bookmark_new (location, NULL, NULL);
131 132 133
        }
}

134
static void
Alexander Larsson's avatar
Alexander Larsson committed
135
check_bookmark_location_matches (NautilusBookmark *bookmark, GFile *location)
136
{
Alexander Larsson's avatar
Alexander Larsson committed
137 138
        GFile *bookmark_location;
        char *bookmark_uri, *uri;
139

Alexander Larsson's avatar
Alexander Larsson committed
140 141 142 143
	bookmark_location = nautilus_bookmark_get_location (bookmark);
	if (!g_file_equal (location, bookmark_location)) {
		bookmark_uri = g_file_get_uri (bookmark_location);
		uri = g_file_get_uri (location);
144
		g_warning ("bookmark uri is %s, but expected %s", bookmark_uri, uri);
Alexander Larsson's avatar
Alexander Larsson committed
145 146
		g_free (uri);
		g_free (bookmark_uri);
147
	}
Alexander Larsson's avatar
Alexander Larsson committed
148
	g_object_unref (bookmark_location);
149 150
}

151 152 153 154 155
/* Debugging function used to verify that the last_location_bookmark
 * is in the state we expect when we're about to use it to update the
 * Back or Forward list.
 */
static void
156
check_last_bookmark_location_matches_slot (NautilusWindowSlot *slot)
157
{
158 159
	check_bookmark_location_matches (slot->last_location_bookmark,
					 slot->location);
160 161
}

162
static void
163 164
handle_go_back (NautilusNavigationWindowSlot *navigation_slot,
		GFile *location)
165
{
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	NautilusWindowSlot *slot;
	guint i;
	GList *link;
	NautilusBookmark *bookmark;

	slot = NAUTILUS_WINDOW_SLOT (navigation_slot);

	/* Going back. Move items from the back list to the forward list. */
	g_assert (g_list_length (navigation_slot->back_list) > slot->location_change_distance);
	check_bookmark_location_matches (NAUTILUS_BOOKMARK (g_list_nth_data (navigation_slot->back_list,
									     slot->location_change_distance)),
					 location);
	g_assert (slot->location != NULL);
	
	/* Move current location to Forward list */
181

182
	check_last_bookmark_location_matches_slot (slot);
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	/* Use the first bookmark in the history list rather than creating a new one. */
	navigation_slot->forward_list = g_list_prepend (navigation_slot->forward_list,
							     slot->last_location_bookmark);
	g_object_ref (navigation_slot->forward_list->data);
				
	/* Move extra links from Back to Forward list */
	for (i = 0; i < slot->location_change_distance; ++i) {
		bookmark = NAUTILUS_BOOKMARK (navigation_slot->back_list->data);
		navigation_slot->back_list =
			g_list_remove (navigation_slot->back_list, bookmark);
		navigation_slot->forward_list =
			g_list_prepend (navigation_slot->forward_list, bookmark);
	}
	
	/* One bookmark falls out of back/forward lists and becomes viewed location */
	link = navigation_slot->back_list;
	navigation_slot->back_list = g_list_remove_link (navigation_slot->back_list, link);
	g_object_unref (link->data);
	g_list_free_1 (link);
203 204 205
}

static void
206 207
handle_go_forward (NautilusNavigationWindowSlot *navigation_slot,
		   GFile *location)
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 239 240 241 242 243 244
	NautilusWindowSlot *slot;
	guint i;
	GList *link;
	NautilusBookmark *bookmark;

	slot = NAUTILUS_WINDOW_SLOT (navigation_slot);

	/* Going forward. Move items from the forward list to the back list. */
	g_assert (g_list_length (navigation_slot->forward_list) > slot->location_change_distance);
	check_bookmark_location_matches (NAUTILUS_BOOKMARK (g_list_nth_data (navigation_slot->forward_list,
									     slot->location_change_distance)),
					 location);
	g_assert (slot->location != NULL);
				
	/* Move current location to Back list */
	check_last_bookmark_location_matches_slot (slot);
	
	/* Use the first bookmark in the history list rather than creating a new one. */
      navigation_slot->back_list = g_list_prepend (navigation_slot->back_list,
							  slot->last_location_bookmark);
      g_object_ref (navigation_slot->back_list->data);
	
	/* Move extra links from Forward to Back list */
      for (i = 0; i < slot->location_change_distance; ++i) {
		bookmark = NAUTILUS_BOOKMARK (navigation_slot->forward_list->data);
		navigation_slot->forward_list =
			g_list_remove (navigation_slot->back_list, bookmark);
		navigation_slot->back_list =
			g_list_prepend (navigation_slot->forward_list, bookmark);
	}
	
	/* One bookmark falls out of back/forward lists and becomes viewed location */
	link = navigation_slot->forward_list;
	navigation_slot->forward_list = g_list_remove_link (navigation_slot->forward_list, link);
	g_object_unref (link->data);
	g_list_free_1 (link);
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static void
handle_go_elsewhere (NautilusWindowSlot *slot,
		     GFile *location)
{
	NautilusNavigationWindowSlot *navigation_slot;

	if (NAUTILUS_IS_NAVIGATION_WINDOW_SLOT (slot)) {
		navigation_slot = NAUTILUS_NAVIGATION_WINDOW_SLOT (slot);

		/* Clobber the entire forward list, and move displayed location to back list */
		nautilus_navigation_window_slot_clear_forward_list (navigation_slot);
		
		if (slot->location != NULL) {
			/* If we're returning to the same uri somehow, don't put this uri on back list. 
			 * This also avoids a problem where set_displayed_location
			 * didn't update last_location_bookmark since the uri didn't change.
			 */
			if (!g_file_equal (slot->location, location)) {
				/* Store bookmark for current location in back list, unless there is no current location */
				check_last_bookmark_location_matches_slot (slot);
				/* Use the first bookmark in the history list rather than creating a new one. */
				navigation_slot->back_list = g_list_prepend (navigation_slot->back_list,
									     slot->last_location_bookmark);
				g_object_ref (navigation_slot->back_list->data);
			}
		}
	}
}

276 277
void
nautilus_window_update_up_button (NautilusWindow *window)
278
{
279 280 281 282 283 284 285 286 287 288 289 290
	NautilusWindowSlot *slot;
	gboolean allowed;
	GFile *parent;

	slot = window->details->active_pane->active_slot;

	allowed = FALSE;
	if (slot->location != NULL) {
		parent = g_file_get_parent (slot->location);
		allowed = parent != NULL;
		if (parent != NULL) {
			g_object_unref (parent);
Alexander Larsson's avatar
Alexander Larsson committed
291
		}
292
	}
293

294
	nautilus_window_allow_up (window, allowed);
295 296
}

297
static void
298
viewed_file_changed_callback (NautilusFile *file,
299
                              NautilusWindowSlot *slot)
300
{
301
	NautilusWindow *window;
Alexander Larsson's avatar
Alexander Larsson committed
302
        GFile *new_location;
303
	gboolean is_in_trash, was_in_trash;
304

305
	window = slot->pane->window;
306

307
        g_assert (NAUTILUS_IS_FILE (file));
308
	g_assert (NAUTILUS_IS_WINDOW_PANE (slot->pane));
309
	g_assert (NAUTILUS_IS_WINDOW (window));
310 311

	g_assert (file == slot->viewed_file);
312

313
        if (!nautilus_file_is_not_yet_confirmed (file)) {
314
                slot->viewed_file_seen = TRUE;
315 316
        }

317
	was_in_trash = slot->viewed_file_in_trash;
318

319
	slot->viewed_file_in_trash = is_in_trash = nautilus_file_is_in_trash (file);
320 321 322

	/* Close window if the file it's viewing has been deleted or moved to trash. */
	if (nautilus_file_is_gone (file) || (is_in_trash && !was_in_trash)) {
323 324
                /* Don't close the window in the case where the
                 * file was never seen in the first place.
325
                 */
326
                if (slot->viewed_file_seen) {
327 328 329
			/* auto-show existing parent. */
			GFile *go_to_file, *parent, *location;

330 331 332 333 334 335 336 337 338 339 340 341 342
                        /* Detecting a file is gone may happen in the
                         * middle of a pending location change, we
                         * need to cancel it before closing the window
                         * or things break.
                         */
                        /* FIXME: It makes no sense that this call is
                         * needed. When the window is destroyed, it
                         * calls nautilus_window_manage_views_destroy,
                         * which calls free_location_change, which
                         * should be sufficient. Also, if this was
                         * really needed, wouldn't it be needed for
                         * all other nautilus_window_close callers?
                         */
343
			end_location_change (slot);
344

345 346 347 348 349 350 351 352
			go_to_file = NULL;
			location =  nautilus_file_get_location (file);
			parent = g_file_get_parent (location);
			g_object_unref (location);
			if (parent) {
				go_to_file = nautilus_find_existing_uri_in_hierarchy (parent);
				g_object_unref (parent);
			}
Alexander Larsson's avatar
Alexander Larsson committed
353
				
354 355 356 357 358 359 360 361 362
			if (go_to_file != NULL) {
				/* the path bar URI will be set to go_to_uri immediately
				 * in begin_location_change, but we don't want the
				 * inexistant children to show up anymore */
				if (slot == slot->pane->active_slot) {
					/* multiview-TODO also update NautilusWindowSlot
					 * [which as of writing doesn't save/store any path bar state]
					 */
					nautilus_path_bar_clear_buttons (NAUTILUS_PATH_BAR (NAUTILUS_NAVIGATION_WINDOW_PANE (slot->pane)->path_bar));
363
				}
364 365 366
				
				nautilus_window_slot_go_to (slot, go_to_file, FALSE);
				g_object_unref (go_to_file);
367
			} else {
368
				nautilus_window_slot_go_home (slot, FALSE);
369
			}
370
                }
371
	} else {
Alexander Larsson's avatar
Alexander Larsson committed
372
                new_location = nautilus_file_get_location (file);
373

374
                /* If the file was renamed, update location and/or
Alexander Larsson's avatar
Alexander Larsson committed
375 376
                 * title. */
                if (!g_file_equal (new_location,
377 378 379
				   slot->location)) {
                        g_object_unref (slot->location);
                        slot->location = new_location;
380
			if (slot == slot->pane->active_slot) {
381
				nautilus_window_pane_sync_location_widgets (slot->pane);
382
			}
383
                } else {
384 385
			/* TODO?
 			 *   why do we update title & icon at all in this case? */
Alexander Larsson's avatar
Alexander Larsson committed
386
                        g_object_unref (new_location);
387
                }
Alexander Larsson's avatar
Alexander Larsson committed
388

389 390
                nautilus_window_slot_update_title (slot);
		nautilus_window_slot_update_icon (slot);
391
        }
392 393
}

394
static void
395
update_history (NautilusWindowSlot *slot,
396
                NautilusLocationChangeType type,
Alexander Larsson's avatar
Alexander Larsson committed
397
                GFile *new_location)
398 399 400
{
        switch (type) {
        case NAUTILUS_LOCATION_CHANGE_STANDARD:
401
        case NAUTILUS_LOCATION_CHANGE_FALLBACK:
402
		handle_go_elsewhere (slot, new_location);
403 404 405 406 407
                return;
        case NAUTILUS_LOCATION_CHANGE_RELOAD:
                /* for reload there is no work to do */
                return;
        case NAUTILUS_LOCATION_CHANGE_BACK:
408
                handle_go_back (NAUTILUS_NAVIGATION_WINDOW_SLOT (slot), new_location);
409 410
                return;
        case NAUTILUS_LOCATION_CHANGE_FORWARD:
411
                handle_go_forward (NAUTILUS_NAVIGATION_WINDOW_SLOT (slot), new_location);
412 413 414 415 416 417 418 419
                return;
        case NAUTILUS_LOCATION_CHANGE_REDIRECT:
                /* for the redirect case, the caller can do the updating */
                return;
        }
	g_return_if_fail (FALSE);
}

420
static void
421
cancel_viewed_file_changed_callback (NautilusWindowSlot *slot)
422
{
423
        NautilusFile *file;
424

425
        file = slot->viewed_file;
Alexander Larsson's avatar
Alexander Larsson committed
426 427 428
        if (file != NULL) {
                g_signal_handlers_disconnect_by_func (G_OBJECT (file),
                                                      G_CALLBACK (viewed_file_changed_callback),
429 430
						      slot);
                nautilus_file_monitor_remove (file, &slot->viewed_file);
431
        }
432
}
433

Alexander Larsson's avatar
Alexander Larsson committed
434
void
435 436 437
nautilus_window_slot_open_location_full (NautilusWindowSlot *slot,
					 GFile *location,
					 NautilusWindowOpenFlags flags,
438 439 440
					 GList *new_selection,
					 NautilusWindowGoToCallback callback,
					 gpointer user_data)
441
{
442
	NautilusWindow *window;
443
        NautilusWindow *target_window;
444
        NautilusWindowPane *pane;
445 446
        NautilusWindowSlot *target_slot;
	NautilusWindowOpenFlags slot_flags;
Alexander Larsson's avatar
Alexander Larsson committed
447 448
	GFile *old_location;
	char *old_uri, *new_uri;
449
	int new_slot_position;
450
	GList *l;
451
	gboolean target_navigation, target_same;
452
	gboolean is_desktop;
453
	NautilusApplication *app;
454

455
	window = slot->pane->window;
456

457
        target_window = NULL;
458
	target_slot = NULL;
459 460
	target_same = FALSE;
	target_navigation = FALSE;
461

462
	old_uri = nautilus_window_slot_get_location_uri (slot);
Alexander Larsson's avatar
Alexander Larsson committed
463 464 465 466
	if (old_uri == NULL) {
		old_uri = g_strdup ("(none)");
	}
	new_uri = g_file_get_uri (location);
467 468 469

	DEBUG ("Opening location, old: %s, new: %s", old_uri, new_uri);

Alexander Larsson's avatar
Alexander Larsson committed
470 471 472
	g_free (old_uri);
	g_free (new_uri);

473 474
	g_assert (!((flags & NAUTILUS_WINDOW_OPEN_FLAG_NEW_WINDOW) != 0 &&
		    (flags & NAUTILUS_WINDOW_OPEN_FLAG_NEW_TAB) != 0));
Alexander Larsson's avatar
 
Alexander Larsson committed
475

476 477 478
	is_desktop = NAUTILUS_IS_DESKTOP_WINDOW (window);
	target_same = is_desktop &&
		!nautilus_desktop_window_loaded (NAUTILUS_DESKTOP_WINDOW (window));
479 480

	old_location = nautilus_window_slot_get_location (slot);
481

482 483 484 485 486
	if (g_settings_get_boolean (nautilus_preferences, NAUTILUS_PREFERENCES_ALWAYS_USE_BROWSER)) {
		/* always use browser: if we're on the desktop the target is a new navigation window,
		 * otherwise it's the same window.
		 */
		if (is_desktop) {
487
			target_navigation = TRUE;
488 489
		} else {
			target_same = TRUE;
490
		}
491
	} else {
492
		target_navigation = TRUE;
493
	}
494

495 496
	app = nautilus_application_dup_singleton ();

497 498 499 500 501
	/* now get/create the window according to the mode */
	if (target_same) {
		target_window = window;
	} else if (target_navigation) {
		target_window = nautilus_application_create_navigation_window
502
			(app,
503 504 505 506
			 NULL,
			 gtk_window_get_screen (GTK_WINDOW (window)));
	}

507 508
	g_object_unref (app);

509 510
        g_assert (target_window != NULL);

511
	if ((flags & NAUTILUS_WINDOW_OPEN_FLAG_NEW_TAB) != 0) {
512 513 514 515
		g_assert (target_window == window);

		slot_flags = 0;

516
		new_slot_position = g_settings_get_enum (nautilus_preferences, NAUTILUS_PREFERENCES_NEW_TAB_POSITION);
517 518 519 520
		if (new_slot_position == NAUTILUS_NEW_TAB_POSITION_END) {
			slot_flags = NAUTILUS_WINDOW_OPEN_SLOT_APPEND;
		}

521
		target_slot = nautilus_window_open_slot (window->details->active_pane, slot_flags);
522 523 524 525 526 527
	}

	if (target_slot == NULL) {
		if (target_window == window) {
			target_slot = slot;
		} else {
528
			target_slot = target_window->details->active_pane->active_slot;
529 530 531
		}
	}

532
        if ((target_window == window && target_slot == slot &&
Alexander Larsson's avatar
Alexander Larsson committed
533
	     old_location && g_file_equal (old_location, location))) {
534 535 536

		if (callback != NULL) {
			callback (window, NULL, user_data);
537
		}
538 539

		g_object_unref (old_location);
540 541
                return;
        }
Alexander Larsson's avatar
Alexander Larsson committed
542 543 544 545
	
	if (old_location) {
		g_object_unref (old_location);
	}
546

547
        begin_location_change (target_slot, location, new_selection,
548
			       NAUTILUS_LOCATION_CHANGE_STANDARD, 0, NULL, callback, user_data);
549 550 551 552 553 554 555 556

	/* Additionally, load this in all slots that have no location, this means
	   we load both panes in e.g. a newly opened dual pane window. */
	for (l = target_window->details->panes; l != NULL; l = l->next) {
		pane = l->data;
		slot = pane->active_slot;
		if (slot->location == NULL && slot->pending_location == NULL) {
			begin_location_change (slot, location, new_selection,
557
					       NAUTILUS_LOCATION_CHANGE_STANDARD, 0, NULL, NULL, NULL);
558 559
		}
	}
560 561
}

562 563
static char *
nautilus_window_slot_get_view_error_label (NautilusWindowSlot *slot)
564
{
565 566
	const NautilusViewInfo *info;

567
	info = nautilus_view_factory_lookup (nautilus_window_slot_get_content_view_id (slot));
568 569 570 571

	return g_strdup (info->error_label);
}

572 573
static char *
nautilus_window_slot_get_view_startup_error_label (NautilusWindowSlot *slot)
574 575 576
{
	const NautilusViewInfo *info;

577
	info = nautilus_view_factory_lookup (nautilus_window_slot_get_content_view_id (slot));
578

579
	return g_strdup (info->startup_error_label);
580 581 582
}

static void
583
report_current_content_view_failure_to_user (NautilusWindowSlot *slot)
584
{
585
	NautilusWindow *window;
586 587
	char *message;

588
	window = slot->pane->window;
589 590 591 592 593

	message = nautilus_window_slot_get_view_startup_error_label (slot);
  	eel_show_error_dialog (message,
  			       _("You can choose another view or go to a different location."),
  			       GTK_WINDOW (window));
594
	g_free (message);
595 596 597
}

static void
598 599
report_nascent_content_view_failure_to_user (NautilusWindowSlot *slot,
					     NautilusView *view)
600
{
601
	NautilusWindow *window;
602 603
	char *message;

604
	window = slot->pane->window;
605 606 607 608 609

	/* TODO? why are we using the current view's error label here, instead of the next view's?
 	 * This behavior has already been present in pre-slot days.
 	 */
	message = nautilus_window_slot_get_view_error_label (slot);
610 611 612 613
	eel_show_error_dialog (message,
			       _("The location cannot be displayed with this viewer."),
			       GTK_WINDOW (window));
	g_free (message);
614 615
}

616

Alexander Larsson's avatar
Alexander Larsson committed
617
const char *
618
nautilus_window_slot_get_content_view_id (NautilusWindowSlot *slot)
619
{
620 621 622 623
	if (slot->content_view == NULL) {
		return NULL;
	}
	return nautilus_view_get_view_id (slot->content_view);
624 625
}

626
gboolean
627 628
nautilus_window_slot_content_view_matches_iid (NautilusWindowSlot *slot, 
					       const char *iid)
629
{
630 631 632
	if (slot->content_view == NULL) {
		return FALSE;
	}
633
	return g_strcmp0 (nautilus_view_get_view_id (slot->content_view), iid) == 0;
634 635
}

636 637 638 639 640 641 642 643 644 645 646 647 648 649
static gboolean
report_callback (NautilusWindowSlot *slot,
		 GError *error)
{
	if (slot->open_callback != NULL) {
		slot->open_callback (slot->pane->window, error, slot->open_callback_user_data);
		slot->open_callback = NULL;
		slot->open_callback_user_data = NULL;

		return TRUE;
	}

	return FALSE;
}
650

Alexander Larsson's avatar
Alexander Larsson committed
651 652 653
/*
 * begin_location_change
 * 
654
 * Change a window slot's location.
Alexander Larsson's avatar
Alexander Larsson committed
655 656 657 658 659 660 661
 * @window: The NautilusWindow whose location should be changed.
 * @location: A url specifying the location to load
 * @new_selection: The initial selection to present after loading the location
 * @type: Which type of location change is this? Standard, back, forward, or reload?
 * @distance: If type is back or forward, the index into the back or forward chain. If
 * type is standard or reload, this is ignored, and must be 0.
 * @scroll_pos: The file to scroll to when the location is loaded.
662 663
 * @callback: function to be called when the location is changed.
 * @user_data: data for @callback.
Alexander Larsson's avatar
Alexander Larsson committed
664 665 666 667
 *
 * This is the core function for changing the location of a window. Every change to the
 * location begins here.
 */
668
static void
669
begin_location_change (NautilusWindowSlot *slot,
Alexander Larsson's avatar
Alexander Larsson committed
670
                       GFile *location,
Alexander Larsson's avatar
Alexander Larsson committed
671 672 673
		       GList *new_selection,
                       NautilusLocationChangeType type,
                       guint distance,
674 675 676
                       const char *scroll_pos,
		       NautilusWindowGoToCallback callback,
		       gpointer user_data)
677
{
Alexander Larsson's avatar
Alexander Larsson committed
678 679 680 681
        NautilusDirectory *directory;
        NautilusFile *file;
	gboolean force_reload;
        char *current_pos;
682

683
	g_assert (slot != NULL);
Alexander Larsson's avatar
Alexander Larsson committed
684 685 686 687
        g_assert (location != NULL);
        g_assert (type == NAUTILUS_LOCATION_CHANGE_BACK
                  || type == NAUTILUS_LOCATION_CHANGE_FORWARD
                  || distance == 0);
688

689
	end_location_change (slot);
690

691 692 693 694 695
	nautilus_window_slot_set_allow_stop (slot, TRUE);
	nautilus_window_slot_set_status (slot, " ");

	g_assert (slot->pending_location == NULL);
	g_assert (slot->pending_selection == NULL);
Alexander Larsson's avatar
Alexander Larsson committed
696
	
697 698 699 700 701
	slot->pending_location = g_object_ref (location);
        slot->location_change_type = type;
        slot->location_change_distance = distance;
	slot->tried_mount = FALSE;
	slot->pending_selection = eel_g_object_list_copy (new_selection);
702

703
	slot->pending_scroll_to = g_strdup (scroll_pos);
704 705 706 707

	slot->open_callback = callback;
	slot->open_callback_user_data = user_data;

Alexander Larsson's avatar
Alexander Larsson committed
708
        directory = nautilus_directory_get (location);
709

Alexander Larsson's avatar
Alexander Larsson committed
710 711 712 713 714 715 716 717 718 719 720
	/* The code to force a reload is here because if we do it
	 * after determining an initial view (in the components), then
	 * we end up fetching things twice.
	 */
	if (type == NAUTILUS_LOCATION_CHANGE_RELOAD) {
		force_reload = TRUE;
	} else if (!nautilus_monitor_active ()) {
		force_reload = TRUE;
	} else {
		force_reload = !nautilus_directory_is_local (directory);
	}
721

Alexander Larsson's avatar
Alexander Larsson committed
722 723 724 725 726 727
	if (force_reload) {
		nautilus_directory_force_reload (directory);
		file = nautilus_directory_get_corresponding_file (directory);
		nautilus_file_invalidate_all_attributes (file);
		nautilus_file_unref (file);
	}
728

Alexander Larsson's avatar
Alexander Larsson committed
729
        nautilus_directory_unref (directory);
730

Alexander Larsson's avatar
Alexander Larsson committed
731
        /* Set current_bookmark scroll pos */
732 733 734 735
        if (slot->current_location_bookmark != NULL &&
            slot->content_view != NULL) {
                current_pos = nautilus_view_get_first_visible_file (slot->content_view);
                nautilus_bookmark_set_scroll_pos (slot->current_location_bookmark, current_pos);
Alexander Larsson's avatar
Alexander Larsson committed
736
                g_free (current_pos);
737 738
        }

Alexander Larsson's avatar
Alexander Larsson committed
739
	/* Get the info needed for view selection */
740
	
741 742
        slot->determine_view_file = nautilus_file_get (location);
	g_assert (slot->determine_view_file != NULL);
743 744 745

	/* if the currently viewed file is marked gone while loading the new location,
	 * this ensures that the window isn't destroyed */
746
        cancel_viewed_file_changed_callback (slot);
747

748
	nautilus_file_call_when_ready (slot->determine_view_file,
Alexander Larsson's avatar
Alexander Larsson committed
749
				       NAUTILUS_FILE_ATTRIBUTE_INFO |
750
				       NAUTILUS_FILE_ATTRIBUTE_MOUNT,
Alexander Larsson's avatar
Alexander Larsson committed
751
                                       got_file_info_for_view_selection_callback,
752
				       slot);
Alexander Larsson's avatar
Alexander Larsson committed
753 754
}

Alexander Larsson's avatar
Alexander Larsson committed
755 756
typedef struct {
	GCancellable *cancellable;
757
	NautilusWindowSlot *slot;
Alexander Larsson's avatar
Alexander Larsson committed
758 759 760 761 762 763 764 765
} MountNotMountedData;

static void 
mount_not_mounted_callback (GObject *source_object,
			    GAsyncResult *res,
			    gpointer user_data)
{
	MountNotMountedData *data;
766
	NautilusWindowSlot *slot;
Alexander Larsson's avatar
Alexander Larsson committed
767 768 769 770
	GError *error;
	GCancellable *cancellable;

	data = user_data;
771
	slot = data->slot;
Alexander Larsson's avatar
Alexander Larsson committed
772 773 774 775 776 777 778 779 780
	cancellable = data->cancellable;
	g_free (data);

	if (g_cancellable_is_cancelled (cancellable)) {
		/* Cancelled, don't call back */
		g_object_unref (cancellable);
		return;
	}

781
	slot->mount_cancellable = NULL;
Alexander Larsson's avatar
Alexander Larsson committed
782

783
	slot->determine_view_file = nautilus_file_get (slot->pending_location);
Alexander Larsson's avatar
Alexander Larsson committed
784 785
	
	error = NULL;
786
	if (!g_file_mount_enclosing_volume_finish (G_FILE (source_object), res, &error)) {
787 788 789
		slot->mount_error = error;
		got_file_info_for_view_selection_callback (slot->determine_view_file, slot);
		slot->mount_error = NULL;
Alexander Larsson's avatar
Alexander Larsson committed
790 791
		g_error_free (error);
	} else {
792 793
		nautilus_file_invalidate_all_attributes (slot->determine_view_file);
		nautilus_file_call_when_ready (slot->determine_view_file,
794
					       NAUTILUS_FILE_ATTRIBUTE_INFO,
Alexander Larsson's avatar
Alexander Larsson committed
795
					       got_file_info_for_view_selection_callback,
796
					       slot);
Alexander Larsson's avatar
Alexander Larsson committed
797 798 799 800 801
	}

	g_object_unref (cancellable);
}

Alexander Larsson's avatar
Alexander Larsson committed
802 803 804 805
static void
got_file_info_for_view_selection_callback (NautilusFile *file,
					   gpointer callback_data)
{
Alexander Larsson's avatar
Alexander Larsson committed
806
        GError *error;
Alexander Larsson's avatar
Alexander Larsson committed
807 808 809
	char *view_id;
	char *mimetype;
	NautilusWindow *window;
810
	NautilusWindowSlot *slot;
811
	NautilusFile *viewed_file;
Alexander Larsson's avatar
Alexander Larsson committed
812 813 814
	GFile *location;
	GMountOperation *mount_op;
	MountNotMountedData *data;
815

816 817 818 819
	slot = callback_data;
	g_assert (NAUTILUS_IS_WINDOW_SLOT (slot));
	g_assert (slot->determine_view_file == file);

820
	window = slot->pane->window;
821 822 823 824 825 826
	g_assert (NAUTILUS_IS_WINDOW (window));

	slot->determine_view_file = NULL;

	if (slot->mount_error) {
		error = slot->mount_error;
Alexander Larsson's avatar
Alexander Larsson committed
827 828 829
	} else {
		error = nautilus_file_get_file_info_error (file);
	}
830

Alexander Larsson's avatar
Alexander Larsson committed
831
	if (error && error->domain == G_IO_ERROR && error->code == G_IO_ERROR_NOT_MOUNTED &&
832 833 834
	    !slot->tried_mount) {
		slot->tried_mount = TRUE;

835
		mount_op = gtk_mount_operation_new (GTK_WINDOW (window));
836
		g_mount_operation_set_password_save (mount_op, G_PASSWORD_SAVE_FOR_SESSION);
Alexander Larsson's avatar
Alexander Larsson committed
837 838 839
		location = nautilus_file_get_location (file);
		data = g_new0 (MountNotMountedData, 1);
		data->cancellable = g_cancellable_new ();
840 841 842
		data->slot = slot;
		slot->mount_cancellable = data->cancellable;
		g_file_mount_enclosing_volume (location, 0, mount_op, slot->mount_cancellable,
843
					       mount_not_mounted_callback, data);
Alexander Larsson's avatar
Alexander Larsson committed
844 845
		g_object_unref (location);
		g_object_unref (mount_op);
846 847 848

		nautilus_file_unref (file);

Alexander Larsson's avatar
Alexander Larsson committed
849 850 851
		return;
	}
	
852
	location = slot->pending_location;
853
	
Alexander Larsson's avatar
Alexander Larsson committed
854 855
	view_id = NULL;
	
Alexander Larsson's avatar
Alexander Larsson committed
856 857
        if (error == NULL ||
	    (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_NOT_SUPPORTED)) {
Alexander Larsson's avatar
Alexander Larsson committed
858
		/* We got the information we need, now pick what view to use: */
859

Alexander Larsson's avatar
Alexander Larsson committed
860
		mimetype = nautilus_file_get_mime_type (file);