From f2122c69d6fd23776cf1574af7a6d42596db3bdf Mon Sep 17 00:00:00 2001 From: Matthew Loper Date: Wed, 26 Jan 2000 00:53:53 +0000 Subject: + * camel/camel-formatter.c: By looking up a mimetype in a + hashtable, we can now get a handler function for an arbitrary + mimetype. svn path=/trunk/; revision=1641 --- camel/camel-formatter.c | 203 +++++++++++++++++++++++++++++++++++++----------- camel/camel-formatter.h | 6 +- 2 files changed, 159 insertions(+), 50 deletions(-) (limited to 'camel') diff --git a/camel/camel-formatter.c b/camel/camel-formatter.c index fec74db7e2..ae426c2e63 100644 --- a/camel/camel-formatter.c +++ b/camel/camel-formatter.c @@ -32,6 +32,8 @@ struct _CamelFormatterPrivate { /* nothing here yet */ }; +GHashTable *mime_function_table = NULL; + static void write_field_to_stream (gchar* description, gchar* value, CamelStream *stream) { @@ -47,24 +49,24 @@ write_field_to_stream (gchar* description, gchar* value, CamelStream *stream) static void write_recipients_to_stream (const gchar *recipient_type, const GList *recipients, - CamelStream* stream_out) + CamelStream* stream) { gchar *s; - g_assert (recipient_type && stream_out); + g_assert (recipient_type && stream); /* Write "To:", "CC:", or "BCC:" to the stream */ s = g_strdup_printf ("%s: ", recipient_type); - camel_stream_write_string (stream_out, s); + camel_stream_write_string (stream, s); g_free (s); /* Write out each recipient of 'recipient_type' to the stream */ while (recipients) { - camel_stream_write_string (stream_out, recipients->data); + camel_stream_write_string (stream, recipients->data); recipients = recipients->next; if (recipients) - camel_stream_write_string (stream_out, "; "); + camel_stream_write_string (stream, "; "); } - camel_stream_write_string (stream_out, "

\n"); + camel_stream_write_string (stream, "

\n"); } @@ -77,32 +79,32 @@ camel_formatter_new () static void write_header_info_to_stream (CamelMimeMessage* mime_message, - CamelStream* stream_out) + CamelStream* stream) { gchar *s = NULL; const GList *recipients = NULL; - g_assert (mime_message && stream_out); + g_assert (mime_message && stream); - camel_stream_write_string (stream_out, "Content type: text/html\n"); + camel_stream_write_string (stream, "Content type: text/html\n"); /* A few fields will probably be available from the mime_message; for each one that's available, write it to the output stream with a helper function, 'write_field_to_stream'. */ if ((s = (gchar*)camel_mime_message_get_subject (mime_message))) { - write_field_to_stream ("Subject: ", s, stream_out); + write_field_to_stream ("Subject: ", s, stream); } if ((s = (gchar*)camel_mime_message_get_from (mime_message))) { - write_field_to_stream ("From: ", s, stream_out); + write_field_to_stream ("From: ", s, stream); } if ((s = (gchar*)camel_mime_message_get_received_date (mime_message))) { - write_field_to_stream ("Received Date: ", s, stream_out); + write_field_to_stream ("Received Date: ", s, stream); } if ((s = (gchar*)camel_mime_message_get_sent_date (mime_message))) { - write_field_to_stream ("Sent Date: ", s, stream_out); + write_field_to_stream ("Sent Date: ", s, stream); } /* Fill out the "To:" recipients line */ @@ -110,19 +112,19 @@ write_header_info_to_stream (CamelMimeMessage* mime_message, mime_message, CAMEL_RECIPIENT_TYPE_TO); if (recipients) - write_recipients_to_stream ("To:", recipients, stream_out); + write_recipients_to_stream ("To:", recipients, stream); /* Fill out the "CC:" recipients line */ recipients = camel_mime_message_get_recipients ( mime_message, CAMEL_RECIPIENT_TYPE_CC); if (recipients) - write_recipients_to_stream ("CC:", recipients, stream_out); + write_recipients_to_stream ("CC:", recipients, stream); /* Fill out the "BCC:" recipients line */ recipients = camel_mime_message_get_recipients ( mime_message, CAMEL_RECIPIENT_TYPE_BCC); if (recipients) - write_recipients_to_stream ("BCC:", recipients, stream_out); + write_recipients_to_stream ("BCC:", recipients, stream); } @@ -155,7 +157,8 @@ write_mimepart_to_stream (CamelMimePart* mime_part, CamelStream* stream) /* returns NULL if no text/html or text/plan msg is found */ static CamelMimePart* -find_text_body_part_in_multipart_related (CamelMultipart* multipart) +find_preferred_displayable_body_part_in_multipart_related ( + CamelMultipart* multipart) { int i, max_multiparts; CamelMimePart* html_part = NULL; @@ -163,8 +166,10 @@ find_text_body_part_in_multipart_related (CamelMultipart* multipart) /* find out out many parts are in it...*/ max_multiparts = camel_multipart_get_number (multipart); - - /* ...and write each one, as html, into the stream. */ + + /* TODO: DO LEAF-LOOKUP HERE FOR OTHER MIME-TYPES!!! */ + + /* ...and write each one, as html, into the stream. */ for (i = 0; i < max_multiparts; i++) { CamelMimeBodyPart* body_part = camel_multipart_get_part (multipart, i); if (strmatch (MIME_TYPE_SUB (body_part), "plain")) { @@ -182,33 +187,21 @@ find_text_body_part_in_multipart_related (CamelMultipart* multipart) return NULL; } - -/** - * camel_formatter_make_html: - * @formatter: the camel formatter object - * @stream_out: byte stream where data will be written - * - * Writes a CamelMimeMessage out, as html, into a stream passed in as - * a parameter. - **/ -void -camel_formatter_make_html (CamelFormatter* formatter, - CamelMimeMessage* mime_message, - CamelStream* stream_out) +/* Converts the contents of a CamelMimePart into HTML */ +static void +mime_part_to_html (CamelFormatter* formatter, CamelMimePart* part, + CamelStream *stream) { /* Get the mime-type of the mime message */ gchar* mime_type_whole = /* ex. "text/plain" */ - MIME_TYPE_WHOLE (mime_message); + MIME_TYPE_WHOLE (part); /* get the contents of the mime message */ - CamelDataWrapper* message_contents = camel_medium_get_content_object ( - CAMEL_MEDIUM (mime_message)); - - /* write the 'subject:', 'to:', 'from:', etc. into the output stream */ - write_header_info_to_stream (mime_message, stream_out); + CamelDataWrapper* message_contents = + camel_medium_get_content_object (CAMEL_MEDIUM (part)); /* if we're dealing with a multipart/related message... */ - if (strmatch (MIME_TYPE_WHOLE (mime_message), "multipart/related")) { + if (strmatch (MIME_TYPE_WHOLE (part), "multipart/related")) { CamelMultipart *multipart = CAMEL_MULTIPART ( message_contents); @@ -223,18 +216,28 @@ camel_formatter_make_html (CamelFormatter* formatter, CamelMimeBodyPart* body_part = camel_multipart_get_part (multipart, i); - write_mimepart_to_stream (CAMEL_MIME_PART (body_part), stream_out); + /* TODO: insert html delimiters, probably + *
's, before and after the following + * call*/ + mime_part_to_html ( + formatter, CAMEL_MIME_PART (body_part), + stream); } } - else { /* okay, it's not multipart-related */ + /* okay, it's not multipart-related, so we have only one 'thing' + * to convert to html */ + else { CamelMimePart* mime_part = NULL; + /* if it's a multipart/alternate, track down one we can + * convert to html (if any) */ if (strmatch (mime_type_whole, "multipart/alternate")) { mime_part = - find_text_body_part_in_multipart_related ( + find_preferred_displayable_body_part_in_multipart_related ( CAMEL_MULTIPART(message_contents)); } + else if (strmatch (mime_type_whole, "text/plain") || strmatch (mime_type_whole, "text/html")) { @@ -249,13 +252,108 @@ camel_formatter_make_html (CamelFormatter* formatter, "Sorry, but I don't know how to display items of type %s\n", mime_type_whole); - camel_stream_write_string (stream_out, error_string); + camel_stream_write_string (stream, error_string); g_free (error_string); } - } + } + } +/** + * camel_formatter_mime_message_to_html: + * @formatter: the camel formatter object + * @mime_message: the input mime message + * @stream: byte stream where data will be written + * + * Writes a CamelMimeMessage out, as html, into a stream passed in as + * a parameter. + **/ +void +camel_formatter_mime_message_to_html (CamelFormatter* formatter, + CamelMimeMessage* mime_message, + CamelStream* stream) +{ + +} + +typedef void (*mime_handler_fn) (CamelFormatter *formatter, + CamelDataWrapper *data_wrapper, + CamelStream *stream); + +static void +handle_text_plain (CamelFormatter *formatter, CamelDataWrapper *wrapper, + CamelStream *stream) +{ + +} + +static void +handle_html (CamelFormatter *formatter, CamelDataWrapper *wrapper, + CamelStream *stream) +{ + +} + +static void +handle_multipart_related (CamelFormatter *formatter, + CamelDataWrapper *wrapper, + CamelStream *stream) +{ + +} + +static void +handle_multipart_alternate (CamelFormatter *formatter, + CamelDataWrapper *wrapper, + CamelStream *stream) +{ + +} + +static void +handle_unknown_type (CamelFormatter *formatter, + CamelDataWrapper *wrapper, + CamelStream *stream) +{ + +} + +static void +call_handler_function (CamelFormatter* formatter, + CamelDataWrapper* wrapper, gchar* mimetype) +{ + mime_handler_fn handler_function; + + handler_function = g_hash_table_lookup ( + mime_function_table, mimetype); + + if (!handler_function) + handler_function = handle_unknown_type; +} + + +static void +handle_mime_message (CamelFormatter *formatter, + CamelDataWrapper *wrapper, + CamelStream *stream) +{ + CamelMimeMessage* mime_message = + CAMEL_MIME_MESSAGE (wrapper); + + CamelDataWrapper* message_contents = + camel_medium_get_content_object (CAMEL_MEDIUM (mime_message)); + + /* write the subj:, to:, from: etc. fields out as html */ + write_header_info_to_stream (mime_message, stream); + + /* dispatch the correct handler function for the mime type */ + call_handler_function (formatter, message_contents, MIME_TYPE_WHOLE (mime_message)); + + /* close up the table opened by 'write_header_info_to_stream' */ + camel_stream_write_string (stream, ""); +} + static void camel_formatter_class_init (CamelFormatterClass *camel_formatter_class) { @@ -263,12 +361,23 @@ camel_formatter_class_init (CamelFormatterClass *camel_formatter_class) GTK_OBJECT_CLASS (camel_formatter_class); parent_class = gtk_type_class (gtk_object_get_type ()); - - /* virtual method overload */ + + mime_function_table = g_hash_table_new (g_str_hash, g_str_equal); + +#define ADD_HANDLER(a,b) g_hash_table_insert (mime_function_table, a, b) + + /* hook up mime types to functions that handle them */ + ADD_HANDLER ("text/plain", handle_text_plain); + ADD_HANDLER ("text/html", handle_html); + ADD_HANDLER ("multipart/alternate", handle_multipart_alternate); + ADD_HANDLER ("multipart/related", handle_multipart_related); + ADD_HANDLER ("message/rfc822", handle_mime_message); + + /* virtual method overload */ gtk_object_class->finalize = _finalize; } -void +static void camel_formatter_init (gpointer object, gpointer klass) { CamelFormatter* cmf = CAMEL_FORMATTER (object); diff --git a/camel/camel-formatter.h b/camel/camel-formatter.h index 1bcbb2d01a..30dbc2008e 100644 --- a/camel/camel-formatter.h +++ b/camel/camel-formatter.h @@ -61,9 +61,9 @@ CamelFormatter* camel_formatter_new (void); /* The main job of CamelFormatter is to take a mime message, and produce html from it. */ -void camel_formatter_make_html (CamelFormatter* cmf, - CamelMimeMessage *msg, - CamelStream* stream_out); +void camel_formatter_mime_message_to_html (CamelFormatter* formatter, + CamelMimeMessage* mime_message, + CamelStream* stream); #ifdef __cplusplus -- cgit