* Migrated Modest to the new send queue API
[modest] / src / modest-ui-actions.c
index 8611d41..110a26f 100644 (file)
@@ -398,7 +398,8 @@ modest_ui_actions_refresh_message_window_after_delete (ModestMsgViewWindow* win)
        if (modest_msg_view_window_last_message_selected (win) &&
                modest_msg_view_window_first_message_selected (win)) {
                modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (win));
-       } else if (!modest_msg_view_window_select_next_message (win)) {
+       } else if (!modest_msg_view_window_select_next_message (win) &&
+                  !modest_msg_view_window_select_previous_message (win)) {
                gboolean ret_value;
                g_signal_emit_by_name (G_OBJECT (win), "delete-event", NULL, &ret_value);       
        }
@@ -818,7 +819,7 @@ modest_ui_actions_on_new_msg_or_folder (GtkAction *action, ModestWindow *win)
 {
        g_return_if_fail (MODEST_IS_WINDOW (win));
 
-       /* Check first if the header view has the focus */
+       /* Check first if the folder view has the focus */
        if (MODEST_IS_MAIN_WINDOW (win)) {
                GtkWidget *w;
                w = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
@@ -1020,16 +1021,10 @@ modest_ui_actions_get_msgs_full_error_handler (ModestMailOperation *mail_op,
 {
        const GError *error;
        GObject *win = NULL;
-       const gchar *err_msg;
+       const gchar *err_msg = (const gchar *) user_data;
 
        win = modest_mail_operation_get_source (mail_op);
        error = modest_mail_operation_get_error (mail_op);
-       /* Select error message */
-       if (error->code == MODEST_MAIL_OPERATION_ERROR_MESSAGE_SIZE_LIMIT)
-               err_msg = _("emev_ni_ui_imap_msg_size_exceed_error");
-       else
-               err_msg = (const gchar *) user_data;
 
        /* Show error */
        modest_platform_run_information_dialog ((GtkWindow *) win, err_msg);
@@ -1059,6 +1054,16 @@ get_account_from_header_list (TnyList *headers)
        return account;
 }
 
+static void 
+foreach_unregister_headers (gpointer data,
+                           gpointer user_data)
+{
+       ModestWindowMgr *mgr = (ModestWindowMgr *) user_data;
+       TnyHeader *header = TNY_HEADER (data);
+
+       modest_window_mgr_unregister_header (mgr, header);
+}
+
 static void
 open_msgs_performer(gboolean canceled, 
                    GError *err,
@@ -1077,6 +1082,9 @@ open_msgs_performer(gboolean canceled,
 
        status = tny_account_get_connection_status (account);
        if (err || canceled) {
+               /* Unregister the already registered headers */
+               tny_list_foreach (not_opened_headers, foreach_unregister_headers, 
+                                 modest_runtime_get_window_mgr ());
                goto clean;
        }
 
@@ -1667,7 +1675,7 @@ new_messages_arrived (ModestMailOperation *self,
           send&receive was invoked by the user then do not show any
           visual notification, only play a sound and activate the LED
           (for the Maemo version) */
-       if ((new_headers != NULL) && (tny_list_get_length (new_headers) > 0))
+       if (TNY_IS_LIST(new_headers) && (tny_list_get_length (new_headers)) > 0)
                modest_platform_on_new_headers_received (new_headers, 
                                                         show_visual_notifications);
 
@@ -1758,6 +1766,7 @@ do_send_receive_performer (gboolean canceled,
  */
 void
 modest_ui_actions_do_send_receive (const gchar *account_name, 
+                                  gboolean force_connection,
                                   ModestWindow *win)
 {
        gchar *acc_name = NULL;
@@ -1790,7 +1799,7 @@ modest_ui_actions_do_send_receive (const gchar *account_name,
 
        /* Invoke the connect and perform */
        modest_platform_connect_and_perform ((win) ? GTK_WINDOW (win) : NULL, 
-                                            (win) ? TRUE : FALSE, info->account, 
+                                            force_connection, info->account, 
                                             do_send_receive_performer, info);
 }
 
@@ -1821,9 +1830,12 @@ modest_ui_actions_do_cancel_send (const gchar *account_name,
                             MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
                             "modest: could not find send queue for account\n");
        } else {
-               /* Keeep messages in outbox folder */
-               tny_send_queue_cancel (send_queue, FALSE, &error);
-       }       
+               /* Cancel the current send */
+               tny_account_cancel (TNY_ACCOUNT (transport_account));
+
+               /* Suspend all pending messages */
+               tny_send_queue_cancel (send_queue, TNY_SEND_QUEUE_CANCEL_ACTION_SUSPEND, &error);
+       }
 
  frees:
        if (transport_account != NULL) 
@@ -1869,7 +1881,8 @@ modest_ui_actions_cancel_send (GtkAction *action,  ModestWindow *win)
  * updates
  */
 void
-modest_ui_actions_do_send_receive_all (ModestWindow *win)
+modest_ui_actions_do_send_receive_all (ModestWindow *win, 
+                                      gboolean force_connection)
 {
        GSList *account_names, *iter;
 
@@ -1878,7 +1891,7 @@ modest_ui_actions_do_send_receive_all (ModestWindow *win)
 
        iter = account_names;
        while (iter) {                  
-               modest_ui_actions_do_send_receive ((const char*) iter->data, win);
+               modest_ui_actions_do_send_receive ((const char*) iter->data, force_connection, win);
                iter = g_slist_next (iter);
        }
 
@@ -1919,8 +1932,8 @@ modest_ui_actions_on_send_receive (GtkAction *action, ModestWindow *win)
                        g_object_unref (folder_store);
        }       
        
-       /* Refresh the active account */
-       modest_ui_actions_do_send_receive (NULL, win);
+       /* Refresh the active account. Force the connection if needed */
+       modest_ui_actions_do_send_receive (NULL, TRUE, win);
 }
 
 
@@ -2266,10 +2279,10 @@ on_save_to_drafts_cb (ModestMailOperation *mail_op,
        if (modest_mail_operation_get_error (mail_op) != NULL) {
                g_warning ("%s failed: %s\n", __FUNCTION__, (modest_mail_operation_get_error (mail_op))->message);
                modest_platform_information_banner (NULL, NULL, _("mail_ib_file_operation_failed"));
-               return;
+       } else {
+               modest_msg_edit_window_set_draft (edit_window, saved_draft);
        }
-
-       modest_msg_edit_window_set_draft (edit_window, saved_draft);
+       g_object_unref(edit_window);
 }
 
 void
@@ -2333,7 +2346,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
                                              data->images,
                                              data->priority_flags,
                                              on_save_to_drafts_cb,
-                                             edit_window);
+                                             g_object_ref(edit_window));
        info_text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
        modest_platform_information_banner (NULL, NULL, info_text);
 
@@ -3930,10 +3943,15 @@ on_move_to_dialog_folder_selection_changed (ModestFolderView* self,
 
                if ((gpointer) local_account != (gpointer) folder_store &&
                    (gpointer) mmc_account != (gpointer) folder_store) {
+                       const char *proto_name = tny_account_get_proto (TNY_ACCOUNT (folder_store));
+                       ModestTransportStoreProtocol proto = MODEST_PROTOCOL_STORE_MAILDIR;
+                       if (proto_name != NULL) {
+                               proto = modest_protocol_info_get_transport_store_protocol (proto_name);
+                       }
                        is_local_account = FALSE;
                        /* New button should be dimmed on remote
-                          account root */
-                       new_sensitive = FALSE;
+                          POP account root */
+                       new_sensitive = (proto != MODEST_PROTOCOL_STORE_POP);
                }
                g_object_unref (local_account);
        }
@@ -4244,10 +4262,14 @@ move_to_cb (ModestMailOperation *mail_op,
                if (MODEST_IS_MSG_VIEW_WINDOW (object)) {
                        ModestMsgViewWindow *self = MODEST_MSG_VIEW_WINDOW (object);
 
-                       if (!modest_msg_view_window_select_next_message (self))
-                               if (!modest_msg_view_window_select_previous_message (self))
-                                       /* No more messages to view, so close this window */
-                                       modest_ui_actions_on_close_window (NULL, MODEST_WINDOW(self));
+                       if (modest_msg_view_window_last_message_selected (self) &&
+                           modest_msg_view_window_first_message_selected (self)) {
+                               modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (self));
+                       } else if (!modest_msg_view_window_select_next_message (self) &&
+                                  !modest_msg_view_window_select_previous_message (self)) {
+                               /* No more messages to view, so close this window */
+                               modest_ui_actions_on_close_window (NULL, MODEST_WINDOW(self));
+                       }
                } else if (MODEST_IS_MAIN_WINDOW (object) && helper->reference != NULL) {
                        GtkWidget *header_view;
                        GtkTreePath *path;
@@ -4487,8 +4509,8 @@ modest_ui_actions_on_main_window_remove_attachments (GtkAction *action,
  */
 static void
 xfer_messages_from_move_to_cb  (gboolean canceled, GError *err,
-               GtkWindow *parent_window, 
-               TnyAccount *account, gpointer user_data)
+                               GtkWindow *parent_window, 
+                               TnyAccount *account, gpointer user_data)
 {
        TnyFolderStore *dst_folder = TNY_FOLDER_STORE (user_data);
        ModestWindow *win = MODEST_WINDOW (parent_window);
@@ -4501,13 +4523,6 @@ xfer_messages_from_move_to_cb  (gboolean canceled, GError *err,
                g_object_unref (dst_folder);
                return;
        }
-       
-       if (!TNY_IS_FOLDER (dst_folder)) {
-               modest_platform_information_banner (GTK_WIDGET (win),
-                                                   NULL,
-                                                   _CS("ckdg_ib_unable_to_move_to_current_location"));
-               return;
-       }
 
        dst_account = tny_folder_get_account (TNY_FOLDER (dst_folder));
        proto_str = tny_account_get_proto (dst_account);
@@ -4572,6 +4587,36 @@ xfer_messages_from_move_to_cb  (gboolean canceled, GError *err,
 }
 
 typedef struct {
+       TnyAccount *dst_account;
+       ModestConnectedPerformer callback;
+       gpointer data;
+} DoubleConnectionInfo;
+
+static void
+src_account_connect_performer (gboolean canceled, 
+                              GError *err,
+                              GtkWindow *parent_window, 
+                              TnyAccount *src_account, 
+                              gpointer user_data)
+{
+       DoubleConnectionInfo *info = (DoubleConnectionInfo *) user_data;
+
+       if (canceled || err) {
+               /* If there was any error call the user callback */
+               info->callback (canceled, err, parent_window, src_account, info->data);
+       } else {
+               /* Connect the destination account */
+               modest_platform_connect_if_remote_and_perform (parent_window, TRUE, 
+                                                              TNY_FOLDER_STORE (info->dst_account),
+                                                              info->callback, info->data);
+       }
+
+       /* Free the info object */
+       g_object_unref (info->dst_account);
+       g_slice_free (DoubleConnectionInfo, info);
+}
+
+typedef struct {
        TnyFolder *src_folder;
        TnyFolderStore *dst_folder;
        gboolean delete_original;
@@ -4672,8 +4717,8 @@ modest_ui_actions_on_main_window_move_to (GtkAction *action,
 
                /* Allow only to transfer folders to the local root folder */
                if (TNY_IS_ACCOUNT (dst_folder) && 
-                               !MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (dst_folder)) {
-                                       do_xfer = FALSE;
+                   !MODEST_IS_TNY_LOCAL_FOLDERS_ACCOUNT (dst_folder)) {
+                       do_xfer = FALSE;
                } else if (!TNY_IS_FOLDER (src_folder)) {
                        g_warning ("%s: src_folder is not a TnyFolder.\n", __FUNCTION__);
                        do_xfer = FALSE;
@@ -4698,25 +4743,48 @@ modest_ui_actions_on_main_window_move_to (GtkAction *action,
                }
        } else if (gtk_widget_is_focus (GTK_WIDGET(header_view))) {
                gboolean do_xfer = TRUE;
+
+               /* Show an error when trying to move msgs to an account */      
+               if (!TNY_IS_FOLDER (dst_folder)) {
+                       modest_platform_information_banner (GTK_WIDGET (win),
+                                                           NULL,
+                                                           _CS("ckdg_ib_unable_to_move_to_current_location"));
+                       goto free;
+               }
+
                /* Ask for confirmation if the source folder is remote and we're not connected */
                if (!online && modest_tny_folder_store_is_remote(src_folder)) {
                        TnyList *headers = modest_header_view_get_selected_headers(header_view);
                        if (!msgs_already_deleted_from_server(headers, src_folder)) {
                                guint num_headers = tny_list_get_length(headers);
                                TnyAccount *account = get_account_from_header_list (headers);
-                               if (!connect_to_get_msg(MODEST_WINDOW (win), num_headers, account))
+                               GtkResponseType response;
+
+                               response = modest_platform_run_confirmation_dialog (GTK_WINDOW (win),
+                                                                                   ngettext("mcen_nc_get_msg",
+                                                                                            "mcen_nc_get_msgs",
+                                                                                            num_headers));
+                               if (response == GTK_RESPONSE_CANCEL)
                                        do_xfer = FALSE;
+                               
                                g_object_unref (account);
                        }
                        g_object_unref(headers);
                }
                if (do_xfer) /* Transfer messages */ {
-                       g_object_ref (dst_folder);
+                       DoubleConnectionInfo *info = g_slice_new (DoubleConnectionInfo);
+                       info->callback = xfer_messages_from_move_to_cb;
+                       info->dst_account = tny_folder_get_account (TNY_FOLDER (dst_folder));
+                       info->data = g_object_ref (dst_folder);
+
                        modest_platform_connect_if_remote_and_perform(GTK_WINDOW (win), TRUE,
-                                       TNY_FOLDER_STORE (dst_folder), xfer_messages_from_move_to_cb, dst_folder);
+                                                                     TNY_FOLDER_STORE (src_folder), 
+                                                                     src_account_connect_performer, 
+                                                                     info);
                }
        }
 
+ free:
        if (src_folder)
                g_object_unref (src_folder);
 }
@@ -4998,13 +5066,31 @@ modest_ui_actions_on_retrieve_msg_contents (GtkAction *action,
 }
 
 void
+modest_ui_actions_check_toolbar_dimming_rules (ModestWindow *window)
+{
+       g_return_if_fail (MODEST_IS_WINDOW (window));
+
+       /* Update dimmed */
+       modest_window_check_dimming_rules_group (window, MODEST_DIMMING_RULES_TOOLBAR);
+}
+
+void
+modest_ui_actions_check_menu_dimming_rules (ModestWindow *window)
+{
+       g_return_if_fail (MODEST_IS_WINDOW (window));
+
+       /* Update dimmed */
+       modest_window_check_dimming_rules_group (window, MODEST_DIMMING_RULES_MENU);
+}
+
+void
 modest_ui_actions_on_email_menu_activated (GtkAction *action,
                                          ModestWindow *window)
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
        
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5013,8 +5099,8 @@ modest_ui_actions_on_edit_menu_activated (GtkAction *action,
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
 
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5023,8 +5109,8 @@ modest_ui_actions_on_view_menu_activated (GtkAction *action,
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
 
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5033,8 +5119,8 @@ modest_ui_actions_on_format_menu_activated (GtkAction *action,
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
 
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5043,8 +5129,8 @@ modest_ui_actions_on_tools_menu_activated (GtkAction *action,
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
 
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5053,8 +5139,8 @@ modest_ui_actions_on_attachment_menu_activated (GtkAction *action,
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
 
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5063,8 +5149,8 @@ modest_ui_actions_on_toolbar_csm_menu_activated (GtkAction *action,
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
 
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5073,8 +5159,8 @@ modest_ui_actions_on_folder_view_csm_menu_activated (GtkAction *action,
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
 
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5083,17 +5169,8 @@ modest_ui_actions_on_header_view_csm_menu_activated (GtkAction *action,
 {
        g_return_if_fail (MODEST_IS_WINDOW (window));
 
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestMenuDimmingRules");     
-}
-
-void
-modest_ui_actions_check_toolbar_dimming_rules (ModestWindow *window)
-{
-       g_return_if_fail (MODEST_IS_WINDOW (window));
-
-       /* Update dimmed */     
-       modest_window_check_dimming_rules_group (window, "ModestToolbarDimmingRules");  
+       /* Update dimmed */
+       modest_ui_actions_check_menu_dimming_rules (window);
 }
 
 void
@@ -5187,7 +5264,7 @@ modest_ui_actions_on_send_queue_error_happened (TnySendQueue *self,
        gchar *message = NULL;
 
        /* Don't show anything if the user cancelled something */
-       if (err->code == TNY_TRANSPORT_ACCOUNT_ERROR_SEND_USER_CANCEL)
+       if (err->code == TNY_SYSTEM_ERROR_CANCEL)
                return;
 
        /* Get the server name: */
@@ -5204,20 +5281,17 @@ modest_ui_actions_on_send_queue_error_happened (TnySendQueue *self,
 
        /* Show the appropriate message text for the GError: */
        switch (err->code) {
-       case TNY_TRANSPORT_ACCOUNT_ERROR_SEND_HOST_LOOKUP_FAILED:
-               message = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
-               break;
-       case TNY_TRANSPORT_ACCOUNT_ERROR_SEND_SERVICE_UNAVAILABLE:
+       case TNY_SERVICE_ERROR_CONNECT:
                message = g_strdup_printf (_("emev_ib_ui_smtp_server_invalid"), server_name);
                break;
-       case TNY_TRANSPORT_ACCOUNT_ERROR_SEND_AUTHENTICATION_NOT_SUPPORTED:
+       case TNY_SERVICE_ERROR_AUTHENTICATE:
                message = g_strdup_printf (_("emev_ni_ui_smtp_authentication_fail_error"), server_name);
                break;
-       case TNY_TRANSPORT_ACCOUNT_ERROR_SEND:
+       case TNY_SERVICE_ERROR_SEND:
                message = g_strdup (_("emev_ib_ui_smtp_send_error"));
                break;
        default:
-               g_warning ("%s: unexpected TNY_TRANSPORT_ACCOUNT_ERROR %d",
+               g_warning ("%s: unexpected ERROR %d",
                           __FUNCTION__, err->code);
                message = g_strdup (_("emev_ib_ui_smtp_send_error"));
                break;  
@@ -5272,6 +5346,10 @@ modest_ui_actions_on_send_queue_status_changed (ModestTnySendQueue *send_queue,
 #else
        gtk_widget_queue_draw (header_view);
 #endif         
+
+       /* Rerun dimming rules, because the message could become deletable for example */
+       modest_window_check_dimming_rules_group (MODEST_WINDOW (main_window), 
+                                                MODEST_DIMMING_RULES_TOOLBAR);
        
        /* Free */
  frees: