[evolution] Kill em_utils_temp_save_part().



commit 75b078e99793350fa2f54ca80b72213d1bfbb17f
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu Sep 30 08:16:30 2010 -0400

    Kill em_utils_temp_save_part().
    
    Rewrite the last usage of it in itip-formatter.c to use EAttachments
    instead.  This also allowed me to kill mail_save_part() in mail-ops.c.
    
    I may need to reevaluate the EAttachment API at some point for all these
    fringe EAttachment uses we're accumulating.  Having to asynchronously
    "load" an EAttachment whose content is already in memory kinda sucks.

 mail/em-utils.c                         |   64 -----------------
 mail/em-utils.h                         |    2 -
 mail/mail-ops.c                         |  107 -----------------------------
 mail/mail-ops.h                         |    4 -
 plugins/itip-formatter/itip-formatter.c |  113 +++++++++++++++++++++++++++++-
 5 files changed, 109 insertions(+), 181 deletions(-)
---
diff --git a/mail/em-utils.c b/mail/em-utils.c
index c7b7020..971d74f 100644
--- a/mail/em-utils.c
+++ b/mail/em-utils.c
@@ -84,8 +84,6 @@ extern const gchar *shell_builtin_backend;
 /* Used in em_util_ask_open_many() */
 #define TOO_MANY 10
 
-static void emu_save_part_done (CamelMimePart *part, gchar *name, gint done, gpointer data);
-
 #define d(x)
 
 gboolean
@@ -943,68 +941,6 @@ em_utils_selection_get_urilist (GtkSelectionData *selection_data,
 	g_strfreev (uris);
 }
 
