]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-attachments.c
shell/ev-sidebar-links.c
[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-jobs.h"
35 #include "ev-job-scheduler.h"
36 #include "ev-file-helpers.h"
37 #include "ev-sidebar-attachments.h"
38 #include "ev-sidebar-page.h"
39
40 enum {
41         COLUMN_ICON,
42         COLUMN_NAME,
43         COLUMN_DESCRIPTION,
44         COLUMN_ATTACHMENT,
45         N_COLS
46 };
47
48 enum {
49         PROP_0,
50         PROP_WIDGET,
51 };
52
53 enum {
54         SIGNAL_POPUP_MENU,
55         N_SIGNALS
56 };
57
58 static guint signals[N_SIGNALS];
59
60 struct _EvSidebarAttachmentsPrivate {
61         GtkWidget      *icon_view;
62         GtkListStore   *model;
63
64         /* Icons */
65         GtkIconTheme   *icon_theme;
66         GHashTable     *icon_cache;
67 };
68
69 static void ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface);
70
71 G_DEFINE_TYPE_EXTENDED (EvSidebarAttachments,
72                         ev_sidebar_attachments,
73                         GTK_TYPE_VBOX,
74                         0, 
75                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE, 
76                                                ev_sidebar_attachments_page_iface_init))
77
78 #define EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE(object) \
79                 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_ATTACHMENTS, EvSidebarAttachmentsPrivate))
80
81 /* Icon cache */
82 static void
83 ev_sidebar_attachments_icon_cache_add (EvSidebarAttachments *ev_attachbar,
84                                        const gchar          *mime_type,
85                                        const GdkPixbuf      *pixbuf)
86 {
87         g_assert (mime_type != NULL);
88         g_assert (GDK_IS_PIXBUF (pixbuf));
89
90         g_hash_table_insert (ev_attachbar->priv->icon_cache,
91                              (gpointer)g_strdup (mime_type),
92                              (gpointer)pixbuf);
93                              
94 }
95
96 static GdkPixbuf *
97 icon_theme_get_pixbuf_from_mime_type (GtkIconTheme *icon_theme,
98                                       const gchar  *mime_type)
99 {
100         const char *separator;
101         GString *icon_name;
102         GdkPixbuf *pixbuf;
103
104         separator = strchr (mime_type, '/');
105         if (!separator)
106                 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
107
108         icon_name = g_string_new ("gnome-mime-");
109         g_string_append_len (icon_name, mime_type, separator - mime_type);
110         g_string_append_c (icon_name, '-');
111         g_string_append (icon_name, separator + 1);
112         pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
113         g_string_free (icon_name, TRUE);
114         if (pixbuf)
115                 return pixbuf;
116         
117         icon_name = g_string_new ("gnome-mime-");
118         g_string_append_len (icon_name, mime_type, separator - mime_type);
119         pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
120         g_string_free (icon_name, TRUE);
121         
122         return pixbuf;
123 }
124
125 static GdkPixbuf *
126 ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
127                                        const gchar          *mime_type)
128 {
129         GdkPixbuf *pixbuf = NULL;
130         
131         g_assert (mime_type != NULL);
132
133         pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
134                                       mime_type);
135
136         if (GDK_IS_PIXBUF (pixbuf))
137                 return pixbuf;
138
139         pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
140                                                        mime_type);
141
142         if (GDK_IS_PIXBUF (pixbuf))
143                 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
144                                                        mime_type,
145                                                        pixbuf);
146
147         return pixbuf;
148 }
149
150 static gboolean
151 icon_cache_update_icon (gchar                *key,
152                         GdkPixbuf            *value,
153                         EvSidebarAttachments *ev_attachbar)
154 {
155         GdkPixbuf *pixbuf = NULL;
156
157         pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
158                                                        key);
159
160         ev_sidebar_attachments_icon_cache_add (ev_attachbar,
161                                                key,
162                                                pixbuf);
163         
164         return FALSE;
165 }
166
167 static void
168 ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
169 {
170         g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
171                                      (GHRFunc) icon_cache_update_icon,
172                                      ev_attachbar);
173 }
174
175 static EvAttachment *
176 ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
177                                               gint                  x,
178                                               gint                  y)
179 {
180         GtkTreePath  *path = NULL;
181         GtkTreeIter   iter;
182         EvAttachment *attachment = NULL;
183
184         path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
185                                               x, y);
186         if (!path) {
187                 return NULL;
188         }
189
190         gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
191                                  &iter, path);
192         gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
193                             COLUMN_ATTACHMENT, &attachment,
194                             -1);
195
196         gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
197                                    path);
198         
199         gtk_tree_path_free (path);
200
201         return attachment;
202 }
203
204 static gboolean
205 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
206                                         gint                  x,
207                                         gint                  y)
208 {
209         GtkIconView *icon_view;
210         GtkTreePath *path;
211         GList       *selected = NULL, *l;
212         GList       *attach_list = NULL;
213
214         icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
215         
216         path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
217         if (!path)
218                 return FALSE;
219
220         if (!gtk_icon_view_path_is_selected (icon_view, path)) {
221                 gtk_icon_view_unselect_all (icon_view);
222                 gtk_icon_view_select_path (icon_view, path);
223         }
224
225         gtk_tree_path_free (path);
226         
227         selected = gtk_icon_view_get_selected_items (icon_view);
228         if (!selected)
229                 return FALSE;
230
231         for (l = selected; l && l->data; l = g_list_next (l)) {
232                 GtkTreeIter   iter;
233                 EvAttachment *attachment = NULL;
234
235                 path = (GtkTreePath *) l->data;
236
237                 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
238                                          &iter, path);
239                 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
240                                     COLUMN_ATTACHMENT, &attachment,
241                                     -1);
242
243                 if (attachment)
244                         attach_list = g_list_prepend (attach_list, attachment);
245
246                 gtk_tree_path_free (path);
247         }
248
249         g_list_free (selected);
250
251         if (!attach_list)
252                 return FALSE;
253
254         g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
255
256         return TRUE;
257 }
258
259 static gboolean
260 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
261 {
262         EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
263         gint                  x, y;
264
265         gtk_widget_get_pointer (widget, &x, &y);
266
267         return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
268 }
269
270 static gboolean
271 ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
272                                      GdkEventButton       *event,
273                                      GtkWidget            *icon_view)
274 {
275         if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
276                 gtk_widget_grab_focus (icon_view);
277         }
278         
279         if (event->button == 2)
280                 return FALSE;
281
282         switch (event->button) {
283                 case 1:
284                         if (event->type == GDK_2BUTTON_PRESS) {
285                                 GError *error = NULL;
286                                 EvAttachment *attachment;
287                                 
288                                 attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
289                                                                                            event->x,
290                                                                                            event->y);
291                                 if (!attachment)
292                                         return FALSE;
293                                 
294                                 ev_attachment_open (attachment,
295                                                     gtk_widget_get_screen (GTK_WIDGET (ev_attachbar)),
296                                                     event->time,
297                                                     &error);
298                                 
299                                 if (error) {
300                                         g_warning ("%s", error->message);
301                                         g_error_free (error);
302                                 }
303                                 
304                                 g_object_unref (attachment);
305                                 
306                                 return TRUE;
307                         }
308                         break;
309                 case 3: 
310                         return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
311         }
312
313         return FALSE;
314 }
315
316 static void
317 ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
318                                      gpointer              user_data)
319 {
320         GtkTreeIter iter;
321         gboolean    valid;
322
323         ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
324         
325         valid = gtk_tree_model_get_iter_first (
326                 GTK_TREE_MODEL (ev_attachbar->priv->model),
327                 &iter);
328
329         while (valid) {
330                 EvAttachment *attachment = NULL;
331                 GdkPixbuf    *pixbuf = NULL;
332                 const gchar  *mime_type;
333
334                 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
335                                     COLUMN_ATTACHMENT, &attachment,
336                                     -1);
337
338                 mime_type = ev_attachment_get_mime_type (attachment);
339
340                 if (attachment)
341                         g_object_unref (attachment);
342
343                 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
344                                                                 mime_type);
345
346                 gtk_list_store_set (ev_attachbar->priv->model, &iter,
347                                     COLUMN_ICON, pixbuf,
348                                     -1);
349
350                 valid = gtk_tree_model_iter_next (
351                         GTK_TREE_MODEL (ev_attachbar->priv->model),
352                         &iter);
353         }
354 }
355
356 static void
357 ev_sidebar_attachments_screen_changed (GtkWidget *widget,
358                                        GdkScreen *old_screen)
359 {
360         EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
361         GdkScreen            *screen;
362
363         if (!ev_attachbar->priv->icon_theme)
364                 return;
365         
366         screen = gtk_widget_get_screen (widget);
367         if (screen == old_screen)
368                 return;
369
370         if (old_screen) {
371                 g_signal_handlers_disconnect_by_func (
372                         gtk_icon_theme_get_for_screen (old_screen),
373                         G_CALLBACK (ev_sidebar_attachments_update_icons),
374                         ev_attachbar);
375         }
376
377         ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
378         g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
379                                   "changed",
380                                   G_CALLBACK (ev_sidebar_attachments_update_icons),
381                                   (gpointer) ev_attachbar);
382
383         if (GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed) {
384                 GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed (widget, old_screen);
385         }
386 }
387
388 static void
389 ev_sidebar_attachments_drag_data_get (GtkWidget        *widget,
390                                       GdkDragContext   *drag_context,
391                                       GtkSelectionData *data,
392                                       guint             info,
393                                       guint             time,
394                                       gpointer          user_data)
395 {
396         EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (user_data);
397         GList                *selected = NULL, *l;
398         GPtrArray            *uris;
399         char                **uri_list;
400
401         selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
402         if (!selected)
403                 return;
404
405         uris = g_ptr_array_new ();
406         
407         for (l = selected; l && l->data; l = g_list_next (l)) {
408                 EvAttachment *attachment;
409                 GtkTreePath  *path;
410                 GtkTreeIter   iter;
411                 GFile        *file;
412                 gchar        *filename;
413                 GError       *error = NULL;
414                 
415                 path = (GtkTreePath *) l->data;
416
417                 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
418                                          &iter, path);
419                 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
420                                     COLUMN_ATTACHMENT, &attachment,
421                                     -1);
422
423                 filename = g_build_filename (ev_tmp_dir (),
424                                              ev_attachment_get_name (attachment),
425                                              NULL);
426                 file = g_file_new_for_path (filename);
427                 g_free (filename);
428                 
429                 if (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 #if !GTK_CHECK_VERSION (2, 15, 0)
538         const GtkTargetEntry drag_targets[] = {
539                 { "text/uri-list", 0, 0 }
540         };
541 #endif
542
543         GtkWidget *swindow;
544         
545         ev_attachbar->priv = EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE (ev_attachbar);
546
547         swindow = gtk_scrolled_window_new (NULL, NULL);
548         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
549                                         GTK_POLICY_NEVER,
550                                         GTK_POLICY_AUTOMATIC);
551         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
552                                              GTK_SHADOW_IN);
553         /* Data Model */
554         ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
555                                                         GDK_TYPE_PIXBUF, 
556                                                         G_TYPE_STRING,  
557                                                         G_TYPE_STRING,
558                                                         EV_TYPE_ATTACHMENT);
559
560         /* Icon View */
561         ev_attachbar->priv->icon_view =
562                 gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
563         gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
564                                           GTK_SELECTION_MULTIPLE);
565         gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
566         g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
567                       "text-column", COLUMN_NAME,
568                       "pixbuf-column", COLUMN_ICON,
569                       NULL);
570         g_signal_connect_swapped (ev_attachbar->priv->icon_view,
571                                   "button-press-event",
572                                   G_CALLBACK (ev_sidebar_attachments_button_press),
573                                   (gpointer) ev_attachbar);
574
575         gtk_container_add (GTK_CONTAINER (swindow),
576                            ev_attachbar->priv->icon_view);
577
578         gtk_container_add (GTK_CONTAINER (ev_attachbar),
579                            swindow);
580         gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
581
582         /* Icon Theme */
583         ev_attachbar->priv->icon_theme = NULL;
584
585         /* Icon Cache */
586         ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
587                                                                 g_str_equal,
588                                                                 g_free,
589                                                                 g_object_unref);
590
591         /* Drag and Drop */
592 #if GTK_CHECK_VERSION (2, 15, 0)
593         gtk_icon_view_enable_model_drag_source (
594                 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
595                 GDK_BUTTON1_MASK,
596                 NULL, 0,
597                 GDK_ACTION_COPY);
598         gtk_drag_source_add_uri_targets (ev_attachbar->priv->icon_view);
599 #else
600         gtk_icon_view_enable_model_drag_source (
601                 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
602                 GDK_BUTTON1_MASK,
603                 drag_targets,
604                 G_N_ELEMENTS (drag_targets),
605                 GDK_ACTION_COPY);
606 #endif
607
608         g_signal_connect (ev_attachbar->priv->icon_view,
609                           "drag-data-get",
610                           G_CALLBACK (ev_sidebar_attachments_drag_data_get),
611                           (gpointer) ev_attachbar);     
612 }
613
614 GtkWidget *
615 ev_sidebar_attachments_new (void)
616 {
617         GtkWidget *ev_attachbar;
618
619         ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
620
621         return ev_attachbar;
622 }
623
624 static void
625 job_finished_callback (EvJobAttachments     *job,
626                        EvSidebarAttachments *ev_attachbar)
627 {
628         GList *l;
629         
630         for (l = job->attachments; l && l->data; l = g_list_next (l)) {
631                 EvAttachment *attachment;
632                 GtkTreeIter   iter;
633                 GdkPixbuf    *pixbuf = NULL;
634                 const gchar  *mime_type;
635
636                 attachment = EV_ATTACHMENT (l->data);
637
638                 mime_type = ev_attachment_get_mime_type (attachment);
639                 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
640                                                                 mime_type);
641
642                 gtk_list_store_append (ev_attachbar->priv->model, &iter);
643                 gtk_list_store_set (ev_attachbar->priv->model, &iter,
644                                     COLUMN_NAME, ev_attachment_get_name (attachment),
645                                     COLUMN_ICON, pixbuf,
646                                     COLUMN_ATTACHMENT, attachment, 
647                                     -1);
648         }
649
650         g_object_unref (job);
651 }
652
653 static void
654 ev_sidebar_attachments_set_document (EvSidebarPage   *page,
655                                      EvDocument      *document)
656 {
657         EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (page);
658         EvJob *job;
659         
660         if (!ev_document_has_attachments (document))
661                 return;
662
663         if (!ev_attachbar->priv->icon_theme) {
664                 GdkScreen *screen;
665
666                 screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
667                 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
668                 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
669                                           "changed",
670                                           G_CALLBACK (ev_sidebar_attachments_update_icons),
671                                           (gpointer) ev_attachbar);
672         }
673                 
674         gtk_list_store_clear (ev_attachbar->priv->model);
675
676         job = ev_job_attachments_new (document);
677         g_signal_connect (job, "finished",
678                           G_CALLBACK (job_finished_callback),
679                           ev_attachbar);
680         g_signal_connect (job, "cancelled",
681                           G_CALLBACK (g_object_unref),
682                           NULL);
683         /* The priority doesn't matter for this job */
684         ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
685 }
686
687 static gboolean
688 ev_sidebar_attachments_support_document (EvSidebarPage   *sidebar_page,
689                                          EvDocument      *document)
690 {
691         return ev_document_has_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_document = ev_sidebar_attachments_set_document;
705         iface->get_label = ev_sidebar_attachments_get_label;
706 }
707