Check null value of account before unref in on_open_message_performer (fix 7 for...
[modest] / src / modest-ui-dimming-rules.c
index 6891397..5ee8d78 100644 (file)
@@ -34,6 +34,7 @@
 #include <string.h>
 #include "modest-ui-dimming-rules.h"
 #include "modest-ui-actions.h"
+#include "modest-account-mgr-helpers.h"
 #include "modest-dimming-rule.h"
 #include "modest-debug.h"
 #include "modest-tny-folder.h"
@@ -46,6 +47,7 @@
 #include <widgets/modest-attachments-view.h>
 #include <modest-runtime.h>
 #include <tny-simple-list.h>
+#include <tny-merge-folder.h>
 #include <widgets/modest-recpt-editor.h>
 #include <gtkhtml/gtkhtml.h>
 #include <modest-runtime.h>
@@ -83,7 +85,8 @@ static gboolean _selected_folder_has_subfolder_with_same_name (ModestWindow *win
 static void fill_list_of_caches (gpointer key, gpointer value, gpointer userdata);
 static gboolean _send_receive_in_progress (ModestWindow *win);
 static gboolean _msgs_send_in_progress (void);
-static gboolean _all_msgs_in_sending_status (ModestHeaderView *header_view);
+static gboolean _all_msgs_in_sending_status (ModestHeaderView *header_view) G_GNUC_UNUSED;
+static gboolean _forbid_outgoing_xfers (ModestWindow *window);
 
 static DimmedState *
 _define_main_window_dimming_state (ModestMainWindow *window)
@@ -489,7 +492,8 @@ modest_ui_dimming_rules_on_delete (ModestWindow *win, gpointer user_data)
                        header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (win)));
                        folder = modest_header_view_get_folder (MODEST_HEADER_VIEW (header_view));
                        if (folder) {
-                               dimmed = (tny_folder_get_all_count (TNY_FOLDER (folder)) == 0);
+                               dimmed = (tny_folder_get_all_count (TNY_FOLDER (folder)) == 0) ||
+                                       modest_header_view_is_empty (MODEST_HEADER_VIEW (header_view));
 
                                if (!dimmed &&
                                    (tny_folder_get_folder_type (TNY_FOLDER (folder)) == TNY_FOLDER_TYPE_OUTBOX)) {
@@ -618,11 +622,6 @@ modest_ui_dimming_rules_on_rename_folder (ModestWindow *win, gpointer user_data)
                if (dimmed)
                        modest_dimming_rule_set_notification (rule, "");
        }
-       if (!dimmed) {
-               dimmed = _transfer_mode_enabled (win);
-               if (dimmed)
-                       modest_dimming_rule_set_notification (rule, "");
-       }
 
        return dimmed;
 }
@@ -932,19 +931,24 @@ modest_ui_dimming_rules_on_details (ModestWindow *win, gpointer user_data)
                if (MODEST_IS_MSG_VIEW_WINDOW (win))
                        dimmed = _msg_download_in_progress (win);
                if (dimmed)
-                       modest_dimming_rule_set_notification (rule, "");                
-               if (!dimmed && MODEST_IS_MSG_VIEW_WINDOW (win)) {
-                       if (!modest_msg_view_window_is_search_result (MODEST_MSG_VIEW_WINDOW(win))) {
-                               dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
-                       }
-                       if (dimmed)
-                               modest_dimming_rule_set_notification (rule, "");
-               }
+                       modest_dimming_rule_set_notification (rule, "");
        }
 
        return dimmed;
 }
 
+gboolean
+modest_ui_dimming_rules_on_fetch_images (ModestWindow *win, gpointer user_data)
+{
+       gboolean dimmed = FALSE;
+
+       g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), FALSE);
+
+       dimmed = !modest_msg_view_window_has_blocked_external_images (MODEST_MSG_VIEW_WINDOW (win));
+
+       return dimmed;
+}
+
 
 gboolean 
 modest_ui_dimming_rules_on_mark_as_read_msg_in_view (ModestWindow *win, gpointer user_data)
