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>
35 void atom_entry_author_add (AtomEntry *, AtomPerson *);
36 static void atom_entry_authors_delete (AtomEntry *);
39 atom_entry_new (char *title, AtomPerson *author)
42 entry = g_slice_new (AtomEntry);
45 entry->title = g_strdup (title);
46 entry->authors = NULL;
47 atom_entry_author_add (entry, author);
48 entry->summary = NULL;
53 atom_entry_new_data_len (char *data, size_t len)
58 entry = g_slice_new0 (AtomEntry);
59 entry->doc = xmlReadMemory (data, len, NULL, NULL,
60 XML_PARSE_RECOVER | XML_PARSE_NOERROR);
61 if (entry->doc == NULL ||
62 (root = xmlDocGetRootElement (entry->doc)) == NULL)
64 g_slice_free (AtomEntry, entry);
67 if (xmlStrcmp (root->name, "entry"))
69 xmlFreeDoc (entry->doc);
70 g_slice_free (AtomEntry, entry);
73 for (child = root->xmlChildrenNode; child != NULL; child = child->next)
76 content = xmlNodeGetContent (child->xmlChildrenNode);
77 if (!xmlStrcmp (child->name, "id"))
78 entry->id = atom_id_new (content);
79 else if (!xmlStrcmp (child->name, "title"))
80 entry->title = g_strdup (content);
81 else if (!xmlStrcmp (child->name, "summary"))
82 entry->summary = g_strdup (content);
83 else if (!xmlStrcmp (child->name, "author"))
84 atom_entry_author_add (entry, atom_person_new_from_xmlnode (child));
92 atom_entry_delete (AtomEntry *entry)
95 xmlFreeDoc (entry->doc);
97 atom_id_delete (entry->id);
99 g_free (entry->title);
101 atom_entry_authors_delete (entry);
103 g_free (entry->summary);
104 g_slice_free (AtomEntry, entry);
108 atom_entry_id (AtomEntry *entry)
114 atom_entry_id_set (AtomEntry *entry, char *id)
118 entry->id = g_strdup (id);
122 atom_entry_title (AtomEntry *entry)
128 atom_entry_title_set (AtomEntry *entry, char *title)
132 entry->title = g_strdup (title);
136 atom_entry_authors (AtomEntry *entry, AtomPerson *** authors, size_t *len)
139 *len = entry->authors->len;
141 *authors = entry->authors->pdata;
145 atom_entry_author_add (AtomEntry *entry, AtomPerson *author)
147 if (entry->authors == NULL)
149 entry->authors = g_ptr_array_new ();
151 g_ptr_array_add (entry->authors, author);
155 atom_entry_authors_delete (AtomEntry *entry)
157 size_t len = entry->authors->len;
159 for (i = 0; i < len; i++)
160 atom_person_delete (g_ptr_array_index (entry->authors, i));
161 g_ptr_array_free (entry->authors, TRUE);
165 atom_entry_summary (AtomEntry *entry)
167 return entry->summary;
171 atom_entry_summary_set (AtomEntry *entry, char *summary)
174 g_free (entry->summary);
175 entry->summary = g_strdup (summary);
179 atom_entry_update_xmlnode (AtomEntry *entry)
187 if (entry->doc == NULL)
189 entry->doc = xmlNewDoc ("1.0");
190 root = xmlNewNode (NULL, "entry");
191 xmlNewNs (root, ATOM_NAMESPACE, NULL);
192 xmlDocSetRootElement (entry->doc, root);
198 root = xmlDocGetRootElement (entry->doc);
199 child = root->xmlChildrenNode;
200 while (child != NULL)
203 if (!xmlStrcmp (child->name, "id") ||
204 !xmlStrcmp (child->name, "title") ||
205 !xmlStrcmp (child->name, "summary") ||
206 !xmlStrcmp (child->name, "author"))
208 xmlUnlinkNode (child);
214 id = xmlNewTextChild (root, NULL, "id", entry->id);
215 title = xmlNewTextChild (root, NULL, "title", entry->title);
217 summary = xmlNewTextChild (root, NULL, "summary", entry->summary);
218 for (i = 0; i < entry->authors->len; i++)
221 person = g_ptr_array_index (entry->authors, i);
222 author = atom_person_to_xmlnode (person, "author");
223 xmlAddChild (root, author);
228 atom_entry_string (AtomEntry *entry, char **buffer, size_t *len)
230 atom_entry_update_xmlnode (entry);
231 xmlDocDumpMemory (entry->doc, buffer, len);
235 atom_entry_to_xmlnode (AtomEntry *entry)
237 atom_entry_update_xmlnode (entry);
238 return xmlCopyNodeList (xmlDocGetRootElement (entry->doc));