ui_callbacks.c 10.6 KB
Newer Older
1 2 3 4
#if HAVE_CONFIG_H
# include "config.h"
#endif

Cedric Roux's avatar
 
Cedric Roux committed
5 6 7 8 9
#include <stdlib.h>
#include <stdint.h>

#include <gtk/gtk.h>

10
#include "rc.h"
Cedric Roux's avatar
 
Cedric Roux committed
11

Cedric Roux's avatar
Cedric Roux committed
12 13 14
#include "socket.h"

#include "ui_notif_dlg.h"
Cedric Roux's avatar
 
Cedric Roux committed
15
#include "ui_main_screen.h"
16
#include "ui_menu_bar.h"
Cedric Roux's avatar
 
Cedric Roux committed
17 18 19 20 21
#include "ui_callbacks.h"
#include "ui_interface.h"
#include "ui_notifications.h"
#include "ui_tree_view.h"
#include "ui_signal_dissect_view.h"
winckel's avatar
winckel committed
22
#include "ui_filters.h"
Cedric Roux's avatar
 
Cedric Roux committed
23

Cedric Roux's avatar
Cedric Roux committed
24 25 26 27
#include "types.h"
#include "locate_root.h"
#include "xml_parse.h"

28
gboolean ui_callback_on_open_messages(GtkWidget *widget, GdkEvent *event, gpointer data)
Cedric Roux's avatar
 
Cedric Roux committed
29
{
30
    g_debug("Open messages event occurred");
31 32 33 34 35 36 37
    CHECK_FCT(ui_messages_open_file_chooser());

    return TRUE;
}

gboolean ui_callback_on_save_messages(GtkWidget *widget, GdkEvent *event, gpointer data)
{
38
    g_debug("Save messages event occurred");
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    // CHECK_FCT(ui_file_chooser());
    return TRUE;
}

gboolean ui_callback_on_open_filters(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    g_debug("Open filters event occurred");
    CHECK_FCT(ui_filters_open_file_chooser());
    return TRUE;
}

gboolean ui_callback_on_save_filters(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    g_debug("Save filters event occurred");
    CHECK_FCT(ui_filters_save_file_chooser());
Cedric Roux's avatar
 
Cedric Roux committed
54 55 56
    return TRUE;
}

57
gboolean ui_callback_on_about(GtkWidget *widget, GdkEvent *event, gpointer data)
Cedric Roux's avatar
 
Cedric Roux committed
58
{
59 60 61 62 63
#if defined(PACKAGE_STRING)
    ui_notification_dialog (GTK_MESSAGE_INFO, "about", "%s", PACKAGE_STRING);
#else
    ui_notification_dialog (GTK_MESSAGE_INFO, "about", "itti_analyzer");
#endif
Cedric Roux's avatar
 
Cedric Roux committed
64 65 66 67

    return TRUE;
}

68 69
gboolean ui_callback_on_select_signal(GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path,
                                      gboolean path_currently_selected, gpointer user_data)
Cedric Roux's avatar
 
Cedric Roux committed
70
{
71
    ui_text_view_t *text_view;
Cedric Roux's avatar
 
Cedric Roux committed
72 73
    GtkTreeIter iter;

74
    text_view = (ui_text_view_t *) user_data;
75 76 77

    g_assert(text_view != NULL);

78
    if (gtk_tree_model_get_iter (model, &iter, path))
Cedric Roux's avatar
 
Cedric Roux committed
79
    {
Cedric Roux's avatar
Cedric Roux committed
80 81 82
        GValue buffer_store = G_VALUE_INIT;
        gpointer buffer;

83
        gtk_tree_model_get_value (model, &iter, COL_BUFFER, &buffer_store);
Cedric Roux's avatar
Cedric Roux committed
84

85
        buffer = g_value_get_pointer (&buffer_store);
Cedric Roux's avatar
 
Cedric Roux committed
86 87 88

        if (!path_currently_selected)
        {
Cedric Roux's avatar
Cedric Roux committed
89
            /* Clear the view */
90
            CHECK_FCT_DO(ui_signal_dissect_clear_view(text_view), return FALSE);
Cedric Roux's avatar
 
Cedric Roux committed
91

92 93 94
            /* Dissect the signal */
            CHECK_FCT_DO(dissect_signal((buffer_t*)buffer, ui_signal_set_text, text_view), return FALSE);
        }
Cedric Roux's avatar
 
Cedric Roux committed
95 96 97 98
    }
    return TRUE;
}

