]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-links.c
335913a917bc1cf297fb04b772e1dfdff4833316
[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            (EvSidebarPageIface *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
288                 gtk_tree_model_get (model, &iter,
289                                     EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
290                                     -1);
291
292                 if (!link)
293                         return;
294
295                 first_page = ev_link_get_page (link);
296                 if (first_page == -1) {
297                         g_object_unref (link);
298                         return;
299                 }
300                 
301                 first_page++;
302                 g_object_unref (link);
303
304                 if (gtk_tree_model_iter_next (model, &iter)) {
305                         gtk_tree_model_get (model, &iter,
306                                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
307                                             -1);
308
309                         if (link) {
310                                 last_page = ev_link_get_page (link);
311                                 g_object_unref (link);
312                         }
313                 } else {
314                         last_page = ev_document_get_n_pages (sidebar->priv->document);
315                 }
316
317                 if (last_page == -1)
318                         last_page = ev_document_get_n_pages (sidebar->priv->document);
319         
320                 window = gtk_widget_get_toplevel (GTK_WIDGET (sidebar));
321                 if (EV_IS_WINDOW (window)) {
322                         ev_window_print_range (EV_WINDOW (window), first_page, last_page);
323                 }
324         }
325 }
326
327 static GtkMenu *
328 build_popup_menu (EvSidebarLinks *sidebar)
329 {
330         GtkWidget *menu;
331         GtkWidget *item;
332
333         menu = gtk_menu_new ();
334         item = gtk_image_menu_item_new_from_stock (GTK_STOCK_PRINT, NULL);
335         gtk_label_set_label (GTK_LABEL (gtk_bin_get_child (GTK_BIN (item))), _("Print…"));
336         gtk_widget_show (item);
337         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
338         g_signal_connect (item, "activate",
339                           G_CALLBACK (print_section_cb), sidebar);
340
341         return GTK_MENU (menu);
342 }
343
344 static void
345 popup_menu_cb (GtkWidget *treeview, EvSidebarLinks *sidebar)
346 {
347         GtkMenu *menu = build_popup_menu (sidebar);
348
349         gtk_menu_popup (menu, NULL, NULL,
350                         ev_gui_menu_position_tree_selection,
351                         sidebar->priv->tree_view, 0,
352                         gtk_get_current_event_time ());
353         gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
354 }
355
356 static gboolean
357 button_press_cb (GtkWidget *treeview,
358                  GdkEventButton *event,
359                  EvSidebarLinks *sidebar)
360 {
361         GtkTreePath *path = NULL;
362
363         if (event->button == 3) {
364                 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (treeview),
365                                                    event->x,
366                                                    event->y,
367                                                    &path,
368                                                    NULL, NULL, NULL)) {
369                         gtk_tree_view_set_cursor (GTK_TREE_VIEW (treeview),
370                                                   path, NULL, FALSE);
371                         gtk_menu_popup (build_popup_menu (sidebar), NULL,
372                                         NULL, NULL, NULL, event->button,
373                                         gtk_get_current_event_time ());
374                         gtk_tree_path_free (path);
375
376                         return TRUE;
377                 }
378         }
379
380         return FALSE;
381 }
382
383
384 static void
385 ev_sidebar_links_construct (EvSidebarLinks *ev_sidebar_links)
386 {
387         EvSidebarLinksPrivate *priv;
388         GtkWidget *swindow;
389         GtkTreeViewColumn *column;
390         GtkCellRenderer *renderer;
391         GtkTreeSelection *selection;
392         GtkTreeModel *loading_model;
393
394         priv = ev_sidebar_links->priv;
395
396         swindow = gtk_scrolled_window_new (NULL, NULL);
397
398         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
399                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
400         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
401                                              GTK_SHADOW_IN);
402
403         /* Create tree view */
404         loading_model = create_loading_model ();
405         priv->tree_view = gtk_tree_view_new_with_model (loading_model);
406         g_object_unref (loading_model);
407         
408         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
409         gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
410         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
411         gtk_container_add (GTK_CONTAINER (swindow), priv->tree_view);
412
413         gtk_box_pack_start (GTK_BOX (ev_sidebar_links), swindow, TRUE, TRUE, 0);
414         gtk_widget_show_all (GTK_WIDGET (ev_sidebar_links));
415
416         column = gtk_tree_view_column_new ();
417         gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
418         gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), column);
419
420         renderer = (GtkCellRenderer*)
421                 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
422                               "ellipsize", PANGO_ELLIPSIZE_END,
423                               NULL);
424         gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, TRUE);
425         gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
426                                              "markup", EV_DOCUMENT_LINKS_COLUMN_MARKUP,
427                                              NULL);
428
429         
430         renderer = gtk_cell_renderer_text_new ();
431         gtk_tree_view_column_pack_end (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
432         gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
433                                              "text", EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL,
434                                              NULL);
435         g_object_set (G_OBJECT (renderer), "style", PANGO_STYLE_ITALIC, NULL);
436
437         g_signal_connect (priv->tree_view,
438                           "button_press_event",
439                           G_CALLBACK (button_press_cb),
440                           ev_sidebar_links);
441         g_signal_connect (priv->tree_view,
442                           "popup_menu",
443                           G_CALLBACK (popup_menu_cb),
444                           ev_sidebar_links);
445 }
446
447 static void
448 ev_sidebar_links_init (EvSidebarLinks *ev_sidebar_links)
449 {
450         ev_sidebar_links->priv = EV_SIDEBAR_LINKS_GET_PRIVATE (ev_sidebar_links);
451
452         ev_sidebar_links_construct (ev_sidebar_links);
453 }
454
455 static gboolean
456 fill_page_labels (GtkTreeModel *tree_model,
457                   GtkTreePath *path,
458                   GtkTreeIter *iter,
459                   EvSidebarLinks    *sidebar_links)
460 {
461         EvLink *link;
462         gint page;
463         gchar *page_label;
464
465         gtk_tree_model_get (tree_model, iter,
466                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
467                             -1);
468
469         if (!link)
470                 return FALSE;
471
472         page = ev_link_get_page (link);
473
474         if (page < 0) 
475                 return FALSE;
476         
477         page_label = ev_document_get_page_label (sidebar_links->priv->document,
478                                                  page);
479         gtk_tree_store_set (GTK_TREE_STORE (tree_model), iter,
480                             EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL, page_label, 
481                             -1);
482
483         g_free (page_label);
484
485         g_object_unref (link);
486         return FALSE;
487 }
488
489 /* Public Functions */
490
491 GtkWidget *
492 ev_sidebar_links_new (void)
493 {
494         GtkWidget *ev_sidebar_links;
495
496         ev_sidebar_links = g_object_new (EV_TYPE_SIDEBAR_LINKS, NULL);
497
498         return ev_sidebar_links;
499 }
500
501 static gboolean
502 update_page_callback_foreach (GtkTreeModel *model,
503                               GtkTreePath  *path,
504                               GtkTreeIter  *iter,
505                               gpointer      data)
506 {
507         EvSidebarLinks *sidebar_links = (data);
508         EvLink *link;
509
510         gtk_tree_model_get (model, iter,
511                             EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
512                             -1);
513
514         if (link) {
515                 int current_page;
516                 int dest_page;
517
518                 dest_page = ev_link_get_page (link);
519                 g_object_unref (link);
520                 
521                 current_page = ev_document_model_get_page (sidebar_links->priv->doc_model);
522                          
523                 if (dest_page == current_page) {
524                         gtk_tree_view_expand_to_path (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
525                                                       path);
526                         gtk_tree_view_set_cursor (GTK_TREE_VIEW (sidebar_links->priv->tree_view),
527                                                   path, NULL, FALSE);
528                         
529                         return TRUE;
530                 }
531         }
532
533         return FALSE;
534 }
535
536 static void
537 ev_sidebar_links_set_current_page (EvSidebarLinks *sidebar_links,
538                                    gint            current_page)
539 {
540         GtkTreeSelection *selection;
541         GtkTreeModel *model;
542         GtkTreeIter iter;
543
544         /* Widget is not currently visible */
545         if (!gtk_widget_get_mapped (GTK_WIDGET (sidebar_links)))
546                 return;
547         
548         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (sidebar_links->priv->tree_view));
549
550         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
551                 EvLink *link;
552
553                 gtk_tree_model_get (model, &iter,
554                                     EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
555                                     -1);
556                 if (link) {
557                         gint dest_page;
558
559                         dest_page = ev_link_get_page (link);
560                         g_object_unref (link);
561                         
562                         if (dest_page == current_page)
563                                 return;
564                 }
565         }               
566
567         /* We go through the tree linearly looking for the first page that
568          * matches.  This is pretty inefficient.  We can do something neat with
569          * a GtkTreeModelSort here to make it faster, if it turns out to be
570          * slow.
571          */
572         g_signal_handler_block (selection, sidebar_links->priv->selection_id);
573         g_signal_handler_block (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
574
575         gtk_tree_model_foreach (model,
576                                 update_page_callback_foreach,
577                                 sidebar_links);
578         
579         g_signal_handler_unblock (selection, sidebar_links->priv->selection_id);
580         g_signal_handler_unblock (sidebar_links->priv->tree_view, sidebar_links->priv->row_activated_id);
581 }
582
583 static void
584 update_page_callback (EvSidebarLinks *sidebar_links,
585                       gint            old_page,
586                       gint            new_page)
587 {
588         ev_sidebar_links_set_current_page (sidebar_links, new_page);
589 }
590
591 static void 
592 row_activated_callback (GtkTreeView       *treeview,
593                         GtkTreePath       *arg1,
594                         GtkTreeViewColumn *arg2,
595                         gpointer           user_data)
596 {
597         if (gtk_tree_view_row_expanded (GTK_TREE_VIEW (treeview), arg1)) {
598                 gtk_tree_view_collapse_row (GTK_TREE_VIEW (treeview), arg1);
599         } else {
600                 gtk_tree_view_expand_row (GTK_TREE_VIEW (treeview), arg1, FALSE);
601         }
602 }
603
604 static void
605 expand_open_links (GtkTreeView *tree_view, GtkTreeModel *model, GtkTreeIter *parent)
606 {
607         GtkTreeIter iter;
608         gboolean expand;
609
610         if (gtk_tree_model_iter_children (model, &iter, parent)) {
611                 do {
612                         gtk_tree_model_get (model, &iter,
613                                             EV_DOCUMENT_LINKS_COLUMN_EXPAND, &expand,
614                                             -1);
615                         if (expand) {
616                                 GtkTreePath *path;
617                                 
618                                 path = gtk_tree_model_get_path (model, &iter);
619                                 gtk_tree_view_expand_row (tree_view, path, FALSE);
620                                 gtk_tree_path_free (path);
621                         }
622
623                         expand_open_links (tree_view, model, &iter);
624                 } while (gtk_tree_model_iter_next (model, &iter));
625         }
626 }
627
628 static void
629 ev_sidebar_links_set_links_model (EvSidebarLinks *sidebar_links,
630                                   GtkTreeModel   *model)
631 {
632         EvSidebarLinksPrivate *priv = sidebar_links->priv;
633
634         if (priv->model == model)
635                 return;
636
637         if (priv->model)
638                 g_object_unref (priv->model);
639         priv->model = g_object_ref (model);
640
641         g_object_notify (G_OBJECT (sidebar_links), "model");
642 }
643
644 static void
645 job_finished_callback (EvJobLinks     *job,
646                        EvSidebarLinks *sidebar_links)
647 {
648         EvSidebarLinksPrivate *priv = sidebar_links->priv;
649         GtkTreeSelection *selection;
650
651         ev_sidebar_links_set_links_model (sidebar_links, job->model);
652
653         gtk_tree_model_foreach (priv->model, (GtkTreeModelForeachFunc)fill_page_labels, sidebar_links);
654
655         gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), job->model);
656         
657         g_object_unref (job);
658         priv->job = NULL;
659
660         expand_open_links (GTK_TREE_VIEW (priv->tree_view), priv->model, NULL);
661
662         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
663         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
664         
665         if (priv->selection_id <= 0) {
666                 priv->selection_id =
667                         g_signal_connect (selection, "changed",
668                                           G_CALLBACK (selection_changed_callback),
669                                           sidebar_links);
670         }
671         priv->page_changed_id =
672                 g_signal_connect_swapped (priv->doc_model, "page-changed",
673                                           G_CALLBACK (update_page_callback),
674                                           sidebar_links);
675         if (priv->row_activated_id <= 0) {
676                 priv->row_activated_id =
677                         g_signal_connect (priv->tree_view, "row-activated",
678                                           G_CALLBACK (row_activated_callback),
679                                           sidebar_links);
680         }
681
682         ev_sidebar_links_set_current_page (sidebar_links,
683                                            ev_document_model_get_page (priv->doc_model));
684 }
685
686 static void
687 ev_sidebar_links_document_changed_cb (EvDocumentModel *model,
688                                       GParamSpec      *pspec,
689                                       EvSidebarLinks  *sidebar_links)
690 {
691         EvDocument *document = ev_document_model_get_document (model);
692         EvSidebarLinksPrivate *priv = sidebar_links->priv;
693
694         if (!EV_IS_DOCUMENT_LINKS (document))
695                 return;
696
697         if (!ev_document_links_has_document_links (EV_DOCUMENT_LINKS (document)))
698                 return;
699
700         if (priv->document) {
701                 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), NULL);
702                 g_object_unref (priv->document);
703         }
704
705         priv->document = g_object_ref (document);
706
707         if (priv->job) {
708                 g_signal_handlers_disconnect_by_func (priv->job,
709                                                       job_finished_callback,
710                                                       sidebar_links);
711                 g_object_unref (priv->job);
712         }
713
714         priv->job = ev_job_links_new (document);
715         g_signal_connect (priv->job,
716                           "finished",
717                           G_CALLBACK (job_finished_callback),
718                           sidebar_links);
719         /* The priority doesn't matter for this job */
720         ev_job_scheduler_push_job (priv->job, EV_JOB_PRIORITY_NONE);
721 }
722
723 static void
724 ev_sidebar_links_set_model (EvSidebarPage   *sidebar_page,
725                             EvDocumentModel *model)
726 {
727         EvSidebarLinks *sidebar_links = EV_SIDEBAR_LINKS (sidebar_page);
728         EvSidebarLinksPrivate *priv = sidebar_links->priv;
729
730         if (priv->doc_model == model)
731                 return;
732
733         priv->doc_model = model;
734         g_signal_connect (model, "notify::document",
735                           G_CALLBACK (ev_sidebar_links_document_changed_cb),
736                           sidebar_page);
737 }
738
739 static gboolean
740 ev_sidebar_links_support_document (EvSidebarPage  *sidebar_page,
741                                    EvDocument *document)
742 {
743         return (EV_IS_DOCUMENT_LINKS (document) &&
744                     ev_document_links_has_document_links (EV_DOCUMENT_LINKS (document)));
745 }
746
747 static const gchar*
748 ev_sidebar_links_get_label (EvSidebarPage *sidebar_page)
749 {
750     return _("Index");
751 }
752
753 static void
754 ev_sidebar_links_page_iface_init (EvSidebarPageIface *iface)
755 {
756         iface->support_document = ev_sidebar_links_support_document;
757         iface->set_model = ev_sidebar_links_set_model;
758         iface->get_label = ev_sidebar_links_get_label;
759 }
760