summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2012-09-15 20:51:06 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2012-09-17 21:03:30 -0400
commit7148dea82795b81311c46dfb11d09c8fa2b13452 (patch)
tree5635ac7d83b99b8c774af655c5fdfa2a443ef900
parentfa2f1b6eb06e65e6426cc3a488c6cb5c960e7d49 (diff)
downloadsubsurface-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>
-rw-r--r--dive.h2
-rw-r--r--gtk-gui.c27
-rw-r--r--linux.c2
-rw-r--r--macos.c2
-rw-r--r--windows.c2
5 files changed, 23 insertions, 12 deletions
diff --git a/dive.h b/dive.h
index 85d4b9fa4..177897099 100644
--- a/dive.h
+++ b/dive.h
@@ -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
diff --git a/gtk-gui.c b/gtk-gui.c
index 49dc36f12..9b15cb4ee 100644
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -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);
}
diff --git a/linux.c b/linux.c
index 8d9f6f8ce..47a00a059 100644
--- a/linux.c
+++ b/linux.c
@@ -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;
diff --git a/macos.c b/macos.c
index d37b0a925..d76225472 100644
--- a/macos.c
+++ b/macos.c
@@ -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;
diff --git a/windows.c b/windows.c
index 87bcaf6bd..b6e10bb71 100644
--- a/windows.c
+++ b/windows.c
@@ -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;