[libgdata] contacts: Add contact group manipulation methods



commit 457e546708559af44091f01501373fb91bacee9f
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sun Oct 17 12:03:12 2010 +0100

    contacts: Add contact group manipulation methods
    
    This adds gdata_contacts_service_query_groups(),
    gdata_contacts_service_insert_group() and their async counterparts, as well
    as test cases for them.

 docs/reference/gdata-sections.txt                |    4 +
 gdata/gdata.symbols                              |    4 +
 gdata/services/contacts/gdata-contacts-service.c |  167 +++++++++++++++++++-
 gdata/services/contacts/gdata-contacts-service.h |   14 ++
 gdata/tests/contacts.c                           |  185 ++++++++++++++++++++++
 5 files changed, 373 insertions(+), 1 deletions(-)
---
diff --git a/docs/reference/gdata-sections.txt b/docs/reference/gdata-sections.txt
index 8e127a7..68691c4 100644
--- a/docs/reference/gdata-sections.txt
+++ b/docs/reference/gdata-sections.txt
@@ -280,6 +280,10 @@ gdata_contacts_service_query_contacts
 gdata_contacts_service_query_contacts_async
 gdata_contacts_service_insert_contact
 gdata_contacts_service_insert_contact_async
+gdata_contacts_service_query_groups
+gdata_contacts_service_query_groups_async
+gdata_contacts_service_insert_group
+gdata_contacts_service_insert_group_async
 <SUBSECTION Standard>
 gdata_contacts_service_get_type
 GDATA_CONTACTS_SERVICE
diff --git a/gdata/gdata.symbols b/gdata/gdata.symbols
index 694c59f..5cee491 100644
--- a/gdata/gdata.symbols
+++ b/gdata/gdata.symbols
@@ -848,3 +848,7 @@ gdata_contacts_group_get_system_group_id
 gdata_contacts_group_get_extended_property
 gdata_contacts_group_get_extended_properties
 gdata_contacts_group_set_extended_property
