[evolution/gnome-3-6] Bug #684447 - Fallback to text/plain when highlight program is missing or fails



commit d0734298d21c5a59ed408910b552d7c4ffa79577
Author: Dan VrÃtil <dvratil redhat com>
Date:   Mon Oct 8 23:27:29 2012 +0200

    Bug #684447 - Fallback to text/plain when highlight program is missing or fails
    
    We can't add a new dependency to Evolution 3.6, but the runtime
    check will fallback to text/plain formatter when highlight program
    is missing (or when it crashes or fails).
    
    (cherry-picked from 5437fa7c354b5ddf12b4e13136834d86168e3580)

 em-format/e-mail-formatter-text-plain.c            |    1 +
 .../e-mail-formatter-text-highlight.c              |   85 ++++++++++++-------
 2 files changed, 55 insertions(+), 31 deletions(-)
---
diff --git a/em-format/e-mail-formatter-text-plain.c b/em-format/e-mail-formatter-text-plain.c
index 7fd63b5..5ef5ba3 100644
--- a/em-format/e-mail-formatter-text-plain.c
+++ b/em-format/e-mail-formatter-text-plain.c
@@ -34,6 +34,7 @@
 static const gchar *formatter_mime_types[] = { "text/plain",
 					       "text/*",
 					       "message/*",
+					       "application/vnd.evolution.plaintext",
 					       NULL };
 
 typedef struct _EMailFormatterTextPlain {
diff --git a/modules/text-highlight/e-mail-formatter-text-highlight.c b/modules/text-highlight/e-mail-formatter-text-highlight.c
index 48c6e7a..e0a892c 100644
--- a/modules/text-highlight/e-mail-formatter-text-highlight.c
+++ b/modules/text-highlight/e-mail-formatter-text-highlight.c
@@ -258,43 +258,66 @@ emfe_text_highlight_format (EMailFormatterExtension *extension,
 		argv[3] = g_strdup_printf ("--syntax=%s", syntax);
 		g_free (syntax);
 
-		if (!g_spawn_async_with_pipes (
-			NULL, (gchar **) argv, NULL,
-			G_SPAWN_SEARCH_PATH |
-			G_SPAWN_DO_NOT_REAP_CHILD,
-			NULL, NULL, &pid, &pipe_stdin, &pipe_stdout, NULL, NULL)) {
-			return FALSE;
+		if (g_spawn_async_with_pipes (
+				NULL, (gchar **) argv, NULL,
+				G_SPAWN_SEARCH_PATH |
+				G_SPAWN_DO_NOT_REAP_CHILD,
+				NULL, NULL, &pid, &pipe_stdin, &pipe_stdout, NULL, NULL)) {
+
+			write = camel_stream_fs_new_with_fd (pipe_stdin);
+			read = camel_stream_fs_new_with_fd (pipe_stdout);
+
+			/* Decode the content of mime part to the 'utf8' stream */
+			utf8 = camel_stream_mem_new ();
+			camel_data_wrapper_decode_to_stream_sync (
+				dw, utf8, cancellable, NULL);
+
+			/* Convert the binary data do someting displayable */
+			ba = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (utf8));
+			tmp = e_util_utf8_data_make_valid ((gchar *) ba->data, ba->len);
+
+			/* Send the sanitized data to the highlighter */
+			camel_stream_write_string (write, tmp, cancellable, NULL);
+			g_free (tmp);
+			g_object_unref (utf8);
+			g_object_unref (write);
+
+			g_spawn_close_pid (pid);
+
+			g_seekable_seek (G_SEEKABLE (read), 0, G_SEEK_SET, cancellable, NULL);
+			camel_stream_write_to_stream (read, stream, cancellable, NULL);
+			g_object_unref (read);
+		} else {
+			/* We can't call e_mail_formatter_format_as on text/plain,
+			 * because text-highlight is registered as an handler for
+			 * text/plain, so we would end up in an endless recursion.
+			 *
+			 * Just return FALSE here and EMailFormatter will automatically
+			 * fall back to the default text/plain formatter */
+			if (camel_content_type_is (ct, "text", "plain")) {
+				g_free (font_family);
+				g_free (font_size);
+				g_free ((gchar *) argv[3]);
+				pango_font_description_free (fd);
+
+				return FALSE;
+			} else {
+				/* In case of any other content, force use of
+				 * text/plain formatter, because returning FALSE
+				 * for text/x-patch or application/php would show
+				 * an error, as there is no other handler registered
+				 * for these */
+				e_mail_formatter_format_as (
+					formatter, context, part, stream,
+					"application/vnd.evolution.plaintext",
+					cancellable);
+			}
 		}
 
-		write = camel_stream_fs_new_with_fd (pipe_stdin);
-		read = camel_stream_fs_new_with_fd (pipe_stdout);
-
-		/* Decode the content of mime part to the 'utf8' stream */
-		utf8 = camel_stream_mem_new ();
-		camel_data_wrapper_decode_to_stream_sync (
-			dw, utf8, cancellable, NULL);
-
-		/* Convert the binary data do someting displayable */
-		ba = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (utf8));
-		tmp = e_util_utf8_data_make_valid ((gchar *) ba->data, ba->len);
-
-		/* Send the sanitized data to the highlighter */
-		camel_stream_write_string (write, tmp, cancellable, NULL);
-		g_free (tmp);
-		g_object_unref (utf8);
-		g_object_unref (write);
-
-		g_spawn_close_pid (pid);
-
-		g_seekable_seek (G_SEEKABLE (read), 0, G_SEEK_SET, cancellable, NULL);
-		camel_stream_write_to_stream (read, stream, cancellable, NULL);
-		g_object_unref (read);
-
 		g_free (font_family);
 		g_free (font_size);
 		g_free ((gchar *) argv[3]);
 		pango_font_description_free (fd);
-
 	} else {
 		gchar *uri, *str;
 		gchar *syntax;



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