From dd26ed1379b756f3b9eb95e88c31fadd48b5994b Mon Sep 17 00:00:00 2001
From: Sebastian Ramacher <s.ramacher@gmx.at>
Date: Fri, 23 Mar 2012 17:35:41 +0100
Subject: [PATCH 1/6] Don't access members directly and make it GTK+3
 compatible.

---
 page-widget.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/page-widget.c b/page-widget.c
index 81e4ff4..c950639 100644
--- a/page-widget.c
+++ b/page-widget.c
@@ -706,7 +706,7 @@ cb_menu_image_copy(GtkMenuItem* item, ZathuraPage* page)
   int width  = cairo_image_surface_get_width(surface);
   int height = cairo_image_surface_get_height(surface);
 
-  GdkPixmap* pixmap = gdk_pixmap_new(GTK_WIDGET(item)->window, width, height, -1);
+  GdkPixmap* pixmap = gdk_pixmap_new(gtk_widget_get_window(GTK_WIDGET(item)), width, height, -1);
   cairo_t* cairo    = gdk_cairo_create(pixmap);
 
   cairo_set_source_surface(cairo, surface, 0, 0);

From 22d750eb4e4659130f24c8c62cac68887fb0c594 Mon Sep 17 00:00:00 2001
From: Sebastian Ramacher <s.ramacher@gmx.at>
Date: Fri, 23 Mar 2012 18:08:24 +0100
Subject: [PATCH 2/6] If g_guess_content_type fails, fall back to file.

---
 document.c | 48 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 7 deletions(-)

diff --git a/document.c b/document.c
index f938adb..e4b2e47 100644
--- a/document.c
+++ b/document.c
@@ -26,6 +26,9 @@
 #include <girara/session.h>
 #include <girara/settings.h>
 
+/** Read a most GT_MAX_READ bytes before falling back to file. */
+static const size_t GT_MAX_READ = 1 << 16;
+
 /**
  * Register document plugin
  */
@@ -185,7 +188,9 @@ guess_type(const char* path)
     return content_type;
   }
 
-  FILE* f = fopen(path, "r");
+  girara_debug("g_content_type is uncertain, guess: %s\n", content_type);
+
+  FILE* f = fopen(path, "rb");
   if (f == NULL) {
     return NULL;
   }
@@ -193,7 +198,7 @@ guess_type(const char* path)
   const int fd = fileno(f);
   guchar* content = NULL;
   size_t length = 0u;
