2 * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <atompub/atom.h>
23 #include <libxml/tree.h>
24 #include <libxml/parser.h>
32 GPtrArray *categories;
36 void atom_entry_author_add (AtomEntry *, AtomPerson *);
37 static void atom_entry_authors_delete (AtomEntry *);
39 void atom_entry_category_add (AtomEntry *, AtomCategory *);
40 static void atom_entry_categories_delete (AtomEntry *);
43 atom_entry_new (char *id, char *title, AtomPerson *author)
46 entry = g_slice_new (AtomEntry);
48 entry->id = atom_id_new (id);
49 entry->title = g_strdup (title);
50 entry->authors = NULL;
51 entry->categories = NULL;
52 atom_entry_author_add (entry, author);
53 entry->summary = NULL;
58 atom_entry_new_data_len (char *data, size_t len)
63 entry = g_slice_new0 (AtomEntry);
64 entry->doc = xmlReadMemory (data, len, NULL, NULL,
65 XML_PARSE_RECOVER | XML_PARSE_NOERROR);
66 if (entry->doc == NULL ||
67 (root = xmlDocGetRootElement (entry->doc)) == NULL)
69 g_slice_free (AtomEntry, entry);
72 if (xmlStrcmp (root->name, "entry"))
74 xmlFreeDoc (entry->doc);
75 g_slice_free (AtomEntry, entry);
78 for (child = root->xmlChildrenNode; child != NULL; child = child->next)
81 content = xmlNodeGetContent (child->xmlChildrenNode);
82 if (!xmlStrcmp (child->name, "id"))
83 entry->id = atom_id_new (content);
84 else if (!xmlStrcmp (child->name, "title"))
85 entry->title = g_strdup (content);
86 else if (!xmlStrcmp (child->name, "summary"))
87 entry->summary = g_strdup (content);
88 else if (!xmlStrcmp (child->name, "author"))
89 atom_entry_author_add (entry, atom_person_new_from_xmlnode (child));
90 else if (!xmlStrcmp (child->name, "category"))
91 atom_entry_category_add (entry, atom_category_new_from_xmlnode (child));
95 if (entry->id == NULL || entry->title == NULL || entry->authors == NULL)
97 atom_entry_delete (entry);
104 atom_entry_delete (AtomEntry *entry)
107 xmlFreeDoc (entry->doc);
109 atom_id_delete (entry->id);
111 g_free (entry->title);
113 atom_entry_authors_delete (entry);
114 if (entry->categories)
115 atom_entry_categories_delete (entry);
117 g_free (entry->summary);
118 g_slice_free (AtomEntry, entry);
122 atom_entry_id (AtomEntry *entry)
128 atom_entry_id_set (AtomEntry *entry, AtomID *id)
133 atom_id_delete (entry->id);
138 atom_entry_title (AtomEntry *entry)
144 atom_entry_title_set (AtomEntry *entry, char *title)
150 entry->title = g_strdup (title);
154 atom_entry_authors (AtomEntry *entry, AtomPerson *** authors, size_t *len)
157 *len = entry->authors->len;
159 *authors = entry->authors->pdata;
163 atom_entry_author_add (AtomEntry *entry, AtomPerson *author)
165 if (entry->authors == NULL)
167 entry->authors = g_ptr_array_new ();
169 g_ptr_array_add (entry->authors, author);
173 atom_entry_authors_delete (AtomEntry *entry)
175 size_t len = entry->authors->len;
177 for (i = 0; i < len; i++)
178 atom_person_delete (g_ptr_array_index (entry->authors, i));
179 g_ptr_array_free (entry->authors, TRUE);
183 atom_entry_categories (AtomEntry *entry, AtomCategory *** categories,
187 *len = entry->categories->len;
189 *categories = entry->categories->pdata;
193 atom_entry_category_add (AtomEntry *entry, AtomCategory *category)
195 g_ptr_array_add (entry->categories, category);
196 if (entry->categories == NULL)
198 entry->categories = g_ptr_array_new ();
200 g_ptr_array_add (entry->categories, category);
204 atom_entry_categories_delete (AtomEntry *entry)
206 size_t len = entry->categories->len;
208 for (i = 0; i < len; i++)
209 atom_category_delete (g_ptr_array_index (entry->categories, i));
210 g_ptr_array_free (entry->categories, TRUE);
214 atom_entry_summary (AtomEntry *entry)
216 return entry->summary;
220 atom_entry_summary_set (AtomEntry *entry, char *summary)
223 g_free (entry->summary);
224 entry->summary = g_strdup (summary);
228 atom_entry_update_xmlnode (AtomEntry *entry)
237 if (entry->doc == NULL)
239 entry->doc = xmlNewDoc ("1.0");
240 root = xmlNewNode (NULL, "entry");
241 xmlNewNs (root, ATOM_NAMESPACE, NULL);
242 xmlDocSetRootElement (entry->doc, root);
248 root = xmlDocGetRootElement (entry->doc);
249 child = root->xmlChildrenNode;
250 while (child != NULL)
253 if (!xmlStrcmp (child->name, "id") ||
254 !xmlStrcmp (child->name, "title") ||
255 !xmlStrcmp (child->name, "summary") ||
256 !xmlStrcmp (child->name, "author") ||
257 !xmlStrcmp (child->name, "category"))
259 xmlUnlinkNode (child);
265 id = xmlNewTextChild (root, NULL, "id", atom_id_string (entry->id));
266 title = xmlNewTextChild (root, NULL, "title", entry->title);
268 summary = xmlNewTextChild (root, NULL, "summary", entry->summary);
269 for (i = 0; i < entry->authors->len; i++)
272 person = g_ptr_array_index (entry->authors, i);
273 author = atom_person_to_xmlnode (person, "author");
274 xmlAddChild (root, author);
276 for (i = 0; entry->categories && i < entry->categories->len; i++)
278 AtomCategory *category;
279 category = g_ptr_array_index (entry->categories, i);
280 cat = atom_category_to_xmlnode (category, "category");
281 xmlAddChild (root, category);
286 atom_entry_string (AtomEntry *entry, char **buffer, size_t *len)
288 atom_entry_update_xmlnode (entry);
289 xmlDocDumpMemory (entry->doc, buffer, len);
293 atom_entry_to_xmlnode (AtomEntry *entry)
295 atom_entry_update_xmlnode (entry);
296 return xmlCopyNodeList (xmlDocGetRootElement (entry->doc));