@@ -1106,11 +1110,35 @@ modest_ui_dimming_rules_on_main_window_move_to (ModestWindow *win, gpointer user
        folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
                                                           MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
 
-       
+       if (folder_view) {
+               TnyFolderStore *selected = modest_folder_view_get_selected ((ModestFolderView *)folder_view);
+               if (selected) {
+                       TnyAccount *account = NULL;
+
+                       if (TNY_IS_ACCOUNT (selected)) {
+                               account = g_object_ref (selected);
+                       } else if (!TNY_IS_MERGE_FOLDER (selected)){
+                               account = tny_folder_get_account (TNY_FOLDER (selected));
+                       }
+                       if (account) {
+                               ModestProtocolType protocol_type;
+
+                               protocol_type = modest_tny_account_get_protocol_type (account);
+                               dimmed  = modest_protocol_registry_protocol_type_has_tag
+                                       (modest_runtime_get_protocol_registry (),
+                                        protocol_type,
+                                        MODEST_PROTOCOL_REGISTRY_STORE_FORBID_OUTGOING_XFERS);
+
+                               g_object_unref (account);
+                       }
+                       g_object_unref (selected);
+               }
+       }
+
        /* Check diming rules for folders transfer  */
-       if (folder_view && gtk_widget_is_focus (folder_view)) {
+       if (!dimmed && folder_view && gtk_widget_is_focus (folder_view)) {
                TnyFolderType types[5];
-               
+
                types[0] = TNY_FOLDER_TYPE_DRAFTS; 
                types[1] = TNY_FOLDER_TYPE_OUTBOX;
                types[2] = TNY_FOLDER_TYPE_SENT;
@@ -1141,14 +1169,40 @@ modest_ui_dimming_rules_on_main_window_move_to (ModestWindow *win, gpointer user
        if (!dimmed) {
                if (!(folder_view && gtk_widget_is_focus (folder_view)))
                        dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
-               
+
        }
        if (!dimmed) {
                dimmed = _selected_msg_sent_in_progress (win);
                if (dimmed)
                        modest_dimming_rule_set_notification (rule, _("sfil_ib_unable_to_move_selected_items"));
        }
-       
+
+       return dimmed;
+}
+
+static gboolean
+_forbid_outgoing_xfers (ModestWindow *window)
+{
+       const gchar *account_name;
+       TnyAccount *account;
+       gboolean dimmed = FALSE;
+
+       account_name = modest_window_get_active_account (window);
+       account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store (),
+                                                              account_name,
+                                                              TNY_ACCOUNT_TYPE_STORE);
+
+       if (account) {
+               ModestProtocolType protocol_type;
+
+               protocol_type = modest_tny_account_get_protocol_type (account);
+               dimmed  = modest_protocol_registry_protocol_type_has_tag
+                       (modest_runtime_get_protocol_registry (),
+                        protocol_type,
+                        MODEST_PROTOCOL_REGISTRY_STORE_FORBID_OUTGOING_XFERS);
+
+               g_object_unref (account);
+       }
        return dimmed;
 }
 
@@ -1172,19 +1226,25 @@ modest_ui_dimming_rules_on_view_window_move_to (ModestWindow *win, gpointer user
        }
 
        /* Check dimmed rule */
-
        dimmed = _transfer_mode_enabled (win);
        if (dimmed)
-               modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));     
+               modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
+
+       if (!dimmed)
+               dimmed = _forbid_outgoing_xfers (win);
+
        if (!dimmed) {
-               dimmed = modest_window_get_dimming_state (win)->any_marked_as_deleted;
-               if (dimmed) {
-                       gchar *msg = modest_ui_actions_get_msg_already_deleted_error_msg (win);
-                       modest_dimming_rule_set_notification (rule, msg);
-                       g_free (msg);
+               const DimmedState *state = modest_window_get_dimming_state (win);
+               if (state) {
+                       dimmed = state->any_marked_as_deleted;
+                       if (dimmed) {
+                               gchar *msg = modest_ui_actions_get_msg_already_deleted_error_msg (win);
+                               modest_dimming_rule_set_notification (rule, msg);
+                               g_free (msg);
+                       }
                }
        }
-       
+
        if (!dimmed) {
                dimmed = _selected_msg_sent_in_progress (win);
                if (dimmed)
@@ -1206,7 +1266,6 @@ modest_ui_dimming_rules_on_view_window_move_to (ModestWindow *win, gpointer user
                                dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
                        }
                }
-               
                if (dimmed) 
                        modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_item"));
        }
