gth-browser.c 112 KB
Newer Older
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2005-2009 Free Software Foundation, Inc.
 *
 *  This program 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 program 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 program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <gtk/gtk.h>
#include "dlg-personalize-filters.h"
#include "gconf-utils.h"
#include "glib-utils.h"
#include "gtk-utils.h"
#include "gth-browser.h"
#include "gth-browser-actions-callbacks.h"
#include "gth-browser-actions-entries.h"
#include "gth-browser-ui.h"
#include "gth-duplicable.h"
#include "gth-enum-types.h"
#include "gth-file-list.h"
#include "gth-file-view.h"
#include "gth-file-selection.h"
#include "gth-filter.h"
#include "gth-filterbar.h"
#include "gth-folder-tree.h"
#include "gth-icon-cache.h"
#include "gth-image-preloader.h"
#include "gth-location-chooser.h"
#include "gth-main.h"
#include "gth-marshal.h"
#include "gth-metadata-provider.h"
#include "gth-preferences.h"
48
#include "gth-progress-dialog.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
49 50
#include "gth-sidebar.h"
#include "gth-statusbar.h"
51
#include "gth-toggle-menu-tool-button.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "gth-viewer-page.h"
#include "gth-window.h"
#include "gth-window-actions-callbacks.h"
#include "gth-window-actions-entries.h"
#include "gthumb-error.h"

#define GTH_BROWSER_CALLBACK(f) ((GthBrowserCallback) (f))
#define GO_BACK_HISTORY_POPUP "/GoBackHistoryPopup"
#define GO_FORWARD_HISTORY_POPUP "/GoForwardHistoryPopup"
#define GO_PARENT_POPUP "/GoParentPopup"
#define MAX_HISTORY_LENGTH 15
#define GCONF_NOTIFICATIONS 9
#define DEF_SIDEBAR_WIDTH 250
#define DEF_PROPERTIES_HEIGHT 128
#define DEF_THUMBNAIL_SIZE 128
67
#define LOAD_FILE_DELAY 150
68 69
#define HIDE_MOUSE_DELAY 1000
#define MOTION_THRESHOLD 0
70
#define PROGRESS_DIALOG_DELAY 1000
Paolo Bacchilega's avatar
Paolo Bacchilega committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

typedef void (*GthBrowserCallback) (GthBrowser *, gboolean cancelled, gpointer user_data);

typedef enum {
	GTH_ACTION_GO_TO,
	GTH_ACTION_GO_INTO,
	GTH_ACTION_GO_BACK,
	GTH_ACTION_GO_FORWARD,
	GTH_ACTION_GO_UP,
	GTH_ACTION_LIST_CHILDREN,
	GTH_ACTION_VIEW
} GthAction;

enum {
	LOCATION_READY,
	LAST_SIGNAL
};

struct _GthBrowserPrivateData {
	/* UI staff */

	GtkUIManager      *ui;
	GtkActionGroup    *actions;
	GtkWidget         *statusbar;
	GtkWidget         *browser_toolbar;
	GtkWidget         *browser_container;
	GtkWidget         *browser_sidebar;
	GtkWidget         *location_chooser;
	GtkWidget         *folder_tree;
	GtkWidget         *history_list_popup_menu;
	GtkWidget         *folder_popup;
	GtkWidget         *file_list_popup;
103
	GtkWidget         *file_popup;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
104 105 106 107 108 109 110 111 112 113 114 115 116
	GtkWidget         *filterbar;
	GtkWidget         *file_list;
	GtkWidget         *list_extra_widget_container;
	GtkWidget         *list_extra_widget;
	GtkWidget         *file_properties;

	GtkWidget         *viewer_pane;
	GtkWidget         *viewer_sidebar;
	GtkWidget         *viewer_container;
	GtkWidget         *viewer_toolbar;
	GthViewerPage     *viewer_page;
	GthImagePreloader *image_preloader;

117 118
	GtkWidget         *progress_dialog;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132
	GHashTable        *named_dialogs;
	GList             *toolbar_menu_buttons[GTH_BROWSER_N_PAGES];

	guint              browser_ui_merge_id;
	guint              viewer_ui_merge_id;

	/* Browser data */

	guint              help_message_cid;
	gulong             bookmarks_changed_id;
	gulong             folder_changed_id;
	gulong             file_renamed_id;
	gulong             metadata_changed_id;
	gulong             entry_points_changed_id;
133
	GthFileData       *location;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
134 135 136 137 138 139 140 141 142 143 144 145
	GthFileData       *current_file;
	GthFileSource     *location_source;
	gboolean           activity_ref;
	GthIconCache      *menu_icon_cache;
	guint              cnxn_id[GCONF_NOTIFICATIONS];
	GthFileDataSort   *sort_type;
	gboolean           sort_inverse;
	gboolean           show_hidden_files;
	gboolean           fast_file_type;
	gboolean           closing;
	GthTask           *task;
	gulong             task_completed;
146
	gulong             task_progress;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
147
	GList             *load_data_queue;
148
	guint              load_file_timeout;
149 150 151

	/* fulscreen */

Paolo Bacchilega's avatar
Paolo Bacchilega committed
152
	gboolean           fullscreen;
153 154 155 156 157
	GtkWidget         *fullscreen_toolbar;
	guint              hide_mouse_timeout;
	guint              motion_signal;
	gdouble            last_mouse_x;
	gdouble            last_mouse_y;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
158 159 160 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

	/* history */

	GList             *history;
	GList             *history_current;
};


static GthWindowClass *parent_class = NULL;
static guint gth_browser_signals[LAST_SIGNAL] = { 0 };
static GList *browser_list = NULL;


/* -- monitor_event_data -- */


typedef struct {
	int              ref;
	GthFileSource   *file_source;
	GFile           *parent;
	GthMonitorEvent  event;
	GthBrowser      *browser;
	gboolean         update_file_list;
	gboolean         update_folder_tree;
} MonitorEventData;


static MonitorEventData *
monitor_event_data_new (void)
{
	MonitorEventData *monitor_data;

	monitor_data = g_new0 (MonitorEventData, 1);
	monitor_data->ref = 1;

	return monitor_data;
}


G_GNUC_UNUSED
static MonitorEventData *
monitor_event_data_ref (MonitorEventData *monitor_data)
{
	monitor_data->ref++;
	return monitor_data;
}


static void
monitor_event_data_unref (MonitorEventData *monitor_data)
{
	monitor_data->ref--;

	if (monitor_data->ref > 0)
		return;

	g_object_unref (monitor_data->file_source);
	g_object_unref (monitor_data->parent);
	g_free (monitor_data);
}


/* -- gth_browser -- */


static void
_gth_browser_set_action_sensitive (GthBrowser  *browser,
				   const char  *action_name,
				   gboolean     sensitive)
{
	GtkAction *action;

	action = gtk_action_group_get_action (browser->priv->actions, action_name);
	g_object_set (action, "sensitive", sensitive, NULL);
}