-static void
-emu_save_part_done (CamelMimePart *part, gchar *name, gint done, gpointer data)
-{
-	((gint *)data)[0] = done;
-}
-
-/**
- * em_utils_temp_save_part:
- * @parent:
- * @part:
- * @mode: readonly or not.
- *
- * Save a part's content to a temporary file, and return the
- * filename.
- *
- * Return value: NULL if anything failed.
- **/
-gchar *
-em_utils_temp_save_part (GtkWidget *parent, CamelMimePart *part, gboolean mode)
-{
-	const gchar *filename;
-	gchar *tmpdir, *path, *utf8_mfilename = NULL, *mfilename = NULL;
-	gint done;
-	GtkWidget *w;
-
-	tmpdir = e_mkdtemp("evolution-tmp-XXXXXX");
-	if (tmpdir == NULL) {
-		w = e_alert_dialog_new_for_args ((GtkWindow *)parent, "mail:no-create-tmp-path", g_strerror(errno), NULL);
-		em_utils_show_error_silent (w);
-		return NULL;
-	}
-
-	filename = camel_mime_part_get_filename (part);
-	if (filename == NULL) {
-		/* This is the default filename used for temporary file creation */
-		filename = _("Unknown");
-	} else {
-		utf8_mfilename = g_strdup (filename);
-		e_filename_make_safe (utf8_mfilename);
-		mfilename = g_filename_from_utf8 ((const gchar *) utf8_mfilename, -1, NULL, NULL, NULL);
-		g_free (utf8_mfilename);
-		filename = (const gchar *) mfilename;
-	}
-
-	path = g_build_filename (tmpdir, filename, NULL);
-	g_free (tmpdir);
-	g_free (mfilename);
-
-	/* FIXME: This doesn't handle default charsets */
-	if (mode)
-		mail_msg_wait (mail_save_part (part, path, emu_save_part_done, &done, TRUE));
-	else
-		mail_msg_wait (mail_save_part (part, path, emu_save_part_done, &done, FALSE));
-	if (!done) {
-		/* mail_save_part should popup an error box automagically */
-		g_free (path);
-		path = NULL;
-	}
-
-	return path;
-}
-
 /** em_utils_folder_is_templates:
  * @folder: folder
  * @uri: uri for this folder, if known
diff --git a/mail/em-utils.h b/mail/em-utils.h
index 0e9ef9f..36318ff 100644
--- a/mail/em-utils.h
+++ b/mail/em-utils.h
@@ -61,8 +61,6 @@ void em_utils_selection_get_uidlist (GtkSelectionData *data, CamelFolder *dest,
 void em_utils_selection_set_urilist (GtkSelectionData *data, CamelFolder *folder, GPtrArray *uids);
 void em_utils_selection_get_urilist (GtkSelectionData *data, CamelFolder *folder);
 
-gchar *em_utils_temp_save_part (GtkWidget *parent, CamelMimePart *part, gboolean mode);
-
 gboolean em_utils_folder_is_drafts (CamelFolder *folder, const gchar *uri);
 gboolean em_utils_folder_is_templates (CamelFolder *folder, const gchar *uri);
 gboolean em_utils_folder_is_sent (CamelFolder *folder, const gchar *uri);
diff --git a/mail/mail-ops.c b/mail/mail-ops.c
index 1146f8d..4fd6ab2 100644
--- a/mail/mail-ops.c
+++ b/mail/mail-ops.c
@@ -2303,113 +2303,6 @@ mail_save_messages (CamelFolder *folder, GPtrArray *uids, const gchar *path,
 	return id;
 }
 
-/* ** SAVE PART ******************************************************* */
-
-struct _save_part_msg {
-	MailMsg base;
-
-	CamelMimePart *part;
-	gchar *path;
-	void (*done)(CamelMimePart *part, gchar *path, gint saved, gpointer data);
-	gpointer data;
-	gboolean readonly;
-};
-
-static gchar *
-save_part_desc (struct _save_part_msg *m)
-{
-	return g_strdup(_("Saving attachment"));
-}
-
-static void
-save_part_exec (struct _save_part_msg *m)
-{
-	CamelDataWrapper *content;
-	CamelStream *stream;
-	gchar *path;
-
-	if (strstr (m->path, "://"))
-		path = m->path;
-	else
-		path = g_filename_to_uri (m->path, NULL, NULL);
-
-	if (!m->readonly) {
-		if (!(stream = camel_stream_vfs_new_with_uri (path, CAMEL_STREAM_VFS_CREATE))) {
-			g_set_error (
-				&m->base.error, G_FILE_ERROR,
-				g_file_error_from_errno (errno),
-				_("Cannot create output file: %s:\n %s"),
-				path, g_strerror (errno));
-			if (path != m->path)
-				g_free (path);
-			return;
-		}
-	} else if (!(stream = camel_stream_vfs_new_with_uri (path, CAMEL_STREAM_VFS_CREATE))) {
-		g_set_error (
-			&m->base.error, G_FILE_ERROR,
-			g_file_error_from_errno (errno),
-			_("Cannot create output file: %s:\n %s"),
-			path, g_strerror (errno));
-		if (path != m->path)
-			g_free (path);
-		return;
-	}
-
-	if (path != m->path)
-		g_free (path);
-
-	content = camel_medium_get_content (CAMEL_MEDIUM (m->part));
-
-	if (camel_data_wrapper_decode_to_stream_sync (
-		content, stream, m->base.cancellable, &m->base.error) == -1
-	    || camel_stream_flush (stream, m->base.cancellable, &m->base.error) == -1)
-		g_prefix_error (&m->base.error, _("Could not write data: "));
-
-	g_object_unref (stream);
-}
-
-static void
-save_part_done (struct _save_part_msg *m)
-{
-	if (m->done)
-		m->done (m->part, m->path, m->base.error == NULL, m->data);
-}
-
-static void
-save_part_free (struct _save_part_msg *m)
-{
-	g_object_unref (m->part);
-	g_free (m->path);
-}
-
-static MailMsgInfo save_part_info = {
-	sizeof (struct _save_part_msg),
-	(MailMsgDescFunc) save_part_desc,
-	(MailMsgExecFunc) save_part_exec,
-	(MailMsgDoneFunc) save_part_done,
-	(MailMsgFreeFunc) save_part_free
-};
-
-gint
-mail_save_part (CamelMimePart *part, const gchar *path,
-		void (*done)(CamelMimePart *part, gchar *path, gint saved, gpointer data), gpointer data, gboolean readonly)
-{
-	struct _save_part_msg *m;
-	gint id;
-	m = mail_msg_new (&save_part_info);
-	m->part = part;
-	g_object_ref (part);
-	m->path = g_strdup (path);
-	m->data = data;
-	m->done = done;
-	m->readonly = readonly;
-
-	id = m->base.seq;
-	mail_msg_unordered_push (m);
-
-	return id;
-}
-
 /* ** PREPARE OFFLINE ***************************************************** */
 
 struct _prep_offline_msg {
diff --git a/mail/mail-ops.h b/mail/mail-ops.h
index a0af5ff..4bea1b0 100644
--- a/mail/mail-ops.h
+++ b/mail/mail-ops.h
@@ -120,10 +120,6 @@ gint mail_save_messages (CamelFolder *folder, GPtrArray *uids, const gchar *path
 			void (*done) (CamelFolder *folder, GPtrArray *uids, gchar *path, gpointer data),
 			gpointer data);
 
-gint mail_save_part (CamelMimePart *part, const gchar *path,
-		    void (*done)(CamelMimePart *part, gchar *path, gint saved, gpointer data),
-		    gpointer data, gboolean readonly);
-
 /* yeah so this is messy, but it does a lot, maybe i can consolidate all user_data's to be the one */
 void		mail_send_queue			(CamelFolder *queue,
 						 const gchar *destination,
diff --git a/plugins/itip-formatter/itip-formatter.c b/plugins/itip-formatter/itip-formatter.c
index 2a71ce3..2c95876 100644
--- a/plugins/itip-formatter/itip-formatter.c
+++ b/plugins/itip-formatter/itip-formatter.c
@@ -42,6 +42,7 @@
 #include <mail/mail-mt.h>
 #include <libedataserver/e-account-list.h>
 #include <e-util/e-alert-dialog.h>
+#include <e-util/e-mktemp.h>
 #include <calendar/gui/calendar-config.h>
 #include <calendar/gui/itip-utils.h>
 #include <calendar/common/authentication.h>
@@ -1009,6 +1010,108 @@ message_foreach_part (CamelMimePart *part, GSList **part_list)
 	}
 }
 
