* added REALNAME to identities
[modest] / src / modest-tny-transport-actions.c
index 822b743..ed9b27e 100644 (file)
 #include <tny-transport-account-iface.h>       
 #include <tny-transport-account.h>
 #include <tny-stream-camel.h>
+#include <tny-fs-stream.h>
 #include <string.h>
 #include <camel/camel-folder.h>
 #include <camel/camel.h>
 #include <camel/camel-folder-summary.h>
 
-
-
 #include "modest-tny-transport-actions.h"
+#include "modest-tny-attachment.h"
 /* include other impl specific header files */
 
 /* 'private'/'protected' functions */
 static void                              modest_tny_transport_actions_class_init   (ModestTnyTransportActionsClass *klass);
 static void                              modest_tny_transport_actions_init         (ModestTnyTransportActions *obj);
 static void                              modest_tny_transport_actions_finalize     (GObject *obj);
+static gboolean                          is_ascii                                  (const gchar *s);
+static char *                            get_content_type                          (const gchar *s);
 
 /* list my signals */
 enum {
@@ -114,7 +116,34 @@ modest_tny_transport_actions_new (void)
        return G_OBJECT(g_object_new(MODEST_TYPE_TNY_TRANSPORT_ACTIONS, NULL));
 }
 
+static gboolean
+is_ascii(const gchar *s)
+{
+       while (s[0]) {
+               if (s[0] & 128 || s[0] < 32)
+                       return FALSE;
+               s++;
+       }
+       return TRUE;
+}
 
+static char *
+get_content_type(const gchar *s)
+{
+       GString *type;
+       
+       type = g_string_new("text/plain");
+       if (!is_ascii(s)) {
+               if (g_utf8_validate(s, -1, NULL)) {
+                       g_string_append(type, "; charset=\"utf-8\"");
+               } else {
+                       /* it should be impossible to reach this, but better safe than sorry */
+                       g_warning("invalid utf8 in message");
+                       g_string_append(type, "; charset=\"latin1\"");
+               }
+       }
+       return g_string_free(type, FALSE);
+}
 
 gboolean
 modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
@@ -124,48 +153,81 @@ modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
                                           const gchar *cc,
                                           const gchar *bcc,
                                           const gchar *subject,
-                                          const gchar *body)
+                                          const gchar *body,
+                                          const GList *attachments_list)
 {
        TnyMsgIface *new_msg;
-       TnyMsgMimePartIface *body_part;
+       TnyMsgMimePartIface *attachment_part, *text_body_part;
        TnyMsgHeaderIface *headers;
-       TnyStreamIface *body_stream;
-
-       new_msg     = TNY_MSG_IFACE(tny_msg_new ());
-       headers     = TNY_MSG_HEADER_IFACE(tny_msg_header_new ());
-       body_stream = TNY_STREAM_IFACE (tny_stream_camel_new
-                                       (camel_stream_mem_new_with_buffer
-                                        (body, strlen(body))));
-       body_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new
-                                            (camel_mime_part_new()));
-
+       TnyStreamIface *text_body_stream, *attachment_stream;
+       ModestTnyAttachment *attachment;
+       GList *pos;
+       gchar *content_type;
+       const gchar *attachment_content_type;
+       const gchar *attachment_filename;
+       
+       new_msg          = TNY_MSG_IFACE(tny_msg_new ());
+       headers          = TNY_MSG_HEADER_IFACE(tny_msg_header_new ());
+       text_body_stream = TNY_STREAM_IFACE (tny_stream_camel_new
+                                            (camel_stream_mem_new_with_buffer
+                                             (body, strlen(body))));
+       
        tny_msg_header_iface_set_from (TNY_MSG_HEADER_IFACE (headers), from);
        tny_msg_header_iface_set_to (TNY_MSG_HEADER_IFACE (headers), to);
        tny_msg_header_iface_set_cc (TNY_MSG_HEADER_IFACE (headers), cc);
        tny_msg_header_iface_set_bcc (TNY_MSG_HEADER_IFACE (headers), bcc);
        tny_msg_header_iface_set_subject (TNY_MSG_HEADER_IFACE (headers), subject);
-
        tny_msg_iface_set_header (new_msg, headers);
-       tny_msg_mime_part_iface_construct_from_stream (body_part, body_stream,
-                                                      "text/plain");
-       tny_msg_mime_part_iface_set_content_type  (body_part,"text/plain");     
+
+       
+       content_type = get_content_type(body);
        
-       tny_msg_mime_part_iface_set_content_type (
-               TNY_MSG_MIME_PART_IFACE(new_msg), "text/plain");
-       tny_stream_iface_reset (body_stream);
+       if (attachments_list == NULL) {
+               tny_stream_iface_reset (text_body_stream);
+               tny_msg_mime_part_iface_construct_from_stream (TNY_MSG_MIME_PART_IFACE(new_msg),
+                                                              text_body_stream, content_type);
+               tny_stream_iface_reset (text_body_stream);
+       } else {
+               text_body_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new(
+                                                         camel_mime_part_new()));
+               tny_stream_iface_reset (text_body_stream);
+               tny_msg_mime_part_iface_construct_from_stream (text_body_part,
+                                                              text_body_stream,
+                                                              content_type);
+               tny_stream_iface_reset (text_body_stream);
+               tny_msg_iface_add_part(new_msg, text_body_part);
+               //g_object_unref (G_OBJECT(text_body_part));
+       }
        
-       tny_msg_mime_part_iface_construct_from_stream (TNY_MSG_MIME_PART_IFACE(new_msg),
-                                                      body_stream, "text/plain");
+       for (    pos = (GList *)attachments_list;
+                pos;
+                pos = pos->next    ) {
+               attachment = pos->data;
+               attachment_filename = modest_tny_attachment_get_name(attachment);
+               attachment_stream = modest_tny_attachment_get_stream(attachment);
+               attachment_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new (
+                                                               camel_mime_part_new()));
+               
+               attachment_content_type = modest_tny_attachment_get_mime_type(attachment);
+                                
+               tny_msg_mime_part_iface_construct_from_stream (attachment_part,
+                                                              attachment_stream,
+                                                              attachment_content_type);
+               tny_stream_iface_reset (attachment_stream);
+               
+               tny_msg_mime_part_iface_set_filename(attachment_part, attachment_filename);
+               
+               tny_msg_iface_add_part (new_msg, attachment_part);
+               //g_object_unref(G_OBJECT(attachment_part));
+               //close(file);
+       }
        
        tny_transport_account_iface_send (transport_account, new_msg);
 
-       g_object_unref (G_OBJECT(body_stream));
-       g_object_unref (G_OBJECT(body_part));
+       g_object_unref (G_OBJECT(text_body_stream));
        g_object_unref (G_OBJECT(headers));
        g_object_unref (G_OBJECT(new_msg));
+       g_free(content_type);
 
        return TRUE;    
 }
-
-
-