diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2012-09-15 20:51:06 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2012-09-17 21:03:30 -0400 |
commit | 7148dea82795b81311c46dfb11d09c8fa2b13452 (patch) | |
tree | 5635ac7d83b99b8c774af655c5fdfa2a443ef900 /gtk-gui.c | |
parent | fa2f1b6eb06e65e6426cc3a488c6cb5c960e7d49 (diff) | |
download | subsurface-7148dea82795b81311c46dfb11d09c8fa2b13452.tar.gz |
Once again improve existing filename handling
Several potential problems.
- we could end up dereferencing exiting_filename when it was NULL
- we could free the default_filename by mistake -
subsurface_default_filename always needs to return a copy of it
- closing the existing file before opening a new one repopulated the
existing_filename with the default filename - preventing the opened
file to become the new existing filename
Also, make existing filename a const char * and make file_open have the
same sensible default folder behavior as the other file related functions.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'gtk-gui.c')
-rw-r--r-- | gtk-gui.c | 27 |
1 files changed, 19 insertions, 8 deletions
@@ -25,7 +25,7 @@ GtkWidget *vpane, *hpane; GtkWidget *notebook; int error_count; -char *existing_filename; +const char *existing_filename; const char *divelist_font; const char *default_filename; @@ -154,8 +154,14 @@ static void file_save_as(GtkWidget *w, gpointer data) NULL); gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE); - current_dir = g_path_get_dirname(existing_filename); - current_file = g_path_get_basename(existing_filename); + if (existing_filename) { + current_dir = g_path_get_dirname(existing_filename); + current_file = g_path_get_basename(existing_filename); + } else { + const char *current_default = subsurface_default_filename(); + current_dir = g_path_get_dirname(current_default); + current_file = g_path_get_basename(current_default); + } gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), current_dir); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), current_file); @@ -244,7 +250,7 @@ static void file_close(GtkWidget *w, gpointer data) return; if (existing_filename) - free(existing_filename); + free((void *)existing_filename); existing_filename = NULL; /* free the dives and trips */ @@ -278,9 +284,12 @@ static void file_open(GtkWidget *w, gpointer data) { GtkWidget *dialog; GtkFileFilter *filter; + const char *current_default; - /* first, close the existing file, if any */ + /* first, close the existing file, if any, and forget its name */ file_close(w, data); + free((void *)existing_filename); + existing_filename = NULL; dialog = gtk_file_chooser_dialog_new("Open File", GTK_WINDOW(main_window), @@ -288,6 +297,8 @@ static void file_open(GtkWidget *w, gpointer data) GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); + current_default = subsurface_default_filename(); + gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), current_default); /* when opening the data file we should allow only one file to be chosen */ gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE); @@ -669,7 +680,7 @@ static void preferences_dialog(GtkWidget *w, gpointer data) * update existing_filename */ if (existing_filename) { if (strcmp(current_default, existing_filename) == 0) { - free(existing_filename); + free((void *)existing_filename); existing_filename = strdup(new_default); } } @@ -1081,7 +1092,7 @@ void exit_ui(void) if (default_filename) free((char *)default_filename); if (existing_filename) - free(existing_filename); + free((void *)existing_filename); } typedef struct { @@ -1469,7 +1480,7 @@ void set_filename(const char *filename, gboolean force) { if (!force && existing_filename) return; - free(existing_filename); + free((void *)existing_filename); if (filename) existing_filename = strdup(filename); } |