Started adding support for Entry Content.
[cascardo/atompub.git] / atom / content.c
1 /*
2  *  Copyright (C) 2009  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/content.h>
21 #include <atompub/content-xml.h>
22
23 #include <glib.h>
24 #include <libxml/tree.h>
25 #include <string.h>
26
27 struct _atom_content
28 {
29   char *type;
30   char *src;
31   char *content;
32   size_t content_len;
33   xmlNodePtr xmlcontent;
34 };
35
36 AtomContent *
37 atom_content_new (char *type, char *buffer, size_t len)
38 {
39   AtomContent *content;
40   content = g_slice_new0 (AtomContent);
41   content->type = g_strdup (type);
42   content->content = g_malloc (len);
43   memcpy (content->content, buffer, len);
44   content->content_len = len;
45   return content;
46 }
47
48 AtomContent *
49 atom_content_new_src (char *type, char *src)
50 {
51   AtomContent *content;
52   content = g_slice_new0 (AtomContent);
53   content->type = g_strdup (type);
54   content->src = g_strdup (src);
55   return content;
56 }
57
58 AtomContent *
59 atom_content_new_data_len (char *buffer, size_t len)
60 {
61   /* TODO: Here we decode the XML to find content type, possible
62      source and data */
63   return NULL;
64 }
65
66 void
67 atom_content_delete (AtomContent *content)
68 {
69   if (content->type)
70     g_free (content->type);
71   if (content->src)
72     g_free (content->src);
73   if (content->content)
74     g_free (content->content);
75   if (content->xmlcontent)
76     xmlFreeNode (content->xmlcontent);
77   g_slice_free (AtomContent, content);
78 }
79
80 char *
81 atom_content_type (AtomContent *content)
82 {
83   return content->type;
84 }
85
86 void
87 atom_content_type_set (AtomContent *content, char *type)
88 {
89   if (content->type)
90     g_free (content->type);
91   content->type = g_strdup (type);
92 }
93
94 char *
95 atom_content_src (AtomContent *content)
96 {
97   return content->src;
98 }
99
100 void
101 atom_content_src_set (AtomContent *content, char *src)
102 {
103   if (content->src)
104     g_free (content->src);
105   content->src = g_strdup (src);
106 }
107
108 void
109 atom_content_content (AtomContent *content, char **buffer, size_t *len)
110 {
111   /* TODO: Copy the content or allocated the buffer? */
112 }
113
114 void
115 atom_content_content_set (AtomContent *content, char *buffer, size_t len)
116 {
117   if (content->content)
118     g_free (content->content);
119   content->content = g_malloc (len);
120   memcpy (content->content, buffer, len);
121 }
122
123 void
124 atom_content_string (AtomContent *content, char **buffer, size_t *len)
125 {
126   /* TODO: convert everything into a string, like
127      "<content type="html">&lt;br&gt;</content>" */
128 }
129
130 AtomContent *
131 atom_content_new_xmlnode (char *type, xmlNodePtr node)
132 {
133   AtomContent *content;
134   content = g_slice_new0 (AtomContent);
135   content->type = g_strdup (type);
136   content->xmlcontent = xmlCopyNodeList (node);
137   return content;
138 }
139
140 xmlNodePtr
141 atom_content_to_xmlnode (AtomContent *content)
142 {
143   /* TODO: Convert everything into a xmlnode */
144   return NULL;
145 }
146
147 xmlNodePtr
148 atom_content_content_xmlnode (AtomContent *content)
149 {
150   return content->xmlcontent;
151 }
152
153 void
154 atom_content_content_set_xmlnode (AtomContent *content, xmlNodePtr node)
155 {
156   if (content->xmlcontent)
157     xmlFreeNode (content->xmlcontent);
158   content->xmlcontent = xmlCopyNodeList (node);
159 }