summaryrefslogtreecommitdiffstats
path: root/src/gui-wizard-gtk/main.c
blob: 707cf5631e249b7115d4cf1e12ebbd5072dfc317 (plain)
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
#include <gtk/gtk.h>
#include "abrtlib.h"
#include "parse_options.h"
#include "wizard.h"

#define PROGNAME "bug-reporting-wizard"

char *g_glade_file = NULL;
char *g_dump_dir_name = NULL;
char *g_analyze_label_selected = NULL;
char *g_analyze_events = NULL;
char *g_report_events = NULL;
crash_data_t *g_cd;


static void analyze_rb_was_toggled(GtkToggleButton *button, gpointer user_data)
{
    const char *label = gtk_button_get_label(GTK_BUTTON(button));
    if (label)
    {
        free(g_analyze_label_selected);
        g_analyze_label_selected = xstrdup(label);
    }
}

void remove_child_widget(GtkWidget *widget, gpointer container)
{
    /*destroy will safely remove it and free the memory if there are no refs
     * left
     */
    gtk_widget_destroy(widget);
}

static GtkWidget *add_event_buttons(GtkBox *box, char *event_name, GCallback func, bool radio)
{
VERB2 log("removing all buttons from box %p", box);
    gtk_container_foreach(GTK_CONTAINER(box), &remove_child_widget, box);

    GtkWidget *first_button = NULL;
    while (event_name[0])
    {
        char *event_name_end = strchr(event_name, '\n');
        *event_name_end = '\0';

VERB2 log("adding button '%s' to box %p", event_name, box);
        GtkWidget *button = radio
                ? gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(first_button), event_name)
                : gtk_check_button_new_with_label(event_name);
        if (!first_button)
            first_button = button;

        *event_name_end = '\n';
        event_name = event_name_end + 1;

        gtk_box_pack_start(box, button, /*expand*/ false, /*fill*/ false, /*padding*/ 0);

        if (func)
            g_signal_connect(G_OBJECT(button), "toggled", func, NULL);
    }
    return first_button;
}

static void append_item_to_details_ls(gpointer name, gpointer value, gpointer data)
{
    crash_item *item = (crash_item*)value;
    GtkTreeIter iter;

    gtk_list_store_append(g_ls_details, &iter);

    //FIXME: use the value representation here
    /* If text and not multiline... */
    if ((item->flags & CD_FLAG_TXT) && !strchr(item->content, '\n'))
    {
        gtk_list_store_set(g_ls_details, &iter,
                              DETAIL_COLUMN_NAME, (char *)name,
                              DETAIL_COLUMN_VALUE, item->content,
                              //DETAIL_COLUMN_PATH, xasprintf("%s%s", g_dump_dir_name, name),
                              -1);
    }
    else
    {
        gtk_list_store_set(g_ls_details, &iter,
                              DETAIL_COLUMN_NAME, (char *)name,
                              DETAIL_COLUMN_VALUE, _("(click here to view/edit)"),
                              //DETAIL_COLUMN_PATH, xasprintf("%s%s", g_dump_dir_name, name),
                              -1);
        //WARNING: will leak xasprintf results above if uncommented
    }
}

void reload_dump_dir(void)
{
    free_crash_data(g_cd);
    free(g_analyze_events);
    free(g_report_events);

    struct dump_dir *dd = dd_opendir(g_dump_dir_name, 0);
    if (!dd)
        xfunc_die(); /* dd_opendir already logged error msg */
    g_cd = create_crash_data_from_dump_dir(dd);
    g_analyze_events = list_possible_events(dd, g_dump_dir_name, "analyze");
    g_report_events = list_possible_events(dd, g_dump_dir_name, "report");
VERB2 log("g_analyze_events:'%s'", g_analyze_events);
VERB2 log("g_report_events:'%s'", g_report_events);
    dd_close(dd);

    const char *reason = get_crash_item_content_or_NULL(g_cd, FILENAME_REASON);
    gtk_label_set_text(g_lbl_cd_reason, reason ? reason : _("(no description)"));

    gtk_list_store_clear(g_ls_details);
    g_hash_table_foreach(g_cd, append_item_to_details_ls, NULL);

    GtkTextBuffer *backtrace_buf = gtk_text_view_get_buffer(g_tv_backtrace);
    const char *bt = g_cd ? get_crash_item_content_or_NULL(g_cd, FILENAME_BACKTRACE) : NULL;
    gtk_text_buffer_set_text(backtrace_buf, bt ? bt : "", -1);

//Doesn't work: shows empty page
//    if (!g_analyze_events[0])
//    {
//        /* No available analyze events, go to reporter selector page */
//        gtk_assistant_set_current_page(GTK_ASSISTANT(assistant), PAGENO_REPORTER_SELECTOR);
//    }

    GtkWidget *first_rb = add_event_buttons(g_box_analyzers, g_analyze_events, G_CALLBACK(analyze_rb_was_toggled), /*radio:*/ true);
    if (first_rb)
    {
        const char *label = gtk_button_get_label(GTK_BUTTON(first_rb));
        if (label)
        {
            free(g_analyze_label_selected);
            g_analyze_label_selected = xstrdup(label);
        }
    }

    add_event_buttons(g_box_reporters, g_report_events, /*callback:*/ NULL, /*radio:*/ false);

    /* We can't just do gtk_widget_show_all once in main:
     * We created new widgets (buttons). Need to make them visible.
     */
    gtk_widget_show_all(GTK_WIDGET(g_assistant));
}

int main(int argc, char **argv)
{
    gtk_init(&argc, &argv);

    char *env_verbose = getenv("ABRT_VERBOSE");
    if (env_verbose)
        g_verbose = atoi(env_verbose);

    /* Can't keep these strings/structs static: _() doesn't support that */
    const char *program_usage_string = _(
        PROGNAME" [-v] [-g GUI_FILE] DIR\n\n"
        "GUI tool to analyze and report ABRT crash in specified DIR"
    );
    enum {
        OPT_v = 1 << 0,
        OPT_g = 1 << 1,
    };
    /* Keep enum above and order of options below in sync! */
    struct options program_options[] = {
        OPT__VERBOSE(&g_verbose),
        OPT_STRING('g', NULL, &g_glade_file, "FILE" , _("Alternate GUI file")),
        OPT_END()
    };

    /*unsigned opts =*/ parse_opts(argc, argv, program_options, program_usage_string);

    putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose));

    argv += optind;
    if (!argv[0] || argv[1]) /* zero or >1 arguments */
        show_usage_and_die(program_usage_string, program_options);

    g_dump_dir_name = argv[0];

    create_assistant();

    reload_dump_dir();

    /* Enter main loop */
    gtk_main();

    return 0;
}