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