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