Fixed the dimming rule for "add_to_contacts" in the case of ModestMsgEditWindow
authorSergio Villar Senin <svillar@igalia.com>
Thu, 5 Nov 2009 15:29:31 +0000 (16:29 +0100)
committerSergio Villar Senin <svillar@igalia.com>
Mon, 9 Nov 2009 17:46:06 +0000 (18:46 +0100)
Fixes NB#144914 (7/8)

src/hildon2/modest-msg-edit-window.c
src/modest-ui-dimming-rules.c
src/widgets/modest-msg-edit-window.h

index 005efe1..65ffe86 100644 (file)
@@ -3448,6 +3448,9 @@ modest_msg_edit_window_add_to_contacts (ModestMsgEditWindow *self)
        if (!modest_msg_edit_window_check_names (self, FALSE))
                return;
 
+       if (!modest_msg_edit_window_has_pending_addresses (self))
+               return;
+
        /* Don't add the from obviously */
        to  =  g_strdup (modest_recpt_editor_get_recipients ((ModestRecptEditor *) priv->to_field));
        cc  =  g_strdup (modest_recpt_editor_get_recipients ((ModestRecptEditor *) priv->cc_field));
@@ -4467,3 +4470,66 @@ max_chars_banner_unref (ModestMsgEditWindow *self, GObject *old_ref)
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
        priv->max_chars_banner = NULL;
 }
+
+static gboolean
+has_pending_addresses (ModestRecptEditor *recpt_editor)
+{
+       const gchar *recipients = NULL;
+       GSList *start_indexes = NULL, *end_indexes = NULL;
+       GSList *current_start, *current_end;
+       GtkTextBuffer *buffer;
+       gint offset_delta = 0;
+       gint last_length;
+       gboolean has_recipients_to_add = FALSE;
+
+       recipients = modest_recpt_editor_get_recipients (recpt_editor);
+       last_length = g_utf8_strlen (recipients, -1);
+       modest_text_utils_get_addresses_indexes (recipients, &start_indexes, &end_indexes);
+
+       if (!start_indexes)
+               return FALSE;
+
+       current_start = start_indexes;
+       current_end = end_indexes;
+       buffer = modest_recpt_editor_get_buffer (recpt_editor);
+
+       while (current_start && !has_recipients_to_add) {
+               gchar *address;
+               gchar *start_ptr, *end_ptr;
+               gint start_pos, end_pos;
+
+               start_pos = (*((gint*) current_start->data)) + offset_delta;
+               end_pos = (*((gint*) current_end->data)) + offset_delta;
+
+               start_ptr = g_utf8_offset_to_pointer (recipients, start_pos);
+               end_ptr = g_utf8_offset_to_pointer (recipients, end_pos);
+
+               address = g_strstrip (g_strndup (start_ptr, end_ptr - start_ptr));
+
+               if (modest_text_utils_validate_recipient (address, NULL)) {
+                       if (!modest_address_book_has_address (address)) {
+                               has_recipients_to_add = TRUE;
+                       }
+               }
+               current_start = g_slist_next (current_start);
+               current_end = g_slist_next (current_end);
+       }
+       return has_recipients_to_add;
+}
+
+gboolean
+modest_msg_edit_window_has_pending_addresses (ModestMsgEditWindow *self)
+{
+       ModestMsgEditWindowPrivate *priv = NULL;
+
+       g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW(self), FALSE);
+
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
+
+       if (!has_pending_addresses ((ModestRecptEditor *) priv->to_field) &&
+           !has_pending_addresses ((ModestRecptEditor *) priv->cc_field) &&
+           !has_pending_addresses ((ModestRecptEditor *) priv->bcc_field))
+               return FALSE;
+       else
+               return TRUE;
+}
index b15b59e..74367e6 100644 (file)
@@ -2098,19 +2098,8 @@ modest_ui_dimming_rules_on_add_to_contacts (ModestWindow *win, gpointer user_dat
                        g_object_unref (msg);
                }
        } else if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
-               /* Get recipients */
-               gchar *joined, *after_remove;
-               MsgData *data = modest_msg_edit_window_get_msg_data ((ModestMsgEditWindow *) win);
-
-               /* We don't check the from */
-               joined = modest_text_utils_join_addresses (NULL, data->to, data->cc, data->bcc);
-               after_remove = modest_text_utils_remove_duplicate_addresses (joined);
-               g_free (joined);
-
-               recipients = modest_text_utils_split_addresses_list (after_remove);
-               g_free (after_remove);
-
-               modest_msg_edit_window_free_msg_data ((ModestMsgEditWindow *) win, data);
+               /* Check if there are pending addresses to add */
+               return !modest_msg_edit_window_has_pending_addresses ((ModestMsgEditWindow *) win);
        }
 
        has_recipients_to_add = FALSE;
index b60dec9..14c113b 100644 (file)
@@ -396,6 +396,18 @@ gboolean        modest_msg_edit_window_check_names    (ModestMsgEditWindow *wind
                                                       gboolean add_to_addressbook);
 
 /**
+ * modest_msg_edit_window_has_pending_addresses:
+ * @window: a #ModestMsgEditWindow
+ * @add_to_addressbook: if TRUE, add valid addresses to the addressbook
+ *
+ * Validates all the recipients, and checks if there are addresses in
+ * any field that could be added to the addressbook
+ *
+ * Returns: %TRUE if there are valid pending addresses, %FALSE otherwise
+ */
+gboolean        modest_msg_edit_window_has_pending_addresses    (ModestMsgEditWindow *window);
+
+/**
  * modest_msg_edit_window_toggle_find_toolbar:
  * @window: a #ModestMsgEditWindow
  * @show: a #gboolean