If, on retrieving bs message, body is not available, mark the message as
[modest] / src / hildon2 / modest-address-book.c
index 285cd2e..993e091 100644 (file)
@@ -51,7 +51,6 @@
 #include <gtk/gtktreeselection.h>
 #include <gtk/gtkentry.h>
 #include <modest-maemo-utils.h>
-#include <dbus_api/asdbus.h>
 
 static OssoABookContactModel *contact_model =  NULL;
 static EBook *book = NULL;
@@ -65,6 +64,7 @@ static gchar *run_add_email_addr_to_contact_dlg(const gchar * contact_name, gboo
 static GSList *select_email_addrs_for_contact(GList * email_addr_list);
 static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, GSList **contact_id, gboolean *canceled);
 static gchar *unquote_string (const gchar *str);
+static void set_contact_from_display_name (EContact *contact, const gchar *display_name);
 
 static gboolean
 open_addressbook ()
@@ -599,7 +599,7 @@ async_get_contacts_cb (EBook *book,
                        if (display_address) {
                                modest_text_utils_get_display_address (display_address);
                                if ((display_address[0] != '\0') && (strlen (display_address) != strlen (address)))
-                                       e_contact_set (contact, E_CONTACT_FULL_NAME, (const gpointer)display_address);
+                                       set_contact_from_display_name (contact, (const gchar *) display_address);
                                g_free (display_address);
                        }
 
@@ -920,7 +920,6 @@ get_contacts_for_name (const gchar *name)
        unquoted = unquote_string (name);
 
        i = 0;
-       queries[i++] = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
        queries[i++] = e_book_query_field_test (E_CONTACT_GIVEN_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
        queries[i++] = e_book_query_field_test (E_CONTACT_FAMILY_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
        queries[i++] = e_book_query_field_test (E_CONTACT_NICKNAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
@@ -953,9 +952,52 @@ get_contacts_for_name (const gchar *name)
        return result;
 }
 
+static void
+set_contact_from_display_name (EContact *contact, const gchar *disp_name)
+{
+       const gchar *parent_open;
+       const gchar *comma_separator;
+       GString *buffer;
+       gchar *display_name;
+
+       display_name = unquote_string (disp_name);
+       buffer = g_string_new ("");
+
+       /* First we remove part in () */
+       parent_open = g_strstr_len (display_name, -1, "(");
+       if (parent_open) {
+               const gchar *parent_close;
+
+               parent_close = g_strstr_len (parent_open, -1, ")");
+
+               buffer = g_string_append_len (buffer, display_name, parent_open - display_name);
+               if (parent_close) {
+                       buffer = g_string_append (buffer, parent_close + 1);
+               }
+       } else {
+               buffer = g_string_append (buffer, display_name);
+       }
+
+       comma_separator = g_strstr_len (buffer->str, -1, ", ");
+       if (comma_separator) {
+               gchar *surname, *name;
+               surname = g_strndup (buffer->str, comma_separator - buffer->str);
+               name = g_strdup (comma_separator + 2);
+
+               e_contact_set (contact, E_CONTACT_FAMILY_NAME, (const gpointer) surname);
+               e_contact_set (contact, E_CONTACT_GIVEN_NAME, (const gpointer) name);
+               g_free (name);
+               g_free (surname);
+       } else {
+               e_contact_set (contact, E_CONTACT_GIVEN_NAME, (const gpointer) buffer->str);
+       }
+
+       g_string_free (buffer, TRUE);
+       g_free (display_name);
+}
 
 static GList *
-select_contacts_for_name_dialog (const gchar *name, GList *external_contacts)
+select_contacts_for_name_dialog (const gchar *name)
 {
        EBookQuery *book_query = NULL;
        EBookView *book_view = NULL;
@@ -989,36 +1031,6 @@ select_contacts_for_name_dialog (const gchar *name, GList *external_contacts)
                                                                                   _AB("addr_ti_dia_select_contacts"),
                                                                                   OSSO_ABOOK_CAPS_EMAIL,
                                                                                   OSSO_ABOOK_CONTACT_ORDER_NAME);
-
-               if (external_contacts) {
-
-                       GList *row_list = NULL;
-                       while (external_contacts) {
-
-                               AsDbusRecipient *recipient = (AsDbusRecipient*)external_contacts->data;
-                               external_contacts = g_list_next (external_contacts);
-                               if (!recipient)
-                                       continue;
-
-                               char *uid = osso_abook_create_temporary_uid ();
-                               OssoABookContact *contact = osso_abook_contact_new ();
-                               osso_abook_contact_set_uid (contact, uid);
-                               e_contact_set (E_CONTACT (contact), E_CONTACT_FULL_NAME, recipient->display_name);
-                               osso_abook_contact_set_value (E_CONTACT (contact), EVC_EMAIL, recipient->email_address);
-
-                               OssoABookListStoreRow *row = osso_abook_list_store_row_new (contact);
-                               row_list = g_list_prepend (row_list, row);
-                               /* FIXME: unref row? */
-
-                               g_free (uid);
-                       }
-
-                       if (row_list) {
-                               osso_abook_list_store_merge_rows (OSSO_ABOOK_LIST_STORE (contact_model), row_list);
-                               g_list_free (row_list);
-                       }
-               }
-
                /* Enable multiselection */
                osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
                                                                  G_MAXUINT);
@@ -1043,7 +1055,7 @@ resolve_address (const gchar *address,
                 gboolean *canceled)
 {
        GList *resolved_contacts;
-       CheckNamesInfo *info;
+       CheckNamesInfo *info;;
 
        g_return_val_if_fail (canceled, FALSE);
 
@@ -1065,103 +1077,20 @@ resolve_address (const gchar *address,
        }
 
        resolved_contacts = get_contacts_for_name (address);
-       GList *external_contacts = asdbus_resolve_recipients (address);
        hide_check_names_banner (info);
 
-       if (NULL == resolved_contacts && NULL == external_contacts) {
+       if (resolved_contacts == NULL) {
                /* no matching contacts for the search string */
                modest_platform_run_information_dialog (NULL, _("mcen_nc_no_matching_contacts"), FALSE);
                clean_check_names_banner (info);
                return FALSE;
        }
 
-       /* check for duplicate emails and remove from external_contacts if any */
-       if (resolved_contacts && external_contacts) {
-
-               GList *node, *ex_node;
-
-               for (ex_node = external_contacts; ex_node != NULL; ex_node = g_list_next (ex_node)) {
-
-                       AsDbusRecipient *recipient = (AsDbusRecipient*)ex_node->data;
-                       if (!recipient)
-                               continue;
-
-                       for (node = resolved_contacts; node != NULL; node = g_list_next (node)) {
-
-                               EContact *contact = (EContact*)node->data;
-                               GList *emails = e_contact_get (contact, E_CONTACT_EMAIL);
-                               if (!emails)
-                                       continue;
-
-                               if (g_list_find_custom (emails, recipient->email_address, (GCompareFunc) compare_addresses)) {
-
-                                       g_free (recipient->display_name);
-                                       g_free (recipient->email_address);
-                                       g_free (recipient);
-                                       recipient = NULL;
-                                       ex_node->data = NULL;
-                               }
-
-                               g_list_foreach (emails, (GFunc) g_free, NULL);
-                               g_list_free (emails);
-                       }
-               }
-       }
-
-       if (g_list_length (resolved_contacts) + g_list_length (external_contacts) > 1) {
+       if (g_list_length (resolved_contacts) > 1) {
                /* show a dialog to select the contact from the resolved ones */
                g_list_free (resolved_contacts);
 
-               resolved_contacts = select_contacts_for_name_dialog (address, external_contacts);
-
-               if (external_contacts) {
-
-                       GList *node;
-                       for (node = external_contacts; node != NULL; node = g_list_next (node)) {
-
-                               AsDbusRecipient *recipient = (AsDbusRecipient*)node->data;
-                               if (!recipient)
-                                       continue;
-
-                               g_free (recipient->display_name);
-                               g_free (recipient->email_address);
-                               g_free (recipient);
-                       }
-
-                       g_list_free (external_contacts);
-                       external_contacts = NULL;
-               }
-       }
-
-       if (external_contacts) {
-
-               gboolean found = FALSE;
-               GList *node;
-               for (node = external_contacts; node != NULL; node = g_list_next (node)) {
-
-                       AsDbusRecipient *recipient = (AsDbusRecipient*)node->data;
-                       if (!recipient)
-                               continue;
-
-                       GString *formatted_recipient = g_string_new (NULL);
-                       g_string_printf (formatted_recipient, "\"%s\" <%s>", recipient->display_name, recipient->email_address);
-
-                       /* FIXME: why we have to have list of lists? */
-                       GSList *formattedlist = g_slist_append(NULL, formatted_recipient->str);
-                       *resolved_addresses = g_slist_append (*resolved_addresses, formattedlist);
-                       /* FIXME: how important is an UID? */
-                       *contact_ids = g_slist_append (*contact_ids, g_strdup ("temp-uid"));
-                       found = TRUE;
-
-                       g_string_free (formatted_recipient, FALSE); /* character data segment is NOT freed */
-                       g_free (recipient->display_name);
-                       g_free (recipient->email_address);
-                       g_free (recipient);
-               }
-
-               g_list_free (external_contacts);
-               external_contacts = NULL;
-               return found;
+               resolved_contacts = select_contacts_for_name_dialog (address);
        }
 
        /* get the resolved contacts (can be no contact) */