gth-browser.c 195 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 1 /* in seconds */
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
#define SECTION_BIG_MARGIN 12
76
#define OVERLAY_MARGIN 10
77

Paolo Bacchilega's avatar
Paolo Bacchilega committed
78

79 80 81
G_DEFINE_TYPE (GthBrowser, gth_browser, GTH_TYPE_WINDOW)


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

87

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


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

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

121 122
	GtkWidget         *thumbnail_list;

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

132 133
	GtkWidget         *progress_dialog;

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

	/* Browser data */

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

180 181 182 183 184 185
	/* settings */

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

186
	/* fullscreen */
187

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

	/* history */

	GList             *history;
	GList             *history_current;
212
	GMenu             *history_menu;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
213 214 215 216 217 218
};


static guint gth_browser_signals[LAST_SIGNAL] = { 0 };


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
/* -- 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
251 252 253 254 255 256 257
/* -- monitor_event_data -- */


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


304 305
static void
_gth_browser_update_current_file_position (GthBrowser *browser)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
306
{
307
	GthFileStore *file_store;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
308

309 310 311 312
	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;

313
	if (browser->priv->current_file != NULL) {
314
		int pos;
315

316
		pos = gth_file_store_get_pos (file_store, browser->priv->current_file->file);
317
		if (pos >= 0)
318
			browser->priv->current_file_position = pos;
319 320 321 322 323 324 325
	}
}


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

	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]"));
349
		}
350 351 352 353 354 355 356 357

		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));
		}
358
		break;
359 360
	}

361 362
	if (title->len == 0)
		g_string_append (title, _("gThumb"));
363

364 365 366
	gth_window_set_title (GTH_WINDOW (browser),
			      title->str,
			      emblems);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
367 368

	g_string_free (title, TRUE);
369
	_g_string_list_free (emblems);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
370 371 372 373 374 375 376 377 378 379
}


void
gth_browser_update_sensitivity (GthBrowser *browser)
{
	GFile    *parent;
	gboolean  parent_available;
	gboolean  viewer_can_save;
	gboolean  modified;
380
	int       n_selected;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
381 382

	if (browser->priv->location != NULL)
383
		parent = g_file_get_parent (browser->priv->location->file);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
384 385 386 387 388 389 390
	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);
391
	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
392

393 394 395
	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 */
396 397 398 399 400
	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);
401
	gth_window_enable_action (GTH_WINDOW (browser), "browser-edit-file", n_selected == 1);
402

403 404
	gth_sidebar_update_sensitivity (GTH_SIDEBAR (browser->priv->file_properties));

Paolo Bacchilega's avatar
Paolo Bacchilega committed
405 406 407 408 409 410 411
	if (browser->priv->viewer_page != NULL)
		gth_viewer_page_update_sensitivity (browser->priv->viewer_page);

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


412
void
413
gth_browser_update_extra_widget (GthBrowser *browser)
414
{
415 416 417
	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);
418
	gth_hook_invoke ("gth-browser-update-extra-widget", browser);
419 420 421
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
422
static void
423 424
_gth_browser_set_location (GthBrowser  *browser,
			   GthFileData *location)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
425
{
426 427
	GtkWidget *location_chooser;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
428 429 430 431 432
	if (location == NULL)
		return;

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

435
	_gth_browser_update_current_file_position (browser);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
436 437
	gth_browser_update_title (browser);
	gth_browser_update_sensitivity (browser);
438

439
	location_chooser = gth_location_bar_get_chooser (GTH_LOCATION_BAR (browser->priv->location_bar));
440
	g_signal_handlers_block_by_data (location_chooser, browser);
441
	gth_browser_update_extra_widget (browser);
442
	g_signal_handlers_unblock_by_data (location_chooser, browser);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
443 444 445
}


446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
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
465 466 467
static void
_gth_browser_update_go_sensitivity (GthBrowser *browser)
{
468 469 470 471 472 473 474 475
	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
476 477 478
}


479 480
#if 0
static void
481
_gth_browser_history_print (GthBrowser *browser)
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
{
	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
499
static void
500
_gth_browser_history_menu (GthBrowser *browser)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
501 502 503
{
	_gth_browser_update_go_sensitivity (browser);

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

506
	g_menu_remove_all (browser->priv->history_menu);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
507

508
	if (browser->priv->history != NULL) {
509 510
		GList *scan;
		int    i;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
511

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

520
			item = _g_menu_item_new_for_file (file, NULL);
521 522 523
			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
524

525 526 527 528
			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
529

530
			g_free (target);
531
			g_object_unref (item);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
532 533 534 535 536 537
		}
	}
}


static void
538 539
_gth_browser_history_add (GthBrowser *browser,
			  GFile      *file)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
540 541 542 543
{
	if (file == NULL)
		return;

544
	if ((browser->priv->history_current == NULL) || ! _g_file_equal_uris (file, browser->priv->history_current->data)) {
545 546
		GList *scan;

547 548 549 550 551 552 553 554 555 556 557
		/* 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 */
558
		for (scan = browser->priv->history; scan; /* void */) {
559 560 561
			GList *next = scan->next;
			GFile *file_in_history = scan->data;

562 563
			if (_g_file_equal_uris (file, file_in_history)) {
				browser->priv->history = g_list_remove_link (browser->priv->history, scan);
564 565 566 567 568 569
				_g_object_list_unref (scan);
			}

			scan = next;
		}

Paolo Bacchilega's avatar
Paolo Bacchilega committed
570 571 572 573 574 575
		browser->priv->history = g_list_prepend (browser->priv->history, g_object_ref (file));
		browser->priv->history_current = browser->priv->history;
	}
}


