1 /*
2 ** 1998-05-23 -	A big fat error handling module. There are *so* many possible errors,
3 **		I think we really must get a grip on that situation. This is it.
4 **		As far as grips go, I consider this to be pretty loose. :(
5 */
6 
7 #include "gentoo.h"
8 
9 #include <ctype.h>
10 #include <stdarg.h>
11 #include <string.h>
12 
13 #include "dirpane.h"
14 #include "dialog.h"
15 #include "guiutil.h"
16 #include "errors.h"
17 
18 /* ----------------------------------------------------------------------------------------- */
19 
20 /* Description of the error. This is in UTF-8. */
21 static gchar	error_desc[1024] = "";
22 
23 /* ----------------------------------------------------------------------------------------- */
24 
err_clear(MainInfo * min)25 void err_clear(MainInfo *min)
26 {
27 	error_desc[0] = '\0';
28 }
29 
30 /* 1999-01-21 -	Format an arbitrary string and print it on the status (error) line.
31 ** UGLY   UGLY	Uses a sneaky little loop to ensure that the text is displayed *NOW*.
32 */
err_printf(MainInfo * min,const gchar * fmt,...)33 void err_printf(MainInfo *min, const gchar *fmt, ...)
34 {
35 	va_list	al;
36 
37 	va_start(al, fmt);
38 	vsnprintf(error_desc, sizeof error_desc, fmt, al);
39 	va_end(al);
40 	err_show(min);
41 	gui_events_flush();
42 }
43 
44 /* 1998-05-30 -	Added special treatment of code == -1; this means that there is no
45 **		errno-compatible error information available, and the entire thing
46 **		is then suppressed.
47 ** 2010-03-07 -	Assumes filenames are displayable, i.e. in UTF-8.
48 */
err_set(MainInfo * min,gint code,const gchar * source,const gchar * obj)49 gint err_set(MainInfo *min, gint code, const gchar *source, const gchar *obj)
50 {
51 	if(source != NULL)
52 	{
53 		if(code >= 0)
54 		{
55 			const gchar	*desc;
56 
57 			desc = g_strerror(code);
58 			if(obj != NULL)
59 				g_snprintf(error_desc, sizeof error_desc, _("Couldn't %s \"%s\": %s (code %d)"), source, obj, desc, code);
60 			else
61 				g_snprintf(error_desc, sizeof error_desc, _("Couldn't %s \"%s\" (code %d)"), source, obj, code);
62 		}
63 		else
64 		{
65 			if(obj != NULL)
66 				g_snprintf(error_desc, sizeof error_desc, _("Couldn't %s \"%s\""), source, obj);
67 			else
68 				g_snprintf(error_desc, sizeof error_desc, _("Couldn't %s"), source);
69 		}
70 	}
71 	return 1;
72 }
73 
74 /* 2009-08-06 -	Sets the error report from a GError, which is what most GIO calls will generate. This frees the GError, too. The file is not freed! */
err_set_gerror(MainInfo * min,GError ** err,const gchar * source,const GFile * file)75 void err_set_gerror(MainInfo *min, GError **err, const gchar *source, const GFile *file)
76 {
77 	if(err != NULL && *err != NULL)
78 	{
79 		/* If a file was supplied, extract the name and include in the displayed message. */
80 		if(file != NULL)
81 		{
82 			gchar	*pn = g_file_get_parse_name((GFile *) file);
83 
84 			g_snprintf(error_desc, sizeof error_desc, _("%s (%s)"), (*err)->message, pn);
85 			g_free(pn);
86 		}
87 		else
88 			g_snprintf(error_desc, sizeof error_desc, _("%s"), (*err)->message);
89 		g_error_free(*err);
90 		*err = NULL;
91 	}
92 }
93 
err_show(MainInfo * min)94 void err_show(MainInfo *min)
95 {
96 	if(error_desc[0] != '\0')
97 	{
98 		if(min->cfg.errors.display == ERR_DISPLAY_STATUSBAR)
99 		{
100 			gtk_label_set_text(GTK_LABEL(min->gui->top), error_desc);
101 			gui_events_flush();
102 		}
103 		else if(min->cfg.errors.display == ERR_DISPLAY_TITLEBAR)
104 			gui_set_main_title(min, error_desc);
105 		else if(min->cfg.errors.display == ERR_DISPLAY_DIALOG)
106 			dlg_dialog_async_new_simple(error_desc, "Error", "OK", NULL, NULL);
107 		if(min->cfg.errors.beep)
108 			gdk_beep();
109 	}
110 	else
111 		dp_show_stats(min->gui->cur_pane);
112 }
113