Squelch more const warnings (and fix some memory leaks that found).
[obnox/wireshark/wip.git] / gtk / summary_dlg.c
index cec8321a114e0752c23f814ecccb07e49efc251b..079862ed9d3340d15d2244aa7bd2fcff1cb2433b 100644 (file)
@@ -1,7 +1,7 @@
 /* summary_dlg.c
  * Routines for capture file summary window
  *
- * $Id: summary_dlg.c,v 1.27 2004/02/13 00:53:36 guy Exp $
+ * $Id$
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
 #include <gtk/gtk.h>
 
 #include <wtap.h>
+#include <time.h>
 
-#ifdef NEED_SNPRINTF_H
-# include "snprintf.h"
+#include "globals.h"
+#include "file.h"
+#ifdef HAVE_LIBPCAP
+#include <pcap.h>
+#include "capture.h"
+#include "main.h"
 #endif
-
 #include "summary.h"
 #include "summary_dlg.h"
 #include "dlg_utils.h"
 #include "ui_util.h"
 #include "compat_macros.h"
+#include "help_dlg.h"
+
+#define SUM_STR_MAX     1024
+#define FILTER_SNIP_LEN 50
 
-#define SUM_STR_MAX 1024
 
+static void
+add_string_to_table_sensitive(GtkWidget *list, guint *row, gchar *title, gchar *value, gboolean sensitive)
+{
+    GtkWidget *label;
+    gchar     *indent;
+
+    if(strlen(value) != 0) {
+        indent = g_strdup_printf("   %s", title);
+    } else {
+        indent = g_strdup(title);
+    }
+    label = gtk_label_new(indent);
+    g_free(indent);
+    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
+    gtk_widget_set_sensitive(label, sensitive);
+    gtk_table_attach_defaults(GTK_TABLE(list), label, 0, 1, *row, *row+1);
+
+    label = gtk_label_new(value);
+    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
+    gtk_widget_set_sensitive(label, sensitive);
+    gtk_table_attach_defaults(GTK_TABLE(list), label, 1, 2, *row, *row+1);
+
+    *row = *row + 1;
+}
 
 static void
-add_string_to_box(gchar *str, GtkWidget *box)
+add_string_to_table(GtkWidget *list, guint *row, gchar *title, gchar *value)
 {
-  GtkWidget *lb;
-  lb = gtk_label_new(str);
-  gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
-  gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
-  gtk_widget_show(lb);
+    add_string_to_table_sensitive(list, row, title, value, TRUE);
 }
 
 
+static void
+add_string_to_list(GtkWidget *list, gchar *title, gchar *captured, gchar *displayed)
+{
+    simple_list_append(list, 0, title, 1, captured, 2, displayed, -1);
+}
+
 void
 summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
 {
   summary_tally summary;
   GtkWidget     *sum_open_w,
-                *main_vb, *file_fr, *data_fr, *capture_fr, *file_box,
-               *filter_box, *filter_fr,
-               *data_box, *capture_box, *bbox, *close_bt;
+                *main_vb, *bbox, *close_bt, *help_bt;
+  GtkWidget     *table;
+  GtkWidget     *list;
+  static const char *titles[] = { "Traffic", "Captured", "Displayed" };
 
   gchar         string_buff[SUM_STR_MAX];
+  gchar         string_buff2[SUM_STR_MAX];
 
   double        seconds;
+  double        disp_seconds;
   guint         offset;
+  guint         snip;
+  guint         row;
   gchar        *str_dup;
   gchar        *str_work;
 
- /* initialize the tally */
-  summary_fill_in(&summary);
+  time_t        ti_time;
+  struct tm    *ti_tm;
+  unsigned int  elapsed_time;
 
   /* initial computations */
+  summary_fill_in(&cfile, &summary);
+#ifdef HAVE_LIBPCAP
+  summary_fill_in_capture(capture_opts, &summary);
+#endif
   seconds = summary.stop_time - summary.start_time;
+  disp_seconds = summary.filtered_stop - summary.filtered_start;
+
   sum_open_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Summary");
 
   /* Container for each row of widgets */
-  main_vb = gtk_vbox_new(FALSE, 3);
-  gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
+  main_vb = gtk_vbox_new(FALSE, 12);
+  gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
-  gtk_widget_show(main_vb);
 
