Add routines to file.c to indicate whether:
authorGuy Harris <guy@alum.mit.edu>
Mon, 1 Apr 2013 23:44:29 +0000 (23:44 -0000)
committerGuy Harris <guy@alum.mit.edu>
Mon, 1 Apr 2013 23:44:29 +0000 (23:44 -0000)
a save can be done ("can" in the sense of "there's something to
save" and in the sense of "we can write that something out");

a "save as" can be done (in the sense of "we can write what we
have out");

there's unsaved data to save (which might be unsaved changes or
might be a temporary file full of packets);

and use them as appropriate.  This means that the "unsaved data"
indicator in the UI will be turned on for temporary files full of
packets as well as for files with unsaved changes; that's what we want.

svn path=/trunk/; revision=48693

file.c
file.h
ui/gtk/capture_file_dlg.c
ui/gtk/main_menubar.c
ui/gtk/main_titlebar.c
ui/gtk/main_toolbar.c
ui/qt/main_window.cpp

diff --git a/file.c b/file.c
index eea204e2d9cb4d6a6c4684e7536373c0b47f3f2f..b9d62a3c6558d549ef582c56714ebc14119ea23b 100644 (file)
--- a/file.c
+++ b/file.c
@@ -3973,14 +3973,102 @@ cf_can_write_with_wiretap(capture_file *cf)
 {
   /* We don't care whether we support the comments in this file or not;
      if we can't, we'll offer the user the option of discarding the
-     comments.
-
-     XXX - we shouldn't offer the option of adding or editing comments
-     of a particular type if we don't support that particular type of
-     comment in any file format. */
+     comments. */
   return wtap_dump_can_write(cf->linktypes, 0);
 }
 
+/*
+ * Should we let the user do a save?
+ *
+ * We should if:
+ *
+ *  the file has unsaved changes, and we can save it in some
+ *  format through Wiretap
+ *
+ * or
+ *
+ *  the file is a temporary file and has no unsaved changes (so
+ *  that "saving" it just means copying it).
+ *
+ * XXX - we shouldn't allow files to be edited if they can't be saved,
+ * so cf->unsaved_changes should be true only if the file can be saved.
+ *
+ * We don't care whether we support the comments in this file or not;
+ * if we can't, we'll offer the user the option of discarding the
+ * comments.
+ */
+gboolean
+cf_can_save(capture_file *cf)
+{
+  if (cf->unsaved_changes && wtap_dump_can_write(cf->linktypes, 0)) {
+    /* Saved changes, and we can write it out with Wiretap. */
+    return TRUE;
+  }
+
+  if (cf->is_tempfile && !cf->unsaved_changes) {
+    /*
+     * Temporary file with no unsaved changes, so we can just do a
+     * raw binary copy.
+     */
+    return TRUE;
+  }
+
+  /* Nothing to save. */
+  return FALSE;
+}
+
+/*
+ * Should we let the user do a "save as"?
+ *
+ * That's true if:
+ *
+ *  we can save it in some format through Wiretap
+ *
+ * or
+ *
+ *  the file is a temporary file and has no unsaved changes (so
+ *  that "saving" it just means copying it).
+ *
+ * XXX - we shouldn't allow files to be edited if they can't be saved,
+ * so cf->unsaved_changes should be true only if the file can be saved.
+ *
+ * We don't care whether we support the comments in this file or not;
+ * if we can't, we'll offer the user the option of discarding the
+ * comments.
+ */
+gboolean
+cf_can_save_as(capture_file *cf)
+{
+  if (wtap_dump_can_write(cf->linktypes, 0)) {
+    /* We can write it out with Wiretap. */
+    return TRUE;
+  }
+
+  if (cf->is_tempfile && !cf->unsaved_changes) {
+    /*
+     * Temporary file with no unsaved changes, so we can just do a
+     * raw binary copy.
+     */
+    return TRUE;
+  }
+
+  /* Nothing to save. */
+  return FALSE;
+}
+
+/*
+ * Does this file have unsaved data?
+ */
+gboolean
+cf_not_saved(capture_file *cf)
+{
+  /*
+   * If this is a temporary file, or a file with unsaved changes, it
+   * has unsaved data.
+   */
+  return cf->is_tempfile || cf->unsaved_changes;
+}
+
 /*
  * Quick scan to find packet offsets.
  */
diff --git a/file.h b/file.h
index ae9fb8cc00abd3ea03b14a88dcab06e27211cd13..774b5264eb0ac147759227cdf36e9ef98580fbe2 100644 (file)
--- a/file.h
+++ b/file.h
@@ -201,14 +201,39 @@ void cf_fake_continue_tail(capture_file *cf);
 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
+ * Determine whether this capture file (or a range of it) can be written
  * in any format using Wiretap rather than by copying the raw data.
  *
  * @param cf the capture file to check
- * @return TRUE if it can be saved, FALSE if it can't
+ * @return TRUE if it can be written, FALSE if it can't
  */
 gboolean cf_can_write_with_wiretap(capture_file *cf);
 
+/**
+ * Determine whether this capture file can be saved with a "save" operation;
+ * if there's nothing unsaved, it can't.
+ *
+ * @param cf the capture file to check
+ * @return TRUE if it can be saved, FALSE if it can't
+ */
+gboolean cf_can_save(capture_file *cf);
+
+/**
+ * Determine whether this capture file can be saved with a "save as" operation.
+ *
+ * @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);
+
+/**
+ * Determine whether this capture file has unsaved data.
+ *
+ * @param cf the capture file to check
+ * @return TRUE if it has unsaved data, FALSE if it doesn't
+ */
+gboolean cf_not_saved(capture_file *cf);
+
 /**
  * Save all packets in a capture file to a new file, and, if that succeeds,
  * make that file the current capture file.  If there's already a file with
index cabbd712e36ade92d2a2c73b0b73101c1856d9e7..2b460159c555e141c0278acdcacc45654bf11279 100644 (file)
@@ -1027,9 +1027,9 @@ file_merge_cmd_cb(GtkWidget *widget, gpointer data _U_) {
   gint       response;
 
   if (prefs.gui_ask_unsaved) {
-    if (cfile.is_tempfile || cfile.unsaved_changes) {
-      /* This is a temporary capture file or has unsaved changes; ask the
-         user whether to save the capture. */
+    if (cf_not_saved(&cfile)) {
+      /* This file has unsaved data; ask the user whether to save the
+         capture. */
       if (cfile.is_tempfile) {
         msg_dialog = gtk_message_dialog_new(GTK_WINDOW(top_level),
                                             (GtkDialogFlags)(GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT),
@@ -1135,10 +1135,9 @@ test_file_close(capture_file *cf, gboolean from_quit, const char *before_what)
     capture_in_progress = FALSE;
 
   if (prefs.gui_ask_unsaved) {
-    if (cf->is_tempfile || capture_in_progress || cf->unsaved_changes) {
-      /* This is a temporary capture file, or there's a capture in
-         progress, or the file has unsaved changes; ask the user whether
-         to save the data. */
+    if (cf_not_saved(cf) || capture_in_progress) {
+      /* This file has unsaved data or there's a capture in progress;
+         ask the user whether to save the data. */
       if (cf->is_tempfile) {
         msg_dialog = gtk_message_dialog_new(GTK_WINDOW(top_level),
                                             (GtkDialogFlags)(GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT),
index 0670ff00ad2422df878cffb90fa6b21a8ec4a593..7bcf4bf6d24ede9abd87cb449df306119cbbdb8e 100644 (file)
@@ -4771,33 +4771,10 @@ set_menus_for_capture_file(capture_file *cf)
     } else {
         set_menu_sensitivity(ui_manager_main_menubar, "/Menubar/FileMenu/Merge", cf_can_write_with_wiretap(cf));
         set_menu_sensitivity(ui_manager_main_menubar, "/Menubar/FileMenu/Close", TRUE);
-        /*
-         * "Save" should be available only if:
-         *
-         *  the file has unsaved changes, and we can save it in some
-         *  format through Wiretap
-         *
-         * or
-         *
-         *  the file is a temporary file and has no unsaved changes (so
-         *  that "saving" it just means copying it).
-         */
         set_menu_sensitivity(ui_manager_main_menubar, "/Menubar/FileMenu/Save",
-                             (cf->unsaved_changes && cf_can_write_with_wiretap(cf)) ||
-                             (cf->is_tempfile && !cf->unsaved_changes));
-        /*
-         * "Save As..." should be available only if:
-         *
-         *  we can save it in some format through Wiretap
-         *
-         * or
-         *
-         *  the file is a temporary file and has no unsaved changes (so
-         *  that "saving" it just means copying it).
-         */
+                             cf_can_save(cf));
         set_menu_sensitivity(ui_manager_main_menubar, "/Menubar/FileMenu/SaveAs",
-                             cf_can_write_with_wiretap(cf) ||
-                             (cf->is_tempfile && !cf->unsaved_changes));
+                             cf_can_save_as(cf));
         /*
          * "Export Specified Packets..." should be available only if
          * we can write the file out in at least one format.
index edfc2b10946cb9efa29fdc012dd5b7b3d4556300..0d0a8cc2f1beed878a0aa80b5c2a30d1158bf585 100644 (file)
@@ -95,7 +95,7 @@ set_titlebar_for_capture_file(capture_file *cf)
 
   if (cf && cf->filename) {
     display_name = cf_get_display_name(cf);
-    window_name = g_strdup_printf("%s%s", cf->unsaved_changes ? "*" : "",
+    window_name = g_strdup_printf("%s%s", cf_not_saved(cf) ? "*" : "",
                                   display_name);
     g_free(display_name);
     main_set_window_name(window_name);
index 2abec2bea01778123109e6b2147da1486cc7b4ad..5d11fe9d86c8490ed9a8ff3a05d5ef1f4313a1a6 100644 (file)
@@ -105,26 +105,15 @@ void set_toolbar_for_capture_file(capture_file *cf) {
         if (cf == NULL || cf->state == FILE_READ_IN_PROGRESS) {
             /* We have no open capture file, or we have one but we're in
                the process of reading it.  Disable everything having to
-               do with the file*/
+               do with the file*/
             gtk_widget_set_sensitive(GTK_WIDGET(save_button), FALSE);
             gtk_widget_set_sensitive(GTK_WIDGET(close_button), FALSE);
             gtk_widget_set_sensitive(GTK_WIDGET(reload_button), FALSE);
         } else {
             /* We have an open capture file and we're finished reading it.
-               Enable "Save" if and only if:
-
-                  the file has unsaved changes, and we can save it in some
-                  format through Wiretap
-
-               or
-
-                  the file is a temporary file and has no unsaved changes (so
-                  that "saving" it just means copying it).
-
-               Enable "Close" and "Reload". */
-            gtk_widget_set_sensitive(GTK_WIDGET(save_button),
-                                     (cf->unsaved_changes && cf_can_write_with_wiretap(cf)) ||
-                                     (cf->is_tempfile && !cf->unsaved_changes));
+               Enable "Save" if and only if we have something to save and
+               can do so.  Enable "Close" and "Reload" unconditionally. */
+            gtk_widget_set_sensitive(GTK_WIDGET(save_button), cf_can_save(cf));
             gtk_widget_set_sensitive(GTK_WIDGET(close_button), TRUE);
             gtk_widget_set_sensitive(GTK_WIDGET(reload_button), TRUE);
         }
index 96eb9ea01bba7586d51c15a98fbb7a85fdff1f5a..47b4cf556d9a78a16b8d8709c29652a105e7d9b4 100644 (file)
@@ -372,14 +372,14 @@ void MainWindow::mergeCaptureFile()
         return;
 
     if (prefs.gui_ask_unsaved) {
-        if (cap_file_->is_tempfile || cap_file_->unsaved_changes) {
+        if (cf_not_saved(cap_file_)) {
             QMessageBox msg_dialog;
             gchar *display_basename;
             int response;
 
             msg_dialog.setIcon(QMessageBox::Question);
-            /* This is a temporary capture file or has unsaved changes; ask the
-               user whether to save the capture. */
+            /* This file has unsaved data; ask the user whether to save
+               the capture. */
             if (cap_file_->is_tempfile) {
                 msg_dialog.setText(tr("Save packets before merging?"));
                 msg_dialog.setInformativeText(tr("A temporary capture file can't be merged."));
@@ -980,7 +980,7 @@ bool MainWindow::testCaptureFileClose(bool from_quit, QString &before_what) {
 #endif
 
     if (prefs.gui_ask_unsaved) {
-        if (cap_file_->is_tempfile || capture_in_progress || cap_file_->unsaved_changes) {
+        if (cf_not_saved(cap_file_) || capture_in_progress) {
             QMessageBox msg_dialog;
             QString question;
             QPushButton *default_button;
@@ -988,9 +988,8 @@ bool MainWindow::testCaptureFileClose(bool from_quit, QString &before_what) {
 
             msg_dialog.setIcon(QMessageBox::Question);
 
-            /* This is a temporary capture file, or there's a capture in
-               progress, or the file has unsaved changes; ask the user whether
-               to save the data. */
+            /* This file has unsaved data or there's a capture in
+               progress; ask the user whether to save the data. */
             if (cap_file_->is_tempfile) {
 
                 msg_dialog.setText(tr("You have unsaved packets"));
@@ -1121,7 +1120,7 @@ void MainWindow::setTitlebarForCaptureFile()
 
     if (cap_file_ && cap_file_->filename) {
         display_name = cf_get_display_name(cap_file_);
-        setWindowModified(cap_file_->unsaved_changes);
+        setWindowModified(cf_not_saved(cap_file_));
         // Clear the window title so that setWindowFilePath does something
         setWindowTitle(NULL);
         setWindowFilePath(display_name);
@@ -1169,33 +1168,8 @@ void MainWindow::setMenusForCaptureFile(bool force_disable)
         main_ui_->actionFileMerge->setEnabled(cf_can_write_with_wiretap(cap_file_));
 
         main_ui_->actionFileClose->setEnabled(true);
-        /*
-         * "Save" should be available only if:
-         *
-         *  the file has unsaved changes, and we can save it in some
-         *  format through Wiretap
-         *
-         * or
-         *
-         *  the file is a temporary file and has no unsaved changes (so
-         *  that "saving" it just means copying it).
-         */
-        main_ui_->actionFileSave->setEnabled(
-                    (cap_file_->unsaved_changes && cf_can_write_with_wiretap(cap_file_)) ||
-                    (cap_file_->is_tempfile && !cap_file_->unsaved_changes));
-        /*
-         * "Save As..." should be available only if:
-         *
-         *  we can save it in some format through Wiretap
-         *
-         * or
-         *
-         *  the file is a temporary file and has no unsaved changes (so
-         *  that "saving" it just means copying it).
-         */
-        main_ui_->actionFileSaveAs->setEnabled(
-                    cf_can_write_with_wiretap(cap_file_) ||
-                    (cap_file_->is_tempfile && !cap_file_->unsaved_changes));
+        main_ui_->actionFileSave->setEnabled(cf_can_save(cap_file_));
+        main_ui_->actionFileSaveAs->setEnabled(cf_can_save_as(cap_file_));
         /*
          * "Export Specified Packets..." should be available only if
          * we can write the file out in at least one format.