b60e46e9cc58654f18ee95fad8985fca5d925181
[cascardo/atompub.git] / atom / person.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
25 struct _atom_person
26 {
27   char *name;
28   IRI *uri;
29   char *email;
30 };
31
32 AtomPerson *
33 atom_person_new (char *name)
34 {
35   AtomPerson *person;
36   person = g_slice_new (AtomPerson);
37   person->name = g_strdup (name);
38   person->uri = NULL;
39   person->email = NULL;
40   return person;
41 }
42
43 void
44 atom_person_delete (AtomPerson *person)
45 {
46   if (person->name)
47     g_free (person->name);
48   if (person->uri)
49     iri_delete (person->uri);
50   if (person->email)
51     g_free (person->email);
52   g_slice_free (AtomPerson, person);
53 }
54
55 char *
56 atom_person_name (AtomPerson *person)
57 {
58   return person->name;
59 }
60
61 void
62 atom_person_name_set (AtomPerson *person, char *name)
63 {
64   if (person->name)
65     g_free (person->name);
66   person->name = g_strdup (name);
67 }
68
69 IRI *
70 atom_person_uri (AtomPerson *person)
71 {
72   return person->uri;
73 }
74
75 void
76 atom_person_uri_set (AtomPerson *person, IRI *uri)
77 {
78   if (person->uri)
79     iri_delete (person->uri);
80   person->uri = iri_copy (uri);
81 }
82
83 char *
84 atom_person_email (AtomPerson *person)
85 {
86   return person->email;
87 }
88
89 void
90 atom_person_email_set (AtomPerson *person, char *email)
91 {
92   if (person->email)
93     g_free (person->email);
94   person->email = g_strdup (email);
95 }
96
97 xmlNodePtr
98 atom_person_to_xmlnode (AtomPerson *person, char *elname)
99 {
100   xmlNodePtr node;
101   node = xmlNewNode (NULL, elname);
102   xmlNewTextChild (node, NULL, "name", person->name);
103   if (person->uri)
104     xmlNewTextChild (node, NULL, "uri", iri_get_string (person->uri));
105   if (person->email)
106     xmlNewTextChild (node, NULL, "email", person->email);
107   return node;
108 }
109
110 AtomPerson *
111 atom_person_new_from_xmlnode (xmlNodePtr node)
112 {
113   xmlNodePtr child;
114   AtomPerson *person;
115   person = g_slice_new0 (AtomPerson);
116   for (child = node->xmlChildrenNode; child != NULL; child = child->next)
117     {
118       char *content = xmlNodeGetContent (child->xmlChildrenNode);
119       if (!xmlStrcmp (child->name, "name"))
120         person->name = content;
121       else if (!xmlStrcmp (child->name, "uri"))
122         person->uri = content;
123       else if (!xmlStrcmp (child->name, "email"))
124         person->email = content;
125       else
126         xmlFree (content);
127     }
128   return person;
129 }