[libgdata] core: Load extra attributes from ‘kind ’ categories



commit 1ea060993046662974f90a22cbf3094c142ee199
Author: Philip Withnall <philip tecnocode co uk>
Date:   Wed Mar 5 23:46:30 2014 +0000

    core: Load extra attributes from ‘kind’ categories
    
    When a GDataEntry is created, it’s automatically given a ‘kind’ category
    element, as this is the way the GData protocol identifies the schema
    which applies to a given <entry>.
    
    Previously, the ‘kind’ category from parsed XML would not override the
    automatically-added category. This meant that if the XML contained extra
    attributes, such as a label, they were effectively lost.
    
    Fix this by always preferring the ‘kind’ category from the XML. Add a
    test case, because test cases are cool.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=707477

 gdata/gdata-entry.c   |   14 ++++++++++++++
 gdata/tests/general.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 0 deletions(-)
---
diff --git a/gdata/gdata-entry.c b/gdata/gdata-entry.c
index 9121555..4d6e4d9 100644
--- a/gdata/gdata-entry.c
+++ b/gdata/gdata-entry.c
@@ -882,11 +882,25 @@ gdata_entry_add_category (GDataEntry *self, GDataCategory *category)
        /* Check to see if it's a kind category and if it matches the entry's predetermined kind */
        if (g_strcmp0 (gdata_category_get_scheme (category), "http://schemas.google.com/g/2005#kind";) == 0) {
                GDataEntryClass *klass = GDATA_ENTRY_GET_CLASS (self);
+               GList *element;
 
                if (klass->kind_term != NULL && g_strcmp0 (gdata_category_get_term (category), 
klass->kind_term) != 0) {
                        g_warning ("Adding a kind category term, '%s', to an entry of kind '%s'.",
                                   gdata_category_get_term (category), klass->kind_term);
                }
+
+               /* If it is a kind category, remove the entry’s existing kind category to allow the new one
+                * to be added. This is necessary because the existing category was set in
+                * gdata_entry_constructed() and might not contain all the attributes of the actual XML
+                * category.
+                *
+                * See: https://bugzilla.gnome.org/show_bug.cgi?id=707477 */
+               element = g_list_find_custom (self->priv->categories, category, (GCompareFunc) 
gdata_comparable_compare);
+               if (element != NULL) {
+                       g_assert (GDATA_IS_CATEGORY (element->data));
+                       g_object_unref (element->data);
+                       self->priv->categories = g_list_delete_link (self->priv->categories, element);
+               }
        }
 
        /* Add the category if we don't already have it */
diff --git a/gdata/tests/general.c b/gdata/tests/general.c
index 8a19563..f7f1b55 100644
--- a/gdata/tests/general.c
+++ b/gdata/tests/general.c
@@ -539,6 +539,50 @@ test_entry_parse_xml (void)
 }
 
 static void
+test_entry_parse_xml_kind_category (void)
+{
+       GDataEntry *entry;
+       GError *error = NULL;
+
+       g_test_bug ("707477");
+
+       /* Create an entry from XML with a ‘kind’ category with extra attributes. */
+       entry = GDATA_ENTRY (gdata_parsable_new_from_xml (GDATA_TYPE_ENTRY,
+               "<entry xmlns='http://www.w3.org/2005/Atom'>"
+                       "<title type='text'>Testing kind categories</title>"
+                       "<updated>2009-01-25T14:07:37Z</updated>"
+                       "<published>2009-01-23T14:06:37Z</published>"
+                       "<content type='text'>Here we test kind categories.</content>"
+                       "<category scheme='http://schemas.google.com/g/2005#kind' "
+                                 "term='http://schemas.google.com/docs/2007#file' "
+                                 "label='application/vnd.oasis.opendocument.presentation'/>"
+                       "<category scheme='http://schemas.google.com/g/2005/labels' "
+                                 "term='http://schemas.google.com/g/2005/labels#modified-by-me' "
+                                 "label='modified-by-me'/>"
+                "</entry>", -1, &error));
+       g_assert_no_error (error);
+       g_assert (GDATA_IS_ENTRY (entry));
+       g_clear_error (&error);
+
+       /* Now check the outputted XML from the entry still has the extra attributes */
+       gdata_test_assert_xml (entry,
+                        "<?xml version='1.0' encoding='UTF-8'?>"
+                        "<entry xmlns='http://www.w3.org/2005/Atom' 
xmlns:gd='http://schemas.google.com/g/2005'>"
+                               "<title type='text'>Testing kind categories</title>"
+                               "<updated>2009-01-25T14:07:37Z</updated>"
+                               "<published>2009-01-23T14:06:37Z</published>"
+                               "<content type='text'>Here we test kind categories.</content>"
+                       "<category term='http://schemas.google.com/docs/2007#file' "
+                                 "scheme='http://schemas.google.com/g/2005#kind' "
+                                 "label='application/vnd.oasis.opendocument.presentation'/>"
+                       "<category term='http://schemas.google.com/g/2005/labels#modified-by-me' "
+                                 "scheme='http://schemas.google.com/g/2005/labels' "
+                                 "label='modified-by-me'/>"
+                        "</entry>");
+       g_object_unref (entry);
+}
+
+static void
 test_entry_parse_json (void)
 {
        GDataEntry *entry;
@@ -4463,6 +4507,7 @@ main (int argc, char *argv[])
        g_test_add_func ("/entry/get_xml", test_entry_get_xml);
        g_test_add_func ("/entry/get_json", test_entry_get_json);
        g_test_add_func ("/entry/parse_xml", test_entry_parse_xml);
+       g_test_add_func ("/entry/parse_xml/kind_category", test_entry_parse_xml_kind_category);
        g_test_add_func ("/entry/parse_json", test_entry_parse_json);
        g_test_add_func ("/entry/error_handling/xml", test_entry_error_handling_xml);
        g_test_add_func ("/entry/error_handling/json", test_entry_error_handling_json);


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