[libgdata] documents: Add an async method to remove entries from folders



commit 4e97fad4065c3d85ffe83406f38eb5de6dbadc7e
Author: Philip Withnall <philip tecnocode co uk>
Date:   Thu Dec 9 17:30:02 2010 +0000

    documents: Add an async method to remove entries from folders
    
    This adds an asynchronous version of
    gdata_documents_service_remove_entry_from_folder(), and the associated normal
    and cancellation tests.
    
    The following API has been added:
     â?¢ gdata_documents_service_from_entry_from_folder_async()
     â?¢ gdata_documents_service_from_entry_from_folder_finish()
    
    Helps: bgo#633363

 docs/reference/gdata-sections.txt                  |    2 +
 gdata/gdata.symbols                                |    2 +
 gdata/services/documents/gdata-documents-service.c |  110 ++++++++++++++++++++
 gdata/services/documents/gdata-documents-service.h |    4 +
 gdata/tests/documents.c                            |   84 +++++++++++++++
 5 files changed, 202 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gdata-sections.txt b/docs/reference/gdata-sections.txt
index 86ef49b..c80da98 100644
--- a/docs/reference/gdata-sections.txt
+++ b/docs/reference/gdata-sections.txt
@@ -1721,6 +1721,8 @@ gdata_documents_service_add_entry_to_folder
 gdata_documents_service_add_entry_to_folder_async
 gdata_documents_service_add_entry_to_folder_finish
 gdata_documents_service_remove_entry_from_folder
+gdata_documents_service_remove_entry_from_folder_async
+gdata_documents_service_remove_entry_from_folder_finish
 gdata_documents_service_get_upload_uri
 <SUBSECTION Standard>
 gdata_documents_service_get_type
diff --git a/gdata/gdata.symbols b/gdata/gdata.symbols
index 62e748a..a4409ec 100644
--- a/gdata/gdata.symbols
+++ b/gdata/gdata.symbols
@@ -854,3 +854,5 @@ gdata_documents_service_add_entry_to_folder
 gdata_documents_service_remove_entry_from_folder
 gdata_documents_service_add_entry_to_folder_async
 gdata_documents_service_add_entry_to_folder_finish
+gdata_documents_service_remove_entry_from_folder_async
+gdata_documents_service_remove_entry_from_folder_finish
diff --git a/gdata/services/documents/gdata-documents-service.c b/gdata/services/documents/gdata-documents-service.c
index 5d15199..4ec5b01 100644
--- a/gdata/services/documents/gdata-documents-service.c
+++ b/gdata/services/documents/gdata-documents-service.c
@@ -749,6 +749,116 @@ gdata_documents_service_remove_entry_from_folder (GDataDocumentsService *self, G
 	                                                                G_OBJECT_TYPE (entry), cancellable, error));
 }
 
+typedef struct {
+	GDataDocumentsEntry *entry;
+	GDataDocumentsFolder *folder;
+} RemoveEntryFromFolderData;
+
+static void
+remove_entry_from_folder_data_free (RemoveEntryFromFolderData *data)
+{
+	g_object_unref (data->entry);
+	g_object_unref (data->folder);
+	g_slice_free (RemoveEntryFromFolderData, data);
+}
+
+static void
+remove_entry_from_folder_thread (GSimpleAsyncResult *result, GDataDocumentsService *service, GCancellable *cancellable)
+{
+	GDataDocumentsEntry *updated_entry;
+	RemoveEntryFromFolderData *data;
+	GError *error = NULL;
+
+	data = g_simple_async_result_get_op_res_gpointer (result);
+
+	/* Remove the entry from the folder and return */
+	updated_entry = gdata_documents_service_remove_entry_from_folder (service, data->entry, data->folder, cancellable, &error);
+	if (error != NULL) {
+		g_simple_async_result_set_from_error (result, error);
+		g_error_free (error);
+		return;
+	}
+
+	/* Return the updated entry */
+	g_simple_async_result_set_op_res_gpointer (result, updated_entry, (GDestroyNotify) g_object_unref);
+}
+
+/**
+ * gdata_documents_service_remove_entry_from_folder_async:
+ * @self: a #GDataDocumentsService
+ * @entry: the #GDataDocumentsEntry to remove from @folder
+ * @folder: the #GDataDocumentsFolder to remove @entry from
+ * @cancellable: optional #GCancellable object, or %NULL
+ * @callback: a #GAsyncReadyCallback to call when the operation is finished, or %NULL
+ * @user_data: (closure): data to pass to the @callback function
+ *
+ * Remove the given @entry from the specified @folder. @self, @entry and @folder are all reffed when this function is called, so can safely be unreffed
+ * after this function returns.
+ *
+ * For more details, see gdata_documents_service_remove_entry_from_folder(), which is the synchronous version of this function.
+ *
+ * When the operation is finished, @callback will be called. You can then call gdata_documents_service_remove_entry_from_folder_finish() to get the
+ * results of the operation.
+ *
+ * Since: 0.8.0
+ **/
+void
+gdata_documents_service_remove_entry_from_folder_async (GDataDocumentsService *self, GDataDocumentsEntry *entry, GDataDocumentsFolder *folder,
+                                                        GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
+{
+	GSimpleAsyncResult *result;
+	RemoveEntryFromFolderData *data;
+
+	g_return_if_fail (GDATA_IS_DOCUMENTS_SERVICE (self));
+	g_return_if_fail (GDATA_IS_DOCUMENTS_ENTRY (entry));
+	g_return_if_fail (GDATA_IS_DOCUMENTS_FOLDER (folder));
+	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+
+	data = g_slice_new (RemoveEntryFromFolderData);
+	data->entry = g_object_ref (entry);
+	data->folder = g_object_ref (folder);
+
+	result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, gdata_documents_service_remove_entry_from_folder_async);
+	g_simple_async_result_set_op_res_gpointer (result, data, (GDestroyNotify) remove_entry_from_folder_data_free);
+	g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) remove_entry_from_folder_thread, G_PRIORITY_DEFAULT, cancellable);
+	g_object_unref (result);
+}
+
+/**
+ * gdata_documents_service_remove_entry_from_folder_finish:
+ * @self: a #GDataDocumentsService
+ * @async_result: a #GAsyncResult
+ * @error: a #GError, or %NULL
+ *
+ * Finish an asynchronous operation to remove a #GDataDocumentsEntry from a folder started with
+ * gdata_documents_service_remove_entry_from_folder_async().
+ *
+ * Return value: (transfer full): an updated #GDataDocumentsEntry, or %NULL; unref with g_object_unref()
+ *
+ * Since: 0.8.0
+ **/
+GDataDocumentsEntry *
+gdata_documents_service_remove_entry_from_folder_finish (GDataDocumentsService *self, GAsyncResult *async_result, GError **error)
+{
+	GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (async_result);
+	GDataDocumentsEntry *entry;
+
+	g_return_val_if_fail (GDATA_IS_DOCUMENTS_SERVICE (self), NULL);
+	g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), NULL);
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+	g_warn_if_fail (g_simple_async_result_get_source_tag (result) == gdata_documents_service_remove_entry_from_folder_async);
+
+	if (g_simple_async_result_propagate_error (result, error) == TRUE)
+		return NULL;
+
+	entry = g_simple_async_result_get_op_res_gpointer (result);
+	if (entry != NULL)
+		return g_object_ref (entry);
+
+	g_assert_not_reached ();
+}
+
 /**
  * gdata_documents_service_get_upload_uri:
  * @folder: (allow-none): the #GDataDocumentsFolder into which to upload the document, or %NULL
diff --git a/gdata/services/documents/gdata-documents-service.h b/gdata/services/documents/gdata-documents-service.h
index 44ddfa9..d9b2eee 100644
--- a/gdata/services/documents/gdata-documents-service.h
+++ b/gdata/services/documents/gdata-documents-service.h
@@ -110,6 +110,10 @@ GDataDocumentsEntry *gdata_documents_service_add_entry_to_folder_finish (GDataDo
 GDataDocumentsEntry *gdata_documents_service_remove_entry_from_folder (GDataDocumentsService *self, GDataDocumentsEntry *entry,
                                                                        GDataDocumentsFolder *folder, GCancellable *cancellable,
                                                                        GError **error) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
+void gdata_documents_service_remove_entry_from_folder_async (GDataDocumentsService *self, GDataDocumentsEntry *entry, GDataDocumentsFolder *folder,
+                                                             GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data);
+GDataDocumentsEntry *gdata_documents_service_remove_entry_from_folder_finish (GDataDocumentsService *self, GAsyncResult *async_result,
+                                                                              GError **error) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
 
 gchar *gdata_documents_service_get_upload_uri (GDataDocumentsFolder *folder) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
 
diff --git a/gdata/tests/documents.c b/gdata/tests/documents.c
index f5f34e2..e1023fd 100644
--- a/gdata/tests/documents.c
+++ b/gdata/tests/documents.c
@@ -558,6 +558,86 @@ test_folders_remove_from_folder (FoldersData *data, gconstpointer service)
 }
 
 static void
+setup_folders_remove_from_folder_async (FoldersAsyncData *data, gconstpointer service)
+{
+	setup_folders_remove_from_folder ((FoldersData*) data, service);
+	data->main_loop = g_main_loop_new (NULL, TRUE);
+}
+
+static void
+teardown_folders_remove_from_folder_async (FoldersAsyncData *data, gconstpointer service)
+{
+	g_main_loop_unref (data->main_loop);
+	teardown_folders_remove_from_folder ((FoldersData*) data, service);
+}
+
+static void
+test_folders_remove_from_folder_async_cb (GDataDocumentsService *service, GAsyncResult *async_result, FoldersAsyncData *data)
+{
+	GDataDocumentsEntry *entry;
+	GError *error = NULL;
+
+	entry = gdata_documents_service_remove_entry_from_folder_finish (service, async_result, &error);
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_DOCUMENTS_ENTRY (entry));
+	g_clear_error (&error);
+
+	/* Check it's still the same document */
+	g_assert_cmpstr (gdata_entry_get_title (GDATA_ENTRY (entry)), ==, gdata_entry_get_title (GDATA_ENTRY (data->data.document)));
+	g_assert (check_document_is_in_folder (GDATA_DOCUMENTS_DOCUMENT (entry), data->data.folder) == FALSE);
+
+	g_object_unref (entry);
+
+	g_main_loop_quit (data->main_loop);
+}
+
+static void
+test_folders_remove_from_folder_async (FoldersAsyncData *data, gconstpointer service)
+{
+	/* Remove the document from the folder asynchronously */
+	gdata_documents_service_remove_entry_from_folder_async (GDATA_DOCUMENTS_SERVICE (service), GDATA_DOCUMENTS_ENTRY (data->data.document),
+	                                                        data->data.folder, NULL,
+	                                                        (GAsyncReadyCallback) test_folders_remove_from_folder_async_cb, data);
+	g_main_loop_run (data->main_loop);
+}
+
+static void
+test_folders_remove_from_folder_cancellation_cb (GDataDocumentsService *service, GAsyncResult *async_result, FoldersAsyncData *data)
+{
+	GDataDocumentsEntry *entry;
+	GError *error = NULL;
+
+	entry = gdata_documents_service_remove_entry_from_folder_finish (service, async_result, &error);
+	g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
+	g_assert (entry == NULL);
+	g_clear_error (&error);
+
+	g_main_loop_quit (data->main_loop);
+}
+
+static gboolean
+test_folders_remove_from_folder_cancellation_cancel_cb (GCancellable *cancellable)
+{
+	g_cancellable_cancel (cancellable);
+	return FALSE;
+}
+
+static void
+test_folders_remove_from_folder_cancellation (FoldersAsyncData *data, gconstpointer service)
+{
+	GCancellable *cancellable = g_cancellable_new ();
+	g_timeout_add (1, (GSourceFunc) test_folders_remove_from_folder_cancellation_cancel_cb, cancellable);
+
+	/* Remove the document from the folder asynchronously and cancel the operation after a few milliseconds */
+	gdata_documents_service_remove_entry_from_folder_async (GDATA_DOCUMENTS_SERVICE (service), GDATA_DOCUMENTS_ENTRY (data->data.document),
+	                                                        data->data.folder, cancellable,
+	                                                        (GAsyncReadyCallback) test_folders_remove_from_folder_cancellation_cb, data);
+	g_main_loop_run (data->main_loop);
+
+	g_object_unref (cancellable);
+}
+
+static void
 test_upload_file_metadata_in_new_folder (gconstpointer service)
 {
 	GDataDocumentsDocument *document, *new_document;
@@ -1294,6 +1374,10 @@ main (int argc, char *argv[])
 		            test_folders_add_to_folder_cancellation, teardown_folders_add_to_folder_async);
 		g_test_add ("/documents/folders/remove_from_folder", FoldersData, service, setup_folders_remove_from_folder,
 		            test_folders_remove_from_folder, teardown_folders_remove_from_folder);
+		g_test_add ("/documents/folders/remove_from_folder/async", FoldersAsyncData, service, setup_folders_remove_from_folder_async,
+		            test_folders_remove_from_folder_async, teardown_folders_remove_from_folder_async);
+		g_test_add ("/documents/folders/remove_from_folder/cancellation", FoldersAsyncData, service, setup_folders_remove_from_folder_async,
+		            test_folders_remove_from_folder_cancellation, teardown_folders_remove_from_folder_async);
 
 		g_test_add_data_func ("/documents/batch", service, test_batch);
 		g_test_add ("/documents/batch/async", BatchAsyncData, service, setup_batch_async, test_batch_async, teardown_batch_async);



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