c99e0342414aa79d9c304c6825aeaf23038fda9d
[obnox/wireshark/wip.git] / gtk / summary_dlg.c
1 /* summary_dlg.c
2  * Routines for capture file summary window
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 #include "summary.h"
39 #include "summary_dlg.h"
40 #include "dlg_utils.h"
41 #include "ui_util.h"
42 #include "compat_macros.h"
43 #include "help_dlg.h"
44
45 #define SUM_STR_MAX     1024
46 #define FILTER_SNIP_LEN 50
47
48
49 static void
50 add_string_to_table_sensitive(GtkWidget *list, guint *row, gchar *title, gchar *value, gboolean sensitive)
51 {
52     GtkWidget *label;
53     gchar     *indent;
54
55     if(strlen(value) != 0) {
56         indent = g_strdup_printf("   %s", title);
57     } else {
58         indent = g_strdup(title);
59     }
60     label = gtk_label_new(indent);
61     g_free(indent);
62     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
63     gtk_widget_set_sensitive(label, sensitive);
64     gtk_table_attach_defaults(GTK_TABLE(list), label, 0, 1, *row, *row+1);
65
66     label = gtk_label_new(value);
67     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
68     gtk_widget_set_sensitive(label, sensitive);
69     gtk_table_attach_defaults(GTK_TABLE(list), label, 1, 2, *row, *row+1);
70
71     *row = *row + 1;
72 }
73
74 static void
75 add_string_to_table(GtkWidget *list, guint *row, gchar *title, gchar *value)
76 {
77     add_string_to_table_sensitive(list, row, title, value, TRUE);
78 }
79
80
81 static void
82 add_string_to_list(GtkWidget *list, gchar *title, gchar *captured, gchar *displayed)
83 {
84     simple_list_append(list, 0, title, 1, captured, 2, displayed, -1);
85 }
86
87 void
88 summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
89 {
90   summary_tally summary;
91   GtkWidget     *sum_open_w,
92                 *main_vb, *bbox, *close_bt, *help_bt;
93   GtkWidget     *table;
94   GtkWidget     *list;
95   char          *titles[] = { "Traffic", "Captured", "Displayed" };
96
97   gchar         string_buff[SUM_STR_MAX];
98   gchar         string_buff2[SUM_STR_MAX];
99
100   double        seconds;
101   double        disp_seconds;
102   guint         offset;
103   guint         snip;
104   guint         row;
105   gchar        *str_dup;
106   gchar        *str_work;
107
108   time_t        ti_time;
109   struct tm    *ti_tm;
110   unsigned int  elapsed_time;
111
112   /* initial computations */
113   summary_fill_in(&cfile, &summary);
114   seconds = summary.stop_time - summary.start_time;
115   disp_seconds = summary.filtered_stop - summary.filtered_start;
116
117   sum_open_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Summary");
118
119   /* Container for each row of widgets */
120   main_vb = gtk_vbox_new(FALSE, 12);
121   gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
122   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
123
124   /* table */
125   table = gtk_table_new(1, 2, FALSE);
126   gtk_table_set_col_spacings(GTK_TABLE(table), 6);
127   gtk_table_set_row_spacings(GTK_TABLE(table), 3);
128   gtk_container_add(GTK_CONTAINER(main_vb), table);
129   row = 0;
130
131
132   /* File */
133   add_string_to_table(table, &row, "File", "");
134
135   /* filename */
136   g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.filename);
137   add_string_to_table(table, &row, "Name:", string_buff);
138
139   /* length */
140   g_snprintf(string_buff, SUM_STR_MAX, "%lu bytes", summary.file_length);
141   add_string_to_table(table, &row, "Length:", string_buff);
142
143   /* format */
144   g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_file_type_string(summary.encap_type));
145   add_string_to_table(table, &row, "Format:", string_buff);
146
147   if (summary.has_snap) {
148     /* snapshot length */
149     g_snprintf(string_buff, SUM_STR_MAX, "%u bytes", summary.snap);
150     add_string_to_table(table, &row, "Packet size limit:", string_buff);
151   }
152
153
154   /* Time */
155   add_string_to_table(table, &row, "", "");
156   add_string_to_table(table, &row, "Time", "");
157
158   /* start time */
159   ti_time = (time_t)summary.start_time;
160   ti_tm = localtime(&ti_time);
161   g_snprintf(string_buff, SUM_STR_MAX,
162              "%04d-%02d-%02d %02d:%02d:%02d",
163              ti_tm->tm_year + 1900,
164              ti_tm->tm_mon + 1,
165              ti_tm->tm_mday,
166              ti_tm->tm_hour,
167              ti_tm->tm_min,
168              ti_tm->tm_sec);
169   add_string_to_table(table, &row, "First packet:", string_buff);
170
171   /* stop time */
172   ti_time = (time_t)summary.stop_time;
173   ti_tm = localtime(&ti_time);
174   g_snprintf(string_buff, SUM_STR_MAX,
175              "%04d-%02d-%02d %02d:%02d:%02d",
176              ti_tm->tm_year + 1900,
177              ti_tm->tm_mon + 1,
178              ti_tm->tm_mday,
179              ti_tm->tm_hour,
180              ti_tm->tm_min,
181              ti_tm->tm_sec);
182   add_string_to_table(table, &row, "Last packet:", string_buff);
183
184   /* elapsed seconds */
185   elapsed_time = (unsigned int)summary.elapsed_time;
186   if(elapsed_time/86400) {
187       g_snprintf(string_buff, SUM_STR_MAX, "%02u days %02u:%02u:%02u", 
188         elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
189   } else {
190       g_snprintf(string_buff, SUM_STR_MAX, "%02u:%02u:%02u", 
191         elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
192   }
193   add_string_to_table(table, &row, "Elapsed:", string_buff);
194
195
196   /* Capture */
197   add_string_to_table(table, &row, "", "");
198   add_string_to_table_sensitive(table, &row, "Capture", "", (summary.iface != NULL));
199
200   /* interface */
201   if (summary.iface) {
202     g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.iface);
203   } else {
204     g_snprintf(string_buff, SUM_STR_MAX, "unknown");
205   }
206   add_string_to_table_sensitive(table, &row, "Interface:", string_buff, (summary.iface) != NULL);
207
208   /* Dropped count */
209   if (summary.drops_known) {
210     g_snprintf(string_buff, SUM_STR_MAX, "%u", summary.drops);
211   } else {
212     g_snprintf(string_buff, SUM_STR_MAX, "unknown");
213   }
214   add_string_to_table_sensitive(table, &row, "Dropped packets:", string_buff, (summary.iface != NULL));
215
216 #ifdef HAVE_LIBPCAP
217   /* Capture filter */
218   if (summary.cfilter && summary.cfilter[0] != '\0') {
219     g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.cfilter);
220   } else {
221     if(summary.iface) {
222       g_snprintf(string_buff, SUM_STR_MAX, "none");
223     } else {
224       g_snprintf(string_buff, SUM_STR_MAX, "unknown");
225     }
226   }
227   add_string_to_table_sensitive(table, &row, "Capture filter:", string_buff, (summary.iface != NULL));
228 #endif
229
230
231   /* Data */
232   add_string_to_table(table, &row, "", "");
233   add_string_to_table(table, &row, "Display", "");
234
235   if (summary.dfilter) {
236     /* Display filter */
237     /* limit each row to some reasonable length */
238     str_dup = g_strdup_printf("%s", summary.dfilter);
239     str_work = g_strdup(str_dup);
240     offset = 0;
241     snip = 0;
242     while(strlen(str_work) > FILTER_SNIP_LEN) {
243         str_work[FILTER_SNIP_LEN] = '\0';
244         add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
245         g_free(str_work);
246         offset+=FILTER_SNIP_LEN;
247         str_work = g_strdup(&str_dup[offset]);
248         snip++;
249     }
250     
251     add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
252     g_free(str_work);
253     g_free(str_dup);
254   } else {
255     /* Display filter */
256     add_string_to_table(table, &row, "Display filter:", "none");
257   }
258
259   /* Marked Packet count */
260   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.marked_count);
261   add_string_to_table(table, &row, "Marked packets:", string_buff);
262
263
264   /* Traffic */
265   list = simple_list_new(3, titles);
266   gtk_container_add(GTK_CONTAINER(main_vb), list);
267
268   g_snprintf(string_buff, SUM_STR_MAX, "%.3f sec", seconds);
269   if(summary.dfilter) {
270     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f sec", disp_seconds);
271   } else {
272     strcpy(string_buff2, "");
273   }
274   add_string_to_list(list, "Between first and last packet", string_buff, string_buff2);
275
276   /* Packet count */
277   g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.packet_count);
278   if(summary.dfilter) {
279     g_snprintf(string_buff2, SUM_STR_MAX, "%i", summary.filtered_count);
280   } else {
281     strcpy(string_buff2, "");
282   }
283   add_string_to_list(list, "Packets", string_buff, string_buff2);
284
285   /* Packets per second */
286   if (seconds > 0){
287     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.packet_count/seconds);
288   } else {
289     strcpy(string_buff, "");
290   }
291   if(summary.dfilter && disp_seconds > 0){
292     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_count/disp_seconds);
293   } else {
294     strcpy(string_buff2, "");
295   }
296   add_string_to_list(list, "Avg. packets/sec", string_buff, string_buff2);
297
298   /* Packet size */
299   if (summary.packet_count > 0){
300     g_snprintf(string_buff, SUM_STR_MAX, "%.3f bytes",
301       (float)summary.bytes/summary.packet_count);
302   } else {
303     strcpy(string_buff, "");
304   }
305   if (summary.dfilter && summary.filtered_count > 0){
306     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f bytes",
307           (float) summary.filtered_bytes/summary.filtered_count);
308   } else {
309     strcpy(string_buff2, "");
310   }
311   add_string_to_list(list, "Avg. packet size", string_buff, string_buff2);
312
313   /* Byte count */
314   g_snprintf(string_buff, SUM_STR_MAX, "%d", summary.bytes);
315   if (summary.dfilter && summary.filtered_count > 0){
316     g_snprintf(string_buff2, SUM_STR_MAX, "%d", summary.filtered_bytes);
317   } else {
318     strcpy(string_buff2, "");
319   }
320   add_string_to_list(list, "Bytes", string_buff, string_buff2);
321
322   /* Bytes per second */
323   if (seconds > 0){
324     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.bytes/seconds);
325   } else {
326     strcpy(string_buff, "");
327   }
328   if (summary.dfilter && disp_seconds > 0){
329     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_bytes/disp_seconds);
330   } else {
331     strcpy(string_buff2, "");
332   }
333   add_string_to_list(list, "Avg. bytes/sec", string_buff, string_buff2);
334
335   /* MBit per second */
336   if (seconds > 0){
337     g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.bytes * 8.0 / (seconds * 1000.0 * 1000.0));
338   } else {
339     strcpy(string_buff, "");
340   }
341   if (summary.dfilter && disp_seconds > 0){
342     g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", 
343           summary.filtered_bytes * 8.0 / (disp_seconds * 1000.0 * 1000.0));
344   } else {
345     strcpy(string_buff2, "");
346   }
347   add_string_to_list(list, "Avg. MBit/sec", string_buff, string_buff2);
348
349   
350   /* Button row. */
351   if(topic_available(HELP_STATS_SUMMARY_DIALOG)) {
352     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
353   } else {
354     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
355   }
356   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
357
358   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
359   window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
360
361   if(topic_available(HELP_STATS_SUMMARY_DIALOG)) {
362     help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
363     SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_STATS_SUMMARY_DIALOG);
364   }
365
366   gtk_widget_grab_focus(close_bt);
367
368   SIGNAL_CONNECT(sum_open_w, "delete_event", window_delete_event_cb, NULL);
369
370   gtk_widget_show_all(sum_open_w);
371   window_present(sum_open_w);
372 }