From e010a0ade7cf553914b9288887b622e20cf8a882 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 12 Jul 2006 06:17:03 +0000 Subject: [PATCH] * update to new iterator API in tinymail * code cleanup / refactoring * added _nth_ method, to get the nth mime-part pmo-trunk-r373 --- src/modest-tny-msg-actions.c | 167 +++++++++++++++++++----------------------- src/modest-tny-msg-actions.h | 29 +++++--- 2 files changed, 94 insertions(+), 102 deletions(-) diff --git a/src/modest-tny-msg-actions.c b/src/modest-tny-msg-actions.c index a646262..d084b7c 100644 --- a/src/modest-tny-msg-actions.c +++ b/src/modest-tny-msg-actions.c @@ -28,8 +28,6 @@ */ -/* modest-ui.c */ - #include #include @@ -37,6 +35,7 @@ #include #include #include +#include #ifdef HAVE_CONFIG_H #include @@ -48,59 +47,47 @@ /* private */ static gchar *quote_msg (const TnyMsgIface * src, const gchar * from, - time_t sent_date, gint limit, gboolean textorhtml); -static GtkTextBuffer *htmltotext (TnyMsgMimePartIface * body); - + time_t sent_date, gint limit); -static GtkTextBuffer * -htmltotext (TnyMsgMimePartIface * body) +static gchar * +quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date, gint limit) { - GtkTextBuffer *buf; - -#ifdef ACTIVATE_HACKS /* it still doesn't work, don't bother! */ - GtkWidget *html, *win; TnyStreamIface *stream; - GtkClipboard *clip; - gchar *text; - - win = gtk_window_new (GTK_WINDOW_TOPLEVEL); - html = gtk_html_new (); - gtk_container_add (GTK_CONTAINER (win), html); + TnyMsgMimePartIface *body; + GtkTextBuffer *buf; + GtkTextIter start, end; + const gchar *to_quote; + gchar *quoted; - gtk_html_set_editable (GTK_HTML (html), FALSE); - gtk_html_allow_selection (GTK_HTML (html), TRUE); - stream = TNY_STREAM_IFACE (modest_tny_stream_gtkhtml_new - (gtk_html_begin (GTK_HTML (html)))); + /* the cast makes me uneasy... */ + body = modest_tny_msg_actions_find_body_part(src, FALSE); + if (!body) + return NULL; + buf = gtk_text_buffer_new (NULL); + stream = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buf)); tny_stream_iface_reset (stream); tny_msg_mime_part_iface_decode_to_stream (body, stream); tny_stream_iface_reset (stream); - g_object_unref (G_OBJECT (stream)); - - gtk_widget_show_all (win); - gtk_html_select_all (GTK_HTML (html)); - clip = gtk_widget_get_clipboard (html, GDK_SELECTION_PRIMARY); - /*clip = gtk_widget_get_clipboard(html, GDK_SELECTION_CLIPBOARD);*/ - text = gtk_clipboard_wait_for_text (clip); + g_object_unref (stream); + + gtk_text_buffer_get_bounds (buf, &start, &end); + to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE); + quoted = modest_text_utils_quote (to_quote, from, sent_date, limit); + g_object_unref (buf); - buf = gtk_text_buffer_new (NULL); - gtk_text_buffer_set_text (buf, text, -1); - g_free (text); - /* destroy win & html */ -#else - buf = gtk_text_buffer_new (NULL); -#endif - return buf; + return quoted; } -gchar * + +gchar* modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from, time_t sent_date, gint limit, const gchar * to_quote) { gchar *quoted; - /* 3 cases: */ + /* 2 cases: */ /* a) quote text from selection */ if (to_quote != NULL) @@ -108,77 +95,71 @@ modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from, limit); /* b) try to find a text/plain part in the msg and quote it */ - quoted = quote_msg (self, from, sent_date, limit, FALSE); - if (quoted) - return quoted; - - /* c) if that fails, try text/html */ - return quote_msg (self, from, sent_date, limit, TRUE); + return quote_msg (self, from, sent_date, limit); } -static gchar * -quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date, - gint limit, gboolean want_html) + +TnyMsgMimePartIface * +modest_tny_msg_actions_find_body_part (const TnyMsgIface *msg, gboolean want_html) { - TnyStreamIface *stream; - TnyMsgMimePartIface *body; - GtkTextBuffer *buf; - GtkTextIter start, end; - const gchar *to_quote; - gchar *quoted; + const gchar *mime_type = want_html ? "text/html" : "text/plain"; + TnyMsgMimePartIface *part; + const TnyListIface *parts; + TnyIteratorIface *iter; + gboolean found; - /* the cast makes me uneasy... */ - body = modest_tny_msg_actions_find_body_part((TnyMsgIface *) src, want_html); - if (!body) + if (!msg) return NULL; - if (want_html) - buf = htmltotext (body); - else { - buf = gtk_text_buffer_new (NULL); - stream = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buf)); - tny_stream_iface_reset (stream); - tny_msg_mime_part_iface_decode_to_stream (body, stream); - tny_stream_iface_reset (stream); - g_object_unref (stream); + found = FALSE; + parts = tny_msg_iface_get_parts ((TnyMsgIface *)msg); + iter = tny_list_iface_create_iterator ((TnyListIface*)parts); + + while (!tny_iterator_iface_is_done(iter)) { + part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current (iter)); + + if (tny_msg_mime_part_iface_content_type_is (part, mime_type) && + !tny_msg_mime_part_iface_is_attachment (part)) { + found = TRUE; + break; + } + tny_iterator_iface_next (iter); } - - gtk_text_buffer_get_bounds (buf, &start, &end); - to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE); - quoted = modest_text_utils_quote (to_quote, from, sent_date, limit); - g_object_unref (buf); - return quoted; + g_object_unref (G_OBJECT(iter)); + + /* if were trying to find an HTML part and could find it, + * try to find a text/plain part instead + */ + if (!found && want_html) + return modest_tny_msg_actions_find_body_part (msg, FALSE); + + return found ? part : NULL; } + + TnyMsgMimePartIface * -modest_tny_msg_actions_find_body_part (TnyMsgIface *self, gboolean want_html) +modest_tny_msg_actions_find_nth_part (const TnyMsgIface *msg, gint index) { - const gchar *mime_type = want_html ? "text/html" : "text/plain"; - TnyMsgMimePartIface *part = NULL; - GList *parts; - - g_return_val_if_fail (self, NULL); - g_return_val_if_fail (mime_type, NULL); + TnyMsgMimePartIface *part; + const TnyListIface *parts; + TnyIteratorIface *iter; - parts = (GList*) tny_msg_iface_get_parts (self); - while (parts && !part) { + g_return_val_if_fail (msg, NULL); + g_return_val_if_fail (index > 0, NULL); + + parts = tny_msg_iface_get_parts ((TnyMsgIface *)msg); + iter = tny_list_iface_create_iterator ((TnyListIface*)parts); - part = TNY_MSG_MIME_PART_IFACE(parts->data); + if (!tny_iterator_iface_has_first(iter)) + return NULL; - if (!tny_msg_mime_part_iface_content_type_is (part, mime_type) - ||tny_msg_mime_part_iface_is_attachment (part)) - part = NULL; - parts = parts->next; - } + part = tny_iterator_iface_nth (iter, index); - /* if were trying to find an HTML part and could find it, - * try to find a text/plain part instead - */ - if (!part && want_html) - return modest_tny_msg_actions_find_body_part (self, FALSE); - else - return part; + g_object_unref (G_OBJECT(iter)); + + return part; } diff --git a/src/modest-tny-msg-actions.h b/src/modest-tny-msg-actions.h index 9704fc8..cc62fb8 100644 --- a/src/modest-tny-msg-actions.h +++ b/src/modest-tny-msg-actions.h @@ -27,9 +27,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - -/* modest-tny-msg-actions.h */ - #ifndef __MODEST_TNY_MSG_ACTIONS_H__ #define __MODEST_TNY_MSG_ACTIONS_H__ @@ -49,23 +46,37 @@ * * Returns: a newly allocated string containing the quoted message */ - gchar *modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from, time_t sent_date, - gint limit, const gchar *to_quote); + gint limit, + const gchar *to_quote); /** * modest_tny_msg_actions_find_body_part: * @self: a message * @want_html: prefer HTML-part when there are multiple body parts? * - * search a message for a body part + * search a message for the body part. if @want_html is true, try HTML mail + * first. * * Returns: the TnyMsgMimePartIface for the found part, or NULL if no matching part is found - */ - -TnyMsgMimePartIface *modest_tny_msg_actions_find_body_part (TnyMsgIface * self, + */ +TnyMsgMimePartIface *modest_tny_msg_actions_find_body_part (const TnyMsgIface * self, gboolean want_html); + +/** + * modest_tny_msg_actions_get_nth_part: + * @self: a message + * @index: number (1-based) of the part to retrieve + * + * search for the nth (mime) part in the message + * + * Returns: the TnyMsgMimePartIface for the found part, or NULL if no matching part is found + */ +TnyMsgMimePartIface * modest_tny_msg_actions_find_nth_part (const TnyMsgIface *msg, + gint index); + + #endif /* __MODEST_TNY_MSG_ACTIONS_H__ */ -- 1.7.9.5