"hex_str_to_bytes()" modifies the GByteArray supplied to it, so don't
[obnox/wireshark/wip.git] / gtk / summary_dlg.c
1 /* summary_dlg.c
2  * Routines for capture file summary window
3  *
4  * $Id: summary_dlg.c,v 1.21 2003/12/21 22:32:07 guy Exp $
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 <gtk/gtk.h>
30
31 #include <wtap.h>
32
33 #ifdef NEED_SNPRINTF_H
34 # include "snprintf.h"
35 #endif
36
37 #include "summary.h"
38 #include "summary_dlg.h"
39 #include "dlg_utils.h"
40 #include "ui_util.h"
41 #include "compat_macros.h"
42
43 #define SUM_STR_MAX 1024
44
45
46 static void
47 add_string_to_box(gchar *str, GtkWidget *box)
48 {
49   GtkWidget *lb;
50   lb = gtk_label_new(str);
51   gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
52   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
53   gtk_widget_show(lb);
54 }
55
56
57 void
58 summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
59 {
60   summary_tally summary;
61   GtkWidget     *sum_open_w,
62                 *main_vb, *file_fr, *data_fr, *capture_fr, *file_box,
63                 *filter_box, *filter_fr,
64                 *data_box, *capture_box, *bbox, *close_bt;
65
66   gchar          string_buff[SUM_STR_MAX];
67
68   double         seconds;
69
70  /* initialize the tally */
71   summary_fill_in(&summary);
72
73   /* initial compututations */
74   seconds = summary.stop_time - summary.start_time;
75   sum_open_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
76   gtk_window_set_title(GTK_WINDOW(sum_open_w), "Ethereal: Summary");
77   SIGNAL_CONNECT(sum_open_w, "realize", window_icon_realize_cb, NULL);
78
79   /* Container for each row of widgets */
80   main_vb = gtk_vbox_new(FALSE, 3);
81   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
82   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
83   gtk_widget_show(main_vb);
84
85   /* File frame */
86   file_fr = gtk_frame_new("File");
87   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
88   gtk_widget_show(file_fr);
89
90   file_box = gtk_vbox_new(FALSE, 3);
91   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
92   gtk_widget_show(file_box);
93
94   /* filename */
95   snprintf(string_buff, SUM_STR_MAX, "Name: %s", summary.filename);
96   add_string_to_box(string_buff, file_box);
97
98   /* length */
99   snprintf(string_buff, SUM_STR_MAX, "Length: %lu", summary.file_length);
100   add_string_to_box(string_buff, file_box);
101
102   /* format */
103   snprintf(string_buff, SUM_STR_MAX, "Format: %s", wtap_file_type_string(summary.encap_type));
104   add_string_to_box(string_buff, file_box);
105
106   if (summary.has_snap) {
107     /* snapshot length */
108     snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", summary.snap);
109     add_string_to_box(string_buff, file_box);
110   }
111
112   /* Data frame */
113   data_fr = gtk_frame_new("Data");
114   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
115   gtk_widget_show(data_fr);
116
117   data_box = gtk_vbox_new(FALSE, 3);
118   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
119   gtk_widget_show(data_box);
120
121   /* seconds */
122   snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", summary.elapsed_time);
123   add_string_to_box(string_buff, data_box);
124
125   snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
126   add_string_to_box(string_buff, data_box);
127
128   /* Packet count */
129   snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", summary.packet_count);
130   add_string_to_box(string_buff, data_box);
131
132   /* Filtered Packet count */
133   /* Unless there is none filter, we move informations about filtered packets in a separate frame */
134   if (!summary.dfilter)
135         add_string_to_box("Filtered packet count: 0", data_box);
136
137   /* Marked Packet count */
138   snprintf(string_buff, SUM_STR_MAX, "Marked packet count: %i", summary.marked_count);
139   add_string_to_box(string_buff, data_box);
140
141   /* Packets per second */
142   if (seconds > 0){
143     snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", summary.packet_count/seconds);
144     add_string_to_box(string_buff, data_box);
145   }
146
147   /* Packet size */
148   if (summary.packet_count > 0){
149     snprintf(string_buff, SUM_STR_MAX, "Avg. packet size: %.3f bytes",
150       (float)summary.bytes/summary.packet_count);
151     add_string_to_box(string_buff, data_box);
152   }
153
154   /* Dropped count */
155   if (summary.drops_known) {
156     snprintf(string_buff, SUM_STR_MAX, "Dropped packets: %u", summary.drops);
157     add_string_to_box(string_buff, data_box);
158   }
159
160   /* Byte count */
161   snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", summary.bytes);
162   add_string_to_box(string_buff, data_box);
163
164   /* Bytes per second */
165   if (seconds > 0){
166     snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", summary.bytes/seconds);
167     add_string_to_box(string_buff, data_box);
168
169     /* MBit per second */
170     snprintf(string_buff, SUM_STR_MAX, "Avg. Mbit/sec: %.3f",
171              summary.bytes * 8.0 / (seconds * 1000.0 * 1000.0));
172     add_string_to_box(string_buff, data_box);
173   }
174
175   /* Filtered packets frame */
176   filter_fr = gtk_frame_new("Data in filtered packets");
177   gtk_container_add(GTK_CONTAINER(main_vb), filter_fr);
178   gtk_widget_show(filter_fr);
179
180   filter_box = gtk_vbox_new( FALSE, 3);
181   gtk_container_add(GTK_CONTAINER(filter_fr), filter_box);
182   gtk_widget_show(filter_box);
183
184   if (summary.dfilter) {
185     double seconds;
186
187     /* Display filter */
188     snprintf(string_buff, SUM_STR_MAX, "Display filter: %s", summary.dfilter);
189     add_string_to_box(string_buff, filter_box);
190
191     /* seconds */
192     seconds = (summary.filtered_stop - summary.filtered_start);
193     snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
194     add_string_to_box(string_buff, filter_box);
195
196     /* Packet count */
197     snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", summary.filtered_count);
198     add_string_to_box(string_buff, filter_box);
199
200     /* Packets per second */
201     if (seconds > 0){
202       snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", summary.filtered_count/seconds);
203       add_string_to_box(string_buff, filter_box);
204     }
205
206     /* Packet size */
207     if (summary.filtered_count > 0){
208       snprintf(string_buff, SUM_STR_MAX, "Avg. packet size: %.3f bytes",
209           (float) summary.filtered_bytes/summary.filtered_count);
210       add_string_to_box(string_buff, filter_box);
211     }
212
213     /* Byte count */
214     snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", summary.filtered_bytes);
215     add_string_to_box(string_buff, filter_box);
216
217     /* Bytes per second */
218     if (seconds > 0){
219       snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", summary.filtered_bytes/seconds);
220       add_string_to_box(string_buff, filter_box);
221
222       /* MBit per second */
223       snprintf(string_buff, SUM_STR_MAX, "Avg. Mbit/sec: %.3f",
224                summary.filtered_bytes * 8.0 / (seconds * 1000.0 * 1000.0));
225       add_string_to_box(string_buff, filter_box);
226     }
227   } else {
228     /* Display filter */
229     snprintf(string_buff, SUM_STR_MAX, "Display filter: none");
230     add_string_to_box(string_buff, filter_box);
231   }
232
233   /* Capture Frame */
234   capture_fr = gtk_frame_new("Capture");
235   gtk_container_add(GTK_CONTAINER(main_vb), capture_fr);
236   gtk_widget_show(capture_fr);
237
238   capture_box = gtk_vbox_new(FALSE, 3);
239   gtk_container_add(GTK_CONTAINER(capture_fr), capture_box);
240   gtk_widget_show(capture_box);
241
242   /* interface */
243   if (summary.iface) {
244     snprintf(string_buff, SUM_STR_MAX, "Interface: %s", summary.iface);
245   } else {
246     sprintf(string_buff, "Interface: unknown");
247   }
248   add_string_to_box(string_buff, capture_box);
249
250 #ifdef HAVE_LIBPCAP
251   /* Capture filter */
252   if (summary.cfilter && summary.cfilter[0] != '\0') {
253     snprintf(string_buff, SUM_STR_MAX, "Capture filter: %s", summary.cfilter);
254   } else {
255     sprintf(string_buff, "Capture filter: none");
256   }
257   add_string_to_box(string_buff, capture_box);
258 #endif
259
260   /* Button row: close button.
261      (We put it in an HButtonBox, even though there's only one of them,
262      so that it doesn't expand to the width of the window.  */
263   bbox = gtk_hbutton_box_new();
264   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
265   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
266   gtk_widget_show(bbox);
267
268   /* Create Close Button */
269 #if GTK_MAJOR_VERSION < 2
270   close_bt = gtk_button_new_with_label("Close");
271 #else
272   close_bt = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
273 #endif
274   SIGNAL_CONNECT_OBJECT(close_bt, "clicked", gtk_widget_destroy, sum_open_w);
275   GTK_WIDGET_SET_FLAGS(close_bt, GTK_CAN_DEFAULT);
276   gtk_box_pack_start(GTK_BOX(bbox), close_bt, FALSE,FALSE, 0);
277   gtk_widget_grab_default(close_bt);
278   gtk_widget_show(close_bt);
279
280   /* Catch the "key_press_event" signal in the window, so that we can catch
281      the ESC key being pressed and act as if the "Close" button had
282      been selected. */
283   dlg_set_cancel(sum_open_w, close_bt);
284
285   gtk_window_set_position(GTK_WINDOW(sum_open_w), GTK_WIN_POS_MOUSE);
286   gtk_widget_show(sum_open_w);
287 }