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.
28 #include <glib/gi18n.h>
29 #include <glib/gstdio.h>
32 #include <libgnomeui/gnome-icon-lookup.h>
34 #include "ev-sidebar-attachments.h"
35 #include "ev-sidebar-page.h"
55 static const GtkTargetEntry drag_targets[] = {
56 { "text/uri-list", 0, 0 }
59 static guint signals[N_SIGNALS];
61 struct _EvSidebarAttachmentsPrivate {
66 GtkIconTheme *icon_theme;
67 GHashTable *icon_cache;
70 static void ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface);
72 G_DEFINE_TYPE_EXTENDED (EvSidebarAttachments,
73 ev_sidebar_attachments,
76 G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
77 ev_sidebar_attachments_page_iface_init))
79 #define EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE(object) \
80 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_ATTACHMENTS, EvSidebarAttachmentsPrivate))
84 ev_sidebar_attachments_icon_cache_add (EvSidebarAttachments *ev_attachbar,
85 const gchar *mime_type,
86 const GdkPixbuf *pixbuf)
88 g_assert (mime_type != NULL);
89 g_assert (GDK_IS_PIXBUF (pixbuf));
91 g_hash_table_insert (ev_attachbar->priv->icon_cache,
92 (gpointer)g_strdup (mime_type),
98 icon_theme_get_pixbuf_from_mime_type (GtkIconTheme *icon_theme,
99 const gchar *mime_type)
101 GdkPixbuf *pixbuf = NULL;
104 icon = gnome_icon_lookup (icon_theme,
108 GNOME_ICON_LOOKUP_FLAGS_NONE,
111 pixbuf = gtk_icon_theme_load_icon (icon_theme,
119 ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
120 const gchar *mime_type)
122 GdkPixbuf *pixbuf = NULL;
124 g_assert (mime_type != NULL);
126 pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
129 if (GDK_IS_PIXBUF (pixbuf))
132 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
135 if (GDK_IS_PIXBUF (pixbuf))
136 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
144 icon_cache_update_icon (gchar *key,
146 EvSidebarAttachments *ev_attachbar)
148 GdkPixbuf *pixbuf = NULL;
150 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
153 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
161 ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
163 g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
164 (GHRFunc) icon_cache_update_icon,
168 static EvAttachment *
169 ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
173 GtkTreePath *path = NULL;
175 EvAttachment *attachment = NULL;
177 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
183 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
185 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
186 COLUMN_ATTACHMENT, &attachment,
189 gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
192 gtk_tree_path_free (path);
198 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
202 GtkIconView *icon_view;
204 GList *selected = NULL, *l;
205 GList *attach_list = NULL;
207 icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
209 path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
213 if (!gtk_icon_view_path_is_selected (icon_view, path)) {
214 gtk_icon_view_unselect_all (icon_view);
215 gtk_icon_view_select_path (icon_view, path);
218 gtk_tree_path_free (path);
220 selected = gtk_icon_view_get_selected_items (icon_view);
224 for (l = selected; l && l->data; l = g_list_next (l)) {
226 EvAttachment *attachment = NULL;
228 path = (GtkTreePath *) l->data;
230 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
232 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
233 COLUMN_ATTACHMENT, &attachment,
237 attach_list = g_list_prepend (attach_list, attachment);
239 gtk_tree_path_free (path);
242 g_list_free (selected);
247 g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
253 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
255 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
258 gtk_widget_get_pointer (widget, &x, &y);
260 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
264 ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
265 GdkEventButton *event,
266 GtkWidget *icon_view)
268 if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
269 gtk_widget_grab_focus (icon_view);
272 if (event->button == 2)
275 switch (event->button) {
277 if (event->type == GDK_2BUTTON_PRESS) {
278 GError *error = NULL;
279 EvAttachment *attachment;
281 attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
287 ev_attachment_open (attachment, &error);
290 g_warning (error->message);
291 g_error_free (error);
294 g_object_unref (attachment);
300 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
307 ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
313 ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
315 valid = gtk_tree_model_get_iter_first (
316 GTK_TREE_MODEL (ev_attachbar->priv->model),
320 EvAttachment *attachment = NULL;
321 GdkPixbuf *pixbuf = NULL;
322 const gchar *mime_type;
324 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
325 COLUMN_ATTACHMENT, &attachment,
328 mime_type = ev_attachment_get_mime_type (attachment);
331 g_object_unref (attachment);
333 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
336 gtk_list_store_set (ev_attachbar->priv->model, &iter,
340 valid = gtk_tree_model_iter_next (
341 GTK_TREE_MODEL (ev_attachbar->priv->model),
347 ev_sidebar_attachments_screen_changed (GtkWidget *widget,
348 GdkScreen *old_screen)
350 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
353 if (!ev_attachbar->priv->icon_theme)
356 screen = gtk_widget_get_screen (widget);
357 if (screen == old_screen)
361 g_signal_handlers_disconnect_by_func (
362 gtk_icon_theme_get_for_screen (old_screen),
363 G_CALLBACK (ev_sidebar_attachments_update_icons),
367 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
368 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
370 G_CALLBACK (ev_sidebar_attachments_update_icons),
371 (gpointer) ev_attachbar);
373 if (GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed) {
374 GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed (widget, old_screen);
379 ev_sidebar_attachments_drag_data_get (GtkWidget *widget,
380 GdkDragContext *drag_context,
381 GtkSelectionData *data,
386 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (user_data);
389 GList *selected = NULL, *l;
391 selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
395 uri_list = g_string_new (NULL);
397 for (l = selected; l && l->data; l = g_list_next (l)) {
398 EvAttachment *attachment;
401 gchar *uri, *filename;
402 GError *error = NULL;
404 path = (GtkTreePath *) l->data;
406 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
408 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
409 COLUMN_ATTACHMENT, &attachment,
412 filename = g_build_filename (g_get_tmp_dir (),
413 ev_attachment_get_name (attachment),
416 uri = g_filename_to_uri (filename, NULL, NULL);
418 if (ev_attachment_save (attachment, filename, &error)) {
419 g_string_append (uri_list, uri);
420 g_string_append_c (uri_list, '\n');
424 g_warning (error->message);
425 g_error_free (error);
429 gtk_tree_path_free (path);
430 g_object_unref (attachment);
433 uris = g_string_free (uri_list, FALSE);
436 gtk_selection_data_set (data,
443 g_list_free (selected);
447 ev_sidebar_attachments_get_property (GObject *object,
452 EvSidebarAttachments *ev_sidebar_attachments;
454 ev_sidebar_attachments = EV_SIDEBAR_ATTACHMENTS (object);
458 g_value_set_object (value, ev_sidebar_attachments->priv->icon_view);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
467 ev_sidebar_attachments_destroy (GtkObject *object)
469 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (object);
471 if (ev_attachbar->priv->icon_theme) {
472 g_signal_handlers_disconnect_by_func (
473 ev_attachbar->priv->icon_theme,
474 G_CALLBACK (ev_sidebar_attachments_update_icons),
476 ev_attachbar->priv->icon_theme = NULL;
479 if (ev_attachbar->priv->model) {
480 g_object_unref (ev_attachbar->priv->model);
481 ev_attachbar->priv->model = NULL;
484 if (ev_attachbar->priv->icon_cache) {
485 g_hash_table_destroy (ev_attachbar->priv->icon_cache);
486 ev_attachbar->priv->icon_cache = NULL;
489 (* GTK_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->destroy) (object);
493 ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class)
495 GObjectClass *g_object_class;
496 GtkObjectClass *gtk_object_class;
497 GtkWidgetClass *gtk_widget_class;
499 g_object_class = G_OBJECT_CLASS (ev_attachbar_class);
500 gtk_object_class = GTK_OBJECT_CLASS (ev_attachbar_class);
501 gtk_widget_class = GTK_WIDGET_CLASS (ev_attachbar_class);
503 g_object_class->get_property = ev_sidebar_attachments_get_property;
504 gtk_object_class->destroy = ev_sidebar_attachments_destroy;
505 gtk_widget_class->popup_menu = ev_sidebar_attachments_popup_menu;
506 gtk_widget_class->screen_changed = ev_sidebar_attachments_screen_changed;
508 g_type_class_add_private (g_object_class, sizeof (EvSidebarAttachmentsPrivate));
511 signals[SIGNAL_POPUP_MENU] =
512 g_signal_new ("popup",
513 G_TYPE_FROM_CLASS (g_object_class),
514 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
515 G_STRUCT_OFFSET (EvSidebarAttachmentsClass, popup_menu),
517 g_cclosure_marshal_VOID__POINTER,
521 g_object_class_override_property (g_object_class,
527 ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
531 ev_attachbar->priv = EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE (ev_attachbar);
533 swindow = gtk_scrolled_window_new (NULL, NULL);
534 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
536 GTK_POLICY_AUTOMATIC);
537 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
540 ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
547 ev_attachbar->priv->icon_view =
548 gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
549 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
550 GTK_SELECTION_MULTIPLE);
551 gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
552 g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
553 "text-column", COLUMN_NAME,
554 "pixbuf-column", COLUMN_ICON,
556 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_view),
557 "button-press-event",
558 G_CALLBACK (ev_sidebar_attachments_button_press),
559 (gpointer) ev_attachbar);
561 gtk_container_add (GTK_CONTAINER (swindow),
562 ev_attachbar->priv->icon_view);
564 gtk_container_add (GTK_CONTAINER (ev_attachbar),
566 gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
569 ev_attachbar->priv->icon_theme = NULL;
572 ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
578 gtk_icon_view_enable_model_drag_source (
579 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
582 G_N_ELEMENTS (drag_targets),
585 g_signal_connect (G_OBJECT (ev_attachbar->priv->icon_view),
587 G_CALLBACK (ev_sidebar_attachments_drag_data_get),
588 (gpointer) ev_attachbar);
592 ev_sidebar_attachments_new (void)
594 GtkWidget *ev_attachbar;
596 ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
602 ev_sidebar_attachments_set_document (EvSidebarPage *page,
603 EvDocument *document)
605 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (page);
606 GList *attachments = NULL;
609 if (!ev_document_has_attachments (document))
612 if (!ev_attachbar->priv->icon_theme) {
615 screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
616 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
617 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_theme),
619 G_CALLBACK (ev_sidebar_attachments_update_icons),
620 (gpointer) ev_attachbar);
623 attachments = ev_document_get_attachments (document);
625 gtk_list_store_clear (ev_attachbar->priv->model);
627 for (l = attachments; l && l->data; l = g_list_next (l)) {
628 EvAttachment *attachment;
630 GdkPixbuf *pixbuf = NULL;
631 const gchar *mime_type;
633 attachment = EV_ATTACHMENT (l->data);
635 mime_type = ev_attachment_get_mime_type (attachment);
636 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
639 gtk_list_store_append (ev_attachbar->priv->model, &iter);
640 gtk_list_store_set (ev_attachbar->priv->model, &iter,
641 COLUMN_NAME, ev_attachment_get_name (attachment),
643 COLUMN_ATTACHMENT, attachment,
646 g_object_unref (attachment);
649 g_list_free (attachments);
653 ev_sidebar_attachments_support_document (EvSidebarPage *sidebar_page,
654 EvDocument *document)
656 return ev_document_has_attachments (document);
660 ev_sidebar_attachments_get_label (EvSidebarPage *sidebar_page)
662 return _("Attachments");
666 ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface)
668 iface->support_document = ev_sidebar_attachments_support_document;
669 iface->set_document = ev_sidebar_attachments_set_document;
670 iface->get_label = ev_sidebar_attachments_get_label;