gedit-tab.c 71.3 KB
Newer Older
Paolo Borelli's avatar
Paolo Borelli 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
/*
 * gedit-tab.c
 * This file is part of gedit
 *
 * Copyright (C) 2005 - Paolo Maggi 
 *
 * 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 Place, Suite 330, 
 * Boston, MA 02111-1307, USA.
 */
 
/*
 * Modified by the gedit Team, 2005. See the AUTHORS file for a 
 * list of people on the gedit Team.  
 * See the ChangeLog files for a list of changes. 
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>
34
#include <gio/gio.h>
Paolo Borelli's avatar
Paolo Borelli committed
35 36 37 38 39

#include "gedit-app.h"
#include "gedit-notebook.h"
#include "gedit-tab.h"
#include "gedit-utils.h"
40
#include "gedit-io-error-info-bar.h"
41 42
#include "gedit-print-job.h"
#include "gedit-print-preview.h"
43
#include "gedit-progress-info-bar.h"
Paolo Borelli's avatar
Paolo Borelli committed
44
#include "gedit-debug.h"
45
#include "gedit-enum-types.h"
46
#include "gedit-settings.h"
47
#include "gedit-view-frame.h"
Paolo Borelli's avatar
Paolo Borelli committed
48 49 50 51 52 53 54

#define GEDIT_TAB_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_TAB, GeditTabPrivate))

#define GEDIT_TAB_KEY "GEDIT_TAB_KEY"

struct _GeditTabPrivate
{
55
	GSettings	       *editor;
56
	GeditTabState	        state;
57 58

	GeditViewFrame         *frame;
Paolo Borelli's avatar
Paolo Borelli committed
59

60
	GtkWidget	       *info_bar;
61
	GtkWidget	       *print_preview;
Paolo Borelli's avatar
Paolo Borelli committed
62

63
	GeditPrintJob          *print_job;
Paolo Borelli's avatar
Paolo Borelli committed
64 65

	/* tmp data for saving */
66
	GFile		       *tmp_save_location;
Paolo Borelli's avatar
Paolo Borelli committed
67 68

	/* tmp data for loading */
69
	gint                    tmp_line_pos;
70
	gint                    tmp_column_pos;
71
	const GeditEncoding    *tmp_encoding;
Paolo Borelli's avatar
Paolo Borelli committed
72
	
73 74
	GTimer 		       *timer;
	guint		        times_called;
Paolo Borelli's avatar
Paolo Borelli committed
75 76

	GeditDocumentSaveFlags	save_flags;
77

78 79 80
	gint                    auto_save_interval;
	guint                   auto_save_timeout;

81 82
	gint	                not_editable : 1;
	gint                    auto_save : 1;
83 84

	gint                    ask_if_externally_modified : 1;
Paolo Borelli's avatar
Paolo Borelli committed
85 86 87 88 89 90 91 92
};

G_DEFINE_TYPE(GeditTab, gedit_tab, GTK_TYPE_VBOX)

enum
{
	PROP_0,
	PROP_NAME,
93 94 95
	PROP_STATE,
	PROP_AUTO_SAVE,
	PROP_AUTO_SAVE_INTERVAL
Paolo Borelli's avatar
Paolo Borelli committed
96 97
};

98 99 100 101 102 103 104 105 106
/* Signals */
enum
{
	DROP_URIS,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

107 108
static gboolean gedit_tab_auto_save (GeditTab *tab);

109 110 111 112 113
static void done_printing_cb        (GeditPrintJob       *job,
                                     GeditPrintJobResult  result,
                                     const GError        *error,
                                     GeditTab            *tab);

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static void
install_auto_save_timeout (GeditTab *tab)
{
	gint timeout;

	gedit_debug (DEBUG_TAB);

	g_return_if_fail (tab->priv->auto_save_timeout <= 0);
	g_return_if_fail (tab->priv->auto_save);
	g_return_if_fail (tab->priv->auto_save_interval > 0);
	
	g_return_if_fail (tab->priv->state != GEDIT_TAB_STATE_LOADING);
	g_return_if_fail (tab->priv->state != GEDIT_TAB_STATE_SAVING);
	g_return_if_fail (tab->priv->state != GEDIT_TAB_STATE_REVERTING);
	g_return_if_fail (tab->priv->state != GEDIT_TAB_STATE_LOADING_ERROR);
	g_return_if_fail (tab->priv->state != GEDIT_TAB_STATE_SAVING_ERROR);
	g_return_if_fail (tab->priv->state != GEDIT_TAB_STATE_SAVING_ERROR);
	g_return_if_fail (tab->priv->state != GEDIT_TAB_STATE_REVERTING_ERROR);
132

133
	/* Add a new timeout */
134 135 136
	timeout = g_timeout_add_seconds (tab->priv->auto_save_interval * 60,
					 (GSourceFunc) gedit_tab_auto_save,
					 tab);
137 138 139 140 141 142 143 144

	tab->priv->auto_save_timeout = timeout;
}

static gboolean
install_auto_save_timeout_if_needed (GeditTab *tab)
{
	GeditDocument *doc;
145

146 147 148 149
	gedit_debug (DEBUG_TAB);
	
	g_return_val_if_fail (tab->priv->auto_save_timeout <= 0, FALSE);
	g_return_val_if_fail ((tab->priv->state == GEDIT_TAB_STATE_NORMAL) ||
150 151 152 153 154 155
			      (tab->priv->state == GEDIT_TAB_STATE_SHOWING_PRINT_PREVIEW) ||
			      (tab->priv->state == GEDIT_TAB_STATE_CLOSING), FALSE);

	if (tab->priv->state == GEDIT_TAB_STATE_CLOSING)
		return FALSE;

156
	doc = gedit_tab_get_document (tab);
157

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
 	if (tab->priv->auto_save && 
 	    !gedit_document_is_untitled (doc) &&
 	    !gedit_document_get_readonly (doc))
 	{ 
 		install_auto_save_timeout (tab);
 		
 		return TRUE;
 	}
 	
 	return FALSE;
}

static void
remove_auto_save_timeout (GeditTab *tab)
{
	gedit_debug (DEBUG_TAB);

	/* FIXME: check sugli stati */
	
	g_return_if_fail (tab->priv->auto_save_timeout > 0);
	
	g_source_remove (tab->priv->auto_save_timeout);
	tab->priv->auto_save_timeout = 0;
}

Paolo Borelli's avatar
Paolo Borelli committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
static void
gedit_tab_get_property (GObject    *object,
		        guint       prop_id,
		        GValue     *value,
		        GParamSpec *pspec)
{
	GeditTab *tab = GEDIT_TAB (object);

	switch (prop_id)
	{
		case PROP_NAME:
			g_value_take_string (value,
					     _gedit_tab_get_name (tab));
			break;
		case PROP_STATE:
198 199
			g_value_set_enum (value,
					  gedit_tab_get_state (tab));
Paolo Borelli's avatar
Paolo Borelli committed
200
			break;			
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
		case PROP_AUTO_SAVE:
			g_value_set_boolean (value,
					     gedit_tab_get_auto_save_enabled (tab));
			break;
		case PROP_AUTO_SAVE_INTERVAL:
			g_value_set_int (value,
					 gedit_tab_get_auto_save_interval (tab));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;			
	}
}

static void
gedit_tab_set_property (GObject      *object,
		        guint         prop_id,
		        const GValue *value,
		        GParamSpec   *pspec)
{
	GeditTab *tab = GEDIT_TAB (object);

	switch (prop_id)
	{
		case PROP_AUTO_SAVE:
			gedit_tab_set_auto_save_enabled (tab,
							 g_value_get_boolean (value));
			break;
		case PROP_AUTO_SAVE_INTERVAL:
			gedit_tab_set_auto_save_interval (tab,
							  g_value_get_int (value));
			break;
Paolo Borelli's avatar
Paolo Borelli committed
233 234 235 236 237 238
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;			
	}
}

239 240 241 242 243
static void
gedit_tab_dispose (GObject *object)
{
	GeditTab *tab = GEDIT_TAB (object);

244 245 246 247 248 249 250 251 252
	if (tab->priv->print_job != NULL)
	{
		g_signal_handlers_disconnect_by_func (tab->priv->print_job,
		                                      done_printing_cb, tab);
		g_object_unref (tab->priv->print_job);
		tab->priv->print_job = NULL;
		tab->priv->print_preview = NULL;
	}

253 254 255 256 257 258
	if (tab->priv->tmp_save_location != NULL)
	{
		g_object_unref (tab->priv->tmp_save_location);
		tab->priv->tmp_save_location = NULL;
	}

259 260 261 262 263 264
	if (tab->priv->editor != NULL)
	{
		g_object_unref (tab->priv->editor);
		tab->priv->editor = NULL;
	}

265 266 267
	G_OBJECT_CLASS (gedit_tab_parent_class)->dispose (object);
}

Paolo Borelli's avatar
Paolo Borelli committed
268 269 270 271
static void
gedit_tab_finalize (GObject *object)
{
	GeditTab *tab = GEDIT_TAB (object);
272

Paolo Borelli's avatar
Paolo Borelli committed
273 274 275
	if (tab->priv->timer != NULL)
		g_timer_destroy (tab->priv->timer);

276 277 278
	if (tab->priv->auto_save_timeout > 0)
		remove_auto_save_timeout (tab);

Paolo Borelli's avatar
Paolo Borelli committed
279 280 281
	G_OBJECT_CLASS (gedit_tab_parent_class)->finalize (object);
}

282 283 284 285 286 287 288
static void
gedit_tab_grab_focus (GtkWidget *widget)
{
	GeditTab *tab = GEDIT_TAB (widget);
	
	GTK_WIDGET_CLASS (gedit_tab_parent_class)->grab_focus (widget);
	
289
	if (tab->priv->info_bar != NULL)
290
	{
291
		gtk_widget_grab_focus (tab->priv->info_bar);
292 293 294 295 296 297 298
	}
	else
	{
		gtk_widget_grab_focus (GTK_WIDGET (gedit_tab_get_view (tab)));
	}
}

Paolo Borelli's avatar
Paolo Borelli committed
299 300 301 302
static void 
gedit_tab_class_init (GeditTabClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
303
	GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
Paolo Borelli's avatar
Paolo Borelli committed
304

305
	object_class->dispose = gedit_tab_dispose;
Paolo Borelli's avatar
Paolo Borelli committed
306 307
	object_class->finalize = gedit_tab_finalize;
	object_class->get_property = gedit_tab_get_property;
308
	object_class->set_property = gedit_tab_set_property;
Paolo Borelli's avatar
Paolo Borelli committed
309
	
310 311
	gtkwidget_class->grab_focus = gedit_tab_grab_focus;
	
Paolo Borelli's avatar
Paolo Borelli committed
312 313 314 315 316 317
	g_object_class_install_property (object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
							      "Name",
							      "The tab's name",
							      NULL,
318 319 320
							      G_PARAM_READABLE |
							      G_PARAM_STATIC_STRINGS));

Paolo Borelli's avatar
Paolo Borelli committed
321 322
	g_object_class_install_property (object_class,
					 PROP_STATE,
323 324 325 326 327
					 g_param_spec_enum ("state",
							    "State",
							    "The tab's state",
							    GEDIT_TYPE_TAB_STATE,
							    GEDIT_TAB_STATE_NORMAL,
328 329 330
							    G_PARAM_READABLE |
							    G_PARAM_STATIC_STRINGS));

331 332 333 334 335 336
	g_object_class_install_property (object_class,
					 PROP_AUTO_SAVE,
					 g_param_spec_boolean ("autosave",
							       "Autosave",
							       "Autosave feature",
							       TRUE,
337 338
							       G_PARAM_READWRITE |
							       G_PARAM_STATIC_STRINGS));
339 340 341

	g_object_class_install_property (object_class,
					 PROP_AUTO_SAVE_INTERVAL,
342
					 g_param_spec_int ("autosave-interval",
343 344 345 346 347
							   "AutosaveInterval",
							   "Time between two autosaves",
							   0,
							   G_MAXINT,
							   0,
348 349 350
							   G_PARAM_READWRITE |
							   G_PARAM_STATIC_STRINGS));

351 352 353 354 355 356 357 358 359 360 361
	signals[DROP_URIS] =
		g_signal_new ("drop-uris",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
			      G_STRUCT_OFFSET (GeditTabClass, drop_uris),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__BOXED,
			      G_TYPE_NONE,
			      1,
			      G_TYPE_STRV);

Paolo Borelli's avatar
Paolo Borelli committed
362 363 364
	g_type_class_add_private (object_class, sizeof (GeditTabPrivate));
}

365 366 367 368 369 370 371 372
/**
 * gedit_tab_get_state:
 * @tab: a #GeditTab
 *
 * Gets the #GeditTabState of @tab.
 *
 * Returns: the #GeditTabState of @tab
 */
Paolo Borelli's avatar
Paolo Borelli committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
GeditTabState
gedit_tab_get_state (GeditTab *tab)
{
	g_return_val_if_fail (GEDIT_IS_TAB (tab), GEDIT_TAB_STATE_NORMAL);
	
	return tab->priv->state;
}

static void
set_cursor_according_to_state (GtkTextView   *view,
			       GeditTabState  state)
{
	GdkCursor *cursor;
	GdkWindow *text_window;
	GdkWindow *left_window;

	text_window = gtk_text_view_get_window (view, GTK_TEXT_WINDOW_TEXT);
	left_window = gtk_text_view_get_window (view, GTK_TEXT_WINDOW_LEFT);

	if ((state == GEDIT_TAB_STATE_LOADING)          ||
	    (state == GEDIT_TAB_STATE_REVERTING)        ||
	    (state == GEDIT_TAB_STATE_SAVING)           ||
	    (state == GEDIT_TAB_STATE_PRINTING)         ||
	    (state == GEDIT_TAB_STATE_PRINT_PREVIEWING) ||
	    (state == GEDIT_TAB_STATE_CLOSING))
	{
		cursor = gdk_cursor_new_for_display (
				gtk_widget_get_display (GTK_WIDGET (view)),
				GDK_WATCH);

		if (text_window != NULL)
			gdk_window_set_cursor (text_window, cursor);
		if (left_window != NULL)
			gdk_window_set_cursor (left_window, cursor);

408
		g_object_unref (cursor);
Paolo Borelli's avatar
Paolo Borelli committed
409 410 411 412 413 414 415 416 417 418 419 420
	}
	else
	{
		cursor = gdk_cursor_new_for_display (
				gtk_widget_get_display (GTK_WIDGET (view)),
				GDK_XTERM);

		if (text_window != NULL)
			gdk_window_set_cursor (text_window, cursor);
		if (left_window != NULL)
			gdk_window_set_cursor (left_window, NULL);

421
		g_object_unref (cursor);
Paolo Borelli's avatar
Paolo Borelli committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435
	}
}

