[libgdata] documents: Support uploads to a specific location using Drive v2



commit 9fc2d52a30e7325957624d75bbbf939cc4e0d978
Author: Debarshi Ray <debarshir gnome org>
Date:   Mon Jun 15 15:56:53 2015 +0200

    documents: Support uploads to a specific location using Drive v2
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684920

 gdata/services/documents/gdata-documents-entry.c   |   47 ++++++++++++++++++++
 gdata/services/documents/gdata-documents-service.c |   25 ++++++++--
 gdata/services/documents/gdata-documents-utils.h   |    3 +
 3 files changed, 70 insertions(+), 5 deletions(-)
---
diff --git a/gdata/services/documents/gdata-documents-entry.c 
b/gdata/services/documents/gdata-documents-entry.c
index e149cec..630d622 100644
--- a/gdata/services/documents/gdata-documents-entry.c
+++ b/gdata/services/documents/gdata-documents-entry.c
@@ -111,6 +111,7 @@
 #include "gdata-documents-presentation.h"
 #include "gdata-documents-text.h"
 #include "gdata-documents-folder.h"
+#include "gdata-documents-utils.h"
 
 static void gdata_documents_entry_access_handler_init (GDataAccessHandlerIface *iface);
 static void gdata_documents_entry_finalize (GObject *object);
