GnuCash 7574033+
Loading...
Searching...
No Matches
gnc-schedxaction-xml-v2.cpp
1/********************************************************************
2 * gnc-schedxactions-xml-v2.c -- xml routines for transactions *
3 * Copyright (C) 2001,2007 Joshua Sled <jsled@asynchronous.org> *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License*
16 * along with this program; if not, contact: *
17 * *
18 * Free Software Foundation Voice: +1-617-542-5942 *
19 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 * Boston, MA 02110-1301, USA gnu@gnu.org *
21 * *
22 *******************************************************************/
23#include <glib.h>
24
25#include <config.h>
26#include <string.h>
27
28#include "SX-book.h"
29
30#include "gnc-xml-helper.h"
31#include "sixtp.h"
32#include "sixtp-utils.h"
33#include "sixtp-parsers.h"
34#include "sixtp-utils.h"
35#include "sixtp-dom-parsers.h"
36#include "sixtp-dom-generators.h"
37
38#include "gnc-xml.h"
39
40#include "io-gncxml-v2.h"
41#include "io-gncxml-gen.h"
42
43#include "sixtp-dom-parsers.h"
44
45#undef G_LOG_DOMAIN
46#define G_LOG_DOMAIN "gnc.backend.file.sx"
47static const QofLogModule log_module = G_LOG_DOMAIN;
48
49#define SX_ID "sx:id"
50#define SX_NAME "sx:name"
51#define SX_ENABLED "sx:enabled"
52#define SX_AUTOCREATE "sx:autoCreate"
53#define SX_AUTOCREATE_NOTIFY "sx:autoCreateNotify"
54#define SX_ADVANCE_CREATE_DAYS "sx:advanceCreateDays"
55#define SX_ADVANCE_REMIND_DAYS "sx:advanceRemindDays"
56#define SX_INSTANCE_COUNT "sx:instanceCount"
57#define SX_START "sx:start"
58#define SX_LAST "sx:last"
59#define SX_NUM_OCCUR "sx:num-occur"
60#define SX_REM_OCCUR "sx:rem-occur"
61#define SX_END "sx:end"
62#define SX_TEMPL_ACCT "sx:templ-acct"
63#define SX_FREQSPEC "sx:freqspec"
64#define SX_SCHEDULE "sx:schedule"
65#define SX_SLOTS "sx:slots"
66#define SX_DEFER_INSTANCE "sx:deferredInstance"
67
68/*
69 * FIXME: These should be defined in a header somewhere
70 */
71
72#define GNC_ACCOUNT_TAG "gnc:account"
73#define GNC_TRANSACTION_TAG "gnc:transaction"
74#define GNC_SCHEDXACTION_TAG "gnc:schedxaction"
75
76const gchar* schedxaction_version_string = "1.0.0";
77const gchar* schedxaction_version2_string = "2.0.0";
78
79xmlNodePtr
80gnc_schedXaction_dom_tree_create (SchedXaction* sx)
81{
82 xmlNodePtr ret;
83 const GDate* date;
84 gint instCount;
85 const GncGUID* templ_acc_guid;
86 gboolean allow_2_2_incompat = TRUE;
87 gchar* name = g_strdup (xaccSchedXactionGetName (sx));
88
89 templ_acc_guid = xaccAccountGetGUID (sx->template_acct);
90
91 /* FIXME: this should be the same as the def in io-gncxml-v2.c */
92 ret = xmlNewNode (NULL, BAD_CAST GNC_SCHEDXACTION_TAG);
93
94 if (allow_2_2_incompat)
95 xmlSetProp (ret, BAD_CAST "version", BAD_CAST schedxaction_version2_string);
96 else
97 xmlSetProp (ret, BAD_CAST "version", BAD_CAST schedxaction_version_string);
98
99 xmlAddChild (ret,
100 guid_to_dom_tree (SX_ID,
102
103 xmlNewTextChild (ret, NULL, BAD_CAST SX_NAME, checked_char_cast (name));
104 g_free (name);
105
106 if (allow_2_2_incompat)
107 {
108 xmlNewTextChild (ret, NULL, BAD_CAST SX_ENABLED,
109 BAD_CAST (sx->enabled ? "y" : "n"));
110 }
111
112 xmlNewTextChild (ret, NULL, BAD_CAST SX_AUTOCREATE,
113 BAD_CAST (sx->autoCreateOption ? "y" : "n"));
114 xmlNewTextChild (ret, NULL, BAD_CAST SX_AUTOCREATE_NOTIFY,
115 BAD_CAST (sx->autoCreateNotify ? "y" : "n"));
116 xmlAddChild (ret, int_to_dom_tree (SX_ADVANCE_CREATE_DAYS,
117 sx->advanceCreateDays));
118 xmlAddChild (ret, int_to_dom_tree (SX_ADVANCE_REMIND_DAYS,
119 sx->advanceRemindDays));
120
121 instCount = gnc_sx_get_instance_count (sx, NULL);
122 xmlAddChild (ret, int_to_dom_tree (SX_INSTANCE_COUNT,
123 instCount));
124
125 xmlAddChild (ret,
126 gdate_to_dom_tree (SX_START,
127 xaccSchedXactionGetStartDate (sx)));
128
129 date = xaccSchedXactionGetLastOccurDate (sx);
130 if (g_date_valid (date))
131 {
132 xmlAddChild (ret, gdate_to_dom_tree (SX_LAST, date));
133 }
134
136 {
137
138 xmlAddChild (ret, int_to_dom_tree (SX_NUM_OCCUR,
139 xaccSchedXactionGetNumOccur (sx)));
140 xmlAddChild (ret, int_to_dom_tree (SX_REM_OCCUR,
141 xaccSchedXactionGetRemOccur (sx)));
142
143 }
144 else if (xaccSchedXactionHasEndDate (sx))
145 {
146 xmlAddChild (ret,
147 gdate_to_dom_tree (SX_END,
149 }
150
151 /* output template account GncGUID */
152 xmlAddChild (ret,
153 guid_to_dom_tree (SX_TEMPL_ACCT,
154 templ_acc_guid));
155
156 if (allow_2_2_incompat)
157 {
158 xmlNodePtr schedule_node = xmlNewNode (NULL,
159 BAD_CAST "sx:schedule");
160 GList* schedule = gnc_sx_get_schedule (sx);
161 for (; schedule != NULL; schedule = schedule->next)
162 {
163 xmlAddChild (schedule_node, recurrence_to_dom_tree ("gnc:recurrence",
164 (Recurrence*)schedule->data));
165 }
166 xmlAddChild (ret, schedule_node);
167 }
168
169 /* Output deferred-instance list. */
170 {
171 xmlNodePtr instNode;
172 SXTmpStateData* tsd;
173 GList* l;
174
175 for (l = gnc_sx_get_defer_instances (sx); l; l = l->next)
176 {
177 tsd = (SXTmpStateData*)l->data;
178
179 instNode = xmlNewNode (NULL, BAD_CAST SX_DEFER_INSTANCE);
180 if (g_date_valid (&tsd->last_date))
181 {
182 xmlAddChild (instNode, gdate_to_dom_tree (SX_LAST,
183 &tsd->last_date));
184 }
185 xmlAddChild (instNode, int_to_dom_tree (SX_REM_OCCUR,
186 tsd->num_occur_rem));
187 xmlAddChild (instNode, int_to_dom_tree (SX_INSTANCE_COUNT,
188 tsd->num_inst));
189 xmlAddChild (ret, instNode);
190 }
191 }
192
193 /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
194 xmlAddChild (ret, qof_instance_slots_to_dom_tree (SX_SLOTS,
195 QOF_INSTANCE (sx)));
196 return ret;
197}
198
200{
201 SchedXaction* sx;
202 QofBook* book;
203 gboolean saw_freqspec;
204 gboolean saw_recurrence;
205};
206
207static
208gboolean
209sx_id_handler (xmlNodePtr node, gpointer sx_pdata)
210{
211 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
212 SchedXaction* sx = pdata->sx;
213 auto tmp = dom_tree_to_guid (node);
214
215 g_return_val_if_fail (tmp, FALSE);
216 xaccSchedXactionSetGUID (sx, &*tmp);
217
218 return TRUE;
219}
220
221static
222gboolean
223sx_name_handler (xmlNodePtr node, gpointer sx_pdata)
224{
225 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
226 return apply_xmlnode_text (xaccSchedXactionSetName, pdata->sx, node);
227}
228
229static gboolean
230sx_enabled_handler (xmlNodePtr node, gpointer sx_pdata)
231{
232 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
233 auto set_enabled = [](SchedXaction* sx, const char* txt)
234 {
235 sx->enabled = !g_strcmp0 (txt, "y");
236 };
237 return apply_xmlnode_text (set_enabled, pdata->sx, node);
238}
239
240static gboolean
241sx_autoCreate_handler (xmlNodePtr node, gpointer sx_pdata)
242{
243 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
244 auto set_autocreate = [](SchedXaction* sx, const char* txt)
245 {
246 sx->autoCreateOption = !g_strcmp0 (txt, "y");
247 };
248 return apply_xmlnode_text (set_autocreate, pdata->sx, node);
249}
250
251static gboolean
252sx_notify_handler (xmlNodePtr node, gpointer sx_pdata)
253{
254 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
255 auto set_notify = [](SchedXaction* sx, const char* txt)
256 {
257 sx->autoCreateNotify = !g_strcmp0 (txt, "y");
258 };
259 return apply_xmlnode_text (set_notify, pdata->sx, node);
260}
261
262static gboolean
263sx_advCreate_handler (xmlNodePtr node, gpointer sx_pdata)
264{
265 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
266 SchedXaction* sx = pdata->sx;
267 gint64 advCreate;
268
269 if (! dom_tree_to_integer (node, &advCreate))
270 {
271 return FALSE;
272 }
273
274 xaccSchedXactionSetAdvanceCreation (sx, advCreate);
275 return TRUE;
276}
277
278static gboolean
279sx_advRemind_handler (xmlNodePtr node, gpointer sx_pdata)
280{
281 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
282 SchedXaction* sx = pdata->sx;
283 gint64 advRemind;
284
285 if (! dom_tree_to_integer (node, &advRemind))
286 {
287 return FALSE;
288 }
289
290 xaccSchedXactionSetAdvanceReminder (sx, advRemind);
291 return TRUE;
292}
293
294static
295gboolean
296sx_set_date (xmlNodePtr node, SchedXaction* sx,
297 void (*settor) (SchedXaction* sx, const GDate* d))
298{
299 auto date = dom_tree_to_gdate (node);
300 g_return_val_if_fail (date, FALSE);
301 (*settor) (sx, &date.value());
302
303 return TRUE;
304}
305
306static
307gboolean
308sx_instcount_handler (xmlNodePtr node, gpointer sx_pdata)
309{
310 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
311 SchedXaction* sx = pdata->sx;
312 gint64 instanceNum;
313
314 if (! dom_tree_to_integer (node, &instanceNum))
315 {
316 return FALSE;
317 }
318
319 gnc_sx_set_instance_count (sx, instanceNum);
320 return TRUE;
321}
322
323static
324gboolean
325sx_start_handler (xmlNodePtr node, gpointer sx_pdata)
326{
327 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
328 SchedXaction* sx = pdata->sx;
329
330 return sx_set_date (node, sx, xaccSchedXactionSetStartDate);
331}
332
333static
334gboolean
335sx_last_handler (xmlNodePtr node, gpointer sx_pdata)
336{
337 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
338 SchedXaction* sx = pdata->sx;
339
340 return sx_set_date (node, sx, xaccSchedXactionSetLastOccurDate);
341}
342
343static
344gboolean
345sx_end_handler (xmlNodePtr node, gpointer sx_pdata)
346{
347 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
348 SchedXaction* sx = pdata->sx;
349
350 return sx_set_date (node, sx, xaccSchedXactionSetEndDate);
351}
352
353static void
354_fixup_recurrence_start_dates (const GDate* sx_start_date, GList* schedule)
355{
356 GList* iter;
357 for (iter = schedule; iter != NULL; iter = iter->next)
358 {
359 Recurrence* r;
360 GDate start, next;
361
362 r = (Recurrence*)iter->data;
363
364 start = *sx_start_date;
365 g_date_subtract_days (&start, 1);
366
367 g_date_clear (&next, 1);
368
369 recurrenceNextInstance (r, &start, &next);
370 g_return_if_fail (g_date_valid (&next));
371
372 {
373 gchar date_str[128];
374 gchar* sched_str;
375
376 g_date_strftime (date_str, 127, "%x", &next);
377 sched_str = recurrenceToString (r);
378 DEBUG ("setting recurrence [%s] start date to [%s]",
379 sched_str, date_str);
380 g_free (sched_str);
381 }
382
383 recurrenceSet (r,
384 recurrenceGetMultiplier (r),
385 recurrenceGetPeriodType (r),
386 &next,
387 recurrenceGetWeekendAdjust (r));
388 }
389
390 if (g_list_length (schedule) == 1
391 && recurrenceGetPeriodType ((Recurrence*)g_list_nth_data (schedule,
392 0)) == PERIOD_ONCE)
393 {
394 char date_buf[128];
395 Recurrence* fixup = (Recurrence*)g_list_nth_data (schedule, 0);
396 g_date_strftime (date_buf, 127, "%x", sx_start_date);
397 recurrenceSet (fixup, 1, PERIOD_ONCE, sx_start_date, WEEKEND_ADJ_NONE);
398 DEBUG ("fixed up period=ONCE Recurrence to date [%s]", date_buf);
399 }
400}
401
402static gboolean
403sx_freqspec_handler (xmlNodePtr node, gpointer sx_pdata)
404{
405 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
406 SchedXaction* sx = pdata->sx;
407 GList* schedule;
408 gchar* debug_str;
409
410 g_return_val_if_fail (node, FALSE);
411
412 schedule = dom_tree_freqSpec_to_recurrences (node, pdata->book);
413 gnc_sx_set_schedule (sx, schedule);
414 debug_str = recurrenceListToString (schedule);
415 DEBUG ("parsed from freqspec [%s]", debug_str);
416 g_free (debug_str);
417
418 _fixup_recurrence_start_dates (xaccSchedXactionGetStartDate (sx), schedule);
419 pdata->saw_freqspec = TRUE;
420
421 return TRUE;
422}
423
424static gboolean
425sx_schedule_recurrence_handler (xmlNodePtr node, gpointer parsing_data)
426{
427 GList** schedule = (GList**)parsing_data;
428 gchar* sched_str;
429 Recurrence* r = dom_tree_to_recurrence (node);
430 g_return_val_if_fail (r, FALSE);
431 sched_str = recurrenceToString (r);
432 DEBUG ("parsed recurrence [%s]", sched_str);
433 g_free (sched_str);
434 *schedule = g_list_append (*schedule, r);
435 return TRUE;
436}
437
438struct dom_tree_handler sx_recurrence_list_handlers[] =
439{
440 { "gnc:recurrence", sx_schedule_recurrence_handler, 0, 0 },
441 { NULL, NULL, 0, 0 }
442};
443
444static gboolean
445sx_recurrence_handler (xmlNodePtr node, gpointer _pdata)
446{
447 struct sx_pdata* parsing_data = static_cast<decltype (parsing_data)> (_pdata);
448 GList* schedule = NULL;
449 gchar* debug_str;
450
451 g_return_val_if_fail (node, FALSE);
452
453 if (!dom_tree_generic_parse (node, sx_recurrence_list_handlers, &schedule))
454 return FALSE;
455 // g_return_val_if_fail(schedule, FALSE);
456 debug_str = recurrenceListToString (schedule);
457 DEBUG ("setting freshly-parsed schedule: [%s]", debug_str);
458 g_free (debug_str);
459 gnc_sx_set_schedule (parsing_data->sx, schedule);
460 parsing_data->saw_recurrence = TRUE;
461 return TRUE;
462}
463
464static
465gboolean
466sx_defer_last_handler (xmlNodePtr node, gpointer gpTSD)
467{
468 SXTmpStateData* tsd = (SXTmpStateData*)gpTSD;
469
470 g_return_val_if_fail (node, FALSE);
471 auto gd = dom_tree_to_gdate (node);
472 g_return_val_if_fail (gd, FALSE);
473 tsd->last_date = *gd;
474 return TRUE;
475}
476
477static
478gboolean
479sx_defer_rem_occur_handler (xmlNodePtr node, gpointer gpTSD)
480{
481 gint64 remOccur;
482 SXTmpStateData* tsd = (SXTmpStateData*)gpTSD;
483 g_return_val_if_fail (node, FALSE);
484
485 if (! dom_tree_to_integer (node, &remOccur))
486 {
487 return FALSE;
488 }
489 tsd->num_occur_rem = remOccur;
490 return TRUE;
491}
492
493static
494gboolean
495sx_defer_inst_count_handler (xmlNodePtr node, gpointer gpTSD)
496{
497 gint64 instCount;
498 SXTmpStateData* tsd = (SXTmpStateData*)gpTSD;
499 g_return_val_if_fail (node, FALSE);
500
501 if (! dom_tree_to_integer (node, &instCount))
502 {
503 return FALSE;
504 }
505 tsd->num_inst = instCount;
506 return TRUE;
507}
508
509struct dom_tree_handler sx_defer_dom_handlers[] =
510{
511 /* tag name, handler, opt, ? */
512 { SX_LAST, sx_defer_last_handler, 1, 0 },
513 { SX_REM_OCCUR, sx_defer_rem_occur_handler, 1, 0 },
514 { SX_INSTANCE_COUNT, sx_defer_inst_count_handler, 1, 0 },
515 { NULL, NULL, 0, 0 }
516};
517
518static
519gboolean
520sx_defer_inst_handler (xmlNodePtr node, gpointer sx_pdata)
521{
522 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
523 SchedXaction* sx = pdata->sx;
524 SXTmpStateData* tsd;
525
526 g_return_val_if_fail (node, FALSE);
527
528 tsd = g_new0 (SXTmpStateData, 1);
529 g_assert (sx_defer_dom_handlers != NULL);
530 if (!dom_tree_generic_parse (node,
531 sx_defer_dom_handlers,
532 tsd))
533 {
534 xmlElemDump (stdout, NULL, node);
535 g_free (tsd);
536 tsd = NULL;
537 return FALSE;
538 }
539
540 /* We assume they were serialized in sorted order, here. */
541 sx->deferredList = g_list_append (sx->deferredList, tsd);
542 return TRUE;
543}
544
545static
546gboolean
547sx_numOccur_handler (xmlNodePtr node, gpointer sx_pdata)
548{
549 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
550 SchedXaction* sx = pdata->sx;
551 gint64 numOccur;
552
553 if (! dom_tree_to_integer (node, &numOccur))
554 {
555 return FALSE;
556 }
557
558 xaccSchedXactionSetNumOccur (sx, numOccur);
559
560 return TRUE;
561}
562
563
564static
565gboolean
566sx_templ_acct_handler (xmlNodePtr node, gpointer sx_pdata)
567{
568 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
569 SchedXaction* sx = pdata->sx;
570 auto templ_acct_guid = dom_tree_to_guid (node);
571 Account* account;
572
573 if (!templ_acct_guid)
574 {
575 return FALSE;
576 }
577
578 account = xaccAccountLookup (&*templ_acct_guid, pdata->book);
579 sx_set_template_account (sx, account);
580
581 return TRUE;
582}
583
584
585static
586gboolean
587sx_remOccur_handler (xmlNodePtr node, gpointer sx_pdata)
588{
589 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
590 SchedXaction* sx = pdata->sx;
591 gint64 remOccur;
592
593 if (! dom_tree_to_integer (node, &remOccur))
594 {
595 return FALSE;
596 }
597
598 xaccSchedXactionSetRemOccur (sx, remOccur);
599
600 return TRUE;
601}
602
603static
604gboolean
605sx_slots_handler (xmlNodePtr node, gpointer sx_pdata)
606{
607 struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
608 SchedXaction* sx = pdata->sx;
609
610 return dom_tree_create_instance_slots (node, QOF_INSTANCE (sx));
611}
612
613struct dom_tree_handler sx_dom_handlers[] =
614{
615 { SX_ID, sx_id_handler, 1, 0 },
616 { SX_NAME, sx_name_handler, 1, 0 },
617 { SX_ENABLED, sx_enabled_handler, 0, 0 },
618 { SX_AUTOCREATE, sx_autoCreate_handler, 1, 0 },
619 { SX_AUTOCREATE_NOTIFY, sx_notify_handler, 1, 0 },
620 { SX_ADVANCE_CREATE_DAYS, sx_advCreate_handler, 1, 0 },
621 { SX_ADVANCE_REMIND_DAYS, sx_advRemind_handler, 1, 0 },
622 { SX_INSTANCE_COUNT, sx_instcount_handler, 0, 0 },
623 { SX_START, sx_start_handler, 1, 0 },
624 { SX_LAST, sx_last_handler, 0, 0 },
625 { SX_NUM_OCCUR, sx_numOccur_handler, 0, 0 },
626 { SX_REM_OCCUR, sx_remOccur_handler, 0, 0 },
627 { SX_END, sx_end_handler, 0, 0 },
628 { SX_TEMPL_ACCT, sx_templ_acct_handler, 0, 0 },
629 { SX_FREQSPEC, sx_freqspec_handler, 0, 0 },
630 { SX_SCHEDULE, sx_recurrence_handler, 0, 0 },
631 { SX_DEFER_INSTANCE, sx_defer_inst_handler, 0, 0 },
632 { SX_SLOTS, sx_slots_handler, 0, 0 },
633 { NULL, NULL, 0, 0 }
634};
635
636static gboolean
637gnc_schedXaction_end_handler (gpointer data_for_children,
638 GSList* data_from_children, GSList* sibling_data,
639 gpointer parent_data, gpointer global_data,
640 gpointer* result, const gchar* tag)
641{
642 SchedXaction* sx;
643 gboolean successful = FALSE;
644 xmlNodePtr tree = (xmlNodePtr)data_for_children;
645 gxpf_data* gdata = (gxpf_data*)global_data;
646 struct sx_pdata sx_pdata;
647
648 if (parent_data)
649 {
650 return TRUE;
651 }
652
653 if (!tag)
654 {
655 return TRUE;
656 }
657
658 g_return_val_if_fail (tree, FALSE);
659
660 sx = xaccSchedXactionMalloc (static_cast<QofBook*> (gdata->bookdata));
661
662 memset (&sx_pdata, 0, sizeof (sx_pdata));
663 sx_pdata.sx = sx;
664 sx_pdata.book = static_cast<decltype (sx_pdata.book)> (gdata->bookdata);
665
666 g_assert (sx_dom_handlers != NULL);
667
668 successful = dom_tree_generic_parse (tree, sx_dom_handlers, &sx_pdata);
669 if (!successful)
670 {
671 g_critical ("failed to parse scheduled xaction");
672 xmlElemDump (stdout, NULL, tree);
673 gnc_sx_begin_edit (sx);
675 goto done;
676 }
677
678 if (tree->properties)
679 {
680 gchar* sx_name = xaccSchedXactionGetName (sx);
681 xmlAttr* attr;
682 for (attr = tree->properties; attr != NULL; attr = attr->next)
683 {
684 xmlChar* attr_value = attr->children->content;
685 DEBUG ("sx attribute name[%s] value[%s]", attr->name, attr_value);
686 if (strcmp ((const char*)attr->name, "version") != 0)
687 {
688 g_warning ("unknown sx attribute [%s]", attr->name);
689 continue;
690 }
691
692 // if version == 1.0.0: ensure freqspec, no recurrence
693 // if version == 2.0.0: ensure recurrence, no freqspec.
694 if (strcmp ((const char*)attr_value,
695 schedxaction_version_string) == 0)
696 {
697 if (!sx_pdata.saw_freqspec)
698 g_critical ("did not see freqspec in version 1 sx [%s]", sx_name);
699 if (sx_pdata.saw_recurrence)
700 g_warning ("saw recurrence in supposedly version 1 sx [%s]", sx_name);
701 }
702
703 if (strcmp ((const char*)attr_value,
704 schedxaction_version2_string) == 0)
705 {
706 if (sx_pdata.saw_freqspec)
707 g_warning ("saw freqspec in version 2 sx [%s]", sx_name);
708 if (!sx_pdata.saw_recurrence)
709 g_critical ("did not find recurrence in version 2 sx [%s]", sx_name);
710 }
711 }
712 }
713
714 // generic_callback -> book_callback: insert the SX in the book
715 gdata->cb (tag, gdata->parsedata, sx);
716
717 /* FIXME: this should be removed somewhere near 1.8 release time. */
718 if (sx->template_acct == NULL)
719 {
720 Account* ra = NULL;
721 Account* acct = NULL;
722 sixtp_gdv2* sixdata = static_cast<decltype (sixdata)> (gdata->parsedata);
723 QofBook* book;
724 gchar guidstr[GUID_ENCODING_LENGTH + 1];
725
726 book = sixdata->book;
727
728 /* We're dealing with a pre-200107<near-end-of-month> rgmerk
729 change re: storing template accounts. */
730 /* Fix: get account with name of our GncGUID from the template
731 accounts. Make that our template_acct pointer. */
733 ra = gnc_book_get_template_root (book);
734 if (ra == NULL)
735 {
736 g_warning ("Error getting template root account from being-parsed Book.");
737 xmlFreeNode (tree);
738 return FALSE;
739 }
740 acct = gnc_account_lookup_by_name (ra, guidstr);
741 if (acct == NULL)
742 {
743 g_warning ("no template account with name [%s]", guidstr);
744 xmlFreeNode (tree);
745 return FALSE;
746 }
747 DEBUG ("template account name [%s] for SX with GncGUID [%s]",
748 xaccAccountGetName (acct), guidstr);
749
750 /* FIXME: free existing template account.
751 * HUH????? We only execute this if there isn't
752 * currently an existing template account, don't we?
753 * <rgmerk>
754 */
755
756 sx->template_acct = acct;
757 }
758
759done:
760 xmlFreeNode (tree);
761
762 return successful;
763}
764
765sixtp*
766gnc_schedXaction_sixtp_parser_create (void)
767{
768 return sixtp_dom_parser_new (gnc_schedXaction_end_handler, NULL, NULL);
769}
770
771static
772gboolean
773tt_act_handler (xmlNodePtr node, gpointer data)
774{
775 gnc_template_xaction_data* txd = static_cast<decltype (txd)> (data);
776 Account* acc;
777 gnc_commodity* com;
778
779 acc = dom_tree_to_account (node, txd->book);
780
781 if (acc == NULL)
782 {
783 return FALSE;
784 }
785 else
786 {
788
789 /* Check for the lack of a commodity [signifying that the
790 pre-7/11/2001-CIT-change SX template Account was parsed [but
791 incorrectly]. */
792 if (xaccAccountGetCommodity (acc) == NULL)
793 {
794#if 1
795 gnc_commodity_table* table;
796
798 com = gnc_commodity_table_lookup (table,
799 GNC_COMMODITY_NS_TEMPLATE, "template");
800#else
801 /* FIXME: This should first look in the table of the
802 book, maybe? The right thing happens [WRT file
803 load/save] if we just _new all the time, but it
804 doesn't seem right. This whole block should go
805 away at some point, but the same concern still
806 applies for
807 SchedXaction.c:xaccSchedXactionInit... */
808 com = gnc_commodity_new (txd->book,
809 "template", GNC_COMMODITY_NS_TEMPLATE,
810 "template", "template",
811 1);
812#endif
813 xaccAccountSetCommodity (acc, com);
814 }
815
816 txd->accts = g_list_append (txd->accts, acc);
817 }
818
819 return TRUE;
820}
821
822static
823gboolean
824tt_trn_handler (xmlNodePtr node, gpointer data)
825{
826 gnc_template_xaction_data* txd = static_cast<decltype (txd)> (data);
827 Transaction* trn;
828
829 trn = dom_tree_to_transaction (node, txd->book);
830
831 if (trn == NULL)
832 {
833 return FALSE;
834 }
835 else
836 {
837 txd->transactions = g_list_append (txd->transactions, trn);
838 }
839
840 return TRUE;
841}
842
843struct dom_tree_handler tt_dom_handlers[] =
844{
845 { GNC_ACCOUNT_TAG, tt_act_handler, 0, 0 },
846 { GNC_TRANSACTION_TAG, tt_trn_handler, 0, 0 },
847 { NULL, NULL, 0, 0 },
848};
849
850static gboolean
851gnc_template_transaction_end_handler (gpointer data_for_children,
852 GSList* data_from_children,
853 GSList* sibling_data,
854 gpointer parent_data,
855 gpointer global_data,
856 gpointer* result,
857 const gchar* tag)
858{
859 gboolean successful = FALSE;
860 xmlNodePtr tree = static_cast<decltype (tree)> (data_for_children);
861 gxpf_data* gdata = static_cast<decltype (gdata)> (global_data);
862 QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
863 GList* n;
864 gnc_template_xaction_data txd;
865
866 txd.book = book;
867 txd.accts = NULL;
868 txd.transactions = NULL;
869
870 /* the DOM tree will have an account tree [the template
871 accounts] and a list of transactions [which will be members
872 of the template account].
873
874 we want to parse through the dom trees for each, placing
875 the null-parent account in the book's template-group slot,
876 the others under it, and the transactions as normal. */
877
878 if (parent_data)
879 {
880 return TRUE;
881 }
882
883 if (!tag)
884 {
885 return TRUE;
886 }
887
888 g_return_val_if_fail (tree, FALSE);
889
890 successful = dom_tree_generic_parse (tree, tt_dom_handlers, &txd);
891
892 if (successful)
893 {
894 gdata->cb (tag, gdata->parsedata, &txd);
895 }
896 else
897 {
898 g_warning ("failed to parse template transaction");
899 xmlElemDump (stdout, NULL, tree);
900 }
901
902 /* cleanup */
903 for (n = txd.accts; n; n = n->next)
904 {
905 n->data = NULL;
906 }
907 for (n = txd.transactions; n; n = n->next)
908 {
909 n->data = NULL;
910 }
911 g_list_free (txd.accts);
912 g_list_free (txd.transactions);
913
914 xmlFreeNode (tree);
915
916 return successful;
917}
918
919sixtp*
920gnc_template_transaction_sixtp_parser_create (void)
921{
922 return sixtp_dom_parser_new (gnc_template_transaction_end_handler,
923 NULL, NULL);
924}
Anchor Scheduled Transaction info in a book.
const char * xaccAccountGetName(const Account *acc)
Get the account's name.
Definition Account.cpp:3270
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition Account.cpp:1464
#define xaccAccountGetGUID(X)
Definition Account.h:252
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
Definition Account.cpp:3389
void xaccAccountSetCommodity(Account *acc, gnc_commodity *com)
Set the account's commodity.
Definition Account.cpp:2659
Account * xaccAccountLookup(const GncGUID *guid, QofBook *book)
The xaccAccountLookup() subroutine will return the account associated with the given id,...
Definition Account.cpp:2039
gnc_commodity * gnc_commodity_new(QofBook *book, const char *fullname, const char *name_space, const char *mnemonic, const char *cusip, int fraction)
Create a new commodity.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
Account * gnc_account_lookup_by_name(const Account *parent, const char *name)
The gnc_account_lookup_by_name() subroutine fetches the account by name from the descendants of the s...
Definition Account.cpp:3074
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
Definition guid.h:84
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
Definition guid.cpp:208
#define DEBUG(format, args...)
Print a debugging message.
Definition qoflog.h:264
#define xaccSchedXactionGetGUID(X)
GList * gnc_sx_get_defer_instances(SchedXaction *sx)
Returns the defer list from the SX; this is a (date-)sorted temporal-state-data instance list.
gint gnc_sx_get_instance_count(const SchedXaction *sx, SXTmpStateData *stateData)
Get the instance count.
void gnc_sx_set_schedule(SchedXaction *sx, GList *schedule)
Account * gnc_book_get_template_root(const QofBook *book)
Returns the template group from the book.
Definition SX-book.cpp:65
gboolean xaccSchedXactionHasOccurDef(const SchedXaction *sx)
Returns true if the scheduled transaction has a defined number of occurrences, false if not.
const GDate * xaccSchedXactionGetEndDate(const SchedXaction *sx)
Returns invalid date when there is no end-date specified.
SchedXaction * xaccSchedXactionMalloc(QofBook *book)
Creates and initializes a scheduled transaction.
GList * gnc_sx_get_schedule(const SchedXaction *sx)
void xaccSchedXactionSetName(SchedXaction *sx, const gchar *newName)
A copy of the name is made.
void xaccSchedXactionSetEndDate(SchedXaction *sx, const GDate *newEnd)
Set to an invalid GDate to turn off 'end-date' definition.
void xaccSchedXactionDestroy(SchedXaction *sx)
Cleans up and frees a SchedXaction and its associated data.
void xaccSchedXactionSetNumOccur(SchedXaction *sx, gint new_num)
Set to '0' to turn off number-of-occurrences definition.
void gnc_sx_set_instance_count(SchedXaction *sx, gint instance_num)
Sets the instance count to something other than the default.
api for GnuCash version 2 XML-based file format
STRUCTS.
The type used to store guids in C.
Definition guid.h:75
QofBook reference.
Definition qofbook-p.hpp:47
Just the variable temporal bits from the SX structure.
Definition sixtp.h:130