[libgdata/wip/rishi/drive: 3/4] documents: Add support for parsing Drive v2 files



commit a55620917322cb37be8a23bffd0627e6ea1bf577
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Mar 31 16:04:33 2015 +0200

    documents: Add support for parsing Drive v2 files

 gdata/services/documents/gdata-documents-entry.c |   86 +++++++++++++++-------
 1 files changed, 60 insertions(+), 26 deletions(-)
---
diff --git a/gdata/services/documents/gdata-documents-entry.c 
b/gdata/services/documents/gdata-documents-entry.c
index 7277cc5..9dd019c 100644
--- a/gdata/services/documents/gdata-documents-entry.c
+++ b/gdata/services/documents/gdata-documents-entry.c
@@ -2,6 +2,7 @@
 /*
  * GData Client
  * Copyright (C) Thibault Saunier 2009 <saunierthibault gmail com>
+ * 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
@@ -119,14 +120,17 @@ static void get_namespaces (GDataParsable *parsable, GHashTable *namespaces);
 static void get_xml (GDataParsable *parsable, GString *xml_string);
 static void gdata_documents_entry_get_property (GObject *object, guint property_id, GValue *value, 
GParamSpec *pspec);
 static void gdata_documents_entry_set_property (GObject *object, guint property_id, const GValue *value, 
GParamSpec *pspec);
+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 gboolean parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError 
**error);
 static gchar *get_entry_uri (const gchar *id);
 
-static const gchar *_get_untyped_resource_id (GDataDocumentsEntry *self) G_GNUC_PURE;
-
 struct _GDataDocumentsEntryPrivate {
        gint64 edited;
        gint64 last_viewed;
+       gchar *document_id;
+       gchar *kind;
+       gchar *mime_type;
        gchar *resource_id;
        gboolean writers_can_invite;
        gboolean is_deleted;
@@ -164,6 +168,8 @@ gdata_documents_entry_class_init (GDataDocumentsEntryClass *klass)
        gobject_class->finalize = gdata_documents_entry_finalize;
        gobject_class->dispose = gdata_entry_dispose;
 
+       parsable_class->parse_json = parse_json;
+       parsable_class->post_parse_json = post_parse_json;
        parsable_class->parse_xml = parse_xml;
        parsable_class->get_xml = get_xml;
        parsable_class->get_namespaces = get_namespaces;
@@ -374,6 +380,9 @@ gdata_documents_entry_finalize (GObject *object)
 {
        GDataDocumentsEntryPrivate *priv = GDATA_DOCUMENTS_ENTRY (object)->priv;
 
+       g_free (priv->document_id);
+       g_free (priv->kind);
+       g_free (priv->mime_type);
        g_free (priv->resource_id);
 
        /* Chain up to the parent class */
@@ -390,7 +399,7 @@ gdata_documents_entry_get_property (GObject *object, guint property_id, GValue *
                        g_value_set_string (value, priv->resource_id);
                        break;
                case PROP_DOCUMENT_ID:
-                       g_value_set_string (value, _get_untyped_resource_id (GDATA_DOCUMENTS_ENTRY (object)));
+                       g_value_set_string (value, priv->document_id);
                        break;
                case PROP_WRITERS_CAN_INVITE:
                        g_value_set_boolean (value, priv->writers_can_invite);
@@ -454,6 +463,52 @@ gdata_documents_entry_set_property (GObject *object, guint property_id, const GV
 }
 
 static gboolean
+parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GError **error)
+{
+       gboolean success;
+       GDataDocumentsEntryPrivate *priv = GDATA_DOCUMENTS_ENTRY (parsable)->priv;
+       gint64 updated;
+
+       if (gdata_parser_string_from_json_member (reader, "id", P_REQUIRED | P_NON_EMPTY, 
&(priv->document_id), &success, error) == TRUE ||
+           gdata_parser_string_from_json_member (reader, "mimeType", P_DEFAULT, &(priv->mime_type), 
&success, error) == TRUE) {
+               return success;
+       } else if (gdata_parser_string_from_json_member (reader, "kind", P_DEFAULT, &(priv->kind), &success, 
error) == TRUE) {
+               /* Ignore */
+       } else if (gdata_parser_int64_time_from_json_member (reader, "modifiedDate", P_DEFAULT, &updated, 
&success, error) == TRUE) {
+               if (success)
+                       _gdata_entry_set_updated (GDATA_ENTRY (parsable), updated);
+               return success;
+       } else {
+               return GDATA_PARSABLE_CLASS (gdata_documents_entry_parent_class)->parse_json (parsable, 
reader, user_data, error);
+       }
+
+       return TRUE;
+}
+
+static gboolean
+post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error)
+{
+       GDataDocumentsEntry *self = GDATA_DOCUMENTS_ENTRY (parsable);
+       GDataDocumentsEntryPrivate *priv = self->priv;
+
+       g_message ("DocumentsEntry: post_parse_json");
+       if (priv->document_id != NULL)
+               priv->resource_id = g_strconcat ("document:", priv->document_id, NULL);
+
+       g_message ("DocumentsEntry: setting category");
+       if (priv->mime_type != NULL && priv->mime_type[0] != '\0') {
+               GDataCategory *category;
+               GDataEntryClass *klass = GDATA_ENTRY_GET_CLASS (self);
+
+               category = gdata_category_new (klass->kind_term, "http://schemas.google.com/g/2005#kind";, 
priv->mime_type);
+               gdata_entry_add_category (GDATA_ENTRY (parsable), category);
+               g_object_unref (category);
+       }
+
+       return TRUE;
+}
+
+static gboolean
 parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError **error)
 {
        gboolean success;
@@ -634,31 +689,11 @@ gdata_documents_entry_get_path (GDataDocumentsEntry *self)
        }
 
        /* Append the document ID */
-       g_string_append (path, _get_untyped_resource_id (self));
+       g_string_append (path, self->priv->document_id);
 
        return g_string_free (path, FALSE);
 }
 
-/* Static version so that we can use it internally without triggering deprecation warnings.
- * Note that this is what libgdata used to call a "document ID". */
-static const gchar *
-_get_untyped_resource_id (GDataDocumentsEntry *self)
-{
-       const gchar *colon;
-
-       /* Untyped resource ID should be NULL iff resource ID is. */
-       if (self->priv->resource_id == NULL) {
-               return NULL;
-       }
-
-       /* Resource ID is of the form "document:[untyped_resource_id]" (or 
"spreadsheet:[untyped_resource_id]", etc.),
-        * so we want to return the portion after the colon. */
-       colon = g_utf8_strchr (self->priv->resource_id, -1, ':');
-       g_assert (colon != NULL);
-
-       return colon + 1;
-}
-
 /**
  * gdata_documents_entry_get_document_id:
  * @self: a #GDataDocumentsEntry
@@ -676,8 +711,7 @@ const gchar *
 gdata_documents_entry_get_document_id (GDataDocumentsEntry *self )
 {
        g_return_val_if_fail (GDATA_IS_DOCUMENTS_ENTRY (self), NULL);
-
-       return _get_untyped_resource_id (self);
+       return self->priv->document_id;
 }
 
 /**


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