LCOV - code coverage report
Current view: top level - libgnucash/backend/xml - gnc-owner-xml-v2.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 59.3 % 91 54
Test Date: 2025-02-07 16:25:45 Functions: 100.0 % 6 6
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     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                 :           4 : owner_type_handler (xmlNodePtr node, gpointer owner_pdata)
     101                 :             : {
     102                 :           4 :     struct owner_pdata* pdata = static_cast<decltype (pdata)> (owner_pdata);
     103                 :           4 :     char* txt = dom_tree_to_text (node);
     104                 :           4 :     g_return_val_if_fail (txt, FALSE);
     105                 :             : 
     106                 :           4 :     if (!g_strcmp0 (txt, GNC_ID_CUSTOMER))
     107                 :           0 :         gncOwnerInitCustomer (pdata->owner, NULL);
     108                 :           4 :     else if (!g_strcmp0 (txt, GNC_ID_JOB))
     109                 :           1 :         gncOwnerInitJob (pdata->owner, NULL);
     110                 :           3 :     else if (!g_strcmp0 (txt, GNC_ID_VENDOR))
     111                 :           3 :         gncOwnerInitVendor (pdata->owner, NULL);
     112                 :           0 :     else if (!g_strcmp0 (txt, GNC_ID_EMPLOYEE))
     113                 :           0 :         gncOwnerInitEmployee (pdata->owner, NULL);
     114                 :             :     else
     115                 :             :     {
     116                 :           0 :         PWARN ("Unknown owner type: %s", txt);
     117                 :           0 :         g_free (txt);
     118                 :           0 :         return FALSE;
     119                 :             :     }
     120                 :             : 
     121                 :           4 :     g_free (txt);
     122                 :           4 :     return TRUE;
     123                 :             : }
     124                 :             : 
     125                 :             : static gboolean
     126                 :           4 : owner_id_handler (xmlNodePtr node, gpointer owner_pdata)
     127                 :             : {
     128                 :           4 :     struct owner_pdata* pdata = static_cast<decltype (pdata)> (owner_pdata);
     129                 :             :     GncGUID* guid;
     130                 :             : 
     131                 :           4 :     guid = dom_tree_to_guid (node);
     132                 :           4 :     g_return_val_if_fail (guid, FALSE);
     133                 :             : 
     134                 :           4 :     switch (gncOwnerGetType (pdata->owner))
     135                 :             :     {
     136                 :           0 :     case GNC_OWNER_CUSTOMER:
     137                 :             :     {
     138                 :           0 :         GncCustomer* cust = gncCustomerLookup (pdata->book, guid);
     139                 :           0 :         if (!cust)
     140                 :             :         {
     141                 :           0 :             cust = gncCustomerCreate (pdata->book);
     142                 :           0 :             gncCustomerSetGUID (cust, guid);
     143                 :             :         }
     144                 :           0 :         gncOwnerInitCustomer (pdata->owner, cust);
     145                 :           0 :         break;
     146                 :             :     }
     147                 :           1 :     case GNC_OWNER_JOB:
     148                 :             :     {
     149                 :           1 :         GncJob* job = gncJobLookup (pdata->book, guid);
     150                 :           1 :         if (!job)
     151                 :             :         {
     152                 :           1 :             job = gncJobCreate (pdata->book);
     153                 :           1 :             gncJobSetGUID (job, guid);
     154                 :             :         }
     155                 :           1 :         gncOwnerInitJob (pdata->owner, job);
     156                 :           1 :         break;
     157                 :             :     }
     158                 :           3 :     case GNC_OWNER_VENDOR:
     159                 :             :     {
     160                 :           3 :         GncVendor* vendor = gncVendorLookup (pdata->book, guid);
     161                 :           3 :         if (!vendor)
     162                 :             :         {
     163                 :           3 :             vendor = gncVendorCreate (pdata->book);
     164                 :           3 :             gncVendorSetGUID (vendor, guid);
     165                 :             :         }
     166                 :           3 :         gncOwnerInitVendor (pdata->owner, vendor);
     167                 :           3 :         break;
     168                 :             :     }
     169                 :           0 :     case GNC_OWNER_EMPLOYEE:
     170                 :             :     {
     171                 :           0 :         GncEmployee* employee = gncEmployeeLookup (pdata->book, guid);
     172                 :           0 :         if (!employee)
     173                 :             :         {
     174                 :           0 :             employee = gncEmployeeCreate (pdata->book);
     175                 :           0 :             gncEmployeeSetGUID (employee, guid);
     176                 :             :         }
     177                 :           0 :         gncOwnerInitEmployee (pdata->owner, employee);
     178                 :           0 :         break;
     179                 :             :     }
     180                 :           0 :     default:
     181                 :           0 :         PWARN ("Invalid owner type: %d\n", gncOwnerGetType (pdata->owner));
     182                 :           0 :         guid_free (guid);
     183                 :           0 :         return FALSE;
     184                 :             :     }
     185                 :             : 
     186                 :           4 :     guid_free (guid);
     187                 :           4 :     return TRUE;
     188                 :             : }
     189                 :             : 
     190                 :             : static struct dom_tree_handler owner_handlers_v2[] =
     191                 :             : {
     192                 :             :     { owner_type_string, owner_type_handler, 1, 0 },
     193                 :             :     { owner_id_string, owner_id_handler, 1, 0 },
     194                 :             :     { NULL, 0, 0, 0 }
     195                 :             : };
     196                 :             : 
     197                 :             : gboolean
     198                 :           4 : gnc_dom_tree_to_owner (xmlNodePtr node, GncOwner* owner, QofBook* book)
     199                 :             : {
     200                 :             :     struct owner_pdata owner_pdata;
     201                 :             :     gboolean successful;
     202                 :             : 
     203                 :           4 :     owner_pdata.owner = owner;
     204                 :           4 :     owner_pdata.book = book;
     205                 :             : 
     206                 :           4 :     successful = dom_tree_generic_parse (node, owner_handlers_v2,
     207                 :             :                                          &owner_pdata);
     208                 :             : 
     209                 :           4 :     if (!successful)
     210                 :             :     {
     211                 :           0 :         PERR ("failed to parse owner tree");
     212                 :             :     }
     213                 :             : 
     214                 :           4 :     return successful;
     215                 :             : }
     216                 :             : 
     217                 :             : static gboolean
     218                 :           4 : owner_ns (FILE* out)
     219                 :             : {
     220                 :           4 :     g_return_val_if_fail (out, FALSE);
     221                 :           4 :     return gnc_xml2_write_namespace_decl (out, "owner");
     222                 :             : }
     223                 :             : 
     224                 :             : void
     225                 :          61 : gnc_owner_xml_initialize (void)
     226                 :             : {
     227                 :             :     static GncXmlDataType_t be_data =
     228                 :             :     {
     229                 :             :         GNC_FILE_BACKEND_VERS,
     230                 :             :         "gnc:Owner",
     231                 :             :         NULL,           /* parser_create */
     232                 :             :         NULL,           /* add_item */
     233                 :             :         NULL,           /* get_count */
     234                 :             :         NULL,           /* write */
     235                 :             :         NULL,           /* scrub */
     236                 :             :         owner_ns,
     237                 :             :     };
     238                 :             : 
     239                 :          61 :     gnc_xml_register_backend (be_data);
     240                 :          61 : }
        

Generated by: LCOV version 2.0-1