diff --git a/zathura/content-type.c b/zathura/content-type.c index 8569502..fb6745d 100644 --- a/zathura/content-type.c +++ b/zathura/content-type.c @@ -213,19 +213,36 @@ guess_type_glib(const char* path) return NULL; } +static int compare_content_types(const void* lhs, const void* rhs) { + return g_strcmp0(lhs, rhs); +} + char* zathura_content_type_guess(zathura_content_type_context_t* context, - const char* path) + const char* path, + const girara_list_t* supported_content_types) { /* try libmagic first */ - char* content_type = guess_type_magic(context, path); + char *content_type = guess_type_magic(context, path); if (content_type != NULL) { - return content_type; + if (supported_content_types == NULL || + girara_list_find(supported_content_types, compare_content_types, + content_type) != NULL) { + return content_type; + } + girara_debug("content type '%s' not supported, trying again", content_type); + g_free(content_type); } /* else fallback to g_content_type_guess method */ content_type = guess_type_glib(path); if (content_type != NULL) { - return content_type; + if (supported_content_types == NULL || + girara_list_find(supported_content_types, compare_content_types, + content_type) != NULL) { + return content_type; + } + girara_debug("content type '%s' not supported, trying again", content_type); + g_free(content_type); } /* and if libmagic is not available, try file as last resort */ return guess_type_file(path); diff --git a/zathura/content-type.h b/zathura/content-type.h index cf46d7b..47f0ad1 100644 --- a/zathura/content-type.h +++ b/zathura/content-type.h @@ -27,6 +27,7 @@ void zathura_content_type_free(zathura_content_type_context_t* context); * @return content type of path, needs to freeed with g_free. */ char* zathura_content_type_guess(zathura_content_type_context_t* context, - const char* path); + const char* path, + const girara_list_t* supported_content_types); #endif diff --git a/zathura/document.c b/zathura/document.c index 08d9b97..1d7e30c 100644 --- a/zathura/document.c +++ b/zathura/document.c @@ -90,7 +90,7 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* uri, goto error_free; } - content_type = zathura_content_type_guess(zathura->content_type_context, real_path); + content_type = zathura_content_type_guess(zathura->content_type_context, real_path, zathura_plugin_manager_get_content_types(zathura->plugins.manager)); if (content_type == NULL) { girara_error("Could not determine file type."); check_set_error(error, ZATHURA_ERROR_UNKNOWN); @@ -98,7 +98,6 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* uri, } plugin = zathura_plugin_manager_get_plugin(zathura->plugins.manager, content_type); - if (plugin == NULL) { girara_error("Unknown file type: '%s'", content_type); check_set_error(error, ZATHURA_ERROR_UNKNOWN); diff --git a/zathura/utils.c b/zathura/utils.c index b5d7363..4a66728 100644 --- a/zathura/utils.c +++ b/zathura/utils.c @@ -52,7 +52,7 @@ file_valid_extension(zathura_t* zathura, const char* path) return false; } - char* content_type = zathura_content_type_guess(zathura->content_type_context, path); + char* content_type = zathura_content_type_guess(zathura->content_type_context, path, NULL); if (content_type == NULL) { return false; }