-  /* File frame */
-  file_fr = gtk_frame_new("File");
-  gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
-  gtk_widget_show(file_fr);
+  /* table */
+  table = gtk_table_new(1, 2, FALSE);
+  gtk_table_set_col_spacings(GTK_TABLE(table), 6);
+  gtk_table_set_row_spacings(GTK_TABLE(table), 3);
+  gtk_container_add(GTK_CONTAINER(main_vb), table);
+  row = 0;
 
-  file_box = gtk_vbox_new(FALSE, 3);
-  gtk_container_border_width(GTK_CONTAINER(file_box), 5);
-  gtk_container_add(GTK_CONTAINER(file_fr), file_box);
-  gtk_widget_show(file_box);
+
+  /* File */
+  add_string_to_table(table, &row, "File", "");
 
   /* filename */
-  snprintf(string_buff, SUM_STR_MAX, "Name: %s", summary.filename);
-  add_string_to_box(string_buff, file_box);
+  g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.filename);
+  add_string_to_table(table, &row, "Name:", string_buff);
 
   /* length */
-  snprintf(string_buff, SUM_STR_MAX, "Length: %lu", summary.file_length);
-  add_string_to_box(string_buff, file_box);
+  g_snprintf(string_buff, SUM_STR_MAX, "%lu bytes", summary.file_length);
+  add_string_to_table(table, &row, "Length:", string_buff);
 
   /* format */
-  snprintf(string_buff, SUM_STR_MAX, "Format: %s", wtap_file_type_string(summary.encap_type));
-  add_string_to_box(string_buff, file_box);
+  g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_file_type_string(summary.encap_type));
+  add_string_to_table(table, &row, "Format:", string_buff);
 
   if (summary.has_snap) {
     /* snapshot length */
-    snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", summary.snap);
-    add_string_to_box(string_buff, file_box);
+    g_snprintf(string_buff, SUM_STR_MAX, "%u bytes", summary.snap);
+    add_string_to_table(table, &row, "Packet size limit:", string_buff);
   }
 
-  /* Data frame */
-  data_fr = gtk_frame_new("Data");
-  gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
-  gtk_widget_show(data_fr);
-
-  data_box = gtk_vbox_new(FALSE, 3);
-  gtk_container_border_width(GTK_CONTAINER(data_box), 5);
-  gtk_container_add(GTK_CONTAINER(data_fr), data_box);
-  gtk_widget_show(data_box);
-
-  /* seconds */
-  snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", summary.elapsed_time);
-  add_string_to_box(string_buff, data_box);
 
