commit 1bcb256cc6a3e010604ecad672fb0c86410c54f2
Author: Matthew Barnes <mbarnes@redhat.com>
Date:   Mon May 4 23:34:48 2009 -0400

    Rewrite the Evolution plugin for the new attachment API.

diff --git a/evolution/Makefile.am b/evolution/Makefile.am
index a216ed0..765b5a2 100644
--- a/evolution/Makefile.am
+++ b/evolution/Makefile.am
@@ -7,8 +7,11 @@ INCLUDES =						\
 plugin_DATA = org-gnome-evolution-send-attachments-to.eplug
 plugin_LTLIBRARIES = liborg-gnome-evolution-send-attachments-to.la
 
-liborg_gnome_evolution_send_attachments_to_la_SOURCES = nautilus-sendto.c
-liborg_gnome_evolution_send_attachments_to_la_LDFLAGS = -module -avoid-version $(NO_UNDEFINED)
+liborg_gnome_evolution_send_attachments_to_la_SOURCES = \
+	e-attachment-handler-sendto.c			\
+	e-attachment-handler-sendto.h
+liborg_gnome_evolution_send_attachments_to_la_LDFLAGS = \
+	-module -avoid-version $(NO_UNDEFINED)
 liborg_gnome_evolution_send_attachments_to_la_LIBADD = $(EVOLUTION_LIBS)
 
 EXTRA_DIST = org-gnome-evolution-send-attachments-to.eplug.xml plugin.mk
