827302297f0320810a3877137536960d8a694854
[obnox/wireshark/wip.git] / gtk / isup_stat.c
1 /* isup_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: isup_stat.c,v 1.17 2004/02/18 04:11:42 jmayer 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 ISUP:
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-isup.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 _isup_stat_dlg_t {
64     GtkWidget           *win;
65     GtkWidget           *scrolled_win;
66     GtkWidget           *table;
67     char                *entries[4];
68 } isup_stat_dlg_t;
69
70 #define N_MESSAGE_TYPES 256
71
72 typedef struct _isup_stat_t {
73     int                 message_type[N_MESSAGE_TYPES];
74 } isup_stat_t;
75
76
77 static isup_stat_dlg_t  dlg;
78 static isup_stat_t      stat;
79
80
81 static void
82 isup_stat_reset(
83     void                *tapdata)
84 {
85     tapdata = tapdata;
86
87     memset((void *) &stat, 0, sizeof(isup_stat_t));
88 }
89
90
91 static int
92 isup_stat_packet(
93     void                *tapdata,
94     packet_info         *pinfo,
95     epan_dissect_t      *edt _U_,
96     void                *data)
97 {
98     isup_tap_rec_t      *data_p = data;
99
100
101     tapdata = tapdata;
102     pinfo = pinfo;
103
104 #if 0   /* always false because message_type is 8 bit value */
105     if (data_p->message_type >= N_MESSAGE_TYPES)
106     {
107         /*
108          * unknown message type !!!
109          */
110         return(0);
111     }
112 #endif
113
114     stat.message_type[data_p->message_type]++;
115
116     return(1);
117 }
118
119
120 static void
121 isup_stat_draw(
122     void                *tapdata)
123 {
124     int                 i, j;
125     char                str[256], *strp;
126
127
128     tapdata = tapdata;
129
130     if (dlg.win != NULL)
131     {
132         i = 0;
133
134         while (isup_message_type_value[i].strptr)
135         {
136             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg.table), (gpointer) i);
137
138             sprintf(str, "%d",
139                 stat.message_type[isup_message_type_value[i].value]);
140             strp = g_strdup(str);
141             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 3, strp);
142             g_free(strp);
143
144             i++;
145         }
146
147         gtk_clist_sort(GTK_CLIST(dlg.table));
148     }
149 }
150
151
152 static void
153 isup_stat_gtk_click_column_cb(
154     GtkCList            *clist,
155     gint                column,
156     gpointer            data)
157 {
158     column_arrows       *col_arrows = (column_arrows *) data;
159     int                 i;
160
161
162     gtk_clist_freeze(clist);
163
164     for (i=0; i < 4; i++)
165     {
166         gtk_widget_hide(col_arrows[i].ascend_pm);
167         gtk_widget_hide(col_arrows[i].descend_pm);
168     }
169
170     if (column == clist->sort_column)
171     {
172         if (clist->sort_type == GTK_SORT_ASCENDING)
173         {
174             clist->sort_type = GTK_SORT_DESCENDING;
175             gtk_widget_show(col_arrows[column].descend_pm);
176         }
177         else
178         {
179             clist->sort_type = GTK_SORT_ASCENDING;
180             gtk_widget_show(col_arrows[column].ascend_pm);
181         }
182     }
183     else
184     {
185         /*
186          * Columns 0-2 sorted in descending order by default
187          * Columns 3 sorted in ascending order by default
188          */
189         if (column <= 2)
190         {
191             clist->sort_type = GTK_SORT_ASCENDING;
192             gtk_widget_show(col_arrows[column].ascend_pm);
193         }
194         else
195         {
196             clist->sort_type = GTK_SORT_DESCENDING;
197             gtk_widget_show(col_arrows[column].descend_pm);
198         }
199
200         gtk_clist_set_sort_column(clist, column);
201     }
202
203     gtk_clist_thaw(clist);
204     gtk_clist_sort(clist);
205 }
206
207
208 static gint
209 isup_stat_gtk_sort_column(
210     GtkCList            *clist,
211     gconstpointer       ptr1,
212     gconstpointer       ptr2)
213 {
214     const GtkCListRow           *row1 = (const GtkCListRow *) ptr1;
215     const GtkCListRow           *row2 = (const GtkCListRow *) ptr2;
216     char                *text1 = NULL;
217     char                *text2 = NULL;
218     int                 i1, i2;
219
220     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
221     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
222
223     switch (clist->sort_column)
224     {
225     case 0:
226         /* FALLTHRU */
227
228     case 3:
229         i1 = strtol(text1, NULL, 0);
230         i2 = strtol(text2, NULL, 0);
231         return(i1 - i2);
232
233     case 2:
234     case 1:
235         return(strcmp(text1, text2));
236     }
237
238     g_assert_not_reached();
239
240     return(0);
241 }
242
243
244 static void
245 isup_stat_gtk_dlg_close_cb(
246     GtkButton           *button _U_,
247     gpointer            user_data _U_)
248 {
249     isup_stat_dlg_t     *dlg_p = user_data;
250
251     gtk_grab_remove(GTK_WIDGET(dlg_p->win));
252     gtk_widget_destroy(GTK_WIDGET(dlg_p->win));
253 }
254
255
256 static void
257 isup_stat_gtk_win_destroy_cb(
258     GtkWindow           *win _U_,
259     gpointer            user_data _U_)
260 {
261     memset((void *) user_data, 0, sizeof(isup_stat_dlg_t));
262 }
263
264
265 static void
266 isup_stat_gtk_win_create(
267     isup_stat_dlg_t     *dlg_p,
268     char                *title)
269 {
270 #define INIT_TABLE_NUM_COLUMNS  4
271     char                *default_titles[] = { "ID", "Acronym", "Message Name", "Count" };
272     int                 i;
273     column_arrows       *col_arrows;
274     GdkBitmap           *ascend_bm, *descend_bm;
275     GdkPixmap           *ascend_pm, *descend_pm;
276     GtkStyle            *win_style;
277     GtkWidget           *column_lb;
278     GtkWidget           *vbox;
279     GtkWidget           *bt_close;
280     GtkWidget           *bbox;
281     GtkWidget           *dialog_vbox;
282     GtkWidget           *dialog_action_area;
283
284
285     dlg_p->win = gtk_dialog_new();
286     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 560, 450);
287     gtk_window_set_title(GTK_WINDOW(dlg_p->win), title);
288     SIGNAL_CONNECT(dlg_p->win, "destroy", isup_stat_gtk_win_destroy_cb, dlg_p);
289
290     dialog_vbox = GTK_DIALOG(dlg_p->win)->vbox;
291     gtk_widget_show(dialog_vbox);
292
293     dialog_action_area = GTK_DIALOG(dlg_p->win)->action_area;
294     gtk_widget_show(dialog_action_area);
295     gtk_container_set_border_width(GTK_CONTAINER(dialog_action_area), 10);
296
297     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
298     gtk_box_pack_start(GTK_BOX(dialog_action_area), bbox, FALSE, FALSE, 0);
299     gtk_widget_show(bbox);
300
301     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
302     SIGNAL_CONNECT(bt_close, "clicked", isup_stat_gtk_dlg_close_cb, dlg_p);
303     gtk_widget_grab_default(bt_close);
304
305     vbox = gtk_vbox_new(FALSE, 0);
306     gtk_widget_ref(vbox);
307     OBJECT_SET_DATA_FULL(dlg_p->win, "vbox", vbox, gtk_widget_unref);
308     gtk_widget_show(vbox);
309     gtk_box_pack_start(GTK_BOX(dialog_vbox), vbox, TRUE, TRUE, 0);
310     gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
311
312     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
313     gtk_widget_ref(dlg_p->scrolled_win);
314     OBJECT_SET_DATA_FULL(dlg_p->win, "scrolled_win", dlg_p->scrolled_win,
315                          gtk_widget_unref);
316     gtk_widget_show(dlg_p->scrolled_win);
317     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
318
319     dlg_p->table = gtk_clist_new(INIT_TABLE_NUM_COLUMNS);
320     gtk_widget_ref(dlg_p->table);
321     OBJECT_SET_DATA_FULL(dlg_p->win, "table", dlg_p->table, gtk_widget_unref);
322     gtk_widget_show(dlg_p->table);
323
324     gtk_widget_show(dlg_p->win);
325
326     col_arrows =
327         (column_arrows *) g_malloc(sizeof(column_arrows) * INIT_TABLE_NUM_COLUMNS);
328
329     win_style =
330         gtk_widget_get_style(dlg_p->scrolled_win);
331
332     ascend_pm =
333         gdk_pixmap_create_from_xpm_d(dlg_p->scrolled_win->window,
334             &ascend_bm,
335             &win_style->bg[GTK_STATE_NORMAL],
336             (gchar **) clist_ascend_xpm);
337
338     descend_pm =
339         gdk_pixmap_create_from_xpm_d(dlg_p->scrolled_win->window,
340             &descend_bm,
341             &win_style->bg[GTK_STATE_NORMAL],
342             (gchar **)clist_descend_xpm);
343
344     for (i = 0; i < INIT_TABLE_NUM_COLUMNS; i++)
345     {
346         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
347
348         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
349
350         column_lb = gtk_label_new(default_titles[i]);
351
352         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
353             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
354
355         gtk_widget_show(column_lb);
356
357         col_arrows[i].ascend_pm =
358             gtk_pixmap_new(ascend_pm, ascend_bm);
359
360         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
361             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
362
363         col_arrows[i].descend_pm =
364             gtk_pixmap_new(descend_pm, descend_bm);
365
366         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
367             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
368
369         if (i == 0)
370         {
371             /* default column sorting */
372             gtk_widget_show(col_arrows[i].ascend_pm);
373         }
374
375         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
376         gtk_widget_show(col_arrows[i].table);
377     }
378     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
379
380     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), isup_stat_gtk_sort_column);
381     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
382     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
383
384     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 0, 60);
385     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 1, 60);
386     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 2, 290);
387     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 3, 50);
388
389     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
390     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
391     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
392
393     SIGNAL_CONNECT(dlg_p->table, "click-column", isup_stat_gtk_click_column_cb, col_arrows);
394 }
395
396
397 /*
398  * Never gets called ?
399  */
400 static void
401 isup_stat_gtk_init(
402     char                *optarg)
403 {
404     /* does not appear to be called */
405
406     optarg = optarg;
407 }
408
409
410 static void
411 isup_stat_gtk_cb(
412     GtkWidget           *w _U_,
413     gpointer            d _U_)
414 {
415     int                 i;
416     char                str[100];
417
418
419     /*
420      * if the window is already open, bring it to front
421      */
422     if (dlg.win)
423     {
424         gdk_window_raise(dlg.win->window);
425         return;
426     }
427
428     isup_stat_gtk_win_create(&dlg, "ISUP Message Type Statistics");
429
430     i = 0;
431     while (isup_message_type_value[i].strptr)
432     {
433         sprintf(str, "%u", isup_message_type_value[i].value);
434         dlg.entries[0] = g_strdup(str);
435
436         dlg.entries[1] = g_strdup(isup_message_type_value_acro[i].strptr);
437
438         dlg.entries[2] = g_strdup(isup_message_type_value[i].strptr);
439
440         dlg.entries[3] = g_strdup("0");
441
442         gtk_clist_insert(GTK_CLIST(dlg.table), i, dlg.entries);
443         gtk_clist_set_row_data(GTK_CLIST(dlg.table), i, (gpointer) i);
444
445         i++;
446     }
447
448     isup_stat_draw(NULL);
449 }
450
451
452 void
453 register_tap_listener_gtkisup_stat(void)
454 {
455     GString             *err_p;
456
457
458     register_ethereal_tap("isup,", isup_stat_gtk_init);
459
460     memset((void *) &stat, 0, sizeof(isup_stat_t));
461
462     err_p =
463         register_tap_listener("isup", NULL, NULL,
464             isup_stat_reset,
465             isup_stat_packet,
466             isup_stat_draw);
467
468     if (err_p != NULL)
469     {
470         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_p->str);
471         g_string_free(err_p, TRUE);
472
473         exit(1);
474     }
475 }
476
477
478 void
479 register_tap_menu_gtkisup_stat(void)
480 {
481     register_tap_menu_item("_Statistics/ISUP Message Type", isup_stat_gtk_cb, NULL, NULL, NULL);
482 }