1 /* ev-sidebar-attachments.c
2  *  this file is part of atril, a mate document viewer
3  *
4  * Copyright (C) 2006 Carlos Garcia Campos
5  *
6  * Author:
7  *   Carlos Garcia Campos <carlosgc@gnome.org>
8  *
9  * Atril 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  * Atril 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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-document-misc.h"
36 #include "ev-jobs.h"
37 #include "ev-job-scheduler.h"
38 #include "ev-file-helpers.h"
39 #include "ev-sidebar-attachments.h"
40 #include "ev-sidebar-page.h"
41 
42 enum {
43 	COLUMN_ICON,
44 	COLUMN_NAME,
45 	COLUMN_DESCRIPTION,
46 	COLUMN_ATTACHMENT,
47 	N_COLS
48 };
49 
50 enum {
51 	PROP_0,
52 	PROP_WIDGET,
53 };
54 
55 enum {
56 	SIGNAL_POPUP_MENU,
57 	N_SIGNALS
58 };
59 
60 static guint signals[N_SIGNALS] = { 0 };
61 
62 struct _EvSidebarAttachmentsPrivate {
63 	GtkWidget      *icon_view;
64 	GtkListStore   *model;
65 
66 	/* Icons */
67 	GtkIconTheme   *icon_theme;
68 	GHashTable     *icon_cache;
69 };
70 
71 static void ev_sidebar_attachments_page_iface_init (EvSidebarPageInterface *iface);
72 
73 G_DEFINE_TYPE_EXTENDED (EvSidebarAttachments,
74                         ev_sidebar_attachments,
75                         GTK_TYPE_BOX,
76                         0,
77                         G_ADD_PRIVATE (EvSidebarAttachments)
78                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
79 					       ev_sidebar_attachments_page_iface_init))
80 
81 /* Icon cache */
82 static void
ev_sidebar_attachments_icon_cache_add(EvSidebarAttachments * ev_attachbar,const gchar * mime_type,const GdkPixbuf * pixbuf)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 *
icon_theme_get_pixbuf_from_mime_type(GtkIconTheme * icon_theme,const gchar * mime_type)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 ("mate-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 ("mate-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 *
ev_sidebar_attachments_icon_cache_get(EvSidebarAttachments * ev_attachbar,const gchar * mime_type)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
icon_cache_update_icon(gchar * key,GdkPixbuf * value,EvSidebarAttachments * ev_attachbar)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
ev_sidebar_attachments_icon_cache_refresh(EvSidebarAttachments * ev_attachbar)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 *
ev_sidebar_attachments_get_attachment_at_pos(EvSidebarAttachments * ev_attachbar,gint x,gint y)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
ev_sidebar_attachments_popup_menu_show(EvSidebarAttachments * ev_attachbar,gint x,gint y)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
ev_sidebar_attachments_popup_menu(GtkWidget * widget)260 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
261 {
262 	EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
263 	gint                  x, y;
264 
265 	ev_document_misc_get_pointer_position (widget, &x, &y);
266 
267 	return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
268 }
269 
270 static gboolean
ev_sidebar_attachments_button_press(EvSidebarAttachments * ev_attachbar,GdkEventButton * event,GtkWidget * icon_view)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
ev_sidebar_attachments_update_icons(EvSidebarAttachments * ev_attachbar,gpointer user_data)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
ev_sidebar_attachments_screen_changed(GtkWidget * widget,GdkScreen * old_screen)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
ev_sidebar_attachments_drag_data_get(GtkWidget * widget,GdkDragContext * drag_context,GtkSelectionData * data,guint info,guint time,gpointer user_data)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        *template;
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                 /* FIXMEchpe: convert to filename encoding first! */
424                 template = g_strdup_printf ("%s.XXXXXX", ev_attachment_get_name (attachment));
425                 file = ev_mkstemp_file (template, &error);
426                 g_free (template);
427 
428 		if (file != NULL && ev_attachment_save (attachment, file, &error)) {
429 			gchar *uri;
430 
431 			uri = g_file_get_uri (file);
432                         g_ptr_array_add (uris, uri);
433 		}
434 
435 		if (error) {
436 			g_warning ("%s", error->message);
437 			g_error_free (error);
438 		}
439 
440 		gtk_tree_path_free (path);
441 		g_object_unref (file);
442 		g_object_unref (attachment);
443 	}
444 
445         g_ptr_array_add (uris, NULL); /* NULL-terminate */
446         uri_list = (char **) g_ptr_array_free (uris, FALSE);
447         gtk_selection_data_set_uris (data, uri_list);
448         g_strfreev (uri_list);
449 
450 	g_list_free (selected);
451 }
452 
453 static void
ev_sidebar_attachments_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)454 ev_sidebar_attachments_get_property (GObject    *object,
455 				     guint       prop_id,
456 			    	     GValue     *value,
457 		      	             GParamSpec *pspec)
458 {
459 	EvSidebarAttachments *ev_sidebar_attachments;
460 
461 	ev_sidebar_attachments = EV_SIDEBAR_ATTACHMENTS (object);
462 
463 	switch (prop_id) {
464 	        case PROP_WIDGET:
465 			g_value_set_object (value, ev_sidebar_attachments->priv->icon_view);
466 			break;
467 	        default:
468 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
469 			break;
470 	}
471 }
472 
473 static void
ev_sidebar_attachments_dispose(GObject * object)474 ev_sidebar_attachments_dispose (GObject *object)
475 {
476 	EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (object);
477 
478 	if (ev_attachbar->priv->icon_theme) {
479 		g_signal_handlers_disconnect_by_func (
480 			ev_attachbar->priv->icon_theme,
481 			G_CALLBACK (ev_sidebar_attachments_update_icons),
482 			ev_attachbar);
483 		ev_attachbar->priv->icon_theme = NULL;
484 	}
485 
486 	if (ev_attachbar->priv->model) {
487 		g_object_unref (ev_attachbar->priv->model);
488 		ev_attachbar->priv->model = NULL;
489 	}
490 
491 	if (ev_attachbar->priv->icon_cache) {
492 		g_hash_table_destroy (ev_attachbar->priv->icon_cache);
493 		ev_attachbar->priv->icon_cache = NULL;
494 	}
495 
496 	(* G_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->dispose) (object);
497 }
498 
499 static void
ev_sidebar_attachments_class_init(EvSidebarAttachmentsClass * ev_attachbar_class)500 ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class)
501 {
502 	GObjectClass   *g_object_class;
503 	GtkWidgetClass *gtk_widget_class;
504 
505 	g_object_class = G_OBJECT_CLASS (ev_attachbar_class);
506 	gtk_widget_class = GTK_WIDGET_CLASS (ev_attachbar_class);
507 
508 	g_object_class->get_property = ev_sidebar_attachments_get_property;
509 	g_object_class->dispose = ev_sidebar_attachments_dispose;
510 	gtk_widget_class->popup_menu = ev_sidebar_attachments_popup_menu;
511 	gtk_widget_class->screen_changed = ev_sidebar_attachments_screen_changed;
512 
513 	/* Signals */
514 	signals[SIGNAL_POPUP_MENU] =
515 		g_signal_new ("popup",
516 			      G_TYPE_FROM_CLASS (g_object_class),
517 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
518 			      G_STRUCT_OFFSET (EvSidebarAttachmentsClass, popup_menu),
519 			      NULL, NULL,
520 			      g_cclosure_marshal_VOID__POINTER,
521 			      G_TYPE_NONE, 1,
522 			      G_TYPE_POINTER);
523 
524 	g_object_class_override_property (g_object_class,
525 					  PROP_WIDGET,
526 					  "main-widget");
527 }
528 
529 static void
ev_sidebar_attachments_init(EvSidebarAttachments * ev_attachbar)530 ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
531 {
532 	GtkWidget *swindow;
533 
534 	ev_attachbar->priv = ev_sidebar_attachments_get_instance_private (ev_attachbar);
535 
536 	gtk_orientable_set_orientation (GTK_ORIENTABLE (ev_attachbar), GTK_ORIENTATION_VERTICAL);
537 	swindow = gtk_scrolled_window_new (NULL, NULL);
538 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
539 					GTK_POLICY_NEVER,
540 					GTK_POLICY_AUTOMATIC);
541 	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
542 					     GTK_SHADOW_IN);
543 	/* Data Model */
544 	ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
545 							GDK_TYPE_PIXBUF,
546 							G_TYPE_STRING,
547 							G_TYPE_STRING,
548 							EV_TYPE_ATTACHMENT);
549 
550 	/* Icon View */
551 	ev_attachbar->priv->icon_view =
552 		gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
553 	gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
554 					  GTK_SELECTION_MULTIPLE);
555 	gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
556 	g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
557 		      "text-column", COLUMN_NAME,
558 		      "pixbuf-column", COLUMN_ICON,
559 		      NULL);
560 	g_signal_connect_swapped (ev_attachbar->priv->icon_view,
561 				  "button-press-event",
562 				  G_CALLBACK (ev_sidebar_attachments_button_press),
563 				  (gpointer) ev_attachbar);
564 
565 	gtk_container_add (GTK_CONTAINER (swindow),
566 			   ev_attachbar->priv->icon_view);
567 
568 	gtk_box_pack_start (GTK_BOX (ev_attachbar), swindow, TRUE, TRUE, 0);
569 
570 	gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
571 
572 	/* Icon Theme */
573 	ev_attachbar->priv->icon_theme = NULL;
574 
575 	/* Icon Cache */
576 	ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
577 								g_str_equal,
578 								g_free,
579 								g_object_unref);
580 
581 	/* Drag and Drop */
582 	gtk_icon_view_enable_model_drag_source (
583 		GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
584 		GDK_BUTTON1_MASK,
585 		NULL, 0,
586 		GDK_ACTION_COPY);
587         gtk_drag_source_add_uri_targets (ev_attachbar->priv->icon_view);
588 
589 	g_signal_connect (ev_attachbar->priv->icon_view,
590 			  "drag-data-get",
591 			  G_CALLBACK (ev_sidebar_attachments_drag_data_get),
592 			  (gpointer) ev_attachbar);
593 }
594 
595 GtkWidget *
ev_sidebar_attachments_new(void)596 ev_sidebar_attachments_new (void)
597 {
598 	GtkWidget *ev_attachbar;
599 
600 	ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
601 
602 	return ev_attachbar;
603 }
604 
605 static void
job_finished_callback(EvJobAttachments * job,EvSidebarAttachments * ev_attachbar)606 job_finished_callback (EvJobAttachments     *job,
607 		       EvSidebarAttachments *ev_attachbar)
608 {
609 	GList *l;
610 
611 	for (l = job->attachments; l && l->data; l = g_list_next (l)) {
612 		EvAttachment *attachment;
613 		GtkTreeIter   iter;
614 		GdkPixbuf    *pixbuf = NULL;
615 		const gchar  *mime_type;
616 
617 		attachment = EV_ATTACHMENT (l->data);
618 
619 		mime_type = ev_attachment_get_mime_type (attachment);
620 		pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
621 								mime_type);
622 
623 		gtk_list_store_append (ev_attachbar->priv->model, &iter);
624 		gtk_list_store_set (ev_attachbar->priv->model, &iter,
625 				    COLUMN_NAME, ev_attachment_get_name (attachment),
626 				    COLUMN_ICON, pixbuf,
627 				    COLUMN_ATTACHMENT, attachment,
628 				    -1);
629 	}
630 
631 	g_object_unref (job);
632 }
633 
634 
635 static void
ev_sidebar_attachments_document_changed_cb(EvDocumentModel * model,GParamSpec * pspec,EvSidebarAttachments * ev_attachbar)636 ev_sidebar_attachments_document_changed_cb (EvDocumentModel      *model,
637 					    GParamSpec           *pspec,
638 					    EvSidebarAttachments *ev_attachbar)
639 {
640 	EvDocument *document = ev_document_model_get_document (model);
641 	EvJob *job;
642 
643 	if (!EV_IS_DOCUMENT_ATTACHMENTS (document))
644 		return;
645 
646 	if (!ev_document_attachments_has_attachments (EV_DOCUMENT_ATTACHMENTS (document)))
647 		return;
648 
649 	if (!ev_attachbar->priv->icon_theme) {
650 		GdkScreen *screen;
651 
652 		screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
653 		ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
654 		g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
655 					  "changed",
656 					  G_CALLBACK (ev_sidebar_attachments_update_icons),
657 					  (gpointer) ev_attachbar);
658 	}
659 
660 	gtk_list_store_clear (ev_attachbar->priv->model);
661 
662 	job = ev_job_attachments_new (document);
663 	g_signal_connect (job, "finished",
664 			  G_CALLBACK (job_finished_callback),
665 			  ev_attachbar);
666 	g_signal_connect (job, "cancelled",
667 			  G_CALLBACK (g_object_unref),
668 			  NULL);
669 	/* The priority doesn't matter for this job */
670 	ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
671 }
672 
673 static void
ev_sidebar_attachments_set_model(EvSidebarPage * page,EvDocumentModel * model)674 ev_sidebar_attachments_set_model (EvSidebarPage   *page,
675 				  EvDocumentModel *model)
676 {
677 	g_signal_connect (model, "notify::document",
678 			  G_CALLBACK (ev_sidebar_attachments_document_changed_cb),
679 			  page);
680 }
681 
682 static gboolean
ev_sidebar_attachments_support_document(EvSidebarPage * sidebar_page,EvDocument * document)683 ev_sidebar_attachments_support_document (EvSidebarPage   *sidebar_page,
684 					 EvDocument      *document)
685 {
686 	return (EV_IS_DOCUMENT_ATTACHMENTS (document) &&
687 		ev_document_attachments_has_attachments (EV_DOCUMENT_ATTACHMENTS (document)));
688 }
689 
690 static const gchar*
ev_sidebar_attachments_get_label(EvSidebarPage * sidebar_page)691 ev_sidebar_attachments_get_label (EvSidebarPage *sidebar_page)
692 {
693 	return _("Attachments");
694 }
695 
696 static void
ev_sidebar_attachments_page_iface_init(EvSidebarPageInterface * iface)697 ev_sidebar_attachments_page_iface_init (EvSidebarPageInterface *iface)
698 {
699 	iface->support_document = ev_sidebar_attachments_support_document;
700 	iface->set_model = ev_sidebar_attachments_set_model;
701 	iface->get_label = ev_sidebar_attachments_get_label;
702 }
703 
704