From ddc67f47c68584c569bb47a58a3fea16795cb7cf Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Tue, 16 Dec 2008 23:50:38 -0200 Subject: [PATCH] Category element in atom entry Now, there are categories associated to an entry. They can be added and retrieved just like retrieving authors. --- atom/entry.c | 51 ++++++++++++++++++++++++++++++++++++++++- include/atompub/entry.h | 3 +++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/atom/entry.c b/atom/entry.c index 99a4172..d4732c3 100644 --- a/atom/entry.c +++ b/atom/entry.c @@ -29,12 +29,16 @@ struct _atom_entry AtomID *id; char *title; GPtrArray *authors; + GPtrArray *categories; char *summary; }; void atom_entry_author_add (AtomEntry *, AtomPerson *); static void atom_entry_authors_delete (AtomEntry *); +void atom_entry_category_add (AtomEntry *, AtomCategory *); +static void atom_entry_categories_delete (AtomEntry *); + AtomEntry * atom_entry_new (char *title, AtomPerson *author) { @@ -44,6 +48,7 @@ atom_entry_new (char *title, AtomPerson *author) entry->id = NULL; entry->title = g_strdup (title); entry->authors = NULL; + entry->categories = NULL; atom_entry_author_add (entry, author); entry->summary = NULL; return entry; @@ -82,6 +87,8 @@ atom_entry_new_data_len (char *data, size_t len) entry->summary = g_strdup (content); else if (!xmlStrcmp (child->name, "author")) atom_entry_author_add (entry, atom_person_new_from_xmlnode (child)); + else if (!xmlStrcmp (child->name, "category")) + atom_entry_category_add (entry, atom_category_new_from_xmlnode (child)); else xmlFree (content); } @@ -99,6 +106,8 @@ atom_entry_delete (AtomEntry *entry) g_free (entry->title); if (entry->authors) atom_entry_authors_delete (entry); + if (entry->categories) + atom_entry_categories_delete (entry); if (entry->summary) g_free (entry->summary); g_slice_free (AtomEntry, entry); @@ -161,6 +170,37 @@ atom_entry_authors_delete (AtomEntry *entry) g_ptr_array_free (entry->authors, TRUE); } +void +atom_entry_categories (AtomEntry *entry, AtomCategory *** categories, + size_t *len) +{ + if (len) + *len = entry->categories->len; + if (categories) + *categories = entry->categories->pdata; +} + +void +atom_entry_category_add (AtomEntry *entry, AtomCategory *category) +{ + g_ptr_array_add (entry->categories, category); + if (entry->categories == NULL) + { + entry->categories = g_ptr_array_new (); + } + g_ptr_array_add (entry->categories, category); +} + +static void +atom_entry_categories_delete (AtomEntry *entry) +{ + size_t len = entry->categories->len; + int i; + for (i = 0; i < len; i++) + atom_category_delete (g_ptr_array_index (entry->categories, i)); + g_ptr_array_free (entry->categories, TRUE); +} + char * atom_entry_summary (AtomEntry *entry) { @@ -183,6 +223,7 @@ atom_entry_update_xmlnode (AtomEntry *entry) xmlNodePtr title; xmlNodePtr summary; xmlNodePtr author; + xmlNodePtr cat; int i; if (entry->doc == NULL) { @@ -203,7 +244,8 @@ atom_entry_update_xmlnode (AtomEntry *entry) if (!xmlStrcmp (child->name, "id") || !xmlStrcmp (child->name, "title") || !xmlStrcmp (child->name, "summary") || - !xmlStrcmp (child->name, "author")) + !xmlStrcmp (child->name, "author") || + !xmlStrcmp (child->name, "category")) { xmlUnlinkNode (child); xmlFreeNode (child); @@ -222,6 +264,13 @@ atom_entry_update_xmlnode (AtomEntry *entry) author = atom_person_to_xmlnode (person, "author"); xmlAddChild (root, author); } + for (i = 0; entry->categories && i < entry->categories->len; i++) + { + AtomCategory *category; + category = g_ptr_array_index (entry->categories, i); + cat = atom_category_to_xmlnode (category, "category"); + xmlAddChild (root, category); + } } void diff --git a/include/atompub/entry.h b/include/atompub/entry.h index 51de991..ce6bd12 100644 --- a/include/atompub/entry.h +++ b/include/atompub/entry.h @@ -22,6 +22,7 @@ #include #include +#include #include typedef struct _atom_entry AtomEntry; @@ -35,6 +36,8 @@ char * atom_entry_title (AtomEntry *); void atom_entry_title_set (AtomEntry *, char *); void atom_entry_authors (AtomEntry *, AtomPerson ***, size_t *); void atom_entry_author_add (AtomEntry *, AtomPerson *); +void atom_entry_categories (AtomEntry *, AtomCategory ***, size_t *); +void atom_entry_category_add (AtomEntry *, AtomCategory *); char * atom_entry_summary (AtomEntry *); void atom_entry_summary_set (AtomEntry *, char *); void atom_entry_string (AtomEntry *, char **, size_t *); -- 2.20.1