From 4acb405a5f486a979865804a475f6159ac74c32a Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 31 Jan 2007 22:03:54 +0000 Subject: [PATCH] * renamed modest-tny-msg-action.[ch] => modest-tny-msg.[ch] * cleaned up mem mgmt in modest-formatter, fixed some leaks, and made is resistant against NULL bodies * fixed two leaks in modest-msg-view pmo-trunk-r770 --- src/Makefile.am | 4 +- src/gtk/modest-edit-msg-window.c | 35 +++---- src/gtk/modest-main-window.c | 2 +- src/modest-formatter.c | 24 ++--- src/modest-mail-operation.c | 9 +- src/modest-text-utils.c | 2 +- src/modest-tny-msg-actions.c | 190 -------------------------------------- src/modest-tny-msg-actions.h | 71 -------------- src/modest-tny-msg.c | 174 ++++++++++++++++++++++++++++++++++ src/modest-tny-msg.h | 72 +++++++++++++++ src/widgets/modest-msg-view.c | 38 ++++---- 11 files changed, 299 insertions(+), 322 deletions(-) delete mode 100644 src/modest-tny-msg-actions.c delete mode 100644 src/modest-tny-msg-actions.h create mode 100644 src/modest-tny-msg.c create mode 100644 src/modest-tny-msg.h diff --git a/src/Makefile.am b/src/Makefile.am index ee2ae40..aa07f53 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -71,8 +71,8 @@ modest_SOURCES=\ modest-tny-account.h\ modest-tny-folder.c \ modest-tny-folder.h \ - modest-tny-msg-actions.c\ - modest-tny-msg-actions.h\ + modest-tny-msg.c\ + modest-tny-msg.h\ modest-tny-platform-factory.c \ modest-tny-platform-factory.h \ modest-tny-send-queue.c\ diff --git a/src/gtk/modest-edit-msg-window.c b/src/gtk/modest-edit-msg-window.c index fb4aaf8..b96b28e 100644 --- a/src/gtk/modest-edit-msg-window.c +++ b/src/gtk/modest-edit-msg-window.c @@ -32,20 +32,13 @@ #include #include #include - -#include "modest-account-mgr.h" -#include "modest-account-mgr-helpers.h" -#include "modest-edit-msg-window.h" -#include "modest-edit-msg-window-ui.h" -#include "modest-icon-names.h" -#include "modest-icon-factory.h" -#include "modest-mail-operation.h" -#include "modest-text-utils.h" -#include "modest-tny-platform-factory.h" -#include "modest-tny-msg-actions.h" -#include "modest-ui-actions.h" -#include "modest-widget-memory.h" -#include "modest-window-priv.h" +#include +#include +#include +#include +#include +#include +#include static void modest_edit_msg_window_class_init (ModestEditMsgWindowClass *klass); static void modest_edit_msg_window_init (ModestEditMsgWindow *obj); @@ -290,7 +283,8 @@ modest_edit_msg_window_set_msg (ModestEditMsgWindow *self, TnyMsg *msg) GtkTextBuffer *buf; const gchar *to, *cc, *bcc, *subject; ModestEditMsgWindowPrivate *priv; - + gchar *body; + g_return_if_fail (MODEST_IS_EDIT_MSG_WINDOW (self)); g_return_if_fail (TNY_IS_MSG (msg)); @@ -311,11 +305,12 @@ modest_edit_msg_window_set_msg (ModestEditMsgWindow *self, TnyMsg *msg) if (subject) gtk_entry_set_text (GTK_ENTRY(priv->subject_field), subject); - buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW(priv->msg_body)); - gtk_text_buffer_set_text (buf, - (const gchar *) modest_tny_msg_actions_find_body (msg, FALSE), - -1); - + buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW(priv->msg_body)); + body = modest_tny_msg_get_body (msg, FALSE); + if (body) + gtk_text_buffer_set_text (buf, body, -1); + g_free (body); + /* TODO: lower priority, select in the From: combo to the value that comes from msg <- not sure, should it be allowed? */ diff --git a/src/gtk/modest-main-window.c b/src/gtk/modest-main-window.c index a1062a9..aed39e4 100644 --- a/src/gtk/modest-main-window.c +++ b/src/gtk/modest-main-window.c @@ -41,7 +41,7 @@ #include "modest-account-mgr.h" #include "modest-conf.h" #include "modest-edit-msg-window.h" -#include "modest-tny-msg-actions.h" +#include #include "modest-mail-operation.h" #include "modest-icon-names.h" diff --git a/src/modest-formatter.c b/src/modest-formatter.c index ddbd2e5..a24f6b7 100644 --- a/src/modest-formatter.c +++ b/src/modest-formatter.c @@ -49,9 +49,7 @@ static GObjectClass *parent_class = NULL; typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header); -static TnyMsg *modest_formatter_do (ModestFormatter *self, - TnyMimePart *body, - TnyHeader *header, +static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func); static gchar* modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header); @@ -74,26 +72,23 @@ extract_text (ModestFormatter *self, TnyMimePart *body) tny_stream_reset (stream); g_object_unref (G_OBJECT(stream)); - g_object_unref (G_OBJECT(body)); gtk_text_buffer_get_bounds (buf, &start, &end); text = gtk_text_buffer_get_text (buf, &start, &end, FALSE); - g_object_unref (buf); + g_object_unref (G_OBJECT(buf)); /* Convert to desired content type if needed */ priv = MODEST_FORMATTER_GET_PRIVATE (self); - if (strcmp (tny_mime_part_get_content_type (body), priv->content_type)) { + if (strcmp (tny_mime_part_get_content_type (body), priv->content_type) == 0) { if (!strcmp (priv->content_type, "text/html")) converted_text = modest_text_utils_convert_to_html (text); else converted_text = g_strdup (text); -/* converted_text = modest_text_utils_convert_to_plain (text); */ g_free (text); text = converted_text; } - return text; } @@ -119,10 +114,7 @@ construct_from_text (TnyMimePart *part, } static TnyMsg * -modest_formatter_do (ModestFormatter *self, - TnyMimePart *body, - TnyHeader *header, - FormatterFunc func) +modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func) { TnyMsg *new_msg = NULL; gchar *body_text = NULL, *txt = NULL; @@ -130,14 +122,18 @@ modest_formatter_do (ModestFormatter *self, TnyPlatformFactory *fact; g_return_val_if_fail (self, NULL); - g_return_val_if_fail (body, NULL); g_return_val_if_fail (header, NULL); g_return_val_if_fail (func, NULL); /* Build new part */ fact = modest_tny_platform_factory_get_instance (); new_msg = tny_platform_factory_new_msg (fact); - body_text = extract_text (self, body); + + if (body) + body_text = extract_text (self, body); + else + body_text = g_strdup (""); + txt = (gchar *) func (self, (const gchar*) body_text, header); priv = MODEST_FORMATTER_GET_PRIVATE (self); construct_from_text (TNY_MIME_PART (new_msg), (const gchar*) txt, priv->content_type); diff --git a/src/modest-mail-operation.c b/src/modest-mail-operation.c index d67f7fb..bad978a 100644 --- a/src/modest-mail-operation.c +++ b/src/modest-mail-operation.c @@ -41,7 +41,7 @@ #include #include #include "modest-text-utils.h" -#include "modest-tny-msg-actions.h" +#include "modest-tny-msg.h" #include "modest-tny-platform-factory.h" #include "modest-marshal.h" #include "modest-formatter.h" @@ -308,7 +308,7 @@ create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, gu /* Get body from original msg. Always look for the text/plain part of the message to create the reply/forwarded mail */ header = tny_msg_get_header (msg); - body = modest_tny_msg_actions_find_body_part (msg, FALSE); + body = modest_tny_msg_find_body_part (msg, FALSE); /* TODO: select the formatter from account prefs */ formatter = modest_formatter_new ("text/plain"); @@ -335,8 +335,9 @@ create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, gu break; } } - g_object_unref (G_OBJECT (formatter)); - + g_object_unref (G_OBJECT(formatter)); + g_object_unref (G_OBJECT(body)); + /* Fill the header */ fact = modest_tny_platform_factory_get_instance (); new_header = TNY_HEADER (tny_platform_factory_new_header (fact)); diff --git a/src/modest-text-utils.c b/src/modest-text-utils.c index 88b40ea..e1f82cc 100644 --- a/src/modest-text-utils.c +++ b/src/modest-text-utils.c @@ -526,7 +526,7 @@ modest_text_utils_quote_plain_text (const gchar *text, } else { do { breakpoint = - get_breakpoint (remaining-> str, + get_breakpoint (remaining->str, rem_indent, limit); append_quoted (q, rem_indent, diff --git a/src/modest-tny-msg-actions.c b/src/modest-tny-msg-actions.c deleted file mode 100644 index ccce454..0000000 --- a/src/modest-tny-msg-actions.c +++ /dev/null @@ -1,190 +0,0 @@ -/* Copyright (c) 2006, Nokia Corporation - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the Nokia Corporation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -#ifdef HAVE_CONFIG_H -#include -#endif /*HAVE_CONFIG_H */ - -#include "modest-tny-msg-actions.h" -#include "modest-text-utils.h" - -static const gchar * -get_body_text (TnyMsg *msg, gboolean want_html) -{ - TnyStream *stream; - TnyMimePart *body; - GtkTextBuffer *buf; - GtkTextIter start, end; - const gchar *to_quote; - - body = modest_tny_msg_actions_find_body_part(msg, want_html); - if (!body) - return NULL; - - buf = gtk_text_buffer_new (NULL); - stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf)); - tny_stream_reset (stream); - tny_mime_part_decode_to_stream (body, stream); - tny_stream_reset (stream); - - g_object_unref (G_OBJECT(stream)); - g_object_unref (G_OBJECT(body)); - - gtk_text_buffer_get_bounds (buf, &start, &end); - to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE); - g_object_unref (buf); - - return to_quote; -} - -static TnyMimePart* -modest_tny_msg_actions_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html) -{ - const gchar *mime_type = want_html ? "text/html" : "text/plain"; - TnyMimePart *part = NULL; - TnyList *parts; - TnyIterator *iter; - - if (!msg) - return NULL; - - parts = TNY_LIST (tny_simple_list_new()); - tny_mime_part_get_parts (TNY_MIME_PART (msg), parts); - - iter = tny_list_create_iterator(parts); - - /* no parts? assume it's single-part message */ - if (tny_iterator_is_done(iter)) - return TNY_MIME_PART (g_object_ref(G_OBJECT(msg))); - else { - do { - const gchar *ct; - gchar *content_type; - part = TNY_MIME_PART(tny_iterator_get_current (iter)); - - /* we need to strdown the content type, because - * tny_mime_part_has_content_type does not do it... - */ - ct = tny_mime_part_get_content_type (part); - content_type = g_ascii_strdown (ct, strlen(ct)); - - if (g_str_has_prefix (content_type, mime_type) && - !tny_mime_part_is_attachment (part)) { - g_free (content_type); - break; - } - - if (g_str_has_prefix(content_type, "multipart")) { - part = modest_tny_msg_actions_find_body_part_from_mime_part (part, - want_html); - g_free (content_type); - if (part) - break; - } - - g_free (content_type); - part = NULL; - tny_iterator_next (iter); - - } while (!tny_iterator_is_done(iter)); - } - - /* did we find a matching part? */ - if (part) - g_object_ref (G_OBJECT(part)); - - g_object_unref (G_OBJECT(iter)); - g_object_unref (G_OBJECT(parts)); - - /* if were trying to find an HTML part and couldn't find it, - * try to find a text/plain part instead - */ - if (!part && want_html) - return modest_tny_msg_actions_find_body_part_from_mime_part (msg, FALSE); - - if (!part) - g_printerr ("modest: cannot find body part\n"); - - return part ? part : NULL; -} - - -TnyMimePart* -modest_tny_msg_actions_find_body_part (TnyMsg *msg, gboolean want_html) -{ - return modest_tny_msg_actions_find_body_part_from_mime_part (TNY_MIME_PART(msg), - want_html); -} - - -TnyMimePart * -modest_tny_msg_actions_find_nth_part (TnyMsg *msg, gint index) -{ - TnyMimePart *part; - TnyList *parts; - TnyIterator *iter; - - g_return_val_if_fail (msg, NULL); - g_return_val_if_fail (index > 0, NULL); - - parts = TNY_LIST(tny_simple_list_new()); - tny_mime_part_get_parts (TNY_MIME_PART(msg), parts); - iter = tny_list_create_iterator (parts); - - part = NULL; - - if (!tny_iterator_is_done(iter)) { - tny_iterator_nth (iter, index); - part = TNY_MIME_PART(tny_iterator_get_current (iter)); - } - - g_object_unref (G_OBJECT(iter)); - g_object_unref (G_OBJECT(parts)); - - return part; -} - -gchar * -modest_tny_msg_actions_find_body (TnyMsg *msg, gboolean want_html) -{ - const gchar *body; - - body = get_body_text (msg, want_html); - - if (body) - return g_strdup (body); - else - return NULL; -} diff --git a/src/modest-tny-msg-actions.h b/src/modest-tny-msg-actions.h deleted file mode 100644 index 8e43fad..0000000 --- a/src/modest-tny-msg-actions.h +++ /dev/null @@ -1,71 +0,0 @@ -/* Copyright (c) 2006, Nokia Corporation - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the Nokia Corporation nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __MODEST_TNY_MSG_ACTIONS_H__ -#define __MODEST_TNY_MSG_ACTIONS_H__ - -/** - * 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 the body part. if @want_html is true, try HTML mail - * first. - * - * Returns: the TnyMsgMimePart for the found part, or NULL if no matching part is found - */ -TnyMimePart *modest_tny_msg_actions_find_body_part (TnyMsg * 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 TnyMsgMimePart for the found part, or NULL if no - * matching part is foundi; must be unref'd - */ -TnyMimePart* modest_tny_msg_actions_find_nth_part (TnyMsg *msg, gint index); - - -/** - * modest_tny_msg_actions_find_body: - * @self: - * @want_html: - * - * gets the body of a message as text, if @want_html is true, try HTML mail - * first. - * - * Returns: the body of the message as text - **/ -gchar* modest_tny_msg_actions_find_body (TnyMsg *self, gboolean want_html); - -#endif /* __MODEST_TNY_MSG_ACTIONS_H__ */ diff --git a/src/modest-tny-msg.c b/src/modest-tny-msg.c new file mode 100644 index 0000000..9cd96a4 --- /dev/null +++ b/src/modest-tny-msg.c @@ -0,0 +1,174 @@ +/* Copyright (c) 2006, Nokia Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Nokia Corporation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H */ + +#include +#include "modest-text-utils.h" + + +gchar * +modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html) +{ + TnyStream *stream; + TnyMimePart *body; + GtkTextBuffer *buf; + GtkTextIter start, end; + gchar *to_quote; + + body = modest_tny_msg_find_body_part(msg, want_html); + if (!body) + return NULL; + + buf = gtk_text_buffer_new (NULL); + stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf)); + tny_stream_reset (stream); + tny_mime_part_decode_to_stream (body, stream); + tny_stream_reset (stream); + + g_object_unref (G_OBJECT(stream)); + g_object_unref (G_OBJECT(body)); + + gtk_text_buffer_get_bounds (buf, &start, &end); + to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE); + g_object_unref (buf); + + return to_quote; +} + + +TnyMimePart* +modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html) +{ + const gchar *mime_type = want_html ? "text/html" : "text/plain"; + TnyMimePart *part = NULL; + TnyList *parts; + TnyIterator *iter; + + if (!msg) + return NULL; + + parts = TNY_LIST (tny_simple_list_new()); + tny_mime_part_get_parts (TNY_MIME_PART (msg), parts); + + iter = tny_list_create_iterator(parts); + + /* no parts? assume it's single-part message */ + if (tny_iterator_is_done(iter)) + return TNY_MIME_PART (g_object_ref(G_OBJECT(msg))); + else { + gchar *content_type = NULL; + do { + part = TNY_MIME_PART(tny_iterator_get_current (iter)); + + /* we need to strdown the content type, because + * tny_mime_part_has_content_type does not do it... + */ + content_type = g_ascii_strdown + (tny_mime_part_get_content_type (part), -1); + + if (g_str_has_prefix (content_type, mime_type) && + !tny_mime_part_is_attachment (part)) + break; + + if (g_str_has_prefix(content_type, "multipart")) { + part = modest_tny_msg_find_body_part_from_mime_part (part, want_html); + if (part) + break; + } + if (part) + g_object_unref (G_OBJECT(part)); + + part = NULL; + + g_free (content_type); + content_type = NULL; + + tny_iterator_next (iter); + + } while (!tny_iterator_is_done(iter)); + g_free (content_type); + } + + g_object_unref (G_OBJECT(iter)); + g_object_unref (G_OBJECT(parts)); + + /* if were trying to find an HTML part and couldn't find it, + * try to find a text/plain part instead + */ + if (!part && want_html) + return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE); + else + return part; /* this maybe NULL, this is not an error; some message just don't have a body + * part */ +} + + +TnyMimePart* +modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html) +{ + return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg), + want_html); +} + + +TnyMimePart * +modest_tny_msg_find_nth_part (TnyMsg *msg, gint index) +{ + TnyMimePart *part; + TnyList *parts; + TnyIterator *iter; + + g_return_val_if_fail (msg, NULL); + g_return_val_if_fail (index > 0, NULL); + + parts = TNY_LIST(tny_simple_list_new()); + tny_mime_part_get_parts (TNY_MIME_PART(msg), parts); + iter = tny_list_create_iterator (parts); + + part = NULL; + + if (!tny_iterator_is_done(iter)) { + tny_iterator_nth (iter, index); + part = TNY_MIME_PART(tny_iterator_get_current (iter)); + } + + g_object_unref (G_OBJECT(iter)); + g_object_unref (G_OBJECT(parts)); + + return part; +} diff --git a/src/modest-tny-msg.h b/src/modest-tny-msg.h new file mode 100644 index 0000000..edfcdd5 --- /dev/null +++ b/src/modest-tny-msg.h @@ -0,0 +1,72 @@ +/* Copyright (c) 2006, Nokia Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Nokia Corporation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __MODEST_TNY_MSG_H__ +#define __MODEST_TNY_MSG_H__ + +/** + * modest_tny_msg_find_body_part: + * @self: a message + * @want_html: prefer HTML-part when there are multiple body parts? + * + * search a message for the body part. if @want_html is true, try HTML mail + * first. + * + * Returns: the TnyMsgMimePart for the found part, or NULL if no matching part is found + */ +TnyMimePart *modest_tny_msg_find_body_part (TnyMsg * self, gboolean want_html); + + +/** + * modest_tny_msg_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 TnyMsgMimePart for the found part, or NULL if no + * matching part is foundi; must be unref'd + */ +TnyMimePart* modest_tny_msg_find_nth_part (TnyMsg *msg, gint index); + + +/** + * modest_tny_msg_find_body: + * @self: + * @want_html: + * + * gets the body of a message as text, if @want_html is true, try HTML mail + * first. + * + * Returns: the body of the message as text, or NULL if it is not found + * the text should be freed with + **/ +gchar* modest_tny_msg_get_body (TnyMsg *self, gboolean want_html); + +#endif /* __MODEST_TNY_MSG_H__ */ diff --git a/src/widgets/modest-msg-view.c b/src/widgets/modest-msg-view.c index 77bbc2b..83d3848 100644 --- a/src/widgets/modest-msg-view.c +++ b/src/widgets/modest-msg-view.c @@ -38,7 +38,7 @@ #include #include -#include +#include #include #include "modest-msg-view.h" #include "modest-tny-stream-gtkhtml.h" @@ -98,7 +98,7 @@ modest_msg_view_get_type (void) (GInstanceInitFunc) modest_msg_view_init, NULL }; - my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW, + my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW, "ModestMsgView", &my_info, 0); } @@ -352,8 +352,7 @@ attachments_as_html (ModestMsgView *self, TnyMsg *msg) while (!tny_iterator_is_done(iter)) { TnyMimePart *part; - ++index; /* attachment numbers are 1-based */ - + ++index; /* attachment numbers are 1-based */ part = TNY_MIME_PART(tny_iterator_get_current (iter)); if (tny_mime_part_is_attachment (part)) { @@ -365,9 +364,12 @@ attachments_as_html (ModestMsgView *self, TnyMsg *msg) g_string_append_printf (appendix, "%s \n", ATT_PREFIX, index, filename); } + + g_object_unref (G_OBJECT(part)); tny_iterator_next (iter); } g_object_unref (G_OBJECT(iter)); + g_object_unref (G_OBJECT(parts)); if (appendix->len == 0) return g_string_free (appendix, TRUE); @@ -386,7 +388,8 @@ static gboolean set_html_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg) { gchar *html_attachments; - TnyStream *gtkhtml_stream; + GtkHTMLStream *gtkhtml_stream; + TnyStream *tny_stream; ModestMsgViewPrivate *priv; g_return_val_if_fail (self, FALSE); @@ -394,25 +397,22 @@ set_html_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg) priv = MODEST_MSG_VIEW_GET_PRIVATE(self); - gtkhtml_stream = - TNY_STREAM(modest_tny_stream_gtkhtml_new - (gtk_html_begin(GTK_HTML(priv->gtkhtml)))); - - tny_stream_reset (gtkhtml_stream); + gtkhtml_stream = gtk_html_begin(GTK_HTML(priv->gtkhtml)); + + tny_stream = TNY_STREAM(modest_tny_stream_gtkhtml_new (gtkhtml_stream)); + tny_stream_reset (tny_stream); html_attachments = attachments_as_html(self, msg); if (html_attachments) { - tny_stream_write (gtkhtml_stream, html_attachments, - strlen(html_attachments)); - tny_stream_reset (gtkhtml_stream); + tny_stream_write (tny_stream, html_attachments, strlen(html_attachments)); + tny_stream_reset (tny_stream); g_free (html_attachments); } - // FIXME: tinymail - tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, - gtkhtml_stream); - - g_object_unref (G_OBJECT(gtkhtml_stream)); + tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body, tny_stream); + g_object_unref (G_OBJECT(tny_stream)); + + gtk_html_stream_destroy (gtkhtml_stream); return TRUE; } @@ -513,7 +513,7 @@ modest_msg_view_set_message (ModestMsgView *self, TnyMsg *msg) return; } - body = modest_tny_msg_actions_find_body_part (msg, TRUE); + body = modest_tny_msg_find_body_part (msg, TRUE); if (body) { if (tny_mime_part_content_type_is (body, "text/html")) set_html_message (self, body, msg); -- 1.7.9.5