Fixes an infinite loop while saving BS mime parts
[modest] / src / hildon2 / modest-msg-view-window.c
index 1aed5f5..0fff509 100644 (file)
 #include <modest-debug.h>
 #include <modest-header-window.h>
 #include <modest-account-protocol.h>
+#include <modest-hildon2-window-mgr.h>
 #include <tny-camel-msg.h>
+#include <tny-camel-bs-mime-part.h>
+#include <tny-camel-bs-msg.h>
 
 #define MYDOCS_ENV "MYDOCSDIR"
 #define DOCS_FOLDER ".documents"
@@ -114,12 +117,14 @@ struct _ModestMsgViewWindowPrivate {
        gulong row_deleted_handler;
        gulong row_inserted_handler;
        gulong rows_reordered_handler;
+       gulong fetch_image_redraw_handler;
 
        guint purge_timeout;
        GtkWidget *remove_attachment_banner;
 
        gchar *msg_uid;
        TnyMimePart *other_body;
+       TnyMsg * top_msg;
 
        GSList *sighandlers;
 };
@@ -469,6 +474,7 @@ modest_msg_view_window_init (ModestMsgViewWindow *obj)
        priv->row_deleted_handler = 0;
        priv->row_inserted_handler = 0;
        priv->rows_reordered_handler = 0;
+       priv->fetch_image_redraw_handler = 0;
        priv->progress_hint = FALSE;
        priv->fetching_images = 0;
 
@@ -617,11 +623,21 @@ modest_msg_view_window_finalize (GObject *obj)
           call this function before */
        modest_msg_view_window_disconnect_signals (MODEST_WINDOW (obj));
 
+       if (priv->fetch_image_redraw_handler > 0) {
+               g_source_remove (priv->fetch_image_redraw_handler);
+               priv->fetch_image_redraw_handler = 0;
+       }
+
        if (priv->other_body != NULL) {
                g_object_unref (priv->other_body);
                priv->other_body = NULL;
        }
 