static void
view_realized (GtkTextView *view,
	       GeditTab    *tab)
{
	set_cursor_according_to_state (view, tab->priv->state);
}

static void
set_view_properties_according_to_state (GeditTab      *tab,
					GeditTabState  state)
{
436
	GeditView *view;
Paolo Borelli's avatar
Paolo Borelli committed
437
	gboolean val;
438 439 440 441
	gboolean hl_current_line;
	
	hl_current_line = g_settings_get_boolean (tab->priv->editor,
						  GEDIT_SETTINGS_HIGHLIGHT_CURRENT_LINE);
Paolo Borelli's avatar
Paolo Borelli committed
442

443 444
	view = gedit_view_frame_get_view (tab->priv->frame);

Paolo Borelli's avatar
Paolo Borelli committed
445 446 447
	val = ((state == GEDIT_TAB_STATE_NORMAL) &&
	       (tab->priv->print_preview == NULL) &&
	       !tab->priv->not_editable);
448
	gtk_text_view_set_editable (GTK_TEXT_VIEW (view), val);
Paolo Borelli's avatar
Paolo Borelli committed
449 450 451

	val = ((state != GEDIT_TAB_STATE_LOADING) &&
	       (state != GEDIT_TAB_STATE_CLOSING));
452
	gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), val);
Paolo Borelli's avatar
Paolo Borelli committed
453 454 455

	val = ((state != GEDIT_TAB_STATE_LOADING) &&
	       (state != GEDIT_TAB_STATE_CLOSING) &&
456
	       (hl_current_line));
457
	gtk_source_view_set_highlight_current_line (GTK_SOURCE_VIEW (view), val);
Paolo Borelli's avatar
Paolo Borelli committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
}

static void
gedit_tab_set_state (GeditTab      *tab,
		     GeditTabState  state)
{
	g_return_if_fail (GEDIT_IS_TAB (tab));
	g_return_if_fail ((state >= 0) && (state < GEDIT_TAB_NUM_OF_STATES));

	if (tab->priv->state == state)
		return;

	tab->priv->state = state;

	set_view_properties_according_to_state (tab, state);

474
	if ((state == GEDIT_TAB_STATE_LOADING_ERROR) || /* FIXME: add other states if needed */
Paolo Borelli's avatar
Paolo Borelli committed
475 476
	    (state == GEDIT_TAB_STATE_SHOWING_PRINT_PREVIEW))
	{
477
		gtk_widget_hide (GTK_WIDGET (tab->priv->frame));
Paolo Borelli's avatar
Paolo Borelli committed
478 479 480 481
	}
	else
	{
		if (tab->priv->print_preview == NULL)
482
			gtk_widget_show (GTK_WIDGET (tab->priv->frame));
Paolo Borelli's avatar
Paolo Borelli committed
483 484
	}

485
	set_cursor_according_to_state (GTK_TEXT_VIEW (gedit_view_frame_get_view (tab->priv->frame)),
Paolo Borelli's avatar
Paolo Borelli committed
486 487
				       state);

488
	g_object_notify (G_OBJECT (tab), "state");
Paolo Borelli's avatar
Paolo Borelli committed
489 490 491
}

static void 
492 493 494
document_location_notify_handler (GeditDocument *document,
				  GParamSpec    *pspec,
				  GeditTab      *tab)
Paolo Borelli's avatar
Paolo Borelli committed
495
{
496 497
	gedit_debug (DEBUG_TAB);
	
498
	/* Notify the change in the location */
Paolo Borelli's avatar
Paolo Borelli committed
499 500 501
	g_object_notify (G_OBJECT (tab), "name");
}

502 503 504 505 506 507 508 509 510 511 512
static void 
document_shortname_notify_handler (GeditDocument *document,
				   GParamSpec    *pspec,
				   GeditTab      *tab)
{
	gedit_debug (DEBUG_TAB);
	
	/* Notify the change in the shortname */
	g_object_notify (G_OBJECT (tab), "name");
}

Paolo Borelli's avatar
Paolo Borelli committed
513 514 515 516 517 518 519 520
static void
document_modified_changed (GtkTextBuffer *document,
			   GeditTab      *tab)
{
	g_object_notify (G_OBJECT (tab), "name");
}

static void
521 522
set_info_bar (GeditTab  *tab,
	      GtkWidget *info_bar)
Paolo Borelli's avatar
Paolo Borelli committed
523
{
524
	if (tab->priv->info_bar == info_bar)
Paolo Borelli's avatar
Paolo Borelli committed
525 526
		return;

527 528
	if (tab->priv->info_bar != NULL)
		gtk_widget_destroy (tab->priv->info_bar);
Paolo Borelli's avatar
Paolo Borelli committed
529

530
	tab->priv->info_bar = info_bar;
Paolo Borelli's avatar
Paolo Borelli committed
531

532
	if (info_bar == NULL)
Paolo Borelli's avatar
Paolo Borelli committed
533 534 535
		return;

	gtk_box_pack_start (GTK_BOX (tab),
536
			    tab->priv->info_bar,
Paolo Borelli's avatar
Paolo Borelli committed
537 538 539 540
			    FALSE,
			    FALSE,
			    0);		

541 542
	g_object_add_weak_pointer (G_OBJECT (tab->priv->info_bar), 
				   (gpointer *)&tab->priv->info_bar);
Paolo Borelli's avatar
Paolo Borelli committed
543 544 545 546 547 548 549 550 551 552 553 554 555
}

static void
remove_tab (GeditTab *tab)
{
	GeditNotebook *notebook;

	notebook = GEDIT_NOTEBOOK (gtk_widget_get_parent (GTK_WIDGET (tab)));

	gedit_notebook_remove_tab (notebook, tab);
}

static void 
556 557 558
io_loading_error_info_bar_response (GtkWidget *info_bar,
				    gint       response_id,
				    GeditTab  *tab)
Paolo Borelli's avatar
Paolo Borelli committed
559 560
{
	GeditDocument *doc;
561
	GeditView *view;
562
	GFile *location;
563
	const GeditEncoding *encoding;
Paolo Borelli's avatar
Paolo Borelli committed
564 565 566 567

	doc = gedit_tab_get_document (tab);
	g_return_if_fail (GEDIT_IS_DOCUMENT (doc));

568 569 570
	view = gedit_tab_get_view (tab);
	g_return_if_fail (GEDIT_IS_VIEW (view));

571
	location = gedit_document_get_location (doc);
Paolo Borelli's avatar
Paolo Borelli committed
572

573
	switch (response_id)
Paolo Borelli's avatar
Paolo Borelli committed
574
	{
575
		case GTK_RESPONSE_OK:
576 577
			g_return_if_fail (location != NULL);

578 579
			encoding = gedit_conversion_error_info_bar_get_encoding (
					GTK_WIDGET (info_bar));
Paolo Borelli's avatar
Paolo Borelli committed
580

581 582 583 584
			if (encoding != NULL)
			{
				tab->priv->tmp_encoding = encoding;
			}
585

586
			set_info_bar (tab, NULL);
587
			gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING);
588

589
			g_return_if_fail (tab->priv->auto_save_timeout <= 0);
590

591
			gedit_document_load (doc,
592
					     location,
593
					     tab->priv->tmp_encoding,
594
					     tab->priv->tmp_line_pos,
595
					     tab->priv->tmp_column_pos,
596 597 598 599
					     FALSE);
			break;
		case GTK_RESPONSE_YES:
			/* This means that we want to edit the document anyway */
600 601 602
			tab->priv->not_editable = FALSE;
			gtk_text_view_set_editable (GTK_TEXT_VIEW (view),
			                            TRUE);
603
			set_info_bar (tab, NULL);
604 605
			break;
		default:
606 607 608 609
			if (location != NULL)
			{
				_gedit_recent_remove (GEDIT_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))), location);
			}
