GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-owner-xml-v2.cpp
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
48static QofLogModule log_module = GNC_MOD_IO;
49
50const 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
56xmlNodePtr
57gnc_owner_to_dom_tree (const char* tag, const GncOwner* owner)
58{
59 xmlNodePtr ret;
60 const char* type_str;
61
62 switch (gncOwnerGetType (owner))
63 {
64 case GNC_OWNER_CUSTOMER:
65 type_str = GNC_ID_CUSTOMER;
66 break;
67 case GNC_OWNER_JOB:
68 type_str = GNC_ID_JOB;
69 break;
70 case GNC_OWNER_VENDOR:
71 type_str = GNC_ID_VENDOR;
72 break;
73 case GNC_OWNER_EMPLOYEE:
74 type_str = GNC_ID_EMPLOYEE;
75 break;
76 default:
77 PWARN ("Invalid owner type: %d", gncOwnerGetType (owner));
78 return NULL;
79 }
80
81 ret = xmlNewNode (NULL, BAD_CAST tag);
82 xmlSetProp (ret, BAD_CAST "version", BAD_CAST owner_version_string);
83
84 xmlAddChild (ret, text_to_dom_tree (owner_type_string, type_str));
85 xmlAddChild (ret, guid_to_dom_tree (owner_id_string,
86 gncOwnerGetGUID (owner)));
87
88 return ret;
89}
90
91/***********************************************************************/
92
94{
95 GncOwner* owner;
96 QofBook* book;
97};
98
99static gboolean
100owner_type_handler (xmlNodePtr node, gpointer owner_pdata)
101{
102 struct owner_pdata* pdata = static_cast<decltype (pdata)> (owner_pdata);
103 GncOwner* owner = pdata->owner;
104 auto init_owner_type = [](GncOwner* owner, const char* txt)
105 {
106 if (!g_strcmp0 (txt, GNC_ID_CUSTOMER))
107 gncOwnerInitCustomer (owner, NULL);
108 else if (!g_strcmp0 (txt, GNC_ID_JOB))
109 gncOwnerInitJob (owner, NULL);
110 else if (!g_strcmp0 (txt, GNC_ID_VENDOR))
111 gncOwnerInitVendor (owner, NULL);
112 else if (!g_strcmp0 (txt, GNC_ID_EMPLOYEE))
113 gncOwnerInitEmployee (owner, NULL);
114 };
115 return apply_xmlnode_text (init_owner_type, owner, node);
116}
117
118static gboolean
119owner_id_handler (xmlNodePtr node, gpointer owner_pdata)
120{
121 struct owner_pdata* pdata = static_cast<decltype (pdata)> (owner_pdata);
122
123 auto guid = dom_tree_to_guid (node);
124 g_return_val_if_fail (guid, FALSE);
125
126 switch (gncOwnerGetType (pdata->owner))
127 {
128 case GNC_OWNER_CUSTOMER:
129 {
130 GncCustomer* cust = gncCustomerLookup (pdata->book, &*guid);
131 if (!cust)
132 {
133 cust = gncCustomerCreate (pdata->book);
134 gncCustomerSetGUID (cust, &*guid);
135 }
136 gncOwnerInitCustomer (pdata->owner, cust);
137 break;
138 }
139 case GNC_OWNER_JOB:
140 {
141 GncJob* job = gncJobLookup (pdata->book, &*guid);
142 if (!job)
143 {
144 job = gncJobCreate (pdata->book);
145 gncJobSetGUID (job, &*guid);
146 }
147 gncOwnerInitJob (pdata->owner, job);
148 break;
149 }
150 case GNC_OWNER_VENDOR:
151 {
152 GncVendor* vendor = gncVendorLookup (pdata->book, &*guid);
153 if (!vendor)
154 {
155 vendor = gncVendorCreate (pdata->book);
156 gncVendorSetGUID (vendor, &*guid);
157 }
158 gncOwnerInitVendor (pdata->owner, vendor);
159 break;
160 }
161 case GNC_OWNER_EMPLOYEE:
162 {
163 GncEmployee* employee = gncEmployeeLookup (pdata->book, &*guid);
164 if (!employee)
165 {
166 employee = gncEmployeeCreate (pdata->book);
167 gncEmployeeSetGUID (employee, &*guid);
168 }
169 gncOwnerInitEmployee (pdata->owner, employee);
170 break;
171 }
172 default:
173 PWARN ("Invalid owner type: %d\n", gncOwnerGetType (pdata->owner));
174 return FALSE;
175 }
176
177 return TRUE;
178}
179
180static struct dom_tree_handler owner_handlers_v2[] =
181{
182 { owner_type_string, owner_type_handler, 1, 0 },
183 { owner_id_string, owner_id_handler, 1, 0 },
184 { NULL, 0, 0, 0 }
185};
186
187gboolean
188gnc_dom_tree_to_owner (xmlNodePtr node, GncOwner* owner, QofBook* book)
189{
191 gboolean successful;
192
193 owner_pdata.owner = owner;
194 owner_pdata.book = book;
195
196 successful = dom_tree_generic_parse (node, owner_handlers_v2,
197 &owner_pdata);
198
199 if (!successful)
200 {
201 PERR ("failed to parse owner tree");
202 }
203
204 return successful;
205}
206
207static gboolean
208owner_ns (FILE* out)
209{
210 g_return_val_if_fail (out, FALSE);
211 return gnc_xml2_write_namespace_decl (out, "owner");
212}
213
214void
215gnc_owner_xml_initialize (void)
216{
217 static GncXmlDataType_t be_data =
218 {
219 GNC_FILE_BACKEND_VERS,
220 "gnc:Owner",
221 NULL, /* parser_create */
222 NULL, /* add_item */
223 NULL, /* get_count */
224 NULL, /* write */
225 NULL, /* scrub */
226 owner_ns,
227 };
228
229 gnc_xml_register_backend (be_data);
230}
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250
const GncGUID * gncOwnerGetGUID(const GncOwner *owner)
Get the GncGUID of the immediate owner.
Definition gncOwner.c:518
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Returns the GncOwnerType of this owner.
Definition gncOwner.c:200
api for GnuCash version 2 XML-based file format
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr,...
QofBook reference.
Definition qofbook-p.hpp:47