From cb2a17de987c453374da8728322c86aff222ad29 Mon Sep 17 00:00:00 2001
From: neldoreth <mail@pwmt.org>
Date: Sun, 11 Apr 2010 14:18:49 +0200
Subject: [PATCH] Follow internal links or upen URI in browser

This commit finishes the follow command by adding the functionality
of follow links / or open them with a web browser.
---
 config.def.h |  3 +++
 zathura.c    | 36 ++++++++++++++++++++++++++++++------
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/config.def.h b/config.def.h
index d4993eb..b9697c9 100644
--- a/config.def.h
+++ b/config.def.h
@@ -48,6 +48,9 @@ static const char DEFAULT_TEXT[] = "[No Name]";
 #define LIST_PRINTER_COMMAND "lpstat -v | sed -n '/^.*device for \\(.*\\): .*$/s//\\1/p'"
 #define PRINT_COMMAND "lp -d '%s' -p %s '%s'" /* printer / pages / file */
 
+/* open uri */
+#define URI_COMMAND "firefox %s" /* uri */
+
 /* additional settings */
 #define SHOW_SCROLLBARS 0
 #define ADJUST_OPEN ADJUST_BESTFIT
diff --git a/zathura.c b/zathura.c
index a63fa80..231b6c5 100644
--- a/zathura.c
+++ b/zathura.c
@@ -262,6 +262,7 @@ void draw(int);
 void eval_marker(int);
 void notify(int, char*);
 gboolean open_file(char*, char*);
+void open_uri(char*);
 void update_status();
 void recalcRectangle(int, PopplerRectangle*);
 void setCompletionRowColor(GtkBox*, int, int);
@@ -897,6 +898,13 @@ open_file(char* path, char* password)
   return TRUE;
 }
 
+void open_uri(char* uri)
+{
+  char* uri_cmd = g_strdup_printf(URI_COMMAND, uri);
+  system(uri_cmd);
+  g_free(uri_cmd);
+}
+
 void
 update_status()
 {
@@ -2962,7 +2970,7 @@ cb_inputbar_form_activate(GtkEntry* entry, gpointer data)
     return TRUE;
 
   Page* current_page = Zathura.PDF.pages[Zathura.PDF.page_number];
-  int number_of_links = 0, link_id = 1;
+  int number_of_links = 0, link_id = 1, new_page_id = Zathura.PDF.page_number;
 
   g_static_mutex_lock(&(Zathura.Lock.pdflib_lock));
   GList *link_list = poppler_page_get_link_mapping(current_page->page);
@@ -2997,14 +3005,30 @@ cb_inputbar_form_activate(GtkEntry* entry, gpointer data)
     /* only handle URI and internal links */
     if(action->type == POPPLER_ACTION_URI)
     {
-      if(li == link_id++)
-        ;
+      if(li == link_id)
+        open_uri(action->uri.uri);
     }
     else if(action->type == POPPLER_ACTION_GOTO_DEST)
     {
-      if(li == link_id++)
-        ;
+      if(li == link_id)
+      {
+        if(action->goto_dest.dest->type == POPPLER_DEST_NAMED)
+        {
+          PopplerDest* destination = poppler_document_find_dest(Zathura.PDF.document, action->goto_dest.dest->named_dest);
+          if(destination)
+          {
+            new_page_id = destination->page_num - 1;
+            poppler_dest_free(destination);
+          }
+        }
+        else
+          new_page_id = action->goto_dest.dest->page_num - 1;
+      }
     }
+    else
+      continue;
+
+    link_id++;
   }
 
   poppler_page_free_link_mapping(link_list);
@@ -3014,7 +3038,7 @@ cb_inputbar_form_activate(GtkEntry* entry, gpointer data)
   Zathura.Handler.inputbar_activate = g_signal_connect(G_OBJECT(Zathura.UI.inputbar), "activate", G_CALLBACK(cb_inputbar_activate), NULL);
 
   /* reset all */
-  set_page(Zathura.PDF.page_number);
+  set_page(new_page_id);
   isc_abort(NULL);
 
   return TRUE;