Work to fix recovering sort status in headers view (fixes NB#58250).
[modest] / src / modest-ui-dimming-rules.c
index fa1feb7..477707a 100644 (file)
@@ -49,11 +49,13 @@ static gboolean _selected_msg_marked_as (ModestWindow *win, TnyHeaderFlags mask,
 static gboolean _selected_folder_not_writeable (ModestMainWindow *win);
 static gboolean _selected_folder_is_any_of_type (ModestWindow *win, TnyFolderType types[], guint ntypes);
 static gboolean _selected_folder_is_root_or_inbox (ModestMainWindow *win);
+static gboolean _selected_folder_is_MMC_or_POP_root (ModestMainWindow *win);
 static gboolean _selected_folder_is_root (ModestMainWindow *win);
 static gboolean _selected_folder_is_empty (ModestMainWindow *win);
 static gboolean _msg_download_in_progress (ModestMsgViewWindow *win);
 static gboolean _msg_download_completed (ModestMainWindow *win);
-static gboolean _msg_sent_in_progress (ModestWindow *win);
+static gboolean _selected_msg_sent_in_progress (ModestWindow *win);
+static gboolean _sending_in_progress (ModestWindow *win);
 static gboolean _marked_as_deleted (ModestWindow *win);
 
 
@@ -101,9 +103,18 @@ modest_ui_dimming_rules_on_new_folder (ModestWindow *win, gpointer user_data)
                }
        } else {
                /* TODO: the specs say that only one level of subfolder is allowed, is this true ? */
+               
+               TnyFolderType types[3];
+                               
+               types[0] = TNY_FOLDER_TYPE_DRAFTS; 
+               types[1] = TNY_FOLDER_TYPE_OUTBOX;
+               types[2] = TNY_FOLDER_TYPE_SENT;
 
                /* Apply folder rules */        
-               dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
+               if (!dimmed)
+                       dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
+               if (!dimmed)
+                       dimmed = _selected_folder_is_any_of_type (win, types, 3);
        }
 
        return dimmed;
@@ -113,6 +124,13 @@ gboolean
 modest_ui_dimming_rules_on_delete_folder (ModestWindow *win, gpointer user_data)
 {
        gboolean dimmed = FALSE;
+       TnyFolderType types[3];
+
+       g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
+
+       types[0] = TNY_FOLDER_TYPE_DRAFTS; 
+       types[1] = TNY_FOLDER_TYPE_OUTBOX;
+       types[2] = TNY_FOLDER_TYPE_SENT;
 
        g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
                
@@ -121,6 +139,10 @@ modest_ui_dimming_rules_on_delete_folder (ModestWindow *win, gpointer user_data)
                dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
        if (!dimmed)
                dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
+       if (!dimmed)
+               dimmed = _selected_folder_is_MMC_or_POP_root (MODEST_MAIN_WINDOW(win));
+       if (!dimmed)
+               dimmed = _selected_folder_is_any_of_type (win, types, 3);
 
        return dimmed;
 }
@@ -144,14 +166,23 @@ gboolean
 modest_ui_dimming_rules_on_rename_folder (ModestWindow *win, gpointer user_data)
 {
        gboolean dimmed = FALSE;
+       TnyFolderType types[3];
 
        g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
+
+       types[0] = TNY_FOLDER_TYPE_DRAFTS; 
+       types[1] = TNY_FOLDER_TYPE_OUTBOX;
+       types[2] = TNY_FOLDER_TYPE_SENT;
+       
+       g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
                
        /* Check dimmed rule */ 
        if (!dimmed)
                dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
        if (!dimmed)
                dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
+       if (!dimmed)
+               dimmed = _selected_folder_is_any_of_type (win, types, 3);
 
        return dimmed;
 }
@@ -170,7 +201,7 @@ modest_ui_dimming_rules_on_open_msg (ModestWindow *win, gpointer user_data)
        if (!dimmed)
                dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, user_data);
        if (!dimmed) {
-               dimmed = _msg_sent_in_progress (win);
+               dimmed = _selected_msg_sent_in_progress (win);
                if (dimmed)
                        modest_dimming_rule_set_notification (rule, _("TEST"));
        }
