Line data Source code
1 : /********************************************************************\
2 : * gnc-tax-table-xml-v2.c -- tax table 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 "gncEntry.h"
30 : #include "gncTaxTableP.h"
31 :
32 : #include "gnc-xml-helper.h"
33 : #include "sixtp.h"
34 : #include "sixtp-utils.h"
35 : #include "sixtp-parsers.h"
36 : #include "sixtp-utils.h"
37 : #include "sixtp-dom-parsers.h"
38 : #include "sixtp-dom-generators.h"
39 :
40 : #include "gnc-xml.h"
41 : #include "io-gncxml-gen.h"
42 : #include "io-gncxml-v2.h"
43 :
44 : #include "gnc-tax-table-xml-v2.h"
45 :
46 : #define _GNC_MOD_NAME GNC_ID_TAXTABLE
47 :
48 : static QofLogModule log_module = GNC_MOD_IO;
49 :
50 : const gchar* taxtable_version_string = "2.0.0";
51 :
52 : /* ids */
53 : #define gnc_taxtable_string "gnc:GncTaxTable"
54 : #define taxtable_guid_string "taxtable:guid"
55 : #define taxtable_name_string "taxtable:name"
56 : #define taxtable_refcount_string "taxtable:refcount"
57 : #define taxtable_invisible_string "taxtable:invisible"
58 : #define taxtable_parent_string "taxtable:parent"
59 : #define taxtable_child_string "taxtable:child"
60 : #define taxtable_entries_string "taxtable:entries"
61 : #define taxtable_slots_string "taxtable:slots"
62 :
63 : #define gnc_taxtableentry_string "gnc:GncTaxTableEntry"
64 : #define ttentry_account_string "tte:acct"
65 : #define ttentry_type_string "tte:type"
66 : #define ttentry_amount_string "tte:amount"
67 :
68 : static void
69 0 : maybe_add_guid (xmlNodePtr ptr, const char* tag, GncTaxTable* table)
70 : {
71 0 : if (table)
72 0 : xmlAddChild (ptr, guid_to_dom_tree (tag,
73 0 : qof_instance_get_guid (QOF_INSTANCE (table))));
74 0 : }
75 :
76 : static xmlNodePtr
77 0 : ttentry_dom_tree_create (GncTaxTableEntry* entry)
78 : {
79 : xmlNodePtr ret;
80 : Account* account;
81 : gnc_numeric amount;
82 :
83 0 : ret = xmlNewNode (NULL, BAD_CAST gnc_taxtableentry_string);
84 :
85 0 : account = gncTaxTableEntryGetAccount (entry);
86 0 : if (account)
87 0 : xmlAddChild (ret, guid_to_dom_tree (ttentry_account_string,
88 0 : qof_instance_get_guid (QOF_INSTANCE (account))));
89 :
90 0 : amount = gncTaxTableEntryGetAmount (entry);
91 0 : xmlAddChild (ret, gnc_numeric_to_dom_tree (ttentry_amount_string, &amount));
92 :
93 0 : xmlAddChild (ret, text_to_dom_tree (ttentry_type_string,
94 : gncAmountTypeToString (
95 : gncTaxTableEntryGetType (entry))));
96 :
97 0 : return ret;
98 : }
99 :
100 : static xmlNodePtr
101 0 : taxtable_dom_tree_create (GncTaxTable* table)
102 : {
103 : xmlNodePtr ret, entries;
104 : GList* list;
105 :
106 0 : ret = xmlNewNode (NULL, BAD_CAST gnc_taxtable_string);
107 0 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST taxtable_version_string);
108 :
109 0 : maybe_add_guid (ret, taxtable_guid_string, table);
110 0 : xmlAddChild (ret, text_to_dom_tree (taxtable_name_string,
111 : gncTaxTableGetName (table)));
112 :
113 0 : xmlAddChild (ret, int_to_dom_tree (taxtable_refcount_string,
114 : gncTaxTableGetRefcount (table)));
115 0 : xmlAddChild (ret, int_to_dom_tree (taxtable_invisible_string,
116 0 : gncTaxTableGetInvisible (table)));
117 :
118 : /* We should not be our own child */
119 0 : if (gncTaxTableGetChild (table) != table)
120 0 : maybe_add_guid (ret, taxtable_child_string, gncTaxTableGetChild (table));
121 :
122 0 : maybe_add_guid (ret, taxtable_parent_string, gncTaxTableGetParent (table));
123 :
124 0 : entries = xmlNewChild (ret, NULL, BAD_CAST taxtable_entries_string, NULL);
125 0 : for (list = gncTaxTableGetEntries (table); list; list = list->next)
126 : {
127 0 : GncTaxTableEntry* entry = static_cast<decltype (entry)> (list->data);
128 0 : xmlAddChild (entries, ttentry_dom_tree_create (entry));
129 : }
130 :
131 : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
132 0 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (taxtable_slots_string,
133 0 : QOF_INSTANCE (table)));
134 0 : return ret;
135 : }
136 :
137 : /***********************************************************************/
138 :
139 : struct ttentry_pdata
140 : {
141 : GncTaxTableEntry* ttentry;
142 : QofBook* book;
143 : };
144 :
145 : static gboolean
146 1 : ttentry_acct_handler (xmlNodePtr node, gpointer ttentry_pdata)
147 : {
148 1 : struct ttentry_pdata* pdata = static_cast<decltype (pdata)> (ttentry_pdata);
149 : Account* acc;
150 :
151 1 : auto guid = dom_tree_to_guid (node);
152 1 : g_return_val_if_fail (guid, FALSE);
153 1 : acc = xaccAccountLookup (&*guid, pdata->book);
154 1 : g_return_val_if_fail (acc, FALSE);
155 :
156 1 : gncTaxTableEntrySetAccount (pdata->ttentry, acc);
157 1 : return TRUE;
158 : }
159 :
160 : static gboolean
161 1 : ttentry_type_handler (xmlNodePtr node, gpointer ttentry_pdata)
162 : {
163 1 : struct ttentry_pdata* pdata = static_cast<decltype (pdata)> (ttentry_pdata);
164 1 : auto tte_settype = [](GncTaxTableEntry* tt, const char *str)
165 : {
166 : GncAmountType type;
167 1 : if (gncAmountStringToType (str, &type))
168 1 : gncTaxTableEntrySetType (tt, type);
169 1 : };
170 2 : return apply_xmlnode_text (tte_settype, pdata->ttentry, node);
171 : }
172 :
173 : static gboolean
174 1 : ttentry_amount_handler (xmlNodePtr node, gpointer ttentry_pdata)
175 : {
176 1 : struct ttentry_pdata* pdata = static_cast<decltype (pdata)> (ttentry_pdata);
177 :
178 1 : gncTaxTableEntrySetAmount (pdata->ttentry, dom_tree_to_gnc_numeric (node));
179 1 : return TRUE;
180 : }
181 :
182 : static struct dom_tree_handler ttentry_handlers_v2[] =
183 : {
184 : { ttentry_account_string, ttentry_acct_handler, 0, 0 },
185 : { ttentry_type_string, ttentry_type_handler, 1, 0 },
186 : { ttentry_amount_string, ttentry_amount_handler, 1, 0 },
187 : { NULL, 0, 0, 0 }
188 : };
189 :
190 : static GncTaxTableEntry*
191 1 : dom_tree_to_ttentry (xmlNodePtr node, QofBook* book)
192 : {
193 : struct ttentry_pdata ttentry_pdata;
194 : gboolean successful;
195 :
196 1 : ttentry_pdata.ttentry = gncTaxTableEntryCreate ();
197 1 : ttentry_pdata.book = book;
198 :
199 1 : successful = dom_tree_generic_parse (node, ttentry_handlers_v2,
200 : &ttentry_pdata);
201 :
202 1 : if (!successful)
203 : {
204 0 : PERR ("failed to parse tax table entry tree");
205 0 : gncTaxTableEntryDestroy (ttentry_pdata.ttentry);
206 0 : ttentry_pdata.ttentry = NULL;
207 : }
208 :
209 1 : return ttentry_pdata.ttentry;
210 : }
211 :
212 : /***********************************************************************/
213 :
214 : struct taxtable_pdata
215 : {
216 : GncTaxTable* table;
217 : QofBook* book;
218 : };
219 :
220 : static gboolean
221 0 : set_parent_child (xmlNodePtr node, struct taxtable_pdata* pdata,
222 : void (*func) (GncTaxTable*, GncTaxTable*))
223 : {
224 : GncTaxTable* table;
225 :
226 0 : auto guid = dom_tree_to_guid (node);
227 0 : g_return_val_if_fail (guid, FALSE);
228 0 : table = gncTaxTableLookup (pdata->book, &*guid);
229 :
230 : /* Ignore pointers to self */
231 0 : if (table == pdata->table)
232 : {
233 0 : PINFO ("found a self-referential parent/child; ignoring.\n");
234 0 : return TRUE;
235 : }
236 :
237 0 : if (!table)
238 : {
239 0 : table = gncTaxTableCreate (pdata->book);
240 0 : gncTaxTableBeginEdit (table);
241 0 : gncTaxTableSetGUID (table, &*guid);
242 0 : gncTaxTableCommitEdit (table);
243 : }
244 0 : g_return_val_if_fail (table, FALSE);
245 0 : func (pdata->table, table);
246 :
247 0 : return TRUE;
248 : }
249 :
250 : static gboolean
251 1 : taxtable_guid_handler (xmlNodePtr node, gpointer taxtable_pdata)
252 : {
253 1 : struct taxtable_pdata* pdata = static_cast<decltype (pdata)> (taxtable_pdata);
254 : GncTaxTable* table;
255 :
256 1 : auto guid = dom_tree_to_guid (node);
257 1 : g_return_val_if_fail (guid, FALSE);
258 1 : table = gncTaxTableLookup (pdata->book, &*guid);
259 1 : if (table)
260 : {
261 0 : gncTaxTableDestroy (pdata->table);
262 0 : pdata->table = table;
263 0 : gncTaxTableBeginEdit (table);
264 : }
265 : else
266 : {
267 1 : gncTaxTableSetGUID (pdata->table, &*guid);
268 : }
269 :
270 1 : return TRUE;
271 : }
272 :
273 : static gboolean
274 1 : taxtable_name_handler (xmlNodePtr node, gpointer taxtable_pdata)
275 : {
276 1 : struct taxtable_pdata* pdata = static_cast<decltype (pdata)> (taxtable_pdata);
277 1 : return apply_xmlnode_text (gncTaxTableSetName, pdata->table, node);
278 : }
279 :
280 : static gboolean
281 1 : taxtable_refcount_handler (xmlNodePtr node, gpointer taxtable_pdata)
282 : {
283 1 : struct taxtable_pdata* pdata = static_cast<decltype (pdata)> (taxtable_pdata);
284 : gint64 val;
285 :
286 1 : dom_tree_to_integer (node, &val);
287 1 : gncTaxTableSetRefcount (pdata->table, val);
288 1 : return TRUE;
289 : }
290 :
291 : static gboolean
292 1 : taxtable_invisible_handler (xmlNodePtr node, gpointer taxtable_pdata)
293 : {
294 1 : struct taxtable_pdata* pdata = static_cast<decltype (pdata)> (taxtable_pdata);
295 : gint64 val;
296 :
297 1 : dom_tree_to_integer (node, &val);
298 1 : if (val)
299 0 : gncTaxTableMakeInvisible (pdata->table);
300 1 : return TRUE;
301 : }
302 :
303 : static gboolean
304 0 : taxtable_parent_handler (xmlNodePtr node, gpointer taxtable_pdata)
305 : {
306 0 : struct taxtable_pdata* pdata = static_cast<decltype (pdata)> (taxtable_pdata);
307 0 : return set_parent_child (node, pdata, gncTaxTableSetParent);
308 : }
309 :
310 : static gboolean
311 0 : taxtable_child_handler (xmlNodePtr node, gpointer taxtable_pdata)
312 : {
313 0 : struct taxtable_pdata* pdata = static_cast<decltype (pdata)> (taxtable_pdata);
314 0 : return set_parent_child (node, pdata, gncTaxTableSetChild);
315 : }
316 :
317 : static gboolean
318 1 : taxtable_entries_handler (xmlNodePtr node, gpointer taxtable_pdata)
319 : {
320 1 : struct taxtable_pdata* pdata = static_cast<decltype (pdata)> (taxtable_pdata);
321 : xmlNodePtr mark;
322 :
323 1 : g_return_val_if_fail (node, FALSE);
324 1 : g_return_val_if_fail (node->xmlChildrenNode, FALSE);
325 :
326 4 : for (mark = node->xmlChildrenNode; mark; mark = mark->next)
327 : {
328 : GncTaxTableEntry* entry;
329 :
330 3 : if (g_strcmp0 ("text", (char*)mark->name) == 0)
331 2 : continue;
332 :
333 1 : if (g_strcmp0 (gnc_taxtableentry_string, (char*)mark->name))
334 0 : return FALSE;
335 :
336 1 : entry = dom_tree_to_ttentry (mark, pdata->book);
337 :
338 1 : if (entry)
339 1 : gncTaxTableAddEntry (pdata->table, entry);
340 : else
341 0 : return FALSE;
342 :
343 : }
344 1 : return TRUE;
345 : }
346 :
347 : static gboolean
348 0 : taxtable_slots_handler (xmlNodePtr node, gpointer taxtable_pdata)
349 : {
350 0 : struct taxtable_pdata* pdata = static_cast<decltype (pdata)> (taxtable_pdata);
351 :
352 0 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->table));
353 : }
354 :
355 : static struct dom_tree_handler taxtable_handlers_v2[] =
356 : {
357 : { taxtable_guid_string, taxtable_guid_handler, 1, 0 },
358 : { taxtable_name_string, taxtable_name_handler, 1, 0 },
359 : { taxtable_refcount_string, taxtable_refcount_handler, 1, 0 },
360 : { taxtable_invisible_string, taxtable_invisible_handler, 1, 0 },
361 : { taxtable_parent_string, taxtable_parent_handler, 0, 0 },
362 : { taxtable_child_string, taxtable_child_handler, 0, 0 },
363 : { taxtable_entries_string, taxtable_entries_handler, 1, 0 },
364 : { taxtable_slots_string, taxtable_slots_handler, 0, 0 },
365 : { NULL, 0, 0, 0 }
366 : };
367 :
368 : static GncTaxTable*
369 1 : dom_tree_to_taxtable (xmlNodePtr node, QofBook* book)
370 : {
371 : struct taxtable_pdata taxtable_pdata;
372 : gboolean successful;
373 :
374 1 : taxtable_pdata.table = gncTaxTableCreate (book);
375 1 : taxtable_pdata.book = book;
376 1 : gncTaxTableBeginEdit (taxtable_pdata.table);
377 :
378 1 : successful = dom_tree_generic_parse (node, taxtable_handlers_v2,
379 : &taxtable_pdata);
380 :
381 1 : if (successful)
382 1 : gncTaxTableCommitEdit (taxtable_pdata.table);
383 : else
384 : {
385 0 : PERR ("failed to parse tax table tree");
386 0 : gncTaxTableDestroy (taxtable_pdata.table);
387 0 : taxtable_pdata.table = NULL;
388 : }
389 :
390 1 : return taxtable_pdata.table;
391 : }
392 :
393 : static gboolean
394 10 : gnc_taxtable_end_handler (gpointer data_for_children,
395 : GSList* data_from_children, GSList* sibling_data,
396 : gpointer parent_data, gpointer global_data,
397 : gpointer* result, const gchar* tag)
398 : {
399 : GncTaxTable* table;
400 10 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
401 10 : gxpf_data* gdata = (gxpf_data*)global_data;
402 10 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
403 :
404 10 : if (parent_data)
405 : {
406 9 : return TRUE;
407 : }
408 :
409 : /* OK. For some messed up reason this is getting called again with a
410 : NULL tag. So we ignore those cases */
411 1 : if (!tag)
412 : {
413 0 : return TRUE;
414 : }
415 :
416 1 : g_return_val_if_fail (tree, FALSE);
417 :
418 1 : table = dom_tree_to_taxtable (tree, book);
419 1 : if (table != NULL)
420 : {
421 1 : gdata->cb (tag, gdata->parsedata, table);
422 : }
423 :
424 1 : xmlFreeNode (tree);
425 :
426 1 : return table != NULL;
427 : }
428 :
429 : static sixtp*
430 22 : taxtable_sixtp_parser_create (void)
431 : {
432 22 : return sixtp_dom_parser_new (gnc_taxtable_end_handler, NULL, NULL);
433 : }
434 :
435 : static void
436 0 : do_count (QofInstance* table_p, gpointer count_p)
437 : {
438 0 : int* count = static_cast<decltype (count)> (count_p);
439 0 : (*count)++;
440 0 : }
441 :
442 : static int
443 4 : taxtable_get_count (QofBook* book)
444 : {
445 4 : int count = 0;
446 4 : qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
447 4 : return count;
448 : }
449 :
450 : static void
451 0 : xml_add_taxtable (QofInstance* table_p, gpointer out_p)
452 : {
453 : xmlNodePtr node;
454 0 : GncTaxTable* table = (GncTaxTable*) table_p;
455 0 : FILE* out = static_cast<decltype (out)> (out_p);
456 :
457 0 : if (ferror (out))
458 0 : return;
459 :
460 0 : node = taxtable_dom_tree_create (table);
461 0 : xmlElemDump (out, NULL, node);
462 0 : xmlFreeNode (node);
463 0 : if (ferror (out) || fprintf (out, "\n") < 0)
464 0 : return;
465 : }
466 :
467 : static gboolean
468 4 : taxtable_write (FILE* out, QofBook* book)
469 : {
470 4 : qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_taxtable,
471 : (gpointer) out);
472 4 : return ferror (out) == 0;
473 : }
474 :
475 :
476 : static gboolean
477 1 : taxtable_is_grandchild (GncTaxTable* table)
478 : {
479 1 : return (gncTaxTableGetParent (gncTaxTableGetParent (table)) != NULL);
480 : }
481 :
482 : static GncTaxTable*
483 0 : taxtable_find_senior (GncTaxTable* table)
484 : {
485 0 : GncTaxTable* temp, *parent, *gp = NULL;
486 :
487 0 : temp = table;
488 : do
489 : {
490 : /* See if "temp" is a grandchild */
491 0 : parent = gncTaxTableGetParent (temp);
492 0 : if (!parent)
493 0 : break;
494 0 : gp = gncTaxTableGetParent (parent);
495 0 : if (!gp)
496 0 : break;
497 :
498 : /* Yep, this is a grandchild. Move up one generation and try again */
499 0 : temp = parent;
500 : }
501 : while (TRUE);
502 :
503 : /* Ok, at this point temp points to the most senior child and parent
504 : * should point to the top taxtable (and gp should be NULL). If
505 : * parent is NULL then we are the most senior child (and have no
506 : * children), so do nothing. If temp == table then there is no
507 : * grandparent, so do nothing.
508 : *
509 : * Do something if parent != NULL && temp != table
510 : */
511 0 : g_assert (gp == NULL);
512 :
513 : /* return the most senior table */
514 0 : return temp;
515 : }
516 :
517 : /* build a list of tax tables that are grandchildren or bogus (empty entry list). */
518 : static void
519 1 : taxtable_scrub_cb (QofInstance* table_p, gpointer list_p)
520 : {
521 1 : GncTaxTable* table = GNC_TAXTABLE (table_p);
522 1 : GList** list = static_cast<decltype (list)> (list_p);
523 :
524 1 : if (taxtable_is_grandchild (table) || gncTaxTableGetEntries (table) == NULL)
525 0 : *list = g_list_prepend (*list, table);
526 1 : }
527 :
528 : /* for each entry, check the tax tables. If the tax tables are
529 : * grandchildren, then fix them to point to the most senior child
530 : */
531 : static void
532 4 : taxtable_scrub_entries (QofInstance* entry_p, gpointer ht_p)
533 : {
534 4 : GHashTable* ht = static_cast<decltype (ht)> (ht_p);
535 4 : GncEntry* entry = GNC_ENTRY (entry_p);
536 : GncTaxTable* table, *new_tt;
537 : gint32 count;
538 :
539 4 : table = gncEntryGetInvTaxTable (entry);
540 4 : if (table)
541 : {
542 0 : if (taxtable_is_grandchild (table))
543 : {
544 : gchar guidstr[GUID_ENCODING_LENGTH + 1];
545 0 : guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (entry)), guidstr);
546 0 : PINFO ("Fixing i-taxtable on entry %s\n", guidstr);
547 0 : new_tt = taxtable_find_senior (table);
548 0 : gncEntryBeginEdit (entry);
549 0 : gncEntrySetInvTaxTable (entry, new_tt);
550 0 : gncEntryCommitEdit (entry);
551 0 : table = new_tt;
552 : }
553 0 : if (table)
554 : {
555 0 : count = GPOINTER_TO_INT (g_hash_table_lookup (ht, table));
556 0 : count++;
557 0 : g_hash_table_insert (ht, table, GINT_TO_POINTER (count));
558 : }
559 : }
560 :
561 4 : table = gncEntryGetBillTaxTable (entry);
562 4 : if (table)
563 : {
564 0 : if (taxtable_is_grandchild (table))
565 : {
566 : gchar guidstr[GUID_ENCODING_LENGTH + 1];
567 0 : guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (entry)), guidstr);
568 0 : PINFO ("Fixing b-taxtable on entry %s\n", guidstr);
569 0 : new_tt = taxtable_find_senior (table);
570 0 : gncEntryBeginEdit (entry);
571 0 : gncEntrySetBillTaxTable (entry, new_tt);
572 0 : gncEntryCommitEdit (entry);
573 0 : table = new_tt;
574 : }
575 0 : if (table)
576 : {
577 0 : count = GPOINTER_TO_INT (g_hash_table_lookup (ht, table));
578 0 : count++;
579 0 : g_hash_table_insert (ht, table, GINT_TO_POINTER (count));
580 : }
581 : }
582 4 : }
583 :
584 : static void
585 1 : taxtable_scrub_cust (QofInstance* cust_p, gpointer ht_p)
586 : {
587 1 : GHashTable* ht = static_cast<decltype (ht)> (ht_p);
588 1 : GncCustomer* cust = GNC_CUSTOMER (cust_p);
589 : GncTaxTable* table;
590 : gint32 count;
591 :
592 1 : table = gncCustomerGetTaxTable (cust);
593 1 : if (table)
594 : {
595 0 : count = GPOINTER_TO_INT (g_hash_table_lookup (ht, table));
596 0 : count++;
597 0 : g_hash_table_insert (ht, table, GINT_TO_POINTER (count));
598 : }
599 1 : }
600 :
601 : static void
602 3 : taxtable_scrub_vendor (QofInstance* vendor_p, gpointer ht_p)
603 : {
604 3 : GHashTable* ht = static_cast<decltype (ht)> (ht_p);
605 3 : GncVendor* vendor = GNC_VENDOR (vendor_p);
606 : GncTaxTable* table;
607 : gint32 count;
608 :
609 3 : table = gncVendorGetTaxTable (vendor);
610 3 : if (table)
611 : {
612 0 : count = GPOINTER_TO_INT (g_hash_table_lookup (ht, table));
613 0 : count++;
614 0 : g_hash_table_insert (ht, table, GINT_TO_POINTER (count));
615 : }
616 3 : }
617 :
618 : static void
619 0 : taxtable_reset_refcount (gpointer key, gpointer value, gpointer notused)
620 : {
621 0 : GncTaxTable* table = static_cast<decltype (table)> (key);
622 0 : gint32 count = GPOINTER_TO_INT (value);
623 :
624 0 : if (count != gncTaxTableGetRefcount (table) &&
625 0 : !gncTaxTableGetInvisible (table))
626 : {
627 : gchar guidstr[GUID_ENCODING_LENGTH + 1];
628 0 : guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (table)), guidstr);
629 0 : PWARN ("Fixing refcount on taxtable %s (%" G_GINT64_FORMAT " -> %d)\n",
630 : guidstr, gncTaxTableGetRefcount (table), count);
631 0 : gncTaxTableSetRefcount (table, count);
632 : }
633 0 : }
634 :
635 : static void
636 22 : taxtable_scrub (QofBook* book)
637 : {
638 22 : GList* list = NULL;
639 : GList* node;
640 : GncTaxTable* parent, *table;
641 22 : GHashTable* ht = g_hash_table_new (g_direct_hash, g_direct_equal);
642 :
643 22 : qof_object_foreach (GNC_ID_ENTRY, book, taxtable_scrub_entries, ht);
644 22 : qof_object_foreach (GNC_ID_CUSTOMER, book, taxtable_scrub_cust, ht);
645 22 : qof_object_foreach (GNC_ID_VENDOR, book, taxtable_scrub_vendor, ht);
646 22 : qof_object_foreach (GNC_ID_TAXTABLE, book, taxtable_scrub_cb, &list);
647 :
648 : /* destroy the list of "grandchildren" tax tables */
649 22 : for (node = list; node; node = node->next)
650 : {
651 : gchar guidstr[GUID_ENCODING_LENGTH + 1];
652 0 : table = static_cast<decltype (table)> (node->data);
653 :
654 0 : guid_to_string_buff (qof_instance_get_guid (QOF_INSTANCE (table)), guidstr);
655 0 : PINFO ("deleting grandchild taxtable: %s\n", guidstr);
656 :
657 : /* Make sure the parent has no children */
658 0 : parent = gncTaxTableGetParent (table);
659 0 : gncTaxTableSetChild (parent, NULL);
660 :
661 : /* Destroy this tax table */
662 0 : gncTaxTableBeginEdit (table);
663 0 : gncTaxTableDestroy (table);
664 : }
665 :
666 : /* reset the refcounts as necessary */
667 22 : g_hash_table_foreach (ht, taxtable_reset_refcount, NULL);
668 :
669 22 : g_list_free (list);
670 22 : g_hash_table_destroy (ht);
671 22 : }
672 :
673 : static gboolean
674 4 : taxtable_ns (FILE* out)
675 : {
676 4 : g_return_val_if_fail (out, FALSE);
677 : return
678 4 : gnc_xml2_write_namespace_decl (out, "taxtable")
679 4 : && gnc_xml2_write_namespace_decl (out, "tte");
680 : }
681 :
682 : void
683 15 : gnc_taxtable_xml_initialize (void)
684 : {
685 : static GncXmlDataType_t be_data =
686 : {
687 : GNC_FILE_BACKEND_VERS,
688 : gnc_taxtable_string,
689 : taxtable_sixtp_parser_create,
690 : NULL, /* add_item */
691 : taxtable_get_count,
692 : taxtable_write,
693 : taxtable_scrub,
694 : taxtable_ns,
695 : };
696 :
697 15 : gnc_xml_register_backend(be_data);
698 15 : }
|