From cd7f1eceea81a69e5b4dcdef90d45aab44f8a11f Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Thu, 8 May 2014 17:54:44 +0100 Subject: make download_context_get_url() return an nsurl and adapt callers to cope --- riscos/download.c | 121 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 67 insertions(+), 54 deletions(-) (limited to 'riscos/download.c') diff --git a/riscos/download.c b/riscos/download.c index 05a2e9ec9..0d15229a9 100644 --- a/riscos/download.c +++ b/riscos/download.c @@ -48,7 +48,7 @@ #include "utils/nsoption.h" #include "utils/log.h" #include "utils/messages.h" -#include "utils/url.h" +#include "utils/nsurl.h" #include "utils/utf8.h" #include "utils/utils.h" @@ -210,6 +210,59 @@ const char *ro_gui_download_temp_name(struct gui_download_window *dw) return temp_name; } +/** + * Try and find the correct RISC OS filetype from a download context. + */ +static nserror download_ro_filetype(download_context *ctx, bits *ftype_out) +{ + nsurl *url = download_context_get_url(ctx); + bits ftype = 0; + lwc_string *scheme; + + /* If the file is local try and read its filetype */ + scheme = nsurl_get_component(url, NSURL_SCHEME); + if (scheme != NULL) { + bool filescheme; + if (lwc_string_isequal(scheme, + corestring_lwc_file, + &filescheme) != lwc_error_ok) { + filescheme = false; + } + + if (filescheme) { + lwc_string *path = nsurl_get_component(url, NSURL_PATH); + if (path != NULL) { + char *raw_path; + raw_path = curl_unescape(path, strlen(path)); + if (raw_path != NULL) { + ftype = ro_filetype_from_unix_path(raw_path); + curl_free(raw_path); + } + } + } + } + + /* If we still don't have a filetype (i.e. failed reading local + * one or fetching a remote object), then use the MIME type. + */ + if (ftype == 0) { + /* convert MIME type to RISC OS file type */ + os_error *error; + const char *mime_type; + + mime_type = download_context_get_mime_type(ctx); + error = xmimemaptranslate_mime_type_to_filetype(mime_type, &ftype); + if (error) { + LOG(("xmimemaptranslate_mime_type_to_filetype: 0x%x: %s", + error->errnum, error->errmess)); + warn_user("MiscError", error->errmess); + ftype = 0xffd; + } + } + + *ftype_out = ftype; + return NSERROR_OK; +} /** * Create and open a download progress window. @@ -219,13 +272,11 @@ const char *ro_gui_download_temp_name(struct gui_download_window *dw) * reported */ -static struct gui_download_window *gui_download_window_create(download_context *ctx, - struct gui_window *gui) +static struct gui_download_window * +gui_download_window_create(download_context *ctx, struct gui_window *gui) { - const char *url = download_context_get_url(ctx); - const char *mime_type = download_context_get_mime_type(ctx); + nsurl *url = download_context_get_url(ctx); const char *temp_name; - char *scheme = NULL; char *filename = NULL; struct gui_download_window *dw; bool space_warning = false; @@ -248,8 +299,13 @@ static struct gui_download_window *gui_download_window_create(download_context * dw->query = QUERY_INVALID; dw->received = 0; dw->total_size = download_context_get_total_length(ctx); - strncpy(dw->url, url, sizeof dw->url); + + /** @todo change this to take a reference to the nsurl and use + * that value directly rather than using a fixed buffer. + */ + strncpy(dw->url, nsurl_access(url), sizeof dw->url); dw->url[sizeof dw->url - 1] = 0; + dw->status[0] = 0; gettimeofday(&dw->start_time, 0); dw->last_time = dw->start_time; @@ -258,55 +314,12 @@ static struct gui_download_window *gui_download_window_create(download_context * dw->average_rate = 0; dw->average_points = 0; - /* Get scheme from URL */ - res = url_scheme(url, &scheme); - if (res == NSERROR_NOMEM) { - warn_user("NoMemory", 0); + /* get filetype */ + err = download_ro_filetype(ctx, &dw->file_type); + if (err != NSERROR_OK) { + warn_user(messages_get_errorcode(err), 0); free(dw); return 0; - } else if (res == NSERROR_OK) { - /* If we have a scheme and it's "file", then - * attempt to use the local filetype directly */ - if (strcasecmp(scheme, "file") == 0) { - char *path = NULL; - res = url_path(url, &path); - if (res == NSERROR_NOMEM) { - warn_user("NoMemory", 0); - free(scheme); - free(dw); - return 0; - } else if (res == NSERROR_OK) { - char *raw_path = curl_unescape(path, - strlen(path)); - if (raw_path == NULL) { - warn_user("NoMemory", 0); - free(path); - free(scheme); - free(dw); - return 0; - } - dw->file_type = - ro_filetype_from_unix_path(raw_path); - curl_free(raw_path); - free(path); - } - } - - free(scheme); - } - - /* If we still don't have a filetype (i.e. failed reading local - * one or fetching a remote object), then use the MIME type */ - if (dw->file_type == 0) { - /* convert MIME type to RISC OS file type */ - error = xmimemaptranslate_mime_type_to_filetype(mime_type, - &(dw->file_type)); - if (error) { - LOG(("xmimemaptranslate_mime_type_to_filetype: 0x%x: %s", - error->errnum, error->errmess)); - warn_user("MiscError", error->errmess); - dw->file_type = 0xffd; - } } /* open temporary output file */ -- cgit v1.2.3