From 4ea9d46584ee1a45c20e8e8ddcb2599a07ee3c27 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sat, 10 Feb 2018 21:09:22 +0100 Subject: [PATCH] Convert all MIME types to glib "content" types On Linux they are the same, but somewhere else they might be different. --- zathura/content-type.c | 21 ++++++++++++++++++--- zathura/content-type.h | 4 ++-- zathura/plugin.c | 13 +++++++++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/zathura/content-type.c b/zathura/content-type.c index 3440973..60bd455 100644 --- a/zathura/content-type.c +++ b/zathura/content-type.c @@ -99,8 +99,14 @@ guess_type_magic(zathura_content_type_context_t* context, const char* path) } girara_debug("magic detected filetype: %s", mime_type); - /* dup so we own the memory */ - return g_strdup(mime_type);; + char* content_type = g_content_type_from_mime_type(mime_type); + if (content_type == NULL) { + girara_warning("failed to convert mime type to content type: %s", mime_type); + /* dup so we own the memory */ + return g_strdup(mime_type); + } + + return content_type; } static char* @@ -143,7 +149,16 @@ guess_type_file(const char* path) } g_strdelimit(out, "\n\r", '\0'); - return out; + girara_debug("file detected filetype: %s", out); + + char* content_type = g_content_type_from_mime_type(out); + if (content_type == NULL) { + girara_warning("failed to convert mime type to content type: %s", out); + return out; + } + + g_free(out); + return content_type; } #endif diff --git a/zathura/content-type.h b/zathura/content-type.h index 9bd367d..bf65482 100644 --- a/zathura/content-type.h +++ b/zathura/content-type.h @@ -6,14 +6,14 @@ #include "types.h" /** - * Create new context for MIME type detection. + * Create new context for content type detection. * * @return new context */ zathura_content_type_context_t* zathura_content_type_new(void); /** - * Free MIME type detection context. + * Free content type detection context. * * @param context The context. */ diff --git a/zathura/plugin.c b/zathura/plugin.c index db6e8a4..96cbffe 100644 --- a/zathura/plugin.c +++ b/zathura/plugin.c @@ -39,7 +39,7 @@ struct zathura_plugin_manager_s { girara_list_t* type_plugin_mapping; /**< List of type -> plugin mappings */ }; -static void zathura_plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type); +static void plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type); static bool register_plugin(zathura_plugin_manager_t* plugin_manager, zathura_plugin_t* plugin); static bool plugin_mapping_new(zathura_plugin_manager_t* plugin_manager, const gchar* type, zathura_plugin_t* plugin); static void zathura_plugin_free(zathura_plugin_t* plugin); @@ -159,7 +159,7 @@ load_plugin(zathura_plugin_manager_t* plugin_manager, const char* plugindir, con // register mime types for (size_t s = 0; s != plugin_definition->mime_types_size; ++s) { - zathura_plugin_add_mimetype(plugin, plugin_definition->mime_types[s]); + plugin_add_mimetype(plugin, plugin_definition->mime_types[s]); } bool ret = register_plugin(plugin_manager, plugin); @@ -328,13 +328,18 @@ zathura_plugin_free(zathura_plugin_t* plugin) } static void -zathura_plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type) +plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type) { if (plugin == NULL || mime_type == NULL) { return; } - girara_list_append(plugin->content_types, g_content_type_from_mime_type(mime_type)); + char* content_type = g_content_type_from_mime_type(mime_type); + if (content_type == NULL) { + girara_warning("plugin: unable to convert mime type: %s", mime_type); + } else { + girara_list_append(plugin->content_types, content_type); + } } zathura_plugin_functions_t*