Line data Source code
1 : /********************************************************************\
2 : * gnc-owner-xml-v2.c -- owner 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 : #include "gncCustomerP.h"
30 : #include "gncJobP.h"
31 : #include "gncVendorP.h"
32 : #include "gncEmployeeP.h"
33 :
34 : #include "gnc-xml-helper.h"
35 : #include "sixtp.h"
36 : #include "sixtp-utils.h"
37 : #include "sixtp-parsers.h"
38 : #include "sixtp-utils.h"
39 : #include "sixtp-dom-parsers.h"
40 : #include "sixtp-dom-generators.h"
41 :
42 : #include "gnc-xml.h"
43 : #include "io-gncxml-gen.h"
44 : #include "io-gncxml-v2.h"
45 :
46 : #include "gnc-owner-xml-v2.h"
47 :
48 : static QofLogModule log_module = GNC_MOD_IO;
49 :
50 : const gchar* owner_version_string = "2.0.0";
51 :
52 : /* ids */
53 : #define owner_type_string "owner:type"
54 : #define owner_id_string "owner:id"
55 :
56 : xmlNodePtr
57 2 : gnc_owner_to_dom_tree (const char* tag, const GncOwner* owner)
58 : {
59 : xmlNodePtr ret;
60 : const char* type_str;
61 :
62 2 : switch (gncOwnerGetType (owner))
63 : {
64 0 : case GNC_OWNER_CUSTOMER:
65 0 : type_str = GNC_ID_CUSTOMER;
66 0 : break;
67 0 : case GNC_OWNER_JOB:
68 0 : type_str = GNC_ID_JOB;
69 0 : break;
70 2 : case GNC_OWNER_VENDOR:
71 2 : type_str = GNC_ID_VENDOR;
72 2 : break;
73 0 : case GNC_OWNER_EMPLOYEE:
74 0 : type_str = GNC_ID_EMPLOYEE;
75 0 : break;
76 0 : default:
77 0 : PWARN ("Invalid owner type: %d", gncOwnerGetType (owner));
78 0 : return NULL;
79 : }
80 :
81 2 : ret = xmlNewNode (NULL, BAD_CAST tag);
82 2 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST owner_version_string);
83 :
84 2 : xmlAddChild (ret, text_to_dom_tree (owner_type_string, type_str));
85 2 : xmlAddChild (ret, guid_to_dom_tree (owner_id_string,
86 : gncOwnerGetGUID (owner)));
87 :
88 2 : return ret;
89 : }
90 :
91 : /***********************************************************************/
92 :
93 : struct owner_pdata
94 : {
95 : GncOwner* owner;
96 : QofBook* book;
97 : };
98 :
99 : static gboolean
100 5 : owner_type_handler (xmlNodePtr node, gpointer owner_pdata)
101 : {
102 5 : struct owner_pdata* pdata = static_cast<decltype (pdata)> (owner_pdata);
103 5 : GncOwner* owner = pdata->owner;
104 5 : auto init_owner_type = [](GncOwner* owner, const char* txt)
105 : {
106 5 : if (!g_strcmp0 (txt, GNC_ID_CUSTOMER))
107 1 : gncOwnerInitCustomer (owner, NULL);
108 4 : else if (!g_strcmp0 (txt, GNC_ID_JOB))
109 1 : gncOwnerInitJob (owner, NULL);
110 3 : else if (!g_strcmp0 (txt, GNC_ID_VENDOR))
111 3 : gncOwnerInitVendor (owner, NULL);
112 0 : else if (!g_strcmp0 (txt, GNC_ID_EMPLOYEE))
113 0 : gncOwnerInitEmployee (owner, NULL);
114 5 : };
115 10 : return apply_xmlnode_text (init_owner_type, owner, node);
116 : }
117 :
118 : static gboolean
119 5 : owner_id_handler (xmlNodePtr node, gpointer owner_pdata)
120 : {
121 5 : struct owner_pdata* pdata = static_cast<decltype (pdata)> (owner_pdata);
122 :
123 5 : auto guid = dom_tree_to_guid (node);
124 5 : g_return_val_if_fail (guid, FALSE);
125 :
126 5 : switch (gncOwnerGetType (pdata->owner))
127 : {
128 1 : case GNC_OWNER_CUSTOMER:
129 : {
130 1 : GncCustomer* cust = gncCustomerLookup (pdata->book, &*guid);
131 1 : if (!cust)
132 : {
133 0 : cust = gncCustomerCreate (pdata->book);
134 0 : gncCustomerSetGUID (cust, &*guid);
135 : }
136 1 : gncOwnerInitCustomer (pdata->owner, cust);
137 1 : break;
138 : }
139 1 : case GNC_OWNER_JOB:
140 : {
141 1 : GncJob* job = gncJobLookup (pdata->book, &*guid);
142 1 : if (!job)
143 : {
144 1 : job = gncJobCreate (pdata->book);
145 1 : gncJobSetGUID (job, &*guid);
146 : }
147 1 : gncOwnerInitJob (pdata->owner, job);
148 1 : break;
149 : }
150 3 : case GNC_OWNER_VENDOR:
151 : {
152 3 : GncVendor* vendor = gncVendorLookup (pdata->book, &*guid);
153 3 : if (!vendor)
154 : {
155 3 : vendor = gncVendorCreate (pdata->book);
156 3 : gncVendorSetGUID (vendor, &*guid);
157 : }
158 3 : gncOwnerInitVendor (pdata->owner, vendor);
159 3 : break;
160 : }
161 0 : case GNC_OWNER_EMPLOYEE:
162 : {
163 0 : GncEmployee* employee = gncEmployeeLookup (pdata->book, &*guid);
164 0 : if (!employee)
165 : {
166 0 : employee = gncEmployeeCreate (pdata->book);
167 0 : gncEmployeeSetGUID (employee, &*guid);
168 : }
169 0 : gncOwnerInitEmployee (pdata->owner, employee);
170 0 : break;
171 : }
172 0 : default:
173 0 : PWARN ("Invalid owner type: %d\n", gncOwnerGetType (pdata->owner));
174 0 : return FALSE;
175 : }
176 :
177 5 : return TRUE;
178 : }
179 :
180 : static struct dom_tree_handler owner_handlers_v2[] =
181 : {
182 : { owner_type_string, owner_type_handler, 1, 0 },
183 : { owner_id_string, owner_id_handler, 1, 0 },
184 : { NULL, 0, 0, 0 }
185 : };
186 :
187 : gboolean
188 5 : gnc_dom_tree_to_owner (xmlNodePtr node, GncOwner* owner, QofBook* book)
189 : {
190 : struct owner_pdata owner_pdata;
191 : gboolean successful;
192 :
193 5 : owner_pdata.owner = owner;
194 5 : owner_pdata.book = book;
195 :
196 5 : successful = dom_tree_generic_parse (node, owner_handlers_v2,
197 : &owner_pdata);
198 :
199 5 : if (!successful)
200 : {
201 0 : PERR ("failed to parse owner tree");
202 : }
203 :
204 5 : return successful;
205 : }
206 :
207 : static gboolean
208 4 : owner_ns (FILE* out)
209 : {
210 4 : g_return_val_if_fail (out, FALSE);
211 4 : return gnc_xml2_write_namespace_decl (out, "owner");
212 : }
213 :
214 : void
215 15 : gnc_owner_xml_initialize (void)
216 : {
217 : static GncXmlDataType_t be_data =
218 : {
219 : GNC_FILE_BACKEND_VERS,
220 : "gnc:Owner",
221 : NULL, /* parser_create */
222 : NULL, /* add_item */
223 : NULL, /* get_count */
224 : NULL, /* write */
225 : NULL, /* scrub */
226 : owner_ns,
227 : };
228 :
229 15 : gnc_xml_register_backend (be_data);
230 15 : }
|