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>
33 #include "ev-file-helpers.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 const char *separator;
105 separator = strchr (mime_type, '/');
107 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
109 icon_name = g_string_new ("gnome-mime-");
110 g_string_append_len (icon_name, mime_type, separator - mime_type);
111 g_string_append_c (icon_name, '-');
112 g_string_append (icon_name, separator + 1);
113 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
114 g_string_free (icon_name, TRUE);
118 icon_name = g_string_new ("gnome-mime-");
119 g_string_append_len (icon_name, mime_type, separator - mime_type);
120 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
121 g_string_free (icon_name, TRUE);
127 ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
128 const gchar *mime_type)
130 GdkPixbuf *pixbuf = NULL;
132 g_assert (mime_type != NULL);
134 pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
137 if (GDK_IS_PIXBUF (pixbuf))
140 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
143 if (GDK_IS_PIXBUF (pixbuf))
144 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
152 icon_cache_update_icon (gchar *key,
154 EvSidebarAttachments *ev_attachbar)
156 GdkPixbuf *pixbuf = NULL;
158 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
161 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
169 ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
171 g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
172 (GHRFunc) icon_cache_update_icon,
176 static EvAttachment *
177 ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
181 GtkTreePath *path = NULL;
183 EvAttachment *attachment = NULL;
185 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
191 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
193 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
194 COLUMN_ATTACHMENT, &attachment,
197 gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
200 gtk_tree_path_free (path);
206 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
210 GtkIconView *icon_view;
212 GList *selected = NULL, *l;
213 GList *attach_list = NULL;
215 icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
217 path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
221 if (!gtk_icon_view_path_is_selected (icon_view, path)) {
222 gtk_icon_view_unselect_all (icon_view);
223 gtk_icon_view_select_path (icon_view, path);
226 gtk_tree_path_free (path);
228 selected = gtk_icon_view_get_selected_items (icon_view);
232 for (l = selected; l && l->data; l = g_list_next (l)) {
234 EvAttachment *attachment = NULL;
236 path = (GtkTreePath *) l->data;
238 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
240 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
241 COLUMN_ATTACHMENT, &attachment,
245 attach_list = g_list_prepend (attach_list, attachment);
247 gtk_tree_path_free (path);
250 g_list_free (selected);
255 g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
261 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
263 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
266 gtk_widget_get_pointer (widget, &x, &y);
268 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
272 ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
273 GdkEventButton *event,
274 GtkWidget *icon_view)
276 if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
277 gtk_widget_grab_focus (icon_view);
280 if (event->button == 2)
283 switch (event->button) {
285 if (event->type == GDK_2BUTTON_PRESS) {
286 GError *error = NULL;
287 EvAttachment *attachment;
289 attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
295 ev_attachment_open (attachment, &error);
298 g_warning (error->message);
299 g_error_free (error);
302 g_object_unref (attachment);
308 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
315 ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
321 ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
323 valid = gtk_tree_model_get_iter_first (
324 GTK_TREE_MODEL (ev_attachbar->priv->model),
328 EvAttachment *attachment = NULL;
329 GdkPixbuf *pixbuf = NULL;
330 const gchar *mime_type;
332 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
333 COLUMN_ATTACHMENT, &attachment,
336 mime_type = ev_attachment_get_mime_type (attachment);
339 g_object_unref (attachment);
341 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
344 gtk_list_store_set (ev_attachbar->priv->model, &iter,
348 valid = gtk_tree_model_iter_next (
349 GTK_TREE_MODEL (ev_attachbar->priv->model),
355 ev_sidebar_attachments_screen_changed (GtkWidget *widget,
356 GdkScreen *old_screen)
358 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
361 if (!ev_attachbar->priv->icon_theme)
364 screen = gtk_widget_get_screen (widget);
365 if (screen == old_screen)
369 g_signal_handlers_disconnect_by_func (
370 gtk_icon_theme_get_for_screen (old_screen),
371 G_CALLBACK (ev_sidebar_attachments_update_icons),
375 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
376 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
378 G_CALLBACK (ev_sidebar_attachments_update_icons),
379 (gpointer) ev_attachbar);
381 if (GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed) {
382 GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed (widget, old_screen);
387 ev_sidebar_attachments_drag_data_get (GtkWidget *widget,
388 GdkDragContext *drag_context,
389 GtkSelectionData *data,
394 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (user_data);
397 GList *selected = NULL, *l;
399 selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
403 uri_list = g_string_new (NULL);
405 for (l = selected; l && l->data; l = g_list_next (l)) {
406 EvAttachment *attachment;
409 gchar *uri, *filename;
410 GError *error = NULL;
412 path = (GtkTreePath *) l->data;
414 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
416 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
417 COLUMN_ATTACHMENT, &attachment,
420 filename = g_build_filename (ev_tmp_dir (),
421 ev_attachment_get_name (attachment),
424 uri = g_filename_to_uri (filename, NULL, NULL);
426 if (ev_attachment_save (attachment, filename, &error)) {
427 g_string_append (uri_list, uri);
428 g_string_append_c (uri_list, '\n');
432 g_warning (error->message);
433 g_error_free (error);
437 gtk_tree_path_free (path);
438 g_object_unref (attachment);
441 uris = g_string_free (uri_list, FALSE);
444 gtk_selection_data_set (data,
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)
539 ev_attachbar->priv = EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE (ev_attachbar);
541 swindow = gtk_scrolled_window_new (NULL, NULL);
542 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
544 GTK_POLICY_AUTOMATIC);
545 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
548 ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
555 ev_attachbar->priv->icon_view =
556 gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
557 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
558 GTK_SELECTION_MULTIPLE);
559 gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
560 g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
561 "text-column", COLUMN_NAME,
562 "pixbuf-column", COLUMN_ICON,
564 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_view),
565 "button-press-event",
566 G_CALLBACK (ev_sidebar_attachments_button_press),
567 (gpointer) ev_attachbar);
569 gtk_container_add (GTK_CONTAINER (swindow),
570 ev_attachbar->priv->icon_view);
572 gtk_container_add (GTK_CONTAINER (ev_attachbar),
574 gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
577 ev_attachbar->priv->icon_theme = NULL;
580 ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
586 gtk_icon_view_enable_model_drag_source (
587 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
590 G_N_ELEMENTS (drag_targets),
593 g_signal_connect (G_OBJECT (ev_attachbar->priv->icon_view),
595 G_CALLBACK (ev_sidebar_attachments_drag_data_get),
596 (gpointer) ev_attachbar);
600 ev_sidebar_attachments_new (void)
602 GtkWidget *ev_attachbar;
604 ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
610 ev_sidebar_attachments_set_document (EvSidebarPage *page,
611 EvDocument *document)
613 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (page);
614 GList *attachments = NULL;
617 if (!ev_document_has_attachments (document))
620 if (!ev_attachbar->priv->icon_theme) {
623 screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
624 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
625 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_theme),
627 G_CALLBACK (ev_sidebar_attachments_update_icons),
628 (gpointer) ev_attachbar);
631 attachments = ev_document_get_attachments (document);
633 gtk_list_store_clear (ev_attachbar->priv->model);
635 for (l = attachments; l && l->data; l = g_list_next (l)) {
636 EvAttachment *attachment;
638 GdkPixbuf *pixbuf = NULL;
639 const gchar *mime_type;
641 attachment = EV_ATTACHMENT (l->data);
643 mime_type = ev_attachment_get_mime_type (attachment);
644 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
647 gtk_list_store_append (ev_attachbar->priv->model, &iter);
648 gtk_list_store_set (ev_attachbar->priv->model, &iter,
649 COLUMN_NAME, ev_attachment_get_name (attachment),
651 COLUMN_ATTACHMENT, attachment,
654 g_object_unref (attachment);
657 g_list_free (attachments);
661 ev_sidebar_attachments_support_document (EvSidebarPage *sidebar_page,
662 EvDocument *document)
664 return ev_document_has_attachments (document);
668 ev_sidebar_attachments_get_label (EvSidebarPage *sidebar_page)
670 return _("Attachments");
674 ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface)
676 iface->support_document = ev_sidebar_attachments_support_document;
677 iface->set_document = ev_sidebar_attachments_set_document;
678 iface->get_label = ev_sidebar_attachments_get_label;