Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-address-xml-v2.c -- address xml i/o implementation *
3 : : * *
4 : : * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
5 : : * *
6 : : * This program is free software; you can redistribute it and/or *
7 : : * modify it under the terms of the GNU General Public License as *
8 : : * published by the Free Software Foundation; either version 2 of *
9 : : * the License, or (at your option) any later version. *
10 : : * *
11 : : * This program is distributed in the hope that it will be useful, *
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 : : * GNU General Public License for more details. *
15 : : * *
16 : : * You should have received a copy of the GNU General Public License*
17 : : * along with this program; if not, contact: *
18 : : * *
19 : : * Free Software Foundation Voice: +1-617-542-5942 *
20 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
22 : : * *
23 : : \********************************************************************/
24 : : #include <glib.h>
25 : :
26 : : #include <config.h>
27 : : #include <stdlib.h>
28 : : #include <string.h>
29 : :
30 : : #include "gnc-xml-helper.h"
31 : :
32 : : #include "sixtp.h"
33 : : #include "sixtp-utils.h"
34 : : #include "sixtp-parsers.h"
35 : : #include "sixtp-utils.h"
36 : : #include "sixtp-dom-parsers.h"
37 : : #include "sixtp-dom-generators.h"
38 : :
39 : : #include "gnc-xml.h"
40 : : #include "io-gncxml-gen.h"
41 : : #include "io-gncxml-v2.h"
42 : :
43 : : #include "gnc-address-xml-v2.h"
44 : :
45 : : static QofLogModule log_module = GNC_MOD_IO;
46 : :
47 : : const gchar* address_version_string = "2.0.0";
48 : :
49 : : /* ids */
50 : : #define addr_name_string "addr:name"
51 : : #define addr_addr1_string "addr:addr1"
52 : : #define addr_addr2_string "addr:addr2"
53 : : #define addr_addr3_string "addr:addr3"
54 : : #define addr_addr4_string "addr:addr4"
55 : : #define addr_phone_string "addr:phone"
56 : : #define addr_fax_string "addr:fax"
57 : : #define addr_email_string "addr:email"
58 : : #define addr_slots_string "addr:slots"
59 : :
60 : : static void
61 : 16 : maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
62 : : {
63 : 16 : if (str && *str)
64 : 10 : xmlAddChild (ptr, text_to_dom_tree (tag, str));
65 : 16 : }
66 : :
67 : : xmlNodePtr
68 : 2 : gnc_address_to_dom_tree (const char* tag, GncAddress* addr)
69 : : {
70 : : xmlNodePtr ret;
71 : :
72 : 2 : ret = xmlNewNode (NULL, BAD_CAST tag);
73 : 2 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST address_version_string);
74 : :
75 : 2 : maybe_add_string (ret, addr_name_string, gncAddressGetName (addr));
76 : :
77 : 2 : maybe_add_string (ret, addr_addr1_string, gncAddressGetAddr1 (addr));
78 : 2 : maybe_add_string (ret, addr_addr2_string, gncAddressGetAddr2 (addr));
79 : 2 : maybe_add_string (ret, addr_addr3_string, gncAddressGetAddr3 (addr));
80 : 2 : maybe_add_string (ret, addr_addr4_string, gncAddressGetAddr4 (addr));
81 : :
82 : 2 : maybe_add_string (ret, addr_phone_string, gncAddressGetPhone (addr));
83 : 2 : maybe_add_string (ret, addr_fax_string, gncAddressGetFax (addr));
84 : 2 : maybe_add_string (ret, addr_email_string, gncAddressGetEmail (addr));
85 : :
86 : : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
87 : 2 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (addr_slots_string,
88 : 2 : QOF_INSTANCE (addr)));
89 : 2 : return ret;
90 : : }
91 : :
92 : : /***********************************************************************/
93 : :
94 : : struct address_pdata
95 : : {
96 : : GncAddress* address;
97 : : };
98 : :
99 : : static gboolean
100 : 14 : set_string (xmlNodePtr node, GncAddress* addr,
101 : : void (*func) (GncAddress* addr, const char* txt))
102 : : {
103 : 14 : gchar* txt = dom_tree_to_text (node);
104 : 14 : g_return_val_if_fail (txt, FALSE);
105 : :
106 : 14 : func (addr, txt);
107 : :
108 : 14 : g_free (txt);
109 : :
110 : 14 : return TRUE;
111 : : }
112 : :
113 : : static gboolean
114 : 3 : address_name_handler (xmlNodePtr node, gpointer addr_pdata)
115 : : {
116 : 3 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
117 : :
118 : 3 : return set_string (node, pdata->address, gncAddressSetName);
119 : : }
120 : :
121 : : static gboolean
122 : 3 : address_addr1_handler (xmlNodePtr node, gpointer addr_pdata)
123 : : {
124 : 3 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
125 : :
126 : 3 : return set_string (node, pdata->address, gncAddressSetAddr1);
127 : : }
128 : :
129 : : static gboolean
130 : 3 : address_addr2_handler (xmlNodePtr node, gpointer addr_pdata)
131 : : {
132 : 3 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
133 : :
134 : 3 : return set_string (node, pdata->address, gncAddressSetAddr2);
135 : : }
136 : :
137 : : static gboolean
138 : 0 : address_addr3_handler (xmlNodePtr node, gpointer addr_pdata)
139 : : {
140 : 0 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
141 : :
142 : 0 : return set_string (node, pdata->address, gncAddressSetAddr3);
143 : : }
144 : :
145 : : static gboolean
146 : 0 : address_addr4_handler (xmlNodePtr node, gpointer addr_pdata)
147 : : {
148 : 0 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
149 : :
150 : 0 : return set_string (node, pdata->address, gncAddressSetAddr4);
151 : : }
152 : :
153 : : static gboolean
154 : 3 : address_phone_handler (xmlNodePtr node, gpointer addr_pdata)
155 : : {
156 : 3 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
157 : :
158 : 3 : return set_string (node, pdata->address, gncAddressSetPhone);
159 : : }
160 : :
161 : : static gboolean
162 : 0 : address_fax_handler (xmlNodePtr node, gpointer addr_pdata)
163 : : {
164 : 0 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
165 : :
166 : 0 : return set_string (node, pdata->address, gncAddressSetFax);
167 : : }
168 : :
169 : : static gboolean
170 : 2 : address_email_handler (xmlNodePtr node, gpointer addr_pdata)
171 : : {
172 : 2 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
173 : :
174 : 2 : return set_string (node, pdata->address, gncAddressSetEmail);
175 : : }
176 : :
177 : : static gboolean
178 : 0 : address_slots_handler (xmlNodePtr node, gpointer addr_pdata)
179 : : {
180 : 0 : struct address_pdata* pdata = static_cast<decltype (pdata)> (addr_pdata);
181 : 0 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->address));
182 : : }
183 : :
184 : : static struct dom_tree_handler address_handlers_v2[] =
185 : : {
186 : : { addr_name_string, address_name_handler, 0, 0 },
187 : : { addr_addr1_string, address_addr1_handler, 0, 0 },
188 : : { addr_addr2_string, address_addr2_handler, 0, 0 },
189 : : { addr_addr3_string, address_addr3_handler, 0, 0 },
190 : : { addr_addr4_string, address_addr4_handler, 0, 0 },
191 : : { addr_phone_string, address_phone_handler, 0, 0 },
192 : : { addr_fax_string, address_fax_handler, 0, 0 },
193 : : { addr_email_string, address_email_handler, 0, 0 },
194 : : { addr_slots_string, address_slots_handler, 0, 0 },
195 : : { NULL, 0, 0, 0 }
196 : : };
197 : :
198 : : gboolean
199 : 3 : gnc_dom_tree_to_address (xmlNodePtr node, GncAddress* address)
200 : : {
201 : : struct address_pdata addr_pdata;
202 : : gboolean successful;
203 : :
204 : 3 : addr_pdata.address = address;
205 : :
206 : 3 : successful = dom_tree_generic_parse (node, address_handlers_v2,
207 : : &addr_pdata);
208 : :
209 : 3 : if (!successful)
210 : : {
211 : 0 : PERR ("failed to parse address tree");
212 : : }
213 : :
214 : 3 : return successful;
215 : : }
216 : :
217 : : static gboolean
218 : 4 : address_ns (FILE* out)
219 : : {
220 : 4 : g_return_val_if_fail (out, FALSE);
221 : 4 : return gnc_xml2_write_namespace_decl (out, "addr");
222 : : }
223 : :
224 : : void
225 : 61 : gnc_address_xml_initialize (void)
226 : : {
227 : : static GncXmlDataType_t be_data =
228 : : {
229 : : GNC_FILE_BACKEND_VERS,
230 : : "gnc:Address",
231 : : NULL, /* parser_create */
232 : : NULL, /* add_item */
233 : : NULL, /* get_count */
234 : : NULL, /* write */
235 : : NULL, /* scrub */
236 : : address_ns,
237 : : };
238 : :
239 : 61 : gnc_xml_register_backend (be_data);
240 : 61 : }
|