static void
_gth_browser_set_action_active (GthBrowser  *browser,
				const char  *action_name,
				gboolean     active)
{
	GtkAction *action;

	action = gtk_action_group_get_action (browser->priv->actions, action_name);
	g_object_set (action, "active", active, NULL);
}


static void
activate_go_back_menu_item (GtkMenuItem *menuitem,
			    gpointer     data)
{
	GthBrowser *browser = data;

	gth_browser_go_back (browser, GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menuitem), "steps")));
}


static void
activate_go_forward_menu_item (GtkMenuItem *menuitem,
			       gpointer     data)
{
	GthBrowser *browser = data;

	gth_browser_go_forward (browser, GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menuitem), "steps")));
}


static void
activate_go_up_menu_item (GtkMenuItem *menuitem,
			  gpointer     data)
{
	GthBrowser *browser = data;

	gth_browser_go_up (browser, GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menuitem), "steps")));
}


static void
activate_go_to_menu_item (GtkMenuItem *menuitem,
			  gpointer     data)
{
	GthBrowser *browser = data;
	GFile      *location;

	location = g_file_new_for_uri (g_object_get_data (G_OBJECT (menuitem), "uri"));
	gth_browser_go_to (browser, location);

	g_object_unref (location);
}


static void
_gth_browser_add_file_menu_item_full (GthBrowser *browser,
				      GtkWidget  *menu,
				      GFile      *file,
				      GIcon      *icon,
				      const char *display_name,
				      GthAction   action,
				      int         steps,
				      int         position)
{
	GdkPixbuf *pixbuf;
	GtkWidget *menu_item;

	pixbuf = gth_icon_cache_get_pixbuf (browser->priv->menu_icon_cache, icon);

	menu_item = gtk_image_menu_item_new_with_label (display_name);
	if (pixbuf != NULL)
		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), gtk_image_new_from_pixbuf (pixbuf));
	else
		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU));
	gtk_widget_show (menu_item);
	if (position == -1)
		gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
	else
		gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menu_item, position);

	if (action == GTH_ACTION_GO_TO) {
		g_object_set_data_full (G_OBJECT (menu_item),
					"uri",
					g_file_get_uri (file),
					(GDestroyNotify) g_free);
		g_signal_connect (menu_item,
				  "activate",
				  G_CALLBACK (activate_go_to_menu_item),
			  	  browser);
	}
	else {
		g_object_set_data (G_OBJECT (menu_item),
				   "steps",
				   GINT_TO_POINTER (steps));
		if (action == GTH_ACTION_GO_BACK)
			g_signal_connect (menu_item,
					  "activate",
					  G_CALLBACK (activate_go_back_menu_item),
			  	  	  browser);
		else if (action == GTH_ACTION_GO_FORWARD)
			g_signal_connect (menu_item,
					  "activate",
					  G_CALLBACK (activate_go_forward_menu_item),
			  	  	  browser);
		else if (action == GTH_ACTION_GO_UP)
			g_signal_connect (menu_item,
					  "activate",
					  G_CALLBACK (activate_go_up_menu_item),
			  	  	  browser);
	}

	if (pixbuf != NULL)
		g_object_unref (pixbuf);
}


static void
_gth_browser_add_file_menu_item (GthBrowser *browser,
				 GtkWidget  *menu,
			 	 GFile      *file,
			 	 GthAction   action,
				 int         steps)
{
	GthFileSource *file_source;
	GFileInfo     *info;

	file_source = gth_main_get_file_source (file);
364
	info = gth_file_source_get_file_info (file_source, file, GFILE_DISPLAY_ATTRIBUTES);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	if (info != NULL) {
		_gth_browser_add_file_menu_item_full (browser,
						      menu,
						      file,
						      g_file_info_get_icon (info),
						      g_file_info_get_display_name (info),
						      action,
						      steps,
						      -1);
		g_object_unref (info);
	}
	g_object_unref (file_source);
}


