[libgdata/wip/rishi/drive: 7/8] core: Parse owners from JSON



commit 1515381cb6b5cf2943010a77cabf901eb0c43ae9
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Apr 2 18:06:10 2015 +0200

    core: Parse owners from JSON

 gdata/gdata-entry.c |  106 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 0 deletions(-)
---
diff --git a/gdata/gdata-entry.c b/gdata/gdata-entry.c
index b5c99cf..1d72633 100644
--- a/gdata/gdata-entry.c
+++ b/gdata/gdata-entry.c
@@ -590,6 +590,60 @@ get_entry_uri (const gchar *id)
        return g_strdup (id);
 }
 
+static void
+get_kind_email_and_name (JsonReader *reader, gchar **out_kind, gchar **out_email, gchar **out_name)
+{
+       GError *error = NULL;
+       gboolean success;
+       gchar *email = NULL;
+       gchar *kind = NULL;
+       gchar *name = NULL;
+       gint i, members;
+
+       for (i = 0, members = json_reader_count_members (reader); i < members; i++) {
+               json_reader_read_element (reader, i);
+
+               gdata_parser_string_from_json_member (reader, "kind", P_REQUIRED | P_NON_EMPTY, &kind, 
&success, &error);
+               if (!success && error != NULL) {
+                       g_warning ("Error parsing JSON: Trying to find ‘kind’: %s", error->message);
+                       g_clear_error (&error);
+               }
+
+               gdata_parser_string_from_json_member (reader, "displayName", P_REQUIRED | P_NON_EMPTY, &name, 
&success, &error);
+               if (!success && error != NULL) {
+                       g_warning ("Error parsing JSON: Trying to find ‘displayName’: %s", error->message);
+                       g_clear_error (&error);
+               }
+
+               gdata_parser_string_from_json_member (reader, "emailAddress", P_DEFAULT, &email, &success, 
&error);
+               if (!success && error != NULL) {
+                       g_warning ("Error parsing JSON: Trying to find ‘emailAddress’: %s", error->message);
+                       g_clear_error (&error);
+               }
+
+               json_reader_end_element (reader);
+       }
+
+       if (out_kind != NULL) {
+               *out_kind = kind;
+               kind = NULL;
+       }
+
+       if (out_email != NULL) {
+               *out_email = email;
+               email = NULL;
+       }
+
+       if (out_name != NULL) {
+               *out_name = name;
+               name = NULL;
+       }
+
+       g_free (kind);
+       g_free (email);
+       g_free (name);
+}
+
 static gboolean
 parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GError **error)
 {
@@ -647,6 +701,58 @@ parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GEr
                g_object_unref (category);
 
                return TRUE;
+       } else if (g_strcmp0 (json_reader_get_member_name (reader), "owners") == 0) {
+               gint i, elements;
+
+               if (json_reader_is_array (reader) == FALSE) {
+                       g_set_error (error, GDATA_PARSER_ERROR, GDATA_PARSER_ERROR_PARSING_STRING,
+                                    /* Translators: the parameter is an error message */
+                                    _("Error parsing JSON: %s"),
+                                    _("JSON node ‘owners’ is not an array."));
+                       return FALSE;
+               }
+
+               /* Loop through the elements array. */
+               for (i = 0, elements = json_reader_count_elements (reader); i < elements; i++) {
+                       gchar *email = NULL;
+                       gchar *kind = NULL;
+                       gchar *name = NULL;
+
+                       json_reader_read_element (reader, i);
+
+                       if (json_reader_is_object (reader) == FALSE) {
+                               g_warning ("Error parsing JSON: JSON node inside ‘owners’ is not an object");
+                               goto continuation;
+                       }
+
+                       get_kind_email_and_name (reader, &kind, &email, &name);
+                       if (kind == NULL) {
+                               g_warning ("Error parsing JSON: Failed to find ‘kind’");
+                               goto continuation;
+                       }
+                       if (name == NULL) {
+                               g_warning ("Error parsing JSON: Failed to find ‘displayName’");
+                               goto continuation;
+                       }
+
+                       if (g_strcmp0 (kind, "drive#user") == 0) {
+                               GDataAuthor *author;
+
+                               author = gdata_author_new (name, NULL, email);
+                               gdata_entry_add_author (GDATA_ENTRY (parsable), author);
+                               g_object_unref (author);
+                       } else {
+                               g_message ("%s parents are not handled yet", kind);
+                       }
+
+               continuation:
+                       g_free (email);
+                       g_free (kind);
+                       g_free (name);
+                       json_reader_end_element (reader);
+               }
+
+               return TRUE;
        }
 
        return GDATA_PARSABLE_CLASS (gdata_entry_parent_class)->parse_json (parsable, reader, user_data, 
error);


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