gth-browser.c 190 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
/* -*- 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
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
Paolo Bacchilega's avatar
Paolo Bacchilega committed
20 21 22
 */

#include <config.h>
23
#include <math.h>
Paolo Bacchilega's avatar
Paolo Bacchilega committed
24
#include <gtk/gtk.h>
25
#include <gdk/gdkkeysyms.h>
Paolo Bacchilega's avatar
Paolo Bacchilega committed
26 27 28
#include "dlg-personalize-filters.h"
#include "glib-utils.h"
#include "gtk-utils.h"
29
#include "gth-auto-paned.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
30 31 32 33 34
#include "gth-browser.h"
#include "gth-browser-actions-callbacks.h"
#include "gth-browser-actions-entries.h"
#include "gth-duplicable.h"
#include "gth-enum-types.h"
35
#include "gth-error.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
36 37 38 39 40 41
#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"
42
#include "gth-grid-view.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
43
#include "gth-icon-cache.h"
44
#include "gth-info-bar.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
45
#include "gth-image-preloader.h"
46
#include "gth-location-bar.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
47 48 49 50
#include "gth-location-chooser.h"
#include "gth-main.h"
#include "gth-marshal.h"
#include "gth-metadata-provider.h"
51
#include "gth-paned.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
52
#include "gth-preferences.h"
53
#include "gth-progress-dialog.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
54 55
#include "gth-sidebar.h"
#include "gth-statusbar.h"
56
#include "gth-user-dir.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
57 58
#include "gth-viewer-page.h"
#include "gth-window.h"
59
#include "main.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
60 61 62

#define GTH_BROWSER_CALLBACK(f) ((GthBrowserCallback) (f))
#define MAX_HISTORY_LENGTH 15
63
#define LOAD_FILE_DELAY 150
64
#define LOAD_METADATA_DELAY 150
65
#define HIDE_MOUSE_DELAY 3000
66
#define MOTION_THRESHOLD 0
67
#define UPDATE_SELECTION_DELAY 200
68
#define MIN_SIDEBAR_SIZE 100
69
#define MIN_VIEWER_SIZE 256
70
#define STATUSBAR_SEPARATOR "  ·  "
71 72
#define SHIRNK_WRAP_WIDTH_OFFSET 100
#define SHIRNK_WRAP_HEIGHT_OFFSET 125
73
#define FILE_PROPERTIES_MINIMUM_HEIGHT 100
74
#define HISTORY_FILE "history.xbel"
75 76
#define SECTION_BIG_MARGIN 12

Paolo Bacchilega's avatar
Paolo Bacchilega committed
77

78 79 80
G_DEFINE_TYPE (GthBrowser, gth_browser, GTH_TYPE_WINDOW)


Paolo Bacchilega's avatar
Paolo Bacchilega committed
81 82 83 84 85
enum {
	LOCATION_READY,
	LAST_SIGNAL
};

86

87 88 89 90 91 92 93 94 95 96
typedef struct {
	gboolean        saved;
	GthBrowserPage  page;
	GFile          *location;
	GFile          *current_file;
	GList          *selected;
	double          vscroll;
} BrowserState;


Paolo Bacchilega's avatar
Paolo Bacchilega committed
97
struct _GthBrowserPrivate {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
98 99
	/* UI staff */

100
	GtkWidget         *infobar;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
101
	GtkWidget         *statusbar;
102 103
	GtkWidget         *browser_right_container;
	GtkWidget         *browser_left_container;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
104 105 106 107
	GtkWidget         *browser_sidebar;
	GtkWidget         *folder_tree;
	GtkWidget         *folder_popup;
	GtkWidget         *file_list_popup;
108
	GtkWidget         *file_popup;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
109 110 111
	GtkWidget         *filterbar;
	GtkWidget         *file_list;
	GtkWidget         *list_extra_widget_container;
112 113
	GtkWidget         *list_info_bar;
	GtkWidget         *location_bar;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
114
	GtkWidget         *file_properties;
115
	GtkWidget         *header_sections[GTH_BROWSER_N_HEADER_SECTIONS];
116 117
	GtkWidget         *browser_status_commands;
	GtkWidget         *viewer_status_commands;
118
	GHashTable	  *menu_managers;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
119

120 121
	GtkWidget         *thumbnail_list;

122
	GList             *viewer_pages;
123
	GtkOrientation     viewer_thumbnails_orientation;
124 125
	GtkWidget         *viewer_thumbnails_pane;
	GtkWidget         *viewer_sidebar_pane;
126
	GtkWidget         *viewer_sidebar_alignment;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
127 128 129 130
	GtkWidget         *viewer_container;
	GthViewerPage     *viewer_page;
	GthImagePreloader *image_preloader;

131 132
	GtkWidget         *progress_dialog;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
133 134 135 136 137 138 139
	GHashTable        *named_dialogs;

