Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-account-xml-v2.c -- account xml i/o implementation *
3 : : * *
4 : : * Copyright (C) 2001 James LewisMoss <dres@debian.org> *
5 : : * Copyright (C) 2002 Linas Vepstas <linas@linas.org> *
6 : : * *
7 : : * This program is free software; you can redistribute it and/or *
8 : : * modify it under the terms of the GNU General Public License as *
9 : : * published by the Free Software Foundation; either version 2 of *
10 : : * the License, or (at your option) any later version. *
11 : : * *
12 : : * This program is distributed in the hope that it will be useful, *
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 : : * GNU General Public License for more details. *
16 : : * *
17 : : * You should have received a copy of the GNU General Public License*
18 : : * along with this program; if not, contact: *
19 : : * *
20 : : * Free Software Foundation Voice: +1-617-542-5942 *
21 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
22 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
23 : : * *
24 : : \********************************************************************/
25 : : #include <glib.h>
26 : :
27 : : #include <config.h>
28 : : #include <stdlib.h>
29 : : #include <string.h>
30 : : #include <AccountP.hpp>
31 : : #include <Account.h>
32 : :
33 : : #include "gnc-xml-helper.h"
34 : : #include "sixtp.h"
35 : : #include "sixtp-utils.h"
36 : : #include "sixtp-parsers.h"
37 : : #include "sixtp-utils.h"
38 : : #include "sixtp-dom-parsers.h"
39 : : #include "sixtp-dom-generators.h"
40 : :
41 : : #include "gnc-xml.h"
42 : : #include "io-gncxml-gen.h"
43 : : #include "io-gncxml-v2.h"
44 : :
45 : : #include "sixtp-dom-parsers.h"
46 : :
47 : : static QofLogModule log_module = GNC_MOD_IO;
48 : :
49 : : const gchar* account_version_string = "2.0.0";
50 : :
51 : : /* ids */
52 : : #define gnc_account_string "gnc:account"
53 : : #define act_name_string "act:name"
54 : : #define act_id_string "act:id"
55 : : #define act_type_string "act:type"
56 : : #define act_commodity_string "act:commodity"
57 : : #define act_commodity_scu_string "act:commodity-scu"
58 : : #define act_non_standard_scu_string "act:non-standard-scu"
59 : : #define act_code_string "act:code"
60 : : #define act_description_string "act:description"
61 : : #define act_slots_string "act:slots"
62 : : #define act_parent_string "act:parent"
63 : : #define act_lots_string "act:lots"
64 : : /* The currency and security strings should not appear in newer
65 : : * xml files (anything post-gnucash-1.6) */
66 : : #define act_currency_string "act:currency"
67 : : #define act_currency_scu_string "act:currency-scu"
68 : : #define act_security_string "act:security"
69 : : #define act_security_scu_string "act:security-scu"
70 : : #define act_hidden_string "act:hidden"
71 : : #define act_placeholder_string "act:placeholder"
72 : :
73 : : xmlNodePtr
74 : 253 : gnc_account_dom_tree_create (Account* act,
75 : : gboolean exporting,
76 : : gboolean allow_incompat)
77 : : {
78 : : const char* str;
79 : : xmlNodePtr ret;
80 : : GList* lots, *n;
81 : : Account* parent;
82 : : gnc_commodity* acct_commodity;
83 : :
84 : 253 : ENTER ("(account=%p)", act);
85 : :
86 : 253 : ret = xmlNewNode (NULL, BAD_CAST gnc_account_string);
87 : 253 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST account_version_string);
88 : :
89 : 253 : xmlAddChild (ret, text_to_dom_tree (act_name_string,
90 : : xaccAccountGetName (act)));
91 : :
92 : 253 : xmlAddChild (ret, guid_to_dom_tree (act_id_string, xaccAccountGetGUID (act)));
93 : :
94 : 253 : xmlAddChild (ret, text_to_dom_tree (
95 : : act_type_string,
96 : : xaccAccountTypeEnumAsString (xaccAccountGetType (act))));
97 : :
98 : : /* Don't write new XML tags in version 2.3.x and 2.4.x because it
99 : : would mean 2.2.x cannot read those files again. But we can
100 : : enable writing these tags in 2.5.x or late in 2.4.x. */
101 : : /*
102 : : xmlAddChild(ret, boolean_to_dom_tree(
103 : : act_hidden_string,
104 : : xaccAccountGetHidden(act)));
105 : : xmlAddChild(ret, boolean_to_dom_tree(
106 : : act_placeholder_string,
107 : : xaccAccountGetPlaceholder(act)));
108 : : */
109 : :
110 : 253 : acct_commodity = xaccAccountGetCommodity (act);
111 : 253 : if (acct_commodity != NULL)
112 : : {
113 : 251 : xmlAddChild (ret, commodity_ref_to_dom_tree (act_commodity_string,
114 : : acct_commodity));
115 : :
116 : 251 : xmlAddChild (ret, int_to_dom_tree (act_commodity_scu_string,
117 : 251 : xaccAccountGetCommoditySCUi (act)));
118 : :
119 : 251 : if (xaccAccountGetNonStdSCU (act))
120 : 4 : xmlNewChild (ret, NULL, BAD_CAST act_non_standard_scu_string, NULL);
121 : : }
122 : :
123 : 253 : str = xaccAccountGetCode (act);
124 : 253 : if (str && *str)
125 : : {
126 : 20 : xmlAddChild (ret, text_to_dom_tree (act_code_string, str));
127 : : }
128 : :
129 : 253 : str = xaccAccountGetDescription (act);
130 : 253 : if (str && *str)
131 : : {
132 : 234 : xmlAddChild (ret, text_to_dom_tree (act_description_string, str));
133 : : }
134 : :
135 : : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
136 : 253 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (act_slots_string,
137 : 253 : QOF_INSTANCE (act)));
138 : 253 : parent = gnc_account_get_parent (act);
139 : 253 : if (parent)
140 : : {
141 : 247 : if (!gnc_account_is_root (parent) || allow_incompat)
142 : 247 : xmlAddChild (ret, guid_to_dom_tree (act_parent_string,
143 : 247 : xaccAccountGetGUID (parent)));
144 : : }
145 : :
146 : 253 : lots = xaccAccountGetLotList (act);
147 : 253 : PINFO ("lot list=%p", lots);
148 : 253 : if (lots && !exporting)
149 : : {
150 : 2 : xmlNodePtr toaddto = xmlNewChild (ret, NULL, BAD_CAST act_lots_string, NULL);
151 : :
152 : 2 : lots = g_list_sort (lots, qof_instance_guid_compare);
153 : :
154 : 4 : for (n = lots; n; n = n->next)
155 : : {
156 : 2 : GNCLot* lot = static_cast<decltype (lot)> (n->data);
157 : 2 : xmlAddChild (toaddto, gnc_lot_dom_tree_create (lot));
158 : : }
159 : : }
160 : 253 : g_list_free (lots);
161 : :
162 : 253 : LEAVE ("");
163 : 253 : return ret;
164 : : }
165 : :
166 : : /***********************************************************************/
167 : :
168 : : struct account_pdata
169 : : {
170 : : Account* account;
171 : : QofBook* book;
172 : : };
173 : :
174 : : static inline gboolean
175 : 2186 : set_string (xmlNodePtr node, Account* act,
176 : : void (*func) (Account* act, const gchar* txt))
177 : : {
178 : 2186 : gchar* txt = dom_tree_to_text (node);
179 : 2186 : g_return_val_if_fail (txt, FALSE);
180 : :
181 : 2186 : func (act, txt);
182 : :
183 : 2186 : g_free (txt);
184 : :
185 : 2186 : return TRUE;
186 : : }
187 : :
188 : : static gboolean
189 : 1203 : account_name_handler (xmlNodePtr node, gpointer act_pdata)
190 : : {
191 : 1203 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
192 : :
193 : 1203 : return set_string (node, pdata->account, xaccAccountSetName);
194 : : }
195 : :
196 : : static gboolean
197 : 1203 : account_id_handler (xmlNodePtr node, gpointer act_pdata)
198 : : {
199 : 1203 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
200 : : GncGUID* guid;
201 : :
202 : 1203 : guid = dom_tree_to_guid (node);
203 : 1203 : g_return_val_if_fail (guid, FALSE);
204 : :
205 : 1203 : xaccAccountSetGUID (pdata->account, guid);
206 : :
207 : 1203 : guid_free (guid);
208 : :
209 : 1203 : return TRUE;
210 : : }
211 : :
212 : : static gboolean
213 : 1203 : account_type_handler (xmlNodePtr node, gpointer act_pdata)
214 : : {
215 : 1203 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
216 : 1203 : GNCAccountType type = ACCT_TYPE_INVALID;
217 : : char* string;
218 : :
219 : 1203 : string = (char*) xmlNodeGetContent (node->xmlChildrenNode);
220 : 1203 : xaccAccountStringToType (string, &type);
221 : 1203 : xmlFree (string);
222 : :
223 : 1203 : xaccAccountSetType (pdata->account, type);
224 : :
225 : 1203 : return TRUE;
226 : : }
227 : :
228 : : static gboolean
229 : 1151 : account_commodity_handler (xmlNodePtr node, gpointer act_pdata)
230 : : {
231 : 1151 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
232 : : gnc_commodity* ref;
233 : :
234 : : // ref = dom_tree_to_commodity_ref_no_engine(node, pdata->book);
235 : 1151 : ref = dom_tree_to_commodity_ref (node, pdata->book);
236 : 1151 : xaccAccountSetCommodity (pdata->account, ref);
237 : :
238 : 1151 : return TRUE;
239 : : }
240 : :
241 : : static gboolean
242 : 659 : account_commodity_scu_handler (xmlNodePtr node, gpointer act_pdata)
243 : : {
244 : 659 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
245 : : gint64 val;
246 : :
247 : 659 : dom_tree_to_integer (node, &val);
248 : 659 : xaccAccountSetCommoditySCU (pdata->account, val);
249 : :
250 : 659 : return TRUE;
251 : : }
252 : :
253 : : static gboolean
254 : 0 : account_hidden_handler (xmlNodePtr node, gpointer act_pdata)
255 : : {
256 : 0 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
257 : : gboolean val;
258 : :
259 : 0 : dom_tree_to_boolean (node, &val);
260 : 0 : xaccAccountSetHidden (pdata->account, val);
261 : :
262 : 0 : return TRUE;
263 : : }
264 : :
265 : : static gboolean
266 : 0 : account_placeholder_handler (xmlNodePtr node, gpointer act_pdata)
267 : : {
268 : 0 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
269 : : gboolean val;
270 : :
271 : 0 : dom_tree_to_boolean (node, &val);
272 : 0 : xaccAccountSetPlaceholder (pdata->account, val);
273 : :
274 : 0 : return TRUE;
275 : : }
276 : :
277 : : static gboolean
278 : 4 : account_non_standard_scu_handler (xmlNodePtr node, gpointer act_pdata)
279 : : {
280 : 4 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
281 : :
282 : 4 : xaccAccountSetNonStdSCU (pdata->account, TRUE);
283 : :
284 : 4 : return TRUE;
285 : : }
286 : :
287 : : /* ============================================================== */
288 : : /* The following deprecated routines are here only to service
289 : : * older XML files. */
290 : :
291 : : static gboolean
292 : 0 : deprecated_account_currency_handler (xmlNodePtr node, gpointer act_pdata)
293 : : {
294 : 0 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
295 : : gnc_commodity* ref;
296 : :
297 : 0 : PWARN ("Account %s: Obsolete xml tag 'act:currency' will not be preserved.",
298 : : xaccAccountGetName (pdata->account));
299 : 0 : ref = dom_tree_to_commodity_ref_no_engine (node, pdata->book);
300 : 0 : DxaccAccountSetCurrency (pdata->account, ref);
301 : :
302 : 0 : return TRUE;
303 : : }
304 : :
305 : : static gboolean
306 : 0 : deprecated_account_currency_scu_handler (xmlNodePtr node, gpointer act_pdata)
307 : : {
308 : 0 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
309 : 0 : PWARN ("Account %s: Obsolete xml tag 'act:currency-scu' will not be preserved.",
310 : : xaccAccountGetName (pdata->account));
311 : 0 : return TRUE;
312 : : }
313 : :
314 : : static gboolean
315 : 0 : deprecated_account_security_handler (xmlNodePtr node, gpointer act_pdata)
316 : : {
317 : 0 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
318 : 0 : gnc_commodity* ref, *orig = xaccAccountGetCommodity (pdata->account);
319 : :
320 : 0 : PWARN ("Account %s: Obsolete xml tag 'act:security' will not be preserved.",
321 : : xaccAccountGetName (pdata->account));
322 : : /* If the account has both a commodity and a security element, and
323 : : the commodity is a currency, then the commodity is probably
324 : : wrong. In that case we want to replace it with the
325 : : security. jralls 2010-11-02 */
326 : 0 : if (!orig || gnc_commodity_is_currency (orig))
327 : : {
328 : 0 : ref = dom_tree_to_commodity_ref_no_engine (node, pdata->book);
329 : 0 : xaccAccountSetCommodity (pdata->account, ref);
330 : : /* If the SCU was set, it was probably wrong, so zero it out
331 : : so that the SCU handler can fix it if there's a
332 : : security-scu element. jralls 2010-11-02 */
333 : 0 : xaccAccountSetCommoditySCU (pdata->account, 0);
334 : : }
335 : :
336 : 0 : return TRUE;
337 : : }
338 : :
339 : : static gboolean
340 : 0 : deprecated_account_security_scu_handler (xmlNodePtr node, gpointer act_pdata)
341 : : {
342 : 0 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
343 : : gint64 val;
344 : :
345 : 0 : PWARN ("Account %s: Obsolete xml tag 'act:security-scu' will not be preserved.",
346 : : xaccAccountGetName (pdata->account));
347 : 0 : if (!xaccAccountGetCommoditySCU (pdata->account))
348 : : {
349 : 0 : dom_tree_to_integer (node, &val);
350 : 0 : xaccAccountSetCommoditySCU (pdata->account, val);
351 : : }
352 : :
353 : 0 : return TRUE;
354 : : }
355 : :
356 : : /* ============================================================== */
357 : :
358 : : static gboolean
359 : 249 : account_slots_handler (xmlNodePtr node, gpointer act_pdata)
360 : : {
361 : 249 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
362 : 249 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->account));
363 : : }
364 : :
365 : : static gboolean
366 : 1146 : account_parent_handler (xmlNodePtr node, gpointer act_pdata)
367 : : {
368 : 1146 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
369 : : Account* parent;
370 : : GncGUID* gid;
371 : :
372 : 1146 : gid = dom_tree_to_guid (node);
373 : 1146 : g_return_val_if_fail (gid, FALSE);
374 : :
375 : 1146 : parent = xaccAccountLookup (gid, pdata->book);
376 : 1146 : if (!parent)
377 : : {
378 : 0 : guid_free (gid);
379 : 0 : g_return_val_if_fail (parent, FALSE);
380 : : }
381 : :
382 : 1146 : gnc_account_append_child (parent, pdata->account);
383 : :
384 : 1146 : guid_free (gid);
385 : :
386 : 1146 : return TRUE;
387 : : }
388 : :
389 : : static gboolean
390 : 28 : account_code_handler (xmlNodePtr node, gpointer act_pdata)
391 : : {
392 : 28 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
393 : :
394 : 28 : return set_string (node, pdata->account, xaccAccountSetCode);
395 : : }
396 : :
397 : : static gboolean
398 : 955 : account_description_handler (xmlNodePtr node, gpointer act_pdata)
399 : : {
400 : 955 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
401 : :
402 : 955 : return set_string (node, pdata->account, xaccAccountSetDescription);
403 : : }
404 : :
405 : : static gboolean
406 : 3 : account_lots_handler (xmlNodePtr node, gpointer act_pdata)
407 : : {
408 : 3 : struct account_pdata* pdata = static_cast<decltype (pdata)> (act_pdata);
409 : : xmlNodePtr mark;
410 : :
411 : 3 : g_return_val_if_fail (node, FALSE);
412 : 3 : g_return_val_if_fail (node->xmlChildrenNode, FALSE);
413 : :
414 : 12 : for (mark = node->xmlChildrenNode; mark; mark = mark->next)
415 : : {
416 : : GNCLot* lot;
417 : :
418 : 9 : if (g_strcmp0 ("text", (char*) mark->name) == 0)
419 : 6 : continue;
420 : :
421 : 3 : lot = dom_tree_to_lot (mark, pdata->book);
422 : :
423 : 3 : if (lot)
424 : : {
425 : 3 : xaccAccountInsertLot (pdata->account, lot);
426 : : }
427 : : else
428 : : {
429 : 0 : return FALSE;
430 : : }
431 : : }
432 : 3 : return TRUE;
433 : : }
434 : :
435 : : static struct dom_tree_handler account_handlers_v2[] =
436 : : {
437 : : { act_name_string, account_name_handler, 1, 0 },
438 : : { act_id_string, account_id_handler, 1, 0 },
439 : : { act_type_string, account_type_handler, 1, 0 },
440 : : { act_commodity_string, account_commodity_handler, 0, 0 },
441 : : { act_commodity_scu_string, account_commodity_scu_handler, 0, 0 },
442 : : { act_non_standard_scu_string, account_non_standard_scu_handler, 0, 0 },
443 : : { act_code_string, account_code_handler, 0, 0 },
444 : : { act_description_string, account_description_handler, 0, 0},
445 : : { act_slots_string, account_slots_handler, 0, 0 },
446 : : { act_parent_string, account_parent_handler, 0, 0 },
447 : : { act_lots_string, account_lots_handler, 0, 0 },
448 : : { act_hidden_string, account_hidden_handler, 0, 0 },
449 : : { act_placeholder_string, account_placeholder_handler, 0, 0 },
450 : :
451 : : /* These should not appear in newer xml files; only in old
452 : : * (circa gnucash-1.6) xml files. We maintain them for backward
453 : : * compatibility. */
454 : : { act_currency_string, deprecated_account_currency_handler, 0, 0 },
455 : : { act_currency_scu_string, deprecated_account_currency_scu_handler, 0, 0 },
456 : : { act_security_string, deprecated_account_security_handler, 0, 0 },
457 : : { act_security_scu_string, deprecated_account_security_scu_handler, 0, 0 },
458 : : { NULL, 0, 0, 0 }
459 : : };
460 : :
461 : : static gboolean
462 : 12678 : gnc_account_end_handler (gpointer data_for_children,
463 : : GSList* data_from_children, GSList* sibling_data,
464 : : gpointer parent_data, gpointer global_data,
465 : : gpointer* result, const gchar* tag)
466 : : {
467 : : Account* acc, *parent, *root;
468 : 12678 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
469 : 12678 : gxpf_data* gdata = (gxpf_data*)global_data;
470 : 12678 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
471 : : int type;
472 : :
473 : :
474 : 12678 : if (parent_data)
475 : : {
476 : 11462 : return TRUE;
477 : : }
478 : :
479 : : /* OK. For some messed up reason this is getting called again with a
480 : : NULL tag. So we ignore those cases */
481 : 1216 : if (!tag)
482 : : {
483 : 21 : return TRUE;
484 : : }
485 : :
486 : 1195 : g_return_val_if_fail (tree, FALSE);
487 : :
488 : 1195 : acc = dom_tree_to_account (tree, book);
489 : 1195 : if (acc != NULL)
490 : : {
491 : 1195 : gdata->cb (tag, gdata->parsedata, acc);
492 : : /*
493 : : * Now return the account to the "edit" state. At the end of reading
494 : : * all the transactions, we will Commit. This replaces #splits
495 : : * rebalances with #accounts rebalances at the end. A BIG win!
496 : : */
497 : 1195 : xaccAccountBeginEdit (acc);
498 : :
499 : : /* Backwards compatibility. If there's no parent, see if this
500 : : * account is of type ROOT. If not, find or create a ROOT
501 : : * account and make that the parent. */
502 : 1195 : parent = gnc_account_get_parent (acc);
503 : 1195 : if (parent == NULL)
504 : : {
505 : 54 : type = xaccAccountGetType (acc);
506 : 54 : if (type != ACCT_TYPE_ROOT)
507 : : {
508 : 0 : root = gnc_book_get_root_account (book);
509 : 0 : if (root == NULL)
510 : : {
511 : 0 : root = gnc_account_create_root (book);
512 : : }
513 : 0 : gnc_account_append_child (root, acc);
514 : : }
515 : : }
516 : : }
517 : :
518 : 1195 : xmlFreeNode (tree);
519 : :
520 : 1195 : return acc != NULL;
521 : : }
522 : :
523 : : Account*
524 : 1203 : dom_tree_to_account (xmlNodePtr node, QofBook* book)
525 : : {
526 : : struct account_pdata act_pdata;
527 : : Account* accToRet;
528 : : gboolean successful;
529 : :
530 : 1203 : accToRet = xaccMallocAccount (book);
531 : 1203 : xaccAccountBeginEdit (accToRet);
532 : :
533 : 1203 : act_pdata.account = accToRet;
534 : 1203 : act_pdata.book = book;
535 : :
536 : 1203 : successful = dom_tree_generic_parse (node, account_handlers_v2,
537 : : &act_pdata);
538 : 1203 : if (successful)
539 : : {
540 : 1203 : xaccAccountCommitEdit (accToRet);
541 : : }
542 : : else
543 : : {
544 : 0 : PERR ("failed to parse account tree");
545 : 0 : xaccAccountDestroy (accToRet);
546 : 0 : accToRet = NULL;
547 : : }
548 : :
549 : 1203 : return accToRet;
550 : : }
551 : :
552 : : sixtp*
553 : 97 : gnc_account_sixtp_parser_create (void)
554 : : {
555 : 97 : return sixtp_dom_parser_new (gnc_account_end_handler, NULL, NULL);
556 : : }
557 : :
558 : : /* ====================== END OF FILE ===================*/
|