* all:
[modest] / src / modest-tny-msg-actions.c
index cf40598..ccce454 100644 (file)
-/* modest-ui.c */
+/* 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.
+ */
 
-/* insert (c)/licensing information) */
-
-#include <gtk/gtk.h>
+#include <string.h>
 #include <gtkhtml/gtkhtml.h>
-
-/* TODO: put in auto* */
-#include <tny-text-buffer-stream.h>
-#include <tny-msg-mime-part-iface.h>
+#include <tny-gtk-text-buffer-stream.h>
+#include <tny-simple-list.h>
+#include <tny-folder.h>
 
 #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"
 
+static const gchar *
+get_body_text (TnyMsg *msg, gboolean want_html)
+{
+       TnyStream *stream;
+       TnyMimePart *body;
+       GtkTextBuffer *buf;
+       GtkTextIter start, end;
+       const gchar *to_quote;
 
-/* private */
-static gchar *quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml);
-static GtkTextBuffer *htmltotext(TnyMsgMimePartIface * body);
+       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);
 
-static GtkTextBuffer *
-htmltotext(TnyMsgMimePartIface * body)
-{
-       GtkTextBuffer *buf;
-#ifdef ACTIVATE_HACKS
-       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);
-       
-       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))));
-       
-       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));
+       g_object_unref (G_OBJECT(body));
        
-       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);
-       /* destroy win & html */
-#else
-       buf = gtk_text_buffer_new(NULL);
-#endif
-       return buf;
+       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;
 }
 
-gchar *
-modest_tny_msg_actions_quote(const TnyMsgIface *self, const gchar *from, time_t sent_date, gint limit, GtkTextBuffer *to_quote)
+static TnyMimePart*
+modest_tny_msg_actions_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
 {
-       gchar *quoted;
+       const gchar *mime_type = want_html ? "text/html" : "text/plain";
+       TnyMimePart *part = NULL;
+       TnyList *parts;
+       TnyIterator *iter;
 
-       /* 3 cases: */
-       
-       /* a) quote text from selection */
-       if (to_quote != NULL) {
-               return modest_text_utils_quote(to_quote, from, sent_date, limit);
+       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));
        }
-               
-       /* 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 != NULL)
-               return quoted;
-       
-       /* c) if that fails, try text/html */
-       return quote_msg(self, from, sent_date, limit, TRUE);
-}
        
+       /* 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");
        
-static gchar *
-quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date, gint limit, gboolean textorhtml)
+       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);
+}
 
-       GList *parts; /* LEAK? */
-       TnyStreamIface* stream;
-       TnyTextBufferStream *dest;
-       TnyMsgMimePartIface *body = NULL;
-       TnyMsgMimePartIface *part;
-       GtkTextBuffer *buf;
-       gchar *quoted;
+
+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;
        
-       /* 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;
+       if (!tny_iterator_is_done(iter)) {
+               tny_iterator_nth (iter, index);
+               part = TNY_MIME_PART(tny_iterator_get_current (iter));
        }
-       
-       if (!body) {
+
+       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;
-       }
-       
-       if (textorhtml == TRUE) {
-               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);
-       }
-       
-       quoted = modest_text_utils_quote (buf, from, sent_date, limit);
-               
-       g_object_unref(stream);
-       g_object_unref(buf);
-       return quoted;
 }