]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-links.c
bookmarks: Add a popup menu to open, rename and remove bookmarks
[evince.git] / shell / ev-sidebar-links.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Red Hat, Inc.
4  *
5  *  Author:
6  *    Jonathan Blandford <jrb@alum.mit.edu>
7  *
8  * Evince is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Evince is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31
32 #include "ev-document-links.h"
33 #include "ev-job-scheduler.h"
34 #include "ev-sidebar-links.h"
35 #include "ev-sidebar-page.h"
36 #include "ev-window.h"
37
38 struct _EvSidebarLinksPrivate {
39         GtkWidget *tree_view;
40
41         /* Keep these ids around for blocking */
42         guint selection_id;
43         guint page_changed_id;
44         guint row_activated_id;
45
46         EvJob *job;
47         GtkTreeModel *model;
48         EvDocument *document;
49         EvDocumentModel *doc_model;
50 };
51
52 enum {
53         PROP_0,
54         PROP_MODEL,
55         PROP_WIDGET,
56 };
57
58 enum {
59         LINK_ACTIVATED,
60         N_SIGNALS
61 };
62
63 static void update_page_callback                        (EvSidebarLinks    *sidebar_links,
64                                                          gint               old_page,
65                                                          gint               current_page);
66 static void row_activated_callback                      (GtkTreeView *treeview,
67                                                          GtkTreePath *arg1,
68                                                          GtkTreeViewColumn *arg2,
69                                                          gpointer user_data);
70 static void ev_sidebar_links_set_links_model            (EvSidebarLinks *links,
71                                                          GtkTreeModel   *model);
72 static void job_finished_callback                       (EvJobLinks     *job,
73                                                          EvSidebarLinks *sidebar_links);
74 static void ev_sidebar_links_set_current_page           (EvSidebarLinks *sidebar_links,
75                                                          gint            current_page);
76 static void ev_sidebar_links_page_iface_init            (EvSidebarPageInterface *iface);
77 static gboolean ev_sidebar_links_support_document       (EvSidebarPage  *sidebar_page,
78                                                          EvDocument     *document);
79 static const gchar* ev_sidebar_links_get_label          (EvSidebarPage *sidebar_page);
80
81 static guint signals[N_SIGNALS];
82
83 G_DEFINE_TYPE_EXTENDED (EvSidebarLinks, 
84                         ev_sidebar_links, 
85                         GTK_TYPE_VBOX,
86                         0, 
87                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
88                                                ev_sidebar_links_page_iface_init))
89
90
91 #define EV_SIDEBAR_LINKS_GET_PRIVATE(object) \
92         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_LINKS, EvSidebarLinksPrivate))
93
94 static void
95 ev_sidebar_links_set_property (GObject      *object,
96                                guint         prop_id,
97                                const GValue *value,
98                                GParamSpec   *pspec)
99 {
100         EvSidebarLinks *ev_sidebar_links = EV_SIDEBAR_LINKS (object);
101
102         switch (prop_id)
103         {
104         case PROP_MODEL:
105                 ev_sidebar_links_set_links_model (ev_sidebar_links, g_value_get_object (value));
106                 break;
107         default:
108                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109                 break;
110         }
111 }
112
113 static void
114 ev_sidebar_links_get_property (GObject    *object,
115                                guint       prop_id,
116                                GValue     *value,
117                                GParamSpec *pspec)
118 {
119         EvSidebarLinks *ev_sidebar_links;
120   
121         ev_sidebar_links = EV_SIDEBAR_LINKS (object);
122
123         switch (prop_id)
124         {
125         case PROP_MODEL:
126                 g_value_set_object (value, ev_sidebar_links->priv->model);
127                 break;
128         case PROP_WIDGET:
129                 g_value_set_object (value, ev_sidebar_links->priv->tree_view);
130                 break;
131         default:
132                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133                 break;
134         }
135 }
136
137 static void
138 ev_sidebar_links_dispose (GObject *object)
139 {
140         EvSidebarLinks *sidebar = EV_SIDEBAR_LINKS (object);
141
142         if (sidebar->priv->job) {
143                 g_signal_handlers_disconnect_by_func (sidebar->priv->job,
144                                                       job_finished_callback, sidebar);
145                 ev_job_cancel (sidebar->priv->job);                                                   
146                 g_object_unref (sidebar->priv->job);
147                 sidebar->priv->job = NULL;
148         }
149
150         if (sidebar->priv->model) {
151                 g_object_unref (sidebar->priv->model);
152                 sidebar->priv->model = NULL;
153         }
154
155         if (sidebar->priv->document) {
156                 g_object_unref (sidebar->priv->document);
157                 sidebar->priv->document = NULL;
158                 sidebar->priv->doc_model = NULL;
159         }
160
161         G_OBJECT_CLASS (ev_sidebar_links_parent_class)->dispose (object);
162 }
163
164 static void
165 ev_sidebar_links_map (GtkWidget *widget)
166 {
167         EvSidebarLinks *links;
168
169         links = EV_SIDEBAR_LINKS (widget);
170
171         GTK_WIDGET_CLASS (ev_sidebar_links_parent_class)->map (widget);
172
173         if (links->priv->model) {
174                 ev_sidebar_links_set_current_page (links,
175                                                    ev_document_model_get_page (links->priv->doc_model));
176         }
177 }
178
179 static void
180 ev_sidebar_links_class_init (EvSidebarLinksClass *ev_sidebar_links_class)
181 {
182         GObjectClass   *g_object_class;
183         GtkWidgetClass *widget_class;
184
185         g_object_class = G_OBJECT_CLASS (ev_sidebar_links_class);
186         widget_class = GTK_WIDGET_CLASS (ev_sidebar_links_class);
187
188         g_object_class->set_property = ev_sidebar_links_set_property;
189         g_object_class->get_property = ev_sidebar_links_get_property;
190         g_object_class->dispose = ev_sidebar_links_dispose;
191
192         widget_class->map = ev_sidebar_links_map;
193
194         signals[LINK_ACTIVATED] = g_signal_new ("link-activated",
195                          G_TYPE_FROM_CLASS (g_object_class),
196                          G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
197                          G_STRUCT_OFFSET (EvSidebarLinksClass, link_activated),
198                          NULL, NULL,
199                          g_cclosure_marshal_VOID__OBJECT,
200                          G_TYPE_NONE, 1, G_TYPE_OBJECT);
201
202         g_object_class_install_property (g_object_class,
203                                          PROP_MODEL,
204                                          g_param_spec_object ("model",
205                                                               "Model",
206                                                               "Current Model",
207                                                               GTK_TYPE_TREE_MODEL,
208                                                               G_PARAM_READWRITE));
209         g_object_class_override_property (g_object_class,
210                                           PROP_WIDGET,
211                                           "main-widget");
212
213         g_type_class_add_private (g_object_class, sizeof (EvSidebarLinksPrivate));
214 }
215
216 static void
217 selection_changed_callback (GtkTreeSelection   *selection,
218                             EvSidebarLinks     *ev_sidebar_links)
219 {
220         EvDocument *document;
221         GtkTreeModel *model;
222         GtkTreeIter iter;
223
224         document = EV_DOCUMENT (ev_sidebar_links->priv->document);
225         g_return_if_fail (ev_sidebar_links->priv->document != NULL);
226
227         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
228                 EvLink *link;
229
230                 gtk_tree_model_get (model, &iter,
231                                     EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
232                                     -1);
233                 
234                 if (link == NULL)
235                         return;
236
237                 g_signal_handler_block (ev_sidebar_links->priv->doc_model,
238                                         ev_sidebar_links->priv->page_changed_id);
239                 g_signal_emit (ev_sidebar_links, signals[LINK_ACTIVATED], 0, link);
240                 g_signal_handler_unblock (ev_sidebar_links->priv->doc_model,
241                                           ev_sidebar_links->priv->page_changed_id);
242
243                 g_object_unref (link);
244         }
245 }
246
247 static GtkTreeModel *
248 create_loading_model (void)
249 {
250         GtkTreeModel *retval;
251         GtkTreeIter iter;
252         gchar *markup;
253
254         /* Creates a fake model to indicate that we're loading */
255         retval = (GtkTreeModel *)gtk_list_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
256                                                      G_TYPE_STRING,
257                                                      G_TYPE_OBJECT,
258                                                      G_TYPE_BOOLEAN,
259                                                      G_TYPE_STRING);
260
261         gtk_list_store_append (GTK_LIST_STORE (retval), &iter);
262         markup = g_strdup_printf ("<span size=\"larger\" style=\"italic\">%s</span>", _("Loading…"));
263         gtk_list_store_set (GTK_LIST_STORE (retval), &iter,
264                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, markup,
265                             EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
266                             EV_DOCUMENT_LINKS_COLUMN_LINK, NULL,
267                             -1);
268         g_free (markup);
269
270         return retval;
271 }
272
273 static void
274 print_section_cb (GtkWidget *menuitem, EvSidebarLinks *sidebar)
275 {
276         GtkWidget *window;
277         GtkTreeSelection *selection;
278         GtkTreeModel *model;
279         GtkTreeIter iter;
280
281         selection = gtk_tree_view_get_selection
282                 (GTK_TREE_VIEW (sidebar->priv->tree_view));
283
284         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
285                 EvLink *link;
286                 int first_page, last_page = -1;
287                 EvDocumentLinks *document_links;
288
289                 gtk_tree_model_get (model, &iter,
290                                     EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
291                                     -1);
292
293                 if (!link)
294                         return;
295
296                 document_links = EV_DOCUMENT_LINKS (sidebar->priv->document);
297
298                 first_page = ev_document_links_get_link_page (document_links, link);
299                 if (first_page == -1) {
300                         g_object_unref (link);
301                         return;
302                 }
303                 
304                 first_page++;
305                 g_object_unref (link);
306
307                 if (gtk_tree_model_iter_next (model, &iter)) {
308                         gtk_tree_model_get (model, &iter,
309                                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
310                                             -1);
311
312                         if (link) {
313                                 last_page = ev_document_links_get_link_page (document_links, link);;
314                                 g_object_unref (link);
315                         }
316                 } else {
317                         last_page = ev_document_get_n_pages (sidebar->priv->document);
318                 }
319
320                 if (last_page == -1)
321                         last_page = ev_document_get_n_pages (sidebar->priv->document);
322         
323                 window = gtk_widget_get_toplevel (GTK_WIDGET (sidebar));
324                 if (EV_IS_WINDOW (window)) {
325                         ev_window_print_range (EV_WINDOW (window), first_page, last_page);
326                 }
327         }
328 }
329
330 static GtkMenu *
331 build_popup_menu (EvSidebarLinks *sidebar)
332 {
333         GtkWidget *menu;
334         GtkWidget *item;
335
336         menu = gtk_menu_new ();
337         item = gtk_image_menu_item_new_from_stock (GTK_STOCK_PRINT, NULL);
338         gtk_label_set_label (GTK_LABEL (gtk_bin_get_child (GTK_BIN (item))), _("Print…"));
339         gtk_widget_show (item);
340         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
341         g_signal_connect (item, "activate",
342                           G_CALLBACK (print_section_cb), sidebar);
343
344         return GTK_MENU (menu);
345 }
346
347 static void
348 popup_menu_cb (GtkWidget *treeview, EvSidebarLinks *sidebar)
349 {
350         GtkMenu *menu = build_popup_menu (sidebar);
351
352         gtk_menu_popup (menu, NULL, NULL,
353                         ev_gui_menu_position_tree_selection,
354                         sidebar->priv->tree_view, 0,
355                         gtk_get_current_event_time ());
356         gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
357 }
358
359 static gboolean
360 button_press_cb (GtkWidget *treeview,
361                  GdkEventButton *event,
362                  EvSidebarLinks *sidebar)
363 {
364         GtkTreePath *path = NULL;
365
366         if (event->button == 3) {
367                 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (treeview),
368                                                    event->x,
369                                                    event->y,
370                                                    &path,
371                                                    NULL, NULL, NULL)) {
372                         gtk_tree_view_set_cursor (GTK_TREE_VIEW (treeview),
373                                                   path, NULL, FALSE);
374                         gtk_menu_popup (build_popup_menu (sidebar), NULL,
375                                         NULL, NULL, NULL, event->button,
376                                         gtk_get_current_event_time ());
377                         gtk_tree_path_free (path);
378
379                         return TRUE;
380                 }
381         }
382
383         return FALSE;
384 }
385
386
387 static void
388 ev_sidebar_links_construct (EvSidebarLinks *ev_sidebar_links)
389 {
390         EvSidebarLinksPrivate *priv;
391         GtkWidget *swindow;
392         GtkTreeViewColumn *column;
393         GtkCellRenderer *renderer;
394         GtkTreeSelection *selection;
395         GtkTreeModel *loading_model;
396
397         priv = ev_sidebar_links->priv;
398
399         swindow = gtk_scrolled_window_new (NULL, NULL);
400
401         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
402                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
403         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
404                                              GTK_SHADOW_IN);
405
406         /* Create tree view */
407         loading_model = create_loading_model ();
408         priv->tree_view = gtk_tree_view_new_with_model (loading_model);
409         g_object_unref (loading_model);
410         
411         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
412         gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
413         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
414         gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
415
416         gtk_box_pack_start (GTK_BOX (ev_sidebar_links), swindow, TRUE, TRUE, 0);
417         gtk_widget_show_all (GTK_WIDGET (ev_sidebar_links));
418
419         column = gtk_tree_view_column_new ();
420         gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
421         gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), column);
422
423         renderer = (GtkCellRenderer*)
424                 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
425                               "ellipsize", PANGO_ELLIPSIZE_END,
426                               NULL);
427         gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, TRUE);
428         gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
429                                              "markup", EV_DOCUMENT_LINKS_COLUMN_MARKUP,
430                                              NULL);
431
432         
433         renderer = gtk_cell_renderer_text_new ();
434         gtk_tree_view_column_pack_end (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
435         gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
436                                              "text", EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL,
437                                              NULL);
438         g_object_set (G_OBJECT (renderer), "style", PANGO_STYLE_ITALIC, NULL);
439
440         g_signal_connect (priv->tree_view,
441                           "button_press_event",
442                           G_CALLBACK (button_press_cb),
443                           ev_sidebar_links);
444         g_signal_connect (priv->tree_view,
445                           "popup_menu",
446                           G_CALLBACK (popup_menu_cb),
447                           ev_sidebar_links);
448 }
449
450 static void
451 ev_sidebar_links_init (EvSidebarLinks *ev_sidebar_links)
452 {
453         ev_sidebar_links->priv = EV_SIDEBAR_LINKS_GET_PRIVATE (ev_sidebar_links);
454
455         ev_sidebar_links_construct (ev_sidebar_links);
456 }
457
458 /* Public Functions */
459
460 GtkWidget *
461 ev_sidebar_links_new (void)
462 {
463         GtkWidget *ev_sidebar_links;
464
465         ev_sidebar_links = g_object_new (EV_TYPE_SIDEBAR_LINKS, NULL);
466
467         return ev_sidebar_links;
468 }
469
470 static gboolean
471 update_page_callback_foreach (GtkTreeModel *model,
472                               GtkTreePath  *path,
473                               GtkTreeIter  *iter,
474                               gpointer      data)
475 {
476         EvSidebarLinks *sidebar_links = (data);
477         EvLink *link;
478
479         gtk_tree_model_get (model, iter,
480                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
481                             -1);
482
483         if (link) {
484                 int current_page;
485                 int dest_page;
486                 EvDocumentLinks *document_links = EV_DOCUMENT_LINKS (sidebar_links->priv->document);
487
488                 dest_page = ev_document_links_get_link_page (document_links, link);
489                 g_object_unref (link);
490                 
491                 current_page = ev_document_model_get_page (sidebar_links->priv->doc_model);
492                          
493                 if (dest_page == current_page) {
494                         gtk_tree_view_expand_to_path (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
495                                                       path);
496                         gtk_tree_view_set_cursor (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
497                                                   path, NULL, FALSE);
498                         
499                         return TRUE;
500                 }
501         }
502
503         return FALSE;
504 }
505
506 static void
507 ev_sidebar_links_set_current_page (EvSidebarLinks *sidebar_links,
508                                    gint            current_page)
509 {
510         GtkTreeSelection *selection;
511         GtkTreeModel *model;
512         GtkTreeIter iter;
513
514         /* Widget is not currently visible */
515         if (!gtk_widget_get_mapped (GTK_WIDGET (sidebar_links)))
516                 return;
517         
518         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (sidebar_links->priv->tree_view));
519
520         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
521                 EvLink *link;
522
523                 gtk_tree_model_get (model, &iter,
524                                     EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
525                                     -1);
526                 if (link) {
527                         gint dest_page;
528                         EvDocumentLinks *document_links = EV_DOCUMENT_LINKS (sidebar_links->priv->document);
529
530                         dest_page = ev_document_links_get_link_page (document_links, link);
531                         g_object_unref (link);
532                         
533                         if (dest_page == current_page)
534                                 return;
535                 }
536         }               
537
538         /* We go through the tree linearly looking for the first page that
539          * matches.  This is pretty inefficient.  We can do something neat with
540          * a GtkTreeModelSort here to make it faster, if it turns out to be
541          * slow.
542          */
543         g_signal_handler_block (selection, sidebar_links->priv->selection_id);
544         g_signal_handler_block (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
545
546         gtk_tree_model_foreach (model,
547                                 update_page_callback_foreach,
548                                 sidebar_links);
549         
550         g_signal_handler_unblock (selection, sidebar_links->priv->selection_id);
551         g_signal_handler_unblock (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
552 }
553
554 static void
555 update_page_callback (EvSidebarLinks *sidebar_links,
556                       gint            old_page,
557                       gint            new_page)
558 {
559         ev_sidebar_links_set_current_page (sidebar_links, new_page);
560 }
561
562 static void 
563 row_activated_callback (GtkTreeView       *treeview,
564                         GtkTreePath       *arg1,
565                         GtkTreeViewColumn *arg2,
566                         gpointer           user_data)
567 {
568         if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (treeview), arg1)) {
569                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (treeview), arg1);
570         } else {
571                 gtk_tree_view_expand_row (GTK_TREE_VIEW (treeview), arg1, FALSE);
572         }
573 }
574
575 static void
576 expand_open_links (GtkTreeView *tree_view, GtkTreeModel *model, GtkTreeIter *parent)
577 {
578         GtkTreeIter iter;
579         gboolean expand;
580
581         if (gtk_tree_model_iter_children (model, &iter, parent)) {
582                 do {
583                         gtk_tree_model_get (model, &iter,
584                                             EV_DOCUMENT_LINKS_COLUMN_EXPAND, &expand,
585                                             -1);
586                         if (expand) {
587                                 GtkTreePath *path;
588                                 
589                                 path = gtk_tree_model_get_path (model, &iter);
590                                 gtk_tree_view_expand_row (tree_view, path, FALSE);
591                                 gtk_tree_path_free (path);
592                         }
593
594                         expand_open_links (tree_view, model, &iter);
595                 } while (gtk_tree_model_iter_next (model, &iter));
596         }
597 }
598
599 static void
600 ev_sidebar_links_set_links_model (EvSidebarLinks *sidebar_links,
601                                   GtkTreeModel   *model)
602 {
603         EvSidebarLinksPrivate *priv = sidebar_links->priv;
604
605         if (priv->model == model)
606                 return;
607
608         if (priv->model)
609                 g_object_unref (priv->model);
610         priv->model = g_object_ref (model);
611
612         g_object_notify (G_OBJECT (sidebar_links), "model");
613 }
614
615 static void
616 job_finished_callback (EvJobLinks     *job,
617                        EvSidebarLinks *sidebar_links)
618 {
619         EvSidebarLinksPrivate *priv = sidebar_links->priv;
620         GtkTreeSelection *selection;
621
622         ev_sidebar_links_set_links_model (sidebar_links, job->model);
623
624         gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), job->model);
625         
626         g_object_unref (job);
627         priv->job = NULL;
628
629         expand_open_links (GTK_TREE_VIEW (priv->tree_view), priv->model, NULL);
630
631         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
632         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
633         
634         if (priv->selection_id <= 0) {
635                 priv->selection_id =
636                         g_signal_connect (selection, "changed",
637                                           G_CALLBACK (selection_changed_callback),
638                                           sidebar_links);
639         }
640         priv->page_changed_id =
641                 g_signal_connect_swapped (priv->doc_model, "page-changed",
642                                           G_CALLBACK (update_page_callback),
643                                           sidebar_links);
644         if (priv->row_activated_id <= 0) {
645                 priv->row_activated_id =
646                         g_signal_connect (priv->tree_view, "row-activated",
647                                           G_CALLBACK (row_activated_callback),
648                                           sidebar_links);
649         }
650
651         ev_sidebar_links_set_current_page (sidebar_links,
652                                            ev_document_model_get_page (priv->doc_model));
653 }
654
655 static void
656 ev_sidebar_links_document_changed_cb (EvDocumentModel *model,
657                                       GParamSpec      *pspec,
658                                       EvSidebarLinks  *sidebar_links)
659 {
660         EvDocument *document = ev_document_model_get_document (model);
661         EvSidebarLinksPrivate *priv = sidebar_links->priv;
662
663         if (!EV_IS_DOCUMENT_LINKS (document))
664                 return;
665
666         if (!ev_document_links_has_document_links (EV_DOCUMENT_LINKS (document)))
667                 return;
668
669         if (priv->document) {
670                 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
671                 g_object_unref (priv->document);
672         }
673
674         priv->document = g_object_ref (document);
675
676         if (priv->job) {
677                 g_signal_handlers_disconnect_by_func (priv->job,
678                                                       job_finished_callback,
679                                                       sidebar_links);
680                 g_object_unref (priv->job);
681         }
682
683         priv->job = ev_job_links_new (document);
684         g_signal_connect (priv->job,
685                           "finished",
686                           G_CALLBACK (job_finished_callback),
687                           sidebar_links);
688         /* The priority doesn't matter for this job */
689         ev_job_scheduler_push_job (priv->job, EV_JOB_PRIORITY_NONE);
690 }
691
692 static void
693 ev_sidebar_links_set_model (EvSidebarPage   *sidebar_page,
694                             EvDocumentModel *model)
695 {
696         EvSidebarLinks *sidebar_links = EV_SIDEBAR_LINKS (sidebar_page);
697         EvSidebarLinksPrivate *priv = sidebar_links->priv;
698
699         if (priv->doc_model == model)
700                 return;
701
702         priv->doc_model = model;
703         g_signal_connect (model, "notify::document",
704                           G_CALLBACK (ev_sidebar_links_document_changed_cb),
705                           sidebar_page);
706 }
707
708 static gboolean
709 ev_sidebar_links_support_document (EvSidebarPage  *sidebar_page,
710                                    EvDocument *document)
711 {
712         return (EV_IS_DOCUMENT_LINKS (document) &&
713                     ev_document_links_has_document_links (EV_DOCUMENT_LINKS (document)));
714 }
715
716 static const gchar*
717 ev_sidebar_links_get_label (EvSidebarPage *sidebar_page)
718 {
719     return _("Index");
720 }
721
722 static void
723 ev_sidebar_links_page_iface_init (EvSidebarPageInterface *iface)
724 {
725         iface->support_document = ev_sidebar_links_support_document;
726         iface->set_model = ev_sidebar_links_set_model;
727         iface->get_label = ev_sidebar_links_get_label;
728 }
729