]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-thumbnails.c
Fix crash on exit, described in the bug #336825.
[evince.git] / shell / ev-sidebar-thumbnails.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Red Hat, Inc.
4  *  Copyright (C) 2004, 2005 Anders Carlsson <andersca@gnome.org>
5  *
6  *  Authors:
7  *    Jonathan Blandford <jrb@alum.mit.edu>
8  *    Anders Carlsson <andersca@gnome.org>
9  *
10  * Evince is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * Evince is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <string.h>
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32
33 #include "ev-sidebar-page.h"
34 #include "ev-sidebar-thumbnails.h"
35 #include "ev-document-thumbnails.h"
36 #include "ev-document-misc.h"
37 #include "ev-job-queue.h"
38 #include "ev-window.h"
39 #include "ev-utils.h"
40
41 #define THUMBNAIL_WIDTH 100
42
43 /* The IconView doesn't scale nearly as well as the TreeView, so we arbitrarily
44  * limit its use */
45 #define MAX_ICON_VIEW_PAGE_COUNT 1500
46
47
48 struct _EvSidebarThumbnailsPrivate {
49         GtkWidget *swindow;
50         GtkWidget *icon_view;
51         GtkWidget *tree_view;
52         GtkAdjustment *vadjustment;
53         GtkListStore *list_store;
54         GdkPixbuf *loading_icon;
55         EvDocument *document;
56         EvPageCache *page_cache;
57
58         gint n_pages, pages_done;
59
60         int rotation;
61
62         /* Visible pages */
63         gint start_page, end_page;
64 };
65
66 enum {
67         COLUMN_PAGE_STRING,
68         COLUMN_PIXBUF,
69         COLUMN_THUMBNAIL_SET,
70         COLUMN_JOB,
71         NUM_COLUMNS
72 };
73
74 enum {
75         PROP_0,
76         PROP_WIDGET,
77 };
78
79 static void         ev_sidebar_thumbnails_clear_model      (EvSidebarThumbnails *sidebar);
80 static gboolean     ev_sidebar_thumbnails_support_document (EvSidebarPage       *sidebar_page,
81                                                             EvDocument          *document);
82 static void         ev_sidebar_thumbnails_page_iface_init  (EvSidebarPageIface  *iface);
83 static void         ev_sidebar_thumbnails_set_document     (EvSidebarPage       *sidebar_page,
84                                                             EvDocument          *document);
85 static const gchar* ev_sidebar_thumbnails_get_label        (EvSidebarPage       *sidebar_page);
86 static void         thumbnail_job_completed_callback       (EvJobThumbnail      *job,
87                                                             EvSidebarThumbnails *sidebar_thumbnails);
88
89 G_DEFINE_TYPE_EXTENDED (EvSidebarThumbnails, 
90                         ev_sidebar_thumbnails, 
91                         GTK_TYPE_VBOX,
92                         0, 
93                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
94                                                ev_sidebar_thumbnails_page_iface_init))
95
96 #define EV_SIDEBAR_THUMBNAILS_GET_PRIVATE(object) \
97         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_THUMBNAILS, EvSidebarThumbnailsPrivate));
98
99
100 static void
101 ev_sidebar_thumbnails_dispose (GObject *object)
102 {
103         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (object);
104         
105         if (sidebar_thumbnails->priv->loading_icon) {
106                 g_object_unref (sidebar_thumbnails->priv->loading_icon);
107                 sidebar_thumbnails->priv->loading_icon = NULL;
108         }
109         
110         if (sidebar_thumbnails->priv->list_store) {
111                 ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
112                 g_object_unref (sidebar_thumbnails->priv->list_store);
113                 sidebar_thumbnails->priv->list_store = NULL;
114         }
115         
116         G_OBJECT_CLASS (ev_sidebar_thumbnails_parent_class)->dispose (object);
117 }
118
119 static void
120 ev_sidebar_thumbnails_get_property (GObject    *object,
121                                     guint       prop_id,
122                                     GValue     *value,
123                                     GParamSpec *pspec)
124 {
125         EvSidebarThumbnails *sidebar = EV_SIDEBAR_THUMBNAILS (object);
126
127         switch (prop_id)
128         {
129         case PROP_WIDGET:
130                 if (sidebar->priv->tree_view)
131                         g_value_set_object (value, sidebar->priv->tree_view);
132                 else
133                         g_value_set_object (value, sidebar->priv->icon_view);
134                 break;
135         default:
136                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137                 break;
138         }
139 }
140
141 static void
142 ev_sidebar_thumbnails_class_init (EvSidebarThumbnailsClass *ev_sidebar_thumbnails_class)
143 {
144         GObjectClass *g_object_class;
145         GtkObjectClass *gtk_object_class;
146
147         g_object_class = G_OBJECT_CLASS (ev_sidebar_thumbnails_class);
148         gtk_object_class = GTK_OBJECT_CLASS (ev_sidebar_thumbnails_class);
149
150         g_object_class->dispose = ev_sidebar_thumbnails_dispose;
151         g_object_class->get_property = ev_sidebar_thumbnails_get_property;
152
153         g_object_class_override_property (g_object_class,
154                                           PROP_WIDGET,
155                                           "main-widget");
156
157         g_type_class_add_private (g_object_class, sizeof (EvSidebarThumbnailsPrivate));
158 }
159
160 GtkWidget *
161 ev_sidebar_thumbnails_new (void)
162 {
163         GtkWidget *ev_sidebar_thumbnails;
164
165         ev_sidebar_thumbnails = g_object_new (EV_TYPE_SIDEBAR_THUMBNAILS, NULL);
166
167         return ev_sidebar_thumbnails;
168 }
169
170 static void
171 clear_range (EvSidebarThumbnails *sidebar_thumbnails,
172              gint                 start_page,
173              gint                 end_page)
174 {
175         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
176         GtkTreePath *path;
177         GtkTreeIter iter;
178         gboolean result;
179
180         g_assert (start_page <= end_page);
181
182         path = gtk_tree_path_new_from_indices (start_page, -1);
183         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
184              result && start_page <= end_page;
185              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), start_page ++) {
186                 EvJobThumbnail *job;
187
188                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store),
189                                     &iter,
190                                     COLUMN_JOB, &job,
191                                     -1);
192
193                 if (job) {
194                         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, sidebar_thumbnails);
195                         ev_job_queue_remove_job (EV_JOB (job));
196                         g_object_unref (job);
197                 }
198
199                 gtk_list_store_set (priv->list_store, &iter,
200                                     COLUMN_JOB, NULL,
201                                     COLUMN_THUMBNAIL_SET, FALSE,
202                                     COLUMN_PIXBUF, priv->loading_icon,
203                                     -1);
204         }
205         gtk_tree_path_free (path);
206 }
207
208 static void
209 add_range (EvSidebarThumbnails *sidebar_thumbnails,
210            gint                 start_page,
211            gint                 end_page)
212 {
213         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
214         GtkTreePath *path;
215         GtkTreeIter iter;
216         gboolean result;
217         gint page = start_page;
218
219         g_assert (start_page <= end_page);
220
221         path = gtk_tree_path_new_from_indices (start_page, -1);
222         for (result = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path);
223              result && page <= end_page;
224              result = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->list_store), &iter), page ++) {
225                 EvJobThumbnail *job;
226                 gboolean thumbnail_set;
227
228                 gtk_tree_model_get (GTK_TREE_MODEL (priv->list_store), &iter,
229                                     COLUMN_JOB, &job,
230                                     COLUMN_THUMBNAIL_SET, &thumbnail_set,
231                                     -1);
232
233                 if (job == NULL && !thumbnail_set) {
234                         /* FIXME: Need rotation */
235                         job = (EvJobThumbnail *)ev_job_thumbnail_new (priv->document, page, priv->rotation, THUMBNAIL_WIDTH);
236                         ev_job_queue_add_job (EV_JOB (job), EV_JOB_PRIORITY_HIGH);
237                         g_object_set_data_full (G_OBJECT (job), "tree_iter",
238                                                 gtk_tree_iter_copy (&iter),
239                                                 (GDestroyNotify) gtk_tree_iter_free);
240                         g_signal_connect (job, "finished",
241                                           G_CALLBACK (thumbnail_job_completed_callback),
242                                           sidebar_thumbnails);
243                         gtk_list_store_set (priv->list_store, &iter,
244                                             COLUMN_JOB, job,
245                                             -1);
246                         /* The queue and the list own a ref to the job now */
247                         g_object_unref (job);
248                 } else if (job) {
249                         g_object_unref (job);
250                 }
251         }
252         gtk_tree_path_free (path);
253 }
254
255 /* This modifies start */
256 static void
257 update_visible_range (EvSidebarThumbnails *sidebar_thumbnails,
258                       gint                 start_page,
259                       gint                 end_page)
260 {
261         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
262         int old_start_page, old_end_page;
263
264         old_start_page = priv->start_page;
265         old_end_page = priv->end_page;
266
267         if (start_page == old_start_page &&
268             end_page == old_end_page)
269                 return;
270
271         /* Clear the areas we no longer display */
272         if (old_start_page < start_page)
273                 clear_range (sidebar_thumbnails, old_start_page, MIN (start_page - 1, old_end_page));
274
275         if (old_end_page > end_page)
276                 clear_range (sidebar_thumbnails, MAX (end_page + 1, old_start_page), old_end_page);
277
278         add_range (sidebar_thumbnails, start_page, end_page);
279         
280         priv->start_page = start_page;
281         priv->end_page = end_page;
282 }
283
284 static void
285 adjustment_changed_cb (EvSidebarThumbnails *sidebar_thumbnails)
286 {
287         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
288         GtkTreePath *path = NULL;
289         GtkTreePath *path2 = NULL;
290         gint wy1;
291         gint wy2;
292
293         if (priv->tree_view) {
294                 if (! GTK_WIDGET_REALIZED (priv->tree_view))
295                         return;
296
297                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
298                                                      0, (int) priv->vadjustment->value,
299                                                      NULL, &wy1);
300                 gtk_tree_view_tree_to_widget_coords (GTK_TREE_VIEW (priv->tree_view),
301                                                      0, (int) (priv->vadjustment->value + priv->vadjustment->page_size),
302                                                      NULL, &wy2);
303                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
304                                                1, wy1 + 1, &path,
305                                                NULL, NULL, NULL);
306                 gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
307                                                1, wy2 -1, &path2,
308                                                NULL, NULL, NULL);
309         } else if (priv->icon_view) {
310 #ifdef HAVE_GTK_ICON_VIEW_GET_VISIBLE_RANGE
311                 if (! GTK_WIDGET_REALIZED (priv->icon_view))
312                         return;
313                 if (! gtk_icon_view_get_visible_range (GTK_ICON_VIEW (priv->icon_view), &path, &path2))
314                         return;
315 #else
316                 g_assert_not_reached ();
317 #endif
318         } else {
319                 return;
320         }
321
322         if (path == NULL)
323                 path = gtk_tree_path_new_first ();
324         if (path2 == NULL)
325                 path2 = gtk_tree_path_new_from_indices (priv->n_pages,
326                                                         -1);
327         update_visible_range (sidebar_thumbnails,
328                               gtk_tree_path_get_indices (path)[0],
329                               gtk_tree_path_get_indices (path2)[0]);
330
331         gtk_tree_path_free (path);
332         gtk_tree_path_free (path2);
333 }
334
335 static void
336 ev_sidebar_thumbnails_fill_model (EvSidebarThumbnails *sidebar_thumbnails)
337 {
338         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
339         GtkTreeIter iter;
340         int i;
341
342         for (i = 0; i < sidebar_thumbnails->priv->n_pages; i++) {
343                 gchar *page_label;
344                 gchar *page_string;
345
346                 page_label = ev_page_cache_get_page_label (priv->page_cache, i);
347                 page_string = g_markup_printf_escaped ("<i>%s</i>", page_label);
348
349                 gtk_list_store_append (priv->list_store, &iter);
350                 gtk_list_store_set (priv->list_store, &iter,
351                                     COLUMN_PAGE_STRING, page_string,
352                                     COLUMN_PIXBUF, priv->loading_icon,
353                                     COLUMN_THUMBNAIL_SET, FALSE,
354                                     -1);
355                 g_free (page_label);
356                 g_free (page_string);
357         }
358 }
359
360
361 static void
362 ev_sidebar_thumbnails_set_loading_icon (EvSidebarThumbnails *sidebar_thumbnails)
363 {
364         gint width = THUMBNAIL_WIDTH;
365         gint height = THUMBNAIL_WIDTH;
366
367         if (sidebar_thumbnails->priv->loading_icon)
368                 g_object_unref (sidebar_thumbnails->priv->loading_icon);
369
370         if (sidebar_thumbnails->priv->document) {
371                 /* We get the dimensions of the first doc so that we can make a blank
372                  * icon.  */
373                 ev_document_doc_mutex_lock ();
374                 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (sidebar_thumbnails->priv->document),
375                                                        0, THUMBNAIL_WIDTH, &width, &height);
376                 ev_document_doc_mutex_unlock ();
377                 sidebar_thumbnails->priv->loading_icon =
378                         ev_document_misc_get_thumbnail_frame (width, height, sidebar_thumbnails->priv->rotation, NULL);
379         } else {
380                 sidebar_thumbnails->priv->loading_icon = NULL;
381         }
382
383 }
384 void
385 ev_sidebar_thumbnails_refresh (EvSidebarThumbnails *sidebar_thumbnails,
386
387                                int                  rotation)
388 {
389         sidebar_thumbnails->priv->rotation = rotation;
390         ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
391
392         if (sidebar_thumbnails->priv->document == NULL)
393                 return;
394
395         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
396         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
397
398         /* Trigger a redraw */
399         sidebar_thumbnails->priv->start_page = 0;
400         sidebar_thumbnails->priv->end_page = 0;
401         adjustment_changed_cb (sidebar_thumbnails);
402 }
403
404 static void
405 ev_sidebar_tree_selection_changed (GtkTreeSelection *selection,
406                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
407 {
408         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
409         GtkTreePath *path;
410         GtkTreeIter iter;
411         int page;
412
413         if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
414                 return;
415
416         path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store),
417                                         &iter);
418         page = gtk_tree_path_get_indices (path)[0];
419         gtk_tree_path_free (path);
420
421         ev_page_cache_set_current_page (priv->page_cache, page);
422 }
423
424 static void
425 ev_sidebar_icon_selection_changed (GtkIconView         *icon_view,
426                                    EvSidebarThumbnails *ev_sidebar_thumbnails)
427 {
428         EvSidebarThumbnailsPrivate *priv = ev_sidebar_thumbnails->priv;
429         GtkTreePath *path;
430         GList *selected;
431         int page;
432
433         selected = gtk_icon_view_get_selected_items (icon_view);
434         if (selected == NULL)
435                 return;
436
437         /* We don't handle or expect multiple selection. */
438         g_assert (selected->next == NULL);
439
440         path = selected->data;
441         page = gtk_tree_path_get_indices (path)[0];
442
443         gtk_tree_path_free (path);
444         g_list_free (selected);
445
446         ev_page_cache_set_current_page (priv->page_cache, page);
447 }
448
449 static void
450 ev_sidebar_init_tree_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
451 {
452         EvSidebarThumbnailsPrivate *priv;
453         GtkTreeSelection *selection;
454         GtkCellRenderer *renderer;
455
456         priv = ev_sidebar_thumbnails->priv;
457         priv->tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
458
459         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
460         g_signal_connect (selection, "changed",
461                           G_CALLBACK (ev_sidebar_tree_selection_changed), ev_sidebar_thumbnails);
462         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
463         renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
464                                  "xpad", 2,
465                                  "ypad", 2,
466                                  NULL);
467         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
468                                                      NULL, renderer,
469                                                      "pixbuf", 1,
470                                                      NULL);
471         gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (priv->tree_view), -1,
472                                                      NULL, gtk_cell_renderer_text_new (),
473                                                      "markup", 0, NULL);
474         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->tree_view);
475         gtk_widget_show (priv->tree_view);
476 }
477
478 static void
479 ev_sidebar_init_icon_view (EvSidebarThumbnails *ev_sidebar_thumbnails)
480 {
481         EvSidebarThumbnailsPrivate *priv;
482
483         priv = ev_sidebar_thumbnails->priv;
484
485         priv->icon_view = gtk_icon_view_new_with_model (GTK_TREE_MODEL (priv->list_store));
486         gtk_icon_view_set_markup_column (GTK_ICON_VIEW (priv->icon_view), 0);
487         gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (priv->icon_view), 1);
488         g_signal_connect (priv->icon_view, "selection-changed",
489                           G_CALLBACK (ev_sidebar_icon_selection_changed), ev_sidebar_thumbnails);
490
491         gtk_container_add (GTK_CONTAINER (priv->swindow), priv->icon_view);
492         gtk_widget_show (priv->icon_view);
493 }
494
495 static gboolean
496 ev_sidebar_thumbnails_use_icon_view (EvSidebarThumbnails *sidebar_thumbnails)
497 {
498 #ifdef HAVE_GTK_ICON_VIEW_GET_VISIBLE_RANGE
499         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
500         if (ev_page_cache_get_n_pages (priv->page_cache) > MAX_ICON_VIEW_PAGE_COUNT)
501                 return FALSE;
502         return TRUE;
503 #else
504         return FALSE;
505 #endif
506 }
507
508 static void
509 ev_sidebar_thumbnails_init (EvSidebarThumbnails *ev_sidebar_thumbnails)
510 {
511         EvSidebarThumbnailsPrivate *priv;
512
513         priv = ev_sidebar_thumbnails->priv = EV_SIDEBAR_THUMBNAILS_GET_PRIVATE (ev_sidebar_thumbnails);
514
515         priv->list_store = gtk_list_store_new (NUM_COLUMNS,
516                                                G_TYPE_STRING,
517                                                GDK_TYPE_PIXBUF,
518                                                G_TYPE_BOOLEAN,
519                                                EV_TYPE_JOB_THUMBNAIL);
520
521         priv->swindow = gtk_scrolled_window_new (NULL, NULL);
522         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->swindow),
523                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
524         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->swindow),
525                                              GTK_SHADOW_IN);
526         priv->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->swindow));
527         g_signal_connect_data (G_OBJECT (priv->vadjustment), "value-changed",
528                                G_CALLBACK (adjustment_changed_cb),
529                                ev_sidebar_thumbnails, NULL,
530                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
531         g_signal_connect_swapped (G_OBJECT (priv->swindow), "size-allocate",
532                                   G_CALLBACK (adjustment_changed_cb),
533                                   ev_sidebar_thumbnails);
534         gtk_box_pack_start (GTK_BOX (ev_sidebar_thumbnails), priv->swindow, TRUE, TRUE, 0);
535
536         /* Put it all together */
537         gtk_widget_show_all (priv->swindow);
538 }
539
540 static void
541 page_changed_cb (EvPageCache         *page_cache,
542                  int                  page,
543                  EvSidebarThumbnails *sidebar)
544 {
545         GtkTreeView *tree_view;
546         GtkTreePath *path;
547
548         path = gtk_tree_path_new_from_indices (page, -1);
549
550         if (sidebar->priv->tree_view) {
551                 tree_view = GTK_TREE_VIEW (sidebar->priv->tree_view);
552                 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
553                 gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
554         } else if (sidebar->priv->icon_view) {
555                 /* Guard against gtk-2.6 */
556 #ifdef HAVE_GTK_ICON_VIEW_GET_VISIBLE_RANGE 
557                 gtk_icon_view_select_path (GTK_ICON_VIEW (sidebar->priv->icon_view), path);
558                 gtk_icon_view_set_cursor (GTK_ICON_VIEW (sidebar->priv->icon_view), path, NULL, FALSE);
559 #endif
560         }
561
562         gtk_tree_path_free (path);
563 }
564
565 static void
566 thumbnail_job_completed_callback (EvJobThumbnail      *job,
567                                   EvSidebarThumbnails *sidebar_thumbnails)
568 {
569         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
570         GtkTreeIter *iter;
571
572         iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
573         gtk_list_store_set (priv->list_store,
574                             iter,
575                             COLUMN_PIXBUF, job->thumbnail,
576                             COLUMN_THUMBNAIL_SET, TRUE,
577                             COLUMN_JOB, NULL,
578                             -1);
579 }
580
581 static void
582 ev_sidebar_thumbnails_set_document (EvSidebarPage       *sidebar_page,
583                                     EvDocument          *document)
584 {
585         EvSidebarThumbnails *sidebar_thumbnails = EV_SIDEBAR_THUMBNAILS (sidebar_page);
586
587         EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
588
589         g_return_if_fail (EV_IS_DOCUMENT_THUMBNAILS (document));
590
591         priv->page_cache = ev_page_cache_get (document);
592         priv->document = document;
593         priv->n_pages = ev_page_cache_get_n_pages (priv->page_cache);
594
595         ev_sidebar_thumbnails_set_loading_icon (sidebar_thumbnails);
596
597         ev_sidebar_thumbnails_clear_model (sidebar_thumbnails);
598         ev_sidebar_thumbnails_fill_model (sidebar_thumbnails);
599
600         /* Create the view widget, and remove the old one, if needed */
601         if (ev_sidebar_thumbnails_use_icon_view (sidebar_thumbnails)) {
602                 if (priv->tree_view) {
603                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->tree_view);
604                         priv->tree_view = NULL;
605                 }
606
607                 if (! priv->icon_view) {
608                         ev_sidebar_init_icon_view (sidebar_thumbnails);
609                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
610                 }
611         } else {
612                 if (priv->icon_view) {
613                         gtk_container_remove (GTK_CONTAINER (priv->swindow), priv->icon_view);
614                         priv->icon_view = NULL;
615                 }
616
617                 if (! priv->tree_view) {
618                         ev_sidebar_init_tree_view (sidebar_thumbnails);
619                         g_object_notify (G_OBJECT (sidebar_thumbnails), "main_widget");
620                 }
621         }
622
623         /* Connect to the signal and trigger a fake callback */
624         g_signal_connect (priv->page_cache, "page-changed", G_CALLBACK (page_changed_cb), sidebar_thumbnails);
625         sidebar_thumbnails->priv->start_page = 0;
626         sidebar_thumbnails->priv->end_page = 0;
627         adjustment_changed_cb (sidebar_thumbnails);
628 }
629
630 static gboolean
631 ev_sidebar_thumbnails_clear_job (GtkTreeModel *model,                                             
632                                  GtkTreePath *path,                                                                                      
633                                  GtkTreeIter *iter,                                                                                                                                   
634                                  gpointer data)
635 {
636     EvJob *job;
637     
638     gtk_tree_model_get (model, iter, COLUMN_JOB, &job, -1);
639     
640     if (job != NULL) {
641         ev_job_queue_remove_job (job);
642         g_signal_handlers_disconnect_by_func (job, thumbnail_job_completed_callback, data);
643         g_object_unref (job);
644     }
645
646     return FALSE;    
647 }
648
649 static void 
650 ev_sidebar_thumbnails_clear_model (EvSidebarThumbnails *sidebar_thumbnails)
651 {
652     EvSidebarThumbnailsPrivate *priv = sidebar_thumbnails->priv;
653     
654     gtk_tree_model_foreach (GTK_TREE_MODEL (priv->list_store), ev_sidebar_thumbnails_clear_job, sidebar_thumbnails);
655     gtk_list_store_clear (priv->list_store);
656 }
657
658 static gboolean
659 ev_sidebar_thumbnails_support_document (EvSidebarPage   *sidebar_page,
660                                         EvDocument *document)
661 {
662         return (EV_IS_DOCUMENT_THUMBNAILS (document) &&
663                     (ev_document_get_n_pages (document) > 1));
664 }
665
666 static const gchar*
667 ev_sidebar_thumbnails_get_label (EvSidebarPage *sidebar_page)
668 {
669     return _("Thumbnails");
670 }
671
672 static void
673 ev_sidebar_thumbnails_page_iface_init (EvSidebarPageIface *iface)
674 {
675         iface->support_document = ev_sidebar_thumbnails_support_document;
676         iface->set_document = ev_sidebar_thumbnails_set_document;
677         iface->get_label = ev_sidebar_thumbnails_get_label;
678 }
679