+       if (priv->top_msg != NULL) {
+               g_object_unref (priv->top_msg);
+               priv->top_msg = NULL;
+       }
+
        if (priv->header_model != NULL) {
                g_object_unref (priv->header_model);
                priv->header_model = NULL;
@@ -872,6 +888,7 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember the message list's TreeModel so we can detect changes
         * and change the list selection when necessary: */
@@ -950,6 +967,7 @@ modest_msg_view_window_new_from_uid (const gchar *modest_account_name,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        is_merge = g_str_has_prefix (msg_uid, "merge:");
 
@@ -1030,6 +1048,7 @@ modest_msg_view_window_new_from_header_view (ModestHeaderView *header_view,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember the message list's TreeModel so we can detect changes 
         * and change the list selection when necessary: */
@@ -1123,6 +1142,7 @@ modest_msg_view_window_new_for_search_result (TnyMsg *msg,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember that this is a search result, 
         * so we can disable some UI appropriately: */
@@ -1157,6 +1177,7 @@ modest_msg_view_window_is_other_body (ModestMsgViewWindow *self)
 ModestWindow *
 modest_msg_view_window_new_with_other_body (TnyMsg *msg, 
                                            TnyMimePart *other_body,
+                                           TnyMsg *top_msg,
                                            const gchar *modest_account_name,
                                            const gchar *mailbox,
                                            const gchar *msg_uid)
@@ -1178,6 +1199,11 @@ modest_msg_view_window_new_with_other_body (TnyMsg *msg,
        } else {
                tny_msg_view_set_msg (TNY_MSG_VIEW (priv->msg_view), msg);
        }
+       if (top_msg) {
+               priv->top_msg = g_object_ref (top_msg);
+       } else {
+               priv->top_msg = NULL;
+       }
        update_window_title (MODEST_MSG_VIEW_WINDOW (obj));
        update_branding (MODEST_MSG_VIEW_WINDOW (obj));
 
@@ -1192,12 +1218,13 @@ modest_msg_view_window_new_with_other_body (TnyMsg *msg,
 }
 
 ModestWindow *
-modest_msg_view_window_new_for_attachment (TnyMsg *msg, 
+modest_msg_view_window_new_for_attachment (TnyMsg *msg,
+                                          TnyMsg *top_msg,
                                           const gchar *modest_account_name,
                                           const gchar *mailbox,
                                           const gchar *msg_uid)
 {
-       return modest_msg_view_window_new_with_other_body (msg, NULL, modest_account_name, mailbox, msg_uid);
+       return modest_msg_view_window_new_with_other_body (msg, NULL, top_msg, modest_account_name, mailbox, msg_uid);
 }
 
 static void
@@ -1504,6 +1531,21 @@ modest_msg_view_window_get_message (ModestMsgViewWindow *self)
        return tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
 }
 
+TnyMsg*
+modest_msg_view_window_get_top_message (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv;
+       
+       g_return_val_if_fail (self, NULL);
+       
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
+
+       if (priv->top_msg)
+               return g_object_ref (priv->top_msg);
+       else
+               return NULL;
+}
+
 const gchar*
 modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
 {
@@ -1880,6 +1922,7 @@ message_reader_performer (gboolean canceled,
        info = (MsgReaderInfo *) user_data;
        if (canceled || err) {
                update_window_title (MODEST_MSG_VIEW_WINDOW (parent_window));
+               modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (parent_window));
                goto frees;
        }
 
@@ -2169,6 +2212,59 @@ view_msg_cb (ModestMailOperation *mail_op,
                        gtk_tree_row_reference_free (row_reference);
                self = (ModestMsgViewWindow *) modest_mail_operation_get_source (mail_op);
                if (self) {
+                       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+                       /* First we check if the parent is a folder window */
+                       if (priv->msg_uid && !modest_hildon2_window_mgr_get_folder_window (MODEST_HILDON2_WINDOW_MGR (modest_runtime_get_window_mgr ()))) {
+                               gboolean is_merge;
+                               TnyAccount *account = NULL;
+                               GtkWidget *header_window = NULL;
+
+                               is_merge = g_str_has_prefix (priv->msg_uid, "merge:");
+
+                               /* Get the account */
+                               if (!is_merge)
+                                       account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
+                                                                                 priv->msg_uid);
+
+                               if (is_merge || account) {
+                                       TnyFolder *folder = NULL;
+
+                                       /* Try to get the message, if it's already downloaded
+                                          we don't need to connect */
+                                       if (account) {
+                                               folder = modest_tny_folder_store_find_folder_from_uri (TNY_FOLDER_STORE (account), 
+                                                                                                      priv->msg_uid);
+                                       } else {
+                                               ModestTnyAccountStore *account_store;
+                                               ModestTnyLocalFoldersAccount *local_folders_account;
+
+                                               account_store = modest_runtime_get_account_store ();
+                                               local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
+                                                       modest_tny_account_store_get_local_folders_account (account_store));
+                                               folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
+                                               g_object_unref (local_folders_account);
+                                       }
+                                       if (account) g_object_unref (account);
+
+                                       if (folder) {
+                                               header_window = (GtkWidget *)
+                                                       modest_header_window_new (
+                                                               folder, 
+                                                               modest_window_get_active_account (MODEST_WINDOW (self)), 
+                                                               modest_window_get_active_mailbox (MODEST_WINDOW (self)));
+                                               if (!modest_window_mgr_register_window (modest_runtime_get_window_mgr (),
+                                                                                       MODEST_WINDOW (header_window),
+                                                                                       NULL)) {
+                                                       gtk_widget_destroy (GTK_WIDGET (header_window));
+                                               } else {
+                                                       gtk_widget_show_all (GTK_WIDGET (header_window));
+                                               }
+                                               g_object_unref (folder);
+                                       }
+                               }
+                       }
+
+
                        /* Restore window title */
                        update_window_title (self);
                        modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (self));
@@ -2584,6 +2680,9 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 {
        DecodeAsyncHelper *helper = (DecodeAsyncHelper *) user_data;
        const gchar *content_type;
+       ModestMsgViewWindowPrivate *priv;
+
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (helper->self);
 
        if (cancelled || err) {
                if (err) {
@@ -2610,7 +2709,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
        set_progress_hint (helper->self, FALSE);
 
        content_type = tny_mime_part_get_content_type (mime_part);
-       if (g_str_has_prefix (content_type, "message/rfc822")) {
+       if (content_type && g_str_has_prefix (content_type, "message/rfc822")) {
                ModestWindowMgr *mgr;
                ModestWindow *msg_win = NULL;
                TnyMsg * msg;
@@ -2621,6 +2720,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 
                fd = g_open (helper->file_path, O_RDONLY, 0644);
                if (fd != -1) {
+                       TnyMsg *top_msg;
                        file_stream = tny_fs_stream_new (fd);
 
                        mgr = modest_runtime_get_window_mgr ();
@@ -2633,7 +2733,15 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 
                        msg = tny_camel_msg_new ();
                        tny_camel_msg_parse (msg, file_stream);
-                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (msg), account, mailbox, helper->attachment_uid);
+
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
+
+                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (msg), top_msg, 
+                                                                            account, mailbox, helper->attachment_uid);
+                       if (top_msg) g_object_unref (top_msg);
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (helper->self)));
                        if (modest_window_mgr_register_window (mgr, msg_win, MODEST_WINDOW (helper->self)))
@@ -2652,7 +2760,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
                g_chmod(helper->file_path, 0444);
 
                /* Activate the file */
-               modest_platform_activate_file (helper->file_path, tny_mime_part_get_content_type (mime_part));
+               modest_platform_activate_file (helper->file_path, content_type);
        }
 
  free:
@@ -2663,6 +2771,24 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
        g_slice_free (DecodeAsyncHelper, helper);
 }
 
+static void
+view_attachment_connect_handler (gboolean canceled,
+                                GError *err,
+                                GtkWindow *parent_window,
+                                TnyAccount *account,
+                                TnyMimePart *part)
+{
+
+       if (canceled || err) {
+               g_object_unref (part);
+               return;
+       }
+
+       modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (parent_window),
+                                               part);
+       g_object_unref (part);
+}
+
 void
 modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, 
                                        TnyMimePart *mime_part)
@@ -2712,6 +2838,28 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
        if (tny_mime_part_is_purged (mime_part))
                goto frees;
 
+       if (TNY_IS_CAMEL_BS_MIME_PART (mime_part) &&
+           !tny_camel_bs_mime_part_is_fetched (TNY_CAMEL_BS_MIME_PART (mime_part))) {
+               gboolean is_merge;
+               TnyAccount *account;
+
+               is_merge = g_str_has_prefix (priv->msg_uid, "merge:");
+               account = NULL;
+               /* Get the account */
+               if (!is_merge)
+                       account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
+                                                                 priv->msg_uid);
+
+               if (!tny_device_is_online (modest_runtime_get_device())) {
+                       modest_platform_connect_and_perform (GTK_WINDOW (window),
+                                                            TRUE,
+                                                            TNY_ACCOUNT (account),
+                                                            (ModestConnectedPerformer) view_attachment_connect_handler,
+                                                            g_object_ref (mime_part));
+                       goto frees;
+               }
+       }
+
        if (!modest_tny_mime_part_is_msg (mime_part) && tny_mime_part_get_filename (mime_part)) {
                gchar *filepath = NULL;
                const gchar *att_filename = tny_mime_part_get_filename (mime_part);
@@ -2796,6 +2944,7 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                if (found) {
                        g_debug ("window for this body is already being created");
                } else {
+                       TnyMsg *top_msg;
 
                        /* it's not found, so create a new window for it */
                        modest_window_mgr_register_header (mgr, header, attachment_uid); /* register the uid before building the window */
@@ -2803,9 +2952,16 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                        const gchar *mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (window));
                        if (!account)
                                account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
+
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
                        
-                       msg_win = modest_msg_view_window_new_with_other_body (TNY_MSG (current_msg), TNY_MIME_PART (mime_part),
+                       msg_win = modest_msg_view_window_new_with_other_body (TNY_MSG (current_msg), TNY_MIME_PART (mime_part), top_msg,
                                                                              account, mailbox, attachment_uid);
+
+                       if (top_msg) g_object_unref (top_msg);
                        
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (window)));
@@ -2831,14 +2987,20 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                         * thus, we don't do anything */
                        g_debug ("window for is already being created");
                } else {
+                       TnyMsg *top_msg;
                        /* it's not found, so create a new window for it */
                        modest_window_mgr_register_header (mgr, header, attachment_uid); /* register the uid before building the window */
                        gchar *account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (window)));
                        const gchar *mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (window));
                        if (!account)
                                account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
