static void update_contact_inline (EBookClient *book, const gchar *uid) { EContact *contact = NULL; EContactPhoto *photo; guchar *data; gsize length = 0; GError *error = NULL; if (!e_book_client_get_contact_sync (book, uid, &contact, NULL, &error)) g_error ("Unable to get contact: %s", error->message); g_assert (contact); data = g_base64_decode (photo_data, &length); photo = g_new (EContactPhoto, 1); photo->type = E_CONTACT_PHOTO_TYPE_INLINED; photo->data.inlined.mime_type = NULL; photo->data.inlined.data = data; photo->data.inlined.length = length; /* set the photo */ e_contact_set (contact, E_CONTACT_PHOTO, photo); if (!e_book_client_modify_contact_sync (book, contact, NULL, &error)) g_error ("Failed to modify contact with inline photo data: %s", error->message); }
static void remove_contact_by_uid_cb (GObject *source_object, GAsyncResult *result, gpointer uid) { GError *error = NULL; EContact *contact = NULL; if (!e_book_client_remove_contact_by_uid_finish (E_BOOK_CLIENT (source_object), result, &error)) { report_error ("remove contact by uid finish", &error); stop_main_loop (1); return; } if (!e_book_client_get_contact_sync (E_BOOK_CLIENT (source_object), uid, &contact, NULL, &error) && g_error_matches (error, E_BOOK_CLIENT_ERROR, E_BOOK_CLIENT_ERROR_CONTACT_NOT_FOUND)) { g_clear_error (&error); stop_main_loop (0); } else { report_error ("fail with get contact on removed contact", &error); if (contact) g_object_unref (contact); stop_main_loop (1); } }
static void test_modify_contact_sync (ETestServerFixture *fixture, gconstpointer user_data) { EBookClient *book_client; GError *error = NULL; EContact *contact, *book_contact; book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient); if (!add_contact_from_test_case_verify (book_client, "name-only", &contact)) g_error ("Failed to add contact"); verify_premodify_and_prepare_contact (contact); if (!e_book_client_modify_contact_sync (book_client, contact, NULL, &error)) g_error ("modify contact sync: %s", error->message); if (!e_book_client_get_contact_sync (book_client, e_contact_get_const (contact, E_CONTACT_UID), &book_contact, NULL, &error)) g_error ("get contact sync: %s", error->message); verify_modify (book_contact); g_object_unref (book_contact); g_object_unref (contact); }
static void check_removed_contact (EBookClient *book_client, const gchar *uid) { GError *error = NULL; EContact *contact = NULL; if (e_book_client_get_contact_sync (book_client, uid, &contact, NULL, &error)) g_error ("succeeded to fetch removed contact"); else if (!g_error_matches (error, E_BOOK_CLIENT_ERROR, E_BOOK_CLIENT_ERROR_CONTACT_NOT_FOUND)) g_error ("Wrong error in get contact sync on removed contact: %s (domain: %s, code: %d)", error->message, g_quark_to_string (error->domain), error->code); else g_clear_error (&error); }
static void map_window_show_contact_editor_cb (EContactMapWindow *window, const gchar *contact_uid, gpointer user_data) { EShell *shell = e_shell_get_default (); EBookShellView *book_shell_view = user_data; EBookShellSidebar *book_shell_sidebar; ESource *source; ESourceSelector *selector; EBookClient *book_client; EContact *contact; EABEditor *editor; GError *error = NULL; book_shell_sidebar = book_shell_view->priv->book_shell_sidebar; selector = e_book_shell_sidebar_get_selector (book_shell_sidebar); source = e_source_selector_get_primary_selection (selector); g_return_if_fail (source != NULL); book_client = e_book_client_new (source, &error); if (error) { g_warning ("Error loading addressbook: %s", error->message); g_error_free (error); if (book_client) g_object_unref (book_client); return; } e_book_client_get_contact_sync (book_client, contact_uid, &contact, NULL, &error); if (error) { g_warning ("Error getting contact from addressbook: %s", error->message); g_error_free (error); g_object_unref (book_client); return; } editor = e_contact_editor_new (shell, book_client, contact, FALSE, TRUE); g_signal_connect (editor, "contact-modified", G_CALLBACK (contact_editor_contact_modified_cb), window); g_signal_connect_swapped (editor, "editor-closed", G_CALLBACK (g_object_unref), editor); eab_editor_show (editor); g_object_unref (book_client); }
static void check_removed (EBookClient *book_client, const GSList *uids) { g_return_if_fail (book_client != NULL); g_return_if_fail (uids != NULL); while (uids) { GError *error = NULL; EContact *contact = NULL; if (!e_book_client_get_contact_sync (book_client, uids->data, &contact, NULL, &error) && g_error_matches (error, E_BOOK_CLIENT_ERROR, E_BOOK_CLIENT_ERROR_CONTACT_NOT_FOUND)) { g_clear_error (&error); } else g_error ("fail with get contact on removed contact: %s", error->message); uids = uids->next; } }
/* This assertion is made a couple of times in the view-complete * handler, we run it to ensure that binary blobs and cross-referenced * photo uris exist on disk while they should */ static void assert_uri_exists (EBookClient *book, const gchar *uid) { EContact *contact; EContactPhoto *photo; const gchar *filename; GError *error = NULL; if (!e_book_client_get_contact_sync (book, uid, &contact, NULL, &error)) g_error ("Unable to get contact: %s", error->message); g_assert (contact); photo = e_contact_get (contact, E_CONTACT_PHOTO); g_assert (photo); g_assert (photo->type == E_CONTACT_PHOTO_TYPE_URI); filename = g_filename_from_uri (photo->data.uri, NULL, NULL); g_assert (filename); /* The file should absolutely exist at this point */ g_assert (g_file_test (filename, G_FILE_TEST_EXISTS)); }
gboolean add_contact_verify (EBookClient *book_client, EContact *contact) { EContact *contact_final; gchar *uid; GError *error = NULL; if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) g_error ("add contact sync: %s", error->message); e_contact_set (contact, E_CONTACT_UID, uid); if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) g_error ("get contact sync: %s", error->message); /* verify the contact was added "successfully" (not thorough) */ g_assert (contacts_are_equal_shallow (contact, contact_final)); g_object_unref (contact_final); g_free (uid); return TRUE; }
static gboolean test_bulk_modify (EBookClient *client, const gchar *vcard_str, gint batch_size) { gint i; EContact *contact; GSList *contacts = NULL; GSList *added_uids = NULL; const GSList *l, *ll; const gchar *old_first_name = "xyz mix"; const gchar *new_first_name = "abc mix"; for (i = 0; i < batch_size; ++i) { EContact *contact = e_contact_new_from_vcard (vcard_str); contacts = g_slist_append (contacts, contact); } g_print (" * Bulk addition of %d contacts...\n", batch_size); /* Bulk addition */ g_return_val_if_fail (e_book_client_add_contacts_sync (client, contacts, &added_uids, NULL, NULL), FALSE); g_return_val_if_fail (added_uids != NULL, FALSE); g_return_val_if_fail (added_uids->data != NULL, FALSE); g_return_val_if_fail (g_slist_length (added_uids) == batch_size, FALSE); g_print (" * Bulk modification of %d contacts...\n", batch_size); ll = added_uids; for (l = contacts; l != NULL; l = l->next) { const gchar * uid = ll->data; contact = E_CONTACT (l->data); /* Set contact UID */ e_contact_set (contact, E_CONTACT_UID, uid); /* Change contact first name */ e_contact_set (contact, E_CONTACT_GIVEN_NAME, new_first_name); ll = ll->next; } g_return_val_if_fail (e_book_client_modify_contacts_sync (client, contacts, NULL, NULL), FALSE); /* Validate */ for (ll = added_uids; ll != NULL; ll = ll->next) { const gchar *first_name; const gchar *uid = ll->data; contact = NULL; g_return_val_if_fail (e_book_client_get_contact_sync (client, uid, &contact, NULL, NULL), FALSE); /* Check contact first name */ first_name = e_contact_get_const (contact, E_CONTACT_GIVEN_NAME); g_return_val_if_fail (g_strcmp0 (first_name, new_first_name) == 0, FALSE); g_object_unref (contact); } /* Test failure case */ g_print (" * Bulk modification of %d contacts (expected failure)...\n", batch_size); contact = E_CONTACT (g_slist_nth_data (contacts, g_slist_length (contacts) - 2)); g_return_val_if_fail (e_book_client_remove_contact_sync (client, contact, NULL, NULL), FALSE); for (l = contacts; l != NULL; l = l->next) { contact = E_CONTACT (l->data); /* Change contact first name */ e_contact_set (contact, E_CONTACT_GIVEN_NAME, old_first_name); } g_return_val_if_fail (!e_book_client_modify_contacts_sync (client, contacts, NULL, NULL), FALSE); /* Remove the UID that no longer exists from the added_uid list */ added_uids = g_slist_delete_link (added_uids, g_slist_nth (added_uids, g_slist_length (added_uids) - 2)); /* Validate */ for (ll = added_uids; ll != NULL; ll = ll->next) { const gchar *first_name; const gchar *uid = ll->data; contact = NULL; g_return_val_if_fail (e_book_client_get_contact_sync (client, uid, &contact, NULL, NULL), FALSE); /* Check contact first name */ first_name = e_contact_get_const (contact, E_CONTACT_GIVEN_NAME); g_return_val_if_fail (g_strcmp0 (first_name, new_first_name) == 0, FALSE); g_object_unref (contact); } g_print (" * Bulk removal of %d contacts...\n", batch_size); /* Bulk removal */ g_return_val_if_fail (e_book_client_remove_contacts_sync (client, added_uids, NULL, NULL), FALSE); g_slist_free_full (added_uids, g_free); g_slist_free_full (contacts, g_object_unref); return TRUE; }
static void map_window_show_contact_editor_cb (EContactMapWindow *window, const gchar *contact_uid, gpointer user_data) { EBookShellView *book_shell_view = user_data; EBookShellSidebar *book_shell_sidebar; EShell *shell; EShellView *shell_view; EShellBackend *shell_backend; ESource *source; ESourceSelector *selector; EClient *client; EClientCache *client_cache; EContact *contact; EABEditor *editor; GError *error = NULL; book_shell_sidebar = book_shell_view->priv->book_shell_sidebar; selector = e_book_shell_sidebar_get_selector (book_shell_sidebar); source = e_source_selector_ref_primary_selection (selector); g_return_if_fail (source != NULL); shell_view = E_SHELL_VIEW (book_shell_view); shell_backend = e_shell_view_get_shell_backend (shell_view); shell = e_shell_backend_get_shell (shell_backend); client_cache = e_shell_get_client_cache (shell); /* FIXME This blocks. Needs to be asynchronous. */ client = e_client_cache_get_client_sync ( client_cache, source, E_SOURCE_EXTENSION_ADDRESS_BOOK, (guint32) -1, NULL, &error); g_object_unref (source); /* Sanity check. */ g_return_if_fail ( ((client != NULL) && (error == NULL)) || ((client == NULL) && (error != NULL))); if (error != NULL) { g_warning ("Error loading addressbook: %s", error->message); g_error_free (error); return; } e_book_client_get_contact_sync ( E_BOOK_CLIENT (client), contact_uid, &contact, NULL, &error); if (error != NULL) { g_warning ("Error getting contact from addressbook: %s", error->message); g_error_free (error); g_object_unref (client); return; } editor = e_contact_editor_new ( shell, E_BOOK_CLIENT (client), contact, FALSE, TRUE); g_signal_connect ( editor, "contact-modified", G_CALLBACK (contact_editor_contact_modified_cb), window); eab_editor_show (editor); g_object_unref (client); }
gint main (gint argc, gchar **argv) { EBookClient *book_client; GError *error = NULL; EContact *contact; gchar *uid; main_initialize (); /* * Setup */ book_client = new_temp_client (NULL); g_return_val_if_fail (book_client != NULL, 1); if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) { report_error ("client open sync", &error); g_object_unref (book_client); return 1; } /* * Sync version */ if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) { g_object_unref (book_client); return 1; } uid = e_contact_get (contact, E_CONTACT_UID); g_object_unref (contact); if (!e_book_client_remove_contact_by_uid_sync (book_client, uid, NULL, &error)) { report_error ("remove contact sync", &error); g_object_unref (book_client); g_free (uid); return 1; } if (!e_book_client_get_contact_sync (book_client, uid, &contact, NULL, &error) && g_error_matches (error, E_BOOK_CLIENT_ERROR, E_BOOK_CLIENT_ERROR_CONTACT_NOT_FOUND)) { g_clear_error (&error); } else { report_error ("fail with get contact sync on removed contact", &error); g_object_unref (book_client); g_free (uid); return 1; } g_free (uid); if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) { report_error ("client remove sync", &error); g_object_unref (book_client); return 1; } g_object_unref (book_client); /* * Async version */ book_client = new_temp_client (NULL); g_return_val_if_fail (book_client != NULL, 1); if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) { report_error ("client open sync", &error); g_object_unref (book_client); return 1; } contact = NULL; if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) { g_object_unref (book_client); return 1; } uid = e_contact_get (contact, E_CONTACT_UID); g_object_unref (contact); e_book_client_remove_contact_by_uid (book_client, uid, NULL, remove_contact_by_uid_cb, uid); start_main_loop (NULL, NULL); g_free (uid); if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) { report_error ("client remove sync", &error); g_object_unref (book_client); return 1; } g_object_unref (book_client); return 0; }