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