-  while (uncertain == TRUE) {
+  while (uncertain == TRUE && length < GT_MAX_READ) {
     g_free((void*)content_type);
     content_type = NULL;
 
@@ -205,16 +210,45 @@ guess_type(const char* path)
 
     length += r;
     content_type = g_content_type_guess(NULL, content, length, &uncertain);
+    girara_debug("new guess: %s uncertain: %d, read: %zu\n", content_type, uncertain, length);
   }
 
   fclose(f);
-  if (uncertain == TRUE) {
-    g_free((void*)content_type);
-    content_type = NULL;
+  g_free(content);
+  if (uncertain == FALSE) {
+    return content_type;
   }
 
-  g_free(content);
-  return content_type;
+  g_free((void*)content_type);
+  content_type = NULL;
+
+  girara_debug("falling back to file");
+
+  GString* command = g_string_new("file -b --mime-type ");
+  char* tmp        = g_shell_quote(path);
+
+  g_string_append(command, tmp);
+  g_free(tmp);
+
+  GError* error = NULL;
+  char* out = NULL;
+  int ret = 0;
+  g_spawn_command_line_sync(command->str, &out, NULL, &ret, &error);
+  g_string_free(command, TRUE);
+  if (error != NULL) {
+    girara_warning("failed to execute command: %s", error->message);
+    g_error_free(error);
+    g_free(out);
+    return NULL;
+  }
+  if (WEXITSTATUS(ret) != 0) {
+    girara_warning("file failed with error code: %d", WEXITSTATUS(ret));
+    g_free(out);
+    return NULL;
+  }
+
+  g_strdelimit(out, "\n\r", '\0');
+  return out;
 }
 
 zathura_document_t*

From 0c85d41c6c3e2f6ac83032dc32efbf699c90843c Mon Sep 17 00:00:00 2001
From: Sebastian Ramacher <s.ramacher@gmx.at>
Date: Fri, 23 Mar 2012 19:28:20 +0100
Subject: [PATCH 3/6] Add command line option to set the log level.

---
 po/de.po  | 24 ++++++++++++++++--------
 zathura.c | 14 ++++++++++++--
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/po/de.po b/po/de.po
index 2eb5c5e..be4893e 100644
--- a/po/de.po
+++ b/po/de.po
@@ -4,7 +4,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: zathura 0.1.1\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2012-03-20 00:55+0100\n"
+"POT-Creation-Date: 2012-03-23 19:24+0100\n"
 "PO-Revision-Date: 2012-03-05 17:26+0100\n"
 "Last-Translator: Sebastian Ramacher <s.ramacher@gmx.at>\n"
 "Language-Team: pwmt.org <mail@pwmt.org>\n"
@@ -176,7 +176,7 @@ msgstr "Zeige Ordner an"
 
 #: ../config.c:120
 msgid "Always open on first page"
-msgstr ""
+msgstr "Öffne Dokument immer auf der ersten Seite"
 
 #. define default inputbar commands
 #: ../config.c:240
@@ -223,19 +223,23 @@ msgstr "Speichere Dokument (und überschreibe bestehende)"
 msgid "Save attachments"
 msgstr "Speichere Anhänge"
 
-#: ../document.c:373 ../document.c:397 ../document.c:416 ../document.c:438
-#: ../document.c:457 ../document.c:479 ../document.c:498 ../document.c:541
-#: ../document.c:561 ../document.c:584 ../document.c:613 ../document.c:642
-#: ../document.c:666 ../document.c:687 ../document.c:708
+#: ../document.c:439 ../document.c:463 ../document.c:482 ../document.c:504
+#: ../document.c:523 ../document.c:545 ../document.c:564 ../document.c:607
+#: ../document.c:627 ../document.c:650 ../document.c:679 ../document.c:708
+#: ../document.c:732 ../document.c:753 ../document.c:774
 #, c-format
 msgid "%s not implemented"
 msgstr "%s ist nicht implementiert."
 
-#: ../page-widget.c:555
+#: ../page-widget.c:570
 #, c-format
 msgid "Copied selected text to clipbard: %s"
 msgstr "Der gewählte Text wurde in die Zwischenablage kopiert: %s"
 
+#: ../page-widget.c:662
+msgid "Copy image"
+msgstr "Bild kopieren"
+
 #: ../shortcuts.c:726
 msgid "This document does not contain any index"
 msgstr "Dieses Dokument beinhaltet kein Inhaltsverzeichnis."
@@ -260,6 +264,10 @@ msgstr "Pfad zum Pluginverzeichnis"
 msgid "Fork into the background"
 msgstr "Forkt den Prozess in den Hintergrund"
 
-#: ../zathura.c:225 ../zathura.c:600
+#: ../zathura.c:60
+msgid "Log level (debug, info, warning, error)"
+msgstr ""
+
+#: ../zathura.c:226 ../zathura.c:601
 msgid "[No name]"
 msgstr ""
diff --git a/zathura.c b/zathura.c
index ee505d0..45f5436 100644
--- a/zathura.c
+++ b/zathura.c
@@ -48,7 +48,7 @@ zathura_init(int argc, char* argv[])
   Window embed = 0;
 #endif
 
-  gchar* config_dir = NULL, *data_dir = NULL, *plugin_path = NULL;
+  gchar* config_dir = NULL, *data_dir = NULL, *plugin_path = NULL, *loglevel = NULL;
   bool forkback = false;
   GOptionEntry entries[] =
   {
@@ -56,7 +56,8 @@ zathura_init(int argc, char* argv[])
     { "config-dir",  'c', 0, G_OPTION_ARG_FILENAME, &config_dir,  _("Path to the config directory"),               "path" },
     { "data-dir",    'd', 0, G_OPTION_ARG_FILENAME, &data_dir,    _("Path to the data directory"),                 "path" },
     { "plugins-dir", 'p', 0, G_OPTION_ARG_STRING,   &plugin_path, _("Path to the directories containing plugins"), "path" },
-    { "fork",        '\0', 0, G_OPTION_ARG_NONE,    &forkback,    _("Fork into the background")                  , NULL },
+    { "fork",        '\0', 0, G_OPTION_ARG_NONE,    &forkback,    _("Fork into the background"),                   NULL },
+    { "debug",       'l', 0, G_OPTION_ARG_STRING,   &loglevel,    _("Log level (debug, info, warning, error)")     "level" },
     { NULL, '\0', 0, 0, NULL, NULL, NULL }
   };
 
@@ -86,6 +87,15 @@ zathura_init(int argc, char* argv[])
     setsid();
   }
 
