name change
[metze/wireshark/wip.git] / gtk / summary_dlg.c
1 /* summary_dlg.c
2  * Routines for capture file summary window
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include <wtap.h>
34 #include <time.h>
35
36 #include "globals.h"
37 #include "file.h"
38 #ifdef HAVE_LIBPCAP
39 #include "capture.h"
40 #include "main.h"
41 #endif
42 #include "summary.h"
43 #include "summary_dlg.h"
44 #include "dlg_utils.h"
45 #include "gui_utils.h"
46 #include "compat_macros.h"
47 #include "help_dlg.h"
48
49 #define SUM_STR_MAX     1024
50 #define FILTER_SNIP_LEN 50
51
52
53 static void
54 add_string_to_table_sensitive(GtkWidget *list, guint *row, gchar *title, gchar *value, gboolean sensitive)
55 {
56     GtkWidget *label;
57     gchar     *indent;
58
59     if(strlen(value) != 0) {
60         indent = g_strdup_printf("   %s", title);
61     } else {
62         indent = g_strdup(title);
63     }
64     label = gtk_label_new(indent);
65     g_free(indent);
66     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
67     gtk_widget_set_sensitive(label, sensitive);
68     gtk_table_attach_defaults(GTK_TABLE(list), label, 0, 1, *row, *row+1);
69
70     label = gtk_label_new(value);
71     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
72     gtk_widget_set_sensitive(label, sensitive);
73     gtk_table_attach_defaults(GTK_TABLE(list), label, 1, 2, *row, *row+1);
74
75     *row = *row + 1;
76 }
77
78 static void
79 add_string_to_table(GtkWidget *list, guint *row, gchar *title, gchar *value)
80 {
81     add_string_to_table_sensitive(list, row, title, value, TRUE);
82 }
83
84
85 static void
86 add_string_to_list(GtkWidget *list, gchar *title, gchar *captured, gchar *displayed)
87 {
88     simple_list_append(list, 0, title, 1, captured, 2, displayed, -1);
89 }
90
91 void
92 summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
93 {
94   summary_tally summary;
95   GtkWidget     *sum_open_w,
96                 *main_vb, *bbox, *close_bt, *help_bt;
97   GtkWidget     *table;
98   GtkWidget     *list;
99   static const char *titles[] = { "Traffic", "Captured", "Displayed" };
100
101   gchar         string_buff[SUM_STR_MAX];
102   gchar         string_buff2[SUM_STR_MAX];
103
104   double        seconds;
105   double        disp_seconds;
106   guint         offset;
107   guint         snip;
108   guint         row;
109   gchar        *str_dup;
110   gchar        *str_work;
111
112   time_t        ti_time;
113   struct tm    *ti_tm;
114   unsigned int  elapsed_time;
115
116   /* initial computations */
117   summary_fill_in(&cfile, &summary);
118 #ifdef HAVE_LIBPCAP
119   summary_fill_in_capture(capture_opts, &summary);
120 #endif
121   seconds = summary.stop_time - summary.start_time;
122   disp_seconds = summary.filtered_stop - summary.filtered_start;
123
124   sum_open_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Summary");
125
126   /* Container for each row of widgets */
127   main_vb = gtk_vbox_new(FALSE, 12);
128   gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
129   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
130
131   /* table */
132   table = gtk_table_new(1, 2, FALSE);
133   gtk_table_set_col_spacings(GTK_TABLE(table), 6);
134   gtk_table_set_row_spacings(GTK_TABLE(table), 3);
135   gtk_container_add(GTK_CONTAINER(main_vb), table);
136   row = 0;
137
138
139   /* File */
140   add_string_to_table(table, &row, "File", "");
141
142   /* filename */
143   g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.filename);
144   add_string_to_table(table, &row, "Name:", string_buff);
145
146   /* length */
147   g_snprintf(string_buff, SUM_STR_MAX, "%lu bytes", summary.file_length);
148   add_string_to_table(table, &row, "Length:", string_buff);
149
150   /* format */
151   g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_file_type_string(summary.encap_type));
152   add_string_to_table(table, &row, "Format:", string_buff);
153
154   if (summary.has_snap) {
155     /* snapshot length */
156     g_snprintf(string_buff, SUM_STR_MAX, "%u bytes", summary.snap);
157     add_string_to_table(table, &row, "Packet size limit:", string_buff);
158   }
159
160
161   /* Time */
162   add_string_to_table(table, &row, "", "");
163   add_string_to_table(table, &row, "Time", "");
164
165   /* start time */
166   ti_time = (time_t)summary.start_time;
167   ti_tm = localtime(&ti_time);
168   g_snprintf(string_buff, SUM_STR_MAX,
169              "%04d-%02d-%02d %02d:%02d:%02d",
170              ti_tm->tm_year + 1900,
171              ti_tm->tm_mon + 1,
172              ti_tm->tm_mday,
173              ti_tm->tm_hour,
174              ti_tm->tm_min,
175              ti_tm->tm_sec);
176   add_string_to_table(table, &row, "First packet:", string_buff);
177
178   /* stop time */
179   ti_time = (time_t)summary.stop_time;
180   ti_tm = localtime(&ti_time);
181   g_snprintf(string_buff, SUM_STR_MAX,
182              "%04d-%02d-%02d %02d:%02d:%02d",
183              ti_tm->tm_year + 1900,
184              ti_tm->tm_mon + 1,
185              ti_tm->tm_mday,
186              ti_tm->tm_hour,
187              ti_tm->tm_min,
188              ti_tm->tm_sec);
189   add_string_to_table(table, &row, "Last packet:", string_buff);
190
191   /* elapsed seconds */
192   elapsed_time = (unsigned int)summary.elapsed_time;
193   if(elapsed_time/86400) {
194       g_snprintf(string_buff, SUM_STR_MAX, "%02u days %02u:%02u:%02u", 
195         elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
196   } else {
197       g_snprintf(string_buff, SUM_STR_MAX, "%02u:%02u:%02u", 
198         elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
199   }
200   add_string_to_table(table, &row, "Elapsed:", string_buff);
201
202
203   /* Capture */
204   add_string_to_table(table, &row, "", "");
205   add_string_to_table_sensitive(table, &row, "Capture", "", (summary.iface != NULL));
206
207   /* interface */
208   if (summary.iface) {
209     g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.iface_descr);
210   } else {
211     g_snprintf(string_buff, SUM_STR_MAX, "unknown");
212   }
213   add_string_to_table_sensitive(table, &row, "Interface:", string_buff, (summary.iface) != NULL);
214
215   /* Dropped count */
216   if (summary.drops_known) {
217     g_snprintf(string_buff, SUM_STR_MAX, "%" PRIu64, summary.drops);
218   } else {
219     g_snprintf(string_buff, SUM_STR_MAX, "unknown");
220   }
221   add_string_to_table_sensitive(table, &row, "Dropped packets:", string_buff, (summary.iface != NULL));
222
223 #ifdef HAVE_LIBPCAP
224   /* Capture filter */
225   if (summary.cfilter && summary.cfilter[0] != '\0') {
226     g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.cfilter);
227   } else {
228     if(summary.iface) {
229       g_snprintf(string_buff, SUM_STR_MAX, "none");
230     } else {
231       g_snprintf(string_buff, SUM_STR_MAX, "unknown");
232     }
233   }
234   add_string_to_table_sensitive(table, &row, "Capture filter:", string_buff, (summary.iface != NULL));
235 #endif
236
237
238   /* Data */
239   add_string_to_table(table, &row, "", "");
240   add_string_to_table(table, &row, "Display", "");
241
242   if (summary.dfilter) {
243     /* Display filter */
244     /* limit each row to some reasonable length */
245     str_dup = g_strdup_printf("%s", summary.dfilter);
246     str_work = g_strdup(str_dup);
247     offset = 0;
248     snip = 0;
249     while(strlen(str_work) > FILTER_SNIP_LEN) {
250         str_work[FILTER_SNIP_LEN] = '\0';
251         add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
252         g_free(str_work);
253         offset+=FILTER_SNIP_LEN;
254         str_work = g_strdup(&str_dup[offset]);
255         snip++;
256     }
257     
258     add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
259     g_free(str_work);
260     g_free(str_dup);
261   } else {
262     /* Display filter */
263     add_string_to_table(table, &row, "Display filter:", "none");
264   }
265
266   /* Marked Packet count */
267   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.marked_count);
268   add_string_to_table(table, &row, "Marked packets:", string_buff);
269
270
271   /* Traffic */
272   list = simple_list_new(3, titles);
273   gtk_container_add(GTK_CONTAINER(main_vb), list);
274
275   g_snprintf(string_buff, SUM_STR_MAX, "%.3f sec", seconds);
276   if(summary.dfilter) {
277     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f sec", disp_seconds);
278   } else {
279     strcpy(string_buff2, "");
280   }
281   add_string_to_list(list, "Between first and last packet", string_buff, string_buff2);
282
283   /* Packet count */
284   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.packet_count);
285   if(summary.dfilter) {
286     g_snprintf(string_buff2, SUM_STR_MAX, "%i", summary.filtered_count);
287   } else {
288     strcpy(string_buff2, "");
289   }
290   add_string_to_list(list, "Packets", string_buff, string_buff2);
291
292   /* Packets per second */
293   if (seconds > 0){
294     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.packet_count/seconds);
295   } else {
296     strcpy(string_buff, "");
297   }
298   if(summary.dfilter && disp_seconds > 0){
299     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_count/disp_seconds);
300   } else {
301     strcpy(string_buff2, "");
302   }
303   add_string_to_list(list, "Avg. packets/sec", string_buff, string_buff2);
304
305   /* Packet size */
306   if (summary.packet_count > 0){
307     g_snprintf(string_buff, SUM_STR_MAX, "%.3f bytes",
308       /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
309       (float) (gint64) (summary.bytes/summary.packet_count) );
310   } else {
311     strcpy(string_buff, "");
312   }
313   if (summary.dfilter && summary.filtered_count > 0){
314     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f bytes",
315           /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
316           (float) (gint64) summary.filtered_bytes/summary.filtered_count);
317   } else {
318     strcpy(string_buff2, "");
319   }
320   add_string_to_list(list, "Avg. packet size", string_buff, string_buff2);
321
322   /* Byte count */
323   g_snprintf(string_buff, SUM_STR_MAX, "%" PRIu64, summary.bytes);
324   if (summary.dfilter && summary.filtered_count > 0){
325     g_snprintf(string_buff2, SUM_STR_MAX, "%" PRIu64, summary.filtered_bytes);
326   } else {
327     strcpy(string_buff2, "");
328   }
329   add_string_to_list(list, "Bytes", string_buff, string_buff2);
330
331   /* Bytes per second */
332   if (seconds > 0){
333     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
334     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", ((gint64) summary.bytes)/seconds );
335   } else {
336     strcpy(string_buff, "");
337   }
338   if (summary.dfilter && disp_seconds > 0){
339     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
340     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", ((gint64) summary.filtered_bytes)/disp_seconds );
341   } else {
342     strcpy(string_buff2, "");
343   }
344   add_string_to_list(list, "Avg. bytes/sec", string_buff, string_buff2);
345
346   /* MBit per second */
347   if (seconds > 0){
348     /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
349     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", ((gint64) summary.bytes) * 8.0 / (seconds * 1000.0 * 1000.0));
350   } else {
351     strcpy(string_buff, "");
352   }
353   if (summary.dfilter && disp_seconds > 0){
354     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", 
355           /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
356           ((gint64) summary.filtered_bytes) * 8.0 / (disp_seconds * 1000.0 * 1000.0));
357   } else {
358     strcpy(string_buff2, "");
359   }
360   add_string_to_list(list, "Avg. MBit/sec", string_buff, string_buff2);
361
362   
363   /* Button row. */
364   if(topic_available(HELP_STATS_SUMMARY_DIALOG)) {
365     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
366   } else {
367     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
368   }
369   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
370
371   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
372   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
373
374   if(topic_available(HELP_STATS_SUMMARY_DIALOG)) {
375     help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
376     SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_STATS_SUMMARY_DIALOG);
377   }
378
379   gtk_widget_grab_focus(close_bt);
380
381   SIGNAL_CONNECT(sum_open_w, "delete_event", window_delete_event_cb, NULL);
382
383   gtk_widget_show_all(sum_open_w);
384   window_present(sum_open_w);
385 }