static void
_gth_browser_update_parent_list (GthBrowser *browser)
{
	GtkWidget *menu;
	int        i;
	GFile     *parent;

	menu = gtk_ui_manager_get_widget (browser->priv->ui, GO_PARENT_POPUP);
	_gtk_container_remove_children (GTK_CONTAINER (menu), NULL, NULL);

	if (browser->priv->location == NULL)
		return;

	/* Update the parent list menu. */

	i = 0;
396
	parent = g_file_get_parent (browser->priv->location->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	while (parent != NULL) {
		GFile *parent_parent;

		_gth_browser_add_file_menu_item (browser,
						 menu,
						 parent,
						 GTH_ACTION_GO_UP,
						 ++i);

		parent_parent = g_file_get_parent (parent);
		g_object_unref (parent);
		parent = parent_parent;
	}
}


void
gth_browser_update_title (GthBrowser *browser)
{
416 417
	GString    *title;
	const char *name = NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
418

419
	title = g_string_new (NULL);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
420

421 422 423 424
	if (browser->priv->current_file != NULL)
		name = g_file_info_get_display_name (browser->priv->current_file->info);
	else if (browser->priv->location != NULL)
		name = g_file_info_get_display_name (browser->priv->location->info);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
425

426 427
	if (name != NULL) {
		g_string_append (title, name);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
428 429 430 431 432 433 434 435
		if (gth_browser_get_file_modified (browser)) {
			g_string_append (title, " ");
			g_string_append (title, _("[modified]"));
		}
	}
	else
		g_string_append (title, _("gthumb"));

436 437 438 439 440 441 442 443 444 445
	if (browser->priv->current_file != NULL) {
		GthFileStore *file_store;
		int           pos;

		file_store = gth_browser_get_file_store (browser);
		pos = gth_file_store_find_visible (file_store, browser->priv->current_file->file);
		if (pos >= 0)
			g_string_append_printf (title, " (%d/%d)", pos + 1, gth_file_store_n_visibles (file_store));
	}

Paolo Bacchilega's avatar
Paolo Bacchilega committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	gtk_window_set_title (GTK_WINDOW (browser), title->str);

	g_string_free (title, TRUE);
}


void
gth_browser_update_sensitivity (GthBrowser *browser)
{
	GFile    *parent;
	gboolean  parent_available;
	gboolean  viewer_can_save;
	gboolean  modified;
	int       current_file_pos;
	int       n_files;
	int       n_selected;

	if (browser->priv->location != NULL)
464
		parent = g_file_get_parent (browser->priv->location->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	else
		parent = NULL;
	parent_available = (parent != NULL);
	_g_object_unref (parent);

	viewer_can_save = (browser->priv->location != NULL) && (browser->priv->viewer_page != NULL) && gth_viewer_page_can_save (GTH_VIEWER_PAGE (browser->priv->viewer_page));
	modified = gth_browser_get_file_modified (browser);

	if (browser->priv->current_file != NULL)
		current_file_pos = gth_file_store_find_visible (gth_browser_get_file_store (browser), browser->priv->current_file->file);
	else
		current_file_pos = -1;
	n_files = gth_file_store_n_visibles (gth_browser_get_file_store (browser));
	n_selected = gth_file_selection_get_n_selected (GTH_FILE_SELECTION (gth_browser_get_file_list_view (browser)));

480
	_gth_browser_set_action_sensitive (browser, "File_Open", n_selected == 1);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
481 482 483 484 485
	_gth_browser_set_action_sensitive (browser, "File_Save", viewer_can_save && modified);
	_gth_browser_set_action_sensitive (browser, "File_SaveAs", viewer_can_save);
	_gth_browser_set_action_sensitive (browser, "File_Revert", viewer_can_save && modified);
	_gth_browser_set_action_sensitive (browser, "Go_Up", parent_available);
	_gth_browser_set_action_sensitive (browser, "Toolbar_Go_Up", parent_available);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
486
	_gth_browser_set_action_sensitive (browser, "View_Stop", browser->priv->fullscreen || (browser->priv->activity_ref > 0));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
487 488 489 490 491 492 493 494 495 496 497 498 499
	_gth_browser_set_action_sensitive (browser, "View_Prev", current_file_pos > 0);
	_gth_browser_set_action_sensitive (browser, "View_Next", (current_file_pos != -1) && (current_file_pos < n_files - 1));
	_gth_browser_set_action_sensitive (browser, "Edit_Metadata", n_selected > 0);

	gth_sidebar_update_sensitivity (GTH_SIDEBAR (browser->priv->viewer_sidebar));
	if (browser->priv->viewer_page != NULL)
		gth_viewer_page_update_sensitivity (browser->priv->viewer_page);

	gth_hook_invoke ("gth-browser-update-sensitivity", browser);
}


static void
500 501
_gth_browser_set_location (GthBrowser  *browser,
			   GthFileData *location)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
502 503 504 505 506 507
{
	if (location == NULL)
		return;

	if (browser->priv->location != NULL)
		g_object_unref (browser->priv->location);
508
	browser->priv->location = gth_file_data_dup (location);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
509 510 511 512 513 514

	gth_browser_update_title (browser);
	_gth_browser_update_parent_list (browser);
	gth_browser_update_sensitivity (browser);

	g_signal_handlers_block_by_data (browser->priv->location_chooser, browser);
515
	gth_location_chooser_set_current (GTH_LOCATION_CHOOSER (browser->priv->location_chooser), browser->priv->location->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
516 517 518 519
	g_signal_handlers_unblock_by_data (browser->priv->location_chooser, browser);
}


520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
static void
_gth_browser_set_location_from_file (GthBrowser *browser,
				     GFile      *file)
{
	GthFileSource *file_source;
	GthFileData   *file_data;
	GFileInfo     *info;

	file_source = gth_main_get_file_source (file);
	info = gth_file_source_get_file_info (file_source, file, GFILE_DISPLAY_ATTRIBUTES);
	file_data = gth_file_data_new (file, info);
	_gth_browser_set_location (browser, file_data);

	g_object_unref (file_data);
	_g_object_unref (info);
	g_object_unref (file_source);
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 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 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
static void
_gth_browser_update_go_sensitivity (GthBrowser *browser)
{
	gboolean  sensitive;

	sensitive = (browser->priv->history_current != NULL) && (browser->priv->history_current->next != NULL);
	_gth_browser_set_action_sensitive (browser, "Go_Back", sensitive);
	_gth_browser_set_action_sensitive (browser, "Toolbar_Go_Back", sensitive);

	sensitive = (browser->priv->history_current != NULL) && (browser->priv->history_current->prev != NULL);
	_gth_browser_set_action_sensitive (browser, "Go_Forward", sensitive);
	_gth_browser_set_action_sensitive (browser, "Toolbar_Go_Forward", sensitive);
}


static void
activate_clear_history_menu_item (GtkMenuItem *menuitem,
				  gpointer     data)
{
	gth_browser_clear_history ((GthBrowser *)data);
}


static void
_gth_browser_add_clear_history_menu_item (GthBrowser *browser,
					  GtkWidget  *menu)
{
	GtkWidget *menu_item;

	menu_item = gtk_separator_menu_item_new ();
	gtk_widget_show (menu_item);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);

	menu_item = gtk_image_menu_item_new_with_mnemonic (_("_Delete History"));
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU));
	gtk_widget_show (menu_item);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);

	g_signal_connect (menu_item,
			  "activate",
			  G_CALLBACK (activate_clear_history_menu_item),
		  	  browser);
}


static void
_gth_browser_update_history_list (GthBrowser *browser)
{
	GtkWidget *menu;
	GList     *scan;
	GtkWidget *separator;

	_gth_browser_update_go_sensitivity (browser);

	/* Update the back history menu. */

	menu = gtk_ui_manager_get_widget (browser->priv->ui, GO_BACK_HISTORY_POPUP);
	_gtk_container_remove_children (GTK_CONTAINER (menu), NULL, NULL);

	if ((browser->priv->history != NULL)
	    && (browser->priv->history_current->next != NULL))
	{
		int i;

		for (i = 0, scan = browser->priv->history_current->next;
		     scan && (i < MAX_HISTORY_LENGTH);
		     scan = scan->next)
		{
			_gth_browser_add_file_menu_item (browser,
							 menu,
							 scan->data,
							 GTH_ACTION_GO_BACK,
							 ++i);
		}
		if (i > 0)
			_gth_browser_add_clear_history_menu_item (browser, menu);
	}

	/* Update the forward history menu. */

	menu = gtk_ui_manager_get_widget (browser->priv->ui, GO_FORWARD_HISTORY_POPUP);
	_gtk_container_remove_children (GTK_CONTAINER (menu), NULL, NULL);

	if ((browser->priv->history != NULL)
	    && (browser->priv->history_current->prev != NULL))
	{
		int i;

		for (i = 0, scan = browser->priv->history_current->prev;
		     scan && (i < MAX_HISTORY_LENGTH);
		     scan = scan->prev)
		{
			_gth_browser_add_file_menu_item (browser,
							 menu,
							 scan->data,
							 GTH_ACTION_GO_FORWARD,
							 ++i);
		}
		if (i > 0)
			_gth_browser_add_clear_history_menu_item (browser, menu);
	}

	/* Update the history list in the go menu */

	separator = gtk_ui_manager_get_widget (browser->priv->ui, "/MenuBar/Go/HistoryList");
	menu = gtk_widget_get_parent (separator);

	_gtk_container_remove_children (GTK_CONTAINER (menu), separator, NULL);

	if (browser->priv->history != NULL) {
		int i;

		for (i = 0, scan = browser->priv->history;
		     scan && (i < MAX_HISTORY_LENGTH);
		     scan = scan->next)
		{
			_gth_browser_add_file_menu_item (browser,
							 menu,
							 scan->data,
							 GTH_ACTION_GO_TO,
							 ++i);
		}
	}

	separator = gtk_ui_manager_get_widget (browser->priv->ui, "/MenuBar/Go/BeforeHistoryList");
	gtk_widget_show (separator);
}