Paolo Borelli's avatar
Paolo Borelli committed
610

611 612
			remove_tab (tab);
			break;
Paolo Borelli's avatar
Paolo Borelli committed
613
	}
Paolo Borelli's avatar
Paolo Borelli committed
614

615 616 617 618
	if (location != NULL)
	{
		g_object_unref (location);
	}
Paolo Borelli's avatar
Paolo Borelli committed
619 620 621
}

static void 
622 623 624
file_already_open_warning_info_bar_response (GtkWidget   *info_bar,
					     gint         response_id,
					     GeditTab    *tab)
Paolo Borelli's avatar
Paolo Borelli committed
625 626 627 628 629 630 631 632 633 634 635 636 637
{
	GeditView *view;
	
	view = gedit_tab_get_view (tab);
	
	if (response_id == GTK_RESPONSE_YES)
	{
		tab->priv->not_editable = FALSE;
		
		gtk_text_view_set_editable (GTK_TEXT_VIEW (view),
					    TRUE);
	}

638
	gtk_widget_destroy (info_bar);
Paolo Borelli's avatar
Paolo Borelli committed
639

640
	gtk_widget_grab_focus (GTK_WIDGET (view));
Paolo Borelli's avatar
Paolo Borelli committed
641 642 643
}

static void
644
load_cancelled (GtkWidget        *bar,
Paolo Borelli's avatar
Paolo Borelli committed
645 646 647
                gint              response_id,
                GeditTab         *tab)
{
648
	g_return_if_fail (GEDIT_IS_PROGRESS_INFO_BAR (tab->priv->info_bar));
Paolo Borelli's avatar
Paolo Borelli committed
649 650 651 652 653 654 655
	
	g_object_ref (tab);
	gedit_document_load_cancel (gedit_tab_get_document (tab));
	g_object_unref (tab);	
}

static void 
656 657 658
unrecoverable_reverting_error_info_bar_response (GtkWidget        *info_bar,
						 gint              response_id,
						 GeditTab         *tab)
Paolo Borelli's avatar
Paolo Borelli committed
659 660 661 662 663 664
{
	GeditView *view;
	
	gedit_tab_set_state (tab,
			     GEDIT_TAB_STATE_NORMAL);

665
	set_info_bar (tab, NULL);
Paolo Borelli's avatar
Paolo Borelli committed
666 667 668

	view = gedit_tab_get_view (tab);

669 670 671
	gtk_widget_grab_focus (GTK_WIDGET (view));
	
	install_auto_save_timeout_if_needed (tab);	
Paolo Borelli's avatar
Paolo Borelli committed
672 673 674 675 676
}

#define MAX_MSG_LENGTH 100

static void
677
show_loading_info_bar (GeditTab *tab)
Paolo Borelli's avatar
Paolo Borelli committed
678
{
679
	GtkWidget *bar;
Paolo Borelli's avatar
Paolo Borelli committed
680 681 682 683
	GeditDocument *doc = NULL;
	gchar *name;
	gchar *dirname = NULL;
	gchar *msg = NULL;
684 685
	gchar *name_markup;
	gchar *dirname_markup;
Paolo Borelli's avatar
Paolo Borelli committed
686
	gint len;
687

688
	if (tab->priv->info_bar != NULL)
Paolo Borelli's avatar
Paolo Borelli committed
689
		return;
690

691
	gedit_debug (DEBUG_TAB);
Paolo Borelli's avatar
Paolo Borelli committed
692 693 694 695
		
	doc = gedit_tab_get_document (tab);
	g_return_if_fail (doc != NULL);

696 697
	name = gedit_document_get_short_name_for_display (doc);
	len = g_utf8_strlen (name, -1);
Paolo Borelli's avatar
Paolo Borelli committed
698 699 700 701 702 703

	/* if the name is awfully long, truncate it and be done with it,
	 * otherwise also show the directory (ellipsized if needed)
	 */
	if (len > MAX_MSG_LENGTH)
	{
704 705 706 707 708
		gchar *str;

		str = gedit_utils_str_middle_truncate (name, MAX_MSG_LENGTH);
		g_free (name);
		name = str;
Paolo Borelli's avatar
Paolo Borelli committed
709 710 711
	}
	else
	{
712
		GFile *location;
Paolo Borelli's avatar
Paolo Borelli committed
713

714 715
		location = gedit_document_get_location (doc);
		if (location != NULL)
Paolo Borelli's avatar
Paolo Borelli committed
716
		{
717 718
			gchar *str;

719 720
			str = gedit_utils_location_get_dirname_for_display (location);
			g_object_unref (location);
721

Paolo Borelli's avatar
Paolo Borelli committed
722 723 724 725 726 727 728 729 730 731 732 733
			/* use the remaining space for the dir, but use a min of 20 chars
			 * so that we do not end up with a dirname like "(a...b)".
			 * This means that in the worst case when the filename is long 99
			 * we have a title long 99 + 20, but I think it's a rare enough
			 * case to be acceptable. It's justa darn title afterall :)
			 */
			dirname = gedit_utils_str_middle_truncate (str, 
								   MAX (20, MAX_MSG_LENGTH - len));
			g_free (str);
		}
	}

734
	name_markup = g_markup_printf_escaped ("<b>%s</b>", name);
735

Paolo Borelli's avatar
Paolo Borelli committed
736 737 738 739
	if (tab->priv->state == GEDIT_TAB_STATE_REVERTING)
	{
		if (dirname != NULL)
		{
740
			dirname_markup = g_markup_printf_escaped ("<b>%s</b>", dirname);
741

Paolo Borelli's avatar
Paolo Borelli committed
742 743
			/* Translators: the first %s is a file name (e.g. test.txt) the second one
			   is a directory (e.g. ssh://master.gnome.org/home/users/paolo) */
744 745 746
			msg = g_strdup_printf (_("Reverting %s from %s"),
					       name_markup,
					       dirname_markup);
747
			g_free (dirname_markup);
Paolo Borelli's avatar
Paolo Borelli committed
748 749 750
		}
		else
		{
751 752
			msg = g_strdup_printf (_("Reverting %s"), 
					       name_markup);
Paolo Borelli's avatar
Paolo Borelli committed
753 754
		}
		
755 756 757
		bar = gedit_progress_info_bar_new (GTK_STOCK_REVERT_TO_SAVED,
						   msg,
						   TRUE);
Paolo Borelli's avatar
Paolo Borelli committed
758 759 760 761 762
	}
	else
	{
		if (dirname != NULL)
		{
763
			dirname_markup = g_markup_printf_escaped ("<b>%s</b>", dirname);
764

Paolo Borelli's avatar
Paolo Borelli committed
765 766
			/* Translators: the first %s is a file name (e.g. test.txt) the second one
			   is a directory (e.g. ssh://master.gnome.org/home/users/paolo) */
767 768 769
			msg = g_strdup_printf (_("Loading %s from %s"),
					       name_markup,
					       dirname_markup);
770
			g_free (dirname_markup);
Paolo Borelli's avatar
Paolo Borelli committed
771 772 773
		}
		else
		{
774 775
			msg = g_strdup_printf (_("Loading %s"), 
					       name_markup);
Paolo Borelli's avatar
Paolo Borelli committed
776 777
		}

778 779 780
		bar = gedit_progress_info_bar_new (GTK_STOCK_OPEN,
						   msg,
						   TRUE);
Paolo Borelli's avatar
Paolo Borelli committed
781 782
	}

783
	g_signal_connect (bar,
Paolo Borelli's avatar
Paolo Borelli committed
784 785 786 787
			  "response",
			  G_CALLBACK (load_cancelled),
			  tab);
						 
788
	gtk_widget_show (bar);
Paolo Borelli's avatar
Paolo Borelli committed
789

790
	set_info_bar (tab, bar);
Paolo Borelli's avatar
Paolo Borelli committed
791 792 793

	g_free (msg);
	g_free (name);
794
	g_free (name_markup);
Paolo Borelli's avatar
Paolo Borelli committed
795 796 797 798
	g_free (dirname);
}

