Moved check_for_active_account to modest_ui_actions
[modest] / src / modest-ui-actions.c
index 13df186..c69a15e 100644 (file)
@@ -521,7 +521,6 @@ modest_ui_actions_on_edit_mode_delete_message (ModestWindow *win)
 
 
        if (response == GTK_RESPONSE_OK) {
-               ModestWindow *main_window = NULL;
                ModestWindowMgr *mgr = NULL;
                GtkTreeModel *model = NULL;
                GtkTreeSelection *sel = NULL;
@@ -552,7 +551,7 @@ modest_ui_actions_on_edit_mode_delete_message (ModestWindow *win)
                }
 
                /* Disable window dimming management */
-               modest_window_disable_dimming (MODEST_WINDOW(win));
+               modest_window_disable_dimming (win);
 
                /* Remove each header. If it's a view window header_view == NULL */
                mail_op = modest_mail_operation_new ((GObject *) win);
@@ -565,18 +564,14 @@ modest_ui_actions_on_edit_mode_delete_message (ModestWindow *win)
                if (sel != NULL) {
                        gtk_tree_selection_unselect_all (sel);
                }
-               modest_window_enable_dimming (MODEST_WINDOW(win));
+               modest_window_enable_dimming (win);
 
                if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
                        modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (win));
 
                        /* Get main window */
                        mgr = modest_runtime_get_window_mgr ();
-                       main_window = modest_window_mgr_get_main_window (mgr, FALSE); /* don't create */
                } else if (MODEST_IS_MAIN_WINDOW (win)) {
-                       /* Move cursor to next row */
-                       main_window = win;
-
                        /* Select next or previous row */
                        if (gtk_tree_row_reference_valid (next_row_reference)) {
                                gtk_tree_selection_select_path (sel, next_path);
@@ -597,10 +592,8 @@ modest_ui_actions_on_edit_mode_delete_message (ModestWindow *win)
                }
 
                /* Update toolbar dimming state */
-               if (main_window) {
-                       modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (main_window));
-                       modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (main_window));
-               }
+               modest_ui_actions_check_menu_dimming_rules (win);
+               modest_ui_actions_check_toolbar_dimming_rules (win);
 
                /* Free */
                g_list_foreach (sel_list, (GFunc) gtk_tree_path_free, NULL);
