[libgdata/wip/rishi/drive: 6/8] core: Add support for JSON to GDataAccessRule



commit 501a868a6f87e0a34e3467559c0eaef497d752f5
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Apr 16 15:14:22 2015 +0200

    core: Add support for JSON to GDataAccessRule
    
    The ‘edited’ property is left unused.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684920

 gdata/gdata-access-rule.c |   69 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)
---
diff --git a/gdata/gdata-access-rule.c b/gdata/gdata-access-rule.c
index 1d1c595..2fb4b88 100644
--- a/gdata/gdata-access-rule.c
+++ b/gdata/gdata-access-rule.c
@@ -3,6 +3,7 @@
  * GData Client
  * Copyright (C) Thibault Saunier 2009 <saunierthibault gmail com>
  * Copyright (C) Philip Withnall 2009–2010, 2014 <philip tecnocode co uk>
+ * Copyright (C) Red Hat, Inc. 2015
  *
  * GData Client is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -93,8 +94,12 @@ static void get_xml (GDataParsable *parsable, GString *xml_string);
 static void gdata_access_rule_set_property (GObject *object, guint property_id, const GValue *value, 
GParamSpec *pspec);
 static void gdata_access_rule_get_property (GObject *object, guint property_id, GValue *value, GParamSpec 
*pspec);
 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);
 
 struct _GDataAccessRulePrivate {
+       gchar *domain;
+       gchar *email;
        gchar *role;
        gchar *scope_type;
        gchar *scope_value;
@@ -131,6 +136,9 @@ gdata_access_rule_class_init (GDataAccessRuleClass *klass)
        parsable_class->get_xml = get_xml;
        parsable_class->get_namespaces = get_namespaces;
 
+       parsable_class->parse_json = parse_json;
+       parsable_class->post_parse_json = post_parse_json;
+
        entry_class->kind_term = "http://schemas.google.com/acl/2007#accessRule";;
 
        /**
@@ -275,6 +283,8 @@ gdata_access_rule_finalize (GObject *object)
 {
        GDataAccessRulePrivate *priv = GDATA_ACCESS_RULE (object)->priv;
 
+       g_free (priv->domain);
+       g_free (priv->email);
        g_free (priv->role);
        g_free (priv->scope_type);
        g_free (priv->scope_value);
@@ -481,6 +491,65 @@ get_namespaces (GDataParsable *parsable, GHashTable *namespaces)
        g_hash_table_insert (namespaces, (gchar*) "gAcl", (gchar*) "http://schemas.google.com/acl/2007";);
 }
 
+static gboolean
+parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GError **error)
+{
+       GDataAccessRulePrivate *priv = GDATA_ACCESS_RULE (parsable)->priv;
+       gboolean success;
+       gchar *scope_type = NULL;
+
+       if (gdata_parser_string_from_json_member (reader, "authKey", P_REQUIRED | P_NON_EMPTY, &(priv->key), 
&success, error) == TRUE ||
+           gdata_parser_string_from_json_member (reader, "emailAddress", P_REQUIRED | P_NON_EMPTY, 
&(priv->email), &success, error) == TRUE ||
+           gdata_parser_string_from_json_member (reader, "domain", P_REQUIRED | P_NON_EMPTY, 
&(priv->domain), &success, error) == TRUE ||
+           gdata_parser_string_from_json_member (reader, "role", P_REQUIRED | P_NON_EMPTY, &(priv->role), 
&success, error) == TRUE) {
+               return success;
+       } else if (gdata_parser_string_from_json_member (reader, "type", P_REQUIRED | P_NON_EMPTY, 
&scope_type, &success, error) == TRUE) {
+               if (g_strcmp0 (scope_type, "anyone") == 0) {
+                       priv->scope_type = g_strdup (GDATA_ACCESS_SCOPE_DEFAULT);
+               } else {
+                       priv->scope_type = scope_type;
+                       scope_type = NULL;
+               }
+
+               g_free (scope_type);
+               return success;
+       }
+
+       return GDATA_PARSABLE_CLASS (gdata_access_rule_parent_class)->parse_json (parsable, reader, 
user_data, error);
+}
+
+static gboolean
+post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error)
+{
+       GDataAccessRulePrivate *priv = GDATA_ACCESS_RULE (parsable)->priv;
+
+       if (g_strcmp0 (priv->scope_type, "group") == 0 || g_strcmp0 (priv->scope_type, "user") == 0) {
+               if (priv->email == NULL || priv->email[0] == '\0') {
+                       g_set_error (error, GDATA_PARSER_ERROR, GDATA_PARSER_ERROR_PARSING_STRING,
+                                    /* Translators: the parameter is an error message */
+                                    _("Error parsing JSON: %s"),
+                                    _("Permission type ‘group’ or ‘user’ needs an ‘emailAddress’ 
property."));
+                       return FALSE;
+               } else {
+                       priv->scope_value = priv->email;
+                       priv->email = NULL;
+               }
+       } else if (g_strcmp0 (priv->scope_type, "domain") == 0) {
+               if (priv->domain == NULL || priv->domain[0] == '\0') {
+                       g_set_error (error, GDATA_PARSER_ERROR, GDATA_PARSER_ERROR_PARSING_STRING,
+                                    /* Translators: the parameter is an error message */
+                                    _("Error parsing JSON: %s"),
+                                    _("Permission type ‘domain’ needs a ‘domain’ property."));
+                       return FALSE;
+               } else {
+                       priv->scope_value = priv->domain;
+                       priv->domain = NULL;
+               }
+       }
+
+       return TRUE;
+}
+
 /**
  * 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]