From 2572c2a5cc894ec84d70f6f4596a04036aa03064 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 21 Oct 2011 15:00:57 +0200 Subject: [PATCH] Guess the content from the actual data if it fails to do so from the file name alone. --- document.c | 25 +++++++++++++++++++++++-- zathura.h | 5 +++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/document.c b/document.c index 075bc16..71fd919 100644 --- a/document.c +++ b/document.c @@ -162,16 +162,37 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password } if (file_exists(path) == false) { - girara_error("File does not exist"); + girara_error("File '%s' does not exist", path); return NULL; } - const gchar* content_type = g_content_type_guess(path, NULL, 0, NULL); + gboolean uncertain; + const gchar* content_type = g_content_type_guess(path, NULL, 0, &uncertain); if (content_type == NULL) { girara_error("Could not determine file type"); return NULL; } + if (uncertain == TRUE) { + g_free((void*)content_type); + content_type = NULL; + + gchar* contents = NULL; + gsize length = 0; + if (g_file_get_contents(path, &contents, &length, NULL) == FALSE) { + girara_error("Could not determine file type"); + return NULL; + } + + content_type = g_content_type_guess(NULL, (guchar*) contents, length, &uncertain); + g_free(contents); + if (content_type == NULL || uncertain == TRUE) { + g_free((void*)content_type); + girara_error("Could not determine file type"); + return NULL; + } + } + /* determine real path */ long path_max; #ifdef PATH_MAX diff --git a/zathura.h b/zathura.h index 732764c..df3d963 100644 --- a/zathura.h +++ b/zathura.h @@ -104,6 +104,11 @@ typedef struct zathura_s girara_list_t* bookmarks; /**> bookmarks */ } bookmarks; + struct + { + gchar* file; + } stdin_support; + zathura_document_t* document; /**> The current document */ zathura_database_t* database; /**> The database */ } zathura_t;