Fix the wireless settings button for AirPCap devices in the
[obnox/wireshark/wip.git] / gtk / mtp3_summary.c
1 /* mtp3_summary.c
2  * Routines for MTP3 Statictics summary window
3  *
4  * Copyright 2004, Michael Lum <mlum [AT] telostech.com>
5  * In association with Telos Technology Inc.
6  *
7  * Modified from gsm_map_summary.c
8  *
9  * $Id$
10  *
11  * Wireshark - Network traffic analyzer
12  * By Gerald Combs <gerald@wireshark.org>
13  * Copyright 1998 Gerald Combs
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 #include <string.h>
38
39 #include <gtk/gtk.h>
40
41 #include "epan/packet_info.h"
42 #include "epan/epan.h"
43 #include "epan/value_string.h"
44 #include <epan/tap.h>
45 #include <epan/dissectors/packet-mtp3.h>
46
47 #include "../stat_menu.h"
48 #include "../globals.h"
49 #include "../file.h"
50 #include "../summary.h"
51
52 #include "gtk/gui_stat_menu.h"
53 #include "gtk/dlg_utils.h"
54 #include "gtk/gui_utils.h"
55 #include "gtk/mtp3_stat.h"
56
57 #define SUM_STR_MAX 1024
58
59
60 typedef struct _my_columns_t {
61     guint32             value;
62     const gchar         *strptr;
63     GtkJustification    just;
64 } my_columns_t;
65
66 enum
67 {
68    SI_COLUMN,
69    NUM_MSUS_COLUMN,
70    NUM_MSUS_SEC_COLUMN,
71    NUM_BYTES_COLUMN,
72    NUM_BYTES_MSU_COLUMN,
73    NUM_BYTES_SEC_COLUMN,
74    N_COLUMN /* The number of columns */
75 };
76
77 /* Create list */
78 static
79 GtkWidget* create_list(void)
80 {
81
82     GtkListStore *list_store;
83     GtkWidget *list;
84     GtkTreeViewColumn *column;
85     GtkCellRenderer *renderer;
86     GtkTreeSortable *sortable;
87         GtkTreeView     *list_view;
88         GtkTreeSelection  *selection;
89
90         /* Create the store */
91     list_store = gtk_list_store_new(N_COLUMN,   /* Total number of columns XXX*/
92                                G_TYPE_STRING,   /* SI                           */
93                                G_TYPE_INT,              /* Num MSUs                     */
94                                G_TYPE_STRING,   /* MSUs/sec                     */
95                                G_TYPE_INT,              /* Num Bytes            */
96                                G_TYPE_STRING,   /* Bytes/MSU            */
97                                G_TYPE_STRING);  /* Bytes/sec            */
98
99     /* Create a view */
100     list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
101
102     list_view = GTK_TREE_VIEW(list);
103     sortable = GTK_TREE_SORTABLE(list_store);
104
105     /* Speed up the list display */
106     gtk_tree_view_set_fixed_height_mode(list_view, TRUE);
107
108     /* Setup the sortable columns */
109     gtk_tree_sortable_set_sort_column_id(sortable, SI_COLUMN, GTK_SORT_ASCENDING);
110     gtk_tree_view_set_headers_clickable(list_view, FALSE);
111
112     /* The view now holds a reference.  We can get rid of our own reference */
113     g_object_unref (G_OBJECT (list_store));
114
115     /*
116      * Create the first column packet, associating the "text" attribute of the
117      * cell_renderer to the first column of the model
118      */
119     /* 1:st column */
120     renderer = gtk_cell_renderer_text_new ();
121     column = gtk_tree_view_column_new_with_attributes ("SI", renderer,
122                 "text", SI_COLUMN,
123                 NULL);
124
125     gtk_tree_view_column_set_sort_column_id(column, SI_COLUMN);
126     gtk_tree_view_column_set_resizable(column, TRUE);
127     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
128     gtk_tree_view_column_set_min_width(column, 110);
129
130     /* Add the column to the view. */
131     gtk_tree_view_append_column (list_view, column);
132
133     /* 2:nd column... */
134     renderer = gtk_cell_renderer_text_new ();
135     column = gtk_tree_view_column_new_with_attributes ("Num MSUs", renderer,
136                 "text", NUM_MSUS_COLUMN,
137                 NULL);
138     gtk_tree_view_column_set_sort_column_id(column, NUM_MSUS_COLUMN);
139     gtk_tree_view_column_set_resizable(column, TRUE);
140     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
141     gtk_tree_view_column_set_min_width(column, 100);
142     gtk_tree_view_append_column (list_view, column);
143
144     /* 3:d column... */
145     renderer = gtk_cell_renderer_text_new ();
146     column = gtk_tree_view_column_new_with_attributes ("MSUs/sec", renderer,
147                 "text", NUM_MSUS_SEC_COLUMN,
148                 NULL);
149     gtk_tree_view_column_set_sort_column_id(column, NUM_MSUS_SEC_COLUMN);
150     gtk_tree_view_column_set_resizable(column, TRUE);
151     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
152     gtk_tree_view_column_set_min_width(column, 100);
153     gtk_tree_view_append_column (list_view, column);
154
155     /* 4:d column... */
156     renderer = gtk_cell_renderer_text_new ();
157     column = gtk_tree_view_column_new_with_attributes ("Num Bytes", renderer,
158                 "text", NUM_BYTES_COLUMN,
159                 NULL);
160     gtk_tree_view_column_set_sort_column_id(column, NUM_BYTES_COLUMN);
161     gtk_tree_view_column_set_resizable(column, TRUE);
162     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
163     gtk_tree_view_column_set_min_width(column, 100);
164     gtk_tree_view_append_column (list_view, column);
165
166     /* 5:th column... */
167     renderer = gtk_cell_renderer_text_new ();
168     column = gtk_tree_view_column_new_with_attributes ("Bytes/MSU", renderer,
169                 "text", NUM_BYTES_MSU_COLUMN,
170                 NULL);
171
172
173     gtk_tree_view_column_set_sort_column_id(column, NUM_BYTES_MSU_COLUMN);
174     gtk_tree_view_column_set_resizable(column, TRUE);
175     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
176     gtk_tree_view_column_set_min_width(column, 100);
177     gtk_tree_view_append_column (list_view, column);
178
179     /* 6:th column... */
180     renderer = gtk_cell_renderer_text_new ();
181     column = gtk_tree_view_column_new_with_attributes ("Bytes/sec", renderer,
182                 "text", NUM_BYTES_SEC_COLUMN,
183                 NULL);
184
185     gtk_tree_view_column_set_sort_column_id(column, NUM_BYTES_SEC_COLUMN);
186     gtk_tree_view_column_set_resizable(column, TRUE);
187     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
188     gtk_tree_view_column_set_min_width(column, 100);
189     gtk_tree_view_append_column (list_view, column);
190
191     /* Now enable the sorting of each column */
192     gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view), TRUE);
193     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(list_view), TRUE);
194
195     /* Setup the selection handler */
196     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
197     gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
198
199     return list;
200
201 }
202
203 static void
204 add_string_to_box(gchar *str, GtkWidget *box)
205 {
206   GtkWidget *lb;
207   lb = gtk_label_new(str);
208   gtk_misc_set_alignment(GTK_MISC(lb), 0.0f, 0.5f);
209   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
210   gtk_widget_show(lb);
211 }
212
213
214
215 static void
216 mtp3_sum_draw(
217     GtkWidget           *table,
218     double              seconds,
219     int                 *tot_num_msus_p,
220     double              *tot_num_bytes_p)
221 {
222     char                *entries[N_COLUMN];
223     int                 i, j;
224     int                 num_msus;
225     int                 num_bytes;
226     GtkListStore *list_store = NULL;
227     GtkTreeIter  iter;
228
229     *tot_num_msus_p = 0;
230     *tot_num_bytes_p = 0;
231
232     list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW (table))); /* Get store */
233
234     for (i=0; i < MTP3_NUM_SI_CODE; i++)
235     {
236         j = 0;
237         num_msus = 0;
238         num_bytes = 0;
239
240         while (j < mtp3_num_used)
241         {
242             num_msus += mtp3_stat[j].si_code[i].num_msus;
243             num_bytes += mtp3_stat[j].si_code[i].size;
244
245             j++;
246         }
247
248         *tot_num_msus_p += num_msus;
249         *tot_num_bytes_p += num_bytes;
250
251         entries[2] = (seconds) ? g_strdup_printf("%.2f", (double)num_msus/seconds) : g_strdup("N/A");
252         entries[4] = (num_msus) ? g_strdup_printf("%.2f", (double)num_bytes/num_msus) : g_strdup("N/A");
253         entries[5] = (seconds) ? g_strdup_printf("%.2f", (double)num_bytes/seconds) : g_strdup("N/A");
254
255         gtk_list_store_insert_with_values( list_store , &iter, G_MAXINT,
256            SI_COLUMN, mtp3_service_indicator_code_short_vals[i].strptr,
257            NUM_MSUS_COLUMN, num_msus,
258            NUM_MSUS_SEC_COLUMN, entries[2],
259            NUM_BYTES_COLUMN, num_bytes,
260            NUM_BYTES_MSU_COLUMN, entries[4],
261            NUM_BYTES_SEC_COLUMN, entries[5],
262            -1);
263
264         g_free(entries[2]);
265         g_free(entries[4]);
266         g_free(entries[5]);
267     }
268 }
269
270
271 #ifdef MAIN_MENU_USE_UIMANAGER
272 void mtp3_sum_gtk_sum_cb(GtkAction *action _U_, gpointer user_data _U_)
273 #else
274 static void
275 mtp3_sum_gtk_sum_cb(GtkWidget *w _U_, gpointer d _U_)
276 #endif
277 {
278   summary_tally summary;
279   GtkWidget     *sum_open_w,
280                 *main_vb, *file_fr, *data_fr, *file_box,
281                 *data_box, *bbox, *close_bt,
282                 *tot_fr, *tot_box,
283                 *table, *table_fr;
284
285   gchar                 string_buff[SUM_STR_MAX];
286   const char *  file_type;
287   double                seconds;
288   int                   tot_num_msus;
289   double                tot_num_bytes;
290
291   /* initialize the tally */
292   summary_fill_in(&cfile, &summary);
293
294   /* initial compututations */
295   seconds = summary.stop_time - summary.start_time;
296
297   sum_open_w = dlg_window_new("MTP3 Statistics: Summary");  /* transient_for top_level */
298   gtk_window_set_destroy_with_parent (GTK_WINDOW(sum_open_w), TRUE);
299
300   /* Container for each row of widgets */
301   main_vb = gtk_vbox_new(FALSE, 3);
302   gtk_container_set_border_width(GTK_CONTAINER(main_vb), 5);
303   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
304   gtk_widget_show(main_vb);
305
306   /* File frame */
307   file_fr = gtk_frame_new("File");
308   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
309   gtk_widget_show(file_fr);
310
311   file_box = gtk_vbox_new(FALSE, 3);
312   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
313   gtk_widget_show(file_box);
314
315   /* filename */
316   g_snprintf(string_buff, SUM_STR_MAX, "Name: %s", ((summary.filename) ? summary.filename : "None"));
317   add_string_to_box(string_buff, file_box);
318
319   /* length */
320   g_snprintf(string_buff, SUM_STR_MAX, "Length: %" G_GINT64_MODIFIER "d", summary.file_length);
321   add_string_to_box(string_buff, file_box);
322
323   /* format */
324   file_type = wtap_file_type_string(summary.file_type);
325   g_snprintf(string_buff, SUM_STR_MAX, "Format: %s", (file_type ? file_type : "N/A"));
326   add_string_to_box(string_buff, file_box);
327
328   if (summary.has_snap) {
329     /* snapshot length */
330     g_snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", summary.snap);
331     add_string_to_box(string_buff, file_box);
332   }
333
334   /* Data frame */
335   data_fr = gtk_frame_new("Data");
336   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
337   gtk_widget_show(data_fr);
338
339   data_box = gtk_vbox_new(FALSE, 3);
340   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
341   gtk_widget_show(data_box);
342
343   /* seconds */
344   g_snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", summary.elapsed_time);
345   add_string_to_box(string_buff, data_box);
346
347   g_snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
348   add_string_to_box(string_buff, data_box);
349
350   /* Packet count */
351   g_snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", summary.packet_count);
352   add_string_to_box(string_buff, data_box);
353
354   /* MTP3 SPECIFIC */
355   table_fr = gtk_frame_new("Service Indicator (SI) Totals");
356   gtk_container_add(GTK_CONTAINER(main_vb), table_fr);
357   gtk_widget_show(table_fr);
358
359    table = create_list();
360
361   gtk_container_add(GTK_CONTAINER(table_fr), table);
362   gtk_widget_show(table);
363
364
365   mtp3_sum_draw(table, seconds, &tot_num_msus, &tot_num_bytes);
366
367   /* Totals frame */
368   tot_fr = gtk_frame_new("Totals");
369   gtk_container_add(GTK_CONTAINER(main_vb), tot_fr);
370   gtk_widget_show(tot_fr);
371
372   tot_box = gtk_vbox_new(FALSE, 3);
373   gtk_container_add(GTK_CONTAINER(tot_fr), tot_box);
374   gtk_widget_show(tot_box);
375
376   g_snprintf(string_buff, SUM_STR_MAX, "Total MSUs: %u", tot_num_msus);
377   add_string_to_box(string_buff, tot_box);
378
379   if (seconds) {
380                 g_snprintf(string_buff, SUM_STR_MAX, "MSUs/second: %.2f", tot_num_msus/seconds);
381   }
382   else {
383                 g_snprintf(string_buff, SUM_STR_MAX, "MSUs/second: N/A");
384   }
385   add_string_to_box(string_buff, tot_box);
386
387   g_snprintf(string_buff, SUM_STR_MAX, "Total Bytes: %.0f", tot_num_bytes);
388   add_string_to_box(string_buff, tot_box);
389
390   if (tot_num_msus) {
391                 g_snprintf(string_buff, SUM_STR_MAX, "Average Bytes/MSU: %.2f", tot_num_bytes/tot_num_msus);
392   }
393   else {
394             g_snprintf(string_buff, SUM_STR_MAX, "Average Bytes/MSU: N/A");
395   }
396   add_string_to_box(string_buff, tot_box);
397
398   if (seconds) {
399           g_snprintf(string_buff, SUM_STR_MAX, "Bytes/second: %.2f", tot_num_bytes/seconds);
400   }
401   else {
402           g_snprintf(string_buff, SUM_STR_MAX, "Bytes/second: N/A");
403   }
404   add_string_to_box(string_buff, tot_box);
405
406   /* Button row. */
407   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
408   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
409   gtk_widget_show(bbox);
410
411   close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
412   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
413
414   g_signal_connect(sum_open_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
415
416   gtk_widget_show_all(sum_open_w);
417   window_present(sum_open_w);
418 }
419
420
421 void
422 register_tap_listener_gtkmtp3_summary(void)
423 {
424 #ifdef MAIN_MENU_USE_UIMANAGER
425 #else
426     register_stat_menu_item("_MTP3/MSU Summary",  REGISTER_STAT_GROUP_TELEPHONY,
427         mtp3_sum_gtk_sum_cb, NULL, NULL, NULL);
428 #endif
429 }