diff options
author | Jeremy Henty <onepoint@starurchin.org> | 2013-01-08 02:00:03 +0000 |
---|---|---|
committer | Jeremy Henty <onepoint@starurchin.org> | 2013-01-08 02:00:03 +0000 |
commit | 93559749353b0611fcd591461ae547b9fb8a35e1 (patch) | |
tree | 0e88ec64701d041ba28251c1b3b9c1687dcd70ca /src | |
parent | e8c6204b4e737bd61945d1e622933f68099873fd (diff) |
uicmd.cc: when saving, test whether the file exists and prompt before overwriting.
Diffstat (limited to 'src')
-rw-r--r-- | src/uicmd.cc | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/src/uicmd.cc b/src/uicmd.cc index 54efa7a4..a0c5760c 100644 --- a/src/uicmd.cc +++ b/src/uicmd.cc @@ -18,6 +18,7 @@ #include <stdlib.h> /* for qsort */ #include <math.h> /* for rint */ #include <limits.h> /* for UINT_MAX */ +#include <sys/stat.h> #include <FL/Fl.H> #include <FL/Fl_Widget.H> @@ -856,19 +857,64 @@ void a_UIcmd_init(void) } /* + * Check a file to save to. + */ +static int UIcmd_save_file_check(const char *name) +{ + struct stat ss; + if (stat(name, &ss) == 0) { + Dstr *ds; + int ch; + ds = dStr_sized_new(128); + dStr_sprintf(ds, + "The file:\n %s (%d Bytes)\nalready exists. What do we do?", + name, (int)ss.st_size); + ch = a_Dialog_choice5("Dillo Save: File exists!", ds->str, + "Abort", "Continue", "Rename", NULL, NULL); + dStr_free(ds, 1); + return ch; + } else { + return 2; /* assume the file does not exist, so Continue */ + } +} + +/* * Save a URL */ static void UIcmd_save(BrowserWindow *bw, const DilloUrl *url, const char *title, const char *url_str) { - char *SuggestedName; const char *name; - SuggestedName = UIcmd_make_save_filename(url_str); - name = a_Dialog_save_file(title, NULL, SuggestedName); - dFree(SuggestedName); - if (name) { - MSG("UIcmd_save: %s\n", name); - a_Nav_save_url(bw, url, name); + bool_t first_prompt = 1; + while (1) { + char *SuggestedName; + + SuggestedName = + first_prompt + ? UIcmd_make_save_filename(url_str) + : dStrdup(name); + first_prompt = 0; + name = a_Dialog_save_file(title, NULL, SuggestedName); + dFree(SuggestedName); + + if (name) { + switch (UIcmd_save_file_check(name)) { + case 0: + case 1: + /* Abort */ + return; + case 2: + /* Continue */ + MSG("UIcmd_save: %s\n", name); + a_Nav_save_url(bw, url, name); + return; + default: + /* Rename */ + break; /* prompt again */ + } + } else { + return; /* no name, so Abort */ + } } } |