@@ -1001,7 +994,8 @@ modest_ui_actions_msg_retrieval_check (ModestMailOperation *mail_op,
                              error->code == TNY_SERVICE_ERROR_MESSAGE_NOT_AVAILABLE)) {
                        gchar *subject, *msg, *format = NULL;
                        TnyAccount *account;
-                       subject = tny_header_dup_subject (header);
+                       
+                       subject = header?tny_header_dup_subject (header):NULL;
                        if (!subject)
                                subject = g_strdup (_("mail_va_no_subject"));
 
@@ -1045,6 +1039,7 @@ typedef struct {
 typedef struct {
        GtkTreeModel *model;
        TnyHeader *header;
+       ModestWindow *caller_window;
        OpenMsgBannerInfo *banner_info;
        GtkTreeRowReference *rowref;
 } OpenMsgHelper;
@@ -1349,10 +1344,22 @@ get_account_from_header (TnyHeader *header)
 }
 
 static void
+caller_win_destroyed (OpenMsgHelper *helper, GObject *object)
+{
+       if (helper->caller_window)
+               helper->caller_window = NULL;
+}
+
+static void
 open_msg_helper_destroyer (gpointer user_data)
 {
        OpenMsgHelper *helper = (OpenMsgHelper *) user_data;
 
+       if (helper->caller_window) {
+               g_object_weak_unref ((GObject *) helper->caller_window, (GWeakNotify) caller_win_destroyed, helper);
+               helper->caller_window = NULL;
+       }
+
        if (helper->banner_info) {
                g_free (helper->banner_info->message);
                if (helper->banner_info->idle_handler > 0) {
@@ -1392,7 +1399,7 @@ open_msg_performer(gboolean canceled,
        helper = (OpenMsgHelper *) user_data;
 
        status = tny_account_get_connection_status (account);
-       if (err || canceled) {
+       if (err || canceled || helper->caller_window == NULL) {
                modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (), helper->header);
                /* Free the helper */
                open_msg_helper_destroyer (helper);
@@ -1591,6 +1598,8 @@ open_msg_from_header (TnyHeader *header, GtkTreeRowReference *rowref, ModestWind
           (the user could switch between folders) */
        helper = g_slice_new (OpenMsgHelper);
        helper->model = g_object_ref (gtk_tree_view_get_model (GTK_TREE_VIEW (header_view)));
+       helper->caller_window = win;
+       g_object_weak_ref ((GObject *) helper->caller_window, (GWeakNotify) caller_win_destroyed, helper);
        helper->header = g_object_ref (header);
        helper->rowref = gtk_tree_row_reference_copy (rowref);
        helper->banner_info = NULL;
@@ -5379,7 +5388,7 @@ modest_ui_actions_move_folder_error_handler (ModestMailOperation *mail_op,
 {
        GObject *win = NULL;
        const GError *error;
-       TnyAccount *account;
+       TnyAccount *account = NULL;
 
 #ifndef MODEST_TOOLKIT_HILDON2
        ModestWindow *main_window = NULL;
@@ -5404,7 +5413,11 @@ modest_ui_actions_move_folder_error_handler (ModestMailOperation *mail_op,
 #endif
        win = modest_mail_operation_get_source (mail_op);
        error = modest_mail_operation_get_error (mail_op);
-       account = modest_mail_operation_get_account (mail_op);
+
+       if (TNY_IS_FOLDER (user_data))
+               account = modest_tny_folder_get_account (TNY_FOLDER (user_data));
+       else if (TNY_IS_ACCOUNT (user_data))
+               account = g_object_ref (user_data);
 
        /* If it's not a disk full error then show a generic error */
        if (!modest_tny_account_store_check_disk_full_error (modest_runtime_get_account_store(),
@@ -5817,15 +5830,10 @@ on_move_folder_cb (gboolean canceled,
 
        mail_op =
                modest_mail_operation_new_with_error_handling (G_OBJECT(parent_window),
-                               modest_ui_actions_move_folder_error_handler,
-                               info->src_folder, NULL);
+                                                              modest_ui_actions_move_folder_error_handler,
+                                                              g_object_ref (info->dst_folder), g_object_unref);
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
-                       mail_op);
-
-       /* Select *after* the changes */
-       /* TODO: this function hangs UI after transfer */
-       /*                      modest_folder_view_select_folder (MODEST_FOLDER_VIEW(folder_view), */
-       /*                                                        TNY_FOLDER (src_folder), TRUE); */
+                                        mail_op);
 
        if (MODEST_IS_MAIN_WINDOW (parent_window)) {
                modest_folder_view_select_folder (MODEST_FOLDER_VIEW(info->folder_view),
@@ -6909,3 +6917,89 @@ modest_ui_actions_on_reload_message (const gchar *msg_id)
 
        modest_msg_view_window_reload (MODEST_MSG_VIEW_WINDOW (window));
 }
+
+/** Check whether any connections are active, and cancel them if 
+ * the user wishes.
+ * Returns TRUE is there was no problem, 
+ * or if an operation was cancelled so we can continue.
+ * Returns FALSE if the user chose to cancel his request instead.
+ */
+
+gboolean
+modest_ui_actions_check_for_active_account (ModestWindow *self,
+                                           const gchar* account_name)
+{
+       ModestTnySendQueue *send_queue;
+       ModestTnyAccountStore *acc_store;
+       ModestMailOperationQueue* queue;
+       TnyConnectionStatus store_conn_status;
+       TnyAccount *store_account = NULL, *transport_account = NULL;
+       gboolean retval = TRUE, sending = FALSE;
+
+       acc_store = modest_runtime_get_account_store ();
+       queue = modest_runtime_get_mail_operation_queue ();
+
+       store_account = 
+               modest_tny_account_store_get_server_account (acc_store,
+                                                            account_name,
+                                                            TNY_ACCOUNT_TYPE_STORE);
+
+       /* This could happen if the account was deleted before the
+          call to this function */
+       if (!store_account)
+               return FALSE;
+
+       transport_account = 
+               modest_tny_account_store_get_server_account (acc_store,
+                                                            account_name,
+                                                            TNY_ACCOUNT_TYPE_TRANSPORT);
+
+       /* This could happen if the account was deleted before the
+          call to this function */
+       if (!transport_account) {
+               g_object_unref (store_account);
+               return FALSE;
+       }
+
+       /* If the transport account was not used yet, then the send
+          queue could not exist (it's created on demand) */
+       send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT (transport_account), FALSE);
+       if (TNY_IS_SEND_QUEUE (send_queue))
+               sending = modest_tny_send_queue_sending_in_progress (send_queue);
+
+       store_conn_status = tny_account_get_connection_status (store_account);
+       if (store_conn_status == TNY_CONNECTION_STATUS_CONNECTED || sending) {
+               gint response;
+
+               response = modest_platform_run_confirmation_dialog (GTK_WINDOW (self), 
+                                                               _("emev_nc_disconnect_account"));
+               if (response == GTK_RESPONSE_OK) {
+                       retval = TRUE;
+               } else {
+                       retval = FALSE;
+               }
+       }
+
+       if (retval) {
+
+               /* FIXME: We should only cancel those of this account */
+               modest_mail_operation_queue_cancel_all (queue);
+
+               /* Also disconnect the account */
+               if ((tny_account_get_connection_status (store_account) != TNY_CONNECTION_STATUS_DISCONNECTED) &&
+                   (tny_account_get_connection_status (store_account) != TNY_CONNECTION_STATUS_DISCONNECTED_BROKEN)) {
+                       tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (store_account),
+                                                     FALSE, NULL, NULL);
+               }
+               if (sending) {
+                       tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (transport_account),
+                                                     FALSE, NULL, NULL);
+               }
+       }
+               
+       /* Frees */
+       g_object_unref (store_account);
+       g_object_unref (transport_account);
+       
+       return retval;
+}