1 /* ev-sidebar-attachments.c
2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2006 Carlos Garcia Campos
7 * Carlos Garcia Campos <carlosgc@gnome.org>
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.
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.
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.
30 #include <glib/gi18n.h>
31 #include <glib/gstdio.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"
58 static const GtkTargetEntry drag_targets[] = {
59 { "text/uri-list", 0, 0 }
62 static guint signals[N_SIGNALS];
64 struct _EvSidebarAttachmentsPrivate {
69 GtkIconTheme *icon_theme;
70 GHashTable *icon_cache;
73 static void ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface);
75 G_DEFINE_TYPE_EXTENDED (EvSidebarAttachments,
76 ev_sidebar_attachments,
79 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
80 ev_sidebar_attachments_page_iface_init))
82 #define EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE(object) \
83 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_ATTACHMENTS, EvSidebarAttachmentsPrivate))
87 ev_sidebar_attachments_icon_cache_add (EvSidebarAttachments *ev_attachbar,
88 const gchar *mime_type,
89 const GdkPixbuf *pixbuf)
91 g_assert (mime_type != NULL);
92 g_assert (GDK_IS_PIXBUF (pixbuf));
94 g_hash_table_insert (ev_attachbar->priv->icon_cache,
95 (gpointer)g_strdup (mime_type),
101 icon_theme_get_pixbuf_from_mime_type (GtkIconTheme *icon_theme,
102 const gchar *mime_type)
104 const char *separator;
108 separator = strchr (mime_type, '/');
110 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
112 icon_name = g_string_new ("gnome-mime-");
113 g_string_append_len (icon_name, mime_type, separator - mime_type);
114 g_string_append_c (icon_name, '-');
115 g_string_append (icon_name, separator + 1);
116 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
117 g_string_free (icon_name, TRUE);
121 icon_name = g_string_new ("gnome-mime-");
122 g_string_append_len (icon_name, mime_type, separator - mime_type);
123 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
124 g_string_free (icon_name, TRUE);
130 ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
131 const gchar *mime_type)
133 GdkPixbuf *pixbuf = NULL;
135 g_assert (mime_type != NULL);
137 pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
140 if (GDK_IS_PIXBUF (pixbuf))
143 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
146 if (GDK_IS_PIXBUF (pixbuf))
147 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
155 icon_cache_update_icon (gchar *key,
157 EvSidebarAttachments *ev_attachbar)
159 GdkPixbuf *pixbuf = NULL;
161 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
164 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
172 ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
174 g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
175 (GHRFunc) icon_cache_update_icon,
179 static EvAttachment *
180 ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
184 GtkTreePath *path = NULL;
186 EvAttachment *attachment = NULL;
188 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
194 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
196 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
197 COLUMN_ATTACHMENT, &attachment,
200 gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
203 gtk_tree_path_free (path);
209 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
213 GtkIconView *icon_view;
215 GList *selected = NULL, *l;
216 GList *attach_list = NULL;
218 icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
220 path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
224 if (!gtk_icon_view_path_is_selected (icon_view, path)) {
225 gtk_icon_view_unselect_all (icon_view);
226 gtk_icon_view_select_path (icon_view, path);
229 gtk_tree_path_free (path);
231 selected = gtk_icon_view_get_selected_items (icon_view);
235 for (l = selected; l && l->data; l = g_list_next (l)) {
237 EvAttachment *attachment = NULL;
239 path = (GtkTreePath *) l->data;
241 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
243 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
244 COLUMN_ATTACHMENT, &attachment,
248 attach_list = g_list_prepend (attach_list, attachment);
250 gtk_tree_path_free (path);
253 g_list_free (selected);
258 g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
264 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
266 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
269 gtk_widget_get_pointer (widget, &x, &y);
271 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
275 ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
276 GdkEventButton *event,
277 GtkWidget *icon_view)
279 if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
280 gtk_widget_grab_focus (icon_view);
283 if (event->button == 2)
286 switch (event->button) {
288 if (event->type == GDK_2BUTTON_PRESS) {
289 GError *error = NULL;
290 EvAttachment *attachment;
292 attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
298 ev_attachment_open (attachment, &error);
301 g_warning ("%s", error->message);
302 g_error_free (error);
305 g_object_unref (attachment);
311 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
318 ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
324 ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
326 valid = gtk_tree_model_get_iter_first (
327 GTK_TREE_MODEL (ev_attachbar->priv->model),
331 EvAttachment *attachment = NULL;
332 GdkPixbuf *pixbuf = NULL;
333 const gchar *mime_type;
335 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
336 COLUMN_ATTACHMENT, &attachment,
339 mime_type = ev_attachment_get_mime_type (attachment);
342 g_object_unref (attachment);
344 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
347 gtk_list_store_set (ev_attachbar->priv->model, &iter,
351 valid = gtk_tree_model_iter_next (
352 GTK_TREE_MODEL (ev_attachbar->priv->model),
358 ev_sidebar_attachments_screen_changed (GtkWidget *widget,
359 GdkScreen *old_screen)
361 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
364 if (!ev_attachbar->priv->icon_theme)
367 screen = gtk_widget_get_screen (widget);
368 if (screen == 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),
378 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
379 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
381 G_CALLBACK (ev_sidebar_attachments_update_icons),
382 (gpointer) ev_attachbar);
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);
390 ev_sidebar_attachments_drag_data_get (GtkWidget *widget,
391 GdkDragContext *drag_context,
392 GtkSelectionData *data,
397 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (user_data);
400 GList *selected = NULL, *l;
402 selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
406 uri_list = g_string_new (NULL);
408 for (l = selected; l && l->data; l = g_list_next (l)) {
409 EvAttachment *attachment;
414 GError *error = NULL;
416 path = (GtkTreePath *) l->data;
418 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
420 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
421 COLUMN_ATTACHMENT, &attachment,
424 filename = g_build_filename (ev_tmp_dir (),
425 ev_attachment_get_name (attachment),
427 file = g_file_new_for_path (filename);
430 if (ev_attachment_save (attachment, file, &error)) {
433 uri = g_file_get_uri (file);
434 g_string_append (uri_list, uri);
435 g_string_append_c (uri_list, '\n');
440 g_warning ("%s", error->message);
441 g_error_free (error);
444 gtk_tree_path_free (path);
445 g_object_unref (file);
446 g_object_unref (attachment);
449 uris = g_string_free (uri_list, FALSE);
452 gtk_selection_data_set (data,
459 g_list_free (selected);
463 ev_sidebar_attachments_get_property (GObject *object,
468 EvSidebarAttachments *ev_sidebar_attachments;
470 ev_sidebar_attachments = EV_SIDEBAR_ATTACHMENTS (object);
474 g_value_set_object (value, ev_sidebar_attachments->priv->icon_view);
477 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
483 ev_sidebar_attachments_destroy (GtkObject *object)
485 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (object);
487 if (ev_attachbar->priv->icon_theme) {
488 g_signal_handlers_disconnect_by_func (
489 ev_attachbar->priv->icon_theme,
490 G_CALLBACK (ev_sidebar_attachments_update_icons),
492 ev_attachbar->priv->icon_theme = NULL;
495 if (ev_attachbar->priv->model) {
496 g_object_unref (ev_attachbar->priv->model);
497 ev_attachbar->priv->model = NULL;
500 if (ev_attachbar->priv->icon_cache) {
501 g_hash_table_destroy (ev_attachbar->priv->icon_cache);
502 ev_attachbar->priv->icon_cache = NULL;
505 (* GTK_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->destroy) (object);
509 ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class)
511 GObjectClass *g_object_class;
512 GtkObjectClass *gtk_object_class;
513 GtkWidgetClass *gtk_widget_class;
515 g_object_class = G_OBJECT_CLASS (ev_attachbar_class);
516 gtk_object_class = GTK_OBJECT_CLASS (ev_attachbar_class);
517 gtk_widget_class = GTK_WIDGET_CLASS (ev_attachbar_class);
519 g_object_class->get_property = ev_sidebar_attachments_get_property;
520 gtk_object_class->destroy = ev_sidebar_attachments_destroy;
521 gtk_widget_class->popup_menu = ev_sidebar_attachments_popup_menu;
522 gtk_widget_class->screen_changed = ev_sidebar_attachments_screen_changed;
524 g_type_class_add_private (g_object_class, sizeof (EvSidebarAttachmentsPrivate));
527 signals[SIGNAL_POPUP_MENU] =
528 g_signal_new ("popup",
529 G_TYPE_FROM_CLASS (g_object_class),
530 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
531 G_STRUCT_OFFSET (EvSidebarAttachmentsClass, popup_menu),
533 g_cclosure_marshal_VOID__POINTER,
537 g_object_class_override_property (g_object_class,
543 ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
547 ev_attachbar->priv = EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE (ev_attachbar);
549 swindow = gtk_scrolled_window_new (NULL, NULL);
550 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
552 GTK_POLICY_AUTOMATIC);
553 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
556 ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
563 ev_attachbar->priv->icon_view =
564 gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
565 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
566 GTK_SELECTION_MULTIPLE);
567 gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
568 g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
569 "text-column", COLUMN_NAME,
570 "pixbuf-column", COLUMN_ICON,
572 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_view),
573 "button-press-event",
574 G_CALLBACK (ev_sidebar_attachments_button_press),
575 (gpointer) ev_attachbar);
577 gtk_container_add (GTK_CONTAINER (swindow),
578 ev_attachbar->priv->icon_view);
580 gtk_container_add (GTK_CONTAINER (ev_attachbar),
582 gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
585 ev_attachbar->priv->icon_theme = NULL;
588 ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
594 gtk_icon_view_enable_model_drag_source (
595 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
598 G_N_ELEMENTS (drag_targets),
601 g_signal_connect (G_OBJECT (ev_attachbar->priv->icon_view),
603 G_CALLBACK (ev_sidebar_attachments_drag_data_get),
604 (gpointer) ev_attachbar);
608 ev_sidebar_attachments_new (void)
610 GtkWidget *ev_attachbar;
612 ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
618 job_finished_callback (EvJobAttachments *job,
619 EvSidebarAttachments *ev_attachbar)
623 for (l = job->attachments; l && l->data; l = g_list_next (l)) {
624 EvAttachment *attachment;
626 GdkPixbuf *pixbuf = NULL;
627 const gchar *mime_type;
629 attachment = EV_ATTACHMENT (l->data);
631 mime_type = ev_attachment_get_mime_type (attachment);
632 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
635 gtk_list_store_append (ev_attachbar->priv->model, &iter);
636 gtk_list_store_set (ev_attachbar->priv->model, &iter,
637 COLUMN_NAME, ev_attachment_get_name (attachment),
639 COLUMN_ATTACHMENT, attachment,
643 g_object_unref (job);
647 ev_sidebar_attachments_set_document (EvSidebarPage *page,
648 EvDocument *document)
650 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (page);
653 if (!ev_document_has_attachments (document))
656 if (!ev_attachbar->priv->icon_theme) {
659 screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
660 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
661 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_theme),
663 G_CALLBACK (ev_sidebar_attachments_update_icons),
664 (gpointer) ev_attachbar);
667 gtk_list_store_clear (ev_attachbar->priv->model);
669 job = ev_job_attachments_new (document);
670 g_signal_connect (job, "finished",
671 G_CALLBACK (job_finished_callback),
673 g_signal_connect (job, "cancelled",
674 G_CALLBACK (g_object_unref),
676 /* The priority doesn't matter for this job */
677 ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
681 ev_sidebar_attachments_support_document (EvSidebarPage *sidebar_page,
682 EvDocument *document)
684 return ev_document_has_attachments (document);
688 ev_sidebar_attachments_get_label (EvSidebarPage *sidebar_page)
690 return _("Attachments");
694 ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface)
696 iface->support_document = ev_sidebar_attachments_support_document;
697 iface->set_document = ev_sidebar_attachments_set_document;
698 iface->get_label = ev_sidebar_attachments_get_label;