+typedef struct _FindTask {
+ const gchar *page_label;
+ gchar *chapter;
+} FindTask;
+
+static gboolean
+ev_window_find_chapter (GtkTreeModel *tree_model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer data)
+{
+ FindTask *task = (FindTask *)data;
+ gchar *page_string;
+
+ gtk_tree_model_get (tree_model, iter,
+ EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL, &page_string,
+ -1);
+
+ if (!page_string)
+ return FALSE;
+
+ if (!strcmp (page_string, task->page_label)) {
+ gtk_tree_model_get (tree_model, iter,
+ EV_DOCUMENT_LINKS_COLUMN_MARKUP, &task->chapter,
+ -1);
+ g_free (page_string);
+ return TRUE;
+ }
+
+ g_free (page_string);
+ return FALSE;
+}
+
+static void
+ev_window_add_history (EvWindow *window, gint page, EvLink *link)
+{
+ gchar *page_label = NULL;
+ gchar *link_title;
+ FindTask find_task;
+ EvLink *real_link;
+ EvLinkAction *action;
+ EvLinkDest *dest;
+
+ if (window->priv->history == NULL)
+ return;
+
+ if (link) {
+ action = g_object_ref (ev_link_get_action (link));
+ dest = ev_link_action_get_dest (action);
+ page = ev_link_dest_get_page (dest);
+ page_label = ev_view_page_label_from_dest (EV_VIEW (window->priv->view), dest);
+ } else {
+ dest = ev_link_dest_new_page (page);
+ action = ev_link_action_new_dest (dest);
+ page_label = ev_page_cache_get_page_label (window->priv->page_cache, page);
+ }
+
+ if (!page_label)
+ return;
+
+ find_task.page_label = page_label;
+ find_task.chapter = NULL;
+
+ if (EV_IS_DOCUMENT_LINKS (window->priv->document) &&
+ ev_document_links_has_document_links (EV_DOCUMENT_LINKS (window->priv->document))) {
+ GtkTreeModel *model;
+
+ g_object_get (G_OBJECT (window->priv->sidebar_links), "model", &model, NULL);
+
+ if (model) {
+ gtk_tree_model_foreach (model,
+ ev_window_find_chapter,
+ &find_task);
+
+ g_object_unref (model);
+ }
+ }
+
+ if (find_task.chapter)
+ link_title = g_strdup_printf (_("Page %s - %s"), page_label, find_task.chapter);
+ else
+ link_title = g_strdup_printf (_("Page %s"), page_label);
+
+ real_link = ev_link_new (link_title, action);
+
+ ev_history_add_link (window->priv->history, real_link);
+
+ g_free (find_task.chapter);
+ g_free (link_title);
+ g_free (page_label);
+ g_object_unref (real_link);
+}
+
+static void
+view_handle_link_cb (EvView *view, EvLink *link, EvWindow *window)
+{
+ int current_page = ev_page_cache_get_current_page (window->priv->page_cache);
+
+ ev_window_add_history (window, 0, link);
+ ev_window_add_history (window, current_page, NULL);
+}
+
+static void
+history_changed_cb (EvPageCache *page_cache,
+ gint page,
+ EvWindow *window)
+{
+ int current_page = ev_page_cache_get_current_page (window->priv->page_cache);
+
+ ev_window_add_history (window, page, NULL);
+ ev_window_add_history (window, current_page, NULL);
+
+ return;
+}
+