static void
799
show_saving_info_bar (GeditTab *tab)
Paolo Borelli's avatar
Paolo Borelli committed
800
{
801
	GtkWidget *bar;
Paolo Borelli's avatar
Paolo Borelli committed
802 803 804 805
	GeditDocument *doc = NULL;
	gchar *short_name;
	gchar *from;
	gchar *to = NULL;
806 807
	gchar *from_markup;
	gchar *to_markup;
Paolo Borelli's avatar
Paolo Borelli committed
808 809 810
	gchar *msg = NULL;
	gint len;

811
	g_return_if_fail (tab->priv->tmp_save_location != NULL);
Paolo Borelli's avatar
Paolo Borelli committed
812
	
813
	if (tab->priv->info_bar != NULL)
Paolo Borelli's avatar
Paolo Borelli committed
814 815
		return;
	
816
	gedit_debug (DEBUG_TAB);
Paolo Borelli's avatar
Paolo Borelli committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
		
	doc = gedit_tab_get_document (tab);
	g_return_if_fail (doc != NULL);

	short_name = gedit_document_get_short_name_for_display (doc);

	len = g_utf8_strlen (short_name, -1);

	/* if the name is awfully long, truncate it and be done with it,
	 * otherwise also show the directory (ellipsized if needed)
	 */
	if (len > MAX_MSG_LENGTH)
	{
		from = gedit_utils_str_middle_truncate (short_name, 
							MAX_MSG_LENGTH);
		g_free (short_name);
	}
	else
	{
		gchar *str;

		from = short_name;
839
		to = g_file_get_parse_name (tab->priv->tmp_save_location);
Paolo Borelli's avatar
Paolo Borelli committed
840 841 842 843 844 845 846
		str = gedit_utils_str_middle_truncate (to, 
						       MAX (20, MAX_MSG_LENGTH - len));
		g_free (to);
			
		to = str;
	}

847
	from_markup = g_markup_printf_escaped ("<b>%s</b>", from);
848

Paolo Borelli's avatar
Paolo Borelli committed
849 850
	if (to != NULL)
	{
851
		to_markup = g_markup_printf_escaped ("<b>%s</b>", to);
852

Paolo Borelli's avatar
Paolo Borelli committed
853 854
		/* Translators: the first %s is a file name (e.g. test.txt) the second one
		   is a directory (e.g. ssh://master.gnome.org/home/users/paolo) */
855 856 857
		msg = g_strdup_printf (_("Saving %s to %s"),
				       from_markup,
				       to_markup);
858
		g_free (to_markup);
Paolo Borelli's avatar
Paolo Borelli committed
859 860 861
	}
	else
	{
862
		msg = g_strdup_printf (_("Saving %s"), from_markup);
Paolo Borelli's avatar
Paolo Borelli committed
863 864
	}

865 866 867
	bar = gedit_progress_info_bar_new (GTK_STOCK_SAVE,
					   msg,
					   FALSE);
Paolo Borelli's avatar
Paolo Borelli committed
868

869
	gtk_widget_show (bar);
Paolo Borelli's avatar
Paolo Borelli committed
870

871
	set_info_bar (tab, bar);
Paolo Borelli's avatar
Paolo Borelli committed
872 873 874 875

	g_free (msg);
	g_free (to);
	g_free (from);
876
	g_free (from_markup);
Paolo Borelli's avatar
Paolo Borelli committed
877 878 879
}

static void
880
info_bar_set_progress (GeditTab *tab,
881 882
			   goffset   size,
			   goffset   total_size)
Paolo Borelli's avatar
Paolo Borelli committed
883
{
884
	if (tab->priv->info_bar == NULL)
Paolo Borelli's avatar
Paolo Borelli committed
885
		return;
886 887 888

	gedit_debug_message (DEBUG_TAB, "%" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, size, total_size);

889
	g_return_if_fail (GEDIT_IS_PROGRESS_INFO_BAR (tab->priv->info_bar));
Paolo Borelli's avatar
Paolo Borelli committed
890 891 892 893
	
	if (total_size == 0)
	{
		if (size != 0)
894 895
			gedit_progress_info_bar_pulse (
					GEDIT_PROGRESS_INFO_BAR (tab->priv->info_bar));	
Paolo Borelli's avatar
Paolo Borelli committed
896
		else
897 898
			gedit_progress_info_bar_set_fraction (
				GEDIT_PROGRESS_INFO_BAR (tab->priv->info_bar),
Paolo Borelli's avatar
Paolo Borelli committed
899 900 901 902 903 904 905 906
				0);
	}
	else
	{
		gdouble frac;

		frac = (gdouble)size / (gdouble)total_size;

907 908
		gedit_progress_info_bar_set_fraction (
				GEDIT_PROGRESS_INFO_BAR (tab->priv->info_bar),
Paolo Borelli's avatar
Paolo Borelli committed
909 910 911 912 913
				frac);		
	}
}

static void
914 915 916 917
document_loading (GeditDocument *document,
		  goffset        size,
		  goffset        total_size,
		  GeditTab      *tab)
Paolo Borelli's avatar
Paolo Borelli committed
918
{
919 920
	gdouble et;
	gdouble total_time;
Paolo Borelli's avatar
Paolo Borelli committed
921 922 923 924

	g_return_if_fail ((tab->priv->state == GEDIT_TAB_STATE_LOADING) ||
		 	  (tab->priv->state == GEDIT_TAB_STATE_REVERTING));

925 926
	gedit_debug_message (DEBUG_TAB, "%" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, size, total_size);

Paolo Borelli's avatar
Paolo Borelli committed
927 928 929 930 931 932 933 934
	if (tab->priv->timer == NULL)
	{
		g_return_if_fail (tab->priv->times_called == 0);
		tab->priv->timer = g_timer_new ();
	}

	et = g_timer_elapsed (tab->priv->timer, NULL);

935 936 937 938
	/* et : total_time = size : total_size */
	total_time = (et * total_size) / size;

	if ((total_time - et) > 3.0)
Paolo Borelli's avatar
Paolo Borelli committed
939
	{
940
		show_loading_info_bar (tab);
Paolo Borelli's avatar
Paolo Borelli committed
941 942
	}

943
	info_bar_set_progress (tab, size, total_size);
Paolo Borelli's avatar
Paolo Borelli committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
}