@@ -256,8 +287,7 @@ modest_ui_dimming_rules_on_delete_msg (ModestWindow *win, gpointer user_data)
 {
        ModestDimmingRule *rule = NULL;
        gboolean dimmed = FALSE;
-       
-       g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (win), FALSE);
+
        g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
        rule = MODEST_DIMMING_RULE (user_data);
        
@@ -284,7 +314,7 @@ modest_ui_dimming_rules_on_delete_msg (ModestWindow *win, gpointer user_data)
                                modest_dimming_rule_set_notification (rule, _("mcen_ib_message_unableto_delete"));
                }
                if (!dimmed) {
-                       dimmed = _msg_sent_in_progress (win);
+                       dimmed = _selected_msg_sent_in_progress (win);
                        if (dimmed)
                                modest_dimming_rule_set_notification (rule, _("mcen_ib_message_unableto_delete"));
                }
@@ -583,7 +613,8 @@ modest_ui_dimming_rules_on_view_next (ModestWindow *win, gpointer user_data)
 }
 
 
-gboolean modest_ui_dimming_rules_on_tools_smtp_servers (ModestWindow *win, gpointer user_data)
+gboolean 
+modest_ui_dimming_rules_on_tools_smtp_servers (ModestWindow *win, gpointer user_data)
 {
        const gboolean dimmed = 
                !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
@@ -592,6 +623,36 @@ gboolean modest_ui_dimming_rules_on_tools_smtp_servers (ModestWindow *win, gpoin
        return dimmed;
 }
 
+gboolean 
+modest_ui_dimming_rules_on_cancel_sending (ModestWindow *win, gpointer user_data)
+{
+       gboolean dimmed = FALSE;
+
+       /* Check dimmed rule */ 
+       if (!dimmed) 
+               dimmed = !_sending_in_progress (win);
+               
+       return dimmed;
+}
+
+gboolean 
+modest_ui_dimming_rules_on_send_receive (ModestWindow *win, gpointer user_data)
+{
+       ModestDimmingRule *rule = NULL;
+       gboolean dimmed = FALSE;
+       
+       g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
+       rule = MODEST_DIMMING_RULE (user_data);
+       /* Check dimmed rule */ 
+       if (!dimmed) {
+               dimmed = !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
+                                                         TRUE);        
+               modest_dimming_rule_set_notification (rule, _("mcen_nc_no_email_acnts_defined"));
+       }
+
+       return dimmed;
+}
 
 /* *********************** static utility functions ******************** */
 
@@ -657,39 +718,15 @@ _selected_folder_is_root_or_inbox (ModestMainWindow *win)
        /* Check folder type */
        result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 2);
 
+       /* Check pop and MMC accounts */
        if (!result) {
-               GtkWidget *folder_view = NULL;
-               TnyFolderStore *parent_folder = NULL;
-               
-               folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
-                                                                  MODEST_WIDGET_TYPE_FOLDER_VIEW);
-               /* Get selected folder as parent of new folder to create */
-               parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
-               if (!parent_folder)
-                       return TRUE;
-
-               if (TNY_IS_ACCOUNT (parent_folder)) {
-                       /* If it's the local account then do not dim */
-                       if (modest_tny_account_is_virtual_local_folders (
-                                                                        TNY_ACCOUNT (parent_folder)))
-                               return FALSE;
-                       else {
-                               /* If it's the MMC root folder then dim it */
-                               if (!strcmp (tny_account_get_id (TNY_ACCOUNT (parent_folder)), MODEST_MMC_ACCOUNT_ID)) {
-                                       result = TRUE;
-                               } else {
-                                       const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (parent_folder));
-                                       /* If it's POP then dim */
-                                       result = (modest_protocol_info_get_transport_store_protocol (proto_str) == 
-                                                 MODEST_PROTOCOL_STORE_POP) ? TRUE : FALSE;
-                               }
-                       }
-               }
+               result = _selected_folder_is_MMC_or_POP_root (win);
        }
                
        return result;
 }
 
