1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 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
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
/*
 * lapiz-io-error-message-area.c
 * This file is part of lapiz
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/*
 * Modified by the lapiz Team, 2005. See the AUTHORS file for a
 * list of people on the lapiz Team.
 * See the ChangeLog files for a list of changes.
 *
 * $Id$
 */

/*
 * Verbose error reporting for file I/O operations (load, save, revert, create)
 */

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

#include <errno.h>
#include <string.h>

#include <glib/gi18n.h>
#include <gio/gio.h>

#include "lapiz-utils.h"
#include "lapiz-document.h"
#include "lapiz-io-error-message-area.h"
#include "lapiz-prefs-manager.h"
#include <lapiz/lapiz-encodings-combo-box.h>

#define MAX_URI_IN_DIALOG_LENGTH 50

static gboolean
is_recoverable_error (const GError *error)
{
	gboolean is_recoverable = FALSE;

	if (error->domain == G_IO_ERROR)
	{
		switch (error->code) {
		case G_IO_ERROR_PERMISSION_DENIED:
		case G_IO_ERROR_NOT_FOUND:
		case G_IO_ERROR_HOST_NOT_FOUND:
		case G_IO_ERROR_TIMED_OUT:
		case G_IO_ERROR_NOT_MOUNTABLE_FILE:
		case G_IO_ERROR_NOT_MOUNTED:
		case G_IO_ERROR_BUSY:
			is_recoverable = TRUE;
		}
	}

	return is_recoverable;
}

static gboolean
is_gio_error (const GError *error,
	      gint          code)
{
	return error->domain == G_IO_ERROR && error->code == code;
}

static void
set_contents (CtkWidget *area,
	      CtkWidget *contents)
{
	CtkWidget *content_area;

	content_area = ctk_info_bar_get_content_area (CTK_INFO_BAR (area));
	ctk_container_add (CTK_CONTAINER (content_area), contents);
}

static void
info_bar_add_icon_button_with_text (CtkInfoBar  *infobar,
				    const gchar *text,
				    const gchar *icon_id,
				    gint         response_id)
{
	CtkWidget *button;
	CtkWidget *image;

	button = ctk_info_bar_add_button (infobar, text, response_id);
	image = ctk_image_new_from_icon_name (icon_id, CTK_ICON_SIZE_BUTTON);
	ctk_button_set_image (CTK_BUTTON (button), image);
}

static void
set_message_area_text_and_icon (CtkWidget   *message_area,
				const gchar *icon_name,
				const gchar *primary_text,
				const gchar *secondary_text)
{
	CtkWidget *hbox_content;
	CtkWidget *image;
	CtkWidget *vbox;
	gchar *primary_markup;
	CtkWidget *primary_label;

	hbox_content = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 8);

	image = ctk_image_new_from_icon_name (icon_name, CTK_ICON_SIZE_DIALOG);
	ctk_box_pack_start (CTK_BOX (hbox_content), image, FALSE, FALSE, 0);
	ctk_widget_set_halign (image, CTK_ALIGN_CENTER);
	ctk_widget_set_valign (image, CTK_ALIGN_START);

	vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 6);
	ctk_box_pack_start (CTK_BOX (hbox_content), vbox, TRUE, TRUE, 0);

	primary_markup = g_strdup_printf ("<b>%s</b>", primary_text);
	primary_label = ctk_label_new (primary_markup);
	g_free (primary_markup);
	ctk_box_pack_start (CTK_BOX (vbox), primary_label, TRUE, TRUE, 0);
	ctk_label_set_use_markup (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_line_wrap (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_xalign (CTK_LABEL (primary_label), 0.0);
	ctk_widget_set_can_focus (primary_label, TRUE);
	ctk_label_set_selectable (CTK_LABEL (primary_label), TRUE);

	if (secondary_text != NULL)
	{
		gchar *secondary_markup;
		CtkWidget *secondary_label;

		secondary_markup = g_strdup_printf ("<small>%s</small>",
						    secondary_text);
		secondary_label = ctk_label_new (secondary_markup);
		g_free (secondary_markup);
		ctk_box_pack_start (CTK_BOX (vbox), secondary_label, TRUE, TRUE, 0);
		ctk_widget_set_can_focus (secondary_label, TRUE);
		ctk_label_set_use_markup (CTK_LABEL (secondary_label), TRUE);
		ctk_label_set_line_wrap (CTK_LABEL (secondary_label), TRUE);
		ctk_label_set_selectable (CTK_LABEL (secondary_label), TRUE);
		ctk_label_set_xalign (CTK_LABEL (secondary_label), 0.0);
	}

	ctk_widget_show_all (hbox_content);
	set_contents (message_area, hbox_content);
}

static CtkWidget *
create_io_loading_error_message_area (const gchar *primary_text,
				      const gchar *secondary_text,
				      gboolean     recoverable_error)
{
	CtkWidget *message_area;

	message_area = ctk_info_bar_new ();

	ctk_button_set_image (CTK_BUTTON (ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
								   _("_Cancel"),
								   CTK_RESPONSE_CANCEL)),
			      ctk_image_new_from_icon_name ("process-stop", CTK_ICON_SIZE_BUTTON));

	ctk_info_bar_set_message_type (CTK_INFO_BAR (message_area),
				       CTK_MESSAGE_ERROR);

	set_message_area_text_and_icon (message_area,
					"dialog-error",
					primary_text,
					secondary_text);

	if (recoverable_error)
	{
		info_bar_add_icon_button_with_text (CTK_INFO_BAR (message_area),
						    _("_Retry"),
						    "view-refresh",
						    CTK_RESPONSE_OK);
	}

	return message_area;
}

