[evolution/gnome-2-28] Bug 551464 - Paste files into composer as attachments



commit 5f97b7eaa1d45f0cb31a2a95bab2715d9bf30829
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu Dec 3 14:27:05 2009 -0500

    Bug 551464 - Paste files into composer as attachments

 composer/e-composer-private.c |  107 +++++++++++++++++++++++++++++++++++++++++
 composer/e-composer-private.h |    8 +++
 composer/e-msg-composer.c     |   61 ++----------------------
 3 files changed, 119 insertions(+), 57 deletions(-)
---
diff --git a/composer/e-composer-private.c b/composer/e-composer-private.c
index d1f4f80..529a52d 100644
--- a/composer/e-composer-private.c
+++ b/composer/e-composer-private.c
@@ -419,3 +419,110 @@ e_composer_get_default_charset (void)
 	return charset;
 }
 
+gboolean
+e_composer_paste_image (EMsgComposer *composer,
+                        GtkClipboard *clipboard)
+{
+	GtkhtmlEditor *editor;
+	EAttachmentStore *store;
+	EAttachmentView *view;
+	GdkPixbuf *pixbuf = NULL;
+	gchar *filename = NULL;
+	gchar *uri = NULL;
+	gboolean success = FALSE;
+	GError *error = NULL;
+
+	g_return_val_if_fail (E_IS_MSG_COMPOSER (composer), FALSE);
+	g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
+
+	editor = GTKHTML_EDITOR (composer);
+	view = e_msg_composer_get_attachment_view (composer);
+	store = e_attachment_view_get_store (view);
+
+	/* Extract the image data from the clipboard. */
+	pixbuf = gtk_clipboard_wait_for_image (clipboard);
+	g_return_val_if_fail (pixbuf != NULL, FALSE);
+
+	/* Reserve a temporary file. */
+	filename = e_mktemp (NULL);
+	if (filename == NULL) {
+		g_set_error (
+			&error, G_FILE_ERROR,
+			g_file_error_from_errno (errno),
+			"Could not create temporary file: %s",
+			g_strerror (errno));
+		goto exit;
+	}
+
+	/* Save the pixbuf as a temporary file in image/png format. */
+	if (!gdk_pixbuf_save (pixbuf, filename, "png", &error, NULL))
+		goto exit;
+
+	/* Convert the filename to a URI. */
+	uri = g_filename_to_uri (filename, NULL, &error);
+	if (uri == NULL)
+		goto exit;
+
+	/* In HTML mode, paste the image into the message body.
+	 * In text mode, add the image to the attachment store. */
+	if (gtkhtml_editor_get_html_mode (editor))
+		gtkhtml_editor_insert_image (editor, uri);
+	else {
+		EAttachment *attachment;
+
+		attachment = e_attachment_new_for_uri (uri);
+		e_attachment_store_add_attachment (store, attachment);
+		e_attachment_load_async (
+			attachment, (GAsyncReadyCallback)
+			e_attachment_load_handle_error, composer);
+		g_object_unref (attachment);
+	}
+
+	success = TRUE;
+
+exit:
+	if (error != NULL) {
+		g_warning ("%s", error->message);
+		g_error_free (error);
+	}
+
+	g_object_unref (pixbuf);
+	g_free (filename);
+	g_free (uri);
+
+	return success;
+}
+
+gboolean
+e_composer_paste_uris (EMsgComposer *composer,
+                       GtkClipboard *clipboard)
+{
+	EAttachmentStore *store;
+	EAttachmentView *view;
+	gchar **uris;
+	gint ii;
+
+	g_return_val_if_fail (E_IS_MSG_COMPOSER (composer), FALSE);
+	g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
+
+	view = e_msg_composer_get_attachment_view (composer);
+	store = e_attachment_view_get_store (view);
+
+	/* Extract the URI data from the clipboard. */
+	uris = gtk_clipboard_wait_for_uris (clipboard);
+	g_return_val_if_fail (uris != NULL, FALSE);
+
+	/* Add the URIs to the attachment store. */
+	for (ii = 0; uris[ii] != NULL; ii++) {
+		EAttachment *attachment;
+
+		attachment = e_attachment_new_for_uri (uris[ii]);
+		e_attachment_store_add_attachment (store, attachment);
+		e_attachment_load_async (
+			attachment, (GAsyncReadyCallback)
+			e_attachment_load_handle_error, composer);
+		g_object_unref (attachment);
+	}
+
+	return TRUE;
+}
diff --git a/composer/e-composer-private.h b/composer/e-composer-private.h
index b5ff0ba..73c25dd 100644
--- a/composer/e-composer-private.h
+++ b/composer/e-composer-private.h
@@ -22,6 +22,8 @@
 
 #include "e-msg-composer.h"
 
