[libgdata/wip/rishi/drive: 1/2] core: Set parsed GDataAccessRule fields in JSON



commit abf9abcf9fd5189a7eef48bdc22e91bc5e3cddf0
Author: Debarshi Ray <debarshir gnome org>
Date:   Mon Apr 27 18:57:09 2015 +0200

    core: Set parsed GDataAccessRule fields in JSON
    
    It is easier to reproduce the exact JSON that was parsed if we
    duplicate the email or domain string instead of letting scope_value
    steal the pointer. The benefits of simpler logic outweigh any possible
    memory advantages that we might have had.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684920

 gdata/gdata-access-rule.c |   51 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 47 insertions(+), 4 deletions(-)
---
diff --git a/gdata/gdata-access-rule.c b/gdata/gdata-access-rule.c
index 01d550a..9092fce 100644
--- a/gdata/gdata-access-rule.c
+++ b/gdata/gdata-access-rule.c
@@ -96,6 +96,7 @@ static void gdata_access_rule_get_property (GObject *object, guint property_id,
 static gboolean parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError 
**error);
 static gboolean parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GError **error);
 static gboolean post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error);
+static void get_json (GDataParsable *parsable, JsonBuilder *builder);
 
 struct _GDataAccessRulePrivate {
        gchar *domain;
@@ -138,6 +139,7 @@ gdata_access_rule_class_init (GDataAccessRuleClass *klass)
 
        parsable_class->parse_json = parse_json;
        parsable_class->post_parse_json = post_parse_json;
+       parsable_class->get_json = get_json;
 
        entry_class->kind_term = "http://schemas.google.com/acl/2007#accessRule";;
 
@@ -531,8 +533,7 @@ post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error)
                                     "Permission type ‘group’ or ‘user’ needs an ‘emailAddress’ property.");
                        return FALSE;
                } else {
-                       priv->scope_value = priv->email;
-                       priv->email = NULL;
+                       priv->scope_value = g_strdup (priv->email);
                }
        } else if (g_strcmp0 (priv->scope_type, "domain") == 0) {
                if (priv->domain == NULL || priv->domain[0] == '\0') {
@@ -542,14 +543,56 @@ post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error)
                                     "Permission type ‘domain’ needs a ‘domain’ property.");
                        return FALSE;
                } else {
-                       priv->scope_value = priv->domain;
-                       priv->domain = NULL;
+                       priv->scope_value = g_strdup (priv->domain);
                }
        }
 
        return TRUE;
 }
 
+static void
+get_json (GDataParsable *parsable, JsonBuilder *builder)
+{
+       GDataAccessRulePrivate *priv = GDATA_ACCESS_RULE (parsable)->priv;
+
+       GDATA_PARSABLE_CLASS (gdata_access_rule_parent_class)->get_json (parsable, builder);
+
+       json_builder_set_member_name (builder, "kind");
+       json_builder_add_string_value (builder, "drive#permission");
+
+       if (priv->key != NULL && priv->key[0] != '\0') {
+               json_builder_set_member_name (builder, "authKey");
+               json_builder_add_string_value (builder, priv->key);
+       }
+
+       if (priv->email != NULL && priv->email[0] != '\0') {
+               json_builder_set_member_name (builder, "emailAddress");
+               json_builder_add_string_value (builder, priv->email);
+       }
+
+       if (priv->domain != NULL && priv->domain[0] != '\0') {
+               json_builder_set_member_name (builder, "domain");
+               json_builder_add_string_value (builder, priv->domain);
+       }
+
+       if (priv->role != NULL && priv->role[0] != '\0') {
+               json_builder_set_member_name (builder, "role");
+               json_builder_add_string_value (builder, priv->role);
+       }
+
+       if (priv->scope_type != NULL && priv->scope_type[0] != '\0') {
+               const gchar *scope_type;
+
+               if (g_strcmp0 (priv->scope_type, GDATA_ACCESS_SCOPE_DEFAULT) == 0)
+                       scope_type = "anyone";
+               else
+                       scope_type = priv->scope_type;
+
+               json_builder_set_member_name (builder, "type");
+               json_builder_add_string_value (builder, scope_type);
+       }
+}
+
 /**
  * gdata_access_rule_new:
  * @id: the access rule's ID, or %NULL


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