diff --git a/evolution/e-attachment-handler-sendto.c b/evolution/e-attachment-handler-sendto.c
new file mode 100644
index 0000000..dd36652
--- /dev/null
+++ b/evolution/e-attachment-handler-sendto.c
@@ -0,0 +1,258 @@
+/*
+ * e-attachment-handler-sendto.c
+ *
+ * Copyright (C) 2009 Matthew Barnes
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#include "e-attachment-handler-sendto.h"
+
+#include <config.h>
+#include <errno.h>
+
+#include <glib/gi18n-lib.h>
+#include <shell/es-event.h>
+#include <e-util/e-mktemp.h>
+
+static gpointer parent_class;
+
+static const gchar *ui =
+"<ui>"
+"  <popup name='context'>"
+"    <placeholder name='custom-actions'>"
+"      <menuitem action='sendto'/>"
+"    </placeholder>"
+"  </popup>"
+"</ui>";
+
+void init (EPlugin *ep, ESEventTargetShell *target);
+
+static void
+sendto_save_finished_cb (EAttachment *attachment,
+                         GAsyncResult *result,
+                         EAttachmentHandler *handler)
+{
+	EAttachmentView *view;
+	EAttachmentStore *store;
+	GtkWidget *dialog;
+	gchar **uris;
+	gpointer parent;
+	gchar *arguments;
+	gchar *command_line;
+	guint n_uris = 1;
+	GError *error = NULL;
+
+	view = e_attachment_handler_get_view (handler);
+	store = e_attachment_view_get_store (view);
+
+	uris = e_attachment_store_get_uris_finish (store, result, &error);
+
+	if (uris != NULL)
+		n_uris = g_strv_length (uris);
+
+	if (error != NULL)
+		goto error;
+
+	arguments = g_strjoinv (" ", uris);
+	command_line = g_strdup_printf ("nautilus-sendto %s", arguments);
+
+	g_message ("Spawning: %s", command_line);
+	g_spawn_command_line_async (command_line, &error);
+
+	g_free (command_line);
+	g_free (arguments);
+
+	if (error != NULL)
+		goto error;
+
+	goto exit;
+
+error:
+	parent = gtk_widget_get_toplevel (GTK_WIDGET (view));
+	parent = GTK_WIDGET_TOPLEVEL (parent) ? parent : NULL;
+
+	dialog = gtk_message_dialog_new_with_markup (
+		parent, GTK_DIALOG_DESTROY_WITH_PARENT,
+		GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
+		"<big><b>%s</b></big>",
+		ngettext ("Could not send attachment",
+		"Could not send attachments", n_uris));
+
+	gtk_message_dialog_format_secondary_text (
+		GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
+
+	gtk_dialog_run (GTK_DIALOG (dialog));
+
+	gtk_widget_destroy (dialog);
+	g_error_free (error);
+
+exit:
+	g_object_unref (handler);
+	g_strfreev (uris);
+}
+
+static void
+action_sendto_cb (GtkAction *action,
+                  EAttachmentHandler *handler)
+{
+	EAttachmentView *view;
+	EAttachmentStore *store;
+	GList *selected;
+
+	view = e_attachment_handler_get_view (handler);
+	store = e_attachment_view_get_store (view);
+
+	selected = e_attachment_view_get_selected_attachments (view);
+	g_return_if_fail (selected != NULL);
+
+	e_attachment_store_get_uris_async (
+		store, selected, (GAsyncReadyCallback)
+		sendto_save_finished_cb, g_object_ref (handler));
+
+	g_list_foreach (selected, (GFunc) g_object_unref, NULL);
+	g_list_free (selected);
+}
+
+static GtkActionEntry standard_entries[] = {
+
+	{ "sendto",
+	  "document-send",
+	  N_("_Send To..."),
+	  NULL,
+	  N_("Send the selected attachments somewhere"),
+	  G_CALLBACK (action_sendto_cb) }
+};
+
+static void
+attachment_handler_sendto_update_actions_cb (EAttachmentView *view,
+                                             EAttachmentHandler *handler)
+{
+	GtkActionGroup *action_group;
+	GList *selected, *iter;
+	gboolean visible = FALSE;
+
+	selected = e_attachment_view_get_selected_attachments (view);
+
+	if (selected == NULL)
+		goto exit;
+
+	for (iter = selected; iter != NULL; iter = iter->next) {
+		EAttachment *attachment = iter->data;
+
+		if (e_attachment_get_loading (attachment))
+			goto exit;
+
+		if (e_attachment_get_saving (attachment))
+			goto exit;
+	}
+
+	visible = TRUE;
+
+exit:
+	action_group = e_attachment_view_get_action_group (view, "sendto");
+	gtk_action_group_set_visible (action_group, visible);
+
+	g_list_foreach (selected, (GFunc) g_object_unref, NULL);
+	g_list_free (selected);
+}
+
+static void
+attachment_handler_sendto_constructed (GObject *object)
+{
+	EAttachmentHandler *handler;
+	EAttachmentView *view;
+	GtkActionGroup *action_group;
+	GtkUIManager *ui_manager;
+	GError *error = NULL;
+
+	handler = E_ATTACHMENT_HANDLER (object);
+
+	/* Chain up to parent's constructed() method. */
+	G_OBJECT_CLASS (parent_class)->constructed (object);
+
+	view = e_attachment_handler_get_view (handler);
+	ui_manager = e_attachment_view_get_ui_manager (view);
+
+	action_group = gtk_action_group_new ("sendto");
+	gtk_action_group_set_translation_domain (
+		action_group, GETTEXT_PACKAGE);
+	gtk_action_group_add_actions (
+		action_group, standard_entries,
+		G_N_ELEMENTS (standard_entries), object);
+	gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
+
+	gtk_ui_manager_add_ui_from_string (ui_manager, ui, -1, &error);
+
+	if (error != NULL) {
+		g_warning ("%s", error->message);
+		g_error_free (error);
+	}
+
+	g_signal_connect (
+		view, "update-actions",
+		G_CALLBACK (attachment_handler_sendto_update_actions_cb),
+		object);
+}
+
+static void
+attachment_handler_sendto_class_init (EAttachmentHandlerSendtoClass *class)
+{
+	GObjectClass *object_class;
+
+	parent_class = g_type_class_peek_parent (class);
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->constructed = attachment_handler_sendto_constructed;
+}
+
+GType
+e_attachment_handler_sendto_get_type (void)
+{
+	static GType type = 0;
+
+	if (G_UNLIKELY (type == 0)) {
+		static const GTypeInfo type_info = {
+			sizeof (EAttachmentHandlerSendtoClass),
+			(GBaseInitFunc) NULL,
+			(GBaseFinalizeFunc) NULL,
+			(GClassInitFunc) attachment_handler_sendto_class_init,
+			(GClassFinalizeFunc) NULL,
+			NULL,  /* class_data */
+			sizeof (EAttachmentHandlerSendto),
+			0,     /* n_preallocs */
+			(GInstanceInitFunc) NULL,
+			NULL   /* value_table */
+		};
+
+		type = g_type_register_static (
+			E_TYPE_ATTACHMENT_HANDLER,
+			"EAttachmentHandlerSendto",
+			&type_info, 0);
+	}
+
+	return type;
+}
+
+void
+init (EPlugin *ep,
+      ESEventTargetShell *target)
+{
+	/* XXX Unfortunately we have to use Evolution's EPlugin framework
+	 *     just to get the stupid GType registered.  Evolution does not
+	 *     yet have a simple GTypeModule loader.  I'm working on that. */
+
+	e_attachment_handler_sendto_get_type ();
+}
diff --git a/evolution/e-attachment-handler-sendto.h b/evolution/e-attachment-handler-sendto.h
new file mode 100644
index 0000000..9ae272a
--- /dev/null
+++ b/evolution/e-attachment-handler-sendto.h
@@ -0,0 +1,62 @@
+/*
+ * e-attachment-handler-sendto.h
+ *
+ * Copyright (C) 2009 Matthew Barnes
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#ifndef E_ATTACHMENT_HANDLER_SENDTO_H
+#define E_ATTACHMENT_HANDLER_SENDTO_H
+
+#include <misc/e-attachment-handler.h>
+
+/* Standard GObject macros */
+#define E_TYPE_ATTACHMENT_HANDLER_SENDTO \
+	(e_attachment_handler_sendto_get_type ())
+#define E_ATTACHMENT_HANDLER_SENDTO(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), E_TYPE_ATTACHMENT_HANDLER_SENDTO, EAttachmentHandlerSendto))
+#define E_ATTACHMENT_HANDLER_SENDTO_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_CAST \
+	((cls), E_TYPE_ATTACHMENT_HANDLER_SENDTO, EAttachmentHandlerSendtoClass))
+#define E_IS_ATTACHMENT_HANDLER_SENDTO(obj) \
+	(G_TYPE_CHECK_INSTANCE_TYPE \
+	((obj), E_TYPE_ATTACHMENT_HANDLER_SENDTO))
+#define E_IS_ATTACHMENT_HANDLER_SENDTO_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_TYPE \
+	((cls), E_TYPE_ATTACHMENT_HANDLER_SENDTO))
+#define E_ATTACHMENT_HANDLER_SENDTO_GET_CLASS(obj) \
+	(G_TYPE_INSTANCE_GET_CLASS \
+	((obj), E_TYPE_ATTACHMENT_HANDLER_SENDTO, EAttachmentHandlerSendtoClass))
+
+G_BEGIN_DECLS
+
+typedef struct _EAttachmentHandlerSendto EAttachmentHandlerSendto;
+typedef struct _EAttachmentHandlerSendtoClass EAttachmentHandlerSendtoClass;
+
+struct _EAttachmentHandlerSendto {
+	EAttachmentHandler parent;
+};
+
+struct _EAttachmentHandlerSendtoClass {
+	EAttachmentHandlerClass parent_class;
+};
+
+GType		e_attachment_handler_sendto_get_type	(void);
+
+G_END_DECLS
+
+#endif /* E_ATTACHMENT_HANDLER_SENDTO_H */
diff --git a/evolution/nautilus-sendto.c b/evolution/nautilus-sendto.c
deleted file mode 100644
index 01728ae..0000000
--- a/evolution/nautilus-sendto.c
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with the program; if not, see <http://www.gnu.org/licenses/>  
- *
- *
- * Authors:
- *		Johnny Jacob <johnnyjacob@gmail.com>
- *
- * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
- *
- */
-
-#include <config.h>
-
-#include <glib.h>
-#include <gtk/gtk.h>
-#include <glib/gi18n-lib.h>
-#include <e-util/e-config.h>
-#include <e-util/e-popup.h>
-#include <mail/em-popup.h>
-#include <mail/em-menu.h>
-#include <mail/em-utils.h>
-#include <misc/e-attachment.h>
-
-static void send_file (EPlugin *ep, EPopupTarget *t, void *data);
-void org_gnome_evolution_send_file_attachments (EPlugin *ep, EMPopupTargetAttachments *t);
-void org_gnome_evolution_send_file_part (EPlugin *ep, EMPopupTargetPart *t);
-
-static void
-popup_free (EPopup *ep, GSList *items, void *data)
-{
-	g_slist_free (items);
-}
-
-static EPopupItem popup_attachment_items[] = {
-	{ E_POPUP_BAR, "25.display.00"},
-	{ E_POPUP_ITEM, "25.display.01", N_("_Send to..."), (EPopupActivateFunc)send_file, NULL, "document-send"}
-};
-
-void org_gnome_evolution_send_file_attachments (EPlugin *ep, EMPopupTargetAttachments *t)
-{
-	GSList *menus = NULL;
-	int len = 0;
-
-	g_message ("org_gnome_evolution_send_file_attachments called");
-
-	len = g_slist_length(t->attachments);
-
-	if (len != 1)
-		return;
-
-	menus = g_slist_prepend (menus, &popup_attachment_items[0]);
-	menus = g_slist_prepend (menus, &popup_attachment_items[1]);
-	e_popup_add_items (t->target.popup, menus, GETTEXT_PACKAGE, popup_free, t);
-}
-
-void org_gnome_evolution_send_file_part (EPlugin *ep, EMPopupTargetPart *t)
-{
-	GSList *menus = NULL;
-
-	g_message ("org_gnome_evolution_send_file_attachments called");
-
-	menus = g_slist_prepend (menus, &popup_attachment_items[0]);
-	menus = g_slist_prepend (menus, &popup_attachment_items[1]);
-	e_popup_add_items (t->target.popup, menus, GETTEXT_PACKAGE, popup_free, t);
-}
-
-static void
-send_file (EPlugin *ep, EPopupTarget *t, void *data)
-{
-	CamelMimePart *part;
-	char *path;
-	EPopupTarget *target = (EPopupTarget *) data;
-	GPtrArray *argv;
-	gboolean ret;
-	GError *err = NULL;
-
-	if (target->type == EM_POPUP_TARGET_ATTACHMENTS)
-		part = ((EAttachment *) ((EMPopupTargetAttachments *) target)->attachments->data)->body;
-	else
-		part = ((EMPopupTargetPart *) target)->part;
-
-	path = em_utils_temp_save_part (NULL, part, FALSE);
-	g_message ("saved part as %s", path);
-
-	argv = g_ptr_array_new ();
-	g_ptr_array_add (argv, "nautilus-sendto");
-	g_ptr_array_add (argv, path);
-	g_ptr_array_add (argv, NULL);
-
-	ret = g_spawn_async (NULL, (gchar **) argv->pdata,
-			     NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &err);
-	g_ptr_array_free (argv, TRUE);
-
-	if (ret == FALSE) {
-		g_warning ("Couldn't send the attachment: %s", err->message);
-		g_error_free (err);
-	}
-
-	g_free (path);
-}
-
diff --git a/evolution/org-gnome-evolution-send-attachments-to.eplug.xml b/evolution/org-gnome-evolution-send-attachments-to.eplug.xml
index b6f7f27..520bdd2 100644
--- a/evolution/org-gnome-evolution-send-attachments-to.eplug.xml
+++ b/evolution/org-gnome-evolution-send-attachments-to.eplug.xml
@@ -7,22 +7,11 @@
     location="@PLUGINDIR@/liborg-gnome-evolution-send-attachments-to@SOEXT@"
     _name="Send to..."
     domain="@GETTEXT_PACKAGE@">
-    
-    <author name="Bastien Nocera" email="hadess@hadess.net"/>
+
+    <author name="Matthew Barnes" email="mbarnes@redhat.com"/>
     <_description>Send files to remote devices, or people</_description>
-        <!-- Attachment to received e-mail -->
-	<hook class="org.gnome.evolution.mail.popup:1.0">
-		<menu id="org.gnome.evolution.mail.formathtmldisplay.popup" target="part" factory="org_gnome_evolution_send_file_part">
-		</menu>
-	</hook>
-	<hook class="org.gnome.evolution.mail.popup:1.0">
-		<menu id="org.gnome.evolution.mail.attachments.popup" target="attachments" factory="org_gnome_evolution_send_file_attachments">
-		</menu>
-	</hook>
-	<!-- Right-click in the attachment bar when creating a new mail -->
-	<hook class="org.gnome.evolution.mail.popup:1.0">
-		<menu id="org.gnome.evolution.mail.composer.attachmentbar.popup" target="attachments" factory="org_gnome_evolution_send_file_attachments">
-		</menu>
-	</hook>
+    <hook class="org.gnome.evolution.shell.events:1.0">
+      <event id="started.done" handle="init" target="shell"/>
+    </hook>
   </e-plugin>
 </e-plugin-list>
