merge_all_tap_menus() has been moved to menus.c.
[obnox/wireshark/wip.git] / gtk / ansi_a_stat.c
1 /* ansi_a_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$
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  * This TAP provides statistics for the ANSI A-Interface:
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 <epan/stat_cmd_args.h>
44 #include <epan/tap.h>
45 #include <epan/dissectors/packet-bssap.h>
46 #include <epan/dissectors/packet-ansi_a.h>
47
48 #include "../stat_menu.h"
49 #include "../simple_dialog.h"
50
51 #include "gtk/gui_stat_menu.h"
52 #include "gtk/dlg_utils.h"
53 #include "gtk/filter_dlg.h"
54 #include "gtk/gui_utils.h"
55
56 #include "gtk/old-gtk-compat.h"
57
58 enum
59 {
60    IEI_COLUMN,
61    MSG_NAME_COLUMN,
62    COUNT_COLUMN,
63    N_COLUMN /* The number of columns */
64 };
65
66 typedef struct _ansi_a_stat_dlg_t {
67     GtkWidget           *win;
68     GtkWidget           *scrolled_win;
69     GtkWidget           *table;
70 } ansi_a_stat_dlg_t;
71
72 typedef struct _ansi_a_stat_t {
73     int                 bsmap_message_type[0xff];
74     int                 dtap_message_type[0xff];
75 } ansi_a_stat_t;
76
77
78 static ansi_a_stat_dlg_t        dlg_bsmap;
79 static ansi_a_stat_dlg_t        dlg_dtap;
80 static ansi_a_stat_t            ansi_a_stat;
81
82
83 static void
84 ansi_a_stat_reset(
85     void                *tapdata)
86 {
87     ansi_a_stat_t       *stat_p = tapdata;
88
89     memset(stat_p, 0, sizeof(ansi_a_stat_t));
90 }
91
92
93 static gboolean
94 ansi_a_stat_packet(
95     void                *tapdata,
96     packet_info         *pinfo _U_,
97     epan_dissect_t      *edt _U_,
98     const void          *data)
99 {
100     ansi_a_stat_t       *stat_p = tapdata;
101     const ansi_a_tap_rec_t      *data_p = data;
102
103     switch (data_p->pdu_type)
104     {
105     case BSSAP_PDU_TYPE_BSMAP:
106         stat_p->bsmap_message_type[data_p->message_type]++;
107         break;
108
109     case BSSAP_PDU_TYPE_DTAP:
110         stat_p->dtap_message_type[data_p->message_type]++;
111         break;
112
113     default:
114         /*
115          * unknown PDU type !!!
116          */
117         return(FALSE);
118     }
119
120     return(TRUE);
121 }
122
123
124 static void
125 ansi_a_stat_draw(
126     void           *tapdata)
127 {
128     ansi_a_stat_t  *stat_p = tapdata;
129     int             i;
130     GtkListStore   *list_store;
131     GtkTreeIter     iter;
132
133     if (dlg_bsmap.win && tapdata)
134     {
135         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg_bsmap.table))); /* Get store */
136
137         i = 0;
138         while (ansi_a_bsmap_strings[i].strptr){
139             /* Creates a new row at position. iter will be changed to point to this new row.
140              * If position is larger than the number of rows on the list, then the new row will be appended to the list.
141              * The row will be filled with the values given to this function.
142              * :
143              * should generally be preferred when inserting rows in a sorted list store.
144              */
145             gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
146                                  IEI_COLUMN, ansi_a_bsmap_strings[i].value,
147                                  MSG_NAME_COLUMN, (char *)ansi_a_bsmap_strings[i].strptr,
148                                  COUNT_COLUMN, stat_p->bsmap_message_type[ansi_a_bsmap_strings[i].value],
149                                  -1);
150             i++;
151         }
152     }
153
154     if (dlg_dtap.win && tapdata){
155         i = 0;
156         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (dlg_dtap.table))); /* Get store */
157
158
159         while (ansi_a_dtap_strings[i].strptr){
160             /* Creates a new row at position. iter will be changed to point to this new row.
161              * If position is larger than the number of rows on the list, then the new row will be appended to the list.
162              * The row will be filled with the values given to this function.
163              * :
164              * should generally be preferred when inserting rows in a sorted list store.
165              */
166             gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
167                                  IEI_COLUMN, ansi_a_dtap_strings[i].value,
168                                  MSG_NAME_COLUMN, (char *)ansi_a_dtap_strings[i].strptr,
169                                  COUNT_COLUMN, stat_p->dtap_message_type[ansi_a_dtap_strings[i].value],
170                                  -1);
171
172             i++;
173         }
174     }
175 }
176
177 static void
178 ansi_a_stat_gtk_win_destroy_cb(
179     GtkWindow   *win _U_,
180     gpointer     user_data )
181 {
182     memset((void *) user_data, 0, sizeof(ansi_a_stat_dlg_t));
183 }
184
185
186 /* Create list */
187 static
188 GtkWidget* create_list(void)
189 {
190
191     GtkListStore *list_store;
192     GtkWidget *list;
193     GtkTreeViewColumn *column;
194     GtkCellRenderer *renderer;
195     GtkTreeSortable *sortable;
196     GtkTreeView     *list_view;
197     GtkTreeSelection  *selection;
198
199     /* Create the store */
200     list_store = gtk_list_store_new(N_COLUMN,           /* Total number of columns XXX  */
201                                     G_TYPE_UINT,        /* IEI                          */
202                                     G_TYPE_STRING,      /* Message Name                 */
203                                     G_TYPE_UINT);       /* Count                        */
204
205     /* Create a view */
206     list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
207
208     list_view = GTK_TREE_VIEW(list);
209     sortable = GTK_TREE_SORTABLE(list_store);
210
211     /* Speed up the list display */
212     gtk_tree_view_set_fixed_height_mode(list_view, TRUE);
213
214     /* Setup the sortable columns */
215     gtk_tree_sortable_set_sort_column_id(sortable, IEI_COLUMN, GTK_SORT_ASCENDING);
216     gtk_tree_view_set_headers_clickable(list_view, FALSE);
217
218     /* The view now holds a reference.  We can get rid of our own reference */
219     g_object_unref (G_OBJECT (list_store));
220
221     /*
222      * Create the first column packet, associating the "text" attribute of the
223      * cell_renderer to the first column of the model
224      */
225     renderer = gtk_cell_renderer_text_new ();
226     column = gtk_tree_view_column_new_with_attributes ("IEI",  renderer,
227                                                        "text", IEI_COLUMN,
228                                                        NULL);
229
230     gtk_tree_view_column_set_cell_data_func(column, renderer, present_as_hex_func,
231                                             GINT_TO_POINTER(IEI_COLUMN), NULL);
232
233     gtk_tree_view_column_set_sort_column_id(column, IEI_COLUMN);
234     gtk_tree_view_column_set_resizable(column, TRUE);
235     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
236     gtk_tree_view_column_set_min_width(column, 50);
237
238     /* Add the column to the view. */
239     gtk_tree_view_append_column (list_view, column);
240
241     /* Second column.. Message Name. */
242     renderer = gtk_cell_renderer_text_new ();
243     column = gtk_tree_view_column_new_with_attributes ("Message Name", renderer,
244                                                        "text", MSG_NAME_COLUMN,
245                                                        NULL);
246     gtk_tree_view_column_set_sort_column_id(column, MSG_NAME_COLUMN);
247     gtk_tree_view_column_set_resizable(column, TRUE);
248     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
249     gtk_tree_view_column_set_min_width(column, 280);
250     gtk_tree_view_append_column (list_view, column);
251
252     /* Third column.. Count. */
253     renderer = gtk_cell_renderer_text_new ();
254     column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
255                                                        "text", COUNT_COLUMN,
256                                                        NULL);
257
258
259     gtk_tree_view_column_set_sort_column_id(column, COUNT_COLUMN);
260     gtk_tree_view_column_set_resizable(column, TRUE);
261     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
262     gtk_tree_view_column_set_min_width(column, 50);
263     gtk_tree_view_append_column (list_view, column);
264
265     /* Now enable the sorting of each column */
266     gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view), TRUE);
267     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(list_view), TRUE);
268
269     /* Setup the selection handler */
270     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
271     gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
272
273     return list;
274
275 }
276 static void
277 ansi_a_stat_gtk_win_create(
278     ansi_a_stat_dlg_t   *dlg_p,
279     const char          *title)
280 {
281     GtkWidget           *vbox;
282     GtkWidget           *bt_close;
283     GtkWidget           *bbox;
284
285
286     dlg_p->win= dlg_window_new(title);  /* transient_for top_level */
287     gtk_window_set_destroy_with_parent (GTK_WINDOW(dlg_p->win), TRUE);
288
289     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 480, 450);
290
291     vbox=gtk_vbox_new(FALSE, 3);
292     gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
293     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
294
295     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
296     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
297
298     dlg_p->table = create_list();
299     gtk_widget_show(dlg_p->table);
300
301
302     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
303
304     /* Button row. */
305     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
306     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
307
308     bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
309     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
310
311     g_signal_connect(dlg_p->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
312     g_signal_connect(dlg_p->win, "destroy", G_CALLBACK(ansi_a_stat_gtk_win_destroy_cb), dlg_p);
313
314     gtk_widget_show_all(dlg_p->win);
315     window_present(dlg_p->win);
316 }
317
318 #ifdef MAIN_MENU_USE_UIMANAGER
319 void
320 ansi_a_stat_gtk_bsmap_cb(GtkAction *action _U_, gpointer user_data _U_)
321 #else
322 static void
323 ansi_a_stat_gtk_bsmap_cb(
324     GtkWidget   *w _U_,
325     gpointer     d _U_)
326 #endif
327 {
328     /*
329      * if the window is already open, bring it to front
330      */
331     if (dlg_bsmap.win)
332     {
333         gdk_window_raise(gtk_widget_get_window(dlg_bsmap.win));
334         return;
335     }
336
337     ansi_a_stat_gtk_win_create(&dlg_bsmap, "ANSI A-I/F BSMAP Statistics");
338     ansi_a_stat_draw(&ansi_a_stat);
339 }
340
341
342 static void
343 ansi_a_stat_gtk_bsmap_init(
344     const char  *optarg _U_, void* userdata _U_)
345 {
346     ansi_a_stat_gtk_bsmap_cb(NULL, NULL);
347 }
348
349 #ifdef MAIN_MENU_USE_UIMANAGER
350 void
351 ansi_a_stat_gtk_dtap_cb(GtkAction *action _U_, gpointer user_data _U_)
352 #else
353 static void
354 ansi_a_stat_gtk_dtap_cb(
355     GtkWidget   *w _U_,
356     gpointer     d _U_)
357 #endif
358 {
359
360     /*
361      * if the window is already open, bring it to front
362      */
363     if (dlg_dtap.win)
364     {
365         gdk_window_raise(gtk_widget_get_window(dlg_dtap.win));
366         return;
367     }
368
369     ansi_a_stat_gtk_win_create(&dlg_dtap, "ANSI A-I/F DTAP Statistics");
370     ansi_a_stat_draw(&ansi_a_stat);
371 }
372
373
374 static void
375 ansi_a_stat_gtk_dtap_init(
376     const char     *optarg _U_,
377     void* userdata  _U_)
378 {
379     ansi_a_stat_gtk_dtap_cb(NULL, NULL);
380 }
381
382
383 void
384 register_tap_listener_gtkansi_a_stat(void)
385 {
386     GString         *err_p;
387
388
389     memset((void *) &ansi_a_stat, 0, sizeof(ansi_a_stat_t));
390
391     err_p =
392         register_tap_listener("ansi_a", &ansi_a_stat, NULL, 0,
393                               ansi_a_stat_reset,
394                               ansi_a_stat_packet,
395                               ansi_a_stat_draw);
396
397     if (err_p != NULL)
398     {
399         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_p->str);
400         g_string_free(err_p, TRUE);
401
402         exit(1);
403     }
404
405 #ifdef MAIN_MENU_USE_UIMANAGER
406 #else
407     register_stat_menu_item("_ANSI/A-Interface BSMAP", REGISTER_STAT_GROUP_TELEPHONY,
408                             ansi_a_stat_gtk_bsmap_cb, NULL, NULL ,NULL);
409     register_stat_menu_item("_ANSI/A-Interface DTAP", REGISTER_STAT_GROUP_TELEPHONY,
410                             ansi_a_stat_gtk_dtap_cb, NULL, NULL ,NULL);
411 #endif
412     register_stat_cmd_arg("ansi_a,bsmap", ansi_a_stat_gtk_bsmap_init,NULL);
413
414     register_stat_cmd_arg("ansi_a,dtap", ansi_a_stat_gtk_dtap_init, NULL);
415 }