[libgdata/wip/rishi/drive: 1/10] documents: Split out the code to get an ID from a GDataLink



commit 450943ccb4bcfef5d536acd2e22bd9cb03f5bfc5
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Sep 30 11:33:08 2016 +0200

    documents: Split out the code to get an ID from a GDataLink
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684920

 gdata/services/documents/gdata-documents-entry.c   |   30 ++++++-----------
 gdata/services/documents/gdata-documents-service.c |   21 +++---------
 gdata/services/documents/gdata-documents-utils.c   |   34 ++++++++++++++++++++
 gdata/services/documents/gdata-documents-utils.h   |    2 +
 4 files changed, 52 insertions(+), 35 deletions(-)
---
diff --git a/gdata/services/documents/gdata-documents-entry.c 
b/gdata/services/documents/gdata-documents-entry.c
index 32f51ef..96d0224 100644
--- a/gdata/services/documents/gdata-documents-entry.c
+++ b/gdata/services/documents/gdata-documents-entry.c
@@ -97,7 +97,6 @@
 #include <config.h>
 #include <glib.h>
 #include <glib/gi18n-lib.h>
-#include <string.h>
 
 #include "gdata-documents-entry.h"
 #include "gdata-parser.h"
@@ -833,25 +832,16 @@ get_json (GDataParsable *parsable, JsonBuilder *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);
-                       }
+               const gchar *id;
+
+               id = gdata_documents_utils_get_id_from_link (_link);
+               if (id != NULL) {
+                       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);
                }
        }
 
diff --git a/gdata/services/documents/gdata-documents-service.c 
b/gdata/services/documents/gdata-documents-service.c
index 2e7b146..57c3283 100644
--- a/gdata/services/documents/gdata-documents-service.c
+++ b/gdata/services/documents/gdata-documents-service.c
@@ -990,21 +990,12 @@ gdata_documents_service_copy_document (GDataDocumentsService *self, GDataDocumen
        parent_folders_list = gdata_entry_look_up_links (GDATA_ENTRY (document), 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') {
-                               parent_id = id;
-                               break;
-                       }
+               const gchar *id;
+
+               id = gdata_documents_utils_get_id_from_link (_link);
+               if (id != NULL) {
+                       parent_id = id;
+                       break;
                }
        }
 
diff --git a/gdata/services/documents/gdata-documents-utils.c 
b/gdata/services/documents/gdata-documents-utils.c
index 2f8cb8b..af588c9 100644
--- a/gdata/services/documents/gdata-documents-utils.c
+++ b/gdata/services/documents/gdata-documents-utils.c
@@ -17,6 +17,8 @@
  * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <string.h>
+
 #include "gdata-documents-spreadsheet.h"
 #include "gdata-documents-text.h"
 #include "gdata-documents-presentation.h"
@@ -118,3 +120,35 @@ gdata_documents_utils_get_content_type (GDataDocumentsEntry *entry)
 
        return retval;
 }
+
+/*
+ * gdata_documents_utils_get_id_from_link:
+ * @_link: a #GDataLink
+ *
+ * Returns the ID, if any, of the #GDataEntry pointed to by @_link.
+ *
+ * Return value: (nullable): ID of @_link, %NULL otherwise
+ *
+ * Since: UNRELEASED
+ */
+const gchar *
+gdata_documents_utils_get_id_from_link (GDataLink *_link)
+{
+       const gchar *retval = NULL;
+       const gchar *uri;
+
+       /* 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);
+       if (g_str_has_prefix (uri, GDATA_DOCUMENTS_URI_PREFIX)) {
+               const gchar *id;
+               gsize uri_prefix_len;
+
+               uri_prefix_len = strlen (GDATA_DOCUMENTS_URI_PREFIX);
+               id = uri + uri_prefix_len;
+               if (id[0] != '\0')
+                       retval = id;
+       }
+
+       return retval;
+}
diff --git a/gdata/services/documents/gdata-documents-utils.h 
b/gdata/services/documents/gdata-documents-utils.h
index 2e11bb8..48625d0 100644
--- a/gdata/services/documents/gdata-documents-utils.h
+++ b/gdata/services/documents/gdata-documents-utils.h
@@ -36,6 +36,8 @@ G_GNUC_INTERNAL GType gdata_documents_utils_get_type_from_content_type (const gc
 
 G_GNUC_INTERNAL const gchar *gdata_documents_utils_get_content_type (GDataDocumentsEntry *entry);
 
+G_GNUC_INTERNAL const gchar *gdata_documents_utils_get_id_from_link (GDataLink *_link);
+
 G_END_DECLS
 
 #endif /* !GDATA_DOCUMENTS_UTILS_H */


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