Avoid calling some free() on WIN32 on memory that may be allocated in
[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
38 #include <gtk/gtk.h>
39 #include <string.h>
40
41 #include <wtap.h>
42
43 #include "epan/packet_info.h"
44 #include "epan/epan.h"
45 #include "epan/value_string.h"
46 #include "../stat_menu.h"
47 #include "gui_stat_menu.h"
48 #include "globals.h"
49 #include "file.h"
50 #include "summary.h"
51 #include "image/clist_ascend.xpm"
52 #include "image/clist_descend.xpm"
53 #include "dlg_utils.h"
54 #include "gui_utils.h"
55 #include "compat_macros.h"
56 #include <epan/tap.h>
57
58 #include <epan/dissectors/packet-mtp3.h>
59 #include "mtp3_stat.h"
60
61 #define SUM_STR_MAX 1024
62
63 typedef struct column_arrows {
64     GtkWidget           *table;
65     GtkWidget           *ascend_pm;
66     GtkWidget           *descend_pm;
67 } column_arrows;
68
69 #define MTP3_SUM_INIT_TABLE_NUM_COLUMNS         6
70
71 typedef struct _my_columns_t {
72     guint32             value;
73     const gchar         *strptr;
74     GtkJustification    just;
75 } my_columns_t;
76
77 static my_columns_t columns[MTP3_SUM_INIT_TABLE_NUM_COLUMNS] = {
78     { 110,      "SI",                   GTK_JUSTIFY_LEFT },
79     { 100,      "Num MSUs",             GTK_JUSTIFY_RIGHT },
80     { 100,      "MSUs/sec",             GTK_JUSTIFY_RIGHT },
81     { 100,      "Num Bytes",            GTK_JUSTIFY_RIGHT },
82     { 100,      "Bytes/MSU",            GTK_JUSTIFY_RIGHT },
83     { 100,      "Bytes/sec",            GTK_JUSTIFY_RIGHT }
84 };
85
86
87 static void
88 add_string_to_box(gchar *str, GtkWidget *box)
89 {
90   GtkWidget *lb;
91   lb = gtk_label_new(str);
92   gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
93   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
94   gtk_widget_show(lb);
95 }
96
97
98 static void
99 mtp3_sum_gtk_click_column_cb(
100     GtkCList            *clist,
101     gint                column,
102     gpointer            data)
103 {
104     column_arrows       *col_arrows = (column_arrows *) data;
105     int                 i;
106
107
108     gtk_clist_freeze(clist);
109
110     for (i=0; i < MTP3_SUM_INIT_TABLE_NUM_COLUMNS; i++)
111     {
112         gtk_widget_hide(col_arrows[i].ascend_pm);
113         gtk_widget_hide(col_arrows[i].descend_pm);
114     }
115
116     if (column == clist->sort_column)
117     {
118         if (clist->sort_type == GTK_SORT_ASCENDING)
119         {
120             clist->sort_type = GTK_SORT_DESCENDING;
121             gtk_widget_show(col_arrows[column].descend_pm);
122         }
123         else
124         {
125             clist->sort_type = GTK_SORT_ASCENDING;
126             gtk_widget_show(col_arrows[column].ascend_pm);
127         }
128     }
129     else
130     {
131         /*
132          * Columns 0 sorted in descending order by default
133          */
134         if (column == 0)
135         {
136             clist->sort_type = GTK_SORT_ASCENDING;
137             gtk_widget_show(col_arrows[column].ascend_pm);
138         }
139         else
140         {
141             clist->sort_type = GTK_SORT_DESCENDING;
142             gtk_widget_show(col_arrows[column].descend_pm);
143         }
144
145         gtk_clist_set_sort_column(clist, column);
146     }
147
148     gtk_clist_thaw(clist);
149     gtk_clist_sort(clist);
150 }
151
152
153 static gint
154 mtp3_sum_gtk_sort_column(
155     GtkCList            *clist,
156     gconstpointer       ptr1,
157     gconstpointer       ptr2)
158 {
159     const GtkCListRow   *row1 = ptr1;
160     const GtkCListRow   *row2 = ptr2;
161     char                *text1 = NULL;
162     char                *text2 = NULL;
163     int                 i1, i2;
164
165     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
166     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
167
168     switch (clist->sort_column)
169     {
170     case 0:
171         /* text columns */
172         return(strcmp(text1, text2));
173
174     default:
175         /* number columns */
176         i1 = strtol(text1, NULL, 0);
177         i2 = strtol(text2, NULL, 0);
178         return(i1 - i2);
179     }
180
181     g_assert_not_reached();
182
183     return(0);
184 }
185
186 static void
187 mtp3_sum_draw(
188     GtkWidget           *table,
189     double              seconds,
190     int                 *tot_num_msus_p,
191     double              *tot_num_bytes_p)
192 {
193     const char          *entries[MTP3_SUM_INIT_TABLE_NUM_COLUMNS];
194     int                 i, j;
195     int                 num_msus;
196     double              num_bytes;
197
198     *tot_num_msus_p = 0;
199     *tot_num_bytes_p = 0;
200
201     for (i=0; i < MTP3_NUM_SI_CODE; i++)
202     {
203         entries[0] = g_strdup(mtp3_service_indicator_code_short_vals[i].strptr);
204
205         j = 0;
206         num_msus = 0;
207         num_bytes = 0;
208
209         while (j < mtp3_num_used)
210         {
211             num_msus += mtp3_stat[j].si_code[i].num_msus;
212             num_bytes += mtp3_stat[j].si_code[i].size;
213
214             j++;
215         }
216
217         *tot_num_msus_p += num_msus;
218         *tot_num_bytes_p += num_bytes;
219
220         entries[1] = g_strdup_printf("%u", num_msus);
221
222         entries[2] = (seconds) ? g_strdup_printf("%.2f", num_msus/seconds) : "N/A";
223
224         entries[3] = g_strdup_printf("%.0f", num_bytes);
225
226         entries[4] = (num_msus) ? g_strdup_printf("%.2f", num_bytes/num_msus) : "N/A";
227
228         entries[5] = (seconds) ? g_strdup_printf("%.2f", num_bytes/seconds) : "N/A";
229
230         gtk_clist_insert(GTK_CLIST(table), i, (gchar **) entries);
231     }
232
233     gtk_clist_sort(GTK_CLIST(table));
234 }
235
236
237 static void
238 mtp3_sum_gtk_sum_cb(GtkWidget *w _U_, gpointer d _U_)
239 {
240   summary_tally summary;
241   GtkWidget     *sum_open_w,
242                 *main_vb, *file_fr, *data_fr, *file_box,
243                 *data_box, *bbox, *close_bt,
244                 *tot_fr, *tot_box,
245                 *table, *column_lb, *table_fr;
246   column_arrows *col_arrows;
247
248   gchar                 string_buff[SUM_STR_MAX];
249   const char *  file_type;
250   double                seconds;
251   int                   tot_num_msus;
252   double                tot_num_bytes;
253   int                   i;
254
255   /* initialize the tally */
256   summary_fill_in(&cfile, &summary);
257
258   /* initial compututations */
259   seconds = summary.stop_time - summary.start_time;
260
261   sum_open_w = window_new(GTK_WINDOW_TOPLEVEL, "MTP3 Statistics: Summary");
262
263   /* Container for each row of widgets */
264   main_vb = gtk_vbox_new(FALSE, 3);
265   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
266   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
267   gtk_widget_show(main_vb);
268
269   /* File frame */
270   file_fr = gtk_frame_new("File");
271   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
272   gtk_widget_show(file_fr);
273
274   file_box = gtk_vbox_new(FALSE, 3);
275   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
276   gtk_widget_show(file_box);
277
278   /* filename */
279   g_snprintf(string_buff, SUM_STR_MAX, "Name: %s", ((summary.filename) ? summary.filename : "None"));
280   add_string_to_box(string_buff, file_box);
281
282   /* length */
283   g_snprintf(string_buff, SUM_STR_MAX, "Length: %" G_GINT64_MODIFIER "d", summary.file_length);
284   add_string_to_box(string_buff, file_box);
285
286   /* format */
287   file_type = wtap_file_type_string(summary.encap_type);
288   g_snprintf(string_buff, SUM_STR_MAX, "Format: %s", (file_type ? file_type : "N/A"));
289   add_string_to_box(string_buff, file_box);
290
291   if (summary.has_snap) {
292     /* snapshot length */
293     g_snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", summary.snap);
294     add_string_to_box(string_buff, file_box);
295   }
296
297   /* Data frame */
298   data_fr = gtk_frame_new("Data");
299   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
300   gtk_widget_show(data_fr);
301
302   data_box = gtk_vbox_new(FALSE, 3);
303   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
304   gtk_widget_show(data_box);
305
306   /* seconds */
307   g_snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", summary.elapsed_time);
308   add_string_to_box(string_buff, data_box);
309
310   g_snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
311   add_string_to_box(string_buff, data_box);
312
313   /* Packet count */
314   g_snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", summary.packet_count);
315   add_string_to_box(string_buff, data_box);
316
317   /* MTP3 SPECIFIC */
318   table_fr = gtk_frame_new("Service Indicator (SI) Totals");
319   gtk_container_add(GTK_CONTAINER(main_vb), table_fr);
320   gtk_widget_show(table_fr);
321
322   table = gtk_clist_new(MTP3_SUM_INIT_TABLE_NUM_COLUMNS);
323   gtk_container_add(GTK_CONTAINER(table_fr), table);
324   gtk_widget_show(table);
325
326   col_arrows =
327       (column_arrows *) g_malloc(sizeof(column_arrows) * MTP3_SUM_INIT_TABLE_NUM_COLUMNS);
328
329   for (i = 0; i < MTP3_SUM_INIT_TABLE_NUM_COLUMNS; i++)
330   {
331       col_arrows[i].table = gtk_table_new(2, 2, FALSE);
332
333       gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
334
335       column_lb = gtk_label_new(columns[i].strptr);
336
337       gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
338           0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
339
340       gtk_widget_show(column_lb);
341
342       col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
343
344       gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
345           1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
346
347       col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
348
349       gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
350           1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
351
352       if (i == 0)
353       {
354           /* default column sorting */
355           gtk_widget_show(col_arrows[i].ascend_pm);
356       }
357
358       gtk_clist_set_column_justification(GTK_CLIST(table), i, columns[i].just);
359
360       gtk_clist_set_column_widget(GTK_CLIST(table), i, col_arrows[i].table);
361       gtk_widget_show(col_arrows[i].table);
362   }
363   gtk_clist_column_titles_show(GTK_CLIST(table));
364
365   gtk_clist_set_compare_func(GTK_CLIST(table), mtp3_sum_gtk_sort_column);
366   gtk_clist_set_sort_column(GTK_CLIST(table), 0);
367   gtk_clist_set_sort_type(GTK_CLIST(table), GTK_SORT_ASCENDING);
368
369   for (i = 0; i < MTP3_SUM_INIT_TABLE_NUM_COLUMNS; i++)
370   {
371       gtk_clist_set_column_width(GTK_CLIST(table), i, columns[i].value);
372   }
373
374   gtk_clist_set_shadow_type(GTK_CLIST(table), GTK_SHADOW_IN);
375   gtk_clist_column_titles_show(GTK_CLIST(table));
376
377   SIGNAL_CONNECT(table, "click-column", mtp3_sum_gtk_click_column_cb, col_arrows);
378
379   mtp3_sum_draw(table, seconds, &tot_num_msus, &tot_num_bytes);
380
381   /* Totals frame */
382   tot_fr = gtk_frame_new("Totals");
383   gtk_container_add(GTK_CONTAINER(main_vb), tot_fr);
384   gtk_widget_show(tot_fr);
385
386   tot_box = gtk_vbox_new(FALSE, 3);
387   gtk_container_add(GTK_CONTAINER(tot_fr), tot_box);
388   gtk_widget_show(tot_box);
389
390   g_snprintf(string_buff, SUM_STR_MAX, "Total MSUs: %u", tot_num_msus);
391   add_string_to_box(string_buff, tot_box);
392
393   if (seconds) {
394                 g_snprintf(string_buff, SUM_STR_MAX, "MSUs/second: %.2f", tot_num_msus/seconds);
395   }
396   else {
397                 g_snprintf(string_buff, SUM_STR_MAX, "MSUs/second: N/A");
398   }
399   add_string_to_box(string_buff, tot_box);
400
401   g_snprintf(string_buff, SUM_STR_MAX, "Total Bytes: %.0f", tot_num_bytes);
402   add_string_to_box(string_buff, tot_box);
403
404   if (tot_num_msus) {
405                 g_snprintf(string_buff, SUM_STR_MAX, "Average Bytes/MSU: %.2f", tot_num_bytes/tot_num_msus);
406   }
407   else {
408             g_snprintf(string_buff, SUM_STR_MAX, "Average Bytes/MSU: N/A");
409   }
410   add_string_to_box(string_buff, tot_box);
411
412   if (seconds) {
413           g_snprintf(string_buff, SUM_STR_MAX, "Bytes/second: %.2f", tot_num_bytes/seconds);
414   }
415   else {
416           g_snprintf(string_buff, SUM_STR_MAX, "Bytes/second: N/A");
417   }
418   add_string_to_box(string_buff, tot_box);
419
420   /* Button row. */
421   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
422   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
423   gtk_widget_show(bbox);
424
425   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
426   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
427
428   SIGNAL_CONNECT(sum_open_w, "delete_event", window_delete_event_cb, NULL);
429
430   gtk_widget_show_all(sum_open_w);
431   window_present(sum_open_w);
432 }
433
434
435 void
436 register_tap_listener_gtkmtp3_summary(void)
437 {
438     register_stat_menu_item("MTP3/MSU Summary",  REGISTER_STAT_GROUP_TELEPHONY,
439         mtp3_sum_gtk_sum_cb, NULL, NULL, NULL);
440 }