[evolution/webkit: 101/105] Load attachments from idle callback in EFHD



commit 93875431c494c28cd2012478006ca15273a7cd48
Author: Dan VrÃtil <dvratil redhat com>
Date:   Thu Jan 5 18:44:56 2012 +0100

    Load attachments from idle callback in EFHD

 mail/em-format-html-display.c |   74 ++++++++++++++++++++++++++++++++--------
 1 files changed, 59 insertions(+), 15 deletions(-)
---
diff --git a/mail/em-format-html-display.c b/mail/em-format-html-display.c
index 586a2cc..6691250 100644
--- a/mail/em-format-html-display.c
+++ b/mail/em-format-html-display.c
@@ -50,6 +50,8 @@
 #include <shell/e-shell.h>
 #include <shell/e-shell-utils.h>
 
+#include <libedataserver/e-flag.h>
+
 #if defined (HAVE_NSS) && defined (ENABLE_SMIME)
 #include "certificate-viewer.h"
 #include "e-cert-db.h"
@@ -72,7 +74,7 @@
 #define d(x)
 
 struct _EMFormatHTMLDisplayPrivate {
-		void *dummy;
+		EFlag *attachment_loaded;
 };
 
 /* TODO: move the dialogue elsehwere */
@@ -400,6 +402,43 @@ efhd_xpkcs7mime_button (EMFormat *emf,
 	return box;
 }
 
+struct attachment_load_data {
+	EAttachment *attachment;
+	EMFormatHTMLDisplay *efhd;
+};
+
+static void
+attachment_loaded (EAttachment *attachment,
+		   GAsyncResult *res,
+		   gpointer user_data)
+{
+	struct attachment_load_data *data = user_data;
+	EShell *shell;
+	GtkWindow *window;
+	
+	shell = e_shell_get_default ();
+	window = e_shell_get_active_window (shell);
+	if (!E_IS_SHELL_WINDOW (window))
+		window = NULL;
+	
+	e_attachment_load_handle_error (data->attachment, res, window);
+
+	e_flag_set (data->efhd->priv->attachment_loaded);
+
+	g_object_unref (data->attachment);
+	g_object_unref (data->efhd);
+	g_free (data);
+}
+
+/* Idle callback */
+static void
+load_attachment_idle (struct attachment_load_data *data)
+{
+	e_attachment_load_async (data->attachment,
+		(GAsyncReadyCallback) attachment_loaded, data);
+}
+
+
 static void
 efhd_parse_attachment (EMFormat *emf,
                        CamelMimePart *part,
@@ -411,15 +450,13 @@ efhd_parse_attachment (EMFormat *emf,
 	EMFormatHTMLDisplay *efhd = (EMFormatHTMLDisplay *) emf;
 	EMFormatAttachmentPURI *puri;
 	EAttachmentStore *store;
-	EShell *shell;
-	GtkWindow *window;
-	GtkWidget *parent;
 	const EMFormatHandler *handler;
 	CamelContentType *ct;
 	gchar *mime_type;
 	gint len;
 	const gchar *cid;
 	guint32 size;
+	struct attachment_load_data *load_data;
 
 	if (g_cancellable_is_cancelled (cancellable))
 		return;
@@ -494,14 +531,6 @@ efhd_parse_attachment (EMFormat *emf,
         }
 	e_attachment_set_can_show (puri->attachment, puri->handle != NULL && puri->handle->write_func);
 
-	/* FIXME: Try to find a better way? */
-	shell = e_shell_get_default ();
-	window = e_shell_get_active_window (shell);
-	if (E_IS_SHELL_WINDOW (window))
-		parent = GTK_WIDGET (window);
-	else
-		parent = NULL;
-
 	store = find_parent_attachment_store (efhd, part_id);
 	e_attachment_store_add_attachment (store, puri->attachment);
 
@@ -524,9 +553,17 @@ efhd_parse_attachment (EMFormat *emf,
 		}
 	}
 
-	e_attachment_load_async (
-		puri->attachment, (GAsyncReadyCallback)
-		e_attachment_load_handle_error, parent);
+	e_flag_clear (efhd->priv->attachment_loaded);
+
+	load_data = g_new0 (struct attachment_load_data, 1);
+	load_data->attachment = g_object_ref (puri->attachment);
+	load_data->efhd = g_object_ref (efhd);
+
+	/* e_attachment_load_async must be called from main thread */
+        g_idle_add ((GSourceFunc) load_attachment_idle, load_data);
+
+	e_flag_wait (efhd->priv->attachment_loaded);
+
 	if (size != 0) {
 		GFileInfo *fileinfo;
 
@@ -810,6 +847,11 @@ efhd_finalize (GObject *object)
 	efhd = EM_FORMAT_HTML_DISPLAY (object);
 	g_return_if_fail (efhd != NULL);
 
+	if (efhd->priv->attachment_loaded) {
+		e_flag_free (efhd->priv->attachment_loaded);
+		efhd->priv->attachment_loaded = NULL;
+	}
+
 	/* Chain up to parent's finalize() method. */
 	G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -843,6 +885,8 @@ efhd_init (EMFormatHTMLDisplay *efhd)
 
 	efhd->priv = EM_FORMAT_HTML_DISPLAY_GET_PRIVATE (efhd);
 
+	efhd->priv->attachment_loaded = e_flag_new ();
+
 	/* we want to convert url's etc */
 	EM_FORMAT_HTML (efhd)->text_html_flags |=
 		CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS |



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