static void
_gth_browser_add_to_history (GthBrowser *browser,
			     GFile      *file)
{
	if (file == NULL)
		return;

	if ((browser->priv->history_current == NULL) || ! g_file_equal (file, browser->priv->history_current->data)) {
		browser->priv->history = g_list_prepend (browser->priv->history, g_object_ref (file));
		browser->priv->history_current = browser->priv->history;
	}
}


static void
_gth_browser_update_bookmark_list (GthBrowser *browser)
{
	GtkWidget      *menu;
	GtkWidget      *bookmark_list;
	GtkWidget      *bookmark_list_separator;
	GBookmarkFile  *bookmarks;
	char          **uris;
	gsize           length;
	int             i;

	bookmark_list = gtk_ui_manager_get_widget (browser->priv->ui, "/MenuBar/Bookmarks/BookmarkList");
	menu = gtk_widget_get_parent (bookmark_list);

	_gtk_container_remove_children (GTK_CONTAINER (menu), bookmark_list, NULL);

	bookmarks = gth_main_get_default_bookmarks ();
	uris = g_bookmark_file_get_uris (bookmarks, &length);

	bookmark_list_separator = gtk_ui_manager_get_widget (browser->priv->ui, "/MenuBar/Bookmarks/BookmarkListSeparator");
	if (length > 0)
		gtk_widget_show (bookmark_list_separator);
	else
		gtk_widget_hide (bookmark_list_separator);

	for (i = 0; uris[i] != NULL; i++) {
		GFile *file;

		file = g_file_new_for_uri (uris[i]);
		_gth_browser_add_file_menu_item (browser,
						 menu,
						 file,
						 GTH_ACTION_GO_TO,
						 i);

		g_object_unref (file);
	}

	g_strfreev (uris);
}


static void
_gth_browser_monitor_entry_points (GthBrowser *browser)
{
	GList *scan;

	for (scan = gth_main_get_all_file_sources (); scan; scan = scan->next) {
		GthFileSource *file_source = scan->data;
		gth_file_source_monitor_entry_points (file_source);
	}
}


static void
_gth_browser_update_entry_point_list (GthBrowser *browser)
{
	GtkWidget *separator1;
	GtkWidget *separator2;
	GtkWidget *menu;
	GList     *entry_points;
	GList     *scan;
	int        position;
	GFile     *root;

	separator1 = gtk_ui_manager_get_widget (browser->priv->ui, "/MenuBar/Go/BeforeEntryPointList");
	separator2 = gtk_ui_manager_get_widget (browser->priv->ui, "/MenuBar/Go/EntryPointList");
	menu = gtk_widget_get_parent (separator1);
	_gtk_container_remove_children (GTK_CONTAINER (menu), separator1, separator2);

	separator1 = separator2;
	separator2 = gtk_ui_manager_get_widget (browser->priv->ui, "/MenuBar/Go/EntryPointListSeparator");
	_gtk_container_remove_children (GTK_CONTAINER (menu), separator1, separator2);

	position = 5;
	entry_points = gth_main_get_all_entry_points ();
	for (scan = entry_points; scan; scan = scan->next) {
759
		GthFileData *file_data = scan->data;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

		g_file_info_set_attribute_boolean (file_data->info, "gthumb::entry-point", TRUE);
		g_file_info_set_sort_order (file_data->info, position);
		_gth_browser_add_file_menu_item_full (browser,
						      menu,
						      file_data->file,
						      g_file_info_get_icon (file_data->info),
						      g_file_info_get_display_name (file_data->info),
						      GTH_ACTION_GO_TO,
						      0,
						      position++);
	}
	root = g_file_new_for_uri ("gthumb-vfs:///");
	gth_folder_tree_set_children (GTH_FOLDER_TREE (browser->priv->folder_tree), root, entry_points);

	g_object_unref (root);
	_g_object_list_unref (entry_points);
}


static GthTest *
_gth_browser_get_file_filter (GthBrowser *browser)
{
	GthTest *filterbar_test;
	GthTest *test;

	filterbar_test = gth_filterbar_get_test (GTH_FILTERBAR (browser->priv->filterbar));
	test = gth_main_add_general_filter (filterbar_test);

	_g_object_unref (filterbar_test);

	return test;
}


static void
_gth_browser_update_statusbar_list_info (GthBrowser *browser)
{
	GList *file_list;
	int    n_total;
	gsize  size_total;
	GList *scan;
	int    n_selected;
	gsize  size_selected;
	GList *selected;
	char  *size_total_formatted;
	char  *size_selected_formatted;
	char  *text_total;
	char  *text_selected;
	char  *text;

	/* total */

	file_list = gth_file_store_get_visibles (gth_browser_get_file_store (browser));
	n_total = 0;
	size_total = 0;
	for (scan = file_list; scan; scan = scan->next) {
		GthFileData *file_data = scan->data;

		n_total++;
		size_total += g_file_info_get_size (file_data->info);
	}
	_g_object_list_unref (file_list);

	/* selected */

	selected = gth_file_selection_get_selected (GTH_FILE_SELECTION (gth_browser_get_file_list_view (browser)));
	file_list = gth_file_list_get_files (GTH_FILE_LIST (gth_browser_get_file_list (browser)), selected);

	n_selected = 0;
	size_selected = 0;
	for (scan = file_list; scan; scan = scan->next) {
		GthFileData *file_data = scan->data;

		n_selected++;
		size_selected += g_file_info_get_size (file_data->info);
	}
	_g_object_list_unref (file_list);
	_gtk_tree_path_list_free (selected);

	/**/

	size_total_formatted = g_format_size_for_display (size_total);
	size_selected_formatted = g_format_size_for_display (size_selected);
	text_total = g_strdup_printf (g_dngettext (NULL, "%d file (%s)", "%d files (%s)", n_total), n_total, size_total_formatted);
	text_selected = g_strdup_printf (g_dngettext (NULL, "%d file (%s)", "%d files (%s)", n_selected), n_selected, size_selected_formatted);
	text = g_strconcat (text_total,
			    ((n_selected == 0) ? NULL : ", "),
			    text_selected,
			    NULL);
	gth_statusbar_set_list_info (GTH_STATUSBAR (browser->priv->statusbar), text);

	g_free (text);
	g_free (text_selected);
	g_free (text_total);
	g_free (size_selected_formatted);
	g_free (size_total_formatted);
}