static gboolean
remove_tab_idle (GeditTab *tab)
{
	remove_tab (tab);

	return FALSE;
}

static void
document_loaded (GeditDocument *document,
		 const GError  *error,
		 GeditTab      *tab)
{
	GtkWidget *emsg;
960
	GFile *location;
Paolo Borelli's avatar
Paolo Borelli committed
961 962 963

	g_return_if_fail ((tab->priv->state == GEDIT_TAB_STATE_LOADING) ||
			  (tab->priv->state == GEDIT_TAB_STATE_REVERTING));
964
	g_return_if_fail (tab->priv->auto_save_timeout <= 0);
965 966 967 968 969 970

	if (tab->priv->timer != NULL)
	{
		g_timer_destroy (tab->priv->timer);
		tab->priv->timer = NULL;
	}
Paolo Borelli's avatar
Paolo Borelli committed
971 972
	tab->priv->times_called = 0;

973
	set_info_bar (tab, NULL);
Paolo Borelli's avatar
Paolo Borelli committed
974

975
	location = gedit_document_get_location (document);
Paolo Borelli's avatar
Paolo Borelli committed
976

977
	/* if the error is CONVERSION FALLBACK don't treat it as a normal error */
978 979
	if (error != NULL &&
	    (error->domain != GEDIT_DOCUMENT_ERROR || error->code != GEDIT_DOCUMENT_ERROR_CONVERSION_FALLBACK))
Paolo Borelli's avatar
Paolo Borelli committed
980 981 982 983 984 985
	{
		if (tab->priv->state == GEDIT_TAB_STATE_LOADING)
			gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING_ERROR);
		else
			gedit_tab_set_state (tab, GEDIT_TAB_STATE_REVERTING_ERROR);

986
		if (error->domain == G_IO_ERROR &&
987
		    error->code == G_IO_ERROR_CANCELLED)
Paolo Borelli's avatar
Paolo Borelli committed
988
		{
989 990 991 992 993
			/* remove the tab, but in an idle handler, since
			 * we are in the handler of doc loaded and we 
			 * don't want doc and tab to be finalized now.
			 */
			g_idle_add ((GSourceFunc) remove_tab_idle, tab);
Paolo Borelli's avatar
Paolo Borelli committed
994

995 996
			goto end;
		}
997
		else
998
		{
999 1000 1001 1002
			if (location)
			{
				_gedit_recent_remove (GEDIT_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))), location);
			}
1003 1004 1005

			if (tab->priv->state == GEDIT_TAB_STATE_LOADING_ERROR)
			{
1006
				emsg = gedit_io_loading_error_info_bar_new (location,
1007 1008
									    tab->priv->tmp_encoding,
									    error);
1009 1010
				g_signal_connect (emsg,
						  "response",
1011
						  G_CALLBACK (io_loading_error_info_bar_response),
1012
						  tab);
Paolo Borelli's avatar
Paolo Borelli committed
1013 1014 1015
			}
			else
			{
1016 1017
				g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_REVERTING_ERROR);
				
1018
				emsg = gedit_unrecoverable_reverting_error_info_bar_new (location,
1019
											 error);
Paolo Borelli's avatar
Paolo Borelli committed
1020

1021 1022
				g_signal_connect (emsg,
						  "response",
1023
						  G_CALLBACK (unrecoverable_reverting_error_info_bar_response),
1024
						  tab);
Paolo Borelli's avatar
Paolo Borelli committed
1025
			}
1026

1027
			set_info_bar (tab, emsg);
1028
		}
Paolo Borelli's avatar
Paolo Borelli committed
1029

1030 1031
		gtk_info_bar_set_default_response (GTK_INFO_BAR (emsg),
						   GTK_RESPONSE_CANCEL);
Paolo Borelli's avatar
Paolo Borelli committed
1032 1033

		gtk_widget_show (emsg);
Paolo Borelli's avatar
Paolo Borelli committed
1034

1035 1036 1037 1038
		if (location)
		{
			g_object_unref (location);
		}
Paolo Borelli's avatar
Paolo Borelli committed
1039 1040 1041 1042 1043

		return;
	}
	else
	{
1044 1045 1046 1047
		if (location != NULL)
		{
			gchar *mime;
			mime = gedit_document_get_mime_type (document);
1048

1049 1050 1051 1052 1053
			_gedit_recent_add (GEDIT_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))),
					   location,
					   mime);
			g_free (mime);
		}
Paolo Borelli's avatar
Paolo Borelli committed
1054

1055 1056 1057
		if (error &&
		    error->domain == GEDIT_DOCUMENT_ERROR &&
		    error->code == GEDIT_DOCUMENT_ERROR_CONVERSION_FALLBACK)
1058 1059 1060
		{
			GtkWidget *emsg;

1061 1062 1063
			/* Set the tab as not editable as we have an error, the
			   user can decide to make it editable again */
			tab->priv->not_editable = TRUE;
1064

1065
			emsg = gedit_io_loading_error_info_bar_new (location,
1066 1067
								    tab->priv->tmp_encoding,
								    error);
1068

1069
			set_info_bar (tab, emsg);
1070 1071 1072

			g_signal_connect (emsg,
					  "response",
1073
					  G_CALLBACK (io_loading_error_info_bar_response),
1074 1075 1076 1077 1078 1079 1080 1081
					  tab);

			gtk_info_bar_set_default_response (GTK_INFO_BAR (emsg),
							   GTK_RESPONSE_CANCEL);

			gtk_widget_show (emsg);
		}

1082
		/* Scroll to the cursor when the document is loaded */
1083
		gedit_view_scroll_to_cursor (gedit_view_frame_get_view (tab->priv->frame));
1084

1085 1086 1087
		/* if the document is readonly we don't care how many times the document
		   is opened */
		if (!gedit_document_get_readonly (document))
Paolo Borelli's avatar
Paolo Borelli committed
1088
		{
1089 1090 1091 1092 1093 1094
			GList *all_documents;
			GList *l;

			all_documents = gedit_app_get_documents (gedit_app_get_default ());

			for (l = all_documents; l != NULL; l = g_list_next (l))
Paolo Borelli's avatar
Paolo Borelli committed
1095
			{
1096 1097 1098 1099 1100
				GeditDocument *d = GEDIT_DOCUMENT (l->data);
			
				if (d != document)
				{
					GFile *loc;
Paolo Borelli's avatar
Paolo Borelli committed
1101

1102
					loc = gedit_document_get_location (d);
Paolo Borelli's avatar
Paolo Borelli committed
1103

1104 1105 1106 1107
					if (loc != NULL && location != NULL &&
					    g_file_equal (location, loc))
					{
						GtkWidget *w;
Paolo Borelli's avatar
Paolo Borelli committed
1108

1109
						tab->priv->not_editable = TRUE;
Paolo Borelli's avatar
Paolo Borelli committed
1110

1111
						w = gedit_file_already_open_warning_info_bar_new (location);
Paolo Borelli's avatar
Paolo Borelli committed
1112

1113
						set_info_bar (tab, w);
Paolo Borelli's avatar
Paolo Borelli committed
1114

1115 1116
						gtk_info_bar_set_default_response (GTK_INFO_BAR (w),
										   GTK_RESPONSE_CANCEL);
Paolo Borelli's avatar
Paolo Borelli committed
1117

1118
						gtk_widget_show (w);
Paolo Borelli's avatar
Paolo Borelli committed
1119

1120 1121 1122 1123
						g_signal_connect (w,
								  "response",
								  G_CALLBACK (file_already_open_warning_info_bar_response),
								  tab);
Paolo Borelli's avatar
Paolo Borelli committed
1124

1125 1126 1127 1128 1129 1130 1131
						g_object_unref (loc);
						break;
					}
				
					if (loc != NULL)
						g_object_unref (loc);
				}
Paolo Borelli's avatar
Paolo Borelli committed
1132 1133
			}

1134 1135
			g_list_free (all_documents);
		}
