Look for contacts using OssoABook API
[modest] / src / hildon2 / modest-address-book.c
index 01c6ea4..b26b80d 100644 (file)
@@ -64,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 ()
@@ -598,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);
                        }
 
@@ -766,6 +767,13 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
                end_ptr = g_utf8_offset_to_pointer (recipients, end_pos);
 
                address = g_strstrip (g_strndup (start_ptr, end_ptr - start_ptr));
+
+               /* Ignore empty addresses */
+               if (!g_strcmp0 (address, "")) {
+                       g_free (address);
+                       goto next_address;
+               }
+
                gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_pos);
                gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, end_pos);
                gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
@@ -862,6 +870,7 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
                if (result == FALSE)
                        break;
 
+       next_address:
                current_start = g_slist_next (current_start);
                current_end = g_slist_next (current_end);
        }
@@ -884,75 +893,52 @@ modest_address_book_check_names (ModestRecptEditor *recpt_editor,
 
 }
 
-typedef struct _GetContactsInfo {
-       GMainLoop *mainloop;
-       GList *result;
-} GetContactsInfo;
 
-static void 
-get_contacts_for_name_cb (EBook *book, 
-                         EBookStatus status, 
-                         GList *list, 
-                         gpointer userdata)
-{
-       GetContactsInfo *info = (GetContactsInfo *) userdata;
 
-       if (status == E_BOOK_ERROR_OK)
-               info->result = list;
-
-       g_main_loop_quit (info->mainloop);
-}
-
-static GList *
-get_contacts_for_name (const gchar *name)
+static void
+set_contact_from_display_name (EContact *contact, const gchar *disp_name)
 {
-       EBookQuery *book_query = NULL;
-       GList *result;
-       gchar *unquoted;
-       GetContactsInfo *info;
-       EBookQuery *queries[10];
-       gint i;
+       const gchar *parent_open;
+       const gchar *comma_separator;
+       GString *buffer;
+       gchar *display_name;
 
-       if (name == NULL)
-               return NULL;
+       display_name = unquote_string (disp_name);
+       buffer = g_string_new ("");
 
-       unquoted = unquote_string (name);
+       /* First we remove part in () */
+       parent_open = g_strstr_len (display_name, -1, "(");
+       if (parent_open) {
+               const gchar *parent_close;
 
-       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);
-       if (strchr (name, '@')) {
-               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL_1, E_BOOK_QUERY_BEGINS_WITH, unquoted);
-               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL_2, E_BOOK_QUERY_BEGINS_WITH, unquoted);
-               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL_3, E_BOOK_QUERY_BEGINS_WITH, unquoted);
-               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL_4, E_BOOK_QUERY_BEGINS_WITH, unquoted);
-               queries[i++] = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_BEGINS_WITH, unquoted);
+               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);
        }
-       queries[i] = e_book_query_field_test (E_CONTACT_NAME, E_BOOK_QUERY_BEGINS_WITH, unquoted);
-       book_query = e_book_query_or (i, queries, TRUE);
 
-       g_free (unquoted);
+       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);
 
-       /* TODO: Make it launch a mainloop */
-       info = g_slice_new (GetContactsInfo);
-       info->mainloop = g_main_loop_new (NULL, FALSE);
-       info->result = NULL;
-       if (e_book_async_get_contacts (book, book_query, get_contacts_for_name_cb, info) == 0) {
-               GDK_THREADS_LEAVE ();
-               g_main_loop_run (info->mainloop);
-               GDK_THREADS_ENTER ();
-       } 
-       result = info->result;
-       e_book_query_unref (book_query);
-       g_main_loop_unref (info->mainloop);
-       g_slice_free (GetContactsInfo, info);
+               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);
+       }
 
-       return result;
+       g_string_free (buffer, TRUE);
+       g_free (display_name);
 }
 
-
 static GList *
 select_contacts_for_name_dialog (const gchar *name)
 {
@@ -1006,6 +992,55 @@ select_contacts_for_name_dialog (const gchar *name)
 }
 
 static gboolean
+contact_name_or_email_starts_with (OssoABookContact *contact,
+                                   gpointer          user_data)
+{
+       const char *prefix = user_data;
+       gboolean rv = FALSE;
+       GList *contacts, *l;
+
+
+       contacts = osso_abook_contact_get_roster_contacts (contact);
+       contacts = g_list_prepend (contacts, contact);
+
+       for (l = contacts; l; l = l->next) {
+               GList *attrs;
+               attrs = e_vcard_get_attributes (E_VCARD (l->data));
+               for (;attrs;attrs = attrs->next) {
+                       EVCardAttribute *attr = attrs->data;
+                       const char *name;
+
+                       name = e_vcard_attribute_get_name (attr);
+                       if (!g_strcmp0 (name, "N") ||
+                           (strchr (prefix, '@') && !g_strcmp0 (name, "EMAIL"))) {
+                               GList *values = e_vcard_attribute_get_values (attr);
+                               gchar *prefix_down = g_utf8_strdown (prefix, -1);
+
+                               for (;values; values = values->next) {
+                                       gchar *value_down = NULL;
+
+                                       if (g_strcmp0 (values->data, ""))
+                                               value_down = g_utf8_strdown (values->data, -1);
+
+                                       if (value_down && g_str_has_prefix (value_down, prefix_down)) {
+                                               rv = TRUE;
+                                               g_free (value_down);
+                                               g_free (prefix_down);
+                                               goto out;
+                                       }
+                                       g_free (value_down);
+                               }
+                               g_free (prefix_down);
+                       }
+               }
+       }
+ out:
+       g_list_free (contacts);
+
+       return rv;
+}
+
+static gboolean
 resolve_address (const gchar *address, 
                 GSList **resolved_addresses, 
                 GSList **contact_ids,
@@ -1013,6 +1048,7 @@ resolve_address (const gchar *address,
 {
        GList *resolved_contacts;
        CheckNamesInfo *info;;
+       OssoABookRoster *roster;
 
        g_return_val_if_fail (canceled, FALSE);
 
@@ -1033,7 +1069,11 @@ resolve_address (const gchar *address,
                return FALSE;
        }
 
-       resolved_contacts = get_contacts_for_name (address);
+       roster = osso_abook_aggregator_get_default (NULL);
+       resolved_contacts =
+               osso_abook_aggregator_find_contacts_full ((OssoABookAggregator *) roster,
+                                                         contact_name_or_email_starts_with,
+                                                         (gpointer) address);
        hide_check_names_banner (info);
 
        if (resolved_contacts == NULL) {