typedef struct {
	GthBrowser    *browser;
862
	GFile         *requested_folder;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
	GthAction      action;
	GList         *list;
	GList         *current;
	GFile         *entry_point;
	GthFileSource *file_source;
	GCancellable  *cancellable;
} LoadData;


static LoadData *
load_data_new (GthBrowser *browser,
	       GFile      *location,
	       GthAction   action,
	       GFile      *entry_point)
{
	LoadData *load_data;
	GFile    *file;

	load_data = g_new0 (LoadData, 1);
	load_data->browser = browser;
883
	load_data->requested_folder = g_object_ref (location);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
884 885 886 887 888 889 890
	load_data->action = action;
	load_data->cancellable = g_cancellable_new ();

	if (entry_point == NULL)
		return load_data;

	load_data->entry_point = g_object_ref (entry_point);
891
	load_data->file_source = gth_main_get_file_source (load_data->requested_folder);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
892

893
	file = g_object_ref (load_data->requested_folder);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
	load_data->list = g_list_prepend (NULL, g_object_ref (file));
	while (! g_file_equal (load_data->entry_point, file)) {
		GFile *parent;

		parent = g_file_get_parent (file);
		g_object_unref (file);
		file = parent;

		load_data->list = g_list_prepend (load_data->list, g_object_ref (file));
	}
	g_object_unref (file);
	load_data->current = NULL;

	browser->priv->load_data_queue = g_list_prepend (browser->priv->load_data_queue, load_data);

	return load_data;
}


static void
load_data_free (LoadData *data)
{
	data->browser->priv->load_data_queue = g_list_remove (data->browser->priv->load_data_queue, data);

918
	g_object_unref (data->requested_folder);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	_g_object_unref (data->file_source);
	_g_object_list_unref (data->list);
	_g_object_unref (data->entry_point);
	g_object_unref (data->cancellable);
	g_free (data);
}


static void
load_data_cancel (LoadData *data)
{
	if (data->file_source != NULL)
		gth_file_source_cancel (data->file_source);
	g_cancellable_cancel (data->cancellable);
}


static void
load_data_done (LoadData *data,
		GError   *error)
{
	GthBrowser *browser = data->browser;

	{
		char *uri;

945
		uri = g_file_get_uri (data->requested_folder);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
946 947 948 949 950 951 952 953 954 955
		debug (DEBUG_INFO, "LOAD READY: %s [%s]\n", uri, (error == NULL ? "Ok" : "Error"));
		performance (DEBUG_INFO, "load done for %s", uri);

		g_free (uri);
	}

	browser->priv->activity_ref--;
	g_signal_emit (G_OBJECT (browser),
		       gth_browser_signals[LOCATION_READY],
		       0,
956
		       data->requested_folder,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
957 958 959 960 961 962 963
		       (error != NULL));

	if (error == NULL) {
		_g_object_unref (browser->priv->location_source);
		browser->priv->location_source = g_object_ref (data->file_source);
	}

964
	gth_hook_invoke ("gth-browser-load-location-after", browser, data->requested_folder, error);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982

	if (error == NULL)
		return;

	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		g_error_free (error);
		return;
	}

	gth_browser_update_sensitivity (browser);
	_gtk_error_dialog_from_gerror_show (GTK_WINDOW (browser), _("Could not load the position"), &error);
}


static void _gth_browser_load_ready_cb (GthFileSource *file_source, GList *files, GError *error, gpointer user_data);


static void
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
requested_folder_attributes_ready_cb (GthFileSource *file_source,
				      GList         *files,
				      GError        *error,
				      gpointer       user_data)
{
	LoadData    *load_data = user_data;
	GthFileData *requested_folder_data;

	if (error != NULL) {
		load_data_done (load_data, error);
		load_data_free (load_data);
		return;
	}

	requested_folder_data = (GthFileData *) files->data;
	gth_file_data_set_info (load_data->browser->priv->location, requested_folder_data->info);

	gth_file_source_list (load_data->file_source,
			      load_data->requested_folder,
			      eel_gconf_get_boolean (PREF_FAST_FILE_TYPE, TRUE) ? GFILE_STANDARD_ATTRIBUTES_WITH_FAST_CONTENT_TYPE : GFILE_STANDARD_ATTRIBUTES_WITH_CONTENT_TYPE,
			      _gth_browser_load_ready_cb,
			      load_data);
}


static void
load_data_load_next_folder (LoadData *load_data)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1010 1011 1012 1013
{
	GthFolderTree *folder_tree;
	GFile         *folder_to_load;

1014
	folder_tree = GTH_FOLDER_TREE (load_data->browser->priv->folder_tree);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1015 1016 1017
	do {
		GtkTreePath *path;

1018 1019
		if (load_data->current == NULL)
			load_data->current = load_data->list;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1020
		else
1021
			load_data->current = load_data->current->next;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1022

1023 1024
		folder_to_load = (GFile *) load_data->current->data;
		if (g_file_equal (folder_to_load, load_data->requested_folder))
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
			break;

		path = gth_folder_tree_get_path (folder_tree, folder_to_load);
		if (path == NULL)
			break;

		if (! gth_folder_tree_is_loaded (folder_tree, path)) {
			gtk_tree_path_free (path);
			break;
		}

1036
		if (! g_file_equal (folder_to_load, load_data->requested_folder))
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1037 1038 1039 1040 1041 1042
			gth_folder_tree_expand_row (folder_tree, path, FALSE);

		gtk_tree_path_free (path);
	}
	while (TRUE);

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
	if (g_file_equal (folder_to_load, load_data->requested_folder)) {
		GList *files;

		files = g_list_prepend (NULL, folder_to_load);
		gth_file_source_read_attributes (load_data->file_source,
						 files,
						 GFILE_BASIC_ATTRIBUTES ",access::*",
						 requested_folder_attributes_ready_cb,
						 load_data);

		g_list_free (files);
	}
	else
		gth_file_source_list (load_data->file_source,
				      folder_to_load,
				      eel_gconf_get_boolean (PREF_FAST_FILE_TYPE, TRUE) ? GFILE_STANDARD_ATTRIBUTES_WITH_FAST_CONTENT_TYPE : GFILE_STANDARD_ATTRIBUTES_WITH_CONTENT_TYPE,
				      _gth_browser_load_ready_cb,
				      load_data);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1061 1062 1063 1064
}


static void
1065
load_data_continue (LoadData *load_data,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1066 1067
		    GList    *loaded_files)
{
1068
	GthBrowser  *browser = load_data->browser;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1069 1070 1071 1072 1073
	GList       *files;
	GFile       *loaded_folder;
	GtkTreePath *path;
	GthTest     *filter;

1074 1075
	if ((load_data->action != GTH_ACTION_LIST_CHILDREN)
	    && ! g_file_equal (load_data->requested_folder, load_data->browser->priv->location->file))
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1076
	{
1077 1078
		load_data_done (load_data, g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED, ""));
		load_data_free (load_data);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
		return;
	}

	if (! browser->priv->show_hidden_files) {
		GList *scan;

		files = NULL;
		for (scan = loaded_files; scan; scan = scan->next) {
			GthFileData *file_data = scan->data;

			if (! g_file_info_get_is_hidden (file_data->info))
				files = g_list_prepend (files, file_data);
		}
		files = g_list_reverse (files);
	}
	else
		files = g_list_copy (loaded_files);

