]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-attachments.c
60c7ae0aca7de6977c7da98431412a00dac658c9
[evince.git] / shell / ev-sidebar-attachments.c
1 /* ev-sidebar-attachments.c
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2006 Carlos Garcia Campos
5  *
6  * Author:
7  *   Carlos Garcia Campos <carlosgc@gnome.org>
8  *
9  * Evince is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Evince is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <string.h>
29
30 #include <glib/gi18n.h>
31 #include <glib/gstdio.h>
32 #include <gtk/gtk.h>
33
34 #include "ev-document-attachments.h"
35 #include "ev-jobs.h"
36 #include "ev-job-scheduler.h"
37 #include "ev-file-helpers.h"
38 #include "ev-sidebar-attachments.h"
39 #include "ev-sidebar-page.h"
40
41 enum {
42         COLUMN_ICON,
43         COLUMN_NAME,
44         COLUMN_DESCRIPTION,
45         COLUMN_ATTACHMENT,
46         N_COLS
47 };
48
49 enum {
50         PROP_0,
51         PROP_WIDGET,
52 };
53
54 enum {
55         SIGNAL_POPUP_MENU,
56         N_SIGNALS
57 };
58
59 static guint signals[N_SIGNALS];
60
61 struct _EvSidebarAttachmentsPrivate {
62         GtkWidget      *icon_view;
63         GtkListStore   *model;
64
65         /* Icons */
66         GtkIconTheme   *icon_theme;
67         GHashTable     *icon_cache;
68 };
69
70 static void ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface);
71
72 G_DEFINE_TYPE_EXTENDED (EvSidebarAttachments,
73                         ev_sidebar_attachments,
74                         GTK_TYPE_VBOX,
75                         0, 
76                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
77                                                ev_sidebar_attachments_page_iface_init))
78
79 #define EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE(object) \
80                 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_ATTACHMENTS, EvSidebarAttachmentsPrivate))
81
82 /* Icon cache */
83 static void
84 ev_sidebar_attachments_icon_cache_add (EvSidebarAttachments *ev_attachbar,
85                                        const gchar          *mime_type,
86                                        const GdkPixbuf      *pixbuf)
87 {
88         g_assert (mime_type != NULL);
89         g_assert (GDK_IS_PIXBUF (pixbuf));
90
91         g_hash_table_insert (ev_attachbar->priv->icon_cache,
92                              (gpointer)g_strdup (mime_type),
93                              (gpointer)pixbuf);
94                              
95 }
96
97 static GdkPixbuf *
98 icon_theme_get_pixbuf_from_mime_type (GtkIconTheme *icon_theme,
99                                       const gchar  *mime_type)
100 {
101         const char *separator;
102         GString *icon_name;
103         GdkPixbuf *pixbuf;
104
105         separator = strchr (mime_type, '/');
106         if (!separator)
107                 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
108
109         icon_name = g_string_new ("gnome-mime-");
110         g_string_append_len (icon_name, mime_type, separator - mime_type);
111         g_string_append_c (icon_name, '-');
112         g_string_append (icon_name, separator + 1);
113         pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
114         g_string_free (icon_name, TRUE);
115         if (pixbuf)
116                 return pixbuf;
117         
118         icon_name = g_string_new ("gnome-mime-");
119         g_string_append_len (icon_name, mime_type, separator - mime_type);
120         pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
121         g_string_free (icon_name, TRUE);
122         
123         return pixbuf;
124 }
125
126 static GdkPixbuf *
127 ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
128                                        const gchar          *mime_type)
129 {
130         GdkPixbuf *pixbuf = NULL;
131         
132         g_assert (mime_type != NULL);
133
134         pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
135                                       mime_type);
136
137         if (GDK_IS_PIXBUF (pixbuf))
138                 return pixbuf;
139
140         pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
141                                                        mime_type);
142
143         if (GDK_IS_PIXBUF (pixbuf))
144                 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
145                                                        mime_type,
146                                                        pixbuf);
147
148         return pixbuf;
149 }
150
151 static gboolean
152 icon_cache_update_icon (gchar                *key,
153                         GdkPixbuf            *value,
154                         EvSidebarAttachments *ev_attachbar)
155 {
156         GdkPixbuf *pixbuf = NULL;
157
158         pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
159                                                        key);
160
161         ev_sidebar_attachments_icon_cache_add (ev_attachbar,
162                                                key,
163                                                pixbuf);
164         
165         return FALSE;
166 }
167
168 static void
169 ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
170 {
171         g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
172                                      (GHRFunc) icon_cache_update_icon,
173                                      ev_attachbar);
174 }
175
176 static EvAttachment *
177 ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
178                                               gint                  x,
179                                               gint                  y)
180 {
181         GtkTreePath  *path = NULL;
182         GtkTreeIter   iter;
183         EvAttachment *attachment = NULL;
184
185         path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
186                                               x, y);
187         if (!path) {
188                 return NULL;
189         }
190
191         gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
192                                  &iter, path);
193         gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
194                             COLUMN_ATTACHMENT, &attachment,
195                             -1);
196
197         gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
198                                    path);
199         
200         gtk_tree_path_free (path);
201
202         return attachment;
203 }
204
205 static gboolean
206 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
207                                         gint                  x,
208                                         gint                  y)
209 {
210         GtkIconView *icon_view;
211         GtkTreePath *path;
212         GList       *selected = NULL, *l;
213         GList       *attach_list = NULL;
214
215         icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
216         
217         path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
218         if (!path)
219                 return FALSE;
220
221         if (!gtk_icon_view_path_is_selected (icon_view, path)) {
222                 gtk_icon_view_unselect_all (icon_view);
223                 gtk_icon_view_select_path (icon_view, path);
224         }
225
226         gtk_tree_path_free (path);
227         
228         selected = gtk_icon_view_get_selected_items (icon_view);
229         if (!selected)
230                 return FALSE;
231
232         for (l = selected; l && l->data; l = g_list_next (l)) {
233                 GtkTreeIter   iter;
234                 EvAttachment *attachment = NULL;
235
236                 path = (GtkTreePath *) l->data;
237
238                 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
239                                          &iter, path);
240                 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
241                                     COLUMN_ATTACHMENT, &attachment,
242                                     -1);
243
244                 if (attachment)
245                         attach_list = g_list_prepend (attach_list, attachment);
246
247                 gtk_tree_path_free (path);
248         }
249
250         g_list_free (selected);
251
252         if (!attach_list)
253                 return FALSE;
254
255         g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
256
257         return TRUE;
258 }
259
260 static gboolean
261 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
262 {
263         EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
264         gint                  x, y;
265
266         gtk_widget_get_pointer (widget, &x, &y);
267
268         return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
269 }
270
271 static gboolean
272 ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
273                                      GdkEventButton       *event,
274                                      GtkWidget            *icon_view)
275 {
276         if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
277                 gtk_widget_grab_focus (icon_view);
278         }
279         
280         if (event->button == 2)
281                 return FALSE;
282
283         switch (event->button) {
284                 case 1:
285                         if (event->type == GDK_2BUTTON_PRESS) {
286                                 GError *error = NULL;
287                                 EvAttachment *attachment;
288                                 
289                                 attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
290                                                                                            event->x,
291                                                                                            event->y);
292                                 if (!attachment)
293                                         return FALSE;
294                                 
295                                 ev_attachment_open (attachment,
296                                                     gtk_widget_get_screen (GTK_WIDGET (ev_attachbar)),
297                                                     event->time,
298                                                     &error);
299                                 
300                                 if (error) {
301                                         g_warning ("%s", error->message);
302                                         g_error_free (error);
303                                 }
304                                 
305                                 g_object_unref (attachment);
306                                 
307                                 return TRUE;
308                         }
309                         break;
310                 case 3: 
311                         return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
312         }
313
314         return FALSE;
315 }
316
317 static void
318 ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
319                                      gpointer              user_data)
320 {
321         GtkTreeIter iter;
322         gboolean    valid;
323
324         ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
325         
326         valid = gtk_tree_model_get_iter_first (
327                 GTK_TREE_MODEL (ev_attachbar->priv->model),
328                 &iter);
329
330         while (valid) {
331                 EvAttachment *attachment = NULL;
332                 GdkPixbuf    *pixbuf = NULL;
333                 const gchar  *mime_type;
334
335                 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
336                                     COLUMN_ATTACHMENT, &attachment,
337                                     -1);
338
339                 mime_type = ev_attachment_get_mime_type (attachment);
340
341                 if (attachment)
342                         g_object_unref (attachment);
343
344                 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
345                                                                 mime_type);
346
347                 gtk_list_store_set (ev_attachbar->priv->model, &iter,
348                                     COLUMN_ICON, pixbuf,
349                                     -1);
350
351                 valid = gtk_tree_model_iter_next (
352                         GTK_TREE_MODEL (ev_attachbar->priv->model),
353                         &iter);
354         }
355 }
356
357 static void
358 ev_sidebar_attachments_screen_changed (GtkWidget *widget,
359                                        GdkScreen *old_screen)
360 {
361         EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
362         GdkScreen            *screen;
363
364         if (!ev_attachbar->priv->icon_theme)
365                 return;
366         
367         screen = gtk_widget_get_screen (widget);
368         if (screen == old_screen)
369                 return;
370
371         if (old_screen) {
372                 g_signal_handlers_disconnect_by_func (
373                         gtk_icon_theme_get_for_screen (old_screen),
374                         G_CALLBACK (ev_sidebar_attachments_update_icons),
375                         ev_attachbar);
376         }
377
378         ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
379         g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
380                                   "changed",
381                                   G_CALLBACK (ev_sidebar_attachments_update_icons),
382                                   (gpointer) ev_attachbar);
383
384         if (GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed) {
385                 GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed (widget, old_screen);
386         }
387 }
388
389 static void
390 ev_sidebar_attachments_drag_data_get (GtkWidget        *widget,
391                                       GdkDragContext   *drag_context,
392                                       GtkSelectionData *data,
393                                       guint             info,
394                                       guint             time,
395                                       gpointer          user_data)
396 {
397         EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (user_data);
398         GList                *selected = NULL, *l;
399         GPtrArray            *uris;
400         char                **uri_list;
401
402         selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
403         if (!selected)
404                 return;
405
406         uris = g_ptr_array_new ();
407         
408         for (l = selected; l && l->data; l = g_list_next (l)) {
409                 EvAttachment *attachment;
410                 GtkTreePath  *path;
411                 GtkTreeIter   iter;
412                 GFile        *file;
413                 gchar        *template;
414                 GError       *error = NULL;
415                 
416                 path = (GtkTreePath *) l->data;
417
418                 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
419                                          &iter, path);
420                 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
421                                     COLUMN_ATTACHMENT, &attachment,
422                                     -1);
423
424                 /* FIXMEchpe: convert to filename encoding first! */
425                 template = g_strdup_printf ("%s.XXXXXX", ev_attachment_get_name (attachment));
426                 file = ev_mkstemp_file (template, &error);
427                 g_free (template);
428                 
429                 if (file != NULL && ev_attachment_save (attachment, file, &error)) {
430                         gchar *uri;
431
432                         uri = g_file_get_uri (file);
433                         g_ptr_array_add (uris, uri);
434                 }
435         
436                 if (error) {
437                         g_warning ("%s", error->message);
438                         g_error_free (error);
439                 }
440
441                 gtk_tree_path_free (path);
442                 g_object_unref (file);
443                 g_object_unref (attachment);
444         }
445
446         g_ptr_array_add (uris, NULL); /* NULL-terminate */
447         uri_list = (char **) g_ptr_array_free (uris, FALSE);
448         gtk_selection_data_set_uris (data, uri_list);
449         g_strfreev (uri_list);
450
451         g_list_free (selected);
452 }
453
454 static void
455 ev_sidebar_attachments_get_property (GObject    *object,
456                                      guint       prop_id,
457                                      GValue     *value,
458                                      GParamSpec *pspec)
459 {
460         EvSidebarAttachments *ev_sidebar_attachments;
461   
462         ev_sidebar_attachments = EV_SIDEBAR_ATTACHMENTS (object);
463
464         switch (prop_id) {
465                 case PROP_WIDGET:
466                         g_value_set_object (value, ev_sidebar_attachments->priv->icon_view);
467                         break;
468                 default:
469                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
470                         break;
471         }
472 }
473
474 static void
475 ev_sidebar_attachments_destroy (GtkObject *object)
476 {
477         EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (object);
478
479         if (ev_attachbar->priv->icon_theme) {
480                 g_signal_handlers_disconnect_by_func (
481                         ev_attachbar->priv->icon_theme, 
482                         G_CALLBACK (ev_sidebar_attachments_update_icons),
483                         ev_attachbar);
484                 ev_attachbar->priv->icon_theme = NULL;
485         }
486         
487         if (ev_attachbar->priv->model) {
488                 g_object_unref (ev_attachbar->priv->model);
489                 ev_attachbar->priv->model = NULL;
490         }
491
492         if (ev_attachbar->priv->icon_cache) {
493                 g_hash_table_destroy (ev_attachbar->priv->icon_cache);
494                 ev_attachbar->priv->icon_cache = NULL;
495         }
496
497         (* GTK_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->destroy) (object);
498 }
499
500 static void
501 ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class)
502 {
503         GObjectClass   *g_object_class;
504         GtkObjectClass *gtk_object_class;
505         GtkWidgetClass *gtk_widget_class;
506
507         g_object_class = G_OBJECT_CLASS (ev_attachbar_class);
508         gtk_object_class = GTK_OBJECT_CLASS (ev_attachbar_class);
509         gtk_widget_class = GTK_WIDGET_CLASS (ev_attachbar_class);
510
511         g_object_class->get_property = ev_sidebar_attachments_get_property;
512         gtk_object_class->destroy = ev_sidebar_attachments_destroy;
513         gtk_widget_class->popup_menu = ev_sidebar_attachments_popup_menu;
514         gtk_widget_class->screen_changed = ev_sidebar_attachments_screen_changed;
515
516         g_type_class_add_private (g_object_class, sizeof (EvSidebarAttachmentsPrivate));
517
518         /* Signals */
519         signals[SIGNAL_POPUP_MENU] =
520                 g_signal_new ("popup",
521                               G_TYPE_FROM_CLASS (g_object_class),
522                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
523                               G_STRUCT_OFFSET (EvSidebarAttachmentsClass, popup_menu),
524                               NULL, NULL,
525                               g_cclosure_marshal_VOID__POINTER,
526                               G_TYPE_NONE, 1,
527                               G_TYPE_POINTER);
528
529         g_object_class_override_property (g_object_class,
530                                           PROP_WIDGET,
531                                           "main-widget");
532 }
533
534 static void
535 ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
536 {
537         GtkWidget *swindow;
538         
539         ev_attachbar->priv = EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE (ev_attachbar);
540
541         swindow = gtk_scrolled_window_new (NULL, NULL);
542         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
543                                         GTK_POLICY_NEVER,
544                                         GTK_POLICY_AUTOMATIC);
545         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
546                                              GTK_SHADOW_IN);
547         /* Data Model */
548         ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
549                                                         GDK_TYPE_PIXBUF, 
550                                                         G_TYPE_STRING,  
551                                                         G_TYPE_STRING,
552                                                         EV_TYPE_ATTACHMENT);
553
554         /* Icon View */
555         ev_attachbar->priv->icon_view =
556                 gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
557         gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
558                                           GTK_SELECTION_MULTIPLE);
559         gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
560         g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
561                       "text-column", COLUMN_NAME,
562                       "pixbuf-column", COLUMN_ICON,
563                       NULL);
564         g_signal_connect_swapped (ev_attachbar->priv->icon_view,
565                                   "button-press-event",
566                                   G_CALLBACK (ev_sidebar_attachments_button_press),
567                                   (gpointer) ev_attachbar);
568
569         gtk_container_add (GTK_CONTAINER (swindow),
570                            ev_attachbar->priv->icon_view);
571
572         gtk_container_add (GTK_CONTAINER (ev_attachbar),
573                            swindow);
574         gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
575
576         /* Icon Theme */
577         ev_attachbar->priv->icon_theme = NULL;
578
579         /* Icon Cache */
580         ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
581                                                                 g_str_equal,
582                                                                 g_free,
583                                                                 g_object_unref);
584
585         /* Drag and Drop */
586         gtk_icon_view_enable_model_drag_source (
587                 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
588                 GDK_BUTTON1_MASK,
589                 NULL, 0,
590                 GDK_ACTION_COPY);
591         gtk_drag_source_add_uri_targets (ev_attachbar->priv->icon_view);
592
593         g_signal_connect (ev_attachbar->priv->icon_view,
594                           "drag-data-get",
595                           G_CALLBACK (ev_sidebar_attachments_drag_data_get),
596                           (gpointer) ev_attachbar);     
597 }
598
599 GtkWidget *
600 ev_sidebar_attachments_new (void)
601 {
602         GtkWidget *ev_attachbar;
603
604         ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
605
606         return ev_attachbar;
607 }
608
609 static void
610 job_finished_callback (EvJobAttachments     *job,
611                        EvSidebarAttachments *ev_attachbar)
612 {
613         GList *l;
614         
615         for (l = job->attachments; l && l->data; l = g_list_next (l)) {
616                 EvAttachment *attachment;
617                 GtkTreeIter   iter;
618                 GdkPixbuf    *pixbuf = NULL;
619                 const gchar  *mime_type;
620
621                 attachment = EV_ATTACHMENT (l->data);
622
623                 mime_type = ev_attachment_get_mime_type (attachment);
624                 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
625                                                                 mime_type);
626
627                 gtk_list_store_append (ev_attachbar->priv->model, &iter);
628                 gtk_list_store_set (ev_attachbar->priv->model, &iter,
629                                     COLUMN_NAME, ev_attachment_get_name (attachment),
630                                     COLUMN_ICON, pixbuf,
631                                     COLUMN_ATTACHMENT, attachment, 
632                                     -1);
633         }
634
635         g_object_unref (job);
636 }
637
638
639 static void
640 ev_sidebar_attachments_document_changed_cb (EvDocumentModel      *model,
641                                             GParamSpec           *pspec,
642                                             EvSidebarAttachments *ev_attachbar)
643 {
644         EvDocument *document = ev_document_model_get_document (model);
645         EvJob *job;
646
647         if (!EV_IS_DOCUMENT_ATTACHMENTS (document))
648                 return;
649
650         if (!ev_document_attachments_has_attachments (EV_DOCUMENT_ATTACHMENTS (document)))
651                 return;
652
653         if (!ev_attachbar->priv->icon_theme) {
654                 GdkScreen *screen;
655
656                 screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
657                 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
658                 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
659                                           "changed",
660                                           G_CALLBACK (ev_sidebar_attachments_update_icons),
661                                           (gpointer) ev_attachbar);
662         }
663                 
664         gtk_list_store_clear (ev_attachbar->priv->model);
665
666         job = ev_job_attachments_new (document);
667         g_signal_connect (job, "finished",
668                           G_CALLBACK (job_finished_callback),
669                           ev_attachbar);
670         g_signal_connect (job, "cancelled",
671                           G_CALLBACK (g_object_unref),
672                           NULL);
673         /* The priority doesn't matter for this job */
674         ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
675 }
676
677 static void
678 ev_sidebar_attachments_set_model (EvSidebarPage   *page,
679                                   EvDocumentModel *model)
680 {
681         g_signal_connect (model, "notify::document",
682                           G_CALLBACK (ev_sidebar_attachments_document_changed_cb),
683                           page);
684 }
685
686 static gboolean
687 ev_sidebar_attachments_support_document (EvSidebarPage   *sidebar_page,
688                                          EvDocument      *document)
689 {
690         return (EV_IS_DOCUMENT_ATTACHMENTS (document) &&
691                 ev_document_attachments_has_attachments (EV_DOCUMENT_ATTACHMENTS (document)));
692 }
693
694 static const gchar*
695 ev_sidebar_attachments_get_label (EvSidebarPage *sidebar_page)
696 {
697         return _("Attachments");
698 }
699
700 static void
701 ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface)
702 {
703         iface->support_document = ev_sidebar_attachments_support_document;
704         iface->set_model = ev_sidebar_attachments_set_model;
705         iface->get_label = ev_sidebar_attachments_get_label;
706 }
707