-                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (mime_part), account, 
-                                                                            mailbox, attachment_uid);
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
+                       msg_win = modest_msg_view_window_new_for_attachment (
+                               TNY_MSG (mime_part), top_msg, account, 
+                               mailbox, attachment_uid);
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (window)));
                        if (modest_window_mgr_register_window (mgr, msg_win, MODEST_WINDOW (window)))
@@ -2902,7 +3064,9 @@ idle_save_mime_part_show_result (SaveMimePartInfo *info)
         * hildon_banner_show_information is or does Gtk+ code */
 
        gdk_threads_enter (); /* CHECKED */
-       if (info->result == GNOME_VFS_OK) {
+       if (info->result == GNOME_VFS_ERROR_CANCELLED) {
+               /* nothing */
+       } else if (info->result == GNOME_VFS_OK) {
                hildon_banner_show_information (NULL, NULL, _CS("sfil_ib_saved"));
        } else if (info->result == GNOME_VFS_ERROR_NO_SPACE) {
                gchar *msg = NULL;
@@ -2923,6 +3087,52 @@ idle_save_mime_part_show_result (SaveMimePartInfo *info)
        return FALSE;
 }
 
+static void
+save_mime_part_to_file_connect_handler (gboolean canceled,
+                                       GError *err,
+                                       GtkWindow *parent_window,
+                                       TnyAccount *account,
+                                       SaveMimePartInfo *info)
+{
+       if (canceled || err) {
+               if (canceled && !err) {
+                       info->result = GNOME_VFS_ERROR_CANCELLED;
+               }
+               g_idle_add ((GSourceFunc) idle_save_mime_part_show_result, info);
+       } else {
+               g_thread_create ((GThreadFunc)save_mime_part_to_file, info, FALSE, NULL);
+       }
+}
+
+static gboolean
+save_mime_part_to_file_connect_idle (SaveMimePartInfo *info)
+{
+       gboolean is_merge;
+       TnyAccount *account;
+       ModestMsgViewWindowPrivate *priv;
+
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (info->window);
+
+       is_merge = g_str_has_prefix (priv->msg_uid, "merge:");
+       account = NULL;
+
+       /* Get the account */
+       if (!is_merge)
+               account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
+                                                         priv->msg_uid);
+
+       modest_platform_connect_and_perform (GTK_WINDOW (info->window),
+                                            TRUE,
+                                            TNY_ACCOUNT (account),
+                                            (ModestConnectedPerformer) save_mime_part_to_file_connect_handler,
+                                            info);
+
+       if (account)
+               g_object_unref (account);
+
+       return FALSE;
+}
+
 static gpointer
 save_mime_part_to_file (SaveMimePartInfo *info)
 {
@@ -2930,6 +3140,38 @@ save_mime_part_to_file (SaveMimePartInfo *info)
        TnyStream *stream;
        SaveMimePartPair *pair = (SaveMimePartPair *) info->pairs->data;
 
+       if (TNY_IS_CAMEL_BS_MIME_PART (pair->part) &&
+           !tny_camel_bs_mime_part_is_fetched (TNY_CAMEL_BS_MIME_PART (pair->part))) {
+               gboolean check_online = TRUE;
+               ModestMsgViewWindowPrivate *priv = NULL;
+
+               /* Check if we really need to connect to save the mime part */
+               priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (info->window);
+               if (g_str_has_prefix (priv->msg_uid, "merge:")) {
+                       check_online = FALSE;
+               } else {
+                       TnyAccountStore *acc_store;
+                       TnyAccount *account = NULL;
+
+                       acc_store = (TnyAccountStore*) modest_runtime_get_account_store ();
+                       account = tny_account_store_find_account (acc_store, priv->msg_uid);
+
+                       if (account) {
+                               if (tny_account_get_connection_status (account) ==
+                                   TNY_CONNECTION_STATUS_CONNECTED)
+                                       check_online = FALSE;
+                               g_object_unref (account);
+                       } else {
+                               check_online = !tny_device_is_online (tny_account_store_get_device (acc_store));
+                       }
+               }
+
+               if (check_online) {
+                       g_idle_add ((GSourceFunc) save_mime_part_to_file_connect_idle, info);
+                       return NULL;
+               }
+       }
+
        info->result = gnome_vfs_create (&handle, pair->filename, GNOME_VFS_OPEN_WRITE, FALSE, 0644);
        if (info->result == GNOME_VFS_OK) {
                GError *error = NULL;
@@ -3467,6 +3709,23 @@ typedef struct {
 } FetchImageData;
 
 gboolean
+on_fetch_image_timeout_refresh_view (gpointer userdata)
+{
+       ModestMsgViewWindowPrivate *priv;
+
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (userdata);
+       update_progress_hint (MODEST_MSG_VIEW_WINDOW (userdata));
+       /* Note that priv->msg_view is set to NULL when this window is
+          distroyed */
+       if (priv->msg_view && GTK_WIDGET_DRAWABLE (priv->msg_view)) {
+               gtk_widget_queue_draw (GTK_WIDGET (priv->msg_view));
+       }
+       priv->fetch_image_redraw_handler = 0;
+       g_object_unref (userdata);
+       return FALSE;
+}
+
+gboolean
 on_fetch_image_idle_refresh_view (gpointer userdata)
 {
 
@@ -3478,8 +3737,10 @@ on_fetch_image_idle_refresh_view (gpointer userdata)
 
                priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (fidata->window);
                priv->fetching_images--;
-               gtk_widget_queue_draw (fidata->msg_view);
-               update_progress_hint (MODEST_MSG_VIEW_WINDOW (fidata->window));
+               if (priv->fetch_image_redraw_handler == 0) {
+                       priv->fetch_image_redraw_handler = g_timeout_add (500, on_fetch_image_timeout_refresh_view, g_object_ref (fidata->window));
+               }
+
        }
        gdk_threads_leave ();
 
@@ -3631,7 +3892,6 @@ modest_msg_view_window_add_to_contacts (ModestMsgViewWindow *self)
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
        GSList *recipients = NULL;
        TnyMsg *msg = NULL;
-       gboolean contacts_to_add = FALSE;
 
        msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
        if (msg == NULL) {
@@ -3647,51 +3907,11 @@ modest_msg_view_window_add_to_contacts (ModestMsgViewWindow *self)
                g_object_unref (msg);
        }
 
-       if (recipients != NULL) {
-               GtkWidget *picker_dialog;
-               GtkWidget *selector;
-               GSList *node;
-               gchar *selected = NULL;
-
-               selector = hildon_touch_selector_new_text ();
-               g_object_ref (selector);
-
-               for (node = recipients; node != NULL; node = g_slist_next (node)) {
-                       if (!modest_address_book_has_address ((const gchar *) node->data)) {
-                               hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), 
-                                                                  (const gchar *) node->data);
-                               contacts_to_add = TRUE;
-                       }
-               }
-
-               if (contacts_to_add) {
-                       gint picker_result;
-
-                       picker_dialog = hildon_picker_dialog_new (GTK_WINDOW (self));
-                       gtk_window_set_title (GTK_WINDOW (picker_dialog), _("mcen_me_viewer_addtocontacts"));
-
-                       hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (picker_dialog), 
-                                                          HILDON_TOUCH_SELECTOR (selector));
-                       
-                       picker_result = gtk_dialog_run (GTK_DIALOG (picker_dialog));
-
-                       if (picker_result == GTK_RESPONSE_OK) {
-                               selected = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
-                       }
-                       gtk_widget_destroy (picker_dialog);
-
-                       if (selected)
-                               modest_address_book_add_address (selected, (GtkWindow *) self);
-                       g_free (selected);
-
-               } else {
-
-                       g_object_unref (selector);
-
-               }
+       if (recipients) {
+               /* Offer the user to add recipients to the address book */
+               modest_address_book_add_address_list_with_selector (recipients, (GtkWindow *) self);
+               g_slist_foreach (recipients, (GFunc) g_free, NULL); g_slist_free (recipients);
        }
-       
-       if (recipients) {g_slist_foreach (recipients, (GFunc) g_free, NULL); g_slist_free (recipients);}
 }
 
 static gboolean 
@@ -3819,7 +4039,7 @@ sync_flags (ModestMsgViewWindow *self)
                        mail_op = modest_mail_operation_new (NULL);
                        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
                                                         mail_op);
-                       modest_mail_operation_sync_folder (mail_op, folder, FALSE);
+                       modest_mail_operation_sync_folder (mail_op, folder, FALSE, NULL, NULL);
                        g_object_unref (mail_op);
                        g_object_unref (folder);
                }