LCOV - code coverage report
Current view: top level - libgnucash/backend/xml - gnc-tax-table-xml-v2.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 47.6 % 286 136
Test Date: 2025-02-07 16:25:45 Functions: 63.6 % 33 21
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

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

Generated by: LCOV version 2.0-1