+  /* Set log level. */
+  if (loglevel == NULL || g_strcmp0(loglevel, "info") == 0) {
+    girara_set_debug_level(GIRARA_INFO);
+  } else if (g_strcmp0(loglevel, "warning") == 0) {
+    girara_set_debug_level(GIRARA_WARNING);
+  } else if (g_strcmp0(loglevel, "error") == 0) {
+    girara_set_debug_level(GIRARA_ERROR);
+  }
+
   zathura_t* zathura = g_malloc0(sizeof(zathura_t));
 
   /* plugins */

From e0dff15fecb3dc0f3f1352c54bdbcfa81634a845 Mon Sep 17 00:00:00 2001
From: Sebastian Ramacher <s.ramacher@gmx.at>
Date: Fri, 23 Mar 2012 19:35:37 +0100
Subject: [PATCH 4/6] Update AUTHORS.

---
 AUTHORS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/AUTHORS b/AUTHORS
index b6e0d30..a7685f4 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -6,6 +6,7 @@ Sebastian Ramacher <s.ramacher@gmx.at>
 Other contributors are (in alphabetical order):
 
 Pavel Borzenkov <pavel.borzenkov@gmail.com>
+Géraud Le Falher <daureg@gmail.com>
 Ivan Sichmann Freitas <ivansichfreitas@gmail.com>
 Felix Herrmann <felix@herrmann-koenigsberg.de>
 int3 <jezreel@gmail.com>

From 9cacecaa5b7574b80d6e00b9b0840833d6d50e32 Mon Sep 17 00:00:00 2001
From: Sebastian Ramacher <s.ramacher@gmx.at>
Date: Fri, 23 Mar 2012 21:21:07 +0100
Subject: [PATCH 5/6] A missing comma.

---
 zathura.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/zathura.c b/zathura.c
index 45f5436..df5299a 100644
--- a/zathura.c
+++ b/zathura.c
@@ -57,7 +57,7 @@ zathura_init(int argc, char* argv[])
     { "data-dir",    'd', 0, G_OPTION_ARG_FILENAME, &data_dir,    _("Path to the data directory"),                 "path" },
     { "plugins-dir", 'p', 0, G_OPTION_ARG_STRING,   &plugin_path, _("Path to the directories containing plugins"), "path" },
     { "fork",        '\0', 0, G_OPTION_ARG_NONE,    &forkback,    _("Fork into the background"),                   NULL },
-    { "debug",       'l', 0, G_OPTION_ARG_STRING,   &loglevel,    _("Log level (debug, info, warning, error)")     "level" },
+    { "debug",       'l', 0, G_OPTION_ARG_STRING,   &loglevel,    _("Log level (debug, info, warning, error)"),    "level" },
     { NULL, '\0', 0, 0, NULL, NULL, NULL }
   };
 

From e45cbfa8859964def023b72478a18a7143961a7c Mon Sep 17 00:00:00 2001
From: Sebastian Ramacher <s.ramacher@gmx.at>
Date: Fri, 23 Mar 2012 22:33:50 +0100
Subject: [PATCH 6/6] Don't fail install if there are no manpages.

---
 Makefile | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/Makefile b/Makefile
index 1cf8093..9473556 100644
--- a/Makefile
+++ b/Makefile
@@ -145,8 +145,12 @@ endif
 install-manpages: build-manpages
 	$(ECHO) installing manual pages
 	$(QUIET)mkdir -p ${DESTDIR}${MANPREFIX}/man1 ${DESTDIR}${MANPREFIX}/man5
+ifneq "$(wildcard ${PROJECT}.1)" ""
 	$(QUIET)install -m 644 ${PROJECT}.1 ${DESTDIR}${MANPREFIX}/man1
+endif
+ifneq "$(wildcard ${PROJECT}rc.5)" ""
 	$(QUIET)install -m 644 ${PROJECT}rc.5 ${DESTDIR}${MANPREFIX}/man5
+endif
 
 install-headers: ${PROJECT}.pc
 	$(ECHO) installing header files