+
 static gboolean
 _selected_folder_is_root (ModestMainWindow *win)
 {
@@ -703,10 +740,52 @@ _selected_folder_is_root (ModestMainWindow *win)
        /* Check folder type */
        result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 1);
                
+       /* Check pop and MMC accounts */
+       if (!result) {
+               result = _selected_folder_is_MMC_or_POP_root (win);
+       }
+
+       return result;
+}
+
+static gboolean
+_selected_folder_is_MMC_or_POP_root (ModestMainWindow *win)
+{
+       GtkWidget *folder_view = NULL;
+       TnyFolderStore *parent_folder = NULL;
+       gboolean result = FALSE;
+
+       folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
+                                                          MODEST_WIDGET_TYPE_FOLDER_VIEW);
+       /* Get selected folder as parent of new folder to create */
+       parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
+       if (!parent_folder)
+               return TRUE;
+       
+       if (TNY_IS_ACCOUNT (parent_folder)) {
+               /* If it's the local account then do not dim */
+               if (modest_tny_account_is_virtual_local_folders (
+                                                                TNY_ACCOUNT (parent_folder)))
+                       return FALSE;
+               else {
+                               /* If it's the MMC root folder then dim it */
+                       if (!strcmp (tny_account_get_id (TNY_ACCOUNT (parent_folder)), MODEST_MMC_ACCOUNT_ID)) {
+                                       result = TRUE;
+                       } else {
+                               const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (parent_folder));
+                               /* If it's POP then dim */
+                               result = (modest_protocol_info_get_transport_store_protocol (proto_str) == 
+                                                 MODEST_PROTOCOL_STORE_POP) ? TRUE : FALSE;
+                       }
+               }
+       }
+
        return result;
 }
 
 
+
+
 static gboolean
 _selected_folder_is_empty (ModestMainWindow *win)
 {
@@ -873,7 +952,7 @@ _invalid_attach_selected (ModestWindow *win,
                        result = n_selected != 1;
                else
                        
-                       result = n_selected == 1;
+                       result = n_selected < 1;
                
                /* Set notifications */
                if (!result && rule != NULL) {
@@ -1082,66 +1161,95 @@ _msg_download_completed (ModestMainWindow *win)
 }
 
 static gboolean
-_msg_sent_in_progress (ModestWindow *win)
+_selected_msg_sent_in_progress (ModestWindow *win)
 {
-/*     ModestTnySendQueue *send_queue = NULL; */
-/*     GtkWidget *header_view = NULL;  */
-/*     ModestTnyAccountStore *acc_store = NULL; */
-/*     TnyAccount *account = NULL;      */
-/*     TnyList *header_list = NULL; */
-/*     TnyIterator *iter = NULL; */
-/*     TnyHeader *header = NULL; */
-/*     const gchar *account_name = NULL; */
+       ModestTnySendQueue *send_queue = NULL;
+       GtkWidget *header_view = NULL;
+       ModestTnyAccountStore *acc_store = NULL;
+       TnyAccount *account = NULL;
+       TnyList *header_list = NULL;
+       TnyIterator *iter = NULL;
+       TnyHeader *header = NULL;
+       const gchar *account_name = NULL;
        gboolean result = FALSE;
-/*     gchar *id = NULL; */
+       gchar *id = NULL;
        
-/*     g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), FALSE); */
+       /* Get transport account */
+       acc_store = modest_runtime_get_account_store();
+       account_name = modest_window_get_active_account (win);
        
-/*     /\* Get transport account *\/   */
-/*     acc_store = modest_runtime_get_account_store(); */
-/*     account_name = modest_window_get_active_account (win);   */
-/*     account = modest_tny_account_store_get_transport_account_for_open_connection (acc_store, account_name); */
+       /* If no account defined, this action must be always dimmed  */
+       if (account_name == NULL) return FALSE;
+       account = modest_tny_account_store_get_transport_account_for_open_connection (acc_store, account_name);
+       if (!TNY_IS_TRANSPORT_ACCOUNT (account)) return FALSE;
 
-/*     /\* Get send queue for current ransport account *\/  */
-/*     send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(account)); */
-/*     g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE (send_queue), FALSE); */
+       /* Get send queue for current ransport account */
+       send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(account));
+       g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE (send_queue), FALSE);
 
-/*     if (MODEST_IS_MAIN_WINDOW(win)) { */
+       if (MODEST_IS_MAIN_WINDOW(win)) {
                
-/*             /\* Get header view to check selected messages *\/ */
-/*             header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win), */
-/*                                                                MODEST_WIDGET_TYPE_HEADER_VIEW); */
+               /* Get header view to check selected messages */
+               header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
+                                                                  MODEST_WIDGET_TYPE_HEADER_VIEW);
                
-/*             /\* Get selected headers *\/ */
-/*             header_list = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view)); */
+               /* Get selected headers */
+               header_list = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
 
-/*             /\* Get message header *\/ */
-/*             if (!header_list) return FALSE; */
-/*             iter = tny_list_create_iterator (header_list); */
-/*             header = TNY_HEADER (tny_iterator_get_current (iter)); */
+               /* Get message header */
+               if (!header_list) return FALSE;
+               iter = tny_list_create_iterator (header_list);
+               header = TNY_HEADER (tny_iterator_get_current (iter));
 
-/*             /\* Get message id *\/ */
-/*             id = g_strdup(tny_header_get_message_id (header));               */
+               /* Get message id */
+               id = g_strdup(tny_header_get_message_id (header));
                
-/*         } else if (MODEST_IS_MSG_VIEW_WINDOW(win)) { */
+        } else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
                