576 577 578
static void
_gth_browser_history_save (GthBrowser *browser)
{
579 580
	GSettings     *privacy_settings;
	gboolean       save_history;
581 582 583 584 585 586
	GBookmarkFile *bookmarks;
	GFile         *file;
	char          *filename;
	GList         *scan;
	int            n;

587 588 589 590
	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);

591 592 593 594
	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);
595
		return;
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
	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)
{
621 622
	GSettings     *privacy_settings;
	gboolean       load_history;
623 624 625 626 627 628
	GBookmarkFile *bookmarks;
	GFile         *file;
	char          *filename;

	_g_object_list_unref (browser->priv->history);
	browser->priv->history = NULL;
629 630 631 632 633 634 635 636
	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;
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

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

	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;

694 695 696
	if (browser->priv->filterbar == NULL)
		return NULL;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
697 698 699 700 701 702 703 704 705
	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;
}


706 707 708 709 710 711 712 713
static gboolean
_gth_browser_get_fast_file_type (GthBrowser *browser,
				 GFile      *file)
{
	gboolean fast_file_type;

	fast_file_type = browser->priv->fast_file_type;

714 715 716 717 718
	/* 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. */
719 720
	if (g_file_has_uri_scheme (file, "file")) {
		char  *uri;
721
		char  *tmp_uri;
722 723 724 725
		char **uri_v;
		int    i;

		uri = g_file_get_uri (file);
726 727 728 729 730 731 732 733 734 735 736 737 738 739

		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;
			}
		}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
		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
758
static void
759
_update_statusbar_list_info (GthBrowser *browser)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
760
{
761 762 763 764 765 766 767 768 769 770 771 772
	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
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

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

	/**/

805 806
	size_total_formatted = g_format_size (size_total);
	size_selected_formatted = g_format_size (size_selected);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
807
	text_total = g_strdup_printf (g_dngettext (NULL, "%d file (%s)", "%d files (%s)", n_total), n_total, size_total_formatted);
808
	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
809

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

	g_string_free (text, TRUE);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
822 823 824 825 826 827 828
	g_free (text_selected);
	g_free (text_total);
	g_free (size_selected_formatted);
	g_free (size_total_formatted);
}


829 830 831 832 833 834 835 836 837 838 839 840 841
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;

842
		free_space_formatted = g_format_size (free_space);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
		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);
}


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


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


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

	load_data = g_new0 (LoadData, 1);
	load_data->browser = browser;
918 919
	load_data->requested_folder = gth_file_data_new (location, NULL);
	load_data->requested_folder_parent = g_file_get_parent (load_data->requested_folder->file);
920 921
	if (file_to_select != NULL)
		load_data->file_to_select = g_file_dup (file_to_select);
922
	else if (browser->priv->current_file != NULL)
923
		load_data->file_to_select = g_file_dup (browser->priv->current_file->file);
924 925
	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
926
	load_data->action = action;
927
	load_data->automatic = automatic;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
928 929 930 931 932 933
	load_data->cancellable = g_cancellable_new ();

	if (entry_point == NULL)
		return load_data;

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

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

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


973 974 975 976 977 978 979 980
static void
_gth_browser_load (GthBrowser *browser,
		   GFile      *location,
		   GFile      *file_to_select,
		   GList      *selected,
		   double      vscroll,
		   GthAction   action,
		   gboolean    automatic);
981 982


983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
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;
}


999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
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);
1010
	gth_info_bar_set_icon_name (GTH_INFO_BAR (browser->priv->infobar), "dialog-error-symbolic", GTK_ICON_SIZE_DIALOG);
1011 1012 1013
	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),
1014
				  _GTK_LABEL_CLOSE, GTK_RESPONSE_CLOSE,
1015 1016 1017 1018 1019
				  NULL);
	gtk_widget_show (browser->priv->infobar);
}


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

	{
		char *uri;

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

		g_free (uri);
	}

1037 1038 1039
	/* moving the "gth-browser-load-location-after" after the
	 * LOCATION_READY signal emition can brake the extensions */

1040 1041 1042 1043 1044 1045
	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))
	{
1046 1047 1048 1049
		if (error == NULL) {
			_g_object_unref (browser->priv->location_source);
			browser->priv->location_source = g_object_ref (load_data->file_source);
		}
1050 1051
		gth_browser_update_extra_widget (browser);
		gth_hook_invoke ("gth-browser-load-location-after", browser, browser->priv->location, error);
1052
	}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1053

1054 1055 1056 1057 1058 1059 1060
	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
1061 1062 1063 1064 1065 1066 1067 1068
	if (error == NULL)
		return;

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

1069 1070 1071
	if (load_data->automatic) {
		GFile *parent;

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

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

	g_free (title);
1091
	g_error_free (error);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1092 1093 1094
}


1095 1096 1097 1098