diff options
Diffstat (limited to 'src/uicmd.cc')
-rw-r--r-- | src/uicmd.cc | 135 |
1 files changed, 101 insertions, 34 deletions
diff --git a/src/uicmd.cc b/src/uicmd.cc index 54efa7a4..be2359da 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> @@ -502,9 +503,9 @@ static void win_cb (Fl_Widget *w, void *cb_data) { } if (prefs.show_quit_dialog && ntabs > 1) - choice = a_Dialog_choice5("Dillo: Close window?", - "Window contains more than one tab.", - "Close", "Cancel", NULL, NULL, NULL); + choice = a_Dialog_choice("Dillo: Close window?", + "Window contains more than one tab.", + "Close", "Cancel", NULL); if (choice == 1) while (ntabs-- > 0) a_UIcmd_close_bw(a_UIcmd_get_bw_by_widget(tabs->wizard()->value())); @@ -643,9 +644,9 @@ void a_UIcmd_close_all_bw(void *) int choice = 1; if (prefs.show_quit_dialog && a_Bw_num() > 1) - choice = a_Dialog_choice5("Dillo: Quit?", - "More than one open tab or window.", - "Quit", "Cancel", NULL, NULL, NULL); + choice = a_Dialog_choice("Dillo: Quit?", + "More than one open tab or window.", + "Quit", "Cancel", NULL); if (choice == 1) while ((bw = a_Bw_get(0))) a_UIcmd_close_bw((void*)bw); @@ -813,30 +814,51 @@ void a_UIcmd_redirection0(void *vbw, const DilloUrl *url) /* * Return a suitable filename for a given URL path. */ -static char *UIcmd_make_save_filename(const char *pathstr) +static char *UIcmd_make_save_filename(const DilloUrl *url) { size_t MaxLen = 64; - char *FileName, *newname, *o, *n; - const char *name, *dir = save_dir; + const char *dir = save_dir, *path, *path2, *query; + char *name, *free1, *free2, *n1, *n2; - if ((name = strrchr(pathstr, '/'))) { - if (strlen(++name) > MaxLen) { - name = name + strlen(name) - MaxLen; - } - /* Replace %20 and ' ' with '_' in Filename */ - o = n = newname = dStrdup(name); - for (int i = 0; o[i]; i++) { - *n++ = (o[i] == ' ') ? '_' : - (o[i] == '%' && o[i+1] == '2' && o[i+2] == '0') ? - i+=2, '_' : o[i]; + free1 = free2 = NULL; + + /* get the last component of the path */ + path = URL_PATH(url); + path2 = strrchr(path, '/'); + path = path2 ? path2 + 1 : path; + + /* truncate the path if necessary */ + if (strlen(path) > MaxLen) { + path = free1 = dStrndup(path, MaxLen); + } + + /* is there a query? */ + query = URL_QUERY(url); + if (*query) { + /* truncate the query if necessary */ + if (strlen(query) > MaxLen) { + query = free2 = dStrndup(query, MaxLen); } - *n = 0; - FileName = dStrconcat(dir, newname, NULL); - dFree(newname); + name = dStrconcat(dir, path, "?", query, NULL); } else { - FileName = dStrconcat(dir, pathstr, NULL); + name = dStrconcat(dir, path, NULL); } - return FileName; + + dFree(free1); + dFree(free2); + + /* Replace %20 and ' ' with '_' */ + for (n1 = n2 = name; *n1; n1++, n2++) { + *n2 = + (n1[0] == ' ') + ? '_' : + (n1[0] == '%' && n1[1] == '2' && n1[2] == '0') + ? (n1 += 2, '_') : + n1[0]; + } + *n2 = 0; + + return name; } /* @@ -856,19 +878,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_choice("Dillo Save: File exists!", ds->str, + "Abort", "Continue", "Rename", 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) + const char *title) { - 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) + : 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 */ + } } } @@ -881,7 +948,7 @@ void a_UIcmd_save(void *vbw) const DilloUrl *url = a_History_get_url(NAV_TOP_UIDX(bw)); if (url) { - UIcmd_save(bw, url, "Save Page as File", URL_PATH(url)); + UIcmd_save(bw, url, "Save Page as File"); } } @@ -1002,7 +1069,7 @@ const char *a_UIcmd_get_passwd(const char *user) */ void a_UIcmd_save_link(BrowserWindow *bw, const DilloUrl *url) { - UIcmd_save(bw, url, "Dillo: Save Link as File", URL_STR(url)); + UIcmd_save(bw, url, "Dillo: Save Link as File"); } /* |