1 /* Eye Of Mate - Erro Message Area
2  *
3  * Copyright (C) 2007 The Free Software Foundation
4  *
5  * Author: Lucas Rocha <lucasr@gnome.org>
6  *
7  * Based on gedit code (gedit/gedit-message-area.h) by:
8  * 	- Paolo Maggi <paolo@gnome.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include "eom-error-message-area.h"
30 #include "eom-image.h"
31 #include "eom-util.h"
32 
33 #include <glib.h>
34 #include <glib/gi18n.h>
35 #include <glib-object.h>
36 #include <gtk/gtk.h>
37 
38 static void
set_message_area_text_and_icon(GtkInfoBar * message_area,const gchar * icon_name,const gchar * primary_text,const gchar * secondary_text)39 set_message_area_text_and_icon (GtkInfoBar   *message_area,
40 				const gchar  *icon_name,
41 				const gchar  *primary_text,
42 				const gchar  *secondary_text)
43 {
44 	GtkWidget *hbox_content;
45 	GtkWidget *image;
46 	GtkWidget *vbox;
47 	gchar *primary_markup;
48 	gchar *secondary_markup;
49 	GtkWidget *primary_label;
50 	GtkWidget *secondary_label;
51 
52 	hbox_content = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
53 	gtk_widget_show (hbox_content);
54 
55 	image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_DIALOG);
56 	gtk_widget_show (image);
57 	gtk_box_pack_start (GTK_BOX (hbox_content), image, FALSE, FALSE, 0);
58 	gtk_widget_set_valign (image, GTK_ALIGN_START);
59 
60 	vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
61 	gtk_widget_show (vbox);
62 	gtk_box_pack_start (GTK_BOX (hbox_content), vbox, TRUE, TRUE, 0);
63 
64 	primary_markup = g_markup_printf_escaped ("<b>%s</b>", primary_text);
65 	primary_label = gtk_label_new (primary_markup);
66 	g_free (primary_markup);
67 
68 	gtk_widget_show (primary_label);
69 
70 	gtk_box_pack_start (GTK_BOX (vbox), primary_label, TRUE, TRUE, 0);
71 	gtk_label_set_use_markup (GTK_LABEL (primary_label), TRUE);
72 	gtk_label_set_line_wrap (GTK_LABEL (primary_label), FALSE);
73 	gtk_label_set_xalign (GTK_LABEL (primary_label), 0.0);
74 
75 	gtk_widget_set_can_focus (primary_label, TRUE);
76 
77 	gtk_label_set_selectable (GTK_LABEL (primary_label), TRUE);
78 
79 	if (secondary_text != NULL) {
80 		secondary_markup = g_markup_printf_escaped ("<small>%s</small>",
81 		                                            secondary_text);
82 		secondary_label = gtk_label_new (secondary_markup);
83 		g_free (secondary_markup);
84 
85 		gtk_widget_show (secondary_label);
86 
87 		gtk_box_pack_start (GTK_BOX (vbox), secondary_label, TRUE, TRUE, 0);
88 
89 		gtk_widget_set_can_focus (secondary_label, TRUE);
90 
91 		gtk_label_set_use_markup (GTK_LABEL (secondary_label), TRUE);
92 		gtk_label_set_line_wrap (GTK_LABEL (secondary_label), TRUE);
93 		gtk_label_set_selectable (GTK_LABEL (secondary_label), TRUE);
94 		gtk_label_set_xalign (GTK_LABEL (secondary_label), 0.0);
95 	}
96 
97 	gtk_box_pack_start (GTK_BOX (gtk_info_bar_get_content_area (GTK_INFO_BAR (message_area))), hbox_content, TRUE, TRUE, 0);
98 }
99 
100 static GtkWidget *
create_error_message_area(const gchar * primary_text,const gchar * secondary_text,gboolean recoverable)101 create_error_message_area (const gchar *primary_text,
102 			   const gchar *secondary_text,
103 			   gboolean     recoverable)
104 {
105 	GtkWidget *message_area;
106 
107 	if (recoverable)
108 		message_area = gtk_info_bar_new_with_buttons (_("_Retry"),
109 							      GTK_RESPONSE_OK,
110 							      NULL);
111 	else
112 		message_area = gtk_info_bar_new ();
113 
114 	gtk_info_bar_set_message_type (GTK_INFO_BAR (message_area),
115 				       GTK_MESSAGE_ERROR);
116 
117 	set_message_area_text_and_icon (GTK_INFO_BAR (message_area),
118 					"dialog-error",
119 					primary_text,
120 					secondary_text);
121 
122 	return message_area;
123 }
124 
125 /**
126  * eom_image_load_error_message_area_new:
127  * @caption:
128  * @error:
129  *
130  *
131  *
132  * Returns: (transfer full): a new #GtkInfoArea
133  **/
134 GtkWidget *
eom_image_load_error_message_area_new(const gchar * caption,const GError * error)135 eom_image_load_error_message_area_new (const gchar  *caption,
136 				       const GError *error)
137 {
138 	GtkWidget *message_area;
139 	gchar *error_message = NULL;
140 	gchar *message_details = NULL;
141 	gchar *pango_escaped_caption = NULL;
142 
143 	g_return_val_if_fail (caption != NULL, NULL);
144 	g_return_val_if_fail (error != NULL, NULL);
145 
146 	/* Escape the caption string with respect to pango markup.
147 	   This is necessary because otherwise characters like "&" will
148 	   be interpreted as the beginning of a pango entity inside
149 	   the message area GtkLabel. */
150 	pango_escaped_caption = g_markup_escape_text (caption, -1);
151 	error_message = g_strdup_printf (_("Could not load image '%s'."),
152 					 pango_escaped_caption);
153 
154 	message_details = eom_util_make_valid_utf8 (error->message);
155 
156 	message_area = create_error_message_area (error_message,
157 						  message_details,
158 						  TRUE);
159 
160 	g_free (pango_escaped_caption);
161 	g_free (error_message);
162 	g_free (message_details);
163 
164 	return message_area;
165 }
166 
167 /**
168  * eom_no_images_error_message_area_new:
169  * @file:
170  *
171  *
172  *
173  * Returns: (transfer full): a new #GtkInfoBar
174  **/
175 GtkWidget *
eom_no_images_error_message_area_new(GFile * file)176 eom_no_images_error_message_area_new (GFile *file)
177 {
178 	GtkWidget *message_area;
179 	gchar *error_message = NULL;
180 
181 	if (file != NULL) {
182 		gchar *uri_str, *unescaped_str, *pango_escaped_str;
183 
184 		uri_str = g_file_get_uri (file);
185 		/* Unescape URI with respect to rules defined in RFC 3986. */
186 		unescaped_str = g_uri_unescape_string (uri_str, NULL);
187 
188 		/* Escape the URI string with respect to pango markup.
189 		   This is necessary because the URI string can contain
190 		   for example "&" which will otherwise be interpreted
191 		   as a pango markup entity when inserted into a GtkLabel. */
192 		pango_escaped_str = g_markup_escape_text (unescaped_str, -1);
193 		error_message = g_strdup_printf (_("No images found in '%s'."),
194 						 pango_escaped_str);
195 
196 		g_free (pango_escaped_str);
197 		g_free (uri_str);
198 		g_free (unescaped_str);
199 	} else {
200 		error_message = g_strdup (_("The given locations contain no images."));
201 	}
202 
203 	message_area = create_error_message_area (error_message,
204 						  NULL,
205 						  FALSE);
206 
207 	g_free (error_message);
208 
209 	return message_area;
210 }
211