* added dutch translation
[modest] / src / modest-tny-msg-actions.c
index 2d60cd8..f05abcc 100644 (file)
 
 #include <gtk/gtk.h>
 #include <gtkhtml/gtkhtml.h>
-
-/* TODO: put in auto* */
-#include <tny-text-buffer-stream.h>
-#include <tny-msg-mime-part-iface.h>
-#include <tny-msg-iface.h>
-#include <tny-list-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>
 #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);
 
-/* private */
-static gchar *quote_msg (const TnyMsgIface * src, const gchar * from,
-                        time_t sent_date, gint limit);
 
-static gchar *
-quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date, gint limit)
+static const gchar *
+get_body_text (TnyMsg *msg, gboolean want_html)
 {
-       TnyStreamIface *stream;
-       TnyMsgMimePartIface *body;
+       TnyStream *stream;
+       TnyMimePart *body;
        GtkTextBuffer *buf;
        GtkTextIter start, end;
        const gchar *to_quote;
-       gchar *quoted;
 
-       /* the cast makes me uneasy... */
-       body = modest_tny_msg_actions_find_body_part(src, FALSE);
+       body = modest_tny_msg_actions_find_body_part(msg, want_html);
        if (!body)
                return NULL;
 
        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);
+       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);
-       quoted = modest_text_utils_quote (to_quote, from, sent_date, limit);
        g_object_unref (buf);
 
-       return quoted;
+       return to_quote;
 }
 
-
 gchar*
-modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
+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 */
@@ -93,71 +90,152 @@ modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
                                                limit);
        
        /* b) try to find a text/plain part in the msg and quote it */
-       return quote_msg (self, from, sent_date, limit);
+       body = get_body_text (self, FALSE);
+       if (body)
+               quoted_msg = modest_text_utils_quote (body, from, sent_date, limit);
+       
+       return quoted_msg;
 }
 
 
 
-TnyMsgMimePartIface *
-modest_tny_msg_actions_find_body_part (const TnyMsgIface *msg, gboolean want_html)
+TnyMimePart *
+modest_tny_msg_actions_find_body_part (TnyMsg *msg, gboolean want_html)
 {
        const gchar *mime_type = want_html ? "text/html" : "text/plain";
-       TnyMsgMimePartIface *part;
-       const TnyListIface *parts;
-       TnyIteratorIface *iter;
-       gboolean found;
+       TnyMimePart *part = NULL;
+       TnyList *parts;
+       TnyIterator *iter;
 
        if (!msg)
                return NULL;
 
-       found = FALSE;
-       parts =  tny_msg_iface_get_parts ((TnyMsgIface *)msg);
-       iter  = tny_list_iface_create_iterator ((TnyListIface*)parts);
+       parts = TNY_LIST (tny_simple_list_new());
+       tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
+
+       iter  = tny_list_create_iterator(parts);
 
-       while (!tny_iterator_iface_is_done(iter)) {
-               part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current (iter));
+       while (!tny_iterator_is_done(iter)) {
+
+               part = TNY_MIME_PART(tny_iterator_get_current (iter));
                
-               if (tny_msg_mime_part_iface_content_type_is (part, mime_type) &&
-                   !tny_msg_mime_part_iface_is_attachment (part)) {
-                       found = TRUE;
+               if (tny_mime_part_content_type_is (part, mime_type) &&
+                   !tny_mime_part_is_attachment (part)) {
                        break;
-               }       
-               tny_iterator_iface_next (iter);
+               }
+               part = NULL;
+               tny_iterator_next (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 could find it,
+       /* if were trying to find an HTML part and couldn't find it,
         * try to find a text/plain part instead
         */
-       if (!found && want_html) 
+       if (!part && want_html) 
                return modest_tny_msg_actions_find_body_part (msg, FALSE);
 
-       return found ? part : NULL;
+       return part ? part : NULL;
 }
 
 
 
-
-TnyMsgMimePartIface *
-modest_tny_msg_actions_find_nth_part (const TnyMsgIface *msg, gint index)
+TnyMimePart *
+modest_tny_msg_actions_find_nth_part (TnyMsg *msg, gint index)
 {
-       TnyMsgMimePartIface *part;
-       const TnyListIface *parts;
-       TnyIteratorIface *iter;
+       TnyMimePart *part;
+       TnyList *parts;
+       TnyIterator *iter;
 
        g_return_val_if_fail (msg, NULL);
        g_return_val_if_fail (index > 0, NULL);
                
-       parts =  tny_msg_iface_get_parts ((TnyMsgIface *)msg);
-       iter  = tny_list_iface_create_iterator ((TnyListIface*)parts);
-
-       if (!tny_iterator_iface_has_first(iter))
-               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);
 
-       part = tny_iterator_iface_nth (iter, index);
+       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;
+}
+
+
+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);
+}