-/*             /\* Get message header *\/ */
-/*             header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW(win));                */
+               /* Get message header */
+               header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW(win));
 
-/*             /\* Get message id *\/ */
-/*             id = g_strdup(tny_header_get_message_id (header));               */
-/*     } */
+               /* Get message id */
+               id = g_strdup(tny_header_get_message_id (header));
+       }
 
-/*     /\* Check if msg id is being processed inside send queue *\/ */
-/*     result = modest_tny_send_queue_msg_is_being_sent (send_queue, id); */
+       /* Check if msg id is being processed inside send queue */
+       result = modest_tny_send_queue_msg_is_being_sent (send_queue, id);
 
-/*     /\* Free *\/ */
-/*     g_free(id); */
-/*     g_object_unref (header); */
-/*     g_free(header_list); */
-/*     g_free(iter); */
+       /* Free */
+       g_free(id);
+       g_object_unref (header);
+       g_object_unref(header_list);
+       g_object_unref(iter);
 
        return result;
 }
 
 
+static gboolean
+_sending_in_progress (ModestWindow *win)
+{
+       ModestTnySendQueue *send_queue = NULL;
+       ModestTnyAccountStore *acc_store = NULL;
+       TnyAccount *account = NULL;
+       const gchar *account_name = NULL;
+       gboolean result = FALSE;
+       
+       /* Get transport account */
+       acc_store = modest_runtime_get_account_store();
+       account_name = modest_window_get_active_account (win);
+
+       /* If no account defined, this action must be always dimmed  */
+       if (account_name == NULL) return FALSE;
+       account = modest_tny_account_store_get_transport_account_for_open_connection (acc_store, account_name);
+       if (!TNY_IS_TRANSPORT_ACCOUNT (account)) return FALSE;
+
+       /* Get send queue for current ransport account */
+       send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(account));
+       g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE (send_queue), FALSE);
+
+       /* Check if send queue is perfimring any send operation */
+       result = modest_tny_send_queue_sending_in_progress (send_queue);
+
+       return result;
+}