From a16586c9b795aabead419c97ab35871a0f9c3fc0 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Fri, 23 May 2008 13:29:37 +0000 Subject: Use local filetype directly, if we're "downloading" a local file svn path=/trunk/netsurf/; revision=4189 --- riscos/download.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- riscos/filetype.c | 43 +++++++++++++++++++++++++++++++++++++++++++ riscos/gui.h | 1 + 3 files changed, 84 insertions(+), 9 deletions(-) (limited to 'riscos') diff --git a/riscos/download.c b/riscos/download.c index 9d0625f4a..fab09f41e 100644 --- a/riscos/download.c +++ b/riscos/download.c @@ -213,7 +213,7 @@ struct gui_download_window *gui_download_window_create(const char *url, unsigned int total_size) { const char *temp_name; - char *nice; + char *nice, *scheme = NULL; struct gui_download_window *dw; bool space_warning = false; os_error *error; @@ -241,15 +241,46 @@ struct gui_download_window *gui_download_window_create(const char *url, gettimeofday(&dw->start_time, 0); dw->last_time = dw->start_time; dw->last_received = 0; + 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; + /* Get scheme from URL */ + res = url_scheme(url, &scheme); + if (res == URL_FUNC_NOMEM) { + warn_user("NoMemory", 0); + free(dw); + return 0; + } else if (res == URL_FUNC_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 == URL_FUNC_NOMEM) { + warn_user("NoMemory", 0); + free(scheme); + free(dw); + return 0; + } else if (res == URL_FUNC_OK) { + dw->file_type = ro_filetype_from_unix_path(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 */ diff --git a/riscos/filetype.c b/riscos/filetype.c index 08df1ab3f..6a44efefc 100644 --- a/riscos/filetype.c +++ b/riscos/filetype.c @@ -308,3 +308,46 @@ int ro_content_filetype_from_type(content_type type) { } return 0; } + +/** + * Determine the type of a local file. + * + * \param unix_path Unix style path to file on disk + * \return File type + */ +bits ro_filetype_from_unix_path(const char *unix_path) +{ + unsigned int len = strlen(unix_path) + 100; + char *path = calloc(len, 1); + char *r, *slash; + os_error *error; + bits file_type; + + if (!path) { + LOG(("Insufficient memory for calloc")); + warn_user("NoMemory", 0); + return osfile_TYPE_DATA; + } + + /* convert path to RISC OS format and read file type */ + r = __riscosify(unix_path, 0, __RISCOSIFY_NO_SUFFIX, path, len, 0); + if (r == 0) { + LOG(("__riscosify failed")); + free(path); + return osfile_TYPE_DATA; + } + + error = xosfile_read_stamped_no_path(path, 0, 0, 0, 0, 0, + &file_type); + if (error) { + LOG(("xosfile_read_stamped_no_path failed: %s", + error->errmess)); + free(path); + return osfile_TYPE_DATA; + } + + free(path); + + return file_type; +} + diff --git a/riscos/gui.h b/riscos/gui.h index 2c7d024b2..1c6bd41cc 100644 --- a/riscos/gui.h +++ b/riscos/gui.h @@ -187,6 +187,7 @@ bool ro_gui_hotlist_dialog_apply(wimp_w w); /* in filetype.c */ int ro_content_filetype(struct content *content); int ro_content_filetype_from_type(content_type type); +bits ro_filetype_from_unix_path(const char *unix_path); /* in schedule.c */ extern bool sched_active; -- cgit v1.2.3