static gboolean
parse_gio_error (gint          code,
	         gchar       **error_message,
	         gchar       **message_details,
	         const gchar  *uri,
	         const gchar  *uri_for_display)
{
	gboolean ret = TRUE;

	switch (code)
	{
	case G_IO_ERROR_NOT_FOUND:
	case G_IO_ERROR_NOT_DIRECTORY:
		*error_message = g_strdup_printf (_("Could not find the file %s."),
						  uri_for_display);
		*message_details = g_strdup (_("Please check that you typed the "
				      	       "location correctly and try again."));
		break;
	case G_IO_ERROR_NOT_SUPPORTED:
		{
			gchar *scheme_string;

			scheme_string = g_uri_parse_scheme (uri);

			if ((scheme_string != NULL) && g_utf8_validate (scheme_string, -1, NULL))
			{
				gchar *scheme_markup;

				scheme_markup = g_markup_printf_escaped ("<i>%s:</i>", scheme_string);

				/* Translators: %s is a URI scheme (like for example http:, ftp:, etc.) */
				*message_details = g_strdup_printf (_("lapiz cannot handle %s locations."),
								   scheme_markup);
				g_free (scheme_markup);
			}
			else
			{
				*message_details = g_strdup (_("lapiz cannot handle this location."));
			}

			g_free (scheme_string);
		}
		break;

	case G_IO_ERROR_NOT_MOUNTABLE_FILE:
		*message_details = g_strdup (_("The location of the file cannot be mounted."));
		break;

	case G_IO_ERROR_NOT_MOUNTED:
		*message_details = g_strdup( _("The location of the file cannot be accessed because it is not mounted."));

		break;
	case G_IO_ERROR_IS_DIRECTORY:
		*error_message = g_strdup_printf (_("%s is a directory."),
						 uri_for_display);
		*message_details = g_strdup (_("Please check that you typed the "
					      "location correctly and try again."));
		break;

	case G_IO_ERROR_INVALID_FILENAME:
		*error_message = g_strdup_printf (_("%s is not a valid location."),
						 uri_for_display);
		*message_details = g_strdup (_("Please check that you typed the "
					      "location correctly and try again."));
		break;

	case G_IO_ERROR_HOST_NOT_FOUND:
		/* This case can be hit for user-typed strings like "foo" due to
		 * the code that guesses web addresses when there's no initial "/".
		 * But this case is also hit for legiticafe web addresses when
		 * the proxy is set up wrong.
		 */
		{
			gchar *hn = NULL;

			if (lapiz_utils_decode_uri (uri, NULL, NULL, &hn, NULL, NULL))
			{
				if (hn != NULL)
				{
					gchar *host_markup;
					gchar *host_name;

					host_name = lapiz_utils_make_valid_utf8 (hn);
					g_free (hn);

					host_markup = g_markup_printf_escaped ("<i>%s</i>", host_name);
					g_free (host_name);

					/* Translators: %s is a host name */
					*message_details = g_strdup_printf (
						_("Host %s could not be found. "
						"Please check that your proxy settings "
						"are correct and try again."),
						host_markup);

					g_free (host_markup);
				}
			}

			if (!*message_details)
			{
				/* use the same string as INVALID_HOST */
				*message_details = g_strdup_printf (
					_("Hostname was invalid. "
					  "Please check that you typed the location "
					  "correctly and try again."));
			}
		}
		break;

	case G_IO_ERROR_NOT_REGULAR_FILE:
		*message_details = g_strdup_printf (_("%s is not a regular file."),
						   uri_for_display);
		break;

	case G_IO_ERROR_TIMED_OUT:
		*message_details = g_strdup (_("Connection timed out. Please try again."));
		break;

	default:
		ret = FALSE;
		break;
	}

	return ret;
}

static gboolean
parse_lapiz_error (gint          code,
	           gchar       **error_message G_GNUC_UNUSED,
	           gchar       **message_details,
	           const gchar  *uri G_GNUC_UNUSED,
	           const gchar  *uri_for_display G_GNUC_UNUSED)
{
	gboolean ret = TRUE;

	switch (code)
	{
	case LAPIZ_DOCUMENT_ERROR_TOO_BIG:
		*message_details = g_strdup (_("The file is too big."));
		break;

	default:
		ret = FALSE;
		break;
	}

	return ret;
}