@@ -121,6 +122,7 @@ static void gdata_documents_entry_get_property (GObject *object, guint property_
 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 void get_json (GDataParsable *parsable, JsonBuilder *builder);
 static gchar *get_entry_uri (const gchar *id);
 
 struct _GDataDocumentsEntryPrivate {
@@ -164,6 +166,7 @@ gdata_documents_entry_class_init (GDataDocumentsEntryClass *klass)
        parsable_class->parse_json = parse_json;
        parsable_class->post_parse_json = post_parse_json;
        parsable_class->get_content_type = get_content_type;
+       parsable_class->get_json = get_json;
        parsable_class->get_namespaces = get_namespaces;
 
        entry_class->get_entry_uri = get_entry_uri;
@@ -798,6 +801,50 @@ get_content_type (void)
 }
 
 static void
+get_json (GDataParsable *parsable, JsonBuilder *builder)
+{
+       GList *i;
+       GList *parent_folders_list;
+
+       GDATA_PARSABLE_CLASS (gdata_documents_entry_parent_class)->get_json (parsable, builder);
+
+       /* Upload to a folder: https://developers.google.com/drive/web/folder */
+
+       json_builder_set_member_name (builder, "parents");
+       json_builder_begin_array (builder);
+
+       parent_folders_list = gdata_entry_look_up_links (GDATA_ENTRY (parsable), GDATA_LINK_PARENT);
+       for (i = parent_folders_list; i != NULL; i = i->next) {
+               GDataLink *_link = GDATA_LINK (i->data);
+               const gchar *uri;
+               gsize uri_prefix_len;
+
+               /* HACK: Extract the ID from the GDataLink:uri by removing the prefix. Ignore links which
+                * don't have the prefix. */
+               uri = gdata_link_get_uri (_link);
+               uri_prefix_len = strlen (GDATA_DOCUMENTS_URI_PREFIX);
+               if (g_str_has_prefix (uri, GDATA_DOCUMENTS_URI_PREFIX)) {
+                       const gchar *id;
+
+                       id = uri + uri_prefix_len;
+                       if (id[0] != '\0') {
+                               json_builder_begin_object (builder);
+                               json_builder_set_member_name (builder, "kind");
+                               json_builder_add_string_value (builder, "drive#fileLink");
+                               json_builder_set_member_name (builder, "id");
+                               json_builder_add_string_value (builder, id);
+                               json_builder_end_object (builder);
+                       }
+               }
+       }
+
+       json_builder_end_array (builder);
+
+       g_list_free (parent_folders_list);
+
+}
+
+static void
 get_namespaces (GDataParsable *parsable, GHashTable *namespaces)
 {
        /* Chain up to the parent class */
diff --git a/gdata/services/documents/gdata-documents-service.c 
b/gdata/services/documents/gdata-documents-service.c
index 232a875..dea3aad 100644
--- a/gdata/services/documents/gdata-documents-service.c
+++ b/gdata/services/documents/gdata-documents-service.c
@@ -576,7 +576,8 @@ gdata_documents_service_query_documents_async (GDataDocumentsService *self, GDat
 
 static GDataUploadStream *
 upload_update_document (GDataDocumentsService *self, GDataDocumentsDocument *document, const gchar *slug, 
const gchar *content_type,
-                        goffset content_length, const gchar *method, const gchar *upload_uri, GCancellable 
*cancellable)
+                        GDataDocumentsFolder *folder, goffset content_length, const gchar *method, const 
gchar *upload_uri,
+                       GCancellable *cancellable)
 {
        /* HACK: Corrects a bug on spreadsheet content types handling
         * The content type for ODF spreadsheets is "application/vnd.oasis.opendocument.spreadsheet" for my 
ODF spreadsheet;
@@ -586,6 +587,20 @@ upload_update_document (GDataDocumentsService *self, GDataDocumentsDocument *doc
        if (strcmp (content_type, "application/vnd.oasis.opendocument.spreadsheet") == 0)
                content_type = "application/x-vnd.oasis.opendocument.spreadsheet";
 
+       if (folder != NULL) {
+               GDataLink *_link;
+               const gchar *id;
+               gchar *uri;
+
+               /* HACK: Build the GDataLink:uri from the ID by adding the prefix. */
+               id = gdata_entry_get_id (GDATA_ENTRY (folder));
+               uri = g_strconcat (GDATA_DOCUMENTS_URI_PREFIX, id, NULL);
+               _link = gdata_link_new (uri, GDATA_LINK_PARENT);
+               gdata_entry_add_link (GDATA_ENTRY (document), _link);
+               g_object_unref (_link);
+               g_free (uri);
+       }
+
        /* We need streaming file I/O: GDataUploadStream */
        if (content_length == -1) {
                /* Non-resumable upload. */
@@ -674,7 +689,7 @@ gdata_documents_service_upload_document (GDataDocumentsService *self, GDataDocum
 
        upload_uri_prefix = gdata_documents_service_get_upload_uri (folder);
        upload_uri = g_strconcat (upload_uri_prefix, "?uploadType=multipart", NULL);
-       upload_stream = upload_update_document (self, document, slug, content_type, -1, SOUP_METHOD_POST, 
upload_uri, cancellable);
+       upload_stream = upload_update_document (self, document, slug, content_type, folder, -1, 
SOUP_METHOD_POST, upload_uri, cancellable);
        g_free (upload_uri);
        g_free (upload_uri_prefix);
 
@@ -744,7 +759,7 @@ gdata_documents_service_upload_document_resumable (GDataDocumentsService *self,
        }
 
        upload_uri = _get_upload_uri_for_query_and_folder (query, NULL);
-       upload_stream = upload_update_document (self, document, slug, content_type, content_length, 
SOUP_METHOD_POST, upload_uri, cancellable);
+       upload_stream = upload_update_document (self, document, slug, content_type, NULL, content_length, 
SOUP_METHOD_POST, upload_uri, cancellable);
        g_free (upload_uri);
 
        return upload_stream;
@@ -815,7 +830,7 @@ gdata_documents_service_update_document (GDataDocumentsService *self, GDataDocum
        update_link = gdata_entry_look_up_link (GDATA_ENTRY (document), GDATA_LINK_EDIT_MEDIA);
        g_assert (update_link != NULL);
 
-       return upload_update_document (self, document, slug, content_type, -1, SOUP_METHOD_PUT, 
gdata_link_get_uri (update_link),
+       return upload_update_document (self, document, slug, content_type, NULL, -1, SOUP_METHOD_PUT, 
gdata_link_get_uri (update_link),
                                       cancellable);
 }
 
@@ -872,7 +887,7 @@ gdata_documents_service_update_document_resumable (GDataDocumentsService *self,
        update_link = gdata_entry_look_up_link (GDATA_ENTRY (document), GDATA_LINK_RESUMABLE_EDIT_MEDIA);
        g_assert (update_link != NULL);
 
-       return upload_update_document (self, document, slug, content_type, content_length, SOUP_METHOD_PUT, 
gdata_link_get_uri (update_link),
+       return upload_update_document (self, document, slug, content_type, NULL, content_length, 
SOUP_METHOD_PUT, gdata_link_get_uri (update_link),
                                       cancellable);
 }
 
diff --git a/gdata/services/documents/gdata-documents-utils.h 
b/gdata/services/documents/gdata-documents-utils.h
index 66a058b..01cc460 100644
--- a/gdata/services/documents/gdata-documents-utils.h
+++ b/gdata/services/documents/gdata-documents-utils.h
@@ -25,6 +25,9 @@
 
 G_BEGIN_DECLS
 
+/* HACK: Used to convert GDataLink:uri to ID and vice-versa. */
+#define GDATA_DOCUMENTS_URI_PREFIX "https://www.googleapis.com/drive/v2/files/";
+
 G_GNUC_INTERNAL GType gdata_documents_utils_get_type_from_content_type (const gchar *content_type);
 
 G_END_DECLS


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