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>
34 #include "ev-file-helpers.h"
35 #include "ev-sidebar-attachments.h"
36 #include "ev-sidebar-page.h"
56 static const GtkTargetEntry drag_targets[] = {
57 { "text/uri-list", 0, 0 }
60 static guint signals[N_SIGNALS];
62 struct _EvSidebarAttachmentsPrivate {
67 GtkIconTheme *icon_theme;
68 GHashTable *icon_cache;
71 static void ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface);
73 G_DEFINE_TYPE_EXTENDED (EvSidebarAttachments,
74 ev_sidebar_attachments,
77 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
78 ev_sidebar_attachments_page_iface_init))
80 #define EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE(object) \
81 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_ATTACHMENTS, EvSidebarAttachmentsPrivate))
85 ev_sidebar_attachments_icon_cache_add (EvSidebarAttachments *ev_attachbar,
86 const gchar *mime_type,
87 const GdkPixbuf *pixbuf)
89 g_assert (mime_type != NULL);
90 g_assert (GDK_IS_PIXBUF (pixbuf));
92 g_hash_table_insert (ev_attachbar->priv->icon_cache,
93 (gpointer)g_strdup (mime_type),
99 icon_theme_get_pixbuf_from_mime_type (GtkIconTheme *icon_theme,
100 const gchar *mime_type)
102 const char *separator;
106 separator = strchr (mime_type, '/');
108 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
110 icon_name = g_string_new ("gnome-mime-");
111 g_string_append_len (icon_name, mime_type, separator - mime_type);
112 g_string_append_c (icon_name, '-');
113 g_string_append (icon_name, separator + 1);
114 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
115 g_string_free (icon_name, TRUE);
119 icon_name = g_string_new ("gnome-mime-");
120 g_string_append_len (icon_name, mime_type, separator - mime_type);
121 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
122 g_string_free (icon_name, TRUE);
128 ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
129 const gchar *mime_type)
131 GdkPixbuf *pixbuf = NULL;
133 g_assert (mime_type != NULL);
135 pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
138 if (GDK_IS_PIXBUF (pixbuf))
141 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
144 if (GDK_IS_PIXBUF (pixbuf))
145 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
153 icon_cache_update_icon (gchar *key,
155 EvSidebarAttachments *ev_attachbar)
157 GdkPixbuf *pixbuf = NULL;
159 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
162 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
170 ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
172 g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
173 (GHRFunc) icon_cache_update_icon,
177 static EvAttachment *
178 ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
182 GtkTreePath *path = NULL;
184 EvAttachment *attachment = NULL;
186 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
192 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
194 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
195 COLUMN_ATTACHMENT, &attachment,
198 gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
201 gtk_tree_path_free (path);
207 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
211 GtkIconView *icon_view;
213 GList *selected = NULL, *l;
214 GList *attach_list = NULL;
216 icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
218 path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
222 if (!gtk_icon_view_path_is_selected (icon_view, path)) {
223 gtk_icon_view_unselect_all (icon_view);
224 gtk_icon_view_select_path (icon_view, path);
227 gtk_tree_path_free (path);
229 selected = gtk_icon_view_get_selected_items (icon_view);
233 for (l = selected; l && l->data; l = g_list_next (l)) {
235 EvAttachment *attachment = NULL;
237 path = (GtkTreePath *) l->data;
239 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
241 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
242 COLUMN_ATTACHMENT, &attachment,
246 attach_list = g_list_prepend (attach_list, attachment);
248 gtk_tree_path_free (path);
251 g_list_free (selected);
256 g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
262 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
264 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
267 gtk_widget_get_pointer (widget, &x, &y);
269 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
273 ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
274 GdkEventButton *event,
275 GtkWidget *icon_view)
277 if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
278 gtk_widget_grab_focus (icon_view);
281 if (event->button == 2)
284 switch (event->button) {
286 if (event->type == GDK_2BUTTON_PRESS) {
287 GError *error = NULL;
288 EvAttachment *attachment;
290 attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
296 ev_attachment_open (attachment, &error);
299 g_warning ("%s", error->message);
300 g_error_free (error);
303 g_object_unref (attachment);
309 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
316 ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
322 ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
324 valid = gtk_tree_model_get_iter_first (
325 GTK_TREE_MODEL (ev_attachbar->priv->model),
329 EvAttachment *attachment = NULL;
330 GdkPixbuf *pixbuf = NULL;
331 const gchar *mime_type;
333 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
334 COLUMN_ATTACHMENT, &attachment,
337 mime_type = ev_attachment_get_mime_type (attachment);
340 g_object_unref (attachment);
342 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
345 gtk_list_store_set (ev_attachbar->priv->model, &iter,
349 valid = gtk_tree_model_iter_next (
350 GTK_TREE_MODEL (ev_attachbar->priv->model),
356 ev_sidebar_attachments_screen_changed (GtkWidget *widget,
357 GdkScreen *old_screen)
359 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
362 if (!ev_attachbar->priv->icon_theme)
365 screen = gtk_widget_get_screen (widget);
366 if (screen == old_screen)
370 g_signal_handlers_disconnect_by_func (
371 gtk_icon_theme_get_for_screen (old_screen),
372 G_CALLBACK (ev_sidebar_attachments_update_icons),
376 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
377 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
379 G_CALLBACK (ev_sidebar_attachments_update_icons),
380 (gpointer) ev_attachbar);
382 if (GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed) {
383 GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed (widget, old_screen);
388 ev_sidebar_attachments_drag_data_get (GtkWidget *widget,
389 GdkDragContext *drag_context,
390 GtkSelectionData *data,
395 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (user_data);
398 GList *selected = NULL, *l;
400 selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
404 uri_list = g_string_new (NULL);
406 for (l = selected; l && l->data; l = g_list_next (l)) {
407 EvAttachment *attachment;
412 GError *error = NULL;
414 path = (GtkTreePath *) l->data;
416 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
418 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
419 COLUMN_ATTACHMENT, &attachment,
422 filename = g_build_filename (ev_tmp_dir (),
423 ev_attachment_get_name (attachment),
425 file = g_file_new_for_path (filename);
428 if (ev_attachment_save (attachment, file, &error)) {
431 uri = g_file_get_uri (file);
432 g_string_append (uri_list, uri);
433 g_string_append_c (uri_list, '\n');
438 g_warning ("%s", error->message);
439 g_error_free (error);
442 gtk_tree_path_free (path);
443 g_object_unref (file);
444 g_object_unref (attachment);
447 uris = g_string_free (uri_list, FALSE);
450 gtk_selection_data_set (data,
457 g_list_free (selected);
461 ev_sidebar_attachments_get_property (GObject *object,
466 EvSidebarAttachments *ev_sidebar_attachments;
468 ev_sidebar_attachments = EV_SIDEBAR_ATTACHMENTS (object);
472 g_value_set_object (value, ev_sidebar_attachments->priv->icon_view);
475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
481 ev_sidebar_attachments_destroy (GtkObject *object)
483 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (object);
485 if (ev_attachbar->priv->icon_theme) {
486 g_signal_handlers_disconnect_by_func (
487 ev_attachbar->priv->icon_theme,
488 G_CALLBACK (ev_sidebar_attachments_update_icons),
490 ev_attachbar->priv->icon_theme = NULL;
493 if (ev_attachbar->priv->model) {
494 g_object_unref (ev_attachbar->priv->model);
495 ev_attachbar->priv->model = NULL;
498 if (ev_attachbar->priv->icon_cache) {
499 g_hash_table_destroy (ev_attachbar->priv->icon_cache);
500 ev_attachbar->priv->icon_cache = NULL;
503 (* GTK_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->destroy) (object);
507 ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class)
509 GObjectClass *g_object_class;
510 GtkObjectClass *gtk_object_class;
511 GtkWidgetClass *gtk_widget_class;
513 g_object_class = G_OBJECT_CLASS (ev_attachbar_class);
514 gtk_object_class = GTK_OBJECT_CLASS (ev_attachbar_class);
515 gtk_widget_class = GTK_WIDGET_CLASS (ev_attachbar_class);
517 g_object_class->get_property = ev_sidebar_attachments_get_property;
518 gtk_object_class->destroy = ev_sidebar_attachments_destroy;
519 gtk_widget_class->popup_menu = ev_sidebar_attachments_popup_menu;
520 gtk_widget_class->screen_changed = ev_sidebar_attachments_screen_changed;
522 g_type_class_add_private (g_object_class, sizeof (EvSidebarAttachmentsPrivate));
525 signals[SIGNAL_POPUP_MENU] =
526 g_signal_new ("popup",
527 G_TYPE_FROM_CLASS (g_object_class),
528 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
529 G_STRUCT_OFFSET (EvSidebarAttachmentsClass, popup_menu),
531 g_cclosure_marshal_VOID__POINTER,
535 g_object_class_override_property (g_object_class,
541 ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
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 (G_OBJECT (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 gtk_icon_view_enable_model_drag_source (
593 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
596 G_N_ELEMENTS (drag_targets),
599 g_signal_connect (G_OBJECT (ev_attachbar->priv->icon_view),
601 G_CALLBACK (ev_sidebar_attachments_drag_data_get),
602 (gpointer) ev_attachbar);
606 ev_sidebar_attachments_new (void)
608 GtkWidget *ev_attachbar;
610 ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
616 ev_sidebar_attachments_set_document (EvSidebarPage *page,
617 EvDocument *document)
619 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (page);
620 GList *attachments = NULL;
623 if (!ev_document_has_attachments (document))
626 if (!ev_attachbar->priv->icon_theme) {
629 screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
630 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
631 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_theme),
633 G_CALLBACK (ev_sidebar_attachments_update_icons),
634 (gpointer) ev_attachbar);
637 attachments = ev_document_get_attachments (document);
639 gtk_list_store_clear (ev_attachbar->priv->model);
641 for (l = attachments; l && l->data; l = g_list_next (l)) {
642 EvAttachment *attachment;
644 GdkPixbuf *pixbuf = NULL;
645 const gchar *mime_type;
647 attachment = EV_ATTACHMENT (l->data);
649 mime_type = ev_attachment_get_mime_type (attachment);
650 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
653 gtk_list_store_append (ev_attachbar->priv->model, &iter);
654 gtk_list_store_set (ev_attachbar->priv->model, &iter,
655 COLUMN_NAME, ev_attachment_get_name (attachment),
657 COLUMN_ATTACHMENT, attachment,
660 g_object_unref (attachment);
663 g_list_free (attachments);
667 ev_sidebar_attachments_support_document (EvSidebarPage *sidebar_page,
668 EvDocument *document)
670 return ev_document_has_attachments (document);
674 ev_sidebar_attachments_get_label (EvSidebarPage *sidebar_page)
676 return _("Attachments");
680 ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface)
682 iface->support_document = ev_sidebar_attachments_support_document;
683 iface->set_document = ev_sidebar_attachments_set_document;
684 iface->get_label = ev_sidebar_attachments_get_label;