static void
parse_error (const GError *error,
	     gchar       **error_message,
	     gchar       **message_details,
	     const gchar  *uri,
	     const gchar  *uri_for_display)
{
	gboolean ret = FALSE;

	if (error->domain == G_IO_ERROR)
	{
		ret = parse_gio_error (error->code,
				       error_message,
				       message_details,
				       uri,
				       uri_for_display);
	}
	else if (error->domain == LAPIZ_DOCUMENT_ERROR)
	{
		ret = parse_lapiz_error (error->code,
					 error_message,
					 message_details,
					 uri,
					 uri_for_display);
	}

	if (!ret)
	{
		g_warning ("Hit unhandled case %d (%s) in %s.",
			   error->code, error->message, G_STRFUNC);
		*message_details = g_strdup_printf (_("Unexpected error: %s"),
						   error->message);
	}
}

CtkWidget *
lapiz_unrecoverable_reverting_error_message_area_new (const gchar  *uri,
						      const GError *error)
{
	gchar *error_message = NULL;
	gchar *message_details = NULL;
	gchar *full_formatted_uri;
	gchar *uri_for_display;
	gchar *temp_uri_for_display;
	CtkWidget *message_area;

	g_return_val_if_fail (uri != NULL, NULL);
	g_return_val_if_fail (error != NULL, NULL);
	g_return_val_if_fail ((error->domain == LAPIZ_DOCUMENT_ERROR) ||
			      (error->domain == G_IO_ERROR), NULL);

	full_formatted_uri = lapiz_utils_uri_for_display (uri);

	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	temp_uri_for_display = lapiz_utils_str_middle_truncate (full_formatted_uri,
								MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_formatted_uri);

	uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
	g_free (temp_uri_for_display);

	if (is_gio_error (error, G_IO_ERROR_NOT_FOUND))
	{
		message_details = g_strdup (_("lapiz cannot find the file. "
					      "Perhaps it has recently been deleted."));
	}
	else
	{
		parse_error (error, &error_message, &message_details, uri, uri_for_display);
	}

	if (error_message == NULL)
	{
		error_message = g_strdup_printf (_("Could not revert the file %s."),
						 uri_for_display);
	}

	message_area = create_io_loading_error_message_area (error_message,
							     message_details,
							     FALSE);

	g_free (uri_for_display);
	g_free (error_message);
	g_free (message_details);

	return message_area;
}

static void
create_combo_box (CtkWidget *message_area, CtkWidget *vbox)
{
	CtkWidget *hbox;
	CtkWidget *label;
	CtkWidget *menu;
	gchar *label_markup;

	hbox = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 6);

	label_markup = g_strdup_printf ("<small>%s</small>",
					_("Ch_aracter Encoding:"));
	label = ctk_label_new_with_mnemonic (label_markup);
	g_free (label_markup);
	ctk_label_set_use_markup (CTK_LABEL (label), TRUE);
	menu = lapiz_encodings_combo_box_new (TRUE);
	g_object_set_data (G_OBJECT (message_area),
			   "lapiz-message-area-encoding-menu",
			   menu);

	ctk_label_set_mnemonic_widget (CTK_LABEL (label), menu);
	ctk_box_pack_start (CTK_BOX (hbox),
			    label,
			    FALSE,
			    FALSE,
			    0);

	ctk_box_pack_start (CTK_BOX (hbox),
			    menu,
			    FALSE,
			    FALSE,
			    0);

	ctk_widget_show_all (hbox);
	ctk_box_pack_start (CTK_BOX (vbox), hbox, TRUE, TRUE, 0);
}

