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"
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 GdkPixbuf *pixbuf = NULL;
105 icon = gnome_icon_lookup (icon_theme,
109 GNOME_ICON_LOOKUP_FLAGS_NONE,
112 pixbuf = gtk_icon_theme_load_icon (icon_theme,
120 ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
121 const gchar *mime_type)
123 GdkPixbuf *pixbuf = NULL;
125 g_assert (mime_type != NULL);
127 pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
130 if (GDK_IS_PIXBUF (pixbuf))
133 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
136 if (GDK_IS_PIXBUF (pixbuf))
137 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
145 icon_cache_update_icon (gchar *key,
147 EvSidebarAttachments *ev_attachbar)
149 GdkPixbuf *pixbuf = NULL;
151 pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
154 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
162 ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
164 g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
165 (GHRFunc) icon_cache_update_icon,
169 static EvAttachment *
170 ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
174 GtkTreePath *path = NULL;
176 EvAttachment *attachment = NULL;
178 path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
184 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
186 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
187 COLUMN_ATTACHMENT, &attachment,
190 gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
193 gtk_tree_path_free (path);
199 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
203 GtkIconView *icon_view;
205 GList *selected = NULL, *l;
206 GList *attach_list = NULL;
208 icon_view = GTK_ICON_VIEW (ev_attachbar->priv->icon_view);
210 path = gtk_icon_view_get_path_at_pos (icon_view, x, y);
214 if (!gtk_icon_view_path_is_selected (icon_view, path)) {
215 gtk_icon_view_unselect_all (icon_view);
216 gtk_icon_view_select_path (icon_view, path);
219 gtk_tree_path_free (path);
221 selected = gtk_icon_view_get_selected_items (icon_view);
225 for (l = selected; l && l->data; l = g_list_next (l)) {
227 EvAttachment *attachment = NULL;
229 path = (GtkTreePath *) l->data;
231 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
233 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
234 COLUMN_ATTACHMENT, &attachment,
238 attach_list = g_list_prepend (attach_list, attachment);
240 gtk_tree_path_free (path);
243 g_list_free (selected);
248 g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
254 ev_sidebar_attachments_popup_menu (GtkWidget *widget)
256 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
259 gtk_widget_get_pointer (widget, &x, &y);
261 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
265 ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
266 GdkEventButton *event,
267 GtkWidget *icon_view)
269 if (!GTK_WIDGET_HAS_FOCUS (icon_view)) {
270 gtk_widget_grab_focus (icon_view);
273 if (event->button == 2)
276 switch (event->button) {
278 if (event->type == GDK_2BUTTON_PRESS) {
279 GError *error = NULL;
280 EvAttachment *attachment;
282 attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
288 ev_attachment_open (attachment, &error);
291 g_warning (error->message);
292 g_error_free (error);
295 g_object_unref (attachment);
301 return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
308 ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
314 ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
316 valid = gtk_tree_model_get_iter_first (
317 GTK_TREE_MODEL (ev_attachbar->priv->model),
321 EvAttachment *attachment = NULL;
322 GdkPixbuf *pixbuf = NULL;
323 const gchar *mime_type;
325 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
326 COLUMN_ATTACHMENT, &attachment,
329 mime_type = ev_attachment_get_mime_type (attachment);
332 g_object_unref (attachment);
334 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
337 gtk_list_store_set (ev_attachbar->priv->model, &iter,
341 valid = gtk_tree_model_iter_next (
342 GTK_TREE_MODEL (ev_attachbar->priv->model),
348 ev_sidebar_attachments_drag_data_get (GtkWidget *widget,
349 GdkDragContext *drag_context,
350 GtkSelectionData *data,
355 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (user_data);
358 GList *selected = NULL, *l;
360 selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
364 uri_list = g_string_new (NULL);
366 for (l = selected; l && l->data; l = g_list_next (l)) {
367 EvAttachment *attachment;
370 gchar *uri, *filename;
371 GError *error = NULL;
373 path = (GtkTreePath *) l->data;
375 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
377 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
378 COLUMN_ATTACHMENT, &attachment,
381 filename = g_build_filename (g_get_tmp_dir (),
382 ev_attachment_get_name (attachment),
385 uri = g_filename_to_uri (filename, NULL, NULL);
387 if (ev_attachment_save (attachment, filename, &error)) {
388 g_string_append (uri_list, uri);
389 g_string_append_c (uri_list, '\n');
393 g_warning (error->message);
394 g_error_free (error);
398 gtk_tree_path_free (path);
399 g_object_unref (attachment);
402 uris = g_string_free (uri_list, FALSE);
405 gtk_selection_data_set (data,
412 g_list_free (selected);
416 ev_sidebar_attachments_get_property (GObject *object,
421 EvSidebarAttachments *ev_sidebar_attachments;
423 ev_sidebar_attachments = EV_SIDEBAR_ATTACHMENTS (object);
428 g_value_set_object (value, ev_sidebar_attachments->priv->icon_view);
431 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
437 ev_sidebar_attachments_destroy (GtkObject *object)
439 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (object);
441 if (ev_attachbar->priv->model) {
442 g_object_unref (ev_attachbar->priv->model);
443 ev_attachbar->priv->model = NULL;
446 if (ev_attachbar->priv->icon_cache) {
447 g_hash_table_destroy (ev_attachbar->priv->icon_cache);
448 ev_attachbar->priv->icon_cache = NULL;
451 (* GTK_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->destroy) (object);
455 ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class)
457 GObjectClass *g_object_class;
458 GtkObjectClass *gtk_object_class;
459 GtkWidgetClass *gtk_widget_class;
461 g_object_class = G_OBJECT_CLASS (ev_attachbar_class);
462 gtk_object_class = GTK_OBJECT_CLASS (ev_attachbar_class);
463 gtk_widget_class = GTK_WIDGET_CLASS (ev_attachbar_class);
465 g_object_class->get_property = ev_sidebar_attachments_get_property;
466 gtk_object_class->destroy = ev_sidebar_attachments_destroy;
467 gtk_widget_class->popup_menu = ev_sidebar_attachments_popup_menu;
469 g_type_class_add_private (g_object_class, sizeof (EvSidebarAttachmentsPrivate));
472 signals[SIGNAL_POPUP_MENU] =
473 g_signal_new ("popup",
474 G_TYPE_FROM_CLASS (g_object_class),
475 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
476 G_STRUCT_OFFSET (EvSidebarAttachmentsClass, popup_menu),
478 g_cclosure_marshal_VOID__POINTER,
482 g_object_class_override_property (g_object_class,
488 ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
492 ev_attachbar->priv = EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE (ev_attachbar);
494 swindow = gtk_scrolled_window_new (NULL, NULL);
495 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
497 GTK_POLICY_AUTOMATIC);
498 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
501 ev_attachbar->priv->model = gtk_list_store_new (N_COLS,
508 ev_attachbar->priv->icon_view =
509 gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
510 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
511 GTK_SELECTION_MULTIPLE);
512 gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
513 g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
514 "text-column", COLUMN_NAME,
515 "pixbuf-column", COLUMN_ICON,
517 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_view),
518 "button-press-event",
519 G_CALLBACK (ev_sidebar_attachments_button_press),
520 (gpointer) ev_attachbar);
522 gtk_container_add (GTK_CONTAINER (swindow),
523 ev_attachbar->priv->icon_view);
525 gtk_container_add (GTK_CONTAINER (ev_attachbar),
527 gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
529 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_default ();
530 g_signal_connect_swapped (G_OBJECT (ev_attachbar->priv->icon_theme),
532 G_CALLBACK (ev_sidebar_attachments_update_icons),
533 (gpointer) ev_attachbar);
536 ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
542 gtk_icon_view_enable_model_drag_source (
543 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
546 G_N_ELEMENTS (drag_targets),
549 g_signal_connect (G_OBJECT (ev_attachbar->priv->icon_view),
551 G_CALLBACK (ev_sidebar_attachments_drag_data_get),
552 (gpointer) ev_attachbar);
556 ev_sidebar_attachments_new (void)
558 GtkWidget *ev_attachbar;
560 ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL);
566 ev_sidebar_attachments_set_document (EvSidebarPage *page,
567 EvDocument *document)
569 EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (page);
570 GList *attachments = NULL;
573 if (!ev_document_has_attachments (document))
576 attachments = ev_document_get_attachments (document);
578 gtk_list_store_clear (ev_attachbar->priv->model);
580 for (l = attachments; l && l->data; l = g_list_next (l)) {
581 EvAttachment *attachment;
583 GdkPixbuf *pixbuf = NULL;
584 const gchar *mime_type;
586 attachment = EV_ATTACHMENT (l->data);
588 mime_type = ev_attachment_get_mime_type (attachment);
589 pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
592 gtk_list_store_append (ev_attachbar->priv->model, &iter);
593 gtk_list_store_set (ev_attachbar->priv->model, &iter,
594 COLUMN_NAME, ev_attachment_get_name (attachment),
596 COLUMN_ATTACHMENT, attachment,
599 g_object_unref (attachment);
602 g_list_free (attachments);
606 ev_sidebar_attachments_support_document (EvSidebarPage *sidebar_page,
607 EvDocument *document)
609 return ev_document_has_attachments (document);
613 ev_sidebar_attachments_get_label (EvSidebarPage *sidebar_page)
615 return _("Attachments");
619 ev_sidebar_attachments_page_iface_init (EvSidebarPageIface *iface)
621 iface->support_document = ev_sidebar_attachments_support_document;
622 iface->set_document = ev_sidebar_attachments_set_document;
623 iface->get_label = ev_sidebar_attachments_get_label;