* moded find_body_part from modest-tny-msg-view to modest-tny-msg-actions
[modest] / src / modest-tny-msg-actions.c
index cf40598..8b0e68c 100644 (file)
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
-#endif /*HAVE_CONFIG_H*/
+#endif /*HAVE_CONFIG_H */
 
+#include "modest-tny-msg-actions.h"
 #include "modest-text-utils.h"
 
 
 /* private */
-static gchar *quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml);
-static GtkTextBuffer *htmltotext(TnyMsgMimePartIface * body);
-
-
+static gchar *quote_msg (const TnyMsgIface * src, const gchar * from,
+                        time_t sent_date, gint limit, gboolean textorhtml);
+static GtkTextBuffer *htmltotext (TnyMsgMimePartIface * body);
 static GtkTextBuffer *
-htmltotext(TnyMsgMimePartIface * body)
+htmltotext (TnyMsgMimePartIface * body)
 {
        GtkTextBuffer *buf;
+
 #ifdef ACTIVATE_HACKS
        GtkWidget *html, *win;
-       TnyStreamIface *stream; 
+       TnyStreamIface *stream;
        GtkClipboard *clip;
        gchar *text;
-       
-       win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       html = gtk_html_new();
-       gtk_container_add(GTK_CONTAINER(win), html);
-       
-       gtk_html_set_editable        (GTK_HTML(html), FALSE);
-       gtk_html_allow_selection     (GTK_HTML(html), TRUE);
-       gtk_html_set_caret_mode      (GTK_HTML(html), FALSE);
-       gtk_html_set_blocking        (GTK_HTML(html), FALSE);
-       gtk_html_set_images_blocking (GTK_HTML(html), FALSE);
-       
-       stream = TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(
-               gtk_html_begin(GTK_HTML(html))));
-       
+
+       win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       html = gtk_html_new ();
+       gtk_container_add (GTK_CONTAINER (win), html);
+
+       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))));
+
        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);
+       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);
-       
-       buf = gtk_text_buffer_new(NULL);
-       gtk_text_buffer_set_text(buf, text, -1);
-       g_free(text);
+       text = gtk_clipboard_wait_for_text (clip);
+
+       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);
+       buf = gtk_text_buffer_new (NULL);
 #endif
        return buf;
 }
 
 gchar *
-modest_tny_msg_actions_quote(const TnyMsgIface *self, const gchar *from, time_t sent_date, gint limit, GtkTextBuffer *to_quote)
+modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
+                             time_t sent_date, gint limit,
+                             char * to_quote)
 {
        gchar *quoted;
 
        /* 3 cases: */
-       
+
        /* a) quote text from selection */
        if (to_quote != NULL) {
-               return modest_text_utils_quote(to_quote, from, sent_date, limit);
+               return modest_text_utils_quote (to_quote, from, sent_date,
+                                               limit);
        }
-               
+
        /* b) try to find a text/plain part in the msg and quote it */
-       quoted = quote_msg(self, from, sent_date, limit, FALSE);
+       quoted = quote_msg (self, from, sent_date, limit, FALSE);
        if (quoted != NULL)
                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, TRUE);
 }
-       
-       
+
+
 static gchar *
-quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml)
+quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date,
+          gint limit, gboolean textorhtml)
 {
-
-       GList *parts; /* LEAK? */
-       TnyStreamIface* stream;
-       TnyTextBufferStream *dest;
-       TnyMsgMimePartIface *body = NULL;
-       TnyMsgMimePartIface *part;
+       TnyStreamIface *stream;
+       TnyMsgMimePartIface *body;
        GtkTextBuffer *buf;
        gchar *quoted;
-       
-       /* is the warning in this line due to a bug in tinymail? */
-       parts  = (GList*) tny_msg_iface_get_parts (src);
 
-       while (parts) {
-               part = TNY_MSG_MIME_PART_IFACE(parts->data);
-               if (tny_msg_mime_part_iface_content_type_is (part, textorhtml ? "text/html" : "text/plain")) {
-                       body = part;
-                       break;
-               }
-               parts = parts->next;
-       }
+       /* the cast makes me uneasy... */
+       body = modest_tny_msg_actions_find_body_part((TnyMsgIface *) src,
+                                                                                                textorhtml ? "text/html" : "text/plain");
        
-       if (!body) {
+       if (!body)
                return NULL;
-       }
-       
+
        if (textorhtml == TRUE) {
-               buf = htmltotext(body);
+               buf = htmltotext (body);
        } else {
-               buf    = gtk_text_buffer_new (NULL);
-               stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
+               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);
        }
-       
-       quoted = modest_text_utils_quote (buf, from, sent_date, limit);
-               
-       g_object_unref(stream);
-       g_object_unref(buf);
+
+       quoted = modest_text_utils_quote_text_buffer (buf, from, sent_date, limit);
+
+       g_object_unref (buf);
        return quoted;
 }
+
+
+TnyMsgMimePartIface *
+modest_tny_msg_actions_find_body_part (TnyMsgIface *self, const gchar *mime_type)
+{
+       TnyMsgMimePartIface *part = NULL;
+       GList *parts;
+
+       g_return_val_if_fail (self, NULL);
+       g_return_val_if_fail (mime_type, NULL);
+
+       parts  = (GList*) tny_msg_iface_get_parts (self);
+       while (parts && !part) {
+               part = TNY_MSG_MIME_PART_IFACE(parts->data);
+               if (!tny_msg_mime_part_iface_content_type_is (part, mime_type))
+                       part = NULL;
+               parts = parts->next;
+       }
+       
+       return part;
+}