use nsurl_nice and remove url_nice

change all callers over to using new API for generating nice filenames
from a url and remove the old API.
This commit is contained in:
Vincent Sanders 2014-10-31 00:24:57 +00:00
parent f995616509
commit ce3d991918
10 changed files with 76 additions and 178 deletions

View File

@ -220,7 +220,9 @@ void ami_file_save_req(int type, struct gui_window_2 *gwin,
char *fname = AllocVecTags(1024, NULL);
char *initial_fname = NULL;
if(object) url_nice(nsurl_access(hlcache_handle_get_url(object)), &initial_fname, true);
if(object) {
nsurl_nice(hlcache_handle_get_url(object), &initial_fname, true);
}
if(AslRequestTags(savereq,
ASLFR_Window, gwin->win,

View File

@ -76,8 +76,9 @@ static char *download_default_filename(nsurl *url)
{
char *nice;
if (url_nice(nsurl_access(url), &nice, false) == NSERROR_OK)
if (nsurl_nice(url, &nice, false) == NSERROR_OK) {
return nice;
}
return NULL;
}

View File

@ -637,8 +637,8 @@ MULTIHANDLER(savepage)
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fc), filter);
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(fc), filter);
res = url_nice(nsurl_access(browser_window_get_url(
nsgtk_get_browser_window(g->top_level))), &path, false);
res = nsurl_nice(browser_window_get_url(
nsgtk_get_browser_window(g->top_level)), &path, false);
if (res != NSERROR_OK) {
path = strdup(messages_get("SaveText"));
if (path == NULL) {
@ -697,8 +697,7 @@ MULTIHANDLER(pdf)
LOG(("Print preview (generating PDF) started."));
res = url_nice(nsurl_access(browser_window_get_url(bw)),
&url_name, true);
res = nsurl_nice(browser_window_get_url(bw), &url_name, true);
if (res != NSERROR_OK) {
warn_user(messages_get_errorcode(res), 0);
return TRUE;
@ -771,8 +770,8 @@ MULTIHANDLER(plaintext)
char *filename;
nserror res;
res = url_nice(nsurl_access(browser_window_get_url(
nsgtk_get_browser_window(g->top_level))),
res = nsurl_nice(browser_window_get_url(
nsgtk_get_browser_window(g->top_level)),
&filename, false);
if (res != NSERROR_OK) {
filename = strdup(messages_get("SaveText"));

View File

@ -51,7 +51,7 @@ void nsgtk_viewsource(GtkWindow *parent, struct browser_window *bw)
source_data = content_get_source_data(hlcontent, &source_size);
ret = url_nice(nsurl_access(browser_window_get_url(bw)), &filename, false);
ret = nsurl_nice(browser_window_get_url(bw), &filename, false);
if (ret != NSERROR_OK) {
filename = strdup(messages_get("SaveSource"));
if (filename == NULL) {

View File

@ -116,7 +116,7 @@ static void ro_gui_save_bounced(wimp_message *message);
static bool ro_gui_save_object_native(hlcache_handle *h, char *path);
static bool ro_gui_save_link(const char *url, const char *title, link_format format, char *path);
static void ro_gui_save_set_state(hlcache_handle *h, gui_save_type save_type,
const char *url, char *leaf_buf, size_t leaf_len,
const nsurl *url, char *leaf_buf, size_t leaf_len,
char *icon_buf, size_t icon_len);
static void ro_gui_save_drag_end(wimp_dragged *drag, void *data);
static bool ro_gui_save_create_thumbnail(hlcache_handle *h, const char *name);
@ -254,7 +254,7 @@ void ro_gui_saveas_quit(void)
*/
void ro_gui_save_prepare(gui_save_type save_type, hlcache_handle *h,
char *s, const char *url, const char *title)
char *s, const nsurl *url, const char *title)
{
char name_buf[FILENAME_MAX];
size_t leaf_offset = 0;
@ -271,7 +271,11 @@ void ro_gui_save_prepare(gui_save_type save_type, hlcache_handle *h,
free(gui_save_selection);
gui_save_selection = s;
gui_save_url = url;
if (url != NULL) {
gui_save_url = nsurl_access(url);
} else {
gui_save_url = NULL;
}
gui_save_title = title;
if (save_dir) {
@ -280,8 +284,11 @@ void ro_gui_save_prepare(gui_save_type save_type, hlcache_handle *h,
name_buf[leaf_offset++] = '.';
}
ro_gui_save_set_state(h, save_type,
h ? nsurl_access(hlcache_handle_get_url(h)) : url,
if (h != NULL) {
url = hlcache_handle_get_url(h);
}
ro_gui_save_set_state(h, save_type, url,
name_buf + leaf_offset, FILENAME_MAX - leaf_offset,
icon_buf, sizeof(icon_buf));
@ -386,7 +393,7 @@ void gui_drag_save_object(struct gui_window *g, hlcache_handle *c,
return;
}
ro_gui_save_set_state(c, save_type, nsurl_access(hlcache_handle_get_url(c)),
ro_gui_save_set_state(c, save_type, hlcache_handle_get_url(c),
save_leafname, LEAFNAME_MAX,
icon_buf, sizeof(icon_buf));
@ -454,7 +461,7 @@ void gui_drag_save_selection(struct gui_window *g, const char *selection)
* \
*/
void ro_gui_drag_save_link(gui_save_type save_type, const char *url,
void ro_gui_drag_save_link(gui_save_type save_type, const nsurl *url,
const char *title, struct gui_window *g)
{
wimp_pointer pointer;
@ -466,7 +473,7 @@ void ro_gui_drag_save_link(gui_save_type save_type, const char *url,
xwimp_create_menu(wimp_CLOSE_MENU, 0, 0);
ro_gui_dialog_close(dialog_saveas);
gui_save_url = url;
gui_save_url = nsurl_access(url);
gui_save_title = title;
gui_save_sourcew = g->window;
saving_from_dialog = false;
@ -479,8 +486,8 @@ void ro_gui_drag_save_link(gui_save_type save_type, const char *url,
return;
}
ro_gui_save_set_state(NULL, save_type, url, save_leafname, LEAFNAME_MAX,
icon_buf, sizeof(icon_buf));
ro_gui_save_set_state(NULL, save_type, url, save_leafname,
LEAFNAME_MAX, icon_buf, sizeof(icon_buf));
ro_mouse_drag_start(ro_gui_save_drag_end, NULL, NULL, NULL);
@ -1215,7 +1222,7 @@ bool ro_gui_save_link(const char *url, const char *title, link_format format,
*/
void ro_gui_save_set_state(hlcache_handle *h, gui_save_type save_type,
const char *url, char *leaf_buf, size_t leaf_len,
const nsurl *url, char *leaf_buf, size_t leaf_len,
char *icon_buf, size_t icon_len)
{
/* filename */
@ -1251,8 +1258,9 @@ void ro_gui_save_set_state(hlcache_handle *h, gui_save_type save_type,
}
/* leafname */
if (url && url_nice(url, &nice, nsoption_bool(strip_extensions)) ==
NSERROR_OK) {
if ((url != NULL) &&
(nsurl_nice(url, &nice, nsoption_bool(strip_extensions)) ==
NSERROR_OK)) {
size_t i;
for (i = 0; nice[i]; i++) {
if (nice[i] == '.')

View File

@ -34,10 +34,10 @@ void gui_drag_save_selection(struct gui_window *g, const char *selection);
wimp_w ro_gui_saveas_create(const char *template_name);
void ro_gui_saveas_quit(void);
void ro_gui_save_prepare(enum gui_save_type save_type, struct hlcache_handle *h,
char *s, const char *url,
char *s, const nsurl *url,
const char *title);
void ro_gui_save_start_drag(wimp_pointer *pointer);
void ro_gui_drag_save_link(enum gui_save_type save_type, const char *url,
void ro_gui_drag_save_link(enum gui_save_type save_type, const nsurl *url,
const char *title, struct gui_window *g);
void ro_gui_drag_icon(int x, int y, const char *sprite);
void ro_gui_drag_box_cancel(void);

View File

@ -194,7 +194,7 @@ static hlcache_handle *current_menu_main = 0;
/** Object under menu, or 0 if no object. */
static hlcache_handle *current_menu_object = 0;
/** URL of link under menu, or 0 if no link. */
static const char *current_menu_url = 0;
static nsurl *current_menu_url = 0;
static float scale_snap_to[] = {0.10, 0.125, 0.25, 0.333, 0.5, 0.75,
1.0,
@ -1284,7 +1284,10 @@ static bool gui_window_drag_start(struct gui_window *g, gui_drag_type type,
static void gui_window_save_link(struct gui_window *g, const char *url,
const char *title)
{
ro_gui_save_prepare(GUI_SAVE_LINK_URL, NULL, NULL, url, title);
nsurl *nurl;
nsurl_create(url, &nurl);
ro_gui_save_prepare(GUI_SAVE_LINK_URL, NULL, NULL, nurl, title);
nsurl_unref(nurl);
ro_gui_dialog_open_persistent(g->window, dialog_saveas, true);
}
@ -2174,7 +2177,10 @@ bool ro_gui_window_menu_prepare(wimp_w w, wimp_i i, wimp_menu *menu,
current_menu_main = NULL;
current_menu_object = NULL;
current_menu_url = NULL;
if (current_menu_url != NULL) {
nsurl_unref(current_menu_url);
current_menu_url = NULL;
}
if (ro_gui_window_to_window_pos(g, pointer->pos.x,
pointer->pos.y, &pos)) {
@ -2183,7 +2189,7 @@ bool ro_gui_window_menu_prepare(wimp_w w, wimp_i i, wimp_menu *menu,
current_menu_main = cont.main;
current_menu_object = cont.object;
current_menu_url = cont.link_url;
nsurl_create(cont.link_url, &current_menu_url);
}
}
@ -2444,7 +2450,7 @@ void ro_gui_window_menu_warning(wimp_w w, wimp_i i, wimp_menu *menu,
case BROWSER_OBJECT_INFO:
if (current_menu_object != NULL)
ro_gui_window_prepare_objectinfo(current_menu_object,
current_menu_url);
nsurl_access(current_menu_url));
break;
case BROWSER_OBJECT_SAVE:
@ -2463,45 +2469,45 @@ void ro_gui_window_menu_warning(wimp_w w, wimp_i i, wimp_menu *menu,
case BROWSER_SAVE_URL_URI:
if (h != NULL)
ro_gui_save_prepare(GUI_SAVE_LINK_URI, NULL, NULL,
nsurl_access(hlcache_handle_get_url(h)),
hlcache_handle_get_url(h),
content_get_title(h));
break;
case BROWSER_SAVE_URL_URL:
if (h != NULL)
ro_gui_save_prepare(GUI_SAVE_LINK_URL, NULL, NULL,
nsurl_access(hlcache_handle_get_url(h)),
hlcache_handle_get_url(h),
content_get_title(h));
break;
case BROWSER_SAVE_URL_TEXT:
if (h != NULL)
ro_gui_save_prepare(GUI_SAVE_LINK_TEXT, NULL, NULL,
nsurl_access(hlcache_handle_get_url(h)),
hlcache_handle_get_url(h),
content_get_title(h));
break;
case BROWSER_OBJECT_SAVE_URL_URI:
if (current_menu_object != NULL)
ro_gui_save_prepare(GUI_SAVE_LINK_URI, NULL, NULL,
nsurl_access(hlcache_handle_get_url(
current_menu_object)),
hlcache_handle_get_url(
current_menu_object),
content_get_title(current_menu_object));
break;
case BROWSER_OBJECT_SAVE_URL_URL:
if (current_menu_object != NULL)
ro_gui_save_prepare(GUI_SAVE_LINK_URL, NULL, NULL,
nsurl_access(hlcache_handle_get_url(
current_menu_object)),
hlcache_handle_get_url(
current_menu_object),
content_get_title(current_menu_object));
break;
case BROWSER_OBJECT_SAVE_URL_TEXT:
if (current_menu_object != NULL)
ro_gui_save_prepare(GUI_SAVE_LINK_TEXT, NULL, NULL,
nsurl_access(hlcache_handle_get_url(
current_menu_object)),
hlcache_handle_get_url(
current_menu_object),
content_get_title(current_menu_object));
break;
@ -2759,7 +2765,7 @@ bool ro_gui_window_menu_select(wimp_w w, wimp_i i, wimp_menu *menu,
case BROWSER_OBJECT_INFO:
if (current_menu_object != NULL) {
ro_gui_window_prepare_objectinfo(current_menu_object,
current_menu_url);
nsurl_access(current_menu_url));
ro_gui_dialog_open_persistent(g->window,
dialog_objinfo, false);
}
@ -2799,33 +2805,25 @@ bool ro_gui_window_menu_select(wimp_w w, wimp_i i, wimp_menu *menu,
case BROWSER_LINK_DOWNLOAD:
if (current_menu_url != NULL) {
error = nsurl_create(current_menu_url, &url);
if (error == NSERROR_OK) {
error = browser_window_navigate(bw,
url,
browser_window_get_url(bw),
BW_NAVIGATE_DOWNLOAD,
NULL,
NULL,
NULL);
nsurl_unref(url);
}
error = browser_window_navigate(bw,
current_menu_url,
browser_window_get_url(bw),
BW_NAVIGATE_DOWNLOAD,
NULL,
NULL,
NULL);
}
break;
case BROWSER_LINK_NEW_WINDOW:
if (current_menu_url != NULL) {
error = nsurl_create(current_menu_url, &url);
if (error == NSERROR_OK) {
error = browser_window_create(
BW_CREATE_HISTORY |
BW_CREATE_CLONE,
url,
browser_window_get_url(bw),
bw,
NULL);
nsurl_unref(url);
}
error = browser_window_create(
BW_CREATE_HISTORY |
BW_CREATE_CLONE,
url,
browser_window_get_url(bw),
bw,
NULL);
}
break;
@ -3050,7 +3048,10 @@ void ro_gui_window_menu_close(wimp_w w, wimp_i i, wimp_menu *menu)
{
if (menu == ro_gui_browser_window_menu) {
current_menu_object = NULL;
current_menu_url = NULL;
if (current_menu_url != NULL) {
nsurl_unref(current_menu_url);
current_menu_url = NULL;
}
} else if (menu == gui_form_select_menu) {
gui_form_select_control = NULL;
}
@ -3579,8 +3580,7 @@ void ro_gui_window_toolbar_click(void *data,
save_type = GUI_SAVE_LINK_TEXT;
ro_gui_drag_save_link(save_type,
nsurl_access(
browser_window_get_url(g->bw)),
browser_window_get_url(g->bw),
browser_window_get_title(g->bw), g);
}
break;

View File

@ -146,109 +146,6 @@ out_true:
}
/* exported interface documented in utils/url.h */
nserror url_nice(const char *url, char **result,
bool remove_extensions)
{
int m;
regmatch_t match[10];
regoff_t start, end;
size_t i;
char *dot;
*result = 0;
m = regexec(&url_re, url, 10, match, 0);
if (m) {
LOG(("url '%s' failed to match regex", url));
return NSERROR_NOT_FOUND;
}
/* extract the last component of the path, if possible */
if (match[URL_RE_PATH].rm_so == -1 || match[URL_RE_PATH].rm_so ==
match[URL_RE_PATH].rm_eo)
goto no_path; /* no path, or empty */
for (end = match[URL_RE_PATH].rm_eo - 1;
end != match[URL_RE_PATH].rm_so && url[end] == '/';
end--)
;
if (end == match[URL_RE_PATH].rm_so)
goto no_path; /* path is a string of '/' */
end++;
for (start = end - 1;
start != match[URL_RE_PATH].rm_so && url[start] != '/';
start--)
;
if (url[start] == '/')
start++;
if (!strncasecmp(url + start, "index.", 6) ||
!strncasecmp(url + start, "default.", 8)) {
/* try again */
if (start == match[URL_RE_PATH].rm_so)
goto no_path;
for (end = start - 1;
end != match[URL_RE_PATH].rm_so &&
url[end] == '/';
end--)
;
if (end == match[URL_RE_PATH].rm_so)
goto no_path;
end++;
for (start = end - 1;
start != match[URL_RE_PATH].rm_so &&
url[start] != '/';
start--)
;
if (url[start] == '/')
start++;
}
*result = malloc(end - start + 1);
if (!*result) {
LOG(("malloc failed"));
return NSERROR_NOMEM;
}
strncpy(*result, url + start, end - start);
(*result)[end - start] = 0;
if (remove_extensions) {
dot = strchr(*result, '.');
if (dot && dot != *result)
*dot = 0;
}
return NSERROR_OK;
no_path:
/* otherwise, use the host name, with '.' replaced by '_' */
if (match[URL_RE_AUTHORITY].rm_so != -1 &&
match[URL_RE_AUTHORITY].rm_so !=
match[URL_RE_AUTHORITY].rm_eo) {
*result = malloc(match[URL_RE_AUTHORITY].rm_eo -
match[URL_RE_AUTHORITY].rm_so + 1);
if (!*result) {
LOG(("malloc failed"));
return NSERROR_NOMEM;
}
strncpy(*result, url + match[URL_RE_AUTHORITY].rm_so,
match[URL_RE_AUTHORITY].rm_eo -
match[URL_RE_AUTHORITY].rm_so);
(*result)[match[URL_RE_AUTHORITY].rm_eo -
match[URL_RE_AUTHORITY].rm_so] = 0;
for (i = 0; (*result)[i]; i++)
if ((*result)[i] == '.')
(*result)[i] = '_';
return NSERROR_OK;
}
return NSERROR_NOT_FOUND;
}
/* exported interface documented in utils/url.h */
nserror url_unescape(const char *str, char **result)
{

View File

@ -55,16 +55,6 @@ nserror url_init(void);
bool url_host_is_ip_address(const char *host);
/**
* Attempt to find a nice filename for a URL.
*
* \param url an absolute URL
* \param result pointer to pointer to buffer to hold filename
* \param remove_extensions remove any extensions from the filename
* \return NSERROR_OK on success
*/
nserror url_nice(const char *url, char **result, bool remove_extensions);
/**
* Escape a string suitable for inclusion in an URL.
*

View File

@ -71,8 +71,9 @@ gui_download_window_create(download_context *ctx, struct gui_window *gui)
messages_get("UnknownSize") :
human_friendly_bytesize(total_size);
if (url_nice(nsurl_access(url), &filename, false) != NSERROR_OK)
if (nsurl_nice(url, &filename, false) != NSERROR_OK) {
filename = strdup(messages_get("UnknownFile"));
}
if (filename == NULL) {
warn_user(messages_get("NoMemory"), 0);
free(w);