aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2013-04-01 23:44:29 +0000
committerGuy Harris <guy@alum.mit.edu>2013-04-01 23:44:29 +0000
commitd94275940164f0b40d12d1caa709b113360491c2 (patch)
treec2db98b41c1d80160f286af62b5d94f6959359c7
parentf8965b7b2cddad13ca785b283dc05d59c3af2438 (diff)
Add routines to file.c to indicate whether:
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
-rw-r--r--file.c98
-rw-r--r--file.h29
-rw-r--r--ui/gtk/capture_file_dlg.c13
-rw-r--r--ui/gtk/main_menubar.c27
-rw-r--r--ui/gtk/main_titlebar.c2
-rw-r--r--ui/gtk/main_toolbar.c19
-rw-r--r--ui/qt/main_window.cpp44
7 files changed, 142 insertions, 90 deletions
diff --git a/file.c b/file.c
index eea204e2d9..b9d62a3c65 100644
--- a/file.c
+++ b/file.c
@@ -3973,15 +3973,103 @@ 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.
*/
static cf_read_status_t
diff --git a/file.h b/file.h
index ae9fb8cc00..774b5264eb 100644
--- a/file.h
+++ b/file.h
@@ -201,15 +201,40 @@ 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
* that name, do a "safe save", writing to a temporary file in the same
diff --git a/ui/gtk/capture_file_dlg.c b/ui/gtk/capture_file_dlg.c
index cabbd712e3..2b460159c5 100644
--- a/ui/gtk/capture_file_dlg.c
+++ b/ui/gtk/capture_file_dlg.c
@@ -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),
diff --git a/ui/gtk/main_menubar.c b/ui/gtk/main_menubar.c
index 0670ff00ad..7bcf4bf6d2 100644
--- a/ui/gtk/main_menubar.c
+++ b/ui/gtk/main_menubar.c
@@ -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.
diff --git a/ui/gtk/main_titlebar.c b/ui/gtk/main_titlebar.c
index edfc2b1094..0d0a8cc2f1 100644
--- a/ui/gtk/main_titlebar.c
+++ b/ui/gtk/main_titlebar.c
@@ -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);
diff --git a/ui/gtk/main_toolbar.c b/ui/gtk/main_toolbar.c
index 2abec2bea0..5d11fe9d86 100644
--- a/ui/gtk/main_toolbar.c
+++ b/ui/gtk/main_toolbar.c
@@ -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);
}
diff --git a/ui/qt/main_window.cpp b/ui/qt/main_window.cpp
index 96eb9ea01b..47b4cf556d 100644
--- a/ui/qt/main_window.cpp
+++ b/ui/qt/main_window.cpp
@@ -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.