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>
This commit is contained in:
Dirk Hohndel 2012-09-15 20:51:06 -07:00
parent fa2f1b6eb0
commit 7148dea827
5 changed files with 23 additions and 12 deletions

2
dive.h
View file

@ -463,7 +463,7 @@ const char *monthname(int mon);
extern const char *star_strings[];
extern const char *default_filename;
extern char *existing_filename;
extern const char *existing_filename;
extern const char *subsurface_default_filename(void);
#define AIR_PERMILLE 209

View file

@ -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);
}

View file

@ -69,7 +69,7 @@ const char *subsurface_icon_name()
const char *subsurface_default_filename()
{
if (default_filename) {
return default_filename;
return strdup(default_filename);
} else {
const char *home, *user;
char *buffer;

View file

@ -89,7 +89,7 @@ const char *subsurface_icon_name()
const char *subsurface_default_filename()
{
if (default_filename) {
return default_filename;
return strdup(default_filename);
} else {
const char *home, *user;
char *buffer;

View file

@ -98,7 +98,7 @@ const char *subsurface_icon_name()
const char *subsurface_default_filename()
{
if (default_filename) {
return default_filename;
return strdup(default_filename);
} else {
char datapath[MAX_PATH];
const char *user;