1097
	loaded_folder = (GFile *) load_data->current->data;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1098 1099
	gth_folder_tree_set_children (GTH_FOLDER_TREE (browser->priv->folder_tree), loaded_folder, files);
	path = gth_folder_tree_get_path (GTH_FOLDER_TREE (browser->priv->folder_tree), loaded_folder);
1100
	if ((path != NULL) && ! g_file_equal (loaded_folder, load_data->requested_folder))
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1101 1102
		gth_folder_tree_expand_row (GTH_FOLDER_TREE (browser->priv->folder_tree), path, FALSE);

1103
	if (! g_file_equal (loaded_folder, load_data->requested_folder)) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1104 1105 1106
		gtk_tree_path_free (path);
		g_list_free (files);

1107
		load_data_load_next_folder (load_data);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1108 1109 1110
		return;
	}

1111
	load_data_done (load_data, NULL);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1112

1113
	switch (load_data->action) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1114 1115 1116 1117 1118
	case GTH_ACTION_VIEW:
	case GTH_ACTION_GO_BACK:
	case GTH_ACTION_GO_FORWARD:
	case GTH_ACTION_GO_TO:
		if (path != NULL) {
1119 1120 1121
			GList *entry_points;
			GList *scan;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
1122 1123
			gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (browser->priv->folder_tree), path, NULL, FALSE, .0, .0);
			gth_folder_tree_select_path (GTH_FOLDER_TREE (browser->priv->folder_tree), path);
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

			/* expand the path if it's an entry point */

			entry_points = gth_main_get_all_entry_points ();
			for (scan = entry_points; scan; scan = scan->next) {
				GthFileData *file_data = scan->data;

				if (g_file_equal (file_data->file, load_data->requested_folder)) {
					gtk_tree_view_expand_row (GTK_TREE_VIEW (browser->priv->folder_tree), path, FALSE);
					break;
				}
			}

			_g_object_list_unref (entry_points);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1138 1139 1140 1141 1142 1143
		}
		break;
	default:
		break;
	}

1144
	switch (load_data->action) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1145 1146 1147 1148 1149 1150 1151
	case GTH_ACTION_VIEW:
	case GTH_ACTION_GO_BACK:
	case GTH_ACTION_GO_FORWARD:
	case GTH_ACTION_GO_INTO:
	case GTH_ACTION_GO_TO:
		filter = _gth_browser_get_file_filter (browser);
		gth_file_list_set_filter (GTH_FILE_LIST (browser->priv->file_list), filter);
1152
		gth_file_list_set_files (GTH_FILE_LIST (browser->priv->file_list), load_data->file_source, files);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
		g_object_unref (filter);
		break;
	default:
		break;
	}

	gth_browser_update_sensitivity (browser);
	_gth_browser_update_statusbar_list_info (browser);

	gth_file_source_monitor_directory (browser->priv->location_source,
1163
					   browser->priv->location->file,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1164 1165 1166 1167
					   TRUE);

	if (path != NULL)
		gtk_tree_path_free (path);
1168
	load_data_free (load_data);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	g_list_free (files);
}


static void
metadata_ready_cb (GList    *files,
		   GError   *error,
		   gpointer  user_data)
{
	LoadData *load_data = user_data;

	if (error != NULL) {
		load_data_done (load_data, error);
		load_data_free (load_data);
		return;
	}

	load_data_continue (load_data, files);
}


static void
load_data_ready (LoadData *data,
		 GList    *files,
		 GError   *error)
{
	if (error != NULL) {
		load_data_done (data, error);
		load_data_free (data);
	}
1199
	else if (g_file_equal ((GFile *) data->current->data, data->requested_folder))
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
		/* FIXME: make the metadata attribute list automatic, based
		 * on the data required to filter and view the file list. */
		_g_query_metadata_async (files,
					 "file::*,comment::*",
					 data->cancellable,
					 metadata_ready_cb,
				 	 data);
	else
		load_data_continue (data, files);
}


static void
_gth_browser_load_ready_cb (GthFileSource *file_source,
			    GList         *files,
			    GError        *error,
			    gpointer       user_data)
{
	load_data_ready ((LoadData *) user_data, files, error);
}


#if 0
static void
_gth_browser_print_history (GthBrowser *browser)
{
	GList *scan;

	g_print ("history:\n");
	for (scan = browser->priv->history; scan; scan = scan->next) {
		GFile *file = scan->data;
		char  *uri;

		uri = g_file_get_uri (file);
		g_print (" %s%s\n", (browser->priv->history_current == scan) ? "*" : " ", uri);

		g_free (uri);
	}
}
#endif


static void
_gth_browser_cancel (GthBrowser *browser)
{
1245 1246 1247 1248 1249
	if (browser->priv->load_file_timeout != 0) {
		g_source_remove (browser->priv->load_file_timeout);
		browser->priv->load_file_timeout = 0;
	}

Paolo Bacchilega's avatar
Paolo Bacchilega committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	g_list_foreach (browser->priv->load_data_queue, (GFunc) load_data_cancel, NULL);
}


static GFile *
get_nearest_entry_point (GFile *file)
{
	GList *list;
	GList *scan;
	GList *entries;
	char  *nearest_uri;
	int    max_len;
	GFile *nearest;

	entries = NULL;
	list = gth_main_get_all_entry_points ();
	for (scan = list; scan; scan = scan->next) {
		GthFileData *entry_point = scan->data;

		if (g_file_equal (file, entry_point->file) || g_file_has_prefix (file, entry_point->file))
			entries = g_list_prepend (entries, g_file_get_uri (entry_point->file));
	}

	nearest_uri = NULL;
	max_len = 0;
	for (scan = entries; scan; scan = scan->next) {
		char *entry_uri = scan->data;
		int   entry_len = strlen (entry_uri);

		if (entry_len > max_len) {
			nearest_uri = entry_uri;
			max_len = entry_len;
		}
	}

	nearest = NULL;
	if (nearest_uri != NULL)
		nearest = g_file_new_for_uri (nearest_uri);

	_g_string_list_free (entries);
	_g_object_list_unref (list);

	return nearest;
}


