99de2c04b741ce5802d66642da810838b74a79c4
[obnox/wireshark/wip.git] / gtk / funnel_stat.c
1 /*
2  * funnel_stat.c
3  *
4  * EPAN's funneled GUI mini-API
5  *
6  * (c) 2006, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 /*
30  * Most of the code here has been harvested from other ethereal gtk modules.
31  * most from prefs_dlg.c and about_dlg.c
32  */
33
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #include <string.h>
43
44 #include <gtk/gtk.h>
45
46 #include "../register.h"
47 #include "../timestats.h"
48 #include "compat_macros.h"
49 #include "../simple_dialog.h"
50 #include "gui_utils.h"
51 #include "dlg_utils.h"
52 #include "../file.h"
53 #include "../globals.h"
54 #include "../stat_menu.h"
55 #include "../tap_dfilter_dlg.h"
56 #include "font_utils.h"
57 #include "../stat_menu.h"
58 #include "gui_stat_menu.h"
59 #include <epan/prefs.h>
60 #include "column_prefs.h"
61 #include "prefs_dlg.h"
62
63 #include "gtkglobals.h"
64
65 #include <epan/funnel.h>
66
67 struct _funnel_text_window_t {
68         GtkWidget* win;
69     GtkWidget* txt;
70     GtkWidget* button_hbox;
71     GtkWidget* bt_close;
72     text_win_close_cb_t close_cb;
73     void* close_data;
74 };
75
76 struct _funnel_tree_window_t {
77         GtkWidget *win;
78
79 };
80
81 struct _funnel_node_t {
82     void* dummy;
83 };
84
85 static void text_window_cancel_button_cb(GtkWidget *bt _U_, gpointer data) {
86     funnel_text_window_t* tw = data;
87     
88     window_destroy(GTK_WIDGET(tw->win));
89     tw->win = NULL;
90     
91     if (tw->close_cb)
92         tw->close_cb(tw->close_data);
93 }
94
95 static void unref_text_win_cancel_bt_cb(GtkWidget *bt _U_, gpointer data) {
96     funnel_text_window_t* tw = data;
97     
98     window_destroy(GTK_WIDGET(tw->win));
99     tw->win = NULL;
100
101     if (tw->close_cb)
102         tw->close_cb(tw->close_data);
103     
104     g_free(tw);
105 }
106
107
108 static gboolean text_window_unref_del_event_cb(GtkWidget *win _U_, GdkEvent *event _U_, gpointer user_data) {
109     funnel_text_window_t* tw = user_data;
110     
111     window_destroy(GTK_WIDGET(tw->win));
112     tw->win = NULL;
113     
114     if (tw->close_cb)
115         tw->close_cb(tw->close_data);
116     
117     g_free(tw);
118     
119     return TRUE;
120 }
121
122 static gboolean text_window_delete_event_cb(GtkWidget *win _U_, GdkEvent *event _U_, gpointer user_data)
123 {
124     funnel_text_window_t* tw = user_data;
125     
126     window_destroy(GTK_WIDGET(tw->win));
127     tw->win = NULL;
128
129     if (tw->close_cb)
130         tw->close_cb(tw->close_data);
131     
132     return TRUE;
133 }
134
135 static funnel_text_window_t* new_text_window(const gchar* title) {
136     funnel_text_window_t* tw = g_malloc(sizeof(funnel_text_window_t));
137         GtkWidget *txt_scrollw, *main_vb, *hbox;
138
139     tw->close_cb = NULL;
140     tw->close_data = NULL;
141     
142     tw->win = window_new(GTK_WINDOW_TOPLEVEL,title);
143     SIGNAL_CONNECT(tw->win, "delete-event", text_window_delete_event_cb, tw);
144
145     txt_scrollw = scrolled_window_new(NULL, NULL);
146     main_vb = gtk_vbox_new(FALSE, 3);
147         gtk_container_border_width(GTK_CONTAINER(main_vb), 6);
148         gtk_container_add(GTK_CONTAINER(tw->win), main_vb);
149     
150     gtk_container_add(GTK_CONTAINER(main_vb), txt_scrollw);
151
152 #if GTK_MAJOR_VERSION < 2
153     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
154                                    GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
155     tw->txt = gtk_text_new(NULL, NULL);
156     gtk_text_set_editable(GTK_TEXT(tw->txt), FALSE);
157     gtk_text_set_word_wrap(GTK_TEXT(tw->txt), TRUE);
158     gtk_text_set_line_wrap(GTK_TEXT(tw->txt), TRUE);
159 #else
160     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw), 
161                                         GTK_SHADOW_IN);
162
163     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
164                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
165     tw->txt = gtk_text_view_new();
166     gtk_text_view_set_editable(GTK_TEXT_VIEW(tw->txt), FALSE);
167     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(tw->txt), GTK_WRAP_WORD);
168     gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(tw->txt), FALSE);
169     
170     gtk_text_view_set_left_margin(GTK_TEXT_VIEW(tw->txt), 4);
171     gtk_text_view_set_right_margin(GTK_TEXT_VIEW(tw->txt), 4);
172 #endif
173
174         hbox = gtk_hbox_new(FALSE, 0);
175     gtk_widget_show(hbox);
176         
177     tw->button_hbox = gtk_hbutton_box_new();
178         gtk_button_box_set_layout(GTK_BUTTON_BOX(tw->button_hbox), GTK_BUTTONBOX_START);
179         
180     gtk_box_pack_start(GTK_BOX(hbox), tw->button_hbox, TRUE, TRUE, 0);
181     gtk_widget_show(tw->button_hbox);
182         
183         gtk_box_pack_start(GTK_BOX(main_vb), hbox, FALSE, FALSE, 0);
184
185         tw->bt_close = gtk_button_new_with_label("Close");
186         GTK_WIDGET_SET_FLAGS(tw->bt_close, GTK_CAN_DEFAULT);
187         OBJECT_SET_DATA(hbox, "Close", tw->bt_close);
188
189         gtk_box_pack_end(GTK_BOX(hbox), tw->bt_close, FALSE, FALSE, 0);
190         gtk_widget_show(tw->bt_close);
191
192         SIGNAL_CONNECT(tw->bt_close, "clicked", text_window_cancel_button_cb, tw);
193     gtk_widget_grab_default(tw->bt_close);
194
195         gtk_container_add(GTK_CONTAINER(txt_scrollw), tw->txt);
196 #if GTK_MAJOR_VERSION >= 2
197     gtk_window_resize(GTK_WINDOW(tw->win),400,300);
198 #else
199     gtk_window_set_default_size(GTK_WINDOW(tw->win), 400, 300);
200     gtk_widget_set_usize(tw->win, 400, 300);
201 #endif
202     gtk_widget_show_all(tw->win);
203     
204     return tw;
205 }
206
207
208 static void text_window_clear(funnel_text_window_t*  tw)
209 {
210 #if GTK_MAJOR_VERSION < 2
211     GtkText *txt;
212
213     if (! tw->win) return; 
214     
215     txt = GTK_TEXT(tw->txt);
216     
217     gtk_text_set_point(txt, 0);
218     /* Keep GTK+ 1.2.3 through 1.2.6 from dumping core - see
219 http://www.ethereal.com/lists/ethereal-dev/199912/msg00312.html and
220 http://www.gnome.org/mailing-lists/archives/gtk-devel-list/1999-October/0051.shtml
221         for more information */
222     gtk_adjustment_set_value(txt->vadj, 0.0);
223     gtk_text_forward_delete(txt, gtk_text_get_length(txt));
224 #else
225     GtkTextBuffer *buf;
226
227     if (! tw->win) return; 
228
229     buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tw->txt));
230     
231     gtk_text_buffer_set_text(buf, "", 0);
232 #endif
233 }
234
235
236 static void text_window_append(funnel_text_window_t*  tw, const char *str)
237 {
238     GtkWidget *txt;
239     int nchars = strlen(str);
240 #if GTK_MAJOR_VERSION >= 2
241     GtkTextBuffer *buf;
242     GtkTextIter    iter;
243 #endif
244  
245     if (! tw->win) return; 
246
247     txt = tw->txt;
248     nchars = strlen(str);
249     
250     
251 #if GTK_MAJOR_VERSION < 2
252         gtk_text_set_point(GTK_TEXT(txt),gtk_text_get_length(GTK_TEXT(txt)));
253     gtk_text_insert(GTK_TEXT(txt), user_font_get_regular(), NULL, NULL, str, nchars);
254 #else
255     buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
256     
257     gtk_text_buffer_get_end_iter(buf, &iter);
258     gtk_widget_modify_font(GTK_WIDGET(txt), user_font_get_regular());
259     
260     if (!g_utf8_validate(str, -1, NULL))
261         printf("Invalid utf8 encoding: %s\n", str);
262     
263     gtk_text_buffer_insert(buf, &iter, str, nchars);
264 #endif
265 }
266
267
268 static void text_window_set_text(funnel_text_window_t*  tw, const gchar* text)
269 {
270     
271     if (! tw->win) return; 
272     
273 #if GTK_MAJOR_VERSION < 2
274     gtk_text_freeze(GTK_TEXT(tw->txt));
275 #endif
276
277     text_window_clear(tw);
278     text_window_append(tw, text);
279
280 #if GTK_MAJOR_VERSION < 2
281     gtk_text_thaw(GTK_TEXT(tw->txt));
282 #endif
283 }
284
285
286 static void text_window_prepend(funnel_text_window_t*  tw, const char *str _U_) {
287     GtkWidget *txt;
288     int nchars = strlen(str);
289 #if GTK_MAJOR_VERSION >= 2
290     GtkTextBuffer *buf;
291     GtkTextIter    iter;
292 #endif
293         
294     if (! tw->win) return; 
295         
296     txt = tw->txt;
297     nchars = strlen(str);
298     
299     
300 #if GTK_MAJOR_VERSION < 2
301         gtk_text_set_point(GTK_TEXT(txt),0);
302     gtk_text_insert(GTK_TEXT(txt), user_font_get_regular(), NULL, NULL, str, nchars);
303 #else
304     buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
305     
306     gtk_text_buffer_get_start_iter(buf, &iter);
307     gtk_widget_modify_font(GTK_WIDGET(txt), user_font_get_regular());
308     
309     if (!g_utf8_validate(str, -1, NULL))
310         printf("Invalid utf8 encoding: %s\n", str);
311     
312     gtk_text_buffer_insert(buf, &iter, str, nchars);
313 #endif
314 }
315
316 static const gchar* text_window_get_text(funnel_text_window_t*  tw) {
317     GtkWidget *txt;
318 #if GTK_MAJOR_VERSION >= 2
319     GtkTextBuffer *buf;
320     GtkTextIter    start;
321     GtkTextIter    end;
322 #endif
323         
324     if (! tw->win) return ""; 
325
326         txt = tw->txt;
327
328 #if GTK_MAJOR_VERSION < 2
329         /* to do */
330         return "";
331 #else
332     buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(txt));
333         gtk_text_buffer_get_start_iter(buf, &start);
334         gtk_text_buffer_get_end_iter(buf, &end);
335     
336         return gtk_text_buffer_get_text(buf, &start, &end, FALSE);
337 #endif
338 }
339
340
341         
342 static void text_window_set_close_cb(funnel_text_window_t*  tw, text_win_close_cb_t cb, void* data) {
343     tw->close_cb = cb;
344     tw->close_data = data;
345 }
346
347 static void text_window_destroy(funnel_text_window_t*  tw) {
348     if (tw->win) {
349         /*
350          * the window is still there and its callbacks refer to this data structure
351          * we need to change the callback so that they free tw.
352          */
353         SIGNAL_CONNECT(tw->bt_close, "clicked", unref_text_win_cancel_bt_cb, tw);
354         SIGNAL_CONNECT(tw->win, "delete-event", text_window_unref_del_event_cb, tw);
355     } else {
356         /*
357          * we have no window anymore a human user closed
358          * the window already just free the container
359          */
360         g_free(tw);
361     }
362 }
363
364 static void text_window_set_editable(funnel_text_window_t*  tw, gboolean editable){
365 #if GTK_MAJOR_VERSION < 2
366     gtk_text_set_editable(GTK_TEXT(tw->txt), editable);
367 #else   
368         gtk_text_view_set_editable(GTK_TEXT_VIEW(tw->txt), editable);
369         gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(tw->txt), editable);
370 #endif
371 }
372
373 static gboolean text_window_button_cb(GtkWidget *bt _U_, gpointer user_data)
374 {
375         funnel_bt_t* cbd = user_data;
376         
377         if (cbd->func) {
378                 return cbd->func(cbd->tw,cbd->data);
379         } else {
380                 return TRUE;
381         }
382 }
383
384 static void text_window_add_button(funnel_text_window_t*  tw, funnel_bt_t* cbd, const gchar* label) {
385         GtkWidget *button;
386         
387         cbd->tw = tw;
388         
389         button = gtk_button_new_with_label(label);
390         GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
391         
392         gtk_box_pack_start(GTK_BOX(tw->button_hbox), button, FALSE, FALSE, 0);
393         
394         gtk_widget_show(button);
395         SIGNAL_CONNECT(button, "clicked", text_window_button_cb, cbd);
396
397 }
398
399
400 struct _funnel_dlg_data {
401     GtkWidget* win;
402     GPtrArray* entries;
403     funnel_dlg_cb_t dlg_cb;
404     void* data;
405 };
406
407 static gboolean funnel_dlg_cb(GtkWidget *win _U_, gpointer user_data)
408 {
409     struct _funnel_dlg_data* dd = user_data;
410     guint i;
411     guint len = dd->entries->len;
412     GPtrArray* returns = g_ptr_array_new();
413     
414     for(i=0; i<len; i++) {
415         GtkEntry* entry = g_ptr_array_index(dd->entries,i);
416         g_ptr_array_add(returns,g_strdup(gtk_entry_get_text(entry)));
417     }
418     
419     g_ptr_array_add(returns,NULL);
420     
421     if (dd->dlg_cb)
422         dd->dlg_cb((gchar**)returns->pdata,dd->data);
423
424     window_destroy(GTK_WIDGET(dd->win));
425
426     g_ptr_array_free(returns,FALSE);
427
428     return TRUE;
429 }
430
431 static void funnel_cancel_btn_cb(GtkWidget *bt _U_, gpointer data) {
432     GtkWidget* win = data;
433     
434     window_destroy(GTK_WIDGET(win));
435 }
436
437 static void funnel_new_dialog(const gchar* title,
438                                           const gchar** fieldnames,
439                                           funnel_dlg_cb_t dlg_cb,
440                                           void* data) {
441     GtkWidget *win, *main_tb, *main_vb, *bbox, *bt_cancel, *bt_ok;
442     guint i;
443     const gchar* fieldname;
444     struct _funnel_dlg_data* dd = g_malloc(sizeof(struct _funnel_dlg_data));
445
446     dd->entries = g_ptr_array_new();
447     dd->dlg_cb = dlg_cb;
448     dd->data = data;
449     
450     for (i=0;fieldnames[i];i++);
451
452     win = dlg_window_new(title);
453
454     dd->win = win;
455     
456 #if GTK_MAJOR_VERSION >= 2
457     gtk_window_resize(GTK_WINDOW(win),400,10*(i+2));
458 #else
459     gtk_window_set_default_size(GTK_WINDOW(win), 400, 10*(i+2));
460     gtk_widget_set_usize(win, 400, 10*(i+2));
461 #endif
462     
463     main_vb = gtk_vbox_new(TRUE,5);
464     gtk_container_add(GTK_CONTAINER(win), main_vb);
465         gtk_container_border_width(GTK_CONTAINER(main_vb), 6);
466
467     main_tb = gtk_table_new(i+1, 2, FALSE);
468     gtk_box_pack_start(GTK_BOX(main_vb), main_tb, FALSE, FALSE, 0);
469     gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
470     gtk_table_set_col_spacings(GTK_TABLE(main_tb), 15);
471     
472     for (i = 0; (fieldname = fieldnames[i]) ; i++) {
473         GtkWidget *entry, *label;
474         
475         label = gtk_label_new(fieldname);
476         gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
477         gtk_table_attach_defaults(GTK_TABLE(main_tb), label, 0, 1, i+1, i + 2);
478         gtk_widget_show(label);
479
480         entry = gtk_entry_new();
481         g_ptr_array_add(dd->entries,entry);
482         gtk_table_attach_defaults(GTK_TABLE(main_tb), entry, 1, 2, i+1, i + 2);
483         gtk_widget_show(entry);
484     }
485
486     bbox = dlg_button_row_new(GTK_STOCK_CANCEL,GTK_STOCK_OK, NULL);
487         gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
488     
489     bt_ok = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
490     SIGNAL_CONNECT(bt_ok, "clicked", funnel_dlg_cb, dd);
491     gtk_widget_grab_default(bt_ok);
492     
493     bt_cancel = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
494     SIGNAL_CONNECT(bt_cancel, "clicked", funnel_cancel_btn_cb, win);
495     gtk_widget_grab_default(bt_cancel);
496     
497     gtk_widget_show(main_tb);
498     gtk_widget_show(main_vb);
499     gtk_widget_show(win);
500 }
501
502
503 /* XXX: finish this */
504 static void funnel_logger(const gchar *log_domain _U_,
505                           GLogLevelFlags log_level _U_,
506                           const gchar *message,
507                           gpointer user_data _U_) {
508     fputs(message,stderr);
509 }
510
511 static void funnel_retap_packets(void) {
512         cf_retap_packets(&cfile, FALSE);
513 }
514
515
516 static const funnel_ops_t funnel_ops = {
517     new_text_window,
518     text_window_set_text,
519     text_window_append,
520     text_window_prepend,
521     text_window_clear,
522     text_window_get_text,
523     text_window_set_close_cb,
524         text_window_set_editable,
525     text_window_destroy,
526         text_window_add_button,
527     /*...,*/
528     funnel_new_dialog,
529     funnel_logger,
530         funnel_retap_packets
531 };
532
533
534 typedef struct _menu_cb_t {
535     void (*callback)(gpointer);
536     void* callback_data;
537     gboolean retap;
538 } menu_cb_t;
539
540 static void our_menu_callback(void* unused _U_, gpointer data) {
541     menu_cb_t* mcb = data;
542     mcb->callback(mcb->callback_data);
543     if (mcb->retap) cf_retap_packets(&cfile, FALSE);
544 }
545
546 static void register_menu_cb(const char *name,
547                              REGISTER_STAT_GROUP_E group,
548                              void (*callback)(gpointer),
549                              gpointer callback_data,
550                              gboolean retap) {
551     menu_cb_t* mcb = g_malloc(sizeof(menu_cb_t));
552
553     mcb->callback = callback;
554     mcb->callback_data = callback_data;
555     mcb->retap = retap;
556     
557     register_stat_menu_item(name, group, our_menu_callback, NULL, NULL, mcb);
558
559 }
560
561 void initialize_funnel_ops(void) {
562     funnel_set_funnel_ops(&funnel_ops);
563 }
564
565 void
566 register_tap_listener_gtkfunnel(void)
567 {
568     funnel_register_all_menus(register_menu_cb);
569 }