Paolo Borelli's avatar
Paolo Borelli committed
1136 1137

		gedit_tab_set_state (tab, GEDIT_TAB_STATE_NORMAL);
1138 1139 1140 1141 1142 1143 1144 1145

		if (location == NULL)
		{
			/* FIXME: hackish */
			gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (gedit_tab_get_document (tab)),
			                              TRUE);
		}

1146
		install_auto_save_timeout_if_needed (tab);
1147 1148

		tab->priv->ask_if_externally_modified = TRUE;
Paolo Borelli's avatar
Paolo Borelli committed
1149 1150 1151
	}

 end:
1152 1153 1154 1155
 	if (location)
 	{
		g_object_unref (location);
	}
Paolo Borelli's avatar
Paolo Borelli committed
1156 1157 1158 1159 1160 1161

	tab->priv->tmp_line_pos = 0;
	tab->priv->tmp_encoding = NULL;
}

static void
1162 1163 1164 1165
document_saving (GeditDocument *document,
		 goffset        size,
		 goffset        total_size,
		 GeditTab      *tab)
Paolo Borelli's avatar
Paolo Borelli committed
1166
{
1167 1168
	gdouble et;
	gdouble total_time;
Paolo Borelli's avatar
Paolo Borelli committed
1169 1170 1171

	g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_SAVING);

1172 1173 1174
	gedit_debug_message (DEBUG_TAB, "%" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, size, total_size);


Paolo Borelli's avatar
Paolo Borelli committed
1175 1176 1177 1178 1179 1180 1181 1182
	if (tab->priv->timer == NULL)
	{
		g_return_if_fail (tab->priv->times_called == 0);
		tab->priv->timer = g_timer_new ();
	}

	et = g_timer_elapsed (tab->priv->timer, NULL);

1183 1184 1185 1186
	/* et : total_time = size : total_size */
	total_time = (et * total_size)/size;

	if ((total_time - et) > 3.0)
Paolo Borelli's avatar
Paolo Borelli committed
1187
	{
1188
		show_saving_info_bar (tab);
Paolo Borelli's avatar
Paolo Borelli committed
1189 1190
	}

1191
	info_bar_set_progress (tab, size, total_size);
Paolo Borelli's avatar
Paolo Borelli committed
1192 1193 1194 1195 1196

	tab->priv->times_called++;
}

static void
1197
end_saving (GeditTab *tab)
Paolo Borelli's avatar
Paolo Borelli committed
1198
{
1199
	/* Reset tmp data for saving */
1200 1201 1202 1203 1204
	if (tab->priv->tmp_save_location)
	{
		g_object_unref (tab->priv->tmp_save_location);
		tab->priv->tmp_save_location = NULL;
	}
Paolo Borelli's avatar
Paolo Borelli committed
1205
	tab->priv->tmp_encoding = NULL;
1206 1207
	
	install_auto_save_timeout_if_needed (tab);
Paolo Borelli's avatar
Paolo Borelli committed
1208 1209 1210
}

static void 
1211
unrecoverable_saving_error_info_bar_response (GtkWidget        *info_bar,
1212 1213
                                              gint              response_id,
                                              GeditTab         *tab)
