Work to fix recovering sort status in headers view (fixes NB#58250).
[modest] / src / modest-ui-dimming-rules.c
index b1379ec..477707a 100644 (file)
 
 static gboolean _folder_is_any_of_type (TnyFolder *folder, TnyFolderType types[], guint ntypes);
 static gboolean _invalid_msg_selected (ModestMainWindow *win, gboolean unique, ModestDimmingRule *rule);
-static gboolean _invalid_attach_selected (ModestMsgViewWindow *win, gboolean unique, ModestDimmingRule *rule);
+static gboolean _invalid_attach_selected (ModestWindow *win, gboolean unique, ModestDimmingRule *rule);
 static gboolean _invalid_clipboard_selected (ModestMsgViewWindow *win);
 static gboolean _already_opened_msg (ModestWindow *win);
 static gboolean _selected_msg_marked_as (ModestWindow *win, TnyHeaderFlags mask, gboolean opposite);
 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"));
                }
@@ -499,7 +529,7 @@ modest_ui_dimming_rules_on_view_attachments (ModestWindow *win, gpointer user_da
 
        /* Check dimmed rule */ 
        if (!dimmed) 
-               dimmed = _invalid_attach_selected (MODEST_MSG_VIEW_WINDOW(win), TRUE, rule);                    
+               dimmed = _invalid_attach_selected (win, TRUE, rule);                    
                
        return dimmed;
 }
@@ -516,7 +546,7 @@ modest_ui_dimming_rules_on_save_attachments (ModestWindow *win, gpointer user_da
 
        /* Check dimmed rule */ 
        if (!dimmed) 
-               dimmed = _invalid_attach_selected (MODEST_MSG_VIEW_WINDOW(win), FALSE, rule);                   
+               dimmed = _invalid_attach_selected (win, FALSE, rule);                   
                
        return dimmed;
 }
@@ -527,13 +557,13 @@ modest_ui_dimming_rules_on_remove_attachments (ModestWindow *win, gpointer user_
        ModestDimmingRule *rule = NULL;
        gboolean dimmed = FALSE;
 
-       g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
+       g_return_val_if_fail (MODEST_IS_WINDOW(win), 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 = _invalid_attach_selected (MODEST_MSG_VIEW_WINDOW(win), FALSE, NULL);                   
+               dimmed = _invalid_attach_selected (win, FALSE, NULL);                   
                modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_display_more"));
        }
 
@@ -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)
 {
@@ -833,54 +912,62 @@ _invalid_clipboard_selected (ModestMsgViewWindow *win)
 }
 
 static gboolean
-_invalid_attach_selected (ModestMsgViewWindow *win,
+_invalid_attach_selected (ModestWindow *win,
                          gboolean unique,
                          ModestDimmingRule *rule) 
 {
        GList *attachments, *node;
        gint n_selected;
+       TnyHeaderFlags flags;
        gboolean nested_attachments = FALSE;
        gboolean selected_messages = FALSE;
        gboolean result = FALSE;
 
-       g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), TRUE);
-
-       /* Get selected atachments */
-       attachments = modest_msg_view_window_get_attachments (win);
-       n_selected = g_list_length (attachments);
-       for (node = attachments; node != NULL; node = g_list_next (node)) {
-               TnyMimePart *mime_part = TNY_MIME_PART (node->data);
-               TnyList *nested_list = tny_simple_list_new ();
-               if (!tny_mime_part_is_attachment (mime_part)) {
-                       selected_messages = TRUE;
-                       break;
-               }
-               tny_mime_part_get_parts (mime_part, nested_list);
-               if (tny_list_get_length (nested_list) > 0)
-                       nested_attachments = TRUE;
-               g_object_unref (nested_list);
+       if (MODEST_IS_MAIN_WINDOW (win)) {
+               flags = TNY_HEADER_FLAG_ATTACHMENTS;
+               if (!result)
+                       result = _selected_msg_marked_as (win, flags, TRUE);
+               
        }
-
-       /* Check unique */
-       if (unique) 
-               result = n_selected != 1;
-       else
+       else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
                
-               result = n_selected == 1;
-
-       /* Set notifications */
-       if (!result && rule != NULL) {
-               if (selected_messages) {
-                       modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_save_attach_mail"));
-               } else if (nested_attachments) {
-                       modest_dimming_rule_set_notification (rule, _("FIXME:unable to save attachments with nested elements"));
-               } else if (n_selected == 0) {
-                       modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
+               /* Get selected atachments */
+               attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
+               n_selected = g_list_length (attachments);
+               for (node = attachments; node != NULL; node = g_list_next (node)) {
+                       TnyMimePart *mime_part = TNY_MIME_PART (node->data);
+                       TnyList *nested_list = tny_simple_list_new ();
+                       if (!tny_mime_part_is_attachment (mime_part)) {
+                               selected_messages = TRUE;
+                               break;
+                       }
+                       tny_mime_part_get_parts (mime_part, nested_list);
+                       if (tny_list_get_length (nested_list) > 0)
+                               nested_attachments = TRUE;
+                       g_object_unref (nested_list);
                }
+               
+               /* Check unique */
+               if (unique) 
+                       result = n_selected != 1;
+               else
+                       
+                       result = n_selected < 1;
+               
+               /* Set notifications */
+               if (!result && rule != NULL) {
+                       if (selected_messages) {
+                               modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_save_attach_mail"));
+                       } else if (nested_attachments) {
+                               modest_dimming_rule_set_notification (rule, _("FIXME:unable to save attachments with nested elements"));
+                       } else if (n_selected == 0) {
+                               modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
+                       }
+               }
+               
+               /* Free */
+               g_list_free (attachments);
        }
-       
-       /* Free */
-       g_list_free (attachments);
 
        return result;
 }
@@ -1074,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;
+}