* modest-pair.[ch]:
[modest] / src / modest-tny-msg-actions.c
index 55c0269..ccce454 100644 (file)
@@ -27,8 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-
-#include <gtk/gtk.h>
+#include <string.h>
 #include <gtkhtml/gtkhtml.h>
 #include <tny-gtk-text-buffer-stream.h>
 #include <tny-simple-list.h>
 #include "modest-tny-msg-actions.h"
 #include "modest-text-utils.h"
 
-static void modest_tny_msg_actions_xfer (TnyHeader *header, TnyFolder *folder, 
-                                        gboolean delete_original);
-
-
 static const gchar *
 get_body_text (TnyMsg *msg, gboolean want_html)
 {
@@ -53,7 +48,6 @@ get_body_text (TnyMsg *msg, gboolean want_html)
        GtkTextBuffer *buf;
        GtkTextIter start, end;
        const gchar *to_quote;
-       gchar *quoted;
 
        body = modest_tny_msg_actions_find_body_part(msg, want_html);
        if (!body)
@@ -75,33 +69,8 @@ get_body_text (TnyMsg *msg, gboolean want_html)
        return to_quote;
 }
 
-gchar*
-modest_tny_msg_actions_quote (TnyMsg * self, const gchar * from,
-                             time_t sent_date, gint limit,
-                             const gchar * to_quote)
-{
-       gchar *quoted_msg = NULL;
-       const gchar *body;
-
-       /* 2 cases: */
-
-       /* a) quote text from selection */
-       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 */
-       body = get_body_text (self, FALSE);
-       if (body)
-               quoted_msg = modest_text_utils_quote (body, from, sent_date, limit);
-       
-       return quoted_msg;
-}
-
-
-
-TnyMimePart *
-modest_tny_msg_actions_find_body_part (TnyMsg *msg, gboolean want_html)
+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;
@@ -116,18 +85,42 @@ modest_tny_msg_actions_find_body_part (TnyMsg *msg, gboolean want_html)
 
        iter  = tny_list_create_iterator(parts);
 
-       while (!tny_iterator_is_done(iter)) {
-
-               part = TNY_MIME_PART(tny_iterator_get_current (iter));
-               
-               if (tny_mime_part_content_type_is (part, mime_type) &&
-                   !tny_mime_part_is_attachment (part)) {
-                       break;
-               }
-               part = NULL;
-               tny_iterator_next (iter);
+       /* 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));
@@ -139,12 +132,22 @@ modest_tny_msg_actions_find_body_part (TnyMsg *msg, gboolean want_html)
         * try to find a text/plain part instead
         */
        if (!part && want_html) 
-               return modest_tny_msg_actions_find_body_part (msg, FALSE);
+               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)
@@ -185,58 +188,3 @@ modest_tny_msg_actions_find_body (TnyMsg *msg, gboolean want_html)
        else 
                return NULL;
 }
-
-
-static void
-modest_tny_msg_actions_xfer (TnyHeader *header, TnyFolder *folder, 
-                            gboolean delete_original)
-{
-       TnyFolder *src_folder;
-       TnyList *headers;
-
-       src_folder = tny_header_get_folder (header);
-       headers = tny_simple_list_new ();
-
-       /* Move */
-       tny_list_prepend (headers, G_OBJECT (header));
-       tny_folder_transfer_msgs (src_folder, headers, folder, delete_original);
-
-       /* Free */
-       g_object_unref (headers);
-       g_object_unref (folder);
-}
-
-void
-modest_tny_msg_actions_copy (TnyHeader *header, TnyFolder *folder)
-{
-       g_return_if_fail (TNY_IS_HEADER (header));
-       g_return_if_fail (TNY_IS_FOLDER (folder));
-
-       modest_tny_msg_actions_xfer (header, folder, FALSE);
-}
-
-void
-modest_tny_msg_actions_move (TnyHeader *header, TnyFolder *folder)
-{
-       g_return_if_fail (TNY_IS_HEADER (header));
-       g_return_if_fail (TNY_IS_FOLDER (folder));
-
-       modest_tny_msg_actions_xfer (header, folder, TRUE);
-}
-
-void
-modest_tny_msg_actions_remove (TnyHeader *header)
-{
-       TnyFolder *folder;
-
-       g_return_if_fail (TNY_IS_HEADER (header));
-
-       folder = tny_header_get_folder (header);
-
-       /* Remove */
-       tny_folder_remove_msg (folder, header);
-       tny_folder_expunge (folder);
-
-       /* Free */
-       g_object_unref (folder);
-}