1 #include <locale.h>
2 #include <malloc.h>
3 #include <string.h>
4 #include <stdarg.h>
5 
6 #include <gtk/gtk.h>
7 
8 #include "gfio.h"
9 #include "gerror.h"
10 
on_info_bar_response(GtkWidget * widget,gint response,gpointer data)11 static void on_info_bar_response(GtkWidget *widget, gint response,
12 				 gpointer data)
13 {
14 	struct gui *ui = (struct gui *) data;
15 
16 	if (response == GTK_RESPONSE_OK) {
17 		gtk_widget_destroy(widget);
18 		ui->error_info_bar = NULL;
19 	}
20 }
21 
report_error(struct gui_entry * ge,GError * error)22 static void report_error(struct gui_entry *ge, GError *error)
23 {
24 	struct gui *ui = ge->ui;
25 
26 	if (ui->error_info_bar == NULL) {
27 		GtkWidget *container;
28 
29 		ui->error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
30 						GTK_RESPONSE_OK, NULL);
31 		g_signal_connect(ui->error_info_bar, "response", G_CALLBACK(on_info_bar_response), ui);
32 		gtk_info_bar_set_message_type(GTK_INFO_BAR(ui->error_info_bar),
33 						GTK_MESSAGE_ERROR);
34 
35 		ui->error_label = gtk_label_new(error->message);
36 		container = gtk_info_bar_get_content_area(GTK_INFO_BAR(ui->error_info_bar));
37 		gtk_container_add(GTK_CONTAINER(container), ui->error_label);
38 
39 		gtk_box_pack_start(GTK_BOX(ui->vbox), ui->error_info_bar, FALSE, FALSE, 0);
40 		gtk_widget_show_all(ui->vbox);
41 	} else {
42 		char buffer[256];
43 		snprintf(buffer, sizeof(buffer), "Failed to open file.");
44 		gtk_label_set_text(GTK_LABEL(ui->error_label), buffer);
45 	}
46 }
47 
gfio_report_error(struct gui_entry * ge,const char * format,...)48 void gfio_report_error(struct gui_entry *ge, const char *format, ...)
49 {
50 	va_list args;
51 	GError *error;
52 
53 	va_start(args, format);
54 	error = g_error_new_valist(g_quark_from_string("fio"), 1, format, args);
55 	va_end(args);
56 
57 	report_error(ge, error);
58 	g_error_free(error);
59 }
60 
gfio_report_info(struct gui * ui,const char * title,const char * message)61 void gfio_report_info(struct gui *ui, const char *title, const char *message)
62 {
63 	GtkWidget *dialog, *content, *label;
64 
65 	dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(ui->window),
66 			GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
67 			GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
68 
69 	content = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
70 	label = gtk_label_new(message);
71 	gtk_container_add(GTK_CONTAINER(content), label);
72 	gtk_widget_show_all(dialog);
73 	gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
74 	gtk_dialog_run(GTK_DIALOG(dialog));
75 	gtk_widget_destroy(dialog);
76 }
77