30 #include <glib/gi18n.h>
31 #include <glib/gstdio.h>
74 ev_sidebar_attachments,
80 #define EV_SIDEBAR_ATTACHMENTS_GET_PRIVATE(object) \
81 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_ATTACHMENTS, EvSidebarAttachmentsPrivate))
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),
100 const gchar *mime_type)
102 const char *separator;
106 separator =
strchr (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);
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))
144 if (GDK_IS_PIXBUF (pixbuf))
145 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
157 GdkPixbuf *pixbuf = NULL;
162 ev_sidebar_attachments_icon_cache_add (ev_attachbar,
172 g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
182 GtkTreePath *path = 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,
198 gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
201 gtk_tree_path_free (path);
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)) {
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,
246 attach_list = g_list_prepend (attach_list, attachment);
248 gtk_tree_path_free (path);
251 g_list_free (selected);
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;
297 gtk_widget_get_screen (GTK_WIDGET (ev_attachbar)),
302 g_warning (
"%s", error->message);
303 g_error_free (error);
306 g_object_unref (attachment);
327 valid = gtk_tree_model_get_iter_first (
328 GTK_TREE_MODEL (ev_attachbar->priv->model),
333 GdkPixbuf *pixbuf = NULL;
334 const gchar *mime_type;
336 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
343 g_object_unref (attachment);
348 gtk_list_store_set (ev_attachbar->priv->model, &iter,
352 valid = gtk_tree_model_iter_next (
353 GTK_TREE_MODEL (ev_attachbar->priv->model),
360 GdkScreen *old_screen)
365 if (!ev_attachbar->priv->icon_theme)
368 screen = gtk_widget_get_screen (widget);
369 if (screen == old_screen)
373 g_signal_handlers_disconnect_by_func (
374 gtk_icon_theme_get_for_screen (old_screen),
379 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
380 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
383 (gpointer) ev_attachbar);
385 if (GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed) {
386 GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed (widget, old_screen);
392 GdkDragContext *drag_context,
393 GtkSelectionData *data,
399 GList *selected = NULL, *l;
403 selected = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (ev_attachbar->priv->icon_view));
407 uris = g_ptr_array_new ();
409 for (l = selected; l && l->data; l = g_list_next (l)) {
415 GError *error = NULL;
417 path = (GtkTreePath *) l->data;
419 gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
421 gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
433 uri = g_file_get_uri (file);
434 g_ptr_array_add (uris, uri);
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 g_ptr_array_add (uris, NULL);
448 uri_list = (
char **) g_ptr_array_free (uris,
FALSE);
449 gtk_selection_data_set_uris (data, uri_list);
450 g_strfreev (uri_list);
452 g_list_free (selected);
467 g_value_set_object (value, ev_sidebar_attachments->priv->icon_view);
470 G_OBJECT_WARN_INVALID_PROPERTY_ID (
object, prop_id, pspec);
480 if (ev_attachbar->priv->icon_theme) {
481 g_signal_handlers_disconnect_by_func (
482 ev_attachbar->priv->icon_theme,
485 ev_attachbar->priv->icon_theme = NULL;
488 if (ev_attachbar->priv->model) {
489 g_object_unref (ev_attachbar->priv->model);
490 ev_attachbar->priv->model = NULL;
493 if (ev_attachbar->priv->icon_cache) {
494 g_hash_table_destroy (ev_attachbar->priv->icon_cache);
495 ev_attachbar->priv->icon_cache = NULL;
498 G_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->dispose (
object);
504 GObjectClass *g_object_class;
505 GtkWidgetClass *gtk_widget_class;
507 g_object_class = G_OBJECT_CLASS (ev_attachbar_class);
508 gtk_widget_class = GTK_WIDGET_CLASS (ev_attachbar_class);
519 g_signal_new (
"popup",
520 G_TYPE_FROM_CLASS (g_object_class),
521 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
524 g_cclosure_marshal_VOID__POINTER,
528 g_object_class_override_property (g_object_class,
540 swindow = gtk_scrolled_window_new (NULL, NULL);
541 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
543 GTK_POLICY_AUTOMATIC);
545 ev_attachbar->priv->model = gtk_list_store_new (
N_COLS,
552 ev_attachbar->priv->icon_view =
553 gtk_icon_view_new_with_model (GTK_TREE_MODEL (ev_attachbar->priv->model));
554 gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
555 GTK_SELECTION_MULTIPLE);
556 gtk_icon_view_set_columns (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), -1);
557 g_object_set (G_OBJECT (ev_attachbar->priv->icon_view),
561 g_signal_connect_swapped (ev_attachbar->priv->icon_view,
562 "button-press-event",
564 (gpointer) ev_attachbar);
566 gtk_container_add (GTK_CONTAINER (swindow),
567 ev_attachbar->priv->icon_view);
569 gtk_box_pack_start (GTK_BOX (ev_attachbar), swindow,
TRUE,
TRUE, 0);
570 gtk_widget_show_all (GTK_WIDGET (ev_attachbar));
573 ev_attachbar->priv->icon_theme = NULL;
576 ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
582 gtk_icon_view_enable_model_drag_source (
583 GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
587 gtk_drag_source_add_uri_targets (ev_attachbar->priv->icon_view);
589 g_signal_connect (ev_attachbar->priv->icon_view,
592 (gpointer) ev_attachbar);
598 GtkWidget *ev_attachbar;
601 "orientation", GTK_ORIENTATION_VERTICAL,
613 for (l = job->
attachments; l && l->data; l = g_list_next (l)) {
616 GdkPixbuf *pixbuf = NULL;
617 const gchar *mime_type;
625 gtk_list_store_append (ev_attachbar->priv->model, &iter);
626 gtk_list_store_set (ev_attachbar->priv->model, &iter,
633 g_object_unref (job);
651 if (!ev_attachbar->priv->icon_theme) {
654 screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
655 ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
656 g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
659 (gpointer) ev_attachbar);
662 gtk_list_store_clear (ev_attachbar->priv->model);
665 g_signal_connect (job,
"finished",
668 g_signal_connect (job,
"cancelled",
669 G_CALLBACK (g_object_unref),
679 g_signal_connect (model,
"notify::document",
695 return _(
"Attachments");