+static void
+attachment_load_finished (EAttachment *attachment,
+                          GAsyncResult *result,
+                          gpointer user_data)
+{
+	struct {
+		GFile *file;
+		gboolean done;
+	} *status = user_data;
+
+	/* Should be no need to check for error here. */
+	e_attachment_load_finish (attachment, result, NULL);
+
+	status->done = TRUE;
+}
+
+static void
+attachment_save_finished (EAttachment *attachment,
+                          GAsyncResult *result,
+                          gpointer user_data)
+{
+	GError *error = NULL;
+
+	struct {
+		GFile *file;
+		gboolean done;
+	} *status = user_data;
+
+	status->file = e_attachment_save_finish (attachment, result, &error);
+	status->done = TRUE;
+
+	/* XXX Error handling needs improvement. */
+	if (error != NULL) {
+		g_warning ("%s", error->message);
+		g_error_free (error);
+	}
+}
+
+static gchar *
+get_uri_for_part (CamelMimePart *mime_part)
+{
+	EAttachment *attachment;
+	GFile *temp_directory;
+	gchar *template;
+	gchar *path;
+
+	struct {
+		GFile *file;
+		gboolean done;
+	} status;
+
+	/* XXX Error handling leaves much to be desired. */
+
+	template = g_strdup_printf (PACKAGE "-%s-XXXXXX", g_get_user_name ());
+	path = e_mkdtemp (template);
+	g_free (template);
+
+	if (path == NULL)
+		return NULL;
+
+	temp_directory = g_file_new_for_path (path);
+	g_free (path);
+
+	attachment = e_attachment_new ();
+	e_attachment_set_mime_part (attachment, mime_part);
+
+	status.done = FALSE;
+
+	e_attachment_load_async (
+		attachment, (GAsyncReadyCallback)
+		attachment_load_finished, &status);
+
+	/* Loading should be instantaneous since we already have
+	 * the full content, but we still have to crank the main
+	 * loop until the callback gets triggered. */
+	while (!status.done)
+		gtk_main_iteration ();
+
+	status.file = NULL;
+	status.done = FALSE;
+
+	e_attachment_save_async (
+		attachment, temp_directory, (GAsyncReadyCallback)
+		attachment_save_finished, &status);
+
+	/* We can't return until we have results, so crank
+	 * the main loop until the callback gets triggered. */
+	while (!status.done)
+		gtk_main_iteration ();
+
+	if (status.file != NULL) {
+		path = g_file_get_path (status.file);
+		g_object_unref (status.file);
+	} else
+		path = NULL;
+
+	g_object_unref (attachment);
+	g_object_unref (temp_directory);
+
+	return path;
+}
+
 static gboolean
 update_item (struct _itip_puri *pitip, ItipViewResponse response)
 {
@@ -1116,8 +1219,9 @@ update_item (struct _itip_puri *pitip, ItipViewResponse response)
 					if (part == (CamelMimePart *) msg || part == pitip->part)
 						continue;
 
-					new_uri = em_utils_temp_save_part (NULL, part, FALSE);
-					new_attachments = g_slist_append (new_attachments, new_uri);
+					new_uri = get_uri_for_part (part);
+					if (new_uri != NULL)
+						new_attachments = g_slist_append (new_attachments, new_uri);
 				}
 
 				g_slist_free (parts);
@@ -1125,8 +1229,9 @@ update_item (struct _itip_puri *pitip, ItipViewResponse response)
 			} else if (!g_ascii_strncasecmp (uri, "cid:", 4)) {
 				part = camel_mime_message_get_part_by_content_id (msg, uri + 4);
 				if (part) {
-					new_uri = em_utils_temp_save_part (NULL, part, FALSE);
-					new_attachments = g_slist_append (new_attachments, new_uri);
+					new_uri = get_uri_for_part (part);
+					if (new_uri != NULL)
+						new_attachments = g_slist_append (new_attachments, new_uri);
 				}
 
 			} else {



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