mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2025-02-27 21:04:38 +01:00
Re-try with different methods if content type is not supported
This commit is contained in:
parent
c998ad32b1
commit
43144b23e8
4 changed files with 25 additions and 8 deletions
|
@ -213,20 +213,37 @@ guess_type_glib(const char* path)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int compare_content_types(const void* lhs, const void* rhs) {
|
||||||
|
return g_strcmp0(lhs, rhs);
|
||||||
|
}
|
||||||
|
|
||||||
char*
|
char*
|
||||||
zathura_content_type_guess(zathura_content_type_context_t* context,
|
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 */
|
/* try libmagic first */
|
||||||
char* content_type = guess_type_magic(context, path);
|
char *content_type = guess_type_magic(context, path);
|
||||||
if (content_type != NULL) {
|
if (content_type != NULL) {
|
||||||
|
if (supported_content_types == NULL ||
|
||||||
|
girara_list_find(supported_content_types, compare_content_types,
|
||||||
|
content_type) != NULL) {
|
||||||
return content_type;
|
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 */
|
/* else fallback to g_content_type_guess method */
|
||||||
content_type = guess_type_glib(path);
|
content_type = guess_type_glib(path);
|
||||||
if (content_type != NULL) {
|
if (content_type != NULL) {
|
||||||
|
if (supported_content_types == NULL ||
|
||||||
|
girara_list_find(supported_content_types, compare_content_types,
|
||||||
|
content_type) != NULL) {
|
||||||
return content_type;
|
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 */
|
/* and if libmagic is not available, try file as last resort */
|
||||||
return guess_type_file(path);
|
return guess_type_file(path);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
* @return content type of path, needs to freeed with g_free.
|
||||||
*/
|
*/
|
||||||
char* zathura_content_type_guess(zathura_content_type_context_t* context,
|
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
|
#endif
|
||||||
|
|
|
@ -90,7 +90,7 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* uri,
|
||||||
goto error_free;
|
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) {
|
if (content_type == NULL) {
|
||||||
girara_error("Could not determine file type.");
|
girara_error("Could not determine file type.");
|
||||||
check_set_error(error, ZATHURA_ERROR_UNKNOWN);
|
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);
|
plugin = zathura_plugin_manager_get_plugin(zathura->plugins.manager, content_type);
|
||||||
|
|
||||||
if (plugin == NULL) {
|
if (plugin == NULL) {
|
||||||
girara_error("Unknown file type: '%s'", content_type);
|
girara_error("Unknown file type: '%s'", content_type);
|
||||||
check_set_error(error, ZATHURA_ERROR_UNKNOWN);
|
check_set_error(error, ZATHURA_ERROR_UNKNOWN);
|
||||||
|
|
|
@ -52,7 +52,7 @@ file_valid_extension(zathura_t* zathura, const char* path)
|
||||||
return false;
|
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) {
|
if (content_type == NULL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue