[evolution-data-server/treitter-client-gdbus] Port the e-book 'addContact' method to gdbus and add a test program



commit 72e2d8373f55ac27dc01650a08c0f152a63eedf7
Author: Travis Reitter <treitter gmail com>
Date:   Thu Dec 3 17:44:26 2009 -0800

    Port the e-book 'addContact' method to gdbus and add a test program

 addressbook/libebook/e-book.c                     |   14 ++--
 addressbook/libebook/e-data-book-gdbus-bindings.h |   83 +++++++++++++++++++++
 addressbook/tests/ebook/Makefile.am               |    3 +
 addressbook/tests/ebook/ebook-test-utils.c        |   38 ++++++++++
 addressbook/tests/ebook/ebook-test-utils.h        |    5 +
 addressbook/tests/ebook/test-ebook-add-contact.c  |   71 ++++++++++++++++++
 6 files changed, 206 insertions(+), 8 deletions(-)
---
diff --git a/addressbook/libebook/e-book.c b/addressbook/libebook/e-book.c
index b46fb6c..256657f 100644
--- a/addressbook/libebook/e-book.c
+++ b/addressbook/libebook/e-book.c
@@ -373,7 +373,7 @@ e_book_add_contact (EBook           *book,
 
 	vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
 	LOCK_CONN ();
-	org_gnome_evolution_dataserver_addressbook_Book_add_contact (book->priv->proxy, vcard, &uid, &err);
+	e_data_book_gdbus_add_contact_sync (book->priv->gdbus_proxy, vcard, &uid, &err);
 	UNLOCK_CONN ();
 	g_free (vcard);
 	if (uid) {
@@ -384,16 +384,14 @@ e_book_add_contact (EBook           *book,
 }
 
 static void
-add_contact_reply (DBusGProxy *proxy, gchar *uid, GError *error, gpointer user_data)
+add_contact_reply (GDBusProxy *proxy,
+		   gchar      *uid,
+		   GError     *error,
+		   gpointer    user_data)
 {
 	AsyncData *data = user_data;
 	EBookIdCallback cb = data->callback;
 
-	/* If there is an error returned the GLib bindings currently return garbage
-	   for the OUT values. This is bad. */
-	if (error)
-		uid = NULL;
-
 	if (cb)
 		cb (data->book, get_status_from_error (error), uid, data->closure);
 
@@ -436,7 +434,7 @@ e_book_async_add_contact (EBook                 *book,
 	data->closure = closure;
 
 	LOCK_CONN ();
-	org_gnome_evolution_dataserver_addressbook_Book_add_contact_async (book->priv->proxy, vcard, add_contact_reply, data);
+	e_data_book_gdbus_add_contact (book->priv->gdbus_proxy, vcard, add_contact_reply, data);
 	UNLOCK_CONN ();
 	g_free (vcard);
 	return 0;
diff --git a/addressbook/libebook/e-data-book-gdbus-bindings.h b/addressbook/libebook/e-data-book-gdbus-bindings.h
index 4566d3f..1b1a885 100644
--- a/addressbook/libebook/e-data-book-gdbus-bindings.h
+++ b/addressbook/libebook/e-data-book-gdbus-bindings.h
@@ -242,5 +242,88 @@ e_data_book_gdbus_get_contact (GDBusProxy                          *proxy,
         g_dbus_proxy_invoke_method (proxy, "getContact", parameters, -1, NULL, (GAsyncReadyCallback) get_contact_cb, closure);
 }
 
+static gboolean
+add_contact_demarshal_retvals (GVariant *retvals, char **OUT_uid)
+{
+        gboolean success = TRUE;
+
+        if (retvals) {
+                const char *uid = NULL;
+
+                g_variant_get (retvals, "(s)", &uid);
+                if (uid) {
+                        *OUT_uid = g_strdup (uid);
+                } else {
+                        success = FALSE;
+                }
+
+                g_variant_unref (retvals);
+        } else {
+                success = FALSE;
+        }
+
+        return success;
+}
+
+static gboolean
+e_data_book_gdbus_add_contact_sync (GDBusProxy  *proxy,
+				    const char  *IN_vcard,
+				    char       **OUT_uid,
+			            GError     **error)
+
+{
+	GVariant *parameters;
+	GVariant *retvals;
+
+	parameters = g_variant_new ("(s)", IN_vcard);
+	retvals = g_dbus_proxy_invoke_method_sync (proxy, "addContact", parameters, -1, NULL, error);
+
+	return add_contact_demarshal_retvals (retvals, OUT_uid);
+}
+
+typedef void (*e_data_book_gdbus_add_contact_reply) (GDBusProxy *proxy,
+						     char       *OUT_uid,
+						     GError     *error,
+						     gpointer    user_data);
+
+static void
+add_contact_cb (GDBusProxy   *proxy,
+		GAsyncResult *result,
+		gpointer      user_data)
+{
+        Closure *closure = user_data;
+        GVariant *retvals;
+        GError *error = NULL;
+        char *OUT_uid = NULL;
+
+        retvals = g_dbus_proxy_invoke_method_finish (proxy, result, &error);
+        if (retvals) {
+                if (!add_contact_demarshal_retvals (retvals, &OUT_uid)) {
+                        error = g_error_new (E_BOOK_ERROR, E_BOOK_ERROR_CORBA_EXCEPTION, "demarshalling results for Book method 'addContact'");
+                }
+        }
+
+        (*(e_data_book_gdbus_add_contact_reply) closure->cb) (proxy, OUT_uid, error, closure->user_data);
+        closure_free (closure);
+}
+
+static void
+e_data_book_gdbus_add_contact (GDBusProxy                          *proxy,
+			       const char                          *IN_vcard,
+			       e_data_book_gdbus_add_contact_reply  callback,
+			       gpointer                             user_data)
+{
+        GVariant *parameters;
+        Closure *closure;
+
+        parameters = g_variant_new ("(s)", IN_vcard);
+
+        closure = g_slice_new (Closure);
+        closure->cb = G_CALLBACK (callback);
+        closure->user_data = user_data;
+
+        g_dbus_proxy_invoke_method (proxy, "addContact", parameters, -1, NULL, (GAsyncReadyCallback) add_contact_cb, closure);
+}
+
 
 G_END_DECLS
diff --git a/addressbook/tests/ebook/Makefile.am b/addressbook/tests/ebook/Makefile.am
index 159e71e..cd7c7f2 100644
--- a/addressbook/tests/ebook/Makefile.am
+++ b/addressbook/tests/ebook/Makefile.am
@@ -27,6 +27,7 @@ noinst_PROGRAMS = \
 	test-date                            \
 	test-ebook                           \
 	test-ebook-async                     \
+	test-ebook-add-contact               \
 	test-ebook-get-contact               \
 	test-ebook-remove                    \
 	test-ebook-view                      \
@@ -53,6 +54,8 @@ test_ebook_LDADD=$(TEST_LIBS)
 test_ebook_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_async_LDADD=$(TEST_LIBS)
 test_ebook_async_CPPFLAGS=$(TEST_CPPFLAGS)
+test_ebook_add_contact_LDADD=$(TEST_LIBS)
+test_ebook_add_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_get_contact_LDADD=$(TEST_LIBS)
 test_ebook_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_ebook_remove_LDADD=$(TEST_LIBS)
diff --git a/addressbook/tests/ebook/ebook-test-utils.c b/addressbook/tests/ebook/ebook-test-utils.c
index 1497760..5c085ae 100644
--- a/addressbook/tests/ebook/ebook-test-utils.c
+++ b/addressbook/tests/ebook/ebook-test-utils.c
@@ -24,6 +24,44 @@ ebook_test_utils_book_add_contact (EBook    *book,
         return e_contact_get (contact, E_CONTACT_UID);
 }
 
+static void
+add_contact_cb (EBook            *book,
+                EBookStatus       status,
+                const char       *uid,
+                EBookTestClosure *closure)
+{
+        if (status != E_BOOK_ERROR_OK) {
+                g_warning ("failed to asynchronously add the contact '%s': "
+                                "status %d", uid, status);
+                exit (1);
+        }
+
+        g_print ("successfully asynchronously added the contact "
+                        "addressbook\n");
+        if (closure) {
+                (*closure->cb) (closure->user_data);
+                g_free (closure);
+        }
+}
+
+void
+ebook_test_utils_book_async_add_contact (EBook       *book,
+                                         EContact    *contact,
+                                         GSourceFunc  callback,
+                                         gpointer     user_data)
+{
+        EBookTestClosure *closure;
+
+        closure = g_new0 (EBookTestClosure, 1);
+        closure->cb = callback;
+        closure->user_data = user_data;
+        if (e_book_async_add_contact (book, contact,
+                                (EBookIdCallback) add_contact_cb, closure)) {
+                g_warning ("failed to set up contact add");
+                exit(1);
+        }
+}
+
 EContact*
 ebook_test_utils_book_get_contact (EBook      *book,
                                    const char *uid)
diff --git a/addressbook/tests/ebook/ebook-test-utils.h b/addressbook/tests/ebook/ebook-test-utils.h
index c1ee18a..516a6bf 100644
--- a/addressbook/tests/ebook/ebook-test-utils.h
+++ b/addressbook/tests/ebook/ebook-test-utils.h
@@ -43,6 +43,11 @@ ebook_test_utils_book_new_temp (char **uri);
 char*
 ebook_test_utils_book_add_contact (EBook    *book,
                                    EContact *contact);
+void
+ebook_test_utils_book_async_add_contact (EBook       *book,
+                                         EContact    *contact,
+                                         GSourceFunc  callback,
+                                         gpointer     user_data);
 
 EContact*
 ebook_test_utils_book_get_contact (EBook      *book,
diff --git a/addressbook/tests/ebook/test-ebook-add-contact.c b/addressbook/tests/ebook/test-ebook-add-contact.c
new file mode 100644
index 0000000..5e23c46
--- /dev/null
+++ b/addressbook/tests/ebook/test-ebook-add-contact.c
@@ -0,0 +1,71 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <stdlib.h>
+#include <libebook/e-book.h>
+
+#include "ebook-test-utils.h"
+
+gint
+main (gint argc, gchar **argv)
+{
+	EBook *book;
+        GMainLoop *loop;
+        EContact *contact;
+        EContact *contact_final;
+        char *uid;
+        const char *contact_final_uid;
+
+	g_type_init ();
+
+        /*
+         * Setup
+         */
+        book = ebook_test_utils_book_new_temp (NULL);
+        ebook_test_utils_book_open (book, FALSE);
+
+        contact = e_contact_new_from_vcard (EBOOK_TEST_UTILS_VCARD_SIMPLE);
+        uid = ebook_test_utils_book_add_contact (book, contact);
+
+        /*
+         * Sync version
+         */
+        contact_final = ebook_test_utils_book_get_contact (book, uid);
+        contact_final_uid = e_contact_get_const (contact_final, E_CONTACT_UID);
+
+        /* This is not a thorough comparison (which is difficult, assuming we
+         * give the back-ends leniency in implementation), since that's better
+         * suited to more advanced tests */
+        if (g_strcmp0 (uid, contact_final_uid)) {
+                const char *uri;
+
+                uri = e_book_get_uri (book);
+
+                g_warning ("retrieved contact uid '%s' does not match added "
+                                "contact uid '%s'", contact_final_uid, uid);
+                exit(1);
+        }
+
+        g_print ("successfully added and retrieved contact '%s'\n", uid);
+        g_object_unref (contact);
+        g_object_unref (contact_final);
+
+        ebook_test_utils_book_remove (book);
+
+        /*
+         * Async version
+         */
+        book = ebook_test_utils_book_new_temp (NULL);
+        ebook_test_utils_book_open (book, FALSE);
+        contact = e_contact_new_from_vcard (EBOOK_TEST_UTILS_VCARD_SIMPLE);
+        
+        loop = g_main_loop_new (NULL, TRUE);
+        ebook_test_utils_book_async_add_contact (book, contact,
+                        (GSourceFunc) g_main_loop_quit, loop);
+                        
+        g_free (uid);
+        g_main_loop_run (loop);
+
+        ebook_test_utils_book_remove (book);
+
+	return 0;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]