21eae78f3420141581c0ae4392d6ad0bf955c9d0
[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   AtomPerson *author;
32   char *summary;
33 };
34
35 AtomEntry *
36 atom_entry_new (char *title, AtomPerson *author)
37 {
38   AtomEntry *entry;
39   entry = g_slice_new (AtomEntry);
40   entry->doc = NULL;
41   entry->id = NULL;
42   entry->title = g_strdup (title);
43   entry->author = author;
44   entry->summary = NULL;
45   return entry;
46 }
47
48 AtomEntry *
49 atom_entry_new_data_len (char *data, size_t len)
50 {
51   AtomEntry *entry;
52   xmlNodePtr root;
53   xmlNodePtr child;
54   entry = g_slice_new0 (AtomEntry);
55   entry->doc = xmlReadMemory (data, len, NULL, NULL, XML_PARSE_RECOVER);
56   if (entry->doc == NULL ||
57       (root = xmlDocGetRootElement (entry->doc)) == NULL)
58     {
59       g_slice_free (AtomEntry, entry);
60       return NULL;
61     }
62   if (xmlStrcmp (root->name, "entry"))
63     {
64       xmlFreeDoc (entry->doc);
65       g_slice_free (AtomEntry, entry);
66       return NULL;
67     }
68   for (child = root->xmlChildrenNode; child != NULL; child = child->next)
69     {
70       char * content;
71       content = xmlNodeGetContent (child->xmlChildrenNode);
72       if (!xmlStrcmp (child->name, "id"))
73         entry->id = content;
74       else if (!xmlStrcmp (child->name, "title"))
75         entry->title = content;
76       else if (!xmlStrcmp (child->name, "summary"))
77         entry->summary = content;
78       else if (!xmlStrcmp (child->name, "author"))
79         entry->author = atom_person_new_from_xmlnode (child);
80       else
81         xmlFree (content);
82     }
83   return entry;
84 }
85
86 void
87 atom_entry_delete (AtomEntry *entry)
88 {
89   if (entry->doc)
90     xmlFreeDoc (entry->doc);
91   if (entry->id)
92     g_free (entry->id);
93   if (entry->title)
94     g_free (entry->title);
95   if (entry->author)
96     atom_person_delete (entry->author);
97   if (entry->summary)
98     g_free (entry->summary);
99   g_slice_free (AtomEntry, entry);
100 }
101
102 char *
103 atom_entry_id (AtomEntry *entry)
104 {
105   return entry->id;
106 }
107
108 void
109 atom_entry_id_set (AtomEntry *entry, char *id)
110 {
111   if (entry->id)
112     g_free (entry->id);
113   entry->id = g_strdup (id);
114 }
115
116 char *
117 atom_entry_title (AtomEntry *entry)
118 {
119   return entry->title;
120 }
121
122 void
123 atom_entry_title_set (AtomEntry *entry, char *title)
124 {
125   if (entry->title)
126     g_free (title);
127   entry->title = g_strdup (entry);
128 }
129
130 AtomPerson *
131 atom_entry_author (AtomEntry *entry)
132 {
133   return entry->author;
134 }
135
136 void
137 atom_entry_author_set (AtomEntry *entry, AtomPerson *author)
138 {
139   if (entry->author)
140     atom_person_delete (entry->author);
141   entry->author = author;
142 }
143
144 char *
145 atom_entry_summary (AtomEntry *entry)
146 {
147   return entry->summary;
148 }
149
150 void
151 atom_entry_summary_set (AtomEntry *entry, char *summary)
152 {
153   if (entry->summary)
154     g_free (entry->summary);
155   entry->summary = g_strdup (summary);
156 }
157
158 void
159 atom_entry_string (AtomEntry *entry, char **buffer, size_t *len)
160 {
161   xmlDocDumpMemory (entry->doc, buffer, len);
162 }
163
164 xmlNodePtr
165 atom_entry_to_xmlnode (AtomEntry *entry)
166 {
167   if (entry->doc)
168     return xmlCopyNodeList (xmlDocGetRootElement (entry->doc));
169   return NULL;
170 }