Cedric Roux's avatar
Cedric Roux committed
99 100 101
void ui_signal_add_to_list(gpointer data, gpointer user_data)
{
    buffer_t *signal_buffer;
102 103
    GtkTreePath *path;
    GtkTreeViewColumn *focus_column;
Cedric Roux's avatar
Cedric Roux committed
104

105
    gtk_tree_view_get_cursor (GTK_TREE_VIEW(ui_main_data.signalslist), &path, &focus_column);
Cedric Roux's avatar
Cedric Roux committed
106

107
    signal_buffer = (buffer_t *) data;
Cedric Roux's avatar
Cedric Roux committed
108

109 110 111 112
    get_message_id (root, signal_buffer, &signal_buffer->message_id);

    ui_tree_view_new_signal_ind (signal_buffer->message_number, message_id_to_string (signal_buffer->message_id),
                                 get_origin_task_id (signal_buffer), get_destination_task_id (signal_buffer), data);
113 114 115

    /* Increment number of messages */
    ui_main_data.nb_message_received++;
116 117

    /* Check if no signal was selected in the list or if it was the last signal */
118
    if ((ui_main_data.path_last == NULL) || (path == NULL) || (gtk_tree_path_compare(ui_main_data.path_last, path) == 0))
119 120 121 122
    {
        /* Advance to the new last signal */
        ui_callback_signal_go_to_last (NULL, NULL, NULL);
    }
Cedric Roux's avatar
Cedric Roux committed
123 124
}

125
static gboolean ui_handle_update_signal_list(gint fd, void *data, size_t data_length)
Cedric Roux's avatar
Cedric Roux committed
126 127 128
{
    pipe_new_signals_list_message_t *signal_list_message;

129
    /* Enable buttons to move in the list of signals */
130
    ui_set_sensitive_move_buttons (TRUE);
131

132
    signal_list_message = (pipe_new_signals_list_message_t *) data;
Cedric Roux's avatar
Cedric Roux committed
133 134 135 136

    g_assert(signal_list_message != NULL);
    g_assert(signal_list_message->signal_list != NULL);

137
    g_list_foreach (signal_list_message->signal_list, ui_signal_add_to_list, NULL);
Cedric Roux's avatar
Cedric Roux committed
138

139
    /* Free the list but not user data associated with each element */
140
    g_list_free (signal_list_message->signal_list);
141
    /* Free the message */
142
    free (signal_list_message);
Cedric Roux's avatar
Cedric Roux committed
143

144 145
    ui_gtk_flush_events();

Cedric Roux's avatar
Cedric Roux committed
146 147 148 149 150 151 152
    return TRUE;
}

static gboolean ui_handle_socket_connection_failed(gint fd)
{
    GtkWidget *dialogbox;

153 154 155
    dialogbox = gtk_message_dialog_new (GTK_WINDOW(ui_main_data.window), GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
                                        "Failed to connect to provided host/ip address");
Cedric Roux's avatar
Cedric Roux committed
156

157 158
    gtk_dialog_run (GTK_DIALOG(dialogbox));
    gtk_widget_destroy (dialogbox);
Cedric Roux's avatar
Cedric Roux committed
159 160

    /* Re-enable connect button */
161
    ui_enable_connect_button ();
Cedric Roux's avatar
Cedric Roux committed
162 163 164 165 166 167 168
    return TRUE;
}

static gboolean ui_handle_socket_connection_lost(gint fd)
{
    GtkWidget *dialogbox;

169 170 171
    dialogbox = gtk_message_dialog_new (GTK_WINDOW(ui_main_data.window), GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
                                        "Connection with remote host has been lost");
Cedric Roux's avatar
Cedric Roux committed
172

173 174
    gtk_dialog_run (GTK_DIALOG(dialogbox));
    gtk_widget_destroy (dialogbox);
Cedric Roux's avatar
Cedric Roux committed
175 176

    /* Re-enable connect button */
177
    ui_enable_connect_button ();
Cedric Roux's avatar
Cedric Roux committed
178 179 180
    return TRUE;
}

