static gboolean resolve_address (const gchar *address, GSList **resolved_addresses, GSList **contact_id, gboolean *canceled);
static gchar *unquote_string (const gchar *str);
-
-static void
-unref_gobject (GObject *obj)
-{
- if (obj)
- g_object_unref (obj);
-}
-
-
-static void
-get_book_view_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
-{
- if (status != E_BOOK_ERROR_OK) {
- g_object_unref (book);
- book = NULL;
- return;
- }
- book_view = bookview;
-
- if (contact_model)
- osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model),
- book_view);
-
- e_book_view_start (book_view);
-}
-
-static void
-book_open_cb (EBook *view, EBookStatus status, gpointer data)
-{
- EBookQuery *query = NULL;
-
- if (status != E_BOOK_ERROR_OK) {
- g_object_unref (book);
- book = NULL;
- return;
- }
- query = e_book_query_any_field_contains ("");
- e_book_async_get_book_view (book, query, NULL, -1, get_book_view_cb, NULL);
- e_book_query_unref (query);
-}
-
-static gboolean
-open_addressbook ()
-{
- book = e_book_new_system_addressbook (NULL);
- if (!book)
- return FALSE;
-
- if (e_book_async_open (book, FALSE, book_open_cb, NULL) != E_BOOK_ERROR_OK)
- return FALSE;
-
- return TRUE; /* FIXME */
-}
-
-typedef struct _OpenAddressbookSyncInfo {
- gboolean retval;
- GMainLoop *mainloop;
-} OpenAddressbookSyncInfo;
-
-static void
-get_book_view_sync_cb (EBook *book, EBookStatus status, EBookView *bookview, gpointer data)
-{
- OpenAddressbookSyncInfo *info = (OpenAddressbookSyncInfo *) data;
-
- if (status != E_BOOK_ERROR_OK) {
- g_object_unref (book);
- book = NULL;
- info->retval = FALSE;
- g_main_loop_quit (info->mainloop);
- return;
- }
- book_view = bookview;
-
- if (contact_model)
- osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model),
- book_view);
-
- e_book_view_start (book_view);
- info->retval = TRUE;
- g_main_loop_quit (info->mainloop);
-}
-
-static void
-book_open_sync_cb (EBook *view, EBookStatus status, gpointer data)
-{
- EBookQuery *query = NULL;
- OpenAddressbookSyncInfo *info = (OpenAddressbookSyncInfo *) data;
-
- if (status != E_BOOK_ERROR_OK) {
- g_object_unref (book);
- book = NULL;
- info->retval = FALSE;
- g_main_loop_quit (info->mainloop);
- return;
- }
- query = e_book_query_any_field_contains ("");
- e_book_async_get_book_view (book, query, NULL, -1, get_book_view_sync_cb, info);
- e_book_query_unref (query);
-}
-
static gboolean
-open_addressbook_sync ()
+open_addressbook ()
{
- OpenAddressbookSyncInfo *info;
- gboolean retval;
-
- book = e_book_new_system_addressbook (NULL);
- if (!book)
- return FALSE;
-
- info = g_slice_new (OpenAddressbookSyncInfo);
- info->mainloop = g_main_loop_new (NULL, FALSE);
- info->retval = FALSE;
- if (e_book_async_open (book, FALSE, book_open_sync_cb, info) == E_BOOK_ERROR_OK) {
- GDK_THREADS_LEAVE ();
- g_main_loop_run (info->mainloop);
- GDK_THREADS_ENTER ();
- }
- retval = info->retval;
- g_main_loop_unref (info->mainloop);
- g_slice_free (OpenAddressbookSyncInfo, info);
- /* Make it launch a main loop */
- return e_book_open (book, FALSE, NULL);
+ OssoABookRoster *roster;
+ GError *error = NULL;
+ time_t init,end;
+
+ if (book && book_view)
+ return TRUE;
+
+ roster = osso_abook_aggregator_get_default (&error);
+ if (error)
+ goto error;
+
+ /* Wait until it's ready */
+ init = time (NULL);
+ osso_abook_waitable_run ((OssoABookWaitable *) roster,
+ g_main_context_default (),
+ &error);
+ end = time (NULL);
+ g_debug ("Opening addressbook lasted %ld seconds", (gint) end-init);
+
+ if (error)
+ goto error;
+
+ if (!osso_abook_waitable_is_ready ((OssoABookWaitable *) roster,
+ &error))
+ goto error;
+
+ book = osso_abook_roster_get_book (roster);
+ book_view = osso_abook_roster_get_book_view (roster);
+
+ return TRUE;
+ error:
+ g_warning ("error opening addressbook %s", error->message);
+ g_error_free (error);
+ return FALSE;
}
void
emailid = get_email_addr_from_user(display_name, canceled);
if (emailid) {
- e_contact_set(contact, E_CONTACT_EMAIL_1, emailid);
+ e_contact_set(E_CONTACT (abook_contact), E_CONTACT_EMAIL_1, emailid);
osso_abook_contact_commit (abook_contact, FALSE, NULL, NULL);
}
g_object_unref (abook_contact);
g_slist_foreach (addresses, (GFunc) g_free, NULL);
g_slist_free (addresses);
}
- if (contacts) {
- g_list_foreach (contacts, (GFunc) g_object_unref, NULL);
+ if (contacts)
g_list_free (contacts);
- }
}
return result;
}
+static gboolean
+filter_by_name (OssoABookContactChooser *chooser,
+ OssoABookContact *contact,
+ gpointer user_data)
+{
+ const gchar *contact_name;
+ const gchar *name = (const gchar *) user_data;
+
+ contact_name = osso_abook_contact_get_name (contact);
+ /* contact_name includes both name and surname */
+ if (contact_name && name && strstr (contact_name, name))
+ return TRUE;
+ else
+ return FALSE;
+}
+
static GList *
select_contacts_for_name_dialog (const gchar *name)
{
- EBookQuery *full_name_book_query = NULL;
- EBookView *book_view = NULL;
+ GtkWidget *contact_view;
+ OssoABookContactChooser *contact_dialog;
GList *result = NULL;
gchar *unquoted;
+ contact_dialog = (OssoABookContactChooser *)
+ osso_abook_contact_chooser_new_with_capabilities (NULL,
+ _AB("addr_ti_dia_select_contacts"),
+ OSSO_ABOOK_CAPS_EMAIL,
+ OSSO_ABOOK_CONTACT_ORDER_NAME);
+
+ /* Enable multiselection */
+ osso_abook_contact_chooser_set_maximum_selection (contact_dialog, G_MAXUINT);
+
+ /* Set up the filtering */
unquoted = unquote_string (name);
- full_name_book_query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, unquoted);
- g_free (unquoted);
- e_book_get_book_view (book, full_name_book_query, NULL, -1, &book_view, NULL);
- e_book_query_unref (full_name_book_query);
+ contact_view = osso_abook_contact_chooser_get_contact_view (contact_dialog);
+ osso_abook_contact_chooser_set_model (contact_dialog, contact_model);
+ osso_abook_contact_chooser_set_visible_func (contact_dialog, filter_by_name, unquoted, NULL);
- if (book_view) {
- GtkWidget *contact_dialog = NULL;
- osso_abook_list_store_set_book_view (OSSO_ABOOK_LIST_STORE (contact_model), book_view);
- e_book_view_start (book_view);
-
- /* TODO: figure out how to make the contact chooser modal */
- contact_dialog = osso_abook_contact_chooser_new_with_capabilities (NULL,
- _AB("addr_ti_dia_select_contacts"),
- OSSO_ABOOK_CAPS_EMAIL,
- OSSO_ABOOK_CONTACT_ORDER_NAME);
- /* Enable multiselection */
- osso_abook_contact_chooser_set_maximum_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
- G_MAXUINT);
- osso_abook_contact_chooser_set_model (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog),
- contact_model);
-
- if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK)
- result = osso_abook_contact_chooser_get_selection (OSSO_ABOOK_CONTACT_CHOOSER (contact_dialog));
- e_book_view_stop (book_view);
- g_object_unref (book_view);
- gtk_widget_destroy (contact_dialog);
- }
+ if (gtk_dialog_run (GTK_DIALOG (contact_dialog)) == GTK_RESPONSE_OK)
+ result = osso_abook_contact_chooser_get_selection (contact_dialog);
+
+ g_free (unquoted);
return result;
}
*canceled = FALSE;
info = g_slice_new0 (CheckNamesInfo);
show_check_names_banner (info);
-
- contact_model = osso_abook_contact_model_new ();
- if (!open_addressbook_sync ()) {
+
+ contact_model = osso_abook_contact_model_get_default ();
+ if (!open_addressbook ()) {
hide_check_names_banner (info);
if (contact_model) {
g_object_unref (contact_model);
}
}
- g_list_foreach (resolved_contacts, (GFunc)unref_gobject, NULL);
g_list_free (resolved_contacts);
clean_check_names_banner (info);
gboolean
modest_address_book_has_address (const gchar *address)
{
- EBookQuery *query;
GList *contacts = NULL;
GError *err = NULL;
gchar *email;
gboolean result;
+ OssoABookAggregator *roster;
g_return_val_if_fail (address, FALSE);
-
+
if (!book) {
if (!open_addressbook ()) {
g_return_val_if_reached (FALSE);
}
}
-
g_return_val_if_fail (book, FALSE);
email = modest_text_utils_get_email_address (address);
-
- query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, email);
- if (!e_book_get_contacts (book, query, &contacts, &err)) {
- g_printerr ("modest: failed to get contacts: %s",
- err ? err->message : "<unknown>");
+
+ roster = (OssoABookAggregator *) osso_abook_aggregator_get_default (NULL);
+ contacts = osso_abook_aggregator_find_contacts_for_email_address (roster, email);
+ if (!contacts) {
if (err)
g_error_free (err);
g_free (email);
- e_book_query_unref (query);
return FALSE;
}
- e_book_query_unref (query);
- result = (contacts != NULL);
if (contacts) {
- g_list_foreach (contacts, (GFunc)unref_gobject, NULL);
g_list_free (contacts);
+ result = TRUE;
}
-
+
g_free (email);
return result;
else
return NULL;
}
+
+void
+modest_address_book_init (void)
+{
+ open_addressbook ();
+}