Use local filetype directly, if we're "downloading" a local file

svn path=/trunk/netsurf/; revision=4189
This commit is contained in:
John Mark Bell 2008-05-23 13:29:37 +00:00
parent b30fde8b21
commit a16586c9b7
3 changed files with 84 additions and 9 deletions

View File

@ -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 */

View File

@ -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;
}

View File

@ -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;