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>
34 GPtrArray *categories;
38 static void atom_entry_updated_set_from_iso8601 (AtomEntry *, char *);
40 void atom_entry_author_add (AtomEntry *, AtomPerson *);
41 static void atom_entry_authors_delete (AtomEntry *);
43 void atom_entry_category_add (AtomEntry *, AtomCategory *);
44 static void atom_entry_categories_delete (AtomEntry *);
47 atom_entry_new (char *id, char *title, AtomPerson *author)
50 entry = g_slice_new (AtomEntry);
52 entry->id = atom_id_new (id);
53 entry->title = g_strdup (title);
54 entry->updated = time (0);
55 entry->authors = NULL;
56 entry->categories = NULL;
57 atom_entry_author_add (entry, author);
58 entry->summary = NULL;
63 atom_entry_new_data_len (char *data, size_t len)
68 entry = g_slice_new0 (AtomEntry);
69 entry->doc = xmlReadMemory (data, len, NULL, NULL,
70 XML_PARSE_RECOVER | XML_PARSE_NOERROR);
71 if (entry->doc == NULL ||
72 (root = xmlDocGetRootElement (entry->doc)) == NULL)
74 g_slice_free (AtomEntry, entry);
77 if (xmlStrcmp (root->name, "entry"))
79 xmlFreeDoc (entry->doc);
80 g_slice_free (AtomEntry, entry);
83 for (child = root->xmlChildrenNode; child != NULL; child = child->next)
86 content = xmlNodeGetContent (child->xmlChildrenNode);
87 if (!xmlStrcmp (child->name, "id"))
88 entry->id = atom_id_new (content);
89 else if (!xmlStrcmp (child->name, "title"))
90 entry->title = g_strdup (content);
91 else if (!xmlStrcmp (child->name, "updated"))
92 atom_entry_updated_set_from_iso8601 (entry, content);
93 else if (!xmlStrcmp (child->name, "summary"))
94 entry->summary = g_strdup (content);
95 else if (!xmlStrcmp (child->name, "author"))
96 atom_entry_author_add (entry, atom_person_new_from_xmlnode (child));
97 else if (!xmlStrcmp (child->name, "category"))
98 atom_entry_category_add (entry, atom_category_new_from_xmlnode (child));
102 if (entry->id == NULL || entry->title == NULL ||
103 entry->updated == 0 || entry->authors == NULL)
105 atom_entry_delete (entry);
112 atom_entry_delete (AtomEntry *entry)
115 xmlFreeDoc (entry->doc);
117 atom_id_delete (entry->id);
119 g_free (entry->title);
121 atom_entry_authors_delete (entry);
122 if (entry->categories)
123 atom_entry_categories_delete (entry);
125 g_free (entry->summary);
126 g_slice_free (AtomEntry, entry);
130 atom_entry_id (AtomEntry *entry)
136 atom_entry_id_set (AtomEntry *entry, AtomID *id)
141 atom_id_delete (entry->id);
146 atom_entry_title (AtomEntry *entry)
152 atom_entry_title_set (AtomEntry *entry, char *title)
158 entry->title = g_strdup (title);
162 atom_entry_updated (AtomEntry *entry)
164 return entry->updated;
168 atom_entry_updated_set (AtomEntry *entry, time_t updated)
170 entry->updated = updated;
174 atom_entry_updated_set_from_iso8601 (AtomEntry *entry, char *updated)
177 g_time_val_from_iso8601 (updated, >v);
178 entry->updated = gtv.tv_sec;
182 atom_entry_updated_to_iso8601 (AtomEntry *entry)
185 gtv.tv_sec = entry->updated;
187 return g_time_val_to_iso8601 (>v);
191 atom_entry_authors (AtomEntry *entry, AtomPerson *** authors, size_t *len)
194 *len = entry->authors->len;
196 *authors = entry->authors->pdata;
200 atom_entry_author_add (AtomEntry *entry, AtomPerson *author)
202 if (entry->authors == NULL)
204 entry->authors = g_ptr_array_new ();
206 g_ptr_array_add (entry->authors, author);
210 atom_entry_authors_delete (AtomEntry *entry)
212 size_t len = entry->authors->len;
214 for (i = 0; i < len; i++)
215 atom_person_delete (g_ptr_array_index (entry->authors, i));
216 g_ptr_array_free (entry->authors, TRUE);
220 atom_entry_categories (AtomEntry *entry, AtomCategory *** categories,
224 *len = entry->categories->len;
226 *categories = entry->categories->pdata;
230 atom_entry_category_add (AtomEntry *entry, AtomCategory *category)
232 g_ptr_array_add (entry->categories, category);
233 if (entry->categories == NULL)
235 entry->categories = g_ptr_array_new ();
237 g_ptr_array_add (entry->categories, category);
241 atom_entry_categories_delete (AtomEntry *entry)
243 size_t len = entry->categories->len;
245 for (i = 0; i < len; i++)
246 atom_category_delete (g_ptr_array_index (entry->categories, i));
247 g_ptr_array_free (entry->categories, TRUE);
251 atom_entry_summary (AtomEntry *entry)
253 return entry->summary;
257 atom_entry_summary_set (AtomEntry *entry, char *summary)
260 g_free (entry->summary);
261 entry->summary = g_strdup (summary);
265 atom_entry_update_xmlnode (AtomEntry *entry)
276 if (entry->doc == NULL)
278 entry->doc = xmlNewDoc ("1.0");
279 root = xmlNewNode (NULL, "entry");
280 xmlNewNs (root, ATOM_NAMESPACE, NULL);
281 xmlDocSetRootElement (entry->doc, root);
287 root = xmlDocGetRootElement (entry->doc);
288 child = root->xmlChildrenNode;
289 while (child != NULL)
292 if (!xmlStrcmp (child->name, "id") ||
293 !xmlStrcmp (child->name, "title") ||
294 !xmlStrcmp (child->name, "updated") ||
295 !xmlStrcmp (child->name, "summary") ||
296 !xmlStrcmp (child->name, "author") ||
297 !xmlStrcmp (child->name, "category"))
299 xmlUnlinkNode (child);
305 id = xmlNewTextChild (root, NULL, "id", atom_id_string (entry->id));
306 title = xmlNewTextChild (root, NULL, "title", entry->title);
307 updatedstr = atom_entry_updated_to_iso8601 (entry);
308 updated = xmlNewTextChild (root, NULL, "updated", updatedstr);
311 summary = xmlNewTextChild (root, NULL, "summary", entry->summary);
312 for (i = 0; i < entry->authors->len; i++)
315 person = g_ptr_array_index (entry->authors, i);
316 author = atom_person_to_xmlnode (person, "author");
317 xmlAddChild (root, author);
319 for (i = 0; entry->categories && i < entry->categories->len; i++)
321 AtomCategory *category;
322 category = g_ptr_array_index (entry->categories, i);
323 cat = atom_category_to_xmlnode (category, "category");
324 xmlAddChild (root, cat);
329 atom_entry_string (AtomEntry *entry, char **buffer, size_t *len)
331 atom_entry_update_xmlnode (entry);
332 xmlDocDumpMemory (entry->doc, buffer, len);
336 atom_entry_to_xmlnode (AtomEntry *entry)
338 atom_entry_update_xmlnode (entry);
339 return xmlCopyNodeList (xmlDocGetRootElement (entry->doc));