[libgdata] Bug 653224 — Add gdata_picasaweb_service_get_user_async()



commit 227da3c082b12a9bab5a55a88b9d818e4f7b14fe
Author: Philip Withnall <philip tecnocode co uk>
Date:   Mon Jul 4 21:24:22 2011 +0100

    Bug 653224 â Add gdata_picasaweb_service_get_user_async()
    
    Add an async. variant of gdata_picasaweb_service_get_user(). This adds the
    following API:
     â gdata_picasaweb_service_get_user_async()
     â gdata_picasaweb_service_get_user_finish()
    
    Closes: bgo#653224

 docs/reference/gdata-sections.txt                  |    2 +
 gdata/gdata.symbols                                |    2 +
 gdata/services/picasaweb/gdata-picasaweb-service.c |   89 +++++++++++++
 gdata/services/picasaweb/gdata-picasaweb-service.h |    4 +
 gdata/tests/picasaweb.c                            |  136 +++++++++++++++++++-
 5 files changed, 227 insertions(+), 6 deletions(-)
---
diff --git a/docs/reference/gdata-sections.txt b/docs/reference/gdata-sections.txt
index a922447..335b8d2 100644
--- a/docs/reference/gdata-sections.txt
+++ b/docs/reference/gdata-sections.txt
@@ -1339,6 +1339,8 @@ GDataPicasaWebServiceClass
 gdata_picasaweb_service_new
 gdata_picasaweb_service_get_primary_authorization_domain
 gdata_picasaweb_service_get_user
+gdata_picasaweb_service_get_user_async
+gdata_picasaweb_service_get_user_finish
 gdata_picasaweb_service_query_all_albums
 gdata_picasaweb_service_query_all_albums_async
 gdata_picasaweb_service_query_files
diff --git a/gdata/gdata.symbols b/gdata/gdata.symbols
index 2ef3235..1eda93e 100644
--- a/gdata/gdata.symbols
+++ b/gdata/gdata.symbols
@@ -909,3 +909,5 @@ gdata_calendar_query_get_max_attendees
 gdata_calendar_query_set_max_attendees
 gdata_calendar_query_show_deleted
 gdata_calendar_query_set_show_deleted
+gdata_picasaweb_service_get_user_async
+gdata_picasaweb_service_get_user_finish
diff --git a/gdata/services/picasaweb/gdata-picasaweb-service.c b/gdata/services/picasaweb/gdata-picasaweb-service.c
index 919779c..681e20b 100644
--- a/gdata/services/picasaweb/gdata-picasaweb-service.c
+++ b/gdata/services/picasaweb/gdata-picasaweb-service.c
@@ -258,6 +258,95 @@ gdata_picasaweb_service_get_user (GDataPicasaWebService *self, const gchar *user
 	return GDATA_PICASAWEB_USER (user);
 }
 
+static void
+get_user_thread (GSimpleAsyncResult *result, GDataPicasaWebService *service, GCancellable *cancellable)
+{
+	GDataPicasaWebUser *user;
+	GError *error = NULL;
+
+	/* Get the user and return */
+	user = gdata_picasaweb_service_get_user (service, g_simple_async_result_get_op_res_gpointer (result), cancellable, &error);
+
+	if (error != NULL) {
+		g_simple_async_result_set_from_error (result, error);
+		g_error_free (error);
+
+		if (user != NULL) {
+			g_object_unref (user);
+		}
+
+		return;
+	}
+
+	/* Replace the username with the user object */
+	g_simple_async_result_set_op_res_gpointer (result, g_object_ref (user), (GDestroyNotify) g_object_unref);
+}
+
+/**
+ * gdata_picasaweb_service_get_user_async:
+ * @self: a #GDataPicasaWebService
+ * @username: (allow-none): the username of the user whose information you wish to retrieve, or %NULL for the currently authenticated user
+ * @cancellable: (allow-none): optional #GCancellable object, or %NULL
+ * @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 the user specified by @username.
+ *
+ * For more details, see gdata_picasaweb_service_get_user() which is the synchronous version of this method.
+ *
+ * When the operation is finished, @callback will be called. You can then call gdata_picasaweb_service_get_user_finish() to get the results of the
+ * operation.
+ *
+ * Since: 0.9.1
+ */
+void
+gdata_picasaweb_service_get_user_async (GDataPicasaWebService *self, const gchar *username, GCancellable *cancellable,
+                                        GAsyncReadyCallback callback, gpointer user_data)
+{
+	GSimpleAsyncResult *result;
+
+	g_return_if_fail (GDATA_IS_PICASAWEB_SERVICE (self));
+	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+	g_return_if_fail (callback != NULL);
+
+	result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, gdata_picasaweb_service_get_user_async);
+	g_simple_async_result_set_op_res_gpointer (result, g_strdup (username), (GDestroyNotify) g_free);
+	g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) get_user_thread, G_PRIORITY_DEFAULT, cancellable);
+	g_object_unref (result);
+}
+
+/**
+ * gdata_picasaweb_service_get_user_finish:
+ * @self: a #GDataPicasaWebService
+ * @result: a #GAsyncResult
+ * @error: a #GError, or %NULL
+ *
+ * Finishes an asynchronous user retrieval operation started with gdata_picasaweb_service_get_user_async().
+ *
+ * Return value: (transfer full): a #GDataPicasaWebUser; unref with g_object_unref()
+ *
+ * Since: 0.9.1
+ */
+GDataPicasaWebUser *
+gdata_picasaweb_service_get_user_finish (GDataPicasaWebService *self, GAsyncResult *async_result, GError **error)
+{
+	GSimpleAsyncResult *result;
+
+	g_return_val_if_fail (GDATA_IS_PICASAWEB_SERVICE (self), NULL);
+	g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), NULL);
+	g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+	g_return_val_if_fail (g_simple_async_result_is_valid (async_result, G_OBJECT (self), gdata_picasaweb_service_get_user_async) == TRUE, NULL);
+
+	result = G_SIMPLE_ASYNC_RESULT (async_result);
+
+	if (g_simple_async_result_propagate_error (result, error) == TRUE) {
+		return NULL;
+	}
+
+	return g_simple_async_result_get_op_res_gpointer (result);
+}
+
 /**
  * gdata_picasaweb_service_query_all_albums:
  * @self: a #GDataPicasaWebService
diff --git a/gdata/services/picasaweb/gdata-picasaweb-service.h b/gdata/services/picasaweb/gdata-picasaweb-service.h
index b5eddec..6e9c4bd 100644
--- a/gdata/services/picasaweb/gdata-picasaweb-service.h
+++ b/gdata/services/picasaweb/gdata-picasaweb-service.h
@@ -71,6 +71,10 @@ GDataAuthorizationDomain *gdata_picasaweb_service_get_primary_authorization_doma
 
 GDataPicasaWebUser *gdata_picasaweb_service_get_user (GDataPicasaWebService *self, const gchar *username,
                                                       GCancellable *cancellable, GError **error) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
+void gdata_picasaweb_service_get_user_async (GDataPicasaWebService *self, const gchar *username, GCancellable *cancellable,
+                                             GAsyncReadyCallback callback, gpointer user_data);
+GDataPicasaWebUser *gdata_picasaweb_service_get_user_finish (GDataPicasaWebService *self, GAsyncResult *result,
+                                                             GError **error) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
 
 GDataFeed *gdata_picasaweb_service_query_all_albums (GDataPicasaWebService *self, GDataQuery *query, const gchar *username, GCancellable *cancellable,
                                                      GDataQueryProgressCallback progress_callback, gpointer progress_user_data,
diff --git a/gdata/tests/picasaweb.c b/gdata/tests/picasaweb.c
index 4853cf6..28c354c 100644
--- a/gdata/tests/picasaweb.c
+++ b/gdata/tests/picasaweb.c
@@ -1153,6 +1153,22 @@ test_query_all_albums_async_progress_closure (QueryAllAlbumsAsyncData *unused_da
 }
 
 static void
+check_authenticated_user_details (GDataPicasaWebUser *user)
+{
+	g_assert (GDATA_IS_PICASAWEB_USER (user));
+
+	g_assert_cmpstr (gdata_picasaweb_user_get_user (user), ==, "libgdata.picasaweb");
+	g_assert_cmpstr (gdata_picasaweb_user_get_nickname (user), ==, "libgdata.picasaweb");
+	/* 1GiB: it'll be a beautiful day when this assert gets tripped */
+	g_assert_cmpint (gdata_picasaweb_user_get_quota_limit (user), ==, 1073741824);
+	g_assert_cmpint (gdata_picasaweb_user_get_quota_current (user), >=, 0);
+	/* now it's 1000, testing this weakly to avoid having to regularly update it */
+	g_assert_cmpint (gdata_picasaweb_user_get_max_photos_per_album (user), >, 0);
+	/* tested weakly to avoid having to update it regularly */
+	g_assert_cmpstr (gdata_picasaweb_user_get_thumbnail_uri (user), !=, NULL);
+}
+
+static void
 test_query_user (gconstpointer service)
 {
 	GDataPicasaWebUser *user;
@@ -1160,17 +1176,119 @@ test_query_user (gconstpointer service)
 
 	user = gdata_picasaweb_service_get_user (GDATA_PICASAWEB_SERVICE (service), NULL, NULL, &error);
 	g_assert_no_error (error);
+	g_clear_error (&error);
+
+	check_authenticated_user_details (user);
+
+	g_object_unref (user);
+}
+
+typedef struct {
+	GMainLoop *main_loop;
+} QueryUserAsyncData;
+
+static void
+set_up_query_user_async (QueryUserAsyncData *data, gconstpointer service)
+{
+	data->main_loop = g_main_loop_new (NULL, FALSE);
+}
+
+static void
+tear_down_query_user_async (QueryUserAsyncData *data, gconstpointer service)
+{
+	g_main_loop_unref (data->main_loop);
+}
+
+static void
+test_query_user_async_cb (GDataPicasaWebService *service, GAsyncResult *result, QueryUserAsyncData *data)
+{
+	GDataPicasaWebUser *user;
+	GError *error = NULL;
+
+	user = gdata_picasaweb_service_get_user_finish (service, result, &error);
+	g_assert_no_error (error);
+	g_clear_error (&error);
+
+	check_authenticated_user_details (user);
+
+	g_object_unref (user);
+
+	g_main_loop_quit (data->main_loop);
+}
+
+/* Check that asynchronously querying for the currently authenticated user's details works and returns the correct details. */
+static void
+test_query_user_async (QueryUserAsyncData *data, gconstpointer service)
+{
+	gdata_picasaweb_service_get_user_async (GDATA_PICASAWEB_SERVICE (service), NULL, NULL, (GAsyncReadyCallback) test_query_user_async_cb, data);
+
+	g_main_loop_run (data->main_loop);
+}
+
+static void
+test_query_user_async_cancellable_cb (GDataPicasaWebService *service, GAsyncResult *result, QueryUserAsyncData *data)
+{
+	GDataPicasaWebUser *user;
+	GError *error = NULL;
+
+	user = gdata_picasaweb_service_get_user_finish (service, result, &error);
+	g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
+	g_assert (user == NULL);
+	g_clear_error (&error);
+
+	g_main_loop_quit (data->main_loop);
+}
+
+/* Check that cancelling an asynchronous query for the currently authenticated user's details works. */
+static void
+test_query_user_async_cancellation (QueryUserAsyncData *data, gconstpointer service)
+{
+	GCancellable *cancellable;
+
+	cancellable = g_cancellable_new ();
+	g_cancellable_cancel (cancellable);
+	gdata_picasaweb_service_get_user_async (GDATA_PICASAWEB_SERVICE (service), NULL, cancellable,
+	                                        (GAsyncReadyCallback) test_query_user_async_cancellable_cb, data);
+
+	g_main_loop_run (data->main_loop);
+
+	g_object_unref (cancellable);
+}
+
+static void
+test_query_user_by_username_async_cb (GDataPicasaWebService *service, GAsyncResult *result, QueryUserAsyncData *data)
+{
+	GDataPicasaWebUser *user;
+	GError *error = NULL;
+
+	user = gdata_picasaweb_service_get_user_finish (service, result, &error);
+	g_assert_no_error (error);
 	g_assert (GDATA_IS_PICASAWEB_USER (user));
 	g_clear_error (&error);
 
-	g_assert_cmpstr (gdata_picasaweb_user_get_user (user), ==, "libgdata.picasaweb");
-	g_assert_cmpstr (gdata_picasaweb_user_get_nickname (user), ==, "libgdata.picasaweb");
-	g_assert_cmpint (gdata_picasaweb_user_get_quota_limit (user), ==, 1073741824); /* 1GiB: it'll be a beautiful day when this assert gets tripped */
-	g_assert_cmpint (gdata_picasaweb_user_get_quota_current (user), >=, 0);
-	g_assert_cmpint (gdata_picasaweb_user_get_max_photos_per_album (user), >, 0); /* now it's 1000, testing this weakly to avoid having to regularly update it */
-	g_assert_cmpstr (gdata_picasaweb_user_get_thumbnail_uri (user), !=, NULL); /* tested weakly to avoid having to update it regularly */
+	g_assert_cmpstr (gdata_picasaweb_user_get_user (user), ==, "philip.withnall");
+	g_assert_cmpstr (gdata_picasaweb_user_get_nickname (user), ==, "Philip");
+	g_assert_cmpint (gdata_picasaweb_user_get_quota_limit (user), ==, -1); /* not the logged in user */
+	g_assert_cmpint (gdata_picasaweb_user_get_quota_current (user), ==, -1); /* not the logged in user */
+	g_assert_cmpint (gdata_picasaweb_user_get_max_photos_per_album (user), ==, -1); /* not the logged in user */
+	/* tested weakly to avoid having to update it regularly */
+	g_assert_cmpstr (gdata_picasaweb_user_get_thumbnail_uri (user), !=, NULL);
 
 	g_object_unref (user);
+
+	g_main_loop_quit (data->main_loop);
+}
+
+/* Check that querying for a user other than the currently authenticated user, asynchronously, gives us an appropriate result. This result should,
+ * for example, not contain any private information about the queried user. (That's a server-side consideration, but libgdata has to handle the
+ * lack of information correctly.) */
+static void
+test_query_user_by_username_async (QueryUserAsyncData *data, gconstpointer service)
+{
+	gdata_picasaweb_service_get_user_async (GDATA_PICASAWEB_SERVICE (service), "philip.withnall", NULL,
+	                                        (GAsyncReadyCallback) test_query_user_by_username_async_cb, data);
+
+	g_main_loop_run (data->main_loop);
 }
 
 typedef struct {
@@ -1738,6 +1856,12 @@ main (int argc, char *argv[])
 		g_test_add_data_func ("/picasaweb/query/all_albums/bad_query/with_limits", service, test_query_all_albums_bad_query_with_limits);
 
 		g_test_add_data_func ("/picasaweb/query/user", service, test_query_user);
+		g_test_add ("/picasaweb/query/user/async", QueryUserAsyncData, service, set_up_query_user_async, test_query_user_async,
+		            tear_down_query_user_async);
+		g_test_add ("/picasaweb/query/user/async/cancellation", QueryUserAsyncData, service, set_up_query_user_async,
+		            test_query_user_async_cancellation, tear_down_query_user_async);
+		g_test_add ("/picasaweb/query/user/by-username/async", QueryUserAsyncData, service, set_up_query_user_async,
+		            test_query_user_by_username_async, tear_down_query_user_async);
 
 		g_test_add ("/picasaweb/insert/album", InsertAlbumData, service, set_up_insert_album, test_insert_album, tear_down_insert_album);
 		g_test_add ("/picasaweb/insert/album/async", InsertAlbumAsyncData, service, set_up_insert_album_async, test_insert_album_async,



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