[gnome-software/wip/temp/ubuntu-xenial-rebased-corrected: 119/331] Use g_autoptr for Json code



commit dc05f76229a454df848100aa652c4d533a0167d2
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Mar 1 21:58:58 2016 +1300

    Use g_autoptr for Json code

 src/plugins/gs-plugin-ubuntu-reviews.c |   35 ++++++++++++++-----------------
 1 files changed, 16 insertions(+), 19 deletions(-)
---
diff --git a/src/plugins/gs-plugin-ubuntu-reviews.c b/src/plugins/gs-plugin-ubuntu-reviews.c
index 01e51cf..da62250 100644
--- a/src/plugins/gs-plugin-ubuntu-reviews.c
+++ b/src/plugins/gs-plugin-ubuntu-reviews.c
@@ -34,6 +34,13 @@
 
 #include "gs-os-release.h"
 
+// Fixes in json-glib >= 1.1.1
+#ifndef JsonParser_autoptr
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(JsonParser, g_object_unref)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(JsonBuilder, g_object_unref)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(JsonGenerator, g_object_unref)
+#endif
+
 #define SCHEMA_NAME     "com.ubuntu.UbuntuOne.GnomeSoftware"
 #define CONSUMER_KEY    "consumer-key"
 #define CONSUMER_SECRET "consumer-secret"
@@ -615,30 +622,25 @@ get_review_stats (GsPlugin *plugin,
 static gboolean
 parse_histogram (const gchar *text, Histogram *histogram)
 {
-       JsonParser *parser = NULL;
+       g_autoptr(JsonParser) parser = NULL;
        JsonArray *array;
-       gboolean result = FALSE;
 
        /* Histogram is a five element JSON array, e.g. "[1, 3, 5, 8, 4]" */
        parser = json_parser_new ();
        if (!json_parser_load_from_data (parser, text, -1, NULL))
-               goto out;
+               return FALSE;
        if (!JSON_NODE_HOLDS_ARRAY (json_parser_get_root (parser)))
-               goto out;
+               return FALSE;
        array = json_node_get_array (json_parser_get_root (parser));
        if (json_array_get_length (array) != 5)
-               goto out;
+               return FALSE;
        histogram->one_star_count = json_array_get_int_element (array, 0);
        histogram->two_star_count = json_array_get_int_element (array, 1);
        histogram->three_star_count = json_array_get_int_element (array, 2);
        histogram->four_star_count = json_array_get_int_element (array, 3);
        histogram->five_star_count = json_array_get_int_element (array, 4);
-       result = TRUE;
-
-out:
-       g_clear_object (&parser);
 
-       return result;
+       return TRUE;
 }
 
 static gboolean
@@ -726,7 +728,7 @@ send_review_request (GsPlugin *plugin, const gchar *method, const gchar *path, J
        msg = soup_message_new (method, uri);
 
        if (request != NULL) {
-               JsonGenerator *generator;
+               g_autoptr(JsonGenerator) generator = NULL;
                gchar *data;
                gsize length;
 
@@ -734,7 +736,6 @@ send_review_request (GsPlugin *plugin, const gchar *method, const gchar *path, J
                json_generator_set_root (generator, json_builder_get_root (request));
                data = json_generator_to_data (generator, &length);
                soup_message_set_request (msg, "application/json", SOUP_MEMORY_TAKE, data, length);
-               g_object_unref (generator);
        }
 
        if (do_sign)
@@ -756,7 +757,7 @@ send_review_request (GsPlugin *plugin, const gchar *method, const gchar *path, J
        }
 
        if (result != NULL) {
-               JsonParser *parser;
+               g_autoptr(JsonParser) parser = NULL;
                const gchar *content_type;
 
                content_type = soup_message_headers_get_content_type (msg->response_headers, NULL);
@@ -771,7 +772,6 @@ send_review_request (GsPlugin *plugin, const gchar *method, const gchar *path, J
 
                parser = json_parser_new ();
                if (!json_parser_load_from_data (parser, msg->response_body->data, -1, error)) {
-                       g_object_unref (parser);
                        return FALSE;
                }
                *result = parser;
@@ -785,16 +785,13 @@ download_review_stats (GsPlugin *plugin, GError **error)
 {
        g_autofree gchar *uri = NULL;
        g_autoptr(SoupMessage) msg = NULL;
-       JsonParser *result;
-       gboolean ret;
+       g_autoptr(JsonParser) result = NULL;
 
        if (!send_review_request (plugin, SOUP_METHOD_GET, "/api/1.0/review-stats/any/any/", NULL, FALSE, 
&result, error))
                return FALSE;
 
        /* Extract the stats from the data */
-       ret = parse_review_entries (plugin, result, error);
-       g_object_unref (result);
-       if (!ret)
+       if (!parse_review_entries (plugin, result, error))
                return FALSE;
 
        /* Record the time we downloaded it */


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