Paolo Borelli's avatar
Paolo Borelli committed
1214 1215 1216 1217 1218 1219 1220 1221
{
	GeditView *view;
	
	if (tab->priv->print_preview != NULL)
		gedit_tab_set_state (tab, GEDIT_TAB_STATE_SHOWING_PRINT_PREVIEW);
	else
		gedit_tab_set_state (tab, GEDIT_TAB_STATE_NORMAL);

1222 1223
	end_saving (tab);
	
1224
	set_info_bar (tab, NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1225 1226 1227

	view = gedit_tab_get_view (tab);

1228
	gtk_widget_grab_focus (GTK_WIDGET (view));
Paolo Borelli's avatar
Paolo Borelli committed
1229 1230
}

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
static void 
invalid_character_info_bar_response (GtkWidget *info_bar,
                                     gint       response_id,
                                     GeditTab  *tab)
{
	if (response_id == GTK_RESPONSE_YES)
	{
		GeditDocument *doc;

		doc = gedit_tab_get_document (tab);
		g_return_if_fail (GEDIT_IS_DOCUMENT (doc));

		set_info_bar (tab, NULL);

		g_return_if_fail (tab->priv->tmp_save_location != NULL);
		g_return_if_fail (tab->priv->tmp_encoding != NULL);

		gedit_tab_set_state (tab, GEDIT_TAB_STATE_SAVING);

		/* don't bug the user again with this... */
		tab->priv->save_flags |= GEDIT_DOCUMENT_SAVE_IGNORE_INVALID_CHARS;

		g_return_if_fail (tab->priv->auto_save_timeout <= 0);
		
		/* Force saving */
		gedit_document_save (doc, tab->priv->save_flags);
	}
	else
	{
		unrecoverable_saving_error_info_bar_response (info_bar,
								  response_id,
								  tab);
	}
}

Paolo Borelli's avatar
Paolo Borelli committed
1266
static void 
1267
no_backup_error_info_bar_response (GtkWidget *info_bar,
1268 1269
				       gint       response_id,
				       GeditTab  *tab)
Paolo Borelli's avatar
Paolo Borelli committed
1270 1271 1272 1273 1274 1275 1276 1277
{
	if (response_id == GTK_RESPONSE_YES)
	{
		GeditDocument *doc;

		doc = gedit_tab_get_document (tab);
		g_return_if_fail (GEDIT_IS_DOCUMENT (doc));

1278
		set_info_bar (tab, NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1279

1280
		g_return_if_fail (tab->priv->tmp_save_location != NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1281 1282 1283 1284 1285 1286 1287
		g_return_if_fail (tab->priv->tmp_encoding != NULL);

		gedit_tab_set_state (tab, GEDIT_TAB_STATE_SAVING);

		/* don't bug the user again with this... */
		tab->priv->save_flags |= GEDIT_DOCUMENT_SAVE_IGNORE_BACKUP;

1288 1289
		g_return_if_fail (tab->priv->auto_save_timeout <= 0);
		
Paolo Borelli's avatar
Paolo Borelli committed
1290 1291 1292 1293 1294
		/* Force saving */
		gedit_document_save (doc, tab->priv->save_flags);
	}
	else
	{
1295
		unrecoverable_saving_error_info_bar_response (info_bar,
Paolo Borelli's avatar
Paolo Borelli committed
1296 1297 1298 1299 1300 1301
								  response_id,
								  tab);
	}
}

static void
1302
externally_modified_error_info_bar_response (GtkWidget *info_bar,
1303 1304
						 gint       response_id,
						 GeditTab  *tab)
Paolo Borelli's avatar
Paolo Borelli committed
1305 1306 1307 1308 1309 1310 1311 1312
{
	if (response_id == GTK_RESPONSE_YES)
	{
		GeditDocument *doc;

		doc = gedit_tab_get_document (tab);
		g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
		
1313
		set_info_bar (tab, NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1314

1315
		g_return_if_fail (tab->priv->tmp_save_location != NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1316 1317 1318 1319
		g_return_if_fail (tab->priv->tmp_encoding != NULL);

		gedit_tab_set_state (tab, GEDIT_TAB_STATE_SAVING);

1320 1321
		g_return_if_fail (tab->priv->auto_save_timeout <= 0);
		
Paolo Borelli's avatar
Paolo Borelli committed
1322 1323 1324 1325 1326 1327
		/* ignore mtime should not be persisted in save flags across saves */

		/* Force saving */
		gedit_document_save (doc, tab->priv->save_flags | GEDIT_DOCUMENT_SAVE_IGNORE_MTIME);
	}
	else
1328
	{		
1329
		unrecoverable_saving_error_info_bar_response (info_bar,
Paolo Borelli's avatar
Paolo Borelli committed
1330 1331 1332 1333 1334 1335
								  response_id,
								  tab);
	}
}

static void 
1336
recoverable_saving_error_info_bar_response (GtkWidget *info_bar,
1337 1338
						gint       response_id,
						GeditTab  *tab)
Paolo Borelli's avatar
Paolo Borelli committed
1339 1340 1341 1342 1343 1344 1345 1346 1347
{
	GeditDocument *doc;

	doc = gedit_tab_get_document (tab);
	g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
	
	if (response_id == GTK_RESPONSE_OK)
	{
		const GeditEncoding *encoding;
1348
		gchar *tmp_uri;
Paolo Borelli's avatar
Paolo Borelli committed
1349
		
1350 1351
		encoding = gedit_conversion_error_info_bar_get_encoding (
									GTK_WIDGET (info_bar));
Paolo Borelli's avatar
Paolo Borelli committed
1352 1353 1354

		g_return_if_fail (encoding != NULL);

1355
		set_info_bar (tab, NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1356

1357
		g_return_if_fail (tab->priv->tmp_save_location != NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1358 1359 1360 1361 1362
				
		gedit_tab_set_state (tab, GEDIT_TAB_STATE_SAVING);
			
		tab->priv->tmp_encoding = encoding;

1363 1364 1365
		tmp_uri = g_file_get_uri (tab->priv->tmp_save_location);
		gedit_debug_message (DEBUG_TAB, "Force saving with URI '%s'", tmp_uri);
		g_free (tmp_uri);
Paolo Borelli's avatar
Paolo Borelli committed
1366
			 
1367 1368
		g_return_if_fail (tab->priv->auto_save_timeout <= 0);
		
Paolo Borelli's avatar
Paolo Borelli committed
1369
		gedit_document_save_as (doc,
1370
					tab->priv->tmp_save_location,
Paolo Borelli's avatar
Paolo Borelli committed
1371
					tab->priv->tmp_encoding,
1372
					gedit_document_get_newline_type (doc),
1373
					gedit_document_get_compression_type (doc),
Paolo Borelli's avatar
Paolo Borelli committed
1374 1375 1376
					tab->priv->save_flags);
	}
	else
1377
	{		
1378
		unrecoverable_saving_error_info_bar_response (info_bar,
Paolo Borelli's avatar
Paolo Borelli committed
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
								  response_id,
								  tab);
	}
}

static void
document_saved (GeditDocument *document,
		const GError  *error,
		GeditTab      *tab)
{
	GtkWidget *emsg;

	g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_SAVING);

1393 1394
	g_return_if_fail (tab->priv->tmp_save_location != NULL);
	g_return_if_fail (tab->priv->tmp_encoding != NULL);
1395
	g_return_if_fail (tab->priv->auto_save_timeout <= 0);
1396 1397 1398 1399 1400 1401

	if (tab->priv->timer != NULL)
	{
		g_timer_destroy (tab->priv->timer);
		tab->priv->timer = NULL;
	}
Paolo Borelli's avatar
Paolo Borelli committed
1402 1403
	tab->priv->times_called = 0;
	
1404
	set_info_bar (tab, NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1405 1406 1407 1408 1409
	
	if (error != NULL)
	{
		gedit_tab_set_state (tab, GEDIT_TAB_STATE_SAVING_ERROR);
		
1410 1411
		if (error->domain == GEDIT_DOCUMENT_ERROR &&
		    error->code == GEDIT_DOCUMENT_ERROR_EXTERNALLY_MODIFIED)
Paolo Borelli's avatar
Paolo Borelli committed
1412
		{
1413
			/* This error is recoverable */
1414
			emsg = gedit_externally_modified_saving_error_info_bar_new (
1415
							tab->priv->tmp_save_location,
1416 1417
							error);
			g_return_if_fail (emsg != NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1418

1419
			set_info_bar (tab, emsg);
Paolo Borelli's avatar
Paolo Borelli committed
1420

1421 1422
			g_signal_connect (emsg,
					  "response",
1423
					  G_CALLBACK (externally_modified_error_info_bar_response),
1424 1425
					  tab);
		}
1426 1427 1428 1429
		else if ((error->domain == GEDIT_DOCUMENT_ERROR &&
			  error->code == GEDIT_DOCUMENT_ERROR_CANT_CREATE_BACKUP) ||
		         (error->domain == G_IO_ERROR &&
			  error->code == G_IO_ERROR_CANT_CREATE_BACKUP))
1430 1431
		{
			/* This error is recoverable */
1432
			emsg = gedit_no_backup_saving_error_info_bar_new (
1433
							tab->priv->tmp_save_location,
1434 1435
							error);
			g_return_if_fail (emsg != NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1436

1437
			set_info_bar (tab, emsg);
Paolo Borelli's avatar
Paolo Borelli committed
1438

1439 1440
			g_signal_connect (emsg,
					  "response",
1441
					  G_CALLBACK (no_backup_error_info_bar_response),
1442 1443
					  tab);
		}
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
		else if (error->domain == GEDIT_DOCUMENT_ERROR &&
		         error->code == GEDIT_DOCUMENT_ERROR_CONVERSION_FALLBACK)
		{
			/* If we have any invalid char in the document we must warn the user
			   as it can make the document useless if it is saved */
			emsg = gedit_invalid_character_info_bar_new (tab->priv->tmp_save_location);
			g_return_if_fail (emsg != NULL);

			set_info_bar (tab, emsg);

			g_signal_connect (emsg,
			                  "response",
			                  G_CALLBACK (invalid_character_info_bar_response),
			                  tab);
		}
1459 1460 1461 1462
		else if (error->domain == GEDIT_DOCUMENT_ERROR ||
			 (error->domain == G_IO_ERROR &&
			  error->code != G_IO_ERROR_INVALID_DATA &&
			  error->code != G_IO_ERROR_PARTIAL_INPUT))
1463 1464 1465
		{
			/* These errors are _NOT_ recoverable */
			_gedit_recent_remove  (GEDIT_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))),
1466
					       tab->priv->tmp_save_location);
1467

1468
			emsg = gedit_unrecoverable_saving_error_info_bar_new (tab->priv->tmp_save_location,
1469 1470 1471
								  error);
			g_return_if_fail (emsg != NULL);
	
1472
			set_info_bar (tab, emsg);
Paolo Borelli's avatar
Paolo Borelli committed
1473

1474 1475
			g_signal_connect (emsg,
					  "response",
<