	/* Browser data */

	gulong             folder_changed_id;
	gulong             file_renamed_id;
	gulong             metadata_changed_id;
140
	gulong             emblems_changed_id;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
141
	gulong             entry_points_changed_id;
142
	gulong             order_changed_id;
143
	GthFileData       *location;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
144 145
	GthFileData       *current_file;
	GthFileSource     *location_source;
146 147
	int                n_visibles;
	int                current_file_position;
148
	GFile             *monitor_location;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
149 150
	gboolean           activity_ref;
	GthIconCache      *menu_icon_cache;
151 152 153 154
	GthFileDataSort   *current_sort_type;
	gboolean           current_sort_inverse;
	GthFileDataSort   *default_sort_type;
	gboolean           default_sort_inverse;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
155 156 157 158 159
	gboolean           show_hidden_files;
	gboolean           fast_file_type;
	gboolean           closing;
	GthTask           *task;
	gulong             task_completed;
160
	gulong             task_progress;
161
	GList             *background_tasks;
162
	gboolean           close_with_task;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
163
	GList             *load_data_queue;
164
	GList             *load_file_data_queue;
165
	guint              load_file_timeout;
166
	guint              load_metadata_timeout;
167
	char              *list_attributes;
168
	gboolean           constructed;
169
	guint              construct_step2_event;
170
	guint              selection_changed_event;
171
	GthFileData       *folder_popup_file_data;
172
	gboolean           properties_on_screen;
173 174
	char              *location_free_space;
	gboolean           recalc_location_free_space;
175
	gboolean           file_properties_on_the_right;
176
	GthSidebarState    viewer_sidebar;
177
	BrowserState       state;
178

179 180 181 182 183 184
	/* settings */

	GSettings         *browser_settings;
	GSettings         *messages_settings;
	GSettings         *desktop_interface_settings;

185
	/* fullscreen */
186

Paolo Bacchilega's avatar
Paolo Bacchilega committed
187
	gboolean           fullscreen;
188
	gboolean	   was_fullscreen;
189 190
	GtkWidget	  *fullscreen_toolbar;
	GList             *viewer_controls;
191
	gboolean           pointer_visible;
192 193 194 195
	guint              hide_mouse_timeout;
	guint              motion_signal;
	gdouble            last_mouse_x;
	gdouble            last_mouse_y;
196 197 198 199 200
	struct {
		int      page;
		gboolean viewer_properties;
		gboolean viewer_tools;
		gboolean thumbnail_list;
201
		gboolean browser_properties;
202
	} before_fullscreen;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
203 204 205 206 207

	/* history */

	GList             *history;
	GList             *history_current;
208
	GMenu             *history_menu;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
209 210 211 212 213 214
};


static guint gth_browser_signals[LAST_SIGNAL] = { 0 };


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


static void
browser_state_init (BrowserState *state)
{
	state->saved = FALSE;
	state->page = 0;
	state->location = NULL;
	state->current_file = NULL;
	state->selected = NULL;
}


