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