@@ -1214,11 +1273,8 @@ modest_ui_dimming_rules_on_view_window_move_to (ModestWindow *win, gpointer user
        return dimmed;
 }
 
-#ifdef MODEST_TOOLKIT_HILDON2
-#endif
-
 gboolean 
-modest_ui_dimming_rules_on_find_msg (ModestWindow *win, gpointer user_data)
+modest_ui_dimming_rules_on_find_in_msg (ModestWindow *win, gpointer user_data)
 {
        ModestDimmingRule *rule = NULL;
        gboolean dimmed = FALSE;
@@ -1438,7 +1494,7 @@ modest_ui_dimming_rules_on_save_attachments (ModestWindow *win, gpointer user_da
        dimmed = _invalid_attach_selected (win, FALSE, FALSE, FALSE, rule);
 
        if (!dimmed) {
-               dimmed = _purged_attach_selected (win, TRUE);
+               dimmed = _purged_attach_selected (win, TRUE, NULL);
                if (dimmed) {
                        modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
                }
@@ -1469,25 +1525,6 @@ modest_ui_dimming_rules_on_remove_attachments (ModestWindow *win, gpointer user_
                dimmed = _invalid_attach_selected (win, FALSE, FALSE, TRUE, NULL);
                if (dimmed)
                        modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
-
-               if (!dimmed) {
-                       TnyList *attachments;
-                       TnyIterator *iter;
-                       attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW (win));
-                       if (attachments) {
-                               iter = tny_list_create_iterator (attachments);
-                               while (!tny_iterator_is_done (iter) && !dimmed) {
-                                       TnyMimePart *mime_part = (TnyMimePart *)
-                                               tny_iterator_get_current (iter);
-                                       if (modest_tny_mime_part_is_msg (mime_part))
-                                               dimmed = TRUE;
-                                       g_object_unref (mime_part);
-                                       tny_iterator_next (iter);
-                               }
-                               g_object_unref (iter);
-                               g_object_unref (attachments);
-                       }
-               }
        }
 
        if (!dimmed) {
@@ -1910,20 +1947,52 @@ modest_ui_dimming_rules_on_view_next (ModestWindow *win, gpointer user_data)
        if (!dimmed) {
                dimmed = modest_msg_view_window_last_message_selected (MODEST_MSG_VIEW_WINDOW (win));
                modest_dimming_rule_set_notification (rule, NULL);
-       }               
+       }
 
        return dimmed;
 }
 
 
-gboolean 
+gboolean
 modest_ui_dimming_rules_on_tools_smtp_servers (ModestWindow *win, gpointer user_data)
 {
-       return !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
-                                               TRUE);  
+       gboolean dimmed;
+       ModestAccountMgr *mgr;
+
+       mgr = modest_runtime_get_account_mgr();
+       dimmed = !modest_account_mgr_has_accounts(mgr, TRUE);
+
+       /* Dimm it if we only have metaaccounts */
+       if (!dimmed) {
+               ModestProtocolRegistry *reg = modest_runtime_get_protocol_registry ();
+               GSList *account_names = modest_account_mgr_account_names (mgr, TRUE);
+
+               if (account_names) {
+                       ModestProtocolType store_protocol;
+                       gboolean found = FALSE;
+                       GSList *iter = account_names;
+                       const gchar *tag = MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS;
+
+                       while (iter && !found) {
+                               gchar* account_name;
+
+                               account_name = (gchar *) iter->data;
+                               store_protocol = modest_account_mgr_get_store_protocol (mgr, account_name);
+
+                               if (!modest_protocol_registry_protocol_type_has_tag (reg, store_protocol, tag))
+                                       found = TRUE;
+                               else
+                                       iter = g_slist_next (iter);
+                       }
+                       modest_account_mgr_free_account_names (account_names);
+                       dimmed = !found;
+               }
+       }
+
+       return dimmed;
 }
 
