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 *title, AtomPerson *author)
46 entry = g_slice_new (AtomEntry);
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));
99 atom_entry_delete (AtomEntry *entry)
102 xmlFreeDoc (entry->doc);
104 atom_id_delete (entry->id);
106 g_free (entry->title);
108 atom_entry_authors_delete (entry);
109 if (entry->categories)
110 atom_entry_categories_delete (entry);
112 g_free (entry->summary);
113 g_slice_free (AtomEntry, entry);
117 atom_entry_id (AtomEntry *entry)
123 atom_entry_id_set (AtomEntry *entry, AtomID *id)
126 atom_id_delete (entry->id);
131 atom_entry_title (AtomEntry *entry)
137 atom_entry_title_set (AtomEntry *entry, char *title)
141 entry->title = g_strdup (title);
145 atom_entry_authors (AtomEntry *entry, AtomPerson *** authors, size_t *len)
148 *len = entry->authors->len;
150 *authors = entry->authors->pdata;
154 atom_entry_author_add (AtomEntry *entry, AtomPerson *author)
156 if (entry->authors == NULL)
158 entry->authors = g_ptr_array_new ();
160 g_ptr_array_add (entry->authors, author);
164 atom_entry_authors_delete (AtomEntry *entry)
166 size_t len = entry->authors->len;
168 for (i = 0; i < len; i++)
169 atom_person_delete (g_ptr_array_index (entry->authors, i));
170 g_ptr_array_free (entry->authors, TRUE);
174 atom_entry_categories (AtomEntry *entry, AtomCategory *** categories,
178 *len = entry->categories->len;
180 *categories = entry->categories->pdata;
184 atom_entry_category_add (AtomEntry *entry, AtomCategory *category)
186 g_ptr_array_add (entry->categories, category);
187 if (entry->categories == NULL)
189 entry->categories = g_ptr_array_new ();
191 g_ptr_array_add (entry->categories, category);
195 atom_entry_categories_delete (AtomEntry *entry)
197 size_t len = entry->categories->len;
199 for (i = 0; i < len; i++)
200 atom_category_delete (g_ptr_array_index (entry->categories, i));
201 g_ptr_array_free (entry->categories, TRUE);
205 atom_entry_summary (AtomEntry *entry)
207 return entry->summary;
211 atom_entry_summary_set (AtomEntry *entry, char *summary)
214 g_free (entry->summary);
215 entry->summary = g_strdup (summary);
219 atom_entry_update_xmlnode (AtomEntry *entry)
228 if (entry->doc == NULL)
230 entry->doc = xmlNewDoc ("1.0");
231 root = xmlNewNode (NULL, "entry");
232 xmlNewNs (root, ATOM_NAMESPACE, NULL);
233 xmlDocSetRootElement (entry->doc, root);
239 root = xmlDocGetRootElement (entry->doc);
240 child = root->xmlChildrenNode;
241 while (child != NULL)
244 if (!xmlStrcmp (child->name, "id") ||
245 !xmlStrcmp (child->name, "title") ||
246 !xmlStrcmp (child->name, "summary") ||
247 !xmlStrcmp (child->name, "author") ||
248 !xmlStrcmp (child->name, "category"))
250 xmlUnlinkNode (child);
256 id = xmlNewTextChild (root, NULL, "id", atom_id_string (entry->id));
257 title = xmlNewTextChild (root, NULL, "title", entry->title);
259 summary = xmlNewTextChild (root, NULL, "summary", entry->summary);
260 for (i = 0; i < entry->authors->len; i++)
263 person = g_ptr_array_index (entry->authors, i);
264 author = atom_person_to_xmlnode (person, "author");
265 xmlAddChild (root, author);
267 for (i = 0; entry->categories && i < entry->categories->len; i++)
269 AtomCategory *category;
270 category = g_ptr_array_index (entry->categories, i);
271 cat = atom_category_to_xmlnode (category, "category");
272 xmlAddChild (root, category);
277 atom_entry_string (AtomEntry *entry, char **buffer, size_t *len)
279 atom_entry_update_xmlnode (entry);
280 xmlDocDumpMemory (entry->doc, buffer, len);
284 atom_entry_to_xmlnode (AtomEntry *entry)
286 atom_entry_update_xmlnode (entry);
287 return xmlCopyNodeList (xmlDocGetRootElement (entry->doc));