[gnome-software/wip/rancell/reviews] Use g_autoptr for Json code
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/wip/rancell/reviews] Use g_autoptr for Json code
- Date: Tue, 1 Mar 2016 08:59:39 +0000 (UTC)
commit 77e7c70287b36f40f53616abd18f1ec00e2868cb
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 | 59 ++++++++++++-------------------
1 files changed, 23 insertions(+), 36 deletions(-)
---
diff --git a/src/plugins/gs-plugin-ubuntu-reviews.c b/src/plugins/gs-plugin-ubuntu-reviews.c
index b7077b4..b205f16 100644
--- a/src/plugins/gs-plugin-ubuntu-reviews.c
+++ b/src/plugins/gs-plugin-ubuntu-reviews.c
@@ -35,6 +35,13 @@
#include "gs-ubuntu-login-dialog.h"
#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"
@@ -236,30 +243,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
@@ -347,7 +349,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;
@@ -355,7 +357,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)
@@ -377,7 +378,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);
@@ -392,7 +393,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;
@@ -406,16 +406,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 */
@@ -596,15 +593,12 @@ parse_reviews (GsPlugin *plugin, JsonParser *parser, GsApp *app, GError **error)
return FALSE;
array = json_node_get_array (json_parser_get_root (parser));
for (i = 0; i < json_array_get_length (array); i++) {
- GsReview *review;
+ g_autoptr(GsReview) review = NULL;
/* Read in from JSON... (skip bad entries) */
review = parse_review (json_array_get_element (array, i));
- if (!review)
- continue;
-
- gs_app_add_review (app, review);
- g_object_unref (review);
+ if (review)
+ gs_app_add_review (app, review);
}
return TRUE;
@@ -628,8 +622,7 @@ static gboolean
download_reviews (GsPlugin *plugin, GsApp *app, const gchar *package_name, GError **error)
{
g_autofree gchar *language = NULL, *path = NULL;
- JsonParser *result;
- gboolean ret;
+ g_autoptr(JsonParser) result = NULL;
/* Get the review stats using HTTP */
// FIXME: This will only get the first page of reviews
@@ -639,10 +632,7 @@ download_reviews (GsPlugin *plugin, GsApp *app, const gchar *package_name, GErro
return FALSE;
/* Extract the stats from the data */
- ret = parse_reviews (plugin, result, app, error);
- g_object_unref (result);
-
- return ret;
+ return parse_reviews (plugin, result, app, error);
}
static gboolean
@@ -768,8 +758,7 @@ set_package_review (GsPlugin *plugin,
gint rating;
gint n_stars;
g_autofree gchar *os_id = NULL, *os_ubuntu_codename = NULL, *language = NULL, *architecture = NULL;
- JsonBuilder *request;
- gboolean ret;
+ g_autoptr(JsonBuilder) request = NULL;
/* Ubuntu reviews require a summary and description - just make one up for now */
rating = gs_review_get_rating (review);
@@ -809,10 +798,8 @@ set_package_review (GsPlugin *plugin,
add_int_member (request, "rating", n_stars);
add_string_member (request, "arch_tag", architecture);
json_builder_end_object (request);
- ret = send_review_request (plugin, SOUP_METHOD_POST, "/api/1.0/reviews/", request, TRUE, NULL, error);
- g_object_unref (request);
- return ret;
+ return send_review_request (plugin, SOUP_METHOD_POST, "/api/1.0/reviews/", request, TRUE, NULL,
error);
}
typedef struct
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]