-gboolean 
+gboolean
 modest_ui_dimming_rules_on_cancel_sending (ModestWindow *win, gpointer user_data)
 {
        ModestDimmingRule *rule = NULL;
@@ -2548,7 +2617,7 @@ _invalid_clipboard_selected (ModestWindow *win,
                result = !has_selection;
        } else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
                if (focused) {
-                       MODEST_DEBUG_BLOCK (g_message ("FOCUSED %s", g_type_name (G_TYPE_FROM_INSTANCE (focused))););
+                       MODEST_DEBUG_BLOCK (g_debug ("FOCUSED %s", g_type_name (G_TYPE_FROM_INSTANCE (focused))););
                        if (GTK_IS_LABEL (focused) && 
                            !gtk_label_get_selection_bounds (GTK_LABEL (focused), NULL, NULL)) {
                                result = TRUE;
@@ -3087,6 +3156,9 @@ modest_ui_dimming_rules_on_header_window_move_to (ModestWindow *win, gpointer us
        if (dimmed)
                modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
 
+       if (!dimmed)
+               dimmed = _forbid_outgoing_xfers (win);
+
        if (!dimmed) {
                GtkWidget *header_view;
                TnyFolder *folder;
@@ -3094,7 +3166,8 @@ modest_ui_dimming_rules_on_header_window_move_to (ModestWindow *win, gpointer us
                header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (win)));
                folder = modest_header_view_get_folder (MODEST_HEADER_VIEW (header_view));
                if (folder) {
-                       dimmed = (tny_folder_get_all_count (TNY_FOLDER (folder)) == 0);
+                       dimmed = (tny_folder_get_all_count (TNY_FOLDER (folder)) == 0) ||
+                               modest_header_view_is_empty (MODEST_HEADER_VIEW (header_view));
 
                        if (!dimmed &&
                            (tny_folder_get_folder_type (TNY_FOLDER (folder)) == TNY_FOLDER_TYPE_OUTBOX)) {
@@ -3122,6 +3195,9 @@ modest_ui_dimming_rules_on_folder_window_move_to (ModestWindow *win, gpointer us
        if (dimmed)
                modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
 
+       if (!dimmed)
+               dimmed = _forbid_outgoing_xfers (win);
+
        if (MODEST_IS_FOLDER_WINDOW (win)) {
                ModestFolderView *folder_view;
                folder_view = modest_folder_window_get_folder_view (MODEST_FOLDER_WINDOW (win));
@@ -3129,6 +3205,12 @@ modest_ui_dimming_rules_on_folder_window_move_to (ModestWindow *win, gpointer us
                                                                       MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE);
        }
 
+       if (!dimmed) {
+               dimmed = _transfer_mode_enabled (win);
+               if (dimmed)
+                       modest_dimming_rule_set_notification (rule, "");
+       }
+
        return dimmed;
 }
 
@@ -3154,6 +3236,12 @@ modest_ui_dimming_rules_on_folder_window_delete (ModestWindow *win, gpointer use
                                                                       MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE);
        }
 
+       if (!dimmed) {
+               dimmed = _transfer_mode_enabled (win);
+               if (dimmed)
+                       modest_dimming_rule_set_notification (rule, "");
+       }
+
        return dimmed;
 }