-  snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
-  add_string_to_box(string_buff, data_box);
-
-  /* Packet count */
-  snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", summary.packet_count);
-  add_string_to_box(string_buff, data_box);
+  /* Time */
+  add_string_to_table(table, &row, "", "");
+  add_string_to_table(table, &row, "Time", "");
+
+  /* start time */
+  ti_time = (time_t)summary.start_time;
+  ti_tm = localtime(&ti_time);
+  g_snprintf(string_buff, SUM_STR_MAX,
+             "%04d-%02d-%02d %02d:%02d:%02d",
+             ti_tm->tm_year + 1900,
+             ti_tm->tm_mon + 1,
+             ti_tm->tm_mday,
+             ti_tm->tm_hour,
+             ti_tm->tm_min,
+             ti_tm->tm_sec);
+  add_string_to_table(table, &row, "First packet:", string_buff);
+
+  /* stop time */
+  ti_time = (time_t)summary.stop_time;
+  ti_tm = localtime(&ti_time);
+  g_snprintf(string_buff, SUM_STR_MAX,
+             "%04d-%02d-%02d %02d:%02d:%02d",
+             ti_tm->tm_year + 1900,
+             ti_tm->tm_mon + 1,
+             ti_tm->tm_mday,
+             ti_tm->tm_hour,
+             ti_tm->tm_min,
+             ti_tm->tm_sec);
+  add_string_to_table(table, &row, "Last packet:", string_buff);
+
+  /* elapsed seconds */
+  elapsed_time = (unsigned int)summary.elapsed_time;
+  if(elapsed_time/86400) {
+      g_snprintf(string_buff, SUM_STR_MAX, "%02u days %02u:%02u:%02u", 
+        elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
+  } else {
+      g_snprintf(string_buff, SUM_STR_MAX, "%02u:%02u:%02u", 
+        elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
+  }
+  add_string_to_table(table, &row, "Elapsed:", string_buff);
 
-  /* Filtered Packet count */
-  /* Unless there is none filter, we move informations about filtered packets in a separate frame */
-  if (!summary.dfilter)
-       add_string_to_box("Filtered packet count: 0", data_box);
 
-  /* Marked Packet count */
-  snprintf(string_buff, SUM_STR_MAX, "Marked packet count: %i", summary.marked_count);
-  add_string_to_box(string_buff, data_box);
+  /* Capture */
+  add_string_to_table(table, &row, "", "");
+  add_string_to_table_sensitive(table, &row, "Capture", "", (summary.iface != NULL));
 
-  /* Packets per second */
-  if (seconds > 0){
-    snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", summary.packet_count/seconds);
-    add_string_to_box(string_buff, data_box);
-  }
-
-  /* Packet size */
-  if (summary.packet_count > 0){
-    snprintf(string_buff, SUM_STR_MAX, "Avg. packet size: %.3f bytes",
-      (float)summary.bytes/summary.packet_count);
-    add_string_to_box(string_buff, data_box);
+  /* interface */
+  if (summary.iface) {
+    g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.iface_descr);
+  } else {
+    g_snprintf(string_buff, SUM_STR_MAX, "unknown");
   }
+  add_string_to_table_sensitive(table, &row, "Interface:", string_buff, (summary.iface) != NULL);
 
   /* Dropped count */
   if (summary.drops_known) {
-    snprintf(string_buff, SUM_STR_MAX, "Dropped packets: %u", summary.drops);
-    add_string_to_box(string_buff, data_box);
+    g_snprintf(string_buff, SUM_STR_MAX, "%" PRIu64, summary.drops);
+  } else {
+    g_snprintf(string_buff, SUM_STR_MAX, "unknown");
   }
+  add_string_to_table_sensitive(table, &row, "Dropped packets:", string_buff, (summary.iface != NULL));
 
-  /* Byte count */
-  snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", summary.bytes);
-  add_string_to_box(string_buff, data_box);
-
-  /* Bytes per second */
-  if (seconds > 0){
-    snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", summary.bytes/seconds);
-    add_string_to_box(string_buff, data_box);
-
-    /* MBit per second */
-    snprintf(string_buff, SUM_STR_MAX, "Avg. Mbit/sec: %.3f",
-             summary.bytes * 8.0 / (seconds * 1000.0 * 1000.0));
-    add_string_to_box(string_buff, data_box);
+#ifdef HAVE_LIBPCAP
+  /* Capture filter */
+  if (summary.cfilter && summary.cfilter[0] != '\0') {
+    g_snprintf(string_buff, SUM_STR_MAX, "%s", summary.cfilter);
+  } else {
+    if(summary.iface) {
+      g_snprintf(string_buff, SUM_STR_MAX, "none");
+    } else {
+      g_snprintf(string_buff, SUM_STR_MAX, "unknown");
+    }
   }
+  add_string_to_table_sensitive(table, &row, "Capture filter:", string_buff, (summary.iface != NULL));
+#endif
 
-  /* Filtered packets frame */
-  filter_fr = gtk_frame_new("Data in filtered packets");
-  gtk_container_add(GTK_CONTAINER(main_vb), filter_fr);
-  gtk_widget_show(filter_fr);
 
-  filter_box = gtk_vbox_new( FALSE, 3);
-  gtk_container_border_width(GTK_CONTAINER(filter_box), 5);
-  gtk_container_add(GTK_CONTAINER(filter_fr), filter_box);
-  gtk_widget_show(filter_box);
+  /* Data */
+  add_string_to_table(table, &row, "", "");
+  add_string_to_table(table, &row, "Display", "");
 
   if (summary.dfilter) {
-    double seconds;
-
     /* Display filter */
     /* limit each row to some reasonable length */
-    str_dup = g_strdup_printf("Display filter: %s", summary.dfilter);
+    str_dup = g_strdup_printf("%s", summary.dfilter);
     str_work = g_strdup(str_dup);
     offset = 0;
-    while(strlen(str_work) > 100) {
-        str_work[100] = '\0';
-        add_string_to_box(str_work, filter_box);
+    snip = 0;
+    while(strlen(str_work) > FILTER_SNIP_LEN) {
+        str_work[FILTER_SNIP_LEN] = '\0';
+        add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
         g_free(str_work);
-        offset+=100;
+        offset+=FILTER_SNIP_LEN;
         str_work = g_strdup(&str_dup[offset]);
+        snip++;
     }
     
-    add_string_to_box(str_work, filter_box);
+    add_string_to_table(table, &row, (snip == 0) ? "Display filter:" : "", str_work);
     g_free(str_work);
     g_free(str_dup);
+  } else {
+    /* Display filter */
+    add_string_to_table(table, &row, "Display filter:", "none");
+  }
 
-    /* seconds */
-    seconds = (summary.filtered_stop - summary.filtered_start);
-    snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
-    add_string_to_box(string_buff, filter_box);
-
-    /* Packet count */
-    snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", summary.filtered_count);
-    add_string_to_box(string_buff, filter_box);
+  /* Marked Packet count */
+  g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.marked_count);
+  add_string_to_table(table, &row, "Marked packets:", string_buff);
 
-    /* Packets per second */
-    if (seconds > 0){
-      snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", summary.filtered_count/seconds);
-      add_string_to_box(string_buff, filter_box);
-    }
 
-    /* Packet size */
-    if (summary.filtered_count > 0){
-      snprintf(string_buff, SUM_STR_MAX, "Avg. packet size: %.3f bytes",
-          (float) summary.filtered_bytes/summary.filtered_count);
-      add_string_to_box(string_buff, filter_box);
-    }
+  /* Traffic */
+  list = simple_list_new(3, titles);
+  gtk_container_add(GTK_CONTAINER(main_vb), list);
 
-    /* Byte count */
-    snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", summary.filtered_bytes);
-    add_string_to_box(string_buff, filter_box);
+  g_snprintf(string_buff, SUM_STR_MAX, "%.3f sec", seconds);
+  if(summary.dfilter) {
+    g_snprintf(string_buff2, SUM_STR_MAX, "%.3f sec", disp_seconds);
+  } else {
+    strcpy(string_buff2, "");
+  }
+  add_string_to_list(list, "Between first and last packet", string_buff, string_buff2);
 
-    /* Bytes per second */
-    if (seconds > 0){
-      snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", summary.filtered_bytes/seconds);
-      add_string_to_box(string_buff, filter_box);
+  /* Packet count */
+  g_snprintf(string_buff, SUM_STR_MAX, "%i", summary.packet_count);
+  if(summary.dfilter) {
+    g_snprintf(string_buff2, SUM_STR_MAX, "%i", summary.filtered_count);
+  } else {
+    strcpy(string_buff2, "");
+  }
+  add_string_to_list(list, "Packets", string_buff, string_buff2);
 
-      /* MBit per second */
-      snprintf(string_buff, SUM_STR_MAX, "Avg. Mbit/sec: %.3f",
-              summary.filtered_bytes * 8.0 / (seconds * 1000.0 * 1000.0));
-      add_string_to_box(string_buff, filter_box);
-    }
+  /* Packets per second */
+  if (seconds > 0){
+    g_snprintf(string_buff, SUM_STR_MAX, "%.3f", summary.packet_count/seconds);
   } else {
-    /* Display filter */
-    snprintf(string_buff, SUM_STR_MAX, "Display filter: none");
-    add_string_to_box(string_buff, filter_box);
+    strcpy(string_buff, "");
+  }
+  if(summary.dfilter && disp_seconds > 0){
+    g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", summary.filtered_count/disp_seconds);
+  } else {
+    strcpy(string_buff2, "");
   }
+  add_string_to_list(list, "Avg. packets/sec", string_buff, string_buff2);
 
-  /* Capture Frame */
-  capture_fr = gtk_frame_new("Capture");
-  gtk_container_add(GTK_CONTAINER(main_vb), capture_fr);
-  gtk_widget_show(capture_fr);
+  /* Packet size */
+  if (summary.packet_count > 0){
+    g_snprintf(string_buff, SUM_STR_MAX, "%.3f bytes",
+      /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
+      (float) (gint64) (summary.bytes/summary.packet_count) );
+  } else {
+    strcpy(string_buff, "");
+  }
+  if (summary.dfilter && summary.filtered_count > 0){
+    g_snprintf(string_buff2, SUM_STR_MAX, "%.3f bytes",
+          /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
+          (float) (gint64) summary.filtered_bytes/summary.filtered_count);
+  } else {
+    strcpy(string_buff2, "");
+  }
+  add_string_to_list(list, "Avg. packet size", string_buff, string_buff2);
 
-  capture_box = gtk_vbox_new(FALSE, 3);
-  gtk_container_border_width(GTK_CONTAINER(capture_box), 5);
-  gtk_container_add(GTK_CONTAINER(capture_fr), capture_box);
-  gtk_widget_show(capture_box);
+  /* Byte count */
+  g_snprintf(string_buff, SUM_STR_MAX, "%" PRIu64, summary.bytes);
+  if (summary.dfilter && summary.filtered_count > 0){
+    g_snprintf(string_buff2, SUM_STR_MAX, "%" PRIu64, summary.filtered_bytes);
+  } else {
+    strcpy(string_buff2, "");
+  }
+  add_string_to_list(list, "Bytes", string_buff, string_buff2);
 
-  /* interface */
-  if (summary.iface) {
-    snprintf(string_buff, SUM_STR_MAX, "Interface: %s", summary.iface);
+  /* Bytes per second */
+  if (seconds > 0){
+    /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
+    g_snprintf(string_buff, SUM_STR_MAX, "%.3f", ((gint64) summary.bytes)/seconds );
   } else {
-    sprintf(string_buff, "Interface: unknown");
+    strcpy(string_buff, "");
   }
-  add_string_to_box(string_buff, capture_box);
+  if (summary.dfilter && disp_seconds > 0){
+    /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
+    g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", ((gint64) summary.filtered_bytes)/disp_seconds );
+  } else {
+    strcpy(string_buff2, "");
+  }
+  add_string_to_list(list, "Avg. bytes/sec", string_buff, string_buff2);
 
-#ifdef HAVE_LIBPCAP
-  /* Capture filter */
-  if (summary.cfilter && summary.cfilter[0] != '\0') {
-    snprintf(string_buff, SUM_STR_MAX, "Capture filter: %s", summary.cfilter);
+  /* MBit per second */
+  if (seconds > 0){
+    /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
+    g_snprintf(string_buff, SUM_STR_MAX, "%.3f", ((gint64) summary.bytes) * 8.0 / (seconds * 1000.0 * 1000.0));
   } else {
-    sprintf(string_buff, "Capture filter: none");
+    strcpy(string_buff, "");
   }
-  add_string_to_box(string_buff, capture_box);
-#endif
+  if (summary.dfilter && disp_seconds > 0){
+    g_snprintf(string_buff2, SUM_STR_MAX, "%.3f", 
+          /* MSVC cannot convert from unsigned __int64 to float, so first convert to signed __int64 */
+          ((gint64) summary.filtered_bytes) * 8.0 / (disp_seconds * 1000.0 * 1000.0));
+  } else {
+    strcpy(string_buff2, "");
+  }
+  add_string_to_list(list, "Avg. MBit/sec", string_buff, string_buff2);
 
+  
   /* Button row. */
-  bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
+  if(topic_available(HELP_STATS_SUMMARY_DIALOG)) {
+    bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
+  } else {
+    bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
+  }
   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
-  gtk_widget_show(bbox);
 
   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
-  SIGNAL_CONNECT_OBJECT(close_bt, "clicked", gtk_widget_destroy, sum_open_w);
-  gtk_widget_grab_default(close_bt);
+  window_set_cancel_button(sum_open_w, close_bt, window_cancel_button_cb);
+
+  if(topic_available(HELP_STATS_SUMMARY_DIALOG)) {
+    help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
+    SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_STATS_SUMMARY_DIALOG);
+  }
+
+  gtk_widget_grab_focus(close_bt);
 
-  /* Catch the "key_press_event" signal in the window, so that we can catch
-     the ESC key being pressed and act as if the "Close" button had
-     been selected. */
-  dlg_set_cancel(sum_open_w, close_bt);
+  SIGNAL_CONNECT(sum_open_w, "delete_event", window_delete_event_cb, NULL);
 
-  gtk_window_set_position(GTK_WINDOW(sum_open_w), GTK_WIN_POS_MOUSE);
-  gtk_widget_show(sum_open_w);
+  gtk_widget_show_all(sum_open_w);
+  window_present(sum_open_w);
 }