+#include <errno.h>
+
 #include <glib/gi18n-lib.h>
 #include <glib/gstdio.h>
 
@@ -31,6 +33,8 @@
 #include "e-composer-autosave.h"
 #include "e-composer-header-table.h"
 #include "e-util/e-binding.h"
+#include "e-util/e-mktemp.h"
+#include "e-util/e-util.h"
 #include "e-util/gconf-bridge.h"
 #include "widgets/misc/e-attachment-paned.h"
 #include "widgets/misc/e-attachment-store.h"
@@ -140,6 +144,10 @@ void		e_composer_private_finalize	(EMsgComposer *composer);
 void		e_composer_actions_init		(EMsgComposer *composer);
 gchar *		e_composer_find_data_file	(const gchar *basename);
 gchar *		e_composer_get_default_charset	(void);
+gboolean	e_composer_paste_image		(EMsgComposer *composer,
+						 GtkClipboard *clipboard);
+gboolean	e_composer_paste_uris		(EMsgComposer *composer,
+						 GtkClipboard *clipboard);
 
 G_END_DECLS
 
diff --git a/composer/e-msg-composer.c b/composer/e-msg-composer.c
index 3bdec69..f305213 100644
--- a/composer/e-msg-composer.c
+++ b/composer/e-msg-composer.c
@@ -46,7 +46,6 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <fcntl.h>
-#include <errno.h>
 
 #include <glib.h>
 
@@ -1826,19 +1825,11 @@ static void
 msg_composer_paste_clipboard (GtkhtmlEditor *editor)
 {
 	EMsgComposer *composer;
-	EAttachmentView *view;
-	EAttachmentStore *store;
 	GtkClipboard *clipboard;
-	GdkPixbuf *pixbuf;
 	GtkWidget *parent;
 	GtkWidget *widget;
-	gchar *filename;
-	gchar *uri;
-	GError *error = NULL;
 
 	composer = E_MSG_COMPOSER (editor);
-	view = e_msg_composer_get_attachment_view (composer);
-	store = e_attachment_view_get_store (view);
 
 	widget = gtk_window_get_focus (GTK_WINDOW (editor));
 	parent = gtk_widget_get_parent (widget);
@@ -1850,60 +1841,16 @@ msg_composer_paste_clipboard (GtkhtmlEditor *editor)
 
 	clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
 
-	/* Assume the clipboard has an image.  The return
-	 * value will be NULL we we assumed wrong. */
-	pixbuf = gtk_clipboard_wait_for_image (clipboard);
-	if (!GDK_IS_PIXBUF (pixbuf))
-		goto chainup;
-
-	/* Reserve a temporary file. */
-	filename = e_mktemp (NULL);
-	if (filename == NULL) {
-		g_warning ("%s", g_strerror (errno));
-		g_object_unref (pixbuf);
-		g_error_free (error);
-		return;
-	}
-
-	/* Save the pixbuf as a temporary file in image/png format. */
-	if (!gdk_pixbuf_save (pixbuf, filename, "png", &error, NULL)) {
-		g_warning ("%s", error->message);
-		g_object_unref (pixbuf);
-		g_error_free (error);
-		g_free (filename);
+	if (gtk_clipboard_wait_is_image_available (clipboard)) {
+		e_composer_paste_image (composer, clipboard);
 		return;
 	}
 
-	/* Convert the filename to a URI. */
-	uri = g_filename_to_uri (filename, NULL, &error);
-	if (error != NULL) {
-		g_warning ("%s", error->message);
-		g_object_unref (pixbuf);
-		g_error_free (error);
-		g_free (filename);
+	if (gtk_clipboard_wait_is_uris_available (clipboard)) {
+		e_composer_paste_uris (composer, clipboard);
 		return;
 	}
 
-	if (gtkhtml_editor_get_html_mode (editor))
-		gtkhtml_editor_insert_image (editor, uri);
-	else {
-		EAttachment *attachment;
-
-		attachment = e_attachment_new_for_uri (uri);
-		e_attachment_store_add_attachment (store, attachment);
-		e_attachment_load_async (
-			attachment, (GAsyncReadyCallback)
-			e_attachment_load_handle_error, composer);
-		g_object_unref (attachment);
-	}
-
-	g_object_unref (pixbuf);
-	g_free (filename);
-	g_free (uri);
-
-	return;
-
-chainup:
 	/* Chain up to parent's paste_clipboard() method. */
 	GTKHTML_EDITOR_CLASS (parent_class)->paste_clipboard (editor);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]