blob: 43bdabae2839f8a23ce052bbe1962728a0c297d5 [file] [log] [blame]
Jens Axboe41666582012-03-21 10:25:29 +01001#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
11static void on_info_bar_response(GtkWidget *widget, gint response,
Jens Axboe3c3ed072012-03-27 09:12:39 +020012 gpointer data)
Jens Axboe41666582012-03-21 10:25:29 +010013{
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
22static void report_error(struct gui_entry *ge, GError *error)
23{
24 struct gui *ui = ge->ui;
25
26 if (ui->error_info_bar == NULL) {
Jens Axboecab24402013-04-18 05:22:37 -070027 GtkWidget *container;
28
Jens Axboe41666582012-03-21 10:25:29 +010029 ui->error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
Jens Axboe3c3ed072012-03-27 09:12:39 +020030 GTK_RESPONSE_OK, NULL);
Jens Axboe41666582012-03-21 10:25:29 +010031 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),
Jens Axboe3c3ed072012-03-27 09:12:39 +020033 GTK_MESSAGE_ERROR);
34
Jens Axboe41666582012-03-21 10:25:29 +010035 ui->error_label = gtk_label_new(error->message);
Jens Axboecab24402013-04-18 05:22:37 -070036 container = gtk_info_bar_get_content_area(GTK_INFO_BAR(ui->error_info_bar));
Jens Axboe41666582012-03-21 10:25:29 +010037 gtk_container_add(GTK_CONTAINER(container), ui->error_label);
Jens Axboe3c3ed072012-03-27 09:12:39 +020038
Jens Axboe41666582012-03-21 10:25:29 +010039 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.");
Jens Axboef762cef2012-03-26 13:51:11 +020044 gtk_label_set_text(GTK_LABEL(ui->error_label), buffer);
Jens Axboe41666582012-03-21 10:25:29 +010045 }
46}
47
48void 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
Jens Axboe1252d8f2012-03-21 11:13:31 +010061void gfio_report_info(struct gui *ui, const char *title, const char *message)
62{
63 GtkWidget *dialog, *content, *label;
Jens Axboe41666582012-03-21 10:25:29 +010064
Jens Axboe1252d8f2012-03-21 11:13:31 +010065 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}