static void
_gth_browser_load (GthBrowser *browser,
		   GFile      *location,
		   GthAction   action)
{
	LoadData *load_data;
	GFile    *entry_point;

	_gth_browser_cancel (browser);

	switch (action) {
	case GTH_ACTION_GO_BACK:
	case GTH_ACTION_GO_FORWARD:
	case GTH_ACTION_GO_TO:
		if (browser->priv->location_source != NULL) {
			gth_file_source_monitor_directory (browser->priv->location_source,
1312
							   browser->priv->location->file,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
							   FALSE);
			_g_object_unref (browser->priv->location_source);
			browser->priv->location_source = NULL;
		}
		break;
	default:
		break;
	}

	entry_point = get_nearest_entry_point (location);
	load_data = load_data_new (browser, location, action, entry_point);

1325
	gth_hook_invoke ("gth-browser-load-location-before", browser, load_data->requested_folder);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
	browser->priv->activity_ref++;

	if (entry_point == NULL) {
		GError *error;
		char   *uri;

		uri = g_file_get_uri (location);
		error = g_error_new (GTHUMB_ERROR, 0, _("No suitable module found for %s"), uri);
		load_data_ready (load_data, NULL, error);

		g_free (uri);

		return;
	}

	switch (action) {
	case GTH_ACTION_LIST_CHILDREN:
		gth_folder_tree_loading_children (GTH_FOLDER_TREE (browser->priv->folder_tree), location);
		break;
	case GTH_ACTION_GO_BACK:
	case GTH_ACTION_GO_FORWARD:
	case GTH_ACTION_GO_TO:
	case GTH_ACTION_VIEW:
		gth_file_list_clear (GTH_FILE_LIST (browser->priv->file_list), _("Getting folder listing..."));
		break;
	default:
		break;
	}

	if (load_data->file_source == NULL) {
		GError *error;
		char   *uri;

1359
		uri = g_file_get_uri (load_data->requested_folder);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
		error = g_error_new (GTHUMB_ERROR, 0, _("No suitable module found for %s"), uri);
		load_data_ready (load_data, NULL, error);

		g_free (uri);

		return;
	}

	switch (load_data->action) {
	case GTH_ACTION_GO_INTO:
	case GTH_ACTION_GO_TO:
1371 1372 1373
	case GTH_ACTION_VIEW:
		_gth_browser_set_location_from_file (browser, load_data->requested_folder);
		_gth_browser_add_to_history (browser, browser->priv->location->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1374 1375 1376 1377
		_gth_browser_update_history_list (browser);
		break;
	case GTH_ACTION_GO_BACK:
	case GTH_ACTION_GO_FORWARD:
1378
		_gth_browser_set_location_from_file (browser, load_data->requested_folder);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1379 1380 1381 1382 1383 1384 1385 1386 1387
		_gth_browser_update_history_list (browser);
		break;
	default:
		break;
	}

	{
		char *uri;

1388
		uri = g_file_get_uri (load_data->requested_folder);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508

		debug (DEBUG_INFO, "LOAD: %s\n", uri);
		performance (DEBUG_INFO, "loading %s", uri);

		g_free (uri);
	}

	gth_browser_update_sensitivity (browser);
	gth_browser_set_list_extra_widget (browser, NULL);
	load_data_load_next_folder (load_data);

	g_object_unref (entry_point);
}


/* -- _gth_browser_ask_whether_to_save -- */


typedef struct {
	GthBrowser         *browser;
	GthBrowserCallback  callback;
	gpointer            user_data;
} AskSaveData;


static void
ask_whether_to_save__done (AskSaveData *data,
			   gboolean     cancelled)
{
	if (cancelled)
		g_file_info_set_attribute_boolean (data->browser->priv->current_file->info, "file::is-modified", TRUE);
	if (data->callback != NULL)
		(*data->callback) (data->browser, cancelled, data->user_data);
	g_free (data);
}


static void
ask_whether_to_save__file_saved_cb (GthViewerPage *viewer_page,
				    GthFileData   *file_data,
				    GError        *error,
				    gpointer       user_data)
{
	AskSaveData *data = user_data;
	gboolean     error_occurred;

	error_occurred = error != NULL;
	if (error != NULL)
		_gtk_error_dialog_from_gerror_show (GTK_WINDOW (data->browser), _("Could not save the file"), &error);
	ask_whether_to_save__done (data, error_occurred);
}


enum {
	RESPONSE_SAVE,
	RESPONSE_NO_SAVE,
};


static void
ask_whether_to_save__response_cb (GtkWidget   *dialog,
				  int          response_id,
				  AskSaveData *data)
{
	gtk_widget_destroy (dialog);

	if (response_id == RESPONSE_SAVE)
		gth_viewer_page_save (data->browser->priv->viewer_page,
				      NULL,
				      ask_whether_to_save__file_saved_cb,
				      data);
	else
		ask_whether_to_save__done (data, response_id == GTK_RESPONSE_CANCEL);
}


static void
_gth_browser_ask_whether_to_save (GthBrowser         *browser,
				  GthBrowserCallback  callback,
				  gpointer            user_data)
{
	AskSaveData *data;
	char        *title;
	GtkWidget   *d;

	data = g_new0 (AskSaveData, 1);
	data->browser = browser;
	data->callback = callback;
	data->user_data = user_data;

	title = g_strdup_printf (_("Save changes to file '%s'?"), g_file_info_get_display_name (browser->priv->current_file->info));
	d = _gtk_message_dialog_new (GTK_WINDOW (browser),
				     GTK_DIALOG_MODAL,
				     GTK_STOCK_DIALOG_QUESTION,
				     title,
				     _("If you don't save, changes to the file will be permanently lost."),
				     _("Do _Not Save"), RESPONSE_NO_SAVE,
				     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				     GTK_STOCK_SAVE, RESPONSE_SAVE,
				     NULL);
	g_signal_connect (G_OBJECT (d),
			  "response",
			  G_CALLBACK (ask_whether_to_save__response_cb),
			  data);
	gtk_widget_show (d);

	g_free (title);
}


/* -- _gth_browser_close -- */


static void
_gth_browser_close_final_step (gpointer user_data)
{
	GthBrowser      *browser = user_data;
	gboolean        last_window;
	GdkWindowState  state;
	gboolean        maximized;
1509
	int             pos;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525

	browser_list = g_list_remove (browser_list, browser);
	last_window = gth_window_get_n_windows () == 1;

	/* Save visualization options only if the window is not maximized. */

	state = gdk_window_get_state (GTK_WIDGET (browser)->window);
	maximized = (state & GDK_WINDOW_STATE_MAXIMIZED) != 0;
	if (! maximized && GTK_WIDGET_VISIBLE (browser)) {
		int width, height;

		gdk_drawable_get_size (GTK_WIDGET (browser)->window, &width, &height);
		eel_gconf_set_integer (PREF_UI_WINDOW_WIDTH, width);
		eel_gconf_set_integer (PREF_UI_WINDOW_HEIGHT, height);
	}

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
	pos = gtk_paned_get_position (GTK_PANED (browser->priv->browser_container));
	if (pos > 0)
		eel_gconf_set_integer (PREF_UI_BROWSER_SIDEBAR_WIDTH, pos);

	pos = _gtk_paned_get_position2 (GTK_PANED (browser->priv->viewer_pane));
	if (pos > 0)
		eel_gconf_set_integer (PREF_UI_VIEWER_SIDEBAR_WIDTH, pos);

	pos = _gtk_paned_get_position2 (GTK_PANED (browser->priv->browser_sidebar));
	if (pos > 0)
		eel_gconf_set_integer (PREF_UI_PROPERTIES_HEIGHT, pos);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

	/**/

	gth_hook_invoke ("gth-browser-close", browser);

	if (last_window) {
		if (eel_gconf_get_boolean (PREF_GO_TO_LAST_LOCATION, TRUE)
		    && (browser->priv->location != NULL))
		{
			char *uri;

1548
			uri = g_file_get_uri (browser->priv->location->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
			eel_gconf_set_path (PREF_STARTUP_LOCATION, uri);

			g_free (uri);
		}

		eel_gconf_set_string (PREF_SORT_TYPE, browser->priv->sort_type->name);
		eel_gconf_set_boolean (PREF_SORT_INVERSE, browser->priv->sort_inverse);

		gth_hook_invoke ("gth-browser-close-last-window", browser);
	}

	if (browser->priv->folder_popup != NULL)
		gtk_widget_destroy (browser->priv->folder_popup);
	if (browser->priv->file_list_popup != NULL)
		gtk_widget_destroy (browser->priv->file_list_popup);
1564 1565
	if (browser->priv->file_popup != NULL)
		gtk_widget_destroy (browser->priv->file_popup);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 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 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 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 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728

	gtk_widget_destroy (GTK_WIDGET (browser));
}


static void
_gth_browser_close_step3 (gpointer user_data)
{
	GthBrowser *browser = user_data;

	gth_file_list_cancel (GTH_FILE_LIST (browser->priv->file_list), _gth_browser_close_final_step, browser);
}


static void
_gth_browser_real_close (GthBrowser *browser)
{
	int i;

	/* remove gconf notifications */

	for (i = 0; i < GCONF_NOTIFICATIONS; i++)
		if (browser->priv->cnxn_id[i] != 0)
			eel_gconf_notification_remove (browser->priv->cnxn_id[i]);

	/* disconnect from the monitor */

	g_signal_handler_disconnect (gth_main_get_default_monitor (),
				     browser->priv->bookmarks_changed_id);
	g_signal_handler_disconnect (gth_main_get_default_monitor (),
				     browser->priv->folder_changed_id);
	g_signal_handler_disconnect (gth_main_get_default_monitor (),
				     browser->priv->file_renamed_id);
	g_signal_handler_disconnect (gth_main_get_default_monitor (),
				     browser->priv->metadata_changed_id);
	g_signal_handler_disconnect (gth_main_get_default_monitor (),
				     browser->priv->entry_points_changed_id);

	/* cancel async operations */

	browser->priv->closing = TRUE;

	_gth_browser_cancel (browser);

	if ((browser->priv->task != NULL) && gth_task_is_running (browser->priv->task))
		gth_task_cancel (browser->priv->task);
	else
		_gth_browser_close_step3 (browser);
}


static void
close__file_saved_cb (GthBrowser *browser,
		      gboolean    cancelled,
		      gpointer    user_data)
{
	if (! cancelled)
		_gth_browser_real_close (browser);
}


static void
_gth_browser_close (GthWindow *window)
{
	GthBrowser *browser = (GthBrowser *) window;

	if (gth_browser_get_file_modified (browser))
		_gth_browser_ask_whether_to_save (browser,
						  close__file_saved_cb,
						  NULL);
	else
		_gth_browser_real_close (browser);
}


static void
_gth_browser_update_viewer_ui (GthBrowser *browser,
			       int         page)
{
	if (page == GTH_BROWSER_PAGE_VIEWER) {
		GError *error = NULL;

		if (browser->priv->viewer_ui_merge_id != 0)
			return;
		browser->priv->viewer_ui_merge_id = gtk_ui_manager_add_ui_from_string (browser->priv->ui, viewer_ui_info, -1, &error);
		if (browser->priv->viewer_ui_merge_id == 0) {
			g_warning ("ui building failed: %s", error->message);
			g_clear_error (&error);
		}
	}
	else if (browser->priv->viewer_ui_merge_id != 0) {
		gtk_ui_manager_remove_ui (browser->priv->ui, browser->priv->viewer_ui_merge_id);
		browser->priv->viewer_ui_merge_id = 0;
	}

	if (browser->priv->viewer_page != NULL) {
		if (page == GTH_BROWSER_PAGE_VIEWER)
			gth_viewer_page_show (browser->priv->viewer_page);
		else
			gth_viewer_page_hide (browser->priv->viewer_page);
	}
}


static void
_gth_browser_update_browser_ui (GthBrowser *browser,
				int         page)
{
	if (page == GTH_BROWSER_PAGE_BROWSER) {
		GError *error = NULL;

		if (browser->priv->browser_ui_merge_id != 0)
			return;
		browser->priv->browser_ui_merge_id = gtk_ui_manager_add_ui_from_string (browser->priv->ui, browser_ui_info, -1, &error);
		if (browser->priv->browser_ui_merge_id == 0) {
			g_warning ("ui building failed: %s", error->message);
			g_clear_error (&error);
		}
	}
	else if (browser->priv->browser_ui_merge_id != 0) {
		gtk_ui_manager_remove_ui (browser->priv->ui, browser->priv->browser_ui_merge_id);
		browser->priv->browser_ui_merge_id = 0;
	}
}


static void
_gth_browser_set_current_page (GthWindow *window,
			       int        page)
{
	GthBrowser *browser = (GthBrowser *) window;

	GTH_WINDOW_CLASS (parent_class)->set_current_page (window, page);

	_gth_browser_update_viewer_ui (browser, page);
	_gth_browser_update_browser_ui (browser, page);

	gth_hook_invoke ("gth-browser-set-current-page", browser);

	gth_browser_update_title (browser);
}


static void
gth_browser_init (GthBrowser *browser)
{
	int i;

	browser->priv = g_new0 (GthBrowserPrivateData, 1);
	browser->priv->menu_icon_cache = gth_icon_cache_new_for_widget (GTK_WIDGET (browser), GTK_ICON_SIZE_MENU);
	browser->priv->named_dialogs = g_hash_table_new (g_str_hash, g_str_equal);

	for (i = 0; i < GCONF_NOTIFICATIONS; i++)
		browser->priv->cnxn_id[i] = 0;
}


static void
gth_browser_finalize (GObject *object)
{
	GthBrowser *browser = GTH_BROWSER (object);

	if (browser->priv != NULL) {