Fixed setting an entry title
[cascardo/atompub.git] / atom / entry.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19
20 #include <atompub/atom.h>
21
22 #include <glib.h>
23 #include <libxml/tree.h>
24 #include <libxml/parser.h>
25
26 struct _atom_entry
27 {
28   xmlDocPtr doc;
29   char *id;
30   char *title;
31   GPtrArray *authors;
32   char *summary;
33 };
34
35 void atom_entry_author_add (AtomEntry *, AtomPerson *);
36 static void atom_entry_authors_delete (AtomEntry *);
37
38 AtomEntry *
39 atom_entry_new (char *title, AtomPerson *author)
40 {
41   AtomEntry *entry;
42   entry = g_slice_new (AtomEntry);
43   entry->doc = NULL;
44   entry->id = NULL;
45   entry->title = g_strdup (title);
46   entry->authors = NULL;
47   atom_entry_author_add (entry, author);
48   entry->summary = NULL;
49   return entry;
50 }
51
52 AtomEntry *
53 atom_entry_new_data_len (char *data, size_t len)
54 {
55   AtomEntry *entry;
56   xmlNodePtr root;
57   xmlNodePtr child;
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)
63     {
64       g_slice_free (AtomEntry, entry);
65       return NULL;
66     }
67   if (xmlStrcmp (root->name, "entry"))
68     {
69       xmlFreeDoc (entry->doc);
70       g_slice_free (AtomEntry, entry);
71       return NULL;
72     }
73   for (child = root->xmlChildrenNode; child != NULL; child = child->next)
74     {
75       char * content;
76       content = xmlNodeGetContent (child->xmlChildrenNode);
77       if (!xmlStrcmp (child->name, "id"))
78         entry->id = content;
79       else if (!xmlStrcmp (child->name, "title"))
80         entry->title = content;
81       else if (!xmlStrcmp (child->name, "summary"))
82         entry->summary = content;
83       else if (!xmlStrcmp (child->name, "author"))
84         atom_entry_author_add (entry, atom_person_new_from_xmlnode (child));
85       else
86         xmlFree (content);
87     }
88   return entry;
89 }
90
91 void
92 atom_entry_delete (AtomEntry *entry)
93 {
94   if (entry->doc)
95     xmlFreeDoc (entry->doc);
96   if (entry->id)
97     g_free (entry->id);
98   if (entry->title)
99     g_free (entry->title);
100   if (entry->authors)
101     atom_entry_authors_delete (entry);
102   if (entry->summary)
103     g_free (entry->summary);
104   g_slice_free (AtomEntry, entry);
105 }
106
107 char *
108 atom_entry_id (AtomEntry *entry)
109 {
110   return entry->id;
111 }
112
113 void
114 atom_entry_id_set (AtomEntry *entry, char *id)
115 {
116   if (entry->id)
117     g_free (entry->id);
118   entry->id = g_strdup (id);
119 }
120
121 char *
122 atom_entry_title (AtomEntry *entry)
123 {
124   return entry->title;
125 }
126
127 void
128 atom_entry_title_set (AtomEntry *entry, char *title)
129 {
130   if (entry->title)
131     g_free (title);
132   entry->title = g_strdup (title);
133 }
134
135 void
136 atom_entry_authors (AtomEntry *entry, AtomPerson *** authors, size_t *len)
137 {
138   if (len)
139     *len = entry->authors->len;
140   if (authors)
141     *authors = entry->authors->pdata;
142 }
143
144 void
145 atom_entry_author_add (AtomEntry *entry, AtomPerson *author)
146 {
147   if (entry->authors == NULL)
148     {
149       entry->authors = g_ptr_array_new ();
150     }
151   g_ptr_array_add (entry->authors, author);
152 }
153
154 static void
155 atom_entry_authors_delete (AtomEntry *entry)
156 {
157   size_t len = entry->authors->len;
158   int i;
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);
162 }
163
164 char *
165 atom_entry_summary (AtomEntry *entry)
166 {
167   return entry->summary;
168 }
169
170 void
171 atom_entry_summary_set (AtomEntry *entry, char *summary)
172 {
173   if (entry->summary)
174     g_free (entry->summary);
175   entry->summary = g_strdup (summary);
176 }
177
178 void
179 atom_entry_string (AtomEntry *entry, char **buffer, size_t *len)
180 {
181   xmlDocDumpMemory (entry->doc, buffer, len);
182 }
183
184 xmlNodePtr
185 atom_entry_to_xmlnode (AtomEntry *entry)
186 {
187   if (entry->doc)
188     return xmlCopyNodeList (xmlDocGetRootElement (entry->doc));
189   return NULL;
190 }