static CtkWidget *
create_conversion_error_message_area (const gchar *primary_text,
				      const gchar *secondary_text,
				      gboolean     edit_anyway)
{
	CtkWidget *message_area;
	CtkWidget *hbox_content;
	CtkWidget *image;
	CtkWidget *vbox;
	gchar *primary_markup;
	CtkWidget *primary_label;

	message_area = ctk_info_bar_new ();

	info_bar_add_icon_button_with_text (CTK_INFO_BAR (message_area),
					    _("_Retry"),
					    "edit-redo",
					    CTK_RESPONSE_OK);

	if (edit_anyway)
	{
		ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
		/* Translators: the access key chosen for this string should be
		 different from other main menu access keys (Open, Edit, View...) */
					 _("Edit Any_way"),
					 CTK_RESPONSE_YES);
		ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
		/* Translators: the access key chosen for this string should be
		 different from other main menu access keys (Open, Edit, View...) */
					 _("D_on't Edit"),
					 CTK_RESPONSE_NO);
		ctk_info_bar_set_message_type (CTK_INFO_BAR (message_area),
					       CTK_MESSAGE_WARNING);
	}
	else
	{
		ctk_button_set_image (CTK_BUTTON (ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
									   _("_Cancel"),
									   CTK_RESPONSE_CANCEL)),
				      ctk_image_new_from_icon_name ("process-stop", CTK_ICON_SIZE_BUTTON));

		ctk_info_bar_set_message_type (CTK_INFO_BAR (message_area),
					       CTK_MESSAGE_ERROR);
	}

	hbox_content = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 8);

	image = ctk_image_new_from_icon_name ("dialog-error", CTK_ICON_SIZE_DIALOG);
	ctk_box_pack_start (CTK_BOX (hbox_content), image, FALSE, FALSE, 0);
	ctk_widget_set_halign (image, CTK_ALIGN_CENTER);
	ctk_widget_set_valign (image, CTK_ALIGN_START);

	vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 6);
	ctk_box_pack_start (CTK_BOX (hbox_content), vbox, TRUE, TRUE, 0);

	primary_markup = g_strdup_printf ("<b>%s</b>", primary_text);
	primary_label = ctk_label_new (primary_markup);
	g_free (primary_markup);
	ctk_box_pack_start (CTK_BOX (vbox), primary_label, TRUE, TRUE, 0);
	ctk_label_set_use_markup (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_line_wrap (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_xalign (CTK_LABEL (primary_label), 0.0);
	ctk_widget_set_can_focus (primary_label, TRUE);
	ctk_label_set_selectable (CTK_LABEL (primary_label), TRUE);

	if (secondary_text != NULL)
	{
		gchar *secondary_markup;
		CtkWidget *secondary_label;

		secondary_markup = g_strdup_printf ("<small>%s</small>",
						    secondary_text);
		secondary_label = ctk_label_new (secondary_markup);
		g_free (secondary_markup);
		ctk_box_pack_start (CTK_BOX (vbox), secondary_label, TRUE, TRUE, 0);
		ctk_widget_set_can_focus (secondary_label, TRUE);
		ctk_label_set_use_markup (CTK_LABEL (secondary_label), TRUE);
		ctk_label_set_line_wrap (CTK_LABEL (secondary_label), TRUE);
		ctk_label_set_selectable (CTK_LABEL (secondary_label), TRUE);
		ctk_label_set_xalign (CTK_LABEL (secondary_label), 0.0);
	}

	create_combo_box (message_area, vbox);
	ctk_widget_show_all (hbox_content);
	set_contents (message_area, hbox_content);

	return message_area;
}

CtkWidget *
lapiz_io_loading_error_message_area_new (const gchar         *uri,
					 const LapizEncoding *encoding,
					 const GError        *error)
{
	gchar *error_message = NULL;
	gchar *message_details = NULL;
	gchar *full_formatted_uri;
	gchar *encoding_name;
	gchar *uri_for_display;
	gchar *temp_uri_for_display;
	CtkWidget *message_area;
	gboolean edit_anyway = FALSE;
	gboolean convert_error = FALSE;

	g_return_val_if_fail (uri != NULL, NULL);
	g_return_val_if_fail (error != NULL, NULL);
	g_return_val_if_fail ((error->domain == G_CONVERT_ERROR) ||
			      (error->domain == LAPIZ_DOCUMENT_ERROR) ||
			      (error->domain == G_IO_ERROR), NULL);

	full_formatted_uri = lapiz_utils_uri_for_display (uri);

	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	temp_uri_for_display = lapiz_utils_str_middle_truncate (full_formatted_uri,
								MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_formatted_uri);

	uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
	g_free (temp_uri_for_display);

	if (encoding != NULL)
		encoding_name = lapiz_encoding_to_string (encoding);
	else
		encoding_name = g_strdup ("UTF-8");

	if (is_gio_error (error, G_IO_ERROR_TOO_MANY_LINKS))
	{
		message_details = g_strdup (_("The number of followed links is limited and the actual file could not be found within this limit."));
	}
	else if (is_gio_error (error, G_IO_ERROR_PERMISSION_DENIED))
	{
		message_details = g_strdup (_("You do not have the permissions necessary to open the file."));
	}
	else if ((is_gio_error (error, G_IO_ERROR_INVALID_DATA) && encoding == NULL) ||
	         (error->domain == LAPIZ_DOCUMENT_ERROR &&
	         error->code == LAPIZ_DOCUMENT_ERROR_ENCODING_AUTO_DETECTION_FAILED))
	{
		message_details = g_strconcat (_("lapiz has not been able to detect "
					         "the character encoding."), "\n",
					       _("Please check that you are not trying to open a binary file."), "\n",
					       _("Select a character encoding from the menu and try again."), NULL);
		convert_error = TRUE;
	}
	else if (error->domain == LAPIZ_DOCUMENT_ERROR &&
	         error->code == LAPIZ_DOCUMENT_ERROR_CONVERSION_FALLBACK)
	{
		error_message = g_strdup_printf (_("There was a problem opening the file %s."),
						 uri_for_display);
		message_details = g_strconcat (_("The file you opened has some invalid characters. "
					       "If you continue editing this file you could make this "
					       "document useless."), "\n",
					       _("You can also choose another character encoding and try again."),
					       NULL);
		edit_anyway = TRUE;
		convert_error = TRUE;
	}
	else if (is_gio_error (error, G_IO_ERROR_INVALID_DATA) && encoding != NULL)
	{
		error_message = g_strdup_printf (_("Could not open the file %s using the %s character encoding."),
						 uri_for_display,
						 encoding_name);
		message_details = g_strconcat (_("Please check that you are not trying to open a binary file."), "\n",
					       _("Select a different character encoding from the menu and try again."), NULL);
		convert_error = TRUE;
	}
	else
	{
		parse_error (error, &error_message, &message_details, uri, uri_for_display);
	}

	if (error_message == NULL)
	{
		error_message = g_strdup_printf (_("Could not open the file %s."),
						 uri_for_display);
	}

	if (convert_error)
	{
		message_area = create_conversion_error_message_area (error_message,
								     message_details,
								     edit_anyway);
	}
	else
	{
		message_area = create_io_loading_error_message_area (error_message,
								     message_details,
								     is_recoverable_error (error));
	}

	g_free (uri_for_display);
	g_free (encoding_name);
	g_free (error_message);
	g_free (message_details);

	return message_area;
}

CtkWidget *
lapiz_conversion_error_while_saving_message_area_new (
						const gchar         *uri,
						const LapizEncoding *encoding,
				    		const GError        *error)
{
	gchar *error_message = NULL;
	gchar *message_details = NULL;
	gchar *full_formatted_uri;
	gchar *encoding_name;
	gchar *uri_for_display;
	gchar *temp_uri_for_display;
	CtkWidget *message_area;

	g_return_val_if_fail (uri != NULL, NULL);
	g_return_val_if_fail (error != NULL, NULL);
	g_return_val_if_fail (error->domain == G_CONVERT_ERROR ||
	                      error->domain == G_IO_ERROR, NULL);
	g_return_val_if_fail (encoding != NULL, NULL);

	full_formatted_uri = lapiz_utils_uri_for_display (uri);

	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	temp_uri_for_display = lapiz_utils_str_middle_truncate (full_formatted_uri,
								MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_formatted_uri);

	uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
	g_free (temp_uri_for_display);

	encoding_name = lapiz_encoding_to_string (encoding);

	error_message = g_strdup_printf (_("Could not save the file %s using the %s character encoding."),
					 uri_for_display,
					 encoding_name);
	message_details = g_strconcat (_("The document contains one or more characters that cannot be encoded "
					 "using the specified character encoding."), "\n",
				       _("Select a different character encoding from the menu and try again."), NULL);

	message_area = create_conversion_error_message_area (
								error_message,
								message_details,
								FALSE);

	g_free (uri_for_display);
	g_free (encoding_name);
	g_free (error_message);
	g_free (message_details);

	return message_area;
}

const LapizEncoding *
lapiz_conversion_error_message_area_get_encoding (CtkWidget *message_area)
{
	gpointer menu;

	g_return_val_if_fail (CTK_IS_INFO_BAR (message_area), NULL);

	menu = g_object_get_data (G_OBJECT (message_area),
				  "lapiz-message-area-encoding-menu");
	g_return_val_if_fail (menu, NULL);

	return lapiz_encodings_combo_box_get_selected_encoding
					(LAPIZ_ENCODINGS_COMBO_BOX (menu));
}

CtkWidget *
lapiz_file_already_open_warning_message_area_new (const gchar *uri)
{
	CtkWidget *message_area;
	CtkWidget *hbox_content;
	CtkWidget *image;
	CtkWidget *vbox;
	gchar *primary_markup;
	gchar *secondary_markup;
	CtkWidget *primary_label;
	CtkWidget *secondary_label;
	gchar *primary_text;
	const gchar *secondary_text;
	gchar *full_formatted_uri;
	gchar *uri_for_display;
	gchar *temp_uri_for_display;

	full_formatted_uri = lapiz_utils_uri_for_display (uri);

	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	temp_uri_for_display = lapiz_utils_str_middle_truncate (full_formatted_uri,
								MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_formatted_uri);

	uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
	g_free (temp_uri_for_display);

	message_area = ctk_info_bar_new ();
	ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
	/* Translators: the access key chosen for this string should be
	 different from other main menu access keys (Open, Edit, View...) */
				 _("Edit Any_way"),
				 CTK_RESPONSE_YES);
	ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
	/* Translators: the access key chosen for this string should be
	 different from other main menu access keys (Open, Edit, View...) */
				 _("D_on't Edit"),
				 CTK_RESPONSE_CANCEL);
	ctk_info_bar_set_message_type (CTK_INFO_BAR (message_area),
				       CTK_MESSAGE_WARNING);

	hbox_content = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 8);

	image = ctk_image_new_from_icon_name ("dialog-warning", CTK_ICON_SIZE_DIALOG);
	ctk_box_pack_start (CTK_BOX (hbox_content), image, FALSE, FALSE, 0);
	ctk_widget_set_halign (image, CTK_ALIGN_CENTER);
	ctk_widget_set_valign (image, CTK_ALIGN_START);

	vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 6);
	ctk_box_pack_start (CTK_BOX (hbox_content), vbox, TRUE, TRUE, 0);

	primary_text = g_strdup_printf (_("This file (%s) is already open in another lapiz window."), uri_for_display);
	g_free (uri_for_display);

	primary_markup = g_strdup_printf ("<b>%s</b>", primary_text);
	g_free (primary_text);
	primary_label = ctk_label_new (primary_markup);
	g_free (primary_markup);
	ctk_box_pack_start (CTK_BOX (vbox), primary_label, TRUE, TRUE, 0);
	ctk_label_set_use_markup (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_line_wrap (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_xalign (CTK_LABEL (primary_label), 0.0);
	ctk_widget_set_can_focus (primary_label, TRUE);
	ctk_label_set_selectable (CTK_LABEL (primary_label), TRUE);

	secondary_text = _("lapiz opened this instance of the file in a non-editable way. "
			   "Do you want to edit it anyway?");
	secondary_markup = g_strdup_printf ("<small>%s</small>",
					    secondary_text);
	secondary_label = ctk_label_new (secondary_markup);
	g_free (secondary_markup);
	ctk_box_pack_start (CTK_BOX (vbox), secondary_label, TRUE, TRUE, 0);
	ctk_widget_set_can_focus (secondary_label, TRUE);
	ctk_label_set_use_markup (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_line_wrap (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_selectable (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_xalign (CTK_LABEL (secondary_label), 0.0);

	ctk_widget_show_all (hbox_content);
	set_contents (message_area, hbox_content);

	return message_area;
}

CtkWidget *
lapiz_externally_modified_saving_error_message_area_new (
						const gchar  *uri,
						const GError *error)
{
	CtkWidget *message_area;
	CtkWidget *hbox_content;
	CtkWidget *image;
	CtkWidget *vbox;
	gchar *primary_markup;
	gchar *secondary_markup;
	CtkWidget *primary_label;
	CtkWidget *secondary_label;
	gchar *primary_text;
	const gchar *secondary_text;
	gchar *full_formatted_uri;
	gchar *uri_for_display;
	gchar *temp_uri_for_display;

	g_return_val_if_fail (uri != NULL, NULL);
	g_return_val_if_fail (error != NULL, NULL);
	g_return_val_if_fail (error->domain == LAPIZ_DOCUMENT_ERROR, NULL);
	g_return_val_if_fail (error->code == LAPIZ_DOCUMENT_ERROR_EXTERNALLY_MODIFIED, NULL);

	full_formatted_uri = lapiz_utils_uri_for_display (uri);

	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	temp_uri_for_display = lapiz_utils_str_middle_truncate (full_formatted_uri,
								MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_formatted_uri);

	uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
	g_free (temp_uri_for_display);

	message_area = ctk_info_bar_new ();

	info_bar_add_icon_button_with_text (CTK_INFO_BAR (message_area),
					    _("S_ave Anyway"),
					    "document-save",
					    CTK_RESPONSE_YES);
	ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
				 _("D_on't Save"),
				 CTK_RESPONSE_CANCEL);
	ctk_info_bar_set_message_type (CTK_INFO_BAR (message_area),
				       CTK_MESSAGE_WARNING);

	hbox_content = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 8);

	image = ctk_image_new_from_icon_name ("dialog-warning", CTK_ICON_SIZE_DIALOG);
	ctk_box_pack_start (CTK_BOX (hbox_content), image, FALSE, FALSE, 0);
	ctk_widget_set_halign (image, CTK_ALIGN_CENTER);
	ctk_widget_set_valign (image, CTK_ALIGN_START);

	vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 6);
	ctk_box_pack_start (CTK_BOX (hbox_content), vbox, TRUE, TRUE, 0);

	// FIXME: review this message, it's not clear since for the user the "modification"
	// could be interpreted as the changes he made in the document. beside "reading" is
	// not accurate (since last load/save)
	primary_text = g_strdup_printf (_("The file %s has been modified since reading it."),
					uri_for_display);
	g_free (uri_for_display);

	primary_markup = g_strdup_printf ("<b>%s</b>", primary_text);
	g_free (primary_text);
	primary_label = ctk_label_new (primary_markup);
	g_free (primary_markup);
	ctk_box_pack_start (CTK_BOX (vbox), primary_label, TRUE, TRUE, 0);
	ctk_label_set_use_markup (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_line_wrap (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_xalign (CTK_LABEL (primary_label), 0.0);
	ctk_widget_set_can_focus (primary_label, TRUE);
	ctk_label_set_selectable (CTK_LABEL (primary_label), TRUE);

	secondary_text = _("If you save it, all the external changes could be lost. Save it anyway?");
	secondary_markup = g_strdup_printf ("<small>%s</small>",
					    secondary_text);
	secondary_label = ctk_label_new (secondary_markup);
	g_free (secondary_markup);
	ctk_box_pack_start (CTK_BOX (vbox), secondary_label, TRUE, TRUE, 0);
	ctk_widget_set_can_focus (secondary_label, TRUE);
	ctk_label_set_use_markup (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_line_wrap (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_selectable (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_xalign (CTK_LABEL (secondary_label), 0.0);

	ctk_widget_show_all (hbox_content);
	set_contents (message_area, hbox_content);

	return message_area;
}

CtkWidget *
lapiz_no_backup_saving_error_message_area_new (const gchar  *uri,
					       const GError *error)
{
	CtkWidget *message_area;
	CtkWidget *hbox_content;
	CtkWidget *image;
	CtkWidget *vbox;
	gchar *primary_markup;
	gchar *secondary_markup;
	CtkWidget *primary_label;
	CtkWidget *secondary_label;
	gchar *primary_text;
	const gchar *secondary_text;
	gchar *full_formatted_uri;
	gchar *uri_for_display;
	gchar *temp_uri_for_display;

	g_return_val_if_fail (uri != NULL, NULL);
	g_return_val_if_fail (error != NULL, NULL);
	g_return_val_if_fail (((error->domain == LAPIZ_DOCUMENT_ERROR &&
			        error->code == LAPIZ_DOCUMENT_ERROR_CANT_CREATE_BACKUP) ||
			       (error->domain == G_IO_ERROR &&
			        error->code == G_IO_ERROR_CANT_CREATE_BACKUP)), NULL);

	full_formatted_uri = lapiz_utils_uri_for_display (uri);

	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	temp_uri_for_display = lapiz_utils_str_middle_truncate (full_formatted_uri,
								MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_formatted_uri);

	uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
	g_free (temp_uri_for_display);

	message_area = ctk_info_bar_new ();

	info_bar_add_icon_button_with_text (CTK_INFO_BAR (message_area),
					    _("S_ave Anyway"),
					    "document-save",
					    CTK_RESPONSE_YES);
	ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
				 _("D_on't Save"),
				 CTK_RESPONSE_CANCEL);
	ctk_info_bar_set_message_type (CTK_INFO_BAR (message_area),
				       CTK_MESSAGE_WARNING);

	hbox_content = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 8);

	image = ctk_image_new_from_icon_name ("dialog-warning", CTK_ICON_SIZE_DIALOG);
	ctk_box_pack_start (CTK_BOX (hbox_content), image, FALSE, FALSE, 0);
	ctk_widget_set_halign (image, CTK_ALIGN_CENTER);
	ctk_widget_set_valign (image, CTK_ALIGN_START);

	vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 6);
	ctk_box_pack_start (CTK_BOX (hbox_content), vbox, TRUE, TRUE, 0);

	// FIXME: review this messages

	if (lapiz_prefs_manager_get_create_backup_copy ())
		primary_text = g_strdup_printf (_("Could not create a backup file while saving %s"),
						uri_for_display);
	else
		primary_text = g_strdup_printf (_("Could not create a temporary backup file while saving %s"),
						uri_for_display);

	g_free (uri_for_display);

	primary_markup = g_strdup_printf ("<b>%s</b>", primary_text);
	g_free (primary_text);
	primary_label = ctk_label_new (primary_markup);
	g_free (primary_markup);
	ctk_box_pack_start (CTK_BOX (vbox), primary_label, TRUE, TRUE, 0);
	ctk_label_set_use_markup (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_line_wrap (CTK_LABEL (primary_label), TRUE);
	ctk_label_set_xalign (CTK_LABEL (primary_label), 0.0);
	ctk_widget_set_can_focus (primary_label, TRUE);
	ctk_label_set_selectable (CTK_LABEL (primary_label), TRUE);

	secondary_text = _("lapiz could not back up the old copy of the file before saving the new one. "
			   "You can ignore this warning and save the file anyway, but if an error "
			   "occurs while saving, you could lose the old copy of the file. Save anyway?");
	secondary_markup = g_strdup_printf ("<small>%s</small>",
					    secondary_text);
	secondary_label = ctk_label_new (secondary_markup);
	g_free (secondary_markup);
	ctk_box_pack_start (CTK_BOX (vbox), secondary_label, TRUE, TRUE, 0);
	ctk_widget_set_can_focus (secondary_label, TRUE);
	ctk_label_set_use_markup (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_line_wrap (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_selectable (CTK_LABEL (secondary_label), TRUE);
	ctk_label_set_xalign (CTK_LABEL (secondary_label), 0.0);

	ctk_widget_show_all (hbox_content);
	set_contents (message_area, hbox_content);

	return message_area;
}

CtkWidget *
lapiz_unrecoverable_saving_error_message_area_new (const gchar  *uri,
						   const GError *error)
{
	gchar *error_message = NULL;
	gchar *message_details = NULL;
	gchar *full_formatted_uri;
	gchar *uri_for_display;
	gchar *temp_uri_for_display;
	CtkWidget *message_area;

	g_return_val_if_fail (uri != NULL, NULL);
	g_return_val_if_fail (error != NULL, NULL);
	g_return_val_if_fail ((error->domain == LAPIZ_DOCUMENT_ERROR) ||
			      (error->domain == G_IO_ERROR), NULL);

	full_formatted_uri = lapiz_utils_uri_for_display (uri);

	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	temp_uri_for_display = lapiz_utils_str_middle_truncate (full_formatted_uri,
								MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_formatted_uri);

	uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
	g_free (temp_uri_for_display);

	if (is_gio_error (error, G_IO_ERROR_NOT_SUPPORTED))
	{
		gchar *scheme_string;

		scheme_string = g_uri_parse_scheme (uri);

		if ((scheme_string != NULL) && g_utf8_validate (scheme_string, -1, NULL))
		{
			gchar *scheme_markup;

			scheme_markup = g_markup_printf_escaped ("<i>%s:</i>", scheme_string);

			/* Translators: %s is a URI scheme (like for example http:, ftp:, etc.) */
			message_details = g_strdup_printf (_("lapiz cannot handle %s locations in write mode. "
							     "Please check that you typed the "
							     "location correctly and try again."),
							   scheme_markup);
			g_free (scheme_markup);
		}
		else
		{
			message_details = g_strdup (_("lapiz cannot handle this location in write mode. "
						      "Please check that you typed the "
						      "location correctly and try again."));
		}

		g_free (scheme_string);
	}
	else if (is_gio_error (error, G_IO_ERROR_INVALID_FILENAME))
	{
		message_details = g_strdup (_("%s is not a valid location. "
					      "Please check that you typed the "
					      "location correctly and try again."));
	}
	else if (is_gio_error (error, G_IO_ERROR_PERMISSION_DENIED))
	{
		message_details = g_strdup (_("You do not have the permissions necessary to save the file. "
					      "Please check that you typed the "
					      "location correctly and try again."));
	}
	else if (is_gio_error (error, G_IO_ERROR_NO_SPACE))
	{
		message_details = g_strdup (_("There is not enough disk space to save the file. "
					      "Please free some disk space and try again."));
	}
	else if (is_gio_error (error, G_IO_ERROR_READ_ONLY))
	{
		message_details = g_strdup (_("You are trying to save the file on a read-only disk. "
					      "Please check that you typed the location "
					      "correctly and try again."));
	}
	else if (is_gio_error (error, G_IO_ERROR_EXISTS))
	{
		message_details = g_strdup (_("A file with the same name already exists. "
					      "Please use a different name."));
	}
	else if (is_gio_error (error, G_IO_ERROR_FILENAME_TOO_LONG))
	{
		message_details = g_strdup (_("The disk where you are trying to save the file has "
					      "a limitation on length of the file names. "
					      "Please use a shorter name."));
	}
	else if (error->domain == LAPIZ_DOCUMENT_ERROR &&
		 error->code == LAPIZ_DOCUMENT_ERROR_TOO_BIG)
	{
		message_details = g_strdup (_("The disk where you are trying to save the file has "
					      "a limitation on file sizes. Please try saving "
					      "a smaller file or saving it to a disk that does not "
					      "have this limitation."));
	}
	else
	{
		parse_error (error,
			     &error_message,
			     &message_details,
			     uri,
			     uri_for_display);
	}

	if (error_message == NULL)
	{
		error_message = g_strdup_printf (_("Could not save the file %s."),
						 uri_for_display);
	}

	message_area = create_io_loading_error_message_area (error_message,
							     message_details,
							     FALSE);

	g_free (uri_for_display);
	g_free (error_message);
	g_free (message_details);

	return message_area;
}

CtkWidget *
lapiz_externally_modified_message_area_new (const gchar *uri,
					    gboolean     document_modified)
{
	gchar *full_formatted_uri;
	gchar *uri_for_display;
	gchar *temp_uri_for_display;
	const gchar *primary_text;
	const gchar *secondary_text;
	CtkWidget *message_area;

	g_return_val_if_fail (uri != NULL, NULL);

	full_formatted_uri = lapiz_utils_uri_for_display (uri);

	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
	temp_uri_for_display = lapiz_utils_str_middle_truncate (full_formatted_uri,
								MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_formatted_uri);

	uri_for_display = g_markup_printf_escaped ("<i>%s</i>", temp_uri_for_display);
	g_free (temp_uri_for_display);

	// FIXME: review this message, it's not clear since for the user the "modification"
	// could be interpreted as the changes he made in the document. beside "reading" is
	// not accurate (since last load/save)
	primary_text = g_strdup_printf (_("The file %s changed on disk."),
					uri_for_display);
	g_free (uri_for_display);

	if (document_modified)
		secondary_text = _("Do you want to drop your changes and reload the file?");
	else
		secondary_text = _("Do you want to reload the file?");

	message_area = ctk_info_bar_new ();

	info_bar_add_icon_button_with_text (CTK_INFO_BAR (message_area),
					    _("_Reload"),
					    "view-refresh",
					    CTK_RESPONSE_OK);

	ctk_button_set_image (CTK_BUTTON (ctk_info_bar_add_button (CTK_INFO_BAR (message_area),
								   _("_Cancel"),
								   CTK_RESPONSE_CANCEL)),
			      ctk_image_new_from_icon_name ("process-stop", CTK_ICON_SIZE_BUTTON));

	ctk_info_bar_set_message_type (CTK_INFO_BAR (message_area),
				       CTK_MESSAGE_WARNING);

	set_message_area_text_and_icon (message_area,
					"ctk-dialog-warning",
					primary_text,
					secondary_text);

	return message_area;
}