181
static gboolean ui_handle_socket_xml_definition(gint fd, void *data, size_t data_length)
Cedric Roux's avatar
Cedric Roux committed
182 183 184
{
    pipe_xml_definition_message_t *xml_definition_message;

185
    xml_definition_message = (pipe_xml_definition_message_t *) data;
Cedric Roux's avatar
Cedric Roux committed
186 187 188
    g_assert(xml_definition_message != NULL);
    g_assert(data_length == sizeof(pipe_xml_definition_message_t));

189
    xml_parse_buffer (xml_definition_message->xml_definition, xml_definition_message->xml_definition_length);
Cedric Roux's avatar
Cedric Roux committed
190

191
    free (data);
Cedric Roux's avatar
Cedric Roux committed
192 193 194 195 196 197

    return TRUE;
}

gboolean ui_pipe_callback(gint source, gpointer user_data)
{
198 199 200
    void *input_data = NULL;
    size_t input_data_length = 0;
    pipe_input_header_t input_header;
Cedric Roux's avatar
Cedric Roux committed
201 202

    /* Read the header */
203 204
    if (read (source, &input_header, sizeof(input_header)) < 0)
    {
Cedric Roux's avatar
Cedric Roux committed
205 206 207 208 209 210 211
        g_warning("Failed to read from pipe %d: %s", source, g_strerror(errno));
        return FALSE;
    }

    input_data_length = input_header.message_size - sizeof(input_header);

    /* Checking for non-header part */
212 213 214
    if (input_data_length > 0)
    {
        input_data = malloc (input_data_length);
Cedric Roux's avatar
Cedric Roux committed
215

216 217
        if (read (source, input_data, input_data_length) < 0)
        {
Cedric Roux's avatar
Cedric Roux committed
218 219 220 221 222
            g_warning("Failed to read from pipe %d: %s", source, g_strerror(errno));
            return FALSE;
        }
    }

223 224
    switch (input_header.message_type)
    {
Cedric Roux's avatar
Cedric Roux committed
225
        case UI_PIPE_CONNECTION_FAILED:
226
            return ui_handle_socket_connection_failed (source);
Cedric Roux's avatar
Cedric Roux committed
227
        case UI_PIPE_XML_DEFINITION:
228
            return ui_handle_socket_xml_definition (source, input_data, input_data_length);
Cedric Roux's avatar
Cedric Roux committed
229
        case UI_PIPE_CONNECTION_LOST:
230
            return ui_handle_socket_connection_lost (source);
Cedric Roux's avatar
Cedric Roux committed
231
        case UI_PIPE_UPDATE_SIGNAL_LIST:
232
            return ui_handle_update_signal_list (source, input_data, input_data_length);
Cedric Roux's avatar
Cedric Roux committed
233
        default:
234
            g_warning("[gui] Unhandled message type %u", input_header.message_type);
Cedric Roux's avatar
Cedric Roux committed
235 236 237 238 239
            g_assert_not_reached();
    }
    return FALSE;
}

240
gboolean ui_callback_on_connect(GtkWidget *widget, GdkEvent *event, gpointer data)
Cedric Roux's avatar
 
Cedric Roux committed
241 242 243
{
    /* We have to retrieve the ip address and port of remote host */
    const char *ip;
244 245
    uint16_t port;
    int pipe_fd[2];
Cedric Roux's avatar
 
Cedric Roux committed
246

247
    g_debug("Connect event occurred");
Cedric Roux's avatar
 
Cedric Roux committed
248

winckel's avatar
winckel committed
249 250
    port = atoi (gtk_entry_get_text (GTK_ENTRY(ui_main_data.port_entry)));
    ip = gtk_entry_get_text (GTK_ENTRY(ui_main_data.ip_entry));
Cedric Roux's avatar
 
Cedric Roux committed
251

252 253
    if ((ip == NULL) || (port == 0))
    {
Cedric Roux's avatar
Cedric Roux committed
254 255 256 257 258
        g_warning("NULL parameter given for ip address or port = 0");
        /* TODO: add dialog box here */
        return FALSE;
    }

259
    ui_pipe_new (pipe_fd, ui_pipe_callback, NULL);
Cedric Roux's avatar
Cedric Roux committed
260

261
    memcpy (ui_main_data.pipe_fd, pipe_fd, sizeof(int) * 2);
Cedric Roux's avatar
Cedric Roux committed
262 263

    /* Disable the connect button */
264
    ui_disable_connect_button ();
Cedric Roux's avatar
Cedric Roux committed
265

266 267 268 269 270
    ui_callback_signal_clear_list (widget, event, data);

    if (socket_connect_to_remote_host (ip, port, pipe_fd[1]) != 0)
    {
        ui_enable_connect_button ();
Cedric Roux's avatar
Cedric Roux committed
271 272
        return FALSE;
    }
273
    ui_set_title ("%s:%d", ip, port);
Cedric Roux's avatar
 
Cedric Roux committed
274 275 276 277

    return TRUE;
}

