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 guint signals[N_SIGNALS];
60 struct _EvSidebarAttachmentsPrivate {
65 GtkIconTheme *icon_theme;
66 GHashTable *icon_cache;
69 static void ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface);
71 G_DEFINE_TYPE_EXTENDED (EvSidebarAttachments,
72 ev_sidebar_attachments,
75 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
76 ev_sidebar_attachments_page_iface_init))
78 #define EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE(object) \
79 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_ATTACHMENTS, EvSidebarAttachmentsPrivate))
83 ev_sidebar_attachments_icon_cache_add (EvSidebarAttachments *ev_attachbar,
84 const gchar *mime_type,
85 const GdkPixbuf *pixbuf)
87 g_assert (mime_type != NULL);
88 g_assert (GDK_IS_PIXBUF (pixbuf));
90 g_hash_table_insert (ev_attachbar->priv->icon_cache,
91 (gpointer)g_strdup (mime_type),
97 icon_theme_get_pixbuf_from_mime_type (GtkIconTheme *icon_theme,
98 const gchar *mime_type)
100 const char *separator;
104 separator = strchr (mime_type, '/');
106 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
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);
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);
126 ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
127 const gchar *mime_type)
129 GdkPixbuf *pixbuf = NULL;
131 g_assert (mime_type != NULL);
133 pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
136 if (GDK_IS_PIXBUF (pixbuf))
139 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
142 if (GDK_IS_PIXBUF (pixbuf))
143 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
151 icon_cache_update_icon (gchar *key,
153 EvSidebarAttachments *ev_attachbar)
155 GdkPixbuf *pixbuf = NULL;
157 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
160 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
168 ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
170 g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
171 (GHRFunc) icon_cache_update_icon,
175 static EvAttachment *
176 ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
180 GtkTreePath *path = NULL;
182 EvAttachment *attachment = NULL;
184 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
190 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
192 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
193 COLUMN_ATTACHMENT, &attachment,
196 gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
199 gtk_tree_path_free (path);
205 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
209 GtkIconView *icon_view;
211 GList *selected = NULL, *l;
212 GList *attach_list = NULL;
214 icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
216 path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
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);
225 gtk_tree_path_free (path);
227 selected = gtk_icon_view_get_selected_items (icon_view);
231 for (l = selected; l && l->data; l = g_list_next (l)) {
233 EvAttachment *attachment = NULL;
235 path = (GtkTreePath *) l->data;
237 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
239 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
240 COLUMN_ATTACHMENT, &attachment,
244 attach_list = g_list_prepend (attach_list, attachment);
246 gtk_tree_path_free (path);
249 g_list_free (selected);
254 g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
260 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
262 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
265 gtk_widget_get_pointer (widget, &x, &y);
267 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
271 ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
272 GdkEventButton *event,
273 GtkWidget *icon_view)
275 if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
276 gtk_widget_grab_focus (icon_view);
279 if (event->button == 2)
282 switch (event->button) {
284 if (event->type == GDK_2BUTTON_PRESS) {
285 GError *error = NULL;
286 EvAttachment *attachment;
288 attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
294 ev_attachment_open (attachment,
295 gtk_widget_get_screen (GTK_WIDGET (ev_attachbar)),
300 g_warning ("%s", error->message);
301 g_error_free (error);
304 g_object_unref (attachment);
310 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
317 ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
323 ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
325 valid = gtk_tree_model_get_iter_first (
326 GTK_TREE_MODEL (ev_attachbar->priv->model),
330 EvAttachment *attachment = NULL;
331 GdkPixbuf *pixbuf = NULL;
332 const gchar *mime_type;
334 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
335 COLUMN_ATTACHMENT, &attachment,
338 mime_type = ev_attachment_get_mime_type (attachment);
341 g_object_unref (attachment);
343 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
346 gtk_list_store_set (ev_attachbar->priv->model, &iter,
350 valid = gtk_tree_model_iter_next (
351 GTK_TREE_MODEL (ev_attachbar->priv->model),
357 ev_sidebar_attachments_screen_changed (GtkWidget *widget,
358 GdkScreen *old_screen)
360 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
363 if (!ev_attachbar->priv->icon_theme)
366 screen = gtk_widget_get_screen (widget);
367 if (screen == 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),
377 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
378 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
380 G_CALLBACK (ev_sidebar_attachments_update_icons),
381 (gpointer) ev_attachbar);
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);
389 ev_sidebar_attachments_drag_data_get (GtkWidget *widget,
390 GdkDragContext *drag_context,
391 GtkSelectionData *data,
396 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (user_data);
397 GList *selected = NULL, *l;
401 selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
405 uris = g_ptr_array_new ();
407 for (l = selected; l && l->data; l = g_list_next (l)) {
408 EvAttachment *attachment;
413 GError *error = NULL;
415 path = (GtkTreePath *) l->data;
417 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
419 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
420 COLUMN_ATTACHMENT, &attachment,
423 filename = g_build_filename (ev_tmp_dir (),
424 ev_attachment_get_name (attachment),
426 file = g_file_new_for_path (filename);
429 if (ev_attachment_save (attachment, file, &error)) {
432 uri = g_file_get_uri (file);
433 g_ptr_array_add (uris, uri);
437 g_warning ("%s", error->message);
438 g_error_free (error);
441 gtk_tree_path_free (path);
442 g_object_unref (file);
443 g_object_unref (attachment);
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);
451 g_list_free (selected);
455 ev_sidebar_attachments_get_property (GObject *object,
460 EvSidebarAttachments *ev_sidebar_attachments;
462 ev_sidebar_attachments = EV_SIDEBAR_ATTACHMENTS (object);
466 g_value_set_object (value, ev_sidebar_attachments->priv->icon_view);
469 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
475 ev_sidebar_attachments_destroy (GtkObject *object)
477 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (object);
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),
484 ev_attachbar->priv->icon_theme = NULL;
487 if (ev_attachbar->priv->model) {
488 g_object_unref (ev_attachbar->priv->model);
489 ev_attachbar->priv->model = NULL;
492 if (ev_attachbar->priv->icon_cache) {
493 g_hash_table_destroy (ev_attachbar->priv->icon_cache);
494 ev_attachbar->priv->icon_cache = NULL;
497 (* GTK_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->destroy) (object);
501 ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class)
503 GObjectClass *g_object_class;
504 GtkObjectClass *gtk_object_class;
505 GtkWidgetClass *gtk_widget_class;
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);
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;
516 g_type_class_add_private (g_object_class, sizeof (EvSidebarAttachmentsPrivate));
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),
525 g_cclosure_marshal_VOID__POINTER,
529 g_object_class_override_property (g_object_class,
535 ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
537 #if !GTK_CHECK_VERSION (2, 15, 0)
538 const GtkTargetEntry drag_targets[] = {
539 { "text/uri-list", 0, 0 }
545 ev_attachbar->priv = EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE (ev_attachbar);
547 swindow = gtk_scrolled_window_new (NULL, NULL);
548 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
550 GTK_POLICY_AUTOMATIC);
551 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
554 ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
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,
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);
575 gtk_container_add (GTK_CONTAINER (swindow),
576 ev_attachbar->priv->icon_view);
578 gtk_container_add (GTK_CONTAINER (ev_attachbar),
580 gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
583 ev_attachbar->priv->icon_theme = NULL;
586 ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
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),
598 gtk_drag_source_add_uri_targets (ev_attachbar->priv->icon_view);
600 gtk_icon_view_enable_model_drag_source (
601 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
604 G_N_ELEMENTS (drag_targets),
608 g_signal_connect (ev_attachbar->priv->icon_view,
610 G_CALLBACK (ev_sidebar_attachments_drag_data_get),
611 (gpointer) ev_attachbar);
615 ev_sidebar_attachments_new (void)
617 GtkWidget *ev_attachbar;
619 ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
625 job_finished_callback (EvJobAttachments *job,
626 EvSidebarAttachments *ev_attachbar)
630 for (l = job->attachments; l && l->data; l = g_list_next (l)) {
631 EvAttachment *attachment;
633 GdkPixbuf *pixbuf = NULL;
634 const gchar *mime_type;
636 attachment = EV_ATTACHMENT (l->data);
638 mime_type = ev_attachment_get_mime_type (attachment);
639 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
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),
646 COLUMN_ATTACHMENT, attachment,
650 g_object_unref (job);
654 ev_sidebar_attachments_set_document (EvSidebarPage *page,
655 EvDocument *document)
657 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (page);
660 if (!ev_document_has_attachments (document))
663 if (!ev_attachbar->priv->icon_theme) {
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,
670 G_CALLBACK (ev_sidebar_attachments_update_icons),
671 (gpointer) ev_attachbar);
674 gtk_list_store_clear (ev_attachbar->priv->model);
676 job = ev_job_attachments_new (document);
677 g_signal_connect (job, "finished",
678 G_CALLBACK (job_finished_callback),
680 g_signal_connect (job, "cancelled",
681 G_CALLBACK (g_object_unref),
683 /* The priority doesn't matter for this job */
684 ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
688 ev_sidebar_attachments_support_document (EvSidebarPage *sidebar_page,
689 EvDocument *document)
691 return ev_document_has_attachments (document);
695 ev_sidebar_attachments_get_label (EvSidebarPage *sidebar_page)
697 return _("Attachments");
701 ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface)
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;