static void
browser_state_free (BrowserState *state)
{
	if (! state->saved)
		return;

	_g_object_unref (state->location);
	_g_object_unref (state->current_file);
	g_list_foreach (state->selected, (GFunc) gtk_tree_path_free, NULL);
	g_list_free (state->selected);

	state->location = NULL;
	state->current_file = NULL;
	state->selected = NULL;
	state->saved = FALSE;
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
247 248 249 250 251 252 253
/* -- monitor_event_data -- */


typedef struct {
	int              ref;
	GthFileSource   *file_source;
	GFile           *parent;
254
	int              position;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
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
	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 -- */


300 301
static void
_gth_browser_update_current_file_position (GthBrowser *browser)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
302
{
303
	GthFileStore *file_store;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
304

305 306 307 308
	file_store = gth_browser_get_file_store (browser);
	browser->priv->n_visibles = gth_file_store_n_visibles (file_store);
	browser->priv->current_file_position = -1;

309
	if (browser->priv->current_file != NULL) {
310
		int pos;
311

312
		pos = gth_file_store_get_pos (file_store, browser->priv->current_file->file);
313
		if (pos >= 0)
314
			browser->priv->current_file_position = pos;
315 316 317 318 319 320 321
	}
}


void
gth_browser_update_title (GthBrowser *browser)
{
322 323 324
	GString    *title;
	const char *name = NULL;
	GList      *emblems = NULL;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

	title = g_string_new (NULL);

	switch (gth_window_get_current_page (GTH_WINDOW (browser))) {
	case GTH_BROWSER_PAGE_BROWSER:
		if (browser->priv->location != NULL)
			name = g_file_info_get_display_name (browser->priv->location->info);
		if (name != NULL)
			g_string_append (title, name);
		break;

	case GTH_BROWSER_PAGE_VIEWER:
		if (browser->priv->current_file != NULL)
			name = g_file_info_get_display_name (browser->priv->current_file->info);
		if (name != NULL)
			g_string_append (title, name);

		if (gth_browser_get_file_modified (browser)) {
			g_string_append (title, " ");
			g_string_append (title, _("[modified]"));
345
		}
346 347 348 349 350 351 352 353

		if (browser->priv->current_file != NULL) {
			GthStringList *string_list;

			string_list = GTH_STRING_LIST (g_file_info_get_attribute_object (browser->priv->current_file->info, GTH_FILE_ATTRIBUTE_EMBLEMS));
			if (string_list != NULL)
				emblems = _g_string_list_dup (gth_string_list_get_list (string_list));
		}
354
		break;
355 356
	}

357 358
	if (title->len == 0)
		g_string_append (title, _("gThumb"));
359

360 361 362
	gth_window_set_title (GTH_WINDOW (browser),
			      title->str,
			      emblems);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
363 364

	g_string_free (title, TRUE);
365
	_g_string_list_free (emblems);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
366 367 368 369 370 371 372 373 374 375
}


void
gth_browser_update_sensitivity (GthBrowser *browser)
{
	GFile    *parent;
	gboolean  parent_available;
	gboolean  viewer_can_save;
	gboolean  modified;
376
	int       n_selected;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
377 378

	if (browser->priv->location != NULL)
379
		parent = g_file_get_parent (browser->priv->location->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
380 381 382 383 384 385 386
	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);
387
	n_selected = gth_file_selection_get_n_selected (GTH_FILE_SELECTION (gth_browser_get_file_list_view (browser)));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
388

389 390 391
	gth_window_enable_action (GTH_WINDOW (browser), "show-thumbnail-list", gth_window_get_current_page (GTH_WINDOW (browser)) == GTH_BROWSER_PAGE_VIEWER);
	gth_window_enable_action (GTH_WINDOW (browser), "show-sidebar", gth_window_get_current_page (GTH_WINDOW (browser)) == GTH_BROWSER_PAGE_BROWSER);
	/* gth_window_enable_action (GTH_WINDOW (browser), "view-reload", gth_window_get_current_page (GTH_WINDOW (browser)) == GTH_BROWSER_PAGE_BROWSER);*/ /* FIXME add view-reload */
392 393 394 395 396
	gth_window_enable_action (GTH_WINDOW (browser), "save", viewer_can_save && modified);
	gth_window_enable_action (GTH_WINDOW (browser), "save-as", viewer_can_save);
	gth_window_enable_action (GTH_WINDOW (browser), "revert-to-saved", viewer_can_save && modified);
	gth_window_enable_action (GTH_WINDOW (browser), "clear-history", browser->priv->history != NULL);
	gth_window_enable_action (GTH_WINDOW (browser), "go-up", parent_available);
397
	gth_window_enable_action (GTH_WINDOW (browser), "browser-edit-file", n_selected == 1);
398

399 400
	gth_sidebar_update_sensitivity (GTH_SIDEBAR (browser->priv->file_properties));

Paolo Bacchilega's avatar
Paolo Bacchilega committed
401 402 403 404 405 406 407
	if (browser->priv->viewer_page != NULL)
		gth_viewer_page_update_sensitivity (browser->priv->viewer_page);

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


408
void
409
gth_browser_update_extra_widget (GthBrowser *browser)
410
{
411 412 413
	gtk_widget_show (browser->priv->location_bar);
	_gtk_container_remove_children (GTK_CONTAINER (gth_location_bar_get_action_area (GTH_LOCATION_BAR (browser->priv->location_bar))), NULL, NULL);
	gth_location_bar_set_from_file (GTH_LOCATION_BAR (browser->priv->location_bar), browser->priv->location->file);
414
	gth_hook_invoke ("gth-browser-update-extra-widget", browser);
415 416 417
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
418
static void
419 420
_gth_browser_set_location (GthBrowser  *browser,
			   GthFileData *location)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
421
{
422 423
	GtkWidget *location_chooser;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
424 425 426 427 428
	if (location == NULL)
		return;

	if (browser->priv->location != NULL)
		g_object_unref (browser->priv->location);
429
	browser->priv->location = gth_file_data_dup (location);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
430

431
	_gth_browser_update_current_file_position (browser);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
432 433
	gth_browser_update_title (browser);
	gth_browser_update_sensitivity (browser);
434

435
	location_chooser = gth_location_bar_get_chooser (GTH_LOCATION_BAR (browser->priv->location_bar));
436
	g_signal_handlers_block_by_data (location_chooser, browser);
437
	gth_browser_update_extra_widget (browser);
438
	g_signal_handlers_unblock_by_data (location_chooser, browser);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
439 440 441
}


442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
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
461 462 463
static void
_gth_browser_update_go_sensitivity (GthBrowser *browser)
{
464 465 466 467 468 469 470 471
	g_object_set (g_action_map_lookup_action (G_ACTION_MAP (browser), "go-back"),
		      "enabled",
		      (browser->priv->history_current != NULL) && (browser->priv->history_current->next != NULL),
		      NULL);
	g_object_set (g_action_map_lookup_action (G_ACTION_MAP (browser), "go-forward"),
		      "enabled",
		      (browser->priv->history_current != NULL) && (browser->priv->history_current->prev != NULL),
		      NULL);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
472 473 474
}


475 476
#if 0
static void
477
_gth_browser_history_print (GthBrowser *browser)
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
{
	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


Paolo Bacchilega's avatar
Paolo Bacchilega committed
495
static void
496
_gth_browser_history_menu (GthBrowser *browser)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
497 498 499
{
	_gth_browser_update_go_sensitivity (browser);

500
	/* Update the history menu model for the headerbar button */
Paolo Bacchilega's avatar
Paolo Bacchilega committed
501

502
	g_menu_remove_all (browser->priv->history_menu);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
503

504
	if (browser->priv->history != NULL) {
505 506
		GList *scan;
		int    i;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
507

508 509 510
		for (i = 0, scan = browser->priv->history;
		     scan;
		     scan = scan->next, i++)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
511
		{
512 513 514
			GFile     *file = scan->data;
			GMenuItem *item;
			char      *target;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
515

516
			item = _g_menu_item_new_for_file (file, NULL);
517 518 519
			target = g_strdup_printf ("%d", i);
			g_menu_item_set_action_and_target (item, "win.go-to-history-position", "s", target);
			g_menu_append_item (browser->priv->history_menu, item);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
520

521 522 523 524
			if (browser->priv->history_current == scan) {
				GAction *action = g_action_map_lookup_action (G_ACTION_MAP (browser), "go-to-history-position");
				g_simple_action_set_state (G_SIMPLE_ACTION (action), g_variant_new_string (target));
			}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
525

526
			g_free (target);
527
			g_object_unref (item);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
528 529 530 531 532 533
		}
	}
}


static void
534 535
_gth_browser_history_add (GthBrowser *browser,
			  GFile      *file)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
536 537 538 539
{
	if (file == NULL)
		return;

540
	if ((browser->priv->history_current == NULL) || ! _g_file_equal_uris (file, browser->priv->history_current->data)) {
541 542
		GList *scan;

543 544 545 546 547 548 549 550 551 552 553
		/* remove all files after the current position */
		for (scan = browser->priv->history; scan && (scan != browser->priv->history_current); /* void */) {
			GList *next = scan->next;

			browser->priv->history = g_list_remove_link (browser->priv->history, scan);
			_g_object_list_unref (scan);

			scan = next;
		}

		/* remove all the occurrences of 'file' from the history */
554
		for (scan = browser->priv->history; scan; /* void */) {
555 556 557
			GList *next = scan->next;
			GFile *file_in_history = scan->data;

558 559
			if (_g_file_equal_uris (file, file_in_history)) {
				browser->priv->history = g_list_remove_link (browser->priv->history, scan);
560 561 562 563 564 565
				_g_object_list_unref (scan);
			}

			scan = next;
		}

Paolo Bacchilega's avatar
Paolo Bacchilega committed
566 567 568 569 570 571
		browser->priv->history = g_list_prepend (browser->priv->history, g_object_ref (file));
		browser->priv->history_current = browser->priv->history;
	}
}


572 573 574
static void
_gth_browser_history_save (GthBrowser *browser)
{
575 576
	GSettings     *privacy_settings;
	gboolean       save_history;
577 578 579 580 581 582
	GBookmarkFile *bookmarks;
	GFile         *file;
	char          *filename;
	GList         *scan;
	int            n;

583 584 585 586
	privacy_settings = _g_settings_new_if_schema_installed ("org.gnome.desktop.privacy");
        save_history = (privacy_settings == NULL) || g_settings_get_boolean (privacy_settings, "remember-recent-files");
        _g_object_unref (privacy_settings);

587 588 589 590
	if (! save_history) {
		file = gth_user_dir_get_file_for_read (GTH_DIR_CONFIG, GTHUMB_DIR, HISTORY_FILE, NULL);
		g_file_delete (file, NULL, NULL);
		g_object_unref (file);
591
		return;
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
	bookmarks = g_bookmark_file_new ();
	for (scan = browser->priv->history, n = 0; scan && (n < MAX_HISTORY_LENGTH); scan = scan->next, n++) {
		GFile *location = scan->data;
		char  *uri;

		uri = g_file_get_uri (location);
		_g_bookmark_file_add_uri (bookmarks, uri);

		g_free (uri);
	}
	file = gth_user_dir_get_file_for_write (GTH_DIR_CONFIG, GTHUMB_DIR, HISTORY_FILE, NULL);
	filename = g_file_get_path (file);
	g_bookmark_file_to_file (bookmarks, filename, NULL);

	g_free (filename);
	g_object_unref (file);
	g_bookmark_file_free (bookmarks);
}


static void
_gth_browser_history_load (GthBrowser *browser)
{
617 618
	GSettings     *privacy_settings;
	gboolean       load_history;
619 620 621 622 623 624
	GBookmarkFile *bookmarks;
	GFile         *file;
	char          *filename;

	_g_object_list_unref (browser->priv->history);
	browser->priv->history = NULL;
625 626 627 628 629 630 631 632
	browser->priv->history_current = NULL;

	privacy_settings = _g_settings_new_if_schema_installed ("org.gnome.desktop.privacy");
	load_history = (privacy_settings == NULL) || g_settings_get_boolean (privacy_settings, "remember-recent-files");
	_g_object_unref (privacy_settings);

	if (! load_history)
		return;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656

	bookmarks = g_bookmark_file_new ();
	file = gth_user_dir_get_file_for_read (GTH_DIR_CONFIG, GTHUMB_DIR, HISTORY_FILE, NULL);
	filename = g_file_get_path (file);
	if (g_bookmark_file_load_from_file (bookmarks, filename, NULL)) {
		char **uris;
		int    i;

		uris = g_bookmark_file_get_uris (bookmarks, NULL);
		for (i = 0; (uris[i] != NULL) && (i < MAX_HISTORY_LENGTH); i++)
			browser->priv->history = g_list_prepend (browser->priv->history, g_file_new_for_uri (uris[i]));
		browser->priv->history = g_list_reverse (browser->priv->history);

		g_strfreev (uris);
	}

	browser->priv->history_current = browser->priv->history;

	g_free (filename);
	g_object_unref (file);
	g_bookmark_file_free (bookmarks);
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
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)
{
672 673
	GList *entry_points;
	GFile *root;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689

	entry_points = gth_main_get_all_entry_points ();
	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;

690 691 692
	if (browser->priv->filterbar == NULL)
		return NULL;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
693 694 695 696 697 698 699 700 701
	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;
}


702 703 704 705 706 707 708 709
static gboolean
_gth_browser_get_fast_file_type (GthBrowser *browser,
				 GFile      *file)
{
	gboolean fast_file_type;

	fast_file_type = browser->priv->fast_file_type;

710 711 712 713 714
	/* Force the value to FALSE when browsing a cache or a temporary files
	 * directory.
	 * This is mainly used to browse the Firefox cache without changing the
	 * general preference each time, but can be useful for other caches
	 * as well. */
715 716
	if (g_file_has_uri_scheme (file, "file")) {
		char  *uri;
717
		char  *tmp_uri;
718 719 720 721
		char **uri_v;
		int    i;

		uri = g_file_get_uri (file);
722 723 724 725 726 727 728 729 730 731 732 733 734 735

		tmp_uri = g_filename_to_uri (g_get_tmp_dir (), NULL, NULL);
		if (tmp_uri != NULL) {
			gboolean is_tmp_dir;

			is_tmp_dir = (strcmp (uri, tmp_uri) == 0);
			g_free (tmp_uri);

			if (is_tmp_dir) {
				g_free (uri);
				return FALSE;
			}
		}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
		uri_v = g_strsplit (uri, "/", -1);
		for (i = 0; uri_v[i] != NULL; i++)
			if (strstr (uri_v[i], "cache")
			    || strstr (uri_v[i], "CACHE")
			    || strstr (uri_v[i], "Cache"))
			{
				fast_file_type = FALSE;
				break;
			}

		 g_strfreev (uri_v);
		 g_free (uri);
	}

	return fast_file_type;
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
754
static void
755
_update_statusbar_list_info (GthBrowser *browser)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
756
{
757 758 759 760 761 762 763 764 765 766 767 768
	GList   *file_list;
	int      n_total;
	goffset  size_total;
	GList   *scan;
	int      n_selected;
	goffset  size_selected;
	GList   *selected;
	char    *size_total_formatted;
	char    *size_selected_formatted;
	char    *text_total;
	char    *text_selected;
	GString *text;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
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

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

	/**/

801 802
	size_total_formatted = g_format_size (size_total);
	size_selected_formatted = g_format_size (size_selected);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
803
	text_total = g_strdup_printf (g_dngettext (NULL, "%d file (%s)", "%d files (%s)", n_total), n_total, size_total_formatted);
804
	text_selected = g_strdup_printf (g_dngettext (NULL, "%d file selected (%s)", "%d files selected (%s)", n_selected), n_selected, size_selected_formatted);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
805

806 807 808 809 810
	text = g_string_new (text_total);
	if (n_selected > 0) {
		g_string_append (text, STATUSBAR_SEPARATOR);
		g_string_append (text, text_selected);
	}
811
	if (browser->priv->location_free_space != NULL) {
812
		g_string_append (text, STATUSBAR_SEPARATOR);
813
		g_string_append (text, browser->priv->location_free_space);
814 815 816 817
	}
	gth_statusbar_set_list_info (GTH_STATUSBAR (browser->priv->statusbar), text->str);

	g_string_free (text, TRUE);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
818 819 820 821 822 823 824
	g_free (text_selected);
	g_free (text_total);
	g_free (size_selected_formatted);
	g_free (size_total_formatted);
}


825 826 827 828 829 830 831 832 833 834 835 836 837
static void
get_free_space_ready_cb (GthFileSource *file_source,
			 guint64        total_size,
			 guint64        free_space,
			 GError        *error,
			 gpointer       data)
{
	GthBrowser *browser = data;
	char       *free_space_text;

	if (error == NULL) {
		char *free_space_formatted;

838
		free_space_formatted = g_format_size (free_space);
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
		free_space_text = g_strdup_printf (_("%s of free space"), free_space_formatted);

		g_free (free_space_formatted);
	}
	else
		free_space_text = NULL;

	g_free (browser->priv->location_free_space);
	browser->priv->location_free_space = NULL;
	if (free_space_text != NULL)
		browser->priv->location_free_space = g_strdup (free_space_text);
	g_free (free_space_text);

	browser->priv->recalc_location_free_space = FALSE;

	_update_statusbar_list_info (browser);
}


858 859 860
static void
_gth_browser_update_statusbar_list_info (GthBrowser *browser)
{
861 862 863 864
	if (browser->priv->recalc_location_free_space
	    && (browser->priv->location_source != NULL)
	    && (browser->priv->location != NULL))
	{
865 866 867 868
		gth_file_source_get_free_space (browser->priv->location_source,
						browser->priv->location->file,
						get_free_space_ready_cb,
						browser);
869 870 871 872 873 874 875 876
	}
	else {
		if ((browser->priv->location_source == NULL)
		    || (browser->priv->location == NULL))
		{
			g_free (browser->priv->location_free_space);
			browser->priv->location_free_space = NULL;
		}
877
		_update_statusbar_list_info (browser);
878
	}
879 880 881
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
882 883
typedef struct {
	GthBrowser    *browser;
884
	GthFileData   *requested_folder;
885
	GFile         *requested_folder_parent;
886
	GFile         *file_to_select;
887 888
	GList         *selected;
	double         vscroll;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
889
	GthAction      action;
890
	gboolean       automatic;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
891 892 893 894 895 896 897 898 899 900 901
	GList         *list;
	GList         *current;
	GFile         *entry_point;
	GthFileSource *file_source;
	GCancellable  *cancellable;
} LoadData;


static LoadData *
load_data_new (GthBrowser *browser,
	       GFile      *location,
902
	       GFile      *file_to_select,
903 904
	       GList      *selected,
	       double      vscroll,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
905
	       GthAction   action,
906
	       gboolean    automatic,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
907 908 909 910 911 912 913
	       GFile      *entry_point)
{
	LoadData *load_data;
	GFile    *file;

	load_data = g_new0 (LoadData, 1);
	load_data->browser = browser;
914 915
	load_data->requested_folder = gth_file_data_new (location, NULL);
	load_data->requested_folder_parent = g_file_get_parent (load_data->requested_folder->file);
916 917
	if (file_to_select != NULL)
		load_data->file_to_select = g_file_dup (file_to_select);
918
	else if (browser->priv->current_file != NULL)
919
		load_data->file_to_select = g_file_dup (browser->priv->current_file->file);
920 921
	load_data->selected = g_list_copy_deep (selected, (GCopyFunc) gtk_tree_path_copy, NULL);
	load_data->vscroll = vscroll;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
922
	load_data->action = action;
923
	load_data->automatic = automatic;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
924 925 926 927 928 929
	load_data->cancellable = g_cancellable_new ();

	if (entry_point == NULL)
		return load_data;

	load_data->entry_point = g_object_ref (entry_point);
930
	load_data->file_source = gth_main_get_file_source (load_data->requested_folder->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
931

932
	file = g_object_ref (load_data->requested_folder->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	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);

957
	g_object_unref (data->requested_folder);
958
	_g_object_unref (data->requested_folder_parent);
959
	_g_object_unref (data->file_to_select);
960
	g_list_free_full (data->selected, (GDestroyNotify) gtk_tree_path_free);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
961 962 963 964 965 966 967 968
	_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);
}


969 970 971 972 973 974 975 976
static void
_gth_browser_load (GthBrowser *browser,
		   GFile      *location,
		   GFile      *file_to_select,
		   GList      *selected,
		   double      vscroll,
		   GthAction   action,
		   gboolean    automatic);
977 978


979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
static char *
file_format (const char *format,
	     GFile      *file)
{
	char *name;
	char *s;

	name = g_file_get_parse_name (file);
	s = g_strdup_printf (format, name);

	g_free (name);

	return s;
}


995 996 997 998 999 1000 1001 1002 1003 1004 1005
static void
_gth_browser_show_error (GthBrowser *browser,
			 const char *title,
			 GError     *error)
{
	/* _gtk_error_dialog_from_gerror_show (GTK_WINDOW (browser), title, error); */

	gth_info_bar_set_primary_text (GTH_INFO_BAR (browser->priv->infobar), title);
	gth_info_bar_set_secondary_text (GTH_INFO_BAR (browser->priv->infobar), error->message);

	gtk_orientable_set_orientation (GTK_ORIENTABLE (gtk_info_bar_get_action_area (GTK_INFO_BAR (browser->priv->infobar))), GTK_ORIENTATION_HORIZONTAL);
1006
	gth_info_bar_set_icon_name (GTH_INFO_BAR (browser->priv->infobar), "dialog-error-symbolic", GTK_ICON_SIZE_DIALOG);
1007 1008 1009
	gtk_info_bar_set_message_type (GTK_INFO_BAR (browser->priv->infobar), GTK_MESSAGE_ERROR);
	_gtk_info_bar_clear_action_area (GTK_INFO_BAR (browser->priv->infobar));
	gtk_info_bar_add_buttons (GTK_INFO_BAR (browser->priv->infobar),
1010
				  _GTK_LABEL_CLOSE, GTK_RESPONSE_CLOSE,
1011 1012 1013 1014 1015
				  NULL);
	gtk_widget_show (browser->priv->infobar);
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
1016
static void
1017
load_data_done (LoadData *load_data,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1018 1019
		GError   *error)
{
1020
	GthBrowser *browser = load_data->browser;
1021
	char       *title;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1022 1023 1024 1025

	{
		char *uri;

1026
		uri = g_file_get_uri (load_data->requested_folder->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1027 1028 1029 1030 1031 1032
		debug (DEBUG_INFO, "LOAD READY: %s [%s]\n", uri, (error == NULL ? "Ok" : "Error"));
		performance (DEBUG_INFO, "load done for %s", uri);

		g_free (uri);
	}

1033 1034 1035
	/* moving the "gth-browser-load-location-after" after the
	 * LOCATION_READY signal emition can brake the extensions */

1036 1037 1038 1039 1040 1041
	if ((load_data->action == GTH_ACTION_GO_TO)
	    || (load_data->action == GTH_ACTION_GO_BACK)
	    || (load_data->action == GTH_ACTION_GO_FORWARD)
	    || (load_data->action == GTH_ACTION_GO_UP)
	    || (load_data->action == GTH_ACTION_VIEW))
	{
1042 1043 1044 1045
		if (error == NULL) {
			_g_object_unref (browser->priv->location_source);
			browser->priv->location_source = g_object_ref (load_data->file_source);
		}
1046 1047
		gth_browser_update_extra_widget (browser);
		gth_hook_invoke ("gth-browser-load-location-after", browser, browser->priv->location, error);
1048
	}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1049

1050 1051 1052 1053 1054 1055 1056
	browser->priv->activity_ref--;
	g_signal_emit (G_OBJECT (browser),
		       gth_browser_signals[LOCATION_READY],
		       0,
		       load_data->requested_folder->file,
		       (error != NULL));

Paolo Bacchilega's avatar
Paolo Bacchilega committed
1057 1058 1059 1060 1061 1062 1063 1064
	if (error == NULL)
		return;

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

1065 1066 1067
	if (load_data->automatic) {
		GFile *parent;

1068
		parent = g_file_get_parent (load_data->requested_folder->file);
1069 1070 1071
		if (parent != NULL) {
			_gth_browser_load (load_data->browser,
					   parent,
1072
					   NULL,
1073 1074
					   NULL,
					   0,
1075 1076 1077 1078 1079 1080 1081
					   load_data->action,
					   TRUE);
			g_object_unref (parent);
			return;
		}
	}

Paolo Bacchilega's avatar
Paolo Bacchilega committed
1082
	gth_browser_update_sensitivity (browser);
1083
	title = file_format (_("Could not load the position \"%s\""), load_data->requested_folder->file);
1084
	_gth_browser_show_error (browser, title, error);
1085 1086

	g_free (title);
1087
	g_error_free (error);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1088 1089 1090
}


1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
static void
load_data_error (LoadData *load_data,
		 GError   *error)
{
	GthBrowser *browser = load_data->browser;
	GFile      *loaded_folder;

	loaded_folder = (GFile *) load_data->current->data;
	gth_folder_tree_set_children (GTH_FOLDER_TREE (browser->priv->folder_tree), loaded_folder, NULL);

	switch (load_data->action) {
	case GTH_ACTION_GO_TO:
	case GTH_ACTION_GO_BACK:
	case GTH_ACTION_GO_FORWARD:
	case GTH_ACTION_GO_UP: