[discident-glib] Add _get_title() async methods



commit d3e546c258c408bab260c6997001702ce5f22a1c
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Mar 18 17:39:13 2010 +0000

    Add _get_title() async methods

 discident-glib/discident-glib.c |  165 +++++++++++++++++++++++++++++++++-----
 discident-glib/test-diglib.c    |   34 +++++++-
 2 files changed, 173 insertions(+), 26 deletions(-)
---
diff --git a/discident-glib/discident-glib.c b/discident-glib/discident-glib.c
index bbc1472..bf9386b 100644
--- a/discident-glib/discident-glib.c
+++ b/discident-glib/discident-glib.c
@@ -184,8 +184,6 @@ on_videodir_enumerate_next_files (GObject      *source_object,
 	GList *file_infos, *list, *l;
 	GError *error = NULL;
 
-	g_message ("on_videodir_enumerate_next_files");
-
 	enumerator = G_FILE_ENUMERATOR (source_object);
 	file_infos = g_file_enumerator_next_files_finish (enumerator, res, &error);
 	list = g_object_get_data (G_OBJECT (simple), "file-list");
@@ -257,8 +255,6 @@ on_videodir_enumerate_loaded (GObject      *source_object,
 	GFileEnumerator *enumerator;
 	GCancellable *cancellable;
 
-	g_message ("on_videodir_enumerate_loaded");
-
 	cancellable = g_object_get_data (G_OBJECT (simple), "cancellable");
 	video_ts_dir = G_FILE (source_object);
 	enumerator = g_file_enumerate_children_finish (video_ts_dir,
@@ -338,47 +334,50 @@ out:
 	return ret;
 }
 
+static GFile *
+get_file_for_gtin (const char *gtin)
+{
+	GFile *file;
+	char *str;
+
+	str = g_strdup_printf ("http://discident.com/v1/%s/";, gtin);
+	file = g_file_new_for_uri (str);
+	g_free (str);
+
+	return file;
+}
+
 static char *
-get_file_for_gtin (const char *gtin, GError **error)
+get_json_for_gtin (const char *gtin, GError **error)
 {
 	GFile *file;
 	char *contents;
-	char *api;
 
-	api = g_strdup_printf ("http://discident.com/v1/%s/";, gtin);
-	file = g_file_new_for_uri (api);
-	g_free (api);
+	file = get_file_for_gtin (gtin);
 
 	if (g_file_load_contents (file, NULL, &contents, NULL, NULL, NULL) == FALSE) {
 		g_object_unref (file);
 		return NULL;
 	}
-
-	/* Hmm, not that good */
-	if (g_strcmp0 (contents, "404") == 0) {
-		g_free (contents);
-		return NULL;
-	}
+	g_object_unref (file);
 
 	return contents;
 }
 
-char *
-discident_get_title_for_gtin (const char *gtin,
-			      GError    **error)
+static char *
+discident_get_title_for_json_content (const char *contents,
+				      GError    **error)
 {
 	JsonParser *parser;
 	JsonNode *root;
 	JsonObject *object;
-	char *contents, *title;
+	char *title;
 
-	contents = get_file_for_gtin (gtin, error);
-	if (contents == NULL)
+	if (g_str_equal (contents, "404") != FALSE)
 		return NULL;
 
 	parser = json_parser_new ();
 	if (json_parser_load_from_data (parser, contents, -1, error) == FALSE) {
-		g_free (contents);
 		g_object_unref (parser);
 		return NULL;
 	}
@@ -394,6 +393,23 @@ discident_get_title_for_gtin (const char *gtin,
 }
 
 char *
+discident_get_title_for_gtin (const char *gtin,
+			      GError    **error)
+{
+	char *contents, *title;
+
+	contents = get_json_for_gtin (gtin, error);
+	if (contents == NULL)
+		return NULL;
+
+	title = discident_get_title_for_json_content (contents, error);
+
+	g_free (contents);
+
+	return title;
+}
+
+char *
 discident_get_title (GFile   *directory,
 		     GError **error)
 {
@@ -410,12 +426,103 @@ discident_get_title (GFile   *directory,
 	return title;
 }
 
+static void
+on_query_data_loaded (GObject      *source_object,
+		      GAsyncResult *res,
+		      gpointer      user_data)
+{
+	GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
+	GCancellable *cancellable;
+	GFile *query;
+	GError *error = NULL;
+	char *contents, *title;
+
+	cancellable = g_object_get_data (G_OBJECT (simple), "cancellable");
+	query = G_FILE (source_object);
+	if (g_file_load_contents_finish (query,
+					 res,
+					 &contents,
+					 NULL,
+					 NULL,
+					 &error) == FALSE) {
+		g_simple_async_result_set_from_error (simple, error);
+		g_simple_async_result_complete_in_idle (simple);
+		g_object_unref (simple);
+		g_error_free (error);
+		return;
+	}
+
+	title = discident_get_title_for_json_content (contents, &error);
+	g_free (contents);
+
+	if (title == NULL && error != NULL) {
+		g_simple_async_result_set_from_error (simple, error);
+		g_simple_async_result_complete_in_idle (simple);
+		g_object_unref (simple);
+		g_error_free (error);
+		return;
+	}
+
+	g_simple_async_result_set_op_res_gpointer (simple, title, NULL);
+	g_simple_async_result_complete_in_idle (simple);
+	g_object_unref (simple);
+}
+
+static void
+on_discident_got_gtin (GObject      *source_object,
+		       GAsyncResult *res,
+		       gpointer      user_data)
+{
+	GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
+	GCancellable *cancellable;
+	GFile *directory, *query;
+	GError *error = NULL;
+	char *gtin;
+
+	cancellable = g_object_get_data (G_OBJECT (simple), "cancellable");
+	directory = G_FILE (source_object);
+	gtin = discident_get_gtin_file_finish (directory,
+					       res,
+					       &error);
+	if (gtin == NULL) {
+		g_simple_async_result_set_from_error (simple, error);
+		g_simple_async_result_complete_in_idle (simple);
+		g_object_unref (simple);
+		g_error_free (error);
+		return;
+	}
+
+	query = get_file_for_gtin (gtin);
+	g_free (gtin);
+	g_file_load_contents_async (query,
+				    cancellable,
+				    on_query_data_loaded,
+				    simple);
+	g_object_unref (query);
+}
+
 void
 discident_get_title_file_async (GFile               *directory,
 				GCancellable        *cancellable,
 				GAsyncReadyCallback  callback,
 				gpointer             user_data)
 {
+	GSimpleAsyncResult *simple;
+
+	simple = g_simple_async_result_new (G_OBJECT (directory),
+					    callback,
+					    user_data,
+					    discident_get_title_file_async);
+
+	if (cancellable != NULL) {
+		g_object_set_data_full (G_OBJECT (simple), "cancellable",
+					g_object_ref (cancellable), g_object_unref);
+	}
+
+	discident_get_gtin_file_async (directory,
+				       cancellable,
+				       on_discident_got_gtin,
+				       simple);
 }
 
 char *
@@ -423,5 +530,19 @@ discident_get_title_file_finish (GFile               *directory,
 				 GAsyncResult        *res,
 				 GError             **error)
 {
+	GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
+	char *ret;
+
+	g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == discident_get_title_file_async);
+
+	ret = NULL;
+
+	if (g_simple_async_result_propagate_error (simple, error))
+		goto out;
+
+	ret = g_simple_async_result_get_op_res_gpointer (simple);
+
+out:
+	return ret;
 }
 
diff --git a/discident-glib/test-diglib.c b/discident-glib/test-diglib.c
index 5037755..5acbc2f 100644
--- a/discident-glib/test-diglib.c
+++ b/discident-glib/test-diglib.c
@@ -74,9 +74,33 @@ test_json (void)
 }
 
 static void
-discident_print (GObject *source_object,
-		 GAsyncResult *res,
-		 gpointer user_data)
+discident_title_print (GObject *source_object,
+		      GAsyncResult *res,
+		      gpointer user_data)
+{
+	GFile *directory = G_FILE (source_object);
+	GError *error = NULL;
+	char *title;
+
+	title = discident_get_title_file_finish (directory, res, &error);
+	if (title == NULL) {
+		g_message ("Could not get disc ID: %s", error->message);
+		g_object_unref (directory);
+		g_error_free (error);
+		return;
+	}
+	g_print ("Title: %s\n", title);
+	g_free (title);
+
+	num_queries--;
+	if (num_queries == 0)
+		g_main_loop_quit (loop);
+}
+
+static void
+discident_gtin_print (GObject *source_object,
+		      GAsyncResult *res,
+		      gpointer user_data)
 {
 	GFile *directory = G_FILE (source_object);
 	GError *error = NULL;
@@ -166,7 +190,9 @@ int main (int argc, char **argv)
 			g_free (title);
 		} else {
 			num_queries++;
-			discident_get_gtin_file_async (file, NULL, discident_print, NULL);
+			discident_get_gtin_file_async (file, NULL, discident_gtin_print, NULL);
+			num_queries++;
+			discident_get_title_file_async (file, NULL, discident_title_print, NULL);
 			g_object_unref (file);
 		}
 	}



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