ui_notifications.c 8.13 KB
Newer Older
1
#include <fcntl.h>
Cedric Roux's avatar
 
Cedric Roux committed
2
3
4
5
6
7
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>

#include <gtk/gtk.h>

8
#include "itti_types.h"
9
#include "rc.h"
Cedric Roux's avatar
 
Cedric Roux committed
10
11
12

#include "ui_interface.h"
#include "ui_main_screen.h"
13
#include "ui_menu_bar.h"
Cedric Roux's avatar
 
Cedric Roux committed
14
#include "ui_notifications.h"
15
#include "ui_callbacks.h"
winckel's avatar
winckel committed
16
#include "ui_filters.h"
Cedric Roux's avatar
 
Cedric Roux committed
17

Cedric Roux's avatar
Cedric Roux committed
18
#include "xml_parse.h"
Cedric Roux's avatar
 
Cedric Roux committed
19
20
21
22

int ui_disable_connect_button(void)
{
    /* Disable Connect button and enable disconnect button */
23
24
    gtk_widget_set_sensitive (GTK_WIDGET (ui_main_data.connect), FALSE);
    gtk_widget_set_sensitive (GTK_WIDGET (ui_main_data.disconnect), TRUE);
Cedric Roux's avatar
 
Cedric Roux committed
25
26
27
28
29
30
31

    return RC_OK;
}

int ui_enable_connect_button(void)
{
    /* Disable Disconnect button and enable connect button */
32
33
    gtk_widget_set_sensitive (GTK_WIDGET (ui_main_data.connect), TRUE);
    gtk_widget_set_sensitive (GTK_WIDGET (ui_main_data.disconnect), FALSE);
Cedric Roux's avatar
 
Cedric Roux committed
34
35
36
37

    return RC_OK;
}

winckel's avatar
winckel committed
38
static void ui_change_cursor(gboolean busy)
winckel's avatar
winckel committed
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
{
    static GdkWindow *window;

    if (busy)
    {
        GdkDisplay *display;
        GdkCursor *cursor;
        // gint x, y;

        cursor = gdk_cursor_new (GDK_WATCH);
        display = gdk_display_get_default();
        window = gtk_widget_get_window(GTK_WIDGET(ui_main_data.window));
        // window = gdk_display_get_window_at_pointer(display, &x, &y);

        gdk_window_set_cursor (window, cursor);
        gdk_display_sync(display);
        gdk_cursor_unref (cursor);
        gtk_widget_set_sensitive (ui_main_data.window, FALSE);
    }
    else
    {
        gdk_window_set_cursor (window, NULL);
        gtk_widget_set_sensitive (ui_main_data.window, TRUE);
    }
}

winckel's avatar
winckel committed
65
int ui_messages_read(char *filename)
Cedric Roux's avatar
 
Cedric Roux committed
66
{
67
    int result = RC_OK;
winckel's avatar
winckel committed
68
69
70
71
72
73
    int source;
    int read_data = 0;
    void *input_data = NULL;
    size_t input_data_length = 0;
    int read_messages = 0;

winckel's avatar
winckel committed
74
75
    ui_change_cursor(TRUE);

winckel's avatar
winckel committed
76
77
    source = open (filename, O_RDONLY);
    if (source < 0)
Cedric Roux's avatar
 
Cedric Roux committed
78
    {
winckel's avatar
winckel committed
79
80
81
82
83
84
        g_warning( "Failed to open file \"%s\": %s", filename, g_strerror (errno));
        result = RC_FAIL;
    }
    else
    {
        itti_socket_header_t message_header;
Cedric Roux's avatar
 
Cedric Roux committed
85

winckel's avatar
winckel committed
86
        ui_callback_signal_clear_list (NULL, NULL, NULL);
87

winckel's avatar
winckel committed
88
        do
89
        {
winckel's avatar
winckel committed
90
            read_data = read (source, &message_header, sizeof(itti_socket_header_t));
91

winckel's avatar
winckel committed
92
93
94
95
96
97
            if (read_data == -1)
            {
                g_warning("Failed to read from file \"%s\": %s", filename, g_strerror (errno));
                result = RC_FAIL;
                break;
            }
98

winckel's avatar
winckel committed
99
            if (read_data > 0)
100
            {
winckel's avatar
winckel committed
101
                input_data_length = message_header.message_size - sizeof(itti_socket_header_t);
102

103
                // g_debug ("%x, %x ,%x", message_header.message_type, message_header.message_size, input_data_length);
104

winckel's avatar
winckel committed
105
106
                /* Checking for non-header part */
                if (input_data_length > 0)
107
                {
winckel's avatar
winckel committed
108
                    input_data = malloc (input_data_length);
109

winckel's avatar
winckel committed
110
                    if (read (source, input_data, input_data_length) < 0)
111
                    {
winckel's avatar
winckel committed
112
113
114
                        g_warning("Failed to read from file \"%s\": %s", filename, g_strerror (errno));
                        result = RC_FAIL;
                        break;
115
                    }
winckel's avatar
winckel committed
116
                }
117

winckel's avatar
winckel committed
118
119
120
                switch (message_header.message_type)
                {
                    case ITTI_DUMP_MESSAGE_TYPE:
121
                    {
winckel's avatar
winckel committed
122
123
124
125
126
127
                        itti_signal_header_t *itti_signal_header = input_data;
                        buffer_t *buffer;

                        /* Create the new buffer */
                        if (buffer_new_from_data (&buffer, input_data + sizeof(itti_signal_header_t),
                                                  input_data_length - sizeof(itti_signal_header_t), 0) != RC_OK)
128
                        {
winckel's avatar
winckel committed
129
130
                            g_error("Failed to create new buffer");
                            g_assert_not_reached ();
131
132
                        }

winckel's avatar
winckel committed
133
                        buffer->message_number = itti_signal_header->message_number;
134

winckel's avatar
winckel committed
135
136
137
                        ui_signal_add_to_list (buffer, NULL);
                        read_messages++;
                        break;
138
139
                    }

winckel's avatar
winckel committed
140
141
142
143
144
145
                    case ITTI_DUMP_XML_DEFINITION:
                        xml_parse_buffer (input_data, input_data_length);
                        break;

                    case ITTI_STATISTIC_MESSAGE_TYPE:
                    default:
146
                        g_warning("Received unknow (or not implemented) message from socket type: %d", message_header.message_type);
winckel's avatar
winckel committed
147
                        break;
148
149
                }

winckel's avatar
winckel committed
150
                free (input_data);
151
            }
winckel's avatar
winckel committed
152
        } while (read_data > 0);
153

winckel's avatar
winckel committed
154
155
156
157
        if (read_messages > 0)
        {
            /* Enable buttons to move in the list of signals */
            ui_set_sensitive_move_buttons (TRUE);
158
159
        }

160
        g_message("Read %d messages from file \"%s\"\n", read_messages, filename);
winckel's avatar
winckel committed
161
162
163
164

        close (source);
    }

winckel's avatar
winckel committed
165
166
    ui_change_cursor(FALSE);

winckel's avatar
winckel committed
167
168
169
170
171
172
    return result;
}

