Bug 551464 - Paste files into composer as attachments

This commit is contained in:
Matthew Barnes
2009-12-03 14:27:05 -05:00
parent cc0b806134
commit f0adcbbdf7
3 changed files with 118 additions and 58 deletions

View File

@ -426,3 +426,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;
}

View File

@ -22,6 +22,8 @@
#include "e-msg-composer.h"
#include <errno.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
@ -32,6 +34,7 @@
#include "e-composer-header-table.h"
#include "e-util/e-binding.h"
#include "e-util/e-charset.h"
#include "e-util/e-mktemp.h"
#include "e-util/e-util.h"
#include "e-util/gconf-bridge.h"
#include "widgets/misc/e-attachment-icon-view.h"
@ -136,6 +139,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

View File

@ -36,7 +36,6 @@
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <glib.h>
@ -48,7 +47,6 @@
#include "e-util/e-dialog-utils.h"
#include "e-util/e-alert.h"
#include "e-util/e-mktemp.h"
#include "e-util/e-plugin-ui.h"
#include "e-util/e-util-private.h"
#include "e-util/e-account-utils.h"
@ -1783,19 +1781,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);
@ -1807,60 +1797,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 if 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);
if (gtk_clipboard_wait_is_image_available (clipboard)) {
e_composer_paste_image (composer, clipboard);
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_uris_available (clipboard)) {
e_composer_paste_uris (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);
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);
}