* Fixes NB#83407, evaluate the dimming rules when opening mails from search or D-Bus
[modest] / src / maemo / modest-msg-edit-window.c
index 65cefee..a9fd316 100644 (file)
@@ -35,6 +35,7 @@
 #include <tny-account-store.h>
 #include <tny-fs-stream.h>
 #include <tny-vfs-stream.h>
+#include <tny-camel-mem-stream.h>
 
 #include <config.h>
 
@@ -278,7 +279,7 @@ struct _ModestMsgEditWindowPrivate {
        guint        scroll_drag_timeout_id;
        gdouble      last_upper;
 
-       gint last_cid;
+       gint next_cid;
        TnyList *attachments;
        TnyList *images;
        guint64 images_size;
@@ -419,7 +420,7 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
        priv->images        = TNY_LIST (tny_simple_list_new ());
        priv->images_size   = 0;
        priv->images_count  = 0;
-       priv->last_cid      = 0;
+       priv->next_cid      = 0;
 
        priv->cc_caption    = NULL;
        priv->bcc_caption    = NULL;
@@ -507,15 +508,21 @@ get_transports (void)
 static ModestPair *
 find_transport_from_message_sender (ModestPairList *transports, TnyMsg *msg)
 {
+       ModestPair *account_pair = NULL;
+       gchar *from;
+       TnyHeader *header;
+
        g_return_val_if_fail (transports, NULL);
        g_return_val_if_fail (msg, NULL);
 
-       ModestPair *account_pair = NULL;
-       TnyHeader *header = tny_msg_get_header (msg);
+       header = tny_msg_get_header (msg);
 
-       if (header != NULL && tny_header_get_from (header)) {
-               char *from_addr = modest_text_utils_get_email_address (tny_header_get_from (header));
+       if (header != NULL && (from = tny_header_dup_from (header))) {
                GSList *iter;
+               char *from_addr;
+
+               from_addr = modest_text_utils_get_email_address (from);
+               g_free (from);
                for (iter = transports; iter && !account_pair; iter = iter->next) {
                        ModestPair *pair = (ModestPair *) iter->data;
                        char *account_addr = modest_text_utils_get_email_address ((char *) pair->second);
@@ -1147,7 +1154,7 @@ get_related_images (ModestMsgEditWindow *self, TnyMsg *msg)
 }
 
 static void
-update_last_cid (ModestMsgEditWindow *self, TnyList *attachments)
+update_next_cid (ModestMsgEditWindow *self, TnyList *attachments)
 {
        TnyIterator *iter;
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
@@ -1160,8 +1167,8 @@ update_last_cid (ModestMsgEditWindow *self, TnyList *attachments)
                if (cid != NULL) {
                        char *invalid = NULL;
                        gint int_cid = strtol (cid, &invalid, 10);
-                       if ((invalid != NULL) && (*invalid == '\0') && (int_cid > priv->last_cid)) {
-                               priv->last_cid = int_cid;
+                       if ((invalid != NULL) && (*invalid == '\0') && (int_cid >= priv->next_cid)) {
+                               priv->next_cid = int_cid + 1;
                        }
                }
                g_object_unref (part);
@@ -1173,7 +1180,7 @@ static void
 set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
 {
        TnyHeader *header;
-       const gchar *to, *cc, *bcc, *subject;
+       gchar *to, *cc, *bcc, *subject;
        gchar *body;
        ModestMsgEditWindowPrivate *priv;
        GtkTextIter iter;
@@ -1187,10 +1194,10 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
 
        header = tny_msg_get_header (msg);
-       to      = tny_header_get_to (header);
-       cc      = tny_header_get_cc (header);
-       bcc     = tny_header_get_bcc (header);
-       subject = tny_header_get_subject (header);
+       to      = tny_header_dup_to (header);
+       cc      = tny_header_dup_cc (header);
+       bcc     = tny_header_dup_bcc (header);
+       subject = tny_header_dup_subject (header);
        priority_flags = tny_header_get_priority (header);
 
        if (to)
@@ -1243,8 +1250,8 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
                gtk_widget_show_all (priv->attachments_caption);
        }
        get_related_images (self, msg);
-       update_last_cid (self, priv->attachments);
-       update_last_cid (self, priv->images);
+       update_next_cid (self, priv->attachments);
+       update_next_cid (self, priv->images);
        replace_with_images (self, priv->images);
 
        if (preserve_is_rich && !is_html) {
@@ -1298,6 +1305,11 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
                }
                g_object_unref (msg_folder);
        }
+
+       g_free (to);
+       g_free (subject);
+       g_free (cc);
+       g_free (bcc);
 }
 
 static void
@@ -2144,9 +2156,21 @@ static TnyStream* create_stream_for_uri (const gchar* uri)
        GnomeVFSHandle *handle = NULL;
        GnomeVFSResult test = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
        if (test == GNOME_VFS_OK) {
-               /* Create the tinymail stream: */
-               /* Presumably tinymai will call gnome_vfs_close (handle) later. */
-               result = TNY_STREAM (tny_vfs_stream_new (handle));
+               TnyStream *vfssstream = TNY_STREAM (tny_vfs_stream_new (handle));
+               /* Streams over OBEX (Bluetooth) are not seekable but
+                * we expect them to be (we might need to read them
+                * several times). So if this is a Bluetooth URI just
+                * read the whole file into memory (this is not a fast
+                * protocol so we can assume that these files are not
+                * going to be very big) */
+               if (g_ascii_strncasecmp (uri, "obex://", 7) == 0) {
+                       TnyStream *memstream = tny_camel_mem_stream_new ();
+                       tny_stream_write_to_stream (vfssstream, memstream);
+                       g_object_unref (vfssstream);
+                       result = memstream;
+               } else {
+                       result = vfssstream;
+               }
        }
        
        return result;
@@ -2199,6 +2223,7 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
                        GnomeVFSURI *vfs_uri;
                        guint64 stream_size;
 
+                       gnome_vfs_close (handle);
                        vfs_uri = gnome_vfs_uri_new (uri);
 
                        escaped_filename = g_path_get_basename (gnome_vfs_uri_get_path (vfs_uri));
@@ -2218,10 +2243,10 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
                        TnyStream *stream = create_stream_for_uri (uri);
                        tny_mime_part_construct (mime_part, stream, mime_type, "base64");
                        
-                       content_id = g_strdup_printf ("%d", priv->last_cid);
+                       content_id = g_strdup_printf ("%d", priv->next_cid);
                        tny_mime_part_set_content_id (mime_part, content_id);
                        g_free (content_id);
-                       priv->last_cid++;
+                       priv->next_cid++;
                        
                        basename = g_path_get_basename (filename);
                        tny_mime_part_set_filename (mime_part, basename);
@@ -2305,6 +2330,7 @@ modest_msg_edit_window_attach_file_one (
                GnomeVFSFileInfo *info;
                GnomeVFSURI *vfs_uri;
 
+               gnome_vfs_close (handle);
                vfs_uri = gnome_vfs_uri_new (uri);
                
 
@@ -2322,16 +2348,16 @@ modest_msg_edit_window_attach_file_one (
                        mime_type = gnome_vfs_file_info_get_mime_type (info);
                mime_part = tny_platform_factory_new_mime_part
                        (modest_runtime_get_platform_factory ());
-               stream = TNY_STREAM (tny_vfs_stream_new (handle));
+               stream = create_stream_for_uri (uri);
                
                tny_mime_part_construct (mime_part, stream, mime_type, "base64");
 
                g_object_unref (stream);
                
-               content_id = g_strdup_printf ("%d", priv->last_cid);
+               content_id = g_strdup_printf ("%d", priv->next_cid);
                tny_mime_part_set_content_id (mime_part, content_id);
                g_free (content_id);
-               priv->last_cid++;
+               priv->next_cid++;
                
                basename = g_path_get_basename (filename);
                tny_mime_part_set_filename (mime_part, basename);
@@ -2382,7 +2408,7 @@ modest_msg_edit_window_remove_attachments (ModestMsgEditWindow *window,
                        if (TNY_IS_MSG (part)) {
                                TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
                                if (header) {
-                                       filename = g_strdup (tny_header_get_subject (header));
+                                       filename = tny_header_dup_subject (header);
                                        g_object_unref (header);
                                }
                                if (filename == NULL) {
@@ -2396,7 +2422,7 @@ modest_msg_edit_window_remove_attachments (ModestMsgEditWindow *window,
                        filename = g_strdup ("");
                }
                message = g_strdup_printf (ngettext("emev_nc_delete_attachment", "emev_nc_delete_attachments",
-                                                   (tny_list_get_length (att_list) == 1)), filename);
+                                                   tny_list_get_length (att_list)), filename);
                g_free (filename);
                confirmation_dialog = hildon_note_new_confirmation (GTK_WINDOW (window), message);
                g_free (message);