278
gboolean ui_callback_on_disconnect(GtkWidget *widget, GdkEvent *event, gpointer data)
Cedric Roux's avatar
 
Cedric Roux committed
279 280 281
{
    /* We have to retrieve the ip address and port of remote host */

282
    g_debug("Disconnect event occurred");
Cedric Roux's avatar
Cedric Roux committed
283

284
    ui_pipe_write_message (ui_main_data.pipe_fd[0], UI_PIPE_DISCONNECT_EVT, NULL, 0);
Cedric Roux's avatar
Cedric Roux committed
285

286
    ui_enable_connect_button ();
Cedric Roux's avatar
 
Cedric Roux committed
287 288 289
    return TRUE;
}

290
gboolean ui_callback_signal_go_to(GtkWidget *widget, GdkEvent *event, gpointer data)
291
{
292
    ui_tree_view_select_row (ui_main_data.nb_message_received / 2, NULL);
293 294 295
    return TRUE;
}

296
gboolean ui_callback_signal_go_to_first(GtkWidget *widget, GdkEvent *event, gpointer data)
297
{
298
    ui_tree_view_select_row (0, NULL);
299 300 301
    return TRUE;
}

302
gboolean ui_callback_signal_go_to_last(GtkWidget *widget, GdkEvent *event, gpointer data)
303
{
304 305
    GtkTreePath *path;

306
    ui_tree_view_select_row (ui_tree_view_get_filtered_number() - 1, &path);
307 308
    ui_main_data.path_last = path;

309 310 311
    return TRUE;
}

312
gboolean ui_callback_signal_clear_list(GtkWidget *widget, GdkEvent *event, gpointer data)
313 314
{
    /* Disable buttons to move in the list of signals */
315
    ui_set_sensitive_move_buttons (FALSE);
316
    ui_set_title ("");
317

318 319
    /* Clear list of signals */
    ui_tree_view_destroy_list (ui_main_data.signalslist);
320 321 322
    return TRUE;
}

323
gboolean ui_callback_on_menu_item_selected(GtkWidget *widget, gpointer data)
324
{
winckel's avatar
winckel committed
325
    ui_filter_item_t *filter_entry = data;
winckel's avatar
winckel committed
326
    gboolean enabled;
winckel's avatar
winckel committed
327

winckel's avatar
winckel committed
328
    enabled = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM(widget));
329 330 331
    if (filter_entry->enabled != enabled)
    {
        filter_entry->enabled = enabled;
332
        ui_tree_view_refilter ();
333
    }
334
    // g_debug("ui_callback_on_menu_item_selected occurred %x %x %s %d (%d messages to display)", (int) widget, (int) data, filter_entry->name, enabled, ui_tree_view_get_filtered_number());
winckel's avatar
winckel committed
335

336 337 338
    return TRUE;
}

winckel's avatar
winckel committed
339 340 341 342
gboolean ui_callback_on_tree_column_header_click(GtkWidget *widget, gpointer data)
{
    col_type_e col = (col_type_e) data;

343
    // g_debug("ui_callback_on_tree_column_header_click %x", col);
winckel's avatar
winckel committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    switch (col)
    {
        case COL_SIGNAL:
            ui_show_filter_menu (&ui_main_data.menu_filter_messages, &ui_filters.messages);
            break;

        case COL_FROM_TASK:
            ui_show_filter_menu (&ui_main_data.menu_filter_origin_tasks, &ui_filters.origin_tasks);
            break;

        case COL_TO_TASK:
            ui_show_filter_menu (&ui_main_data.menu_filter_destination_tasks, &ui_filters.destination_tasks);
            break;

        default:
            g_warning("Unknown column filter %d in call to ui_callback_on_tree_column_header_click", col);
            return FALSE;
    }

363 364
    return TRUE;
}