+gdata_contacts_service_query_groups
+gdata_contacts_service_query_groups_async
+gdata_contacts_service_insert_group
+gdata_contacts_service_insert_group_async
diff --git a/gdata/services/contacts/gdata-contacts-service.c b/gdata/services/contacts/gdata-contacts-service.c
index 08c4def..595e83d 100644
--- a/gdata/services/contacts/gdata-contacts-service.c
+++ b/gdata/services/contacts/gdata-contacts-service.c
@@ -132,7 +132,7 @@ gdata_contacts_service_query_contacts (GDataContactsService *self, GDataQuery *q
  * @cancellable: optional #GCancellable object, or %NULL
  * @progress_callback: a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
  * @progress_user_data: (closure): data to pass to the @progress_callback function
- * @callback: a #GAsyncReadyCallback to call when authentication is finished
+ * @callback: a #GAsyncReadyCallback to call when the query is finished
  * @user_data: (closure): data to pass to the @callback function
  *
  * Queries the service to return a list of contacts matching the given @query. @self and
@@ -235,3 +235,168 @@ gdata_contacts_service_insert_contact_async (GDataContactsService *self, GDataCo
 	gdata_service_insert_entry_async (GDATA_SERVICE (self), uri, GDATA_ENTRY (contact), cancellable, callback, user_data);
 	g_free (uri);
 }
+
+/**
+ * gdata_contacts_service_query_groups:
+ * @self: a #GDataContactsService
+ * @query: (allow-none): a #GDataQuery with the query parameters, or %NULL
+ * @cancellable: optional #GCancellable object, or %NULL
+ * @progress_callback: (scope call): a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
+ * @progress_user_data: (closure): data to pass to the @progress_callback function
+ * @error: a #GError, or %NULL
+ *
+ * Queries the service to return a list of groups matching the given @query.
+ *
+ * For more details, see gdata_service_query().
+ *
+ * Return value: (transfer full): a #GDataFeed of query results; unref with g_object_unref()
+ *
+ * Since: 0.7.0
+ **/
+GDataFeed *
+gdata_contacts_service_query_groups (GDataContactsService *self, GDataQuery *query, GCancellable *cancellable,
+                                     GDataQueryProgressCallback progress_callback, gpointer progress_user_data, GError **error)
+{
+	GDataFeed *feed;
+	gchar *request_uri;
+
+	g_return_val_if_fail (GDATA_IS_CONTACTS_SERVICE (self), NULL);
+	g_return_val_if_fail (query == NULL || GDATA_IS_QUERY (query), NULL);
+	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
+	g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+	/* Ensure we're authenticated first */
+	if (gdata_service_is_authenticated (GDATA_SERVICE (self)) == FALSE) {
+		g_set_error_literal (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED,
+		                     _("You must be authenticated to query contact groups."));
+		return NULL;
+	}
+
+	request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/groups/default/full", NULL);
+	feed = gdata_service_query (GDATA_SERVICE (self), request_uri, GDATA_QUERY (query),
+	                            GDATA_TYPE_CONTACTS_GROUP, cancellable, progress_callback, progress_user_data, error);
+	g_free (request_uri);
+
+	return feed;
+}
+
+/**
+ * gdata_contacts_service_query_groups_async: (skip)
+ * @self: a #GDataContactsService
+ * @query: (allow-none): a #GDataQuery with the query parameters, or %NULL
+ * @cancellable: optional #GCancellable object, or %NULL
+ * @progress_callback: a #GDataQueryProgressCallback to call when an entry is loaded, or %NULL
+ * @progress_user_data: (closure): data to pass to the @progress_callback function
+ * @callback: a #GAsyncReadyCallback to call when the query is finished
+ * @user_data: (closure): data to pass to the @callback function
+ *
+ * Queries the service to return a list of groups matching the given @query. @self and @query are all reffed when this function is called, so can
+ * safely be unreffed after this function returns.
+ *
+ * For more details, see gdata_contacts_service_query_groups(), which is the synchronous version of this function, and gdata_service_query_async(),
+ * which is the base asynchronous query function.
+ *
+ * Since: 0.7.0
+ **/
+void
+gdata_contacts_service_query_groups_async (GDataContactsService *self, GDataQuery *query, GCancellable *cancellable,
+                                           GDataQueryProgressCallback progress_callback, gpointer progress_user_data,
+                                           GAsyncReadyCallback callback, gpointer user_data)
+{
+	gchar *request_uri;
+
+	g_return_if_fail (GDATA_IS_CONTACTS_SERVICE (self));
+	g_return_if_fail (query == NULL || GDATA_IS_QUERY (query));
+	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+	g_return_if_fail (callback != NULL);
+
+	/* Ensure we're authenticated first */
+	if (gdata_service_is_authenticated (GDATA_SERVICE (self)) == FALSE) {
+		g_simple_async_report_error_in_idle (G_OBJECT (self), callback, user_data,
+		                                     GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED,
+		                                     _("You must be authenticated to query contact groups."));
+		return;
+	}
+
+	request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/groups/default/full", NULL);
+	gdata_service_query_async (GDATA_SERVICE (self), request_uri, GDATA_QUERY (query),
+	                           GDATA_TYPE_CONTACTS_GROUP, cancellable, progress_callback, progress_user_data, callback, user_data);
+	g_free (request_uri);
+}
+
+/**
+ * gdata_contacts_service_insert_group:
+ * @self: a #GDataContactsService
+ * @group: a #GDataContactsGroup to create on the server
+ * @cancellable: optional #GCancellable object, or %NULL
+ * @error: a #GError, or %NULL
+ *
+ * Inserts a new contact group described by @group. The user must be authenticated to use this function.
+ *
+ * Return value: (transfer full): the inserted #GDataContactsGroup; unref with g_object_unref()
+ *
+ * Since: 0.7.0
+ **/
+GDataContactsGroup *
+gdata_contacts_service_insert_group (GDataContactsService *self, GDataContactsGroup *group, GCancellable *cancellable, GError **error)
+{
+	gchar *request_uri;
+	GDataEntry *new_group;
+
+	g_return_val_if_fail (GDATA_IS_CONTACTS_SERVICE (self), NULL);
+	g_return_val_if_fail (GDATA_IS_CONTACTS_GROUP (group), NULL);
+	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
+	g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+	if (gdata_entry_is_inserted (GDATA_ENTRY (group)) == TRUE) {
+		g_set_error_literal (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_ENTRY_ALREADY_INSERTED,
+		                     _("The group has already been inserted."));
+		return NULL;
+	}
+
+	if (gdata_service_is_authenticated (GDATA_SERVICE (self)) == FALSE) {
+		g_set_error_literal (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED,
+		                     _("You must be authenticated to insert a group."));
+		return NULL;
+	}
+
+	request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/groups/default/full", NULL);
+	new_group = gdata_service_insert_entry (GDATA_SERVICE (self), request_uri, GDATA_ENTRY (group), cancellable, error);
+	g_free (request_uri);
+
+	return GDATA_CONTACTS_GROUP (new_group);
+}
+
+/**
+ * gdata_contacts_service_insert_group_async:
+ * @self: a #GDataContactsService
+ * @group: the #GDataContactsGroup to insert
+ * @cancellable: optional #GCancellable object, or %NULL
+ * @callback: a #GAsyncReadyCallback to call when insertion is finished
+ * @user_data: (closure): data to pass to the @callback function
+ *
+ * Inserts a new contact group described by @group. The user must be authenticated to use this function. @self and @group are both reffed when this
+ * function is called, so can safely be unreffed after this function returns.
+ *
+ * @callback should call gdata_service_insert_entry_finish() to obtain a #GDataContactsGroup representing the inserted group and to check for possible
+ * errors.
+ *
+ * For more details, see gdata_contacts_service_insert_group(), which is the synchronous version of this function, and
+ * gdata_service_insert_entry_async(), which is the base asynchronous insertion function.
+ *
+ * Since: 0.7.0
+ **/
+void
+gdata_contacts_service_insert_group_async (GDataContactsService *self, GDataContactsGroup *group, GCancellable *cancellable,
+                                           GAsyncReadyCallback callback, gpointer user_data)
+{
+	gchar *request_uri;
+
+	g_return_if_fail (GDATA_IS_CONTACTS_SERVICE (self));
+	g_return_if_fail (GDATA_IS_CONTACTS_GROUP (group));
+	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+
+	request_uri = g_strconcat (_gdata_service_get_scheme (), "://www.google.com/m8/feeds/groups/default/full", NULL);
+	gdata_service_insert_entry_async (GDATA_SERVICE (self), request_uri, GDATA_ENTRY (group), cancellable, callback, user_data);
+	g_free (request_uri);
+}
diff --git a/gdata/services/contacts/gdata-contacts-service.h b/gdata/services/contacts/gdata-contacts-service.h
index c3c442a..613c8b4 100644
--- a/gdata/services/contacts/gdata-contacts-service.h
+++ b/gdata/services/contacts/gdata-contacts-service.h
@@ -78,6 +78,20 @@ GDataContactsContact *gdata_contacts_service_insert_contact (GDataContactsServic
 void gdata_contacts_service_insert_contact_async (GDataContactsService *self, GDataContactsContact *contact, GCancellable *cancellable,
                                                   GAsyncReadyCallback callback, gpointer user_data);
 
+#include <gdata/services/contacts/gdata-contacts-group.h>
+
+GDataFeed *gdata_contacts_service_query_groups (GDataContactsService *self, GDataQuery *query, GCancellable *cancellable,
+                                                GDataQueryProgressCallback progress_callback, gpointer progress_user_data,
+                                                GError **error) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
+void gdata_contacts_service_query_groups_async (GDataContactsService *self, GDataQuery *query, GCancellable *cancellable,
+                                                GDataQueryProgressCallback progress_callback, gpointer progress_user_data,
+                                                GAsyncReadyCallback callback, gpointer user_data);
+
+GDataContactsGroup *gdata_contacts_service_insert_group (GDataContactsService *self, GDataContactsGroup *group,
+                                                         GCancellable *cancellable, GError **error) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
+void gdata_contacts_service_insert_group_async (GDataContactsService *self, GDataContactsGroup *group, GCancellable *cancellable,
+                                                GAsyncReadyCallback callback, gpointer user_data);
+
 G_END_DECLS
 
 #endif /* !GDATA_CONTACTS_SERVICE_H */
diff --git a/gdata/tests/contacts.c b/gdata/tests/contacts.c
index 18f1a7d..4d22bdf 100644
--- a/gdata/tests/contacts.c
+++ b/gdata/tests/contacts.c
@@ -610,6 +610,186 @@ test_update_simple (gconstpointer service)
 }
 
 static void
+test_query_all_groups (gconstpointer service)
+{
+	GDataFeed *feed;
+	GError *error = NULL;
+
+	feed = gdata_contacts_service_query_groups (GDATA_CONTACTS_SERVICE (service), NULL, NULL, NULL, NULL, &error);
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_FEED (feed));
+	g_clear_error (&error);
+
+	/* TODO: check entries, kinds and feed properties */
+
+	g_object_unref (feed);
+
+}
+
+static void
+test_query_all_groups_async_cb (GDataService *service, GAsyncResult *async_result, GMainLoop *main_loop)
+{
+	GDataFeed *feed;
+	GError *error = NULL;
+
+	feed = gdata_service_query_finish (service, async_result, &error);
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_FEED (feed));
+	g_clear_error (&error);
+
+	/* TODO: Tests? */
+	g_main_loop_quit (main_loop);
+
+	g_object_unref (feed);
+}
+
+static void
+test_query_all_groups_async (gconstpointer service)
+{
+	GMainLoop *main_loop = g_main_loop_new (NULL, TRUE);
+
+	gdata_contacts_service_query_groups_async (GDATA_CONTACTS_SERVICE (service), NULL, NULL, NULL, NULL,
+	                                           (GAsyncReadyCallback) test_query_all_groups_async_cb, main_loop);
+
+	g_main_loop_run (main_loop);
+	g_main_loop_unref (main_loop);
+}
+
+static void
+test_insert_group (gconstpointer service)
+{
+	GDataContactsGroup *group, *new_group;
+	GTimeVal time_val;
+	GHashTable *properties;
+	gint64 edited;
+	gboolean deleted;
+	gchar *system_group_id, *xml;
+	GError *error = NULL;
+
+	group = gdata_contacts_group_new (NULL);
+
+	/* Check the kind is present and correct */
+	g_assert (GDATA_IS_CONTACTS_GROUP (group));
+	check_kind (GDATA_ENTRY (group), "http://schemas.google.com/contact/2008#group";);
+
+	/* Set various properties */
+	gdata_entry_set_title (GDATA_ENTRY (group), "New Group!");
+	g_assert (gdata_contacts_group_set_extended_property (group, "foobar", "barfoo") == TRUE);
+
+	/* Check various properties */
+	g_get_current_time (&time_val);
+	g_assert_cmpint (gdata_contacts_group_get_edited (group), ==, time_val.tv_sec);
+	g_assert (gdata_contacts_group_is_deleted (group) == FALSE);
+	g_assert (gdata_contacts_group_get_system_group_id (group) == NULL);
+
+	properties = gdata_contacts_group_get_extended_properties (group);
+	g_assert (properties != NULL);
+	g_assert_cmpint (g_hash_table_size (properties), ==, 1);
+	g_assert_cmpstr (gdata_contacts_group_get_extended_property (group, "foobar"), ==, "barfoo");
+
+	/* Check the properties a different way */
+	g_object_get (G_OBJECT (group),
+	              "edited", &edited,
+	              "deleted", &deleted,
+	              "system-group-id", &system_group_id,
+	              NULL);
+
+	g_assert_cmpint (edited, ==, time_val.tv_sec);
+	g_assert (deleted == FALSE);
+	g_assert (system_group_id == NULL);
+
+	g_free (system_group_id);
+
+	/* Check the XML */
+	xml = gdata_parsable_get_xml (GDATA_PARSABLE (group));
+	g_assert_cmpstr (xml, ==,
+		"<?xml version='1.0' encoding='UTF-8'?>"
+		"<entry xmlns='http://www.w3.org/2005/Atom' "
+		       "xmlns:gd='http://schemas.google.com/g/2005' "
+		       "xmlns:app='http://www.w3.org/2007/app' "
+		       "xmlns:gContact='http://schemas.google.com/contact/2008'>"
+			"<title type='text'>New Group!</title>"
+			"<content type='text'>New Group!</content>"
+			"<category term='http://schemas.google.com/contact/2008#group' scheme='http://schemas.google.com/g/2005#kind'/>"
+			"<gd:extendedProperty name='foobar'>barfoo</gd:extendedProperty>"
+		"</entry>");
+	g_free (xml);
+
+	/* Insert the group */
+	new_group = gdata_contacts_service_insert_group (GDATA_CONTACTS_SERVICE (service), group, NULL, &error);
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_CONTACTS_GROUP (new_group));
+	check_kind (GDATA_ENTRY (new_group), "http://schemas.google.com/contact/2008#group";);
+	g_clear_error (&error);
+
+	/* Check the properties again */
+	g_assert_cmpint (gdata_contacts_group_get_edited (new_group), >=, time_val.tv_sec);
+	g_assert (gdata_contacts_group_is_deleted (new_group) == FALSE);
+	g_assert (gdata_contacts_group_get_system_group_id (new_group) == NULL);
+
+	properties = gdata_contacts_group_get_extended_properties (new_group);
+	g_assert (properties != NULL);
+	g_assert_cmpint (g_hash_table_size (properties), ==, 1);
+	g_assert_cmpstr (gdata_contacts_group_get_extended_property (new_group, "foobar"), ==, "barfoo");
+
+	/* Delete the group, just to be tidy */
+	g_assert (gdata_service_delete_entry (GDATA_SERVICE (service), GDATA_ENTRY (new_group), NULL, &error) == TRUE);
+	g_assert_no_error (error);
+	g_clear_error (&error);
+
+	g_object_unref (group);
+	g_object_unref (new_group);
+}
+
+static void
+test_insert_group_async_cb (GDataService *service, GAsyncResult *async_result, GMainLoop *main_loop)
+{
+	GDataEntry *entry;
+	GError *error = NULL;
+
+	entry = gdata_service_insert_entry_finish (service, async_result, &error);
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_CONTACTS_GROUP (entry));
+	g_clear_error (&error);
+
+	/* TODO: Tests? */
+
+	/* Delete the group, just to be tidy */
+	g_assert (gdata_service_delete_entry (GDATA_SERVICE (service), entry, NULL, &error) == TRUE);
+	g_assert_no_error (error);
+	g_clear_error (&error);
+
+	g_main_loop_quit (main_loop);
+	g_object_unref (entry);
+}
+
+static void
+test_insert_group_async (gconstpointer service)
+{
+	GDataContactsGroup *group;
+	GMainLoop *main_loop;
+
+	group = gdata_contacts_group_new (NULL);
+
+	/* Check the kind is present and correct */
+	g_assert (GDATA_IS_CONTACTS_GROUP (group));
+	check_kind (GDATA_ENTRY (group), "http://schemas.google.com/contact/2008#group";);
+
+	/* Set various properties */
+	gdata_entry_set_title (GDATA_ENTRY (group), "New Group!");
+	g_assert (gdata_contacts_group_set_extended_property (group, "foobar", "barfoo") == TRUE);
+
+	main_loop = g_main_loop_new (NULL, TRUE);
+
+	gdata_contacts_service_insert_group_async (GDATA_CONTACTS_SERVICE (service), group, NULL, (GAsyncReadyCallback) test_insert_group_async_cb,
+	                                           main_loop);
+
+	g_main_loop_run (main_loop);
+	g_main_loop_unref (main_loop);
+	g_object_unref (group);
+}
+
+static void
 test_query_uri (void)
 {
 	gchar *query_uri;
@@ -1641,6 +1821,11 @@ main (int argc, char *argv[])
 		g_test_add ("/contacts/batch/async", BatchAsyncData, service, setup_batch_async, test_batch_async, teardown_batch_async);
 		g_test_add ("/contacts/batch/async/cancellation", BatchAsyncData, service, setup_batch_async, test_batch_async_cancellation,
 		            teardown_batch_async);
+
+		g_test_add_data_func ("/contacts/groups/query", service, test_query_all_groups);
+		g_test_add_data_func ("/contacts/groups/query_async", service, test_query_all_groups_async);
+		g_test_add_data_func ("/contacts/groups/insert", service, test_insert_group);
+		g_test_add_data_func ("/contacts/groups/insert_async", service, test_insert_group_async);
 	}
 
 	g_test_add_func ("/contacts/query/uri", test_query_uri);



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