Most taps that now include "tap_menu.h" no longer need to include
[obnox/wireshark/wip.git] / gtk / ansi_map_stat.c
1 /* ansi_map_stat.c
2  *
3  * Copyright 2003, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * MUCH code modified from service_response_time_table.c.
7  *
8  * $Id: ansi_map_stat.c,v 1.16 2004/02/11 04:28:47 guy Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
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  * This TAP provides statistics for ANSI MAP:
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <gtk/gtk.h>
38 #include <string.h>
39
40 #include "epan/packet_info.h"
41 #include "epan/epan.h"
42 #include "epan/value_string.h"
43 #include "tap_menu.h"
44 #include "image/clist_ascend.xpm"
45 #include "image/clist_descend.xpm"
46 #include "simple_dialog.h"
47 #include "dlg_utils.h"
48 #include "tap.h"
49 #include "../register.h"
50 #include "../globals.h"
51 #include "filter_prefs.h"
52 #include "compat_macros.h"
53 #include "packet-ansi_map.h"
54 #include "ui_util.h"
55
56
57 typedef struct column_arrows {
58     GtkWidget           *table;
59     GtkWidget           *ascend_pm;
60     GtkWidget           *descend_pm;
61 } column_arrows;
62
63 typedef struct _ansi_map_stat_dlg_t {
64     GtkWidget           *win;
65     GtkWidget           *scrolled_win;
66     GtkWidget           *table;
67     char                *entries[3];
68 } ansi_map_stat_dlg_t;
69
70 #define N_MESSAGE_TYPES 256
71
72 typedef struct _ansi_map_stat_t {
73     int                 message_type[N_MESSAGE_TYPES];
74 } ansi_map_stat_t;
75
76 static ansi_map_stat_dlg_t      dlg;
77 static ansi_map_stat_t          stat;
78
79
80 static void
81 ansi_map_stat_reset(
82     void                *tapdata)
83 {
84     tapdata = tapdata;
85
86     memset((void *) &stat, 0, sizeof(ansi_map_stat_t));
87 }
88
89
90 static int
91 ansi_map_stat_packet(
92     void                *tapdata,
93     packet_info         *pinfo,
94     epan_dissect_t      *edt _U_,
95     void                *data)
96 {
97     ansi_map_tap_rec_t  *data_p = data;
98
99
100     tapdata = tapdata;
101     pinfo = pinfo;
102
103 #if 0   /* always false because message_type is 8 bit value */
104     if (data_p->message_type >= N_MESSAGE_TYPES)
105     {
106         /*
107          * unknown PDU type !!!
108          */
109         return(0);
110     }
111 #endif
112
113     stat.message_type[data_p->message_type]++;
114
115     return(1);
116 }
117
118
119 static void
120 ansi_map_stat_draw(
121     void                *tapdata)
122 {
123     int                 i, j;
124     char                str[256], *strp;
125
126
127     tapdata = tapdata;
128
129     if (dlg.win != NULL)
130     {
131         i = 0;
132
133         while (ansi_map_opr_code_strings[i].strptr)
134         {
135             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg.table), (gpointer) i);
136
137             sprintf(str, "%d",
138                 stat.message_type[ansi_map_opr_code_strings[i].value]);
139             strp = g_strdup(str);
140             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 2, strp);
141             g_free(strp);
142
143             i++;
144         }
145
146         gtk_clist_sort(GTK_CLIST(dlg.table));
147     }
148 }
149
150
151 static void
152 ansi_map_stat_gtk_click_column_cb(
153     GtkCList            *clist,
154     gint                column,
155     gpointer            data)
156 {
157     column_arrows       *col_arrows = (column_arrows *) data;
158     int                 i;
159
160
161     gtk_clist_freeze(clist);
162
163     for (i=0; i < 3; i++)
164     {
165         gtk_widget_hide(col_arrows[i].ascend_pm);
166         gtk_widget_hide(col_arrows[i].descend_pm);
167     }
168
169     if (column == clist->sort_column)
170     {
171         if (clist->sort_type == GTK_SORT_ASCENDING)
172         {
173             clist->sort_type = GTK_SORT_DESCENDING;
174             gtk_widget_show(col_arrows[column].descend_pm);
175         }
176         else
177         {
178             clist->sort_type = GTK_SORT_ASCENDING;
179             gtk_widget_show(col_arrows[column].ascend_pm);
180         }
181     }
182     else
183     {
184         /*
185          * Columns 0-1 sorted in descending order by default
186          * Columns 2 sorted in ascending order by default
187          */
188         if (column <= 1)
189         {
190             clist->sort_type = GTK_SORT_ASCENDING;
191             gtk_widget_show(col_arrows[column].ascend_pm);
192         }
193         else
194         {
195             clist->sort_type = GTK_SORT_DESCENDING;
196             gtk_widget_show(col_arrows[column].descend_pm);
197         }
198
199         gtk_clist_set_sort_column(clist, column);
200     }
201
202     gtk_clist_thaw(clist);
203     gtk_clist_sort(clist);
204 }
205
206
207 static gint
208 ansi_map_stat_gtk_sort_column(
209     GtkCList            *clist,
210     gconstpointer       ptr1,
211     gconstpointer       ptr2)
212 {
213     GtkCListRow         *row1 = (GtkCListRow *) ptr1;
214     GtkCListRow         *row2 = (GtkCListRow *) ptr2;
215     char                *text1 = NULL;
216     char                *text2 = NULL;
217     int                 i1, i2;
218
219     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
220     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
221
222     switch (clist->sort_column)
223     {
224     case 0:
225         /* FALLTHRU */
226
227     case 2:
228         i1 = strtol(text1, NULL, 0);
229         i2 = strtol(text2, NULL, 0);
230         return(i1 - i2);
231
232     case 1:
233         return(strcmp(text1, text2));
234     }
235
236     g_assert_not_reached();
237
238     return(0);
239 }
240
241
242 static void
243 ansi_map_stat_gtk_dlg_close_cb(
244     GtkButton           *button _U_,
245     gpointer            user_data _U_)
246 {
247     ansi_map_stat_dlg_t *dlg_p = user_data;
248
249     gtk_grab_remove(GTK_WIDGET(dlg_p->win));
250     gtk_widget_destroy(GTK_WIDGET(dlg_p->win));
251 }
252
253
254 static void
255 ansi_map_stat_gtk_win_destroy_cb(
256     GtkWindow           *win _U_,
257     gpointer            user_data _U_)
258 {
259     memset((void *) user_data, 0, sizeof(ansi_map_stat_dlg_t));
260 }
261
262
263 static void
264 ansi_map_stat_gtk_win_create(
265     ansi_map_stat_dlg_t *dlg_p,
266     char                *title)
267 {
268 #define INIT_TABLE_NUM_COLUMNS  3
269     char                *default_titles[] = { "Op Code", "Operation Name", "Count" };
270     int                 i;
271     column_arrows       *col_arrows;
272     GdkBitmap           *ascend_bm, *descend_bm;
273     GdkPixmap           *ascend_pm, *descend_pm;
274     GtkStyle            *win_style;
275     GtkWidget           *column_lb;
276     GtkWidget           *vbox;
277     GtkWidget           *bt_close;
278     GtkWidget           *bbox;
279     GtkWidget           *dialog_vbox;
280     GtkWidget           *dialog_action_area;
281
282
283     dlg_p->win = gtk_dialog_new();
284     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 500, 450);
285     gtk_window_set_title(GTK_WINDOW(dlg_p->win), title);
286     SIGNAL_CONNECT(dlg_p->win, "destroy", ansi_map_stat_gtk_win_destroy_cb, dlg_p);
287
288     dialog_vbox = GTK_DIALOG(dlg_p->win)->vbox;
289     gtk_widget_show(dialog_vbox);
290
291     dialog_action_area = GTK_DIALOG(dlg_p->win)->action_area;
292     gtk_widget_show(dialog_action_area);
293     gtk_container_set_border_width(GTK_CONTAINER(dialog_action_area), 10);
294
295     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
296         gtk_box_pack_start(GTK_BOX(dialog_action_area), bbox, FALSE, FALSE, 0);
297     gtk_widget_show(bbox);
298
299     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
300     gtk_widget_grab_default(bt_close);
301     SIGNAL_CONNECT(bt_close, "clicked", ansi_map_stat_gtk_dlg_close_cb, dlg_p);
302
303     vbox = gtk_vbox_new(FALSE, 0);
304     gtk_widget_ref(vbox);
305     OBJECT_SET_DATA_FULL(dlg_p->win, "vbox", vbox, gtk_widget_unref);
306     gtk_widget_show(vbox);
307     gtk_box_pack_start(GTK_BOX(dialog_vbox), vbox, TRUE, TRUE, 0);
308     gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
309
310     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
311     gtk_widget_ref(dlg_p->scrolled_win);
312     OBJECT_SET_DATA_FULL(dlg_p->win, "scrolled_win", dlg_p->scrolled_win,
313                          gtk_widget_unref);
314     gtk_widget_show(dlg_p->scrolled_win);
315     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
316
317     dlg_p->table = gtk_clist_new(INIT_TABLE_NUM_COLUMNS);
318     gtk_widget_ref(dlg_p->table);
319     OBJECT_SET_DATA_FULL(dlg_p->win, "table", dlg_p->table, gtk_widget_unref);
320     gtk_widget_show(dlg_p->table);
321
322     gtk_widget_show(dlg_p->win);
323
324     col_arrows =
325         (column_arrows *) g_malloc(sizeof(column_arrows) * INIT_TABLE_NUM_COLUMNS);
326
327     win_style =
328         gtk_widget_get_style(dlg_p->scrolled_win);
329
330     ascend_pm =
331         gdk_pixmap_create_from_xpm_d(dlg_p->scrolled_win->window,
332             &ascend_bm,
333             &win_style->bg[GTK_STATE_NORMAL],
334             (gchar **) clist_ascend_xpm);
335
336     descend_pm =
337         gdk_pixmap_create_from_xpm_d(dlg_p->scrolled_win->window,
338             &descend_bm,
339             &win_style->bg[GTK_STATE_NORMAL],
340             (gchar **)clist_descend_xpm);
341
342     for (i = 0; i < INIT_TABLE_NUM_COLUMNS; i++)
343     {
344         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
345
346         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
347
348         column_lb = gtk_label_new(default_titles[i]);
349
350         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
351             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
352
353         gtk_widget_show(column_lb);
354
355         col_arrows[i].ascend_pm =
356             gtk_pixmap_new(ascend_pm, ascend_bm);
357
358         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
359             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
360
361         col_arrows[i].descend_pm =
362             gtk_pixmap_new(descend_pm, descend_bm);
363
364         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
365             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
366
367         if (i == 0)
368         {
369             /* default column sorting */
370             gtk_widget_show(col_arrows[i].ascend_pm);
371         }
372
373         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
374         gtk_widget_show(col_arrows[i].table);
375     }
376     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
377
378     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), ansi_map_stat_gtk_sort_column);
379     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
380     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
381
382     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 0, 60);
383     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 1, 290);
384     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 2, 50);
385
386     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
387     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
388     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
389
390     SIGNAL_CONNECT(dlg_p->table, "click-column", ansi_map_stat_gtk_click_column_cb, col_arrows);
391 }
392
393
394 /*
395  * Never gets called ?
396  */
397 static void
398 ansi_map_stat_gtk_init(
399     char                *optarg)
400 {
401     /* does not appear to be called */
402
403     optarg = optarg;
404 }
405
406
407 static void
408 ansi_map_stat_gtk_cb(
409     GtkWidget           *w _U_,
410     gpointer            d _U_)
411 {
412     int                 i;
413     char                str[100];
414
415
416     /*
417      * if the window is already open, bring it to front
418      */
419     if (dlg.win)
420     {
421         gdk_window_raise(dlg.win->window);
422         return;
423     }
424
425     ansi_map_stat_gtk_win_create(&dlg, "ANSI MAP Operation Statistics");
426
427     i = 0;
428     while (ansi_map_opr_code_strings[i].strptr)
429     {
430         sprintf(str, "0x%02x", ansi_map_opr_code_strings[i].value);
431         dlg.entries[0] = g_strdup(str);
432
433         dlg.entries[1] = g_strdup(ansi_map_opr_code_strings[i].strptr);
434
435         dlg.entries[2] = g_strdup("0");
436
437         gtk_clist_insert(GTK_CLIST(dlg.table), i, dlg.entries);
438         gtk_clist_set_row_data(GTK_CLIST(dlg.table), i, (gpointer) i);
439
440         i++;
441     }
442
443     ansi_map_stat_draw(NULL);
444 }
445
446
447 void
448 register_tap_listener_gtkansi_map_stat(void)
449 {
450     GString             *err_p;
451
452
453     register_ethereal_tap("ansi_map,", ansi_map_stat_gtk_init);
454
455     memset((void *) &stat, 0, sizeof(ansi_map_stat_t));
456
457     err_p =
458         register_tap_listener("ansi_map", NULL, NULL,
459             ansi_map_stat_reset,
460             ansi_map_stat_packet,
461             ansi_map_stat_draw);
462
463     if (err_p != NULL)
464     {
465         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_p->str);
466         g_string_free(err_p, TRUE);
467
468         exit(1);
469     }
470 }
471
472
473 void
474 register_tap_menu_gtkansi_map_stat(void)
475 {
476     register_tap_menu_item("_Statistics/ANSI MAP Operation", ansi_map_stat_gtk_cb, NULL, NULL, NULL);
477 }