Don't enable "Save As" if you don't have an unsaved live capture file
authorGuy Harris <guy@alum.mit.edu>
Wed, 14 Mar 2007 19:30:19 +0000 (19:30 -0000)
committerGuy Harris <guy@alum.mit.edu>
Wed, 14 Mar 2007 19:30:19 +0000 (19:30 -0000)
and there are no formats in which the file can be saved by some means
other than copying the raw data; "Save As" isn't a very useful function
in that case, and that prevents us from having an empty list of formats
in which the file can be saved.

svn path=/trunk/; revision=21032

file.c
file.h
gtk/main.c
gtk/menu.c
menu.h

diff --git a/file.c b/file.c
index cc61a67a2a8c91da1efdfbae9751b8168528d93a..a3de5552d0dbefa828b5bfc44ab13a1100a614c1 100644 (file)
--- a/file.c
+++ b/file.c
@@ -3332,6 +3332,28 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
   return TRUE;
 }
 
+/*
+ * Can this capture file be saved in any format except by copying the raw data?
+ */
+gboolean
+cf_can_save_as(capture_file *cf)
+{
+  int ft;
+
+  for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) {
+    /* To save a file with Wiretap, Wiretap has to handle that format,
+       and its code to handle that format must be able to write a file
+       with this file's encapsulation type. */
+    if (wtap_dump_can_open(ft) && wtap_dump_can_write_encap(ft, cf->lnk_t)) {
+      /* OK, we can write it out in this type. */
+      return TRUE;
+    }
+  }
+
+  /* No, we couldn't save it in any format. */
+  return FALSE;
+}
+
 cf_status_t
 cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_format, gboolean compressed)
 {
diff --git a/file.h b/file.h
index 716ea5da99e5c45ae6f3e4f51d67f94a968d0284..c5c00b20957828f00caad122792f177812643707 100644 (file)
--- a/file.h
+++ b/file.h
@@ -153,6 +153,15 @@ cf_read_status_t cf_continue_tail(capture_file *cf, int to_read, int *err);
  */
 cf_read_status_t cf_finish_tail(capture_file *cf, int *err);
 
+/**
+ * Determine whether this capture file (or a range of it) can be saved
+ * (except by copying the raw file data).
+ * 
+ * @param cf the capture file to check
+ * @return TRUE if it can be saved, FALSE if it can't
+ */
+gboolean cf_can_save_as(capture_file *cf);
+
 /**
  * Save a capture file (or a range of it).
  * 
index 963ac567e17d543bd430489dd29ecc2b292c09e1..028a5b619e6ef53190b079abd998615c3e2ab1fa 100644 (file)
@@ -1494,8 +1494,7 @@ main_cf_cb_file_closing(capture_file *cf)
     set_main_window_name("The Wireshark Network Analyzer");
 
     /* Disable all menu items that make sense only if you have a capture. */
-    set_menus_for_capture_file(FALSE);
-    set_menus_for_unsaved_capture_file(FALSE);
+    set_menus_for_capture_file(NULL);
     set_menus_for_captured_packets(FALSE);
     set_menus_for_selected_packet(cf);
     set_menus_for_capture_in_progress(FALSE);
@@ -1542,9 +1541,8 @@ main_cf_cb_file_read_finished(capture_file *cf)
     set_display_filename(cf);
 
     /* Enable menu items that make sense if you have a capture file you've
-     finished reading. */
-    set_menus_for_capture_file(TRUE);
-    set_menus_for_unsaved_capture_file(!cf->user_saved);
+       finished reading. */
+    set_menus_for_capture_file(cf);
 
     /* Enable menu items that make sense if you have some captured packets. */
     set_menus_for_captured_packets(TRUE);
@@ -1745,9 +1743,8 @@ main_cf_cb_live_capture_update_finished(capture_file *cf)
     set_menus_for_capture_in_progress(FALSE);
 
     /* Enable menu items that make sense if you have a capture file
-     you've finished reading. */
-    set_menus_for_capture_file(TRUE);
-    set_menus_for_unsaved_capture_file(!cf->user_saved);
+       you've finished reading. */
+    set_menus_for_capture_file(cf);
 
     /* Set up main window for a capture file. */
     main_set_for_capture_file(TRUE);
@@ -1952,7 +1949,7 @@ main_cf_cb_file_safe_failed(gpointer data _U_)
 static void
 main_cf_cb_file_safe_reload_finished(gpointer data _U_)
 {
-    set_menus_for_unsaved_capture_file(FALSE);
+    set_menus_for_capture_file(&cfile);
 }
 
 static void main_cf_callback(gint event, gpointer data, gpointer user_data _U_)
index 7266b9e098fde58b7321cab52d6d20df71f84e68..3a5040fae6f29dff1fadd6a55993d4805e726500 100644 (file)
@@ -886,8 +886,7 @@ menus_init(void) {
     merge_all_tap_menus(tap_menu_tree_root);
 
     /* Initialize enabled/disabled state of menu items */
-    set_menus_for_unsaved_capture_file(FALSE);
-    set_menus_for_capture_file(FALSE);
+    set_menus_for_capture_file(NULL);
 #if 0
     /* Un-#if this when we actually implement Cut/Copy/Paste.
        Then make sure you enable them when they can be done. */
@@ -2088,32 +2087,41 @@ popup_menu_handler(GtkWidget *widget, GdkEvent *event, gpointer data)
 }
 
 /* Enable or disable menu items based on whether you have a capture file
-   you've finished reading. */
+   you've finished reading and, if you have one, whether it's been saved
+   and whether it could be saved except by copying the raw packet data. */
 void
-set_menus_for_capture_file(gboolean have_capture_file)
+set_menus_for_capture_file(capture_file *cf)
 {
-  set_menu_sensitivity(main_menu_factory, "/File/Open...", have_capture_file);
-  set_menu_sensitivity(main_menu_factory, "/File/Open Recent", have_capture_file);
-  set_menu_sensitivity(main_menu_factory, "/File/Merge...", have_capture_file);
-  set_menu_sensitivity(main_menu_factory, "/File/Close", have_capture_file);
-  set_menu_sensitivity(main_menu_factory, "/File/Save As...",
-      have_capture_file);
-  set_menu_sensitivity(main_menu_factory, "/File/Export", have_capture_file);
-  set_menu_sensitivity(main_menu_factory, "/View/Reload", have_capture_file);
-  set_toolbar_for_capture_file(have_capture_file);
+  if (cf == NULL) {
+    /* We have no capture file */
+    set_menu_sensitivity(main_menu_factory, "/File/Merge...", FALSE);
+    set_menu_sensitivity(main_menu_factory, "/File/Close", FALSE);
+    set_menu_sensitivity(main_menu_factory, "/File/Save", FALSE);
+    set_menu_sensitivity(main_menu_factory, "/File/Save As...", FALSE);
+    set_menu_sensitivity(main_menu_factory, "/File/Export", FALSE);
+    set_menu_sensitivity(main_menu_factory, "/View/Reload", FALSE);
+    set_toolbar_for_capture_file(FALSE);
+    set_toolbar_for_unsaved_capture_file(FALSE);
+  } else {
+    set_menu_sensitivity(main_menu_factory, "/File/Merge...", TRUE);
+    set_menu_sensitivity(main_menu_factory, "/File/Close", TRUE);
+    set_menu_sensitivity(main_menu_factory, "/File/Save", !cf->user_saved);
+    /*
+     * "Save As..." works only if we can write the file out in at least
+     * one format (so we can save the whole file or just a subset) or
+     * if we have an unsaved capture (so writing the whole file out
+     * with a raw data copy makes sense).
+     */
+    set_menu_sensitivity(main_menu_factory, "/File/Save As...",
+        cf_can_save_as(cf) || !cf->user_saved);
+    set_menu_sensitivity(main_menu_factory, "/File/Export", TRUE);
+    set_menu_sensitivity(main_menu_factory, "/View/Reload", TRUE);
+    set_toolbar_for_unsaved_capture_file(!cf->user_saved);
+    set_toolbar_for_capture_file(TRUE);
+  }
   packets_bar_update();
 }
 
-/* Enable or disable menu items based on whether you have an unsaved
-   capture file you've finished reading. */
-void
-set_menus_for_unsaved_capture_file(gboolean have_unsaved_capture_file)
-{
-  set_menu_sensitivity(main_menu_factory, "/File/Save",
-      have_unsaved_capture_file);
-  set_toolbar_for_unsaved_capture_file(have_unsaved_capture_file);
-}
-
 /* Enable or disable menu items based on whether there's a capture in
    progress. */
 void
diff --git a/menu.h b/menu.h
index 9664ac02a654ec4876795933ba3df5aa8f2c6a4b..686834addc48196b61fd466110db2b067023b8e8 100644 (file)
--- a/menu.h
+++ b/menu.h
@@ -38,12 +38,9 @@ void add_menu_recent_capture_file(gchar *file);
 /* Routines to enable or disable sets of menu items. */
 
 /* Enable or disable menu items based on whether you have a capture file
-   you've finished reading. */
-void set_menus_for_capture_file(gboolean);
-
-/* Enable or disable menu items based on whether you have an unsaved
-   capture file you've finished reading. */
-void set_menus_for_unsaved_capture_file(gboolean);
+   you've finished reading and, if you have one, whether it's been saved
+   and whether it could be saved except by copying the raw packet data. */
+void set_menus_for_capture_file(capture_file *);
 
 /* Enable or disable menu items based on whether there's a capture in
    progress. */