Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-employee-xml-v2.c -- employee 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 "gncEmployeeP.h"
30 : : #include "gnc-xml-helper.h"
31 : : #include "sixtp.h"
32 : : #include "sixtp-utils.h"
33 : : #include "sixtp-parsers.h"
34 : : #include "sixtp-utils.h"
35 : : #include "sixtp-dom-parsers.h"
36 : : #include "sixtp-dom-generators.h"
37 : :
38 : : #include "gnc-xml.h"
39 : : #include "io-gncxml-gen.h"
40 : : #include "io-gncxml-v2.h"
41 : :
42 : : #include "gnc-employee-xml-v2.h"
43 : : #include "gnc-address-xml-v2.h"
44 : :
45 : : #define _GNC_MOD_NAME GNC_ID_EMPLOYEE
46 : :
47 : : static QofLogModule log_module = GNC_MOD_IO;
48 : :
49 : : const gchar* employee_version_string = "2.0.0";
50 : :
51 : : /* ids */
52 : : #define gnc_employee_string "gnc:GncEmployee"
53 : : #define employee_username_string "employee:username"
54 : : #define employee_guid_string "employee:guid"
55 : : #define employee_id_string "employee:id"
56 : : #define employee_addr_string "employee:addr"
57 : : #define employee_language_string "employee:language"
58 : : #define employee_acl_string "employee:acl"
59 : : #define employee_active_string "employee:active"
60 : : #define employee_workday_string "employee:workday"
61 : : #define employee_rate_string "employee:rate"
62 : : #define employee_currency_string "employee:currency"
63 : : #define employee_ccard_string "employee:ccard"
64 : : #define employee_slots_string "employee:slots"
65 : :
66 : : static void
67 : 0 : maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
68 : : {
69 : 0 : if (str && *str)
70 : 0 : xmlAddChild (ptr, text_to_dom_tree (tag, str));
71 : 0 : }
72 : :
73 : : static xmlNodePtr
74 : 0 : employee_dom_tree_create (GncEmployee* employee)
75 : : {
76 : : xmlNodePtr ret;
77 : : gnc_numeric num;
78 : : Account* ccard_acc;
79 : :
80 : 0 : ret = xmlNewNode (NULL, BAD_CAST gnc_employee_string);
81 : 0 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST employee_version_string);
82 : :
83 : 0 : xmlAddChild (ret, guid_to_dom_tree (employee_guid_string,
84 : 0 : qof_instance_get_guid (QOF_INSTANCE (employee))));
85 : :
86 : 0 : xmlAddChild (ret, text_to_dom_tree (employee_username_string,
87 : : gncEmployeeGetUsername (employee)));
88 : :
89 : 0 : xmlAddChild (ret, text_to_dom_tree (employee_id_string,
90 : : gncEmployeeGetID (employee)));
91 : :
92 : 0 : xmlAddChild (ret, gnc_address_to_dom_tree (employee_addr_string,
93 : : gncEmployeeGetAddr (employee)));
94 : :
95 : 0 : maybe_add_string (ret, employee_language_string,
96 : : gncEmployeeGetLanguage (employee));
97 : 0 : maybe_add_string (ret, employee_acl_string, gncEmployeeGetAcl (employee));
98 : :
99 : 0 : xmlAddChild (ret, int_to_dom_tree (employee_active_string,
100 : 0 : gncEmployeeGetActive (employee)));
101 : :
102 : 0 : num = gncEmployeeGetWorkday (employee);
103 : 0 : xmlAddChild (ret, gnc_numeric_to_dom_tree (employee_workday_string, &num));
104 : :
105 : 0 : num = gncEmployeeGetRate (employee);
106 : 0 : xmlAddChild (ret, gnc_numeric_to_dom_tree (employee_rate_string, &num));
107 : :
108 : : xmlAddChild
109 : 0 : (ret,
110 : : commodity_ref_to_dom_tree (employee_currency_string,
111 : 0 : gncEmployeeGetCurrency (employee)));
112 : :
113 : 0 : ccard_acc = gncEmployeeGetCCard (employee);
114 : 0 : if (ccard_acc)
115 : 0 : xmlAddChild (ret, guid_to_dom_tree (employee_ccard_string,
116 : 0 : qof_instance_get_guid (QOF_INSTANCE (ccard_acc))));
117 : :
118 : : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
119 : 0 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (employee_slots_string,
120 : 0 : QOF_INSTANCE (employee)));
121 : 0 : return ret;
122 : : }
123 : :
124 : : /***********************************************************************/
125 : :
126 : : struct employee_pdata
127 : : {
128 : : GncEmployee* employee;
129 : : QofBook* book;
130 : : };
131 : :
132 : : static gboolean
133 : 0 : employee_username_handler (xmlNodePtr node, gpointer employee_pdata)
134 : : {
135 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
136 : :
137 : 0 : return apply_xmlnode_text (gncEmployeeSetUsername, pdata->employee, node);
138 : : }
139 : :
140 : : static gboolean
141 : 0 : employee_guid_handler (xmlNodePtr node, gpointer employee_pdata)
142 : : {
143 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
144 : : GncEmployee* employee;
145 : :
146 : 0 : auto guid = dom_tree_to_guid (node);
147 : 0 : g_return_val_if_fail (guid, FALSE);
148 : :
149 : : /* See if we've already created this one */
150 : 0 : employee = gncEmployeeLookup (pdata->book, &*guid);
151 : 0 : if (employee)
152 : : {
153 : 0 : gncEmployeeDestroy (pdata->employee);
154 : 0 : pdata->employee = employee;
155 : 0 : gncEmployeeBeginEdit (employee);
156 : : }
157 : : else
158 : : {
159 : 0 : gncEmployeeSetGUID (pdata->employee, &*guid);
160 : : }
161 : :
162 : 0 : return TRUE;
163 : : }
164 : :
165 : : static gboolean
166 : 0 : employee_id_handler (xmlNodePtr node, gpointer employee_pdata)
167 : : {
168 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
169 : :
170 : 0 : return apply_xmlnode_text (gncEmployeeSetID, pdata->employee, node);
171 : : }
172 : :
173 : : static gboolean
174 : 0 : employee_language_handler (xmlNodePtr node, gpointer employee_pdata)
175 : : {
176 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
177 : :
178 : 0 : return apply_xmlnode_text (gncEmployeeSetLanguage, pdata->employee, node);
179 : : }
180 : :
181 : : static gboolean
182 : 0 : employee_acl_handler (xmlNodePtr node, gpointer employee_pdata)
183 : : {
184 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
185 : :
186 : 0 : return apply_xmlnode_text (gncEmployeeSetAcl, pdata->employee, node);
187 : : }
188 : :
189 : : static gboolean
190 : 0 : employee_addr_handler (xmlNodePtr node, gpointer employee_pdata)
191 : : {
192 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
193 : :
194 : 0 : return gnc_dom_tree_to_address (node, gncEmployeeGetAddr (pdata->employee));
195 : : }
196 : :
197 : : static gboolean
198 : 0 : employee_active_handler (xmlNodePtr node, gpointer employee_pdata)
199 : : {
200 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
201 : : gint64 val;
202 : : gboolean ret;
203 : :
204 : 0 : ret = dom_tree_to_integer (node, &val);
205 : 0 : if (ret)
206 : 0 : gncEmployeeSetActive (pdata->employee, (gboolean)val);
207 : :
208 : 0 : return ret;
209 : : }
210 : :
211 : : static gboolean
212 : 0 : employee_workday_handler (xmlNodePtr node, gpointer employee_pdata)
213 : : {
214 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
215 : :
216 : 0 : gncEmployeeSetWorkday (pdata->employee, dom_tree_to_gnc_numeric (node));
217 : 0 : return TRUE;
218 : : }
219 : :
220 : : static gboolean
221 : 0 : employee_rate_handler (xmlNodePtr node, gpointer employee_pdata)
222 : : {
223 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
224 : :
225 : 0 : gncEmployeeSetRate (pdata->employee, dom_tree_to_gnc_numeric (node));
226 : 0 : return TRUE;
227 : : }
228 : :
229 : : static gboolean
230 : 0 : employee_currency_handler (xmlNodePtr node, gpointer employee_pdata)
231 : : {
232 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
233 : : gnc_commodity* com;
234 : :
235 : 0 : com = dom_tree_to_commodity_ref (node, pdata->book);
236 : 0 : g_return_val_if_fail (com, FALSE);
237 : :
238 : 0 : gncEmployeeSetCurrency (pdata->employee, com);
239 : :
240 : 0 : return TRUE;
241 : : }
242 : :
243 : : static gboolean
244 : 0 : employee_ccard_handler (xmlNodePtr node, gpointer employee_pdata)
245 : : {
246 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
247 : : Account* ccard_acc;
248 : :
249 : 0 : auto guid = dom_tree_to_guid (node);
250 : 0 : g_return_val_if_fail (guid, FALSE);
251 : :
252 : 0 : ccard_acc = xaccAccountLookup (&*guid, pdata->book);
253 : :
254 : 0 : g_return_val_if_fail (ccard_acc, FALSE);
255 : 0 : gncEmployeeSetCCard (pdata->employee, ccard_acc);
256 : :
257 : 0 : return TRUE;
258 : : }
259 : :
260 : : static gboolean
261 : 0 : employee_slots_handler (xmlNodePtr node, gpointer employee_pdata)
262 : : {
263 : 0 : struct employee_pdata* pdata = static_cast<decltype (pdata)> (employee_pdata);
264 : 0 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->employee));
265 : : }
266 : :
267 : : static struct dom_tree_handler employee_handlers_v2[] =
268 : : {
269 : : { employee_username_string, employee_username_handler, 1, 0 },
270 : : { employee_guid_string, employee_guid_handler, 1, 0 },
271 : : { employee_id_string, employee_id_handler, 1, 0 },
272 : : { employee_addr_string, employee_addr_handler, 1, 0 },
273 : : { employee_language_string, employee_language_handler, 0, 0 },
274 : : { employee_acl_string, employee_acl_handler, 0, 0 },
275 : : { employee_active_string, employee_active_handler, 1, 0 },
276 : : { employee_workday_string, employee_workday_handler, 1, 0 },
277 : : { employee_rate_string, employee_rate_handler, 1, 0 },
278 : : { employee_currency_string, employee_currency_handler, 0, 0 }, /* XXX */
279 : : { "employee:commodity", employee_currency_handler, 0, 0 }, /* XXX */
280 : : { employee_ccard_string, employee_ccard_handler, 0, 0 },
281 : : { employee_slots_string, employee_slots_handler, 0, 0 },
282 : : { NULL, 0, 0, 0 }
283 : : };
284 : :
285 : : static GncEmployee*
286 : 0 : dom_tree_to_employee (xmlNodePtr node, QofBook* book)
287 : : {
288 : : struct employee_pdata employee_pdata;
289 : : gboolean successful;
290 : :
291 : 0 : employee_pdata.employee = gncEmployeeCreate (book);
292 : 0 : employee_pdata.book = book;
293 : 0 : gncEmployeeBeginEdit (employee_pdata.employee);
294 : :
295 : 0 : successful = dom_tree_generic_parse (node, employee_handlers_v2,
296 : : &employee_pdata);
297 : 0 : if (successful)
298 : 0 : gncEmployeeCommitEdit (employee_pdata.employee);
299 : : else
300 : : {
301 : 0 : PERR ("failed to parse employee tree");
302 : 0 : gncEmployeeDestroy (employee_pdata.employee);
303 : 0 : employee_pdata.employee = NULL;
304 : : }
305 : :
306 : 0 : return employee_pdata.employee;
307 : : }
308 : :
309 : : static gboolean
310 : 0 : gnc_employee_end_handler (gpointer data_for_children,
311 : : GSList* data_from_children, GSList* sibling_data,
312 : : gpointer parent_data, gpointer global_data,
313 : : gpointer* result, const gchar* tag)
314 : : {
315 : : GncEmployee* employee;
316 : 0 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
317 : 0 : gxpf_data* gdata = (gxpf_data*)global_data;
318 : 0 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
319 : :
320 : :
321 : 0 : if (parent_data)
322 : : {
323 : 0 : return TRUE;
324 : : }
325 : :
326 : : /* OK. For some messed up reason this is getting called again with a
327 : : NULL tag. So we ignore those cases */
328 : 0 : if (!tag)
329 : : {
330 : 0 : return TRUE;
331 : : }
332 : :
333 : 0 : g_return_val_if_fail (tree, FALSE);
334 : :
335 : 0 : employee = dom_tree_to_employee (tree, book);
336 : 0 : if (employee != NULL)
337 : : {
338 : 0 : gdata->cb (tag, gdata->parsedata, employee);
339 : : }
340 : :
341 : 0 : xmlFreeNode (tree);
342 : :
343 : 0 : return employee != NULL;
344 : : }
345 : :
346 : : static sixtp*
347 : 22 : employee_sixtp_parser_create (void)
348 : : {
349 : 22 : return sixtp_dom_parser_new (gnc_employee_end_handler, NULL, NULL);
350 : : }
351 : :
352 : : static gboolean
353 : 0 : employee_should_be_saved (GncEmployee* employee)
354 : : {
355 : : const char* id;
356 : :
357 : : /* make sure this is a valid employee before we save it -- should have an ID */
358 : 0 : id = gncEmployeeGetID (employee);
359 : 0 : if (id == NULL || *id == '\0')
360 : 0 : return FALSE;
361 : :
362 : 0 : return TRUE;
363 : : }
364 : :
365 : : static void
366 : 0 : do_count (QofInstance* employee_p, gpointer count_p)
367 : : {
368 : 0 : int* count = static_cast<decltype (count)> (count_p);
369 : 0 : if (employee_should_be_saved ((GncEmployee*) employee_p))
370 : 0 : (*count)++;
371 : 0 : }
372 : :
373 : : static int
374 : 4 : employee_get_count (QofBook* book)
375 : : {
376 : 4 : int count = 0;
377 : 4 : qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
378 : 4 : return count;
379 : : }
380 : :
381 : : static void
382 : 0 : xml_add_employee (QofInstance* employee_p, gpointer out_p)
383 : : {
384 : : xmlNodePtr node;
385 : 0 : GncEmployee* employee = (GncEmployee*) employee_p;
386 : 0 : FILE* out = static_cast<decltype (out)> (out_p);
387 : :
388 : 0 : if (ferror (out))
389 : 0 : return;
390 : 0 : if (!employee_should_be_saved (employee))
391 : 0 : return;
392 : :
393 : 0 : node = employee_dom_tree_create (employee);
394 : 0 : xmlElemDump (out, NULL, node);
395 : 0 : xmlFreeNode (node);
396 : 0 : if (ferror (out) || fprintf (out, "\n") < 0)
397 : 0 : return;
398 : : }
399 : :
400 : : static gboolean
401 : 4 : employee_write (FILE* out, QofBook* book)
402 : : {
403 : 4 : qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_employee,
404 : : (gpointer) out);
405 : 4 : return ferror (out) == 0;
406 : : }
407 : :
408 : : static gboolean
409 : 4 : employee_ns (FILE* out)
410 : : {
411 : 4 : g_return_val_if_fail (out, FALSE);
412 : 4 : return gnc_xml2_write_namespace_decl (out, "employee");
413 : : }
414 : :
415 : : void
416 : 15 : gnc_employee_xml_initialize (void)
417 : : {
418 : : static GncXmlDataType_t be_data =
419 : : {
420 : : GNC_FILE_BACKEND_VERS,
421 : : gnc_employee_string,
422 : : employee_sixtp_parser_create,
423 : : NULL, /* add_item */
424 : : employee_get_count,
425 : : employee_write,
426 : : NULL, /* scrub */
427 : : employee_ns,
428 : : };
429 : :
430 : 15 : gnc_xml_register_backend (be_data);
431 : 15 : }
|