int ui_messages_open_file_chooser(void)
{
    int result = RC_OK;
winckel's avatar
winckel committed
173
174
175
    GtkWidget *filechooser;
    gboolean accept;
    char *filename;
winckel's avatar
winckel committed
176
177

    filechooser = gtk_file_chooser_dialog_new ("Select file", GTK_WINDOW (ui_main_data.window),
winckel's avatar
winckel committed
178
179
                                               GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
                                               GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
winckel's avatar
winckel committed
180
181

    /* Process the response */
winckel's avatar
winckel committed
182
    accept = gtk_dialog_run (GTK_DIALOG (filechooser)) == GTK_RESPONSE_ACCEPT;
winckel's avatar
winckel committed
183

winckel's avatar
winckel committed
184
185
    if (accept)
    {
winckel's avatar
winckel committed
186
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filechooser));
Cedric Roux's avatar
 
Cedric Roux committed
187
188
    }
    gtk_widget_destroy (filechooser);
winckel's avatar
winckel committed
189
190
191
192
193
    if (accept)
    {
        result = ui_messages_read (filename);
        g_free (filename);
    }
Cedric Roux's avatar
 
Cedric Roux committed
194

195
    return result;
Cedric Roux's avatar
 
Cedric Roux committed
196
197
}

198
199
200
int ui_filters_open_file_chooser(void)
{
    int result = RC_OK;
winckel's avatar
winckel committed
201
202
203
    GtkWidget *filechooser;
    gboolean accept;
    char *filename;
204
205

    filechooser = gtk_file_chooser_dialog_new ("Select file", GTK_WINDOW (ui_main_data.window),
winckel's avatar
winckel committed
206
207
                                               GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
                                               GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
208
209

    /* Process the response */
winckel's avatar
winckel committed
210
    accept = gtk_dialog_run (GTK_DIALOG (filechooser)) == GTK_RESPONSE_ACCEPT;
211

winckel's avatar
winckel committed
212
213
    if (accept)
    {
214
215
216
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filechooser));
    }
    gtk_widget_destroy (filechooser);
winckel's avatar
winckel committed
217
218
219
220
    if (accept)
    {
        g_free (filename);
    }
221

winckel's avatar
winckel committed
222
    return result;
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
}

int ui_filters_save_file_chooser(void)
{
    GtkWidget *filechooser;
    int result = RC_OK;

    filechooser = gtk_file_chooser_dialog_new ("Save file", GTK_WINDOW (ui_main_data.window),
                                               GTK_FILE_CHOOSER_ACTION_SAVE,
                                               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                               GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
                                               NULL);

    gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (filechooser), TRUE);

    //gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filechooser), "filters.xml");
    gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filechooser), "./filters.xml");

    /* Process the response */
    if (gtk_dialog_run (GTK_DIALOG (filechooser)) == GTK_RESPONSE_ACCEPT)
    {
        char *filename;

        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filechooser));

winckel's avatar
winckel committed
248
249
        result = ui_write_filters_file(filename);

250
251
252
253
254
        g_free (filename);
    }

    gtk_widget_destroy (filechooser);

winckel's avatar
winckel committed
255
    return result;
256
257
}

Cedric Roux's avatar
 
Cedric Roux committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
int ui_progress_bar_set_fraction(double fraction)
{
//     /* If not exist instantiate */
//     if (!ui_main_data.progressbar) {
//         ui_main_data.progressbar = gtk_progress_bar_new();
//     }
// 
//     gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui_main_data.progressbar), fraction);
// 
//     gtk_widget_show(ui_main_data.progressbar);
// 
//     gtk_main_iteration();

    return RC_OK;
}

int ui_progress_bar_terminate(void)
{
//     if (!ui_main_data.progressbar)
//         return RC_FAIL;
// 
//     gtk_widget_destroy(ui_main_data.progressbar);
//     ui_main_data.progressbar = NULL;

    return RC_OK;
}