* added licensing boilerplate to source files
[modest] / src / modest-tny-msg-actions.c
index 4adafaf..a646262 100644 (file)
@@ -1,6 +1,34 @@
-/* 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) */
+/* modest-ui.c */
 
 #include <gtk/gtk.h>
 #include <gtkhtml/gtkhtml.h>
@@ -8,6 +36,7 @@
 /* TODO: put in auto* */
 #include <tny-text-buffer-stream.h>
 #include <tny-msg-mime-part-iface.h>
+#include <tny-msg-iface.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -67,23 +96,22 @@ htmltotext (TnyMsgMimePartIface * body)
 gchar *
 modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
                              time_t sent_date, gint limit,
-                             gchar * to_quote)
+                             const gchar * to_quote)
 {
        gchar *quoted;
 
        /* 3 cases: */
 
        /* a) quote text from selection */
-       if (to_quote != NULL) {
+       if (to_quote != NULL) 
                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);
-       if (quoted != NULL)
+       if (quoted)
                return quoted;
-
+       
        /* c) if that fails, try text/html */
        return quote_msg (self, from, sent_date, limit, TRUE);
 }
@@ -91,7 +119,7 @@ modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
 
 static gchar *
 quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date,
-          gint limit, gboolean textorhtml)
+          gint limit, gboolean want_html)
 {
        TnyStreamIface *stream;
        TnyMsgMimePartIface *body;
@@ -101,15 +129,13 @@ quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date,
        gchar *quoted;
 
        /* the cast makes me uneasy... */
-       body = modest_tny_msg_actions_find_body_part((TnyMsgIface *) src,
-                                                                                                textorhtml ? "text/html" : "text/plain");
-       
+       body = modest_tny_msg_actions_find_body_part((TnyMsgIface *) src, want_html);
        if (!body)
                return NULL;
 
-       if (textorhtml == TRUE) {
+       if (want_html) 
                buf = htmltotext (body);
-       } else {
+       else {
                buf = gtk_text_buffer_new (NULL);
                stream = TNY_STREAM_IFACE (tny_text_buffer_stream_new (buf));
                tny_stream_iface_reset (stream);
@@ -122,26 +148,37 @@ quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date,
        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;
 }
 
 
 TnyMsgMimePartIface *
-modest_tny_msg_actions_find_body_part (TnyMsgIface *self, const gchar *mime_type)
+modest_tny_msg_actions_find_body_part (TnyMsgIface *self, gboolean want_html)
 {
+       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);
 
        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))
+
+               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;
        }
-       
-       return part;
+
+       /* 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;
 }