[evolution] Reimplement em_utils_edit_messages().



commit 648eacf49fb9103bbe00e4baa45c6d05a28861c6
Author: Matthew Barnes <mbarnes redhat com>
Date:   Wed May 11 12:09:48 2011 -0400

    Reimplement em_utils_edit_messages().
    
    Now uses e_mail_folder_get_multiple_messages().

 mail/e-mail-folder-pane.c  |   17 ++-----
 mail/e-mail-reader-utils.c |    9 +---
 mail/e-mail-reader.c       |    9 +---
 mail/em-composer-utils.c   |  117 +++++++++++++++++++++++++++++---------------
 mail/em-composer-utils.h   |    2 +-
 5 files changed, 86 insertions(+), 68 deletions(-)
---
diff --git a/mail/e-mail-folder-pane.c b/mail/e-mail-folder-pane.c
index 1c5f8e3..2f1091e 100644
--- a/mail/e-mail-folder-pane.c
+++ b/mail/e-mail-folder-pane.c
@@ -63,10 +63,7 @@ folder_pane_set_preview_visible (EMailView *view,
 static guint
 mail_paned_view_open_selected_mail (EMailPanedView *view)
 {
-	EShell *shell;
-	EShellBackend *shell_backend;
 	EMailReader *reader;
-	EMailBackend *backend;
 	GPtrArray *uids;
 	gint i;
 	GtkWindow *window;
@@ -75,23 +72,19 @@ mail_paned_view_open_selected_mail (EMailPanedView *view)
 	guint n_views, ii;
 
 	reader = E_MAIL_READER (view);
-	backend = e_mail_reader_get_backend (reader);
-
-	shell_backend = E_SHELL_BACKEND (backend);
-	shell = e_shell_backend_get_shell (shell_backend);
-
-	uids = e_mail_reader_get_selected_uids (reader);
+	folder = e_mail_reader_get_folder (reader);
 	window = e_mail_reader_get_window (reader);
+	uids = e_mail_reader_get_selected_uids (reader);
+
 	if (!em_utils_ask_open_many (window, uids->len)) {
 		em_utils_uids_free (uids);
 		return 0;
 	}
 
-	folder = e_mail_reader_get_folder (reader);
 	if (em_utils_folder_is_drafts (folder) ||
 		em_utils_folder_is_outbox (folder) ||
 		em_utils_folder_is_templates (folder)) {
-		em_utils_edit_messages (shell, folder, uids, TRUE);
+		em_utils_edit_messages (reader, folder, uids, TRUE);
 		return 0;
 	}
 
@@ -123,7 +116,7 @@ mail_paned_view_open_selected_mail (EMailPanedView *view)
 			edits = g_ptr_array_new ();
 			g_ptr_array_add (edits, real_uid);
 			em_utils_edit_messages (
-				shell, real_folder, edits, TRUE);
+				reader, real_folder, edits, TRUE);
 		} else {
 			g_free (real_uid);
 			g_ptr_array_add (views, g_strdup (uid));
diff --git a/mail/e-mail-reader-utils.c b/mail/e-mail-reader-utils.c
index 374bed0..81712b8 100644
--- a/mail/e-mail-reader-utils.c
+++ b/mail/e-mail-reader-utils.c
@@ -235,9 +235,7 @@ copy_tree_state (EMailReader *src_reader, EMailReader *des_reader)
 guint
 e_mail_reader_open_selected (EMailReader *reader)
 {
-	EShell *shell;
 	EMailBackend *backend;
-	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GtkWindow *window;
 	GPtrArray *views;
@@ -251,9 +249,6 @@ e_mail_reader_open_selected (EMailReader *reader)
 	uids = e_mail_reader_get_selected_uids (reader);
 	window = e_mail_reader_get_window (reader);
 
-	shell_backend = E_SHELL_BACKEND (backend);
-	shell = e_shell_backend_get_shell (shell_backend);
-
 	if (!em_utils_ask_open_many (window, uids->len)) {
 		em_utils_uids_free (uids);
 		return 0;
@@ -262,7 +257,7 @@ e_mail_reader_open_selected (EMailReader *reader)
 	if (em_utils_folder_is_drafts (folder) ||
 		em_utils_folder_is_outbox (folder) ||
 		em_utils_folder_is_templates (folder)) {
-		em_utils_edit_messages (shell, folder, uids, TRUE);
+		em_utils_edit_messages (reader, folder, uids, TRUE);
 		return uids->len;
 	}
 
@@ -295,7 +290,7 @@ e_mail_reader_open_selected (EMailReader *reader)
 			edits = g_ptr_array_new ();
 			g_ptr_array_add (edits, real_uid);
 			em_utils_edit_messages (
-				shell, real_folder, edits, TRUE);
+				reader, real_folder, edits, TRUE);
 		} else {
 			g_free (real_uid);
 			g_ptr_array_add (views, g_strdup (uid));
diff --git a/mail/e-mail-reader.c b/mail/e-mail-reader.c
index e248a56..e18a546 100644
--- a/mail/e-mail-reader.c
+++ b/mail/e-mail-reader.c
@@ -725,22 +725,15 @@ static void
 action_mail_message_edit_cb (GtkAction *action,
                              EMailReader *reader)
 {
-	EShell *shell;
-	EMailBackend *backend;
-	EShellBackend *shell_backend;
 	CamelFolder *folder;
 	GPtrArray *uids;
 	gboolean replace;
 
-	backend = e_mail_reader_get_backend (reader);
 	folder = e_mail_reader_get_folder (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
-	shell_backend = E_SHELL_BACKEND (backend);
-	shell = e_shell_backend_get_shell (shell_backend);
-
 	replace = em_utils_folder_is_drafts (folder);
-	em_utils_edit_messages (shell, folder, uids, replace);
+	em_utils_edit_messages (reader, folder, uids, replace);
 }
 
 static void
diff --git a/mail/em-composer-utils.c b/mail/em-composer-utils.c
index a54a523..1431975 100644
--- a/mail/em-composer-utils.c
+++ b/mail/em-composer-utils.c
@@ -84,6 +84,7 @@ struct _AsyncContext {
 	EMailForwardStyle style;
 	gchar *folder_uri;
 	gchar *message_uid;
+	gboolean replace;
 };
 
 struct _ForwardData {
@@ -1247,51 +1248,72 @@ em_utils_edit_message (EShell *shell,
 }
 
 static void
-edit_messages_replace (CamelFolder *folder,
-                       GPtrArray *uids,
-                       GPtrArray *msgs,
-                       gpointer user_data)
+edit_messages_cb (CamelFolder *folder,
+                  GAsyncResult *result,
+                  AsyncContext *context)
 {
-	EShell *shell = E_SHELL (user_data);
-	gint ii;
+	EShell *shell;
+	EMailBackend *backend;
+	EAlertSink *alert_sink;
+	GHashTable *hash_table;
+	GHashTableIter iter;
+	gpointer key, value;
+	GError *error = NULL;
 
-	if (msgs == NULL)
+	alert_sink = e_mail_reader_get_alert_sink (context->reader);
+
+	hash_table = e_mail_folder_get_multiple_messages_finish (
+		folder, result, &error);
+
+	/* Ignore cancellations. */
+	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+		g_warn_if_fail (hash_table == NULL);
+		e_activity_set_state (context->activity, E_ACTIVITY_CANCELLED);
+		async_context_free (context);
+		g_error_free (error);
 		return;
 
-	for (ii = 0; ii < msgs->len; ii++) {
-		camel_medium_remove_header (
-			CAMEL_MEDIUM (msgs->pdata[ii]), "X-Mailer");
-		em_utils_edit_message (
-			shell, folder, msgs->pdata[ii], uids->pdata[ii]);
+	} else if (error != NULL) {
+		g_warn_if_fail (hash_table == NULL);
+		e_alert_submit (
+			alert_sink,
+			"mail:get-multiple-messages",
+			error->message, NULL);
+		async_context_free (context);
+		g_error_free (error);
+		return;
 	}
 
-	g_object_unref (shell);
-}
+	g_return_if_fail (hash_table != NULL);
 
-static void
-edit_messages_no_replace (CamelFolder *folder,
-                          GPtrArray *uids,
-                          GPtrArray *msgs,
-                          gpointer user_data)
-{
-	EShell *shell = E_SHELL (user_data);
-	gint ii;
+	backend = e_mail_reader_get_backend (context->reader);
+	shell = e_shell_backend_get_shell (E_SHELL_BACKEND (backend));
 
-	if (msgs == NULL)
-		return;
+	/* Open each message in its own composer window. */
+
+	g_hash_table_iter_init (&iter, hash_table);
 
-	for (ii = 0; ii < msgs->len; ii++) {
-		camel_medium_remove_header (
-			CAMEL_MEDIUM (msgs->pdata[ii]), "X-Mailer");
-		em_utils_edit_message (shell, folder, msgs->pdata[ii], NULL);
+	while (g_hash_table_iter_next (&iter, &key, &value)) {
+		CamelMimeMessage *message;
+
+		if (!context->replace)
+			key = NULL;
+
+		message = CAMEL_MIME_MESSAGE (value);
+		camel_medium_remove_header (CAMEL_MEDIUM (value), "X-Mailer");
+		em_utils_edit_message (shell, folder, message, key);
 	}
 
-	g_object_unref (shell);
+	g_hash_table_unref (hash_table);
+
+	e_activity_set_state (context->activity, E_ACTIVITY_COMPLETED);
+
+	async_context_free (context);
 }
 
 /**
  * em_utils_edit_messages:
- * @shell: an #EShell
+ * @reader: an #EMailReader
  * @folder: folder containing messages to edit
  * @uids: uids of messages to edit
  * @replace: replace the existing message(s) when sent or saved.
@@ -1299,23 +1321,38 @@ edit_messages_no_replace (CamelFolder *folder,
  * Opens a composer for each message to be edited.
  **/
 void
-em_utils_edit_messages (EShell *shell,
+em_utils_edit_messages (EMailReader *reader,
                         CamelFolder *folder,
                         GPtrArray *uids,
                         gboolean replace)
 {
-	g_return_if_fail (E_IS_SHELL (shell));
+	AsyncContext *context;
+	GCancellable *cancellable;
+	EMailBackend *backend;
+
+	g_return_if_fail (E_IS_MAIL_READER (reader));
 	g_return_if_fail (CAMEL_IS_FOLDER (folder));
 	g_return_if_fail (uids != NULL);
 
-	if (replace)
-		mail_get_messages (
-			folder, uids, edit_messages_replace,
-			g_object_ref (shell));
-	else
-		mail_get_messages (
-			folder, uids, edit_messages_no_replace,
-			g_object_ref (shell));
+	context = g_slice_new0 (AsyncContext);
+	context->activity = e_activity_new ();
+	context->reader = g_object_ref (reader);
+	context->ptr_array = g_ptr_array_ref (uids);
+	context->replace = replace;
+
+	cancellable = camel_operation_new ();
+	e_activity_set_cancellable (context->activity, cancellable);
+
+	backend = e_mail_reader_get_backend (reader);
+	e_shell_backend_add_activity (
+		E_SHELL_BACKEND (backend), context->activity);
+
+	e_mail_folder_get_multiple_messages (
+		folder, uids, G_PRIORITY_DEFAULT,
+		cancellable, (GAsyncReadyCallback)
+		edit_messages_cb, context);
+
+	g_object_unref (cancellable);
 }
 
 static void
diff --git a/mail/em-composer-utils.h b/mail/em-composer-utils.h
index 785c89e..ab4ce9e 100644
--- a/mail/em-composer-utils.h
+++ b/mail/em-composer-utils.h
@@ -42,7 +42,7 @@ GtkWidget *	em_utils_edit_message		(EShell *shell,
 						 CamelFolder *folder,
 						 CamelMimeMessage *message,
 						 const gchar *message_uid);
-void		em_utils_edit_messages		(EShell *shell,
+void		em_utils_edit_messages		(EMailReader *reader,
 						 CamelFolder *folder,
 						 GPtrArray *uids,
 						 gboolean replace);



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