Line data Source code
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"
47 : static 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 :
76 : const gchar* schedxaction_version_string = "1.0.0";
77 : const gchar* schedxaction_version2_string = "2.0.0";
78 :
79 : xmlNodePtr
80 2 : gnc_schedXaction_dom_tree_create (SchedXaction* sx)
81 : {
82 : xmlNodePtr ret;
83 : const GDate* date;
84 : gint instCount;
85 : const GncGUID* templ_acc_guid;
86 2 : gboolean allow_2_2_incompat = TRUE;
87 2 : gchar* name = g_strdup (xaccSchedXactionGetName (sx));
88 :
89 2 : templ_acc_guid = xaccAccountGetGUID (sx->template_acct);
90 :
91 : /* FIXME: this should be the same as the def in io-gncxml-v2.c */
92 2 : ret = xmlNewNode (NULL, BAD_CAST GNC_SCHEDXACTION_TAG);
93 :
94 2 : if (allow_2_2_incompat)
95 2 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST schedxaction_version2_string);
96 : else
97 0 : xmlSetProp (ret, BAD_CAST "version", BAD_CAST schedxaction_version_string);
98 :
99 2 : xmlAddChild (ret,
100 : guid_to_dom_tree (SX_ID,
101 2 : xaccSchedXactionGetGUID (sx)));
102 :
103 2 : xmlNewTextChild (ret, NULL, BAD_CAST SX_NAME, checked_char_cast (name));
104 2 : g_free (name);
105 :
106 2 : if (allow_2_2_incompat)
107 : {
108 0 : xmlNewTextChild (ret, NULL, BAD_CAST SX_ENABLED,
109 2 : BAD_CAST (sx->enabled ? "y" : "n"));
110 : }
111 :
112 0 : xmlNewTextChild (ret, NULL, BAD_CAST SX_AUTOCREATE,
113 2 : BAD_CAST (sx->autoCreateOption ? "y" : "n"));
114 0 : xmlNewTextChild (ret, NULL, BAD_CAST SX_AUTOCREATE_NOTIFY,
115 2 : BAD_CAST (sx->autoCreateNotify ? "y" : "n"));
116 2 : xmlAddChild (ret, int_to_dom_tree (SX_ADVANCE_CREATE_DAYS,
117 2 : sx->advanceCreateDays));
118 2 : xmlAddChild (ret, int_to_dom_tree (SX_ADVANCE_REMIND_DAYS,
119 2 : sx->advanceRemindDays));
120 :
121 2 : instCount = gnc_sx_get_instance_count (sx, NULL);
122 2 : xmlAddChild (ret, int_to_dom_tree (SX_INSTANCE_COUNT,
123 : instCount));
124 :
125 2 : xmlAddChild (ret,
126 : gdate_to_dom_tree (SX_START,
127 : xaccSchedXactionGetStartDate (sx)));
128 :
129 2 : date = xaccSchedXactionGetLastOccurDate (sx);
130 2 : if (g_date_valid (date))
131 : {
132 2 : xmlAddChild (ret, gdate_to_dom_tree (SX_LAST, date));
133 : }
134 :
135 2 : if (xaccSchedXactionHasOccurDef (sx))
136 : {
137 :
138 0 : xmlAddChild (ret, int_to_dom_tree (SX_NUM_OCCUR,
139 0 : xaccSchedXactionGetNumOccur (sx)));
140 0 : xmlAddChild (ret, int_to_dom_tree (SX_REM_OCCUR,
141 0 : xaccSchedXactionGetRemOccur (sx)));
142 :
143 : }
144 2 : else if (xaccSchedXactionHasEndDate (sx))
145 : {
146 0 : xmlAddChild (ret,
147 : gdate_to_dom_tree (SX_END,
148 : xaccSchedXactionGetEndDate (sx)));
149 : }
150 :
151 : /* output template account GncGUID */
152 2 : xmlAddChild (ret,
153 : guid_to_dom_tree (SX_TEMPL_ACCT,
154 : templ_acc_guid));
155 :
156 2 : if (allow_2_2_incompat)
157 : {
158 2 : xmlNodePtr schedule_node = xmlNewNode (NULL,
159 : BAD_CAST "sx:schedule");
160 2 : GList* schedule = gnc_sx_get_schedule (sx);
161 4 : for (; schedule != NULL; schedule = schedule->next)
162 : {
163 2 : xmlAddChild (schedule_node, recurrence_to_dom_tree ("gnc:recurrence",
164 2 : (Recurrence*)schedule->data));
165 : }
166 2 : xmlAddChild (ret, schedule_node);
167 : }
168 :
169 : /* Output deferred-instance list. */
170 : {
171 : xmlNodePtr instNode;
172 : SXTmpStateData* tsd;
173 : GList* l;
174 :
175 2 : for (l = gnc_sx_get_defer_instances (sx); l; l = l->next)
176 : {
177 0 : tsd = (SXTmpStateData*)l->data;
178 :
179 0 : instNode = xmlNewNode (NULL, BAD_CAST SX_DEFER_INSTANCE);
180 0 : if (g_date_valid (&tsd->last_date))
181 : {
182 0 : xmlAddChild (instNode, gdate_to_dom_tree (SX_LAST,
183 0 : &tsd->last_date));
184 : }
185 0 : xmlAddChild (instNode, int_to_dom_tree (SX_REM_OCCUR,
186 0 : tsd->num_occur_rem));
187 0 : xmlAddChild (instNode, int_to_dom_tree (SX_INSTANCE_COUNT,
188 0 : tsd->num_inst));
189 0 : xmlAddChild (ret, instNode);
190 : }
191 : }
192 :
193 : /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
194 2 : xmlAddChild (ret, qof_instance_slots_to_dom_tree (SX_SLOTS,
195 2 : QOF_INSTANCE (sx)));
196 2 : return ret;
197 : }
198 :
199 : struct sx_pdata
200 : {
201 : SchedXaction* sx;
202 : QofBook* book;
203 : gboolean saw_freqspec;
204 : gboolean saw_recurrence;
205 : };
206 :
207 : static
208 : gboolean
209 5 : sx_id_handler (xmlNodePtr node, gpointer sx_pdata)
210 : {
211 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
212 5 : SchedXaction* sx = pdata->sx;
213 5 : auto tmp = dom_tree_to_guid (node);
214 :
215 5 : g_return_val_if_fail (tmp, FALSE);
216 5 : xaccSchedXactionSetGUID (sx, &*tmp);
217 :
218 5 : return TRUE;
219 : }
220 :
221 : static
222 : gboolean
223 5 : sx_name_handler (xmlNodePtr node, gpointer sx_pdata)
224 : {
225 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
226 5 : return apply_xmlnode_text (xaccSchedXactionSetName, pdata->sx, node);
227 : }
228 :
229 : static gboolean
230 5 : sx_enabled_handler (xmlNodePtr node, gpointer sx_pdata)
231 : {
232 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
233 5 : auto set_enabled = [](SchedXaction* sx, const char* txt)
234 : {
235 5 : sx->enabled = !g_strcmp0 (txt, "y");
236 5 : };
237 10 : return apply_xmlnode_text (set_enabled, pdata->sx, node);
238 : }
239 :
240 : static gboolean
241 5 : sx_autoCreate_handler (xmlNodePtr node, gpointer sx_pdata)
242 : {
243 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
244 5 : auto set_autocreate = [](SchedXaction* sx, const char* txt)
245 : {
246 5 : sx->autoCreateOption = !g_strcmp0 (txt, "y");
247 5 : };
248 10 : return apply_xmlnode_text (set_autocreate, pdata->sx, node);
249 : }
250 :
251 : static gboolean
252 5 : sx_notify_handler (xmlNodePtr node, gpointer sx_pdata)
253 : {
254 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
255 5 : auto set_notify = [](SchedXaction* sx, const char* txt)
256 : {
257 5 : sx->autoCreateNotify = !g_strcmp0 (txt, "y");
258 5 : };
259 10 : return apply_xmlnode_text (set_notify, pdata->sx, node);
260 : }
261 :
262 : static gboolean
263 5 : sx_advCreate_handler (xmlNodePtr node, gpointer sx_pdata)
264 : {
265 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
266 5 : SchedXaction* sx = pdata->sx;
267 : gint64 advCreate;
268 :
269 5 : if (! dom_tree_to_integer (node, &advCreate))
270 : {
271 0 : return FALSE;
272 : }
273 :
274 5 : xaccSchedXactionSetAdvanceCreation (sx, advCreate);
275 5 : return TRUE;
276 : }
277 :
278 : static gboolean
279 5 : sx_advRemind_handler (xmlNodePtr node, gpointer sx_pdata)
280 : {
281 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
282 5 : SchedXaction* sx = pdata->sx;
283 : gint64 advRemind;
284 :
285 5 : if (! dom_tree_to_integer (node, &advRemind))
286 : {
287 0 : return FALSE;
288 : }
289 :
290 5 : xaccSchedXactionSetAdvanceReminder (sx, advRemind);
291 5 : return TRUE;
292 : }
293 :
294 : static
295 : gboolean
296 8 : sx_set_date (xmlNodePtr node, SchedXaction* sx,
297 : void (*settor) (SchedXaction* sx, const GDate* d))
298 : {
299 8 : auto date = dom_tree_to_gdate (node);
300 8 : g_return_val_if_fail (date, FALSE);
301 8 : (*settor) (sx, &date.value());
302 :
303 8 : return TRUE;
304 : }
305 :
306 : static
307 : gboolean
308 5 : sx_instcount_handler (xmlNodePtr node, gpointer sx_pdata)
309 : {
310 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
311 5 : SchedXaction* sx = pdata->sx;
312 : gint64 instanceNum;
313 :
314 5 : if (! dom_tree_to_integer (node, &instanceNum))
315 : {
316 0 : return FALSE;
317 : }
318 :
319 5 : gnc_sx_set_instance_count (sx, instanceNum);
320 5 : return TRUE;
321 : }
322 :
323 : static
324 : gboolean
325 5 : sx_start_handler (xmlNodePtr node, gpointer sx_pdata)
326 : {
327 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
328 5 : SchedXaction* sx = pdata->sx;
329 :
330 5 : return sx_set_date (node, sx, xaccSchedXactionSetStartDate);
331 : }
332 :
333 : static
334 : gboolean
335 2 : sx_last_handler (xmlNodePtr node, gpointer sx_pdata)
336 : {
337 2 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
338 2 : SchedXaction* sx = pdata->sx;
339 :
340 2 : return sx_set_date (node, sx, xaccSchedXactionSetLastOccurDate);
341 : }
342 :
343 : static
344 : gboolean
345 1 : sx_end_handler (xmlNodePtr node, gpointer sx_pdata)
346 : {
347 1 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
348 1 : SchedXaction* sx = pdata->sx;
349 :
350 1 : return sx_set_date (node, sx, xaccSchedXactionSetEndDate);
351 : }
352 :
353 : static void
354 0 : _fixup_recurrence_start_dates (const GDate* sx_start_date, GList* schedule)
355 : {
356 : GList* iter;
357 0 : for (iter = schedule; iter != NULL; iter = iter->next)
358 : {
359 : Recurrence* r;
360 : GDate start, next;
361 :
362 0 : r = (Recurrence*)iter->data;
363 :
364 0 : start = *sx_start_date;
365 0 : g_date_subtract_days (&start, 1);
366 :
367 0 : g_date_clear (&next, 1);
368 :
369 0 : recurrenceNextInstance (r, &start, &next);
370 0 : g_return_if_fail (g_date_valid (&next));
371 :
372 : {
373 : gchar date_str[128];
374 : gchar* sched_str;
375 :
376 0 : g_date_strftime (date_str, 127, "%x", &next);
377 0 : sched_str = recurrenceToString (r);
378 0 : DEBUG ("setting recurrence [%s] start date to [%s]",
379 : sched_str, date_str);
380 0 : g_free (sched_str);
381 : }
382 :
383 0 : recurrenceSet (r,
384 0 : recurrenceGetMultiplier (r),
385 : recurrenceGetPeriodType (r),
386 : &next,
387 : recurrenceGetWeekendAdjust (r));
388 : }
389 :
390 0 : if (g_list_length (schedule) == 1
391 0 : && recurrenceGetPeriodType ((Recurrence*)g_list_nth_data (schedule,
392 : 0)) == PERIOD_ONCE)
393 : {
394 : char date_buf[128];
395 0 : Recurrence* fixup = (Recurrence*)g_list_nth_data (schedule, 0);
396 0 : g_date_strftime (date_buf, 127, "%x", sx_start_date);
397 0 : recurrenceSet (fixup, 1, PERIOD_ONCE, sx_start_date, WEEKEND_ADJ_NONE);
398 0 : DEBUG ("fixed up period=ONCE Recurrence to date [%s]", date_buf);
399 : }
400 : }
401 :
402 : static gboolean
403 0 : sx_freqspec_handler (xmlNodePtr node, gpointer sx_pdata)
404 : {
405 0 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
406 0 : SchedXaction* sx = pdata->sx;
407 : GList* schedule;
408 : gchar* debug_str;
409 :
410 0 : g_return_val_if_fail (node, FALSE);
411 :
412 0 : schedule = dom_tree_freqSpec_to_recurrences (node, pdata->book);
413 0 : gnc_sx_set_schedule (sx, schedule);
414 0 : debug_str = recurrenceListToString (schedule);
415 0 : DEBUG ("parsed from freqspec [%s]", debug_str);
416 0 : g_free (debug_str);
417 :
418 0 : _fixup_recurrence_start_dates (xaccSchedXactionGetStartDate (sx), schedule);
419 0 : pdata->saw_freqspec = TRUE;
420 :
421 0 : return TRUE;
422 : }
423 :
424 : static gboolean
425 5 : sx_schedule_recurrence_handler (xmlNodePtr node, gpointer parsing_data)
426 : {
427 5 : GList** schedule = (GList**)parsing_data;
428 : gchar* sched_str;
429 5 : Recurrence* r = dom_tree_to_recurrence (node);
430 5 : g_return_val_if_fail (r, FALSE);
431 5 : sched_str = recurrenceToString (r);
432 5 : DEBUG ("parsed recurrence [%s]", sched_str);
433 5 : g_free (sched_str);
434 5 : *schedule = g_list_append (*schedule, r);
435 5 : return TRUE;
436 : }
437 :
438 : struct dom_tree_handler sx_recurrence_list_handlers[] =
439 : {
440 : { "gnc:recurrence", sx_schedule_recurrence_handler, 0, 0 },
441 : { NULL, NULL, 0, 0 }
442 : };
443 :
444 : static gboolean
445 5 : sx_recurrence_handler (xmlNodePtr node, gpointer _pdata)
446 : {
447 5 : struct sx_pdata* parsing_data = static_cast<decltype (parsing_data)> (_pdata);
448 5 : GList* schedule = NULL;
449 : gchar* debug_str;
450 :
451 5 : g_return_val_if_fail (node, FALSE);
452 :
453 5 : if (!dom_tree_generic_parse (node, sx_recurrence_list_handlers, &schedule))
454 0 : return FALSE;
455 : // g_return_val_if_fail(schedule, FALSE);
456 5 : debug_str = recurrenceListToString (schedule);
457 5 : DEBUG ("setting freshly-parsed schedule: [%s]", debug_str);
458 5 : g_free (debug_str);
459 5 : gnc_sx_set_schedule (parsing_data->sx, schedule);
460 5 : parsing_data->saw_recurrence = TRUE;
461 5 : return TRUE;
462 : }
463 :
464 : static
465 : gboolean
466 0 : sx_defer_last_handler (xmlNodePtr node, gpointer gpTSD)
467 : {
468 0 : SXTmpStateData* tsd = (SXTmpStateData*)gpTSD;
469 :
470 0 : g_return_val_if_fail (node, FALSE);
471 0 : auto gd = dom_tree_to_gdate (node);
472 0 : g_return_val_if_fail (gd, FALSE);
473 0 : tsd->last_date = *gd;
474 0 : return TRUE;
475 : }
476 :
477 : static
478 : gboolean
479 0 : sx_defer_rem_occur_handler (xmlNodePtr node, gpointer gpTSD)
480 : {
481 : gint64 remOccur;
482 0 : SXTmpStateData* tsd = (SXTmpStateData*)gpTSD;
483 0 : g_return_val_if_fail (node, FALSE);
484 :
485 0 : if (! dom_tree_to_integer (node, &remOccur))
486 : {
487 0 : return FALSE;
488 : }
489 0 : tsd->num_occur_rem = remOccur;
490 0 : return TRUE;
491 : }
492 :
493 : static
494 : gboolean
495 0 : sx_defer_inst_count_handler (xmlNodePtr node, gpointer gpTSD)
496 : {
497 : gint64 instCount;
498 0 : SXTmpStateData* tsd = (SXTmpStateData*)gpTSD;
499 0 : g_return_val_if_fail (node, FALSE);
500 :
501 0 : if (! dom_tree_to_integer (node, &instCount))
502 : {
503 0 : return FALSE;
504 : }
505 0 : tsd->num_inst = instCount;
506 0 : return TRUE;
507 : }
508 :
509 : struct 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 :
518 : static
519 : gboolean
520 0 : sx_defer_inst_handler (xmlNodePtr node, gpointer sx_pdata)
521 : {
522 0 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
523 0 : SchedXaction* sx = pdata->sx;
524 : SXTmpStateData* tsd;
525 :
526 0 : g_return_val_if_fail (node, FALSE);
527 :
528 0 : tsd = g_new0 (SXTmpStateData, 1);
529 : g_assert (sx_defer_dom_handlers != NULL);
530 0 : if (!dom_tree_generic_parse (node,
531 : sx_defer_dom_handlers,
532 : tsd))
533 : {
534 0 : xmlElemDump (stdout, NULL, node);
535 0 : g_free (tsd);
536 0 : tsd = NULL;
537 0 : return FALSE;
538 : }
539 :
540 : /* We assume they were serialized in sorted order, here. */
541 0 : sx->deferredList = g_list_append (sx->deferredList, tsd);
542 0 : return TRUE;
543 : }
544 :
545 : static
546 : gboolean
547 1 : sx_numOccur_handler (xmlNodePtr node, gpointer sx_pdata)
548 : {
549 1 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
550 1 : SchedXaction* sx = pdata->sx;
551 : gint64 numOccur;
552 :
553 1 : if (! dom_tree_to_integer (node, &numOccur))
554 : {
555 0 : return FALSE;
556 : }
557 :
558 1 : xaccSchedXactionSetNumOccur (sx, numOccur);
559 :
560 1 : return TRUE;
561 : }
562 :
563 :
564 : static
565 : gboolean
566 5 : sx_templ_acct_handler (xmlNodePtr node, gpointer sx_pdata)
567 : {
568 5 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
569 5 : SchedXaction* sx = pdata->sx;
570 5 : auto templ_acct_guid = dom_tree_to_guid (node);
571 : Account* account;
572 :
573 5 : if (!templ_acct_guid)
574 : {
575 0 : return FALSE;
576 : }
577 :
578 5 : account = xaccAccountLookup (&*templ_acct_guid, pdata->book);
579 5 : sx_set_template_account (sx, account);
580 :
581 5 : return TRUE;
582 : }
583 :
584 :
585 : static
586 : gboolean
587 1 : sx_remOccur_handler (xmlNodePtr node, gpointer sx_pdata)
588 : {
589 1 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
590 1 : SchedXaction* sx = pdata->sx;
591 : gint64 remOccur;
592 :
593 1 : if (! dom_tree_to_integer (node, &remOccur))
594 : {
595 0 : return FALSE;
596 : }
597 :
598 1 : xaccSchedXactionSetRemOccur (sx, remOccur);
599 :
600 1 : return TRUE;
601 : }
602 :
603 : static
604 : gboolean
605 0 : sx_slots_handler (xmlNodePtr node, gpointer sx_pdata)
606 : {
607 0 : struct sx_pdata* pdata = static_cast<decltype (pdata)> (sx_pdata);
608 0 : SchedXaction* sx = pdata->sx;
609 :
610 0 : return dom_tree_create_instance_slots (node, QOF_INSTANCE (sx));
611 : }
612 :
613 : struct 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 :
636 : static gboolean
637 100 : gnc_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 100 : gboolean successful = FALSE;
644 100 : xmlNodePtr tree = (xmlNodePtr)data_for_children;
645 100 : gxpf_data* gdata = (gxpf_data*)global_data;
646 : struct sx_pdata sx_pdata;
647 :
648 100 : if (parent_data)
649 : {
650 95 : return TRUE;
651 : }
652 :
653 5 : if (!tag)
654 : {
655 0 : return TRUE;
656 : }
657 :
658 5 : g_return_val_if_fail (tree, FALSE);
659 :
660 5 : sx = xaccSchedXactionMalloc (static_cast<QofBook*> (gdata->bookdata));
661 :
662 5 : memset (&sx_pdata, 0, sizeof (sx_pdata));
663 5 : sx_pdata.sx = sx;
664 5 : sx_pdata.book = static_cast<decltype (sx_pdata.book)> (gdata->bookdata);
665 :
666 : g_assert (sx_dom_handlers != NULL);
667 :
668 5 : successful = dom_tree_generic_parse (tree, sx_dom_handlers, &sx_pdata);
669 5 : if (!successful)
670 : {
671 0 : g_critical ("failed to parse scheduled xaction");
672 0 : xmlElemDump (stdout, NULL, tree);
673 0 : gnc_sx_begin_edit (sx);
674 0 : xaccSchedXactionDestroy (sx);
675 0 : goto done;
676 : }
677 :
678 5 : if (tree->properties)
679 : {
680 5 : gchar* sx_name = xaccSchedXactionGetName (sx);
681 : xmlAttr* attr;
682 10 : for (attr = tree->properties; attr != NULL; attr = attr->next)
683 : {
684 5 : xmlChar* attr_value = attr->children->content;
685 5 : DEBUG ("sx attribute name[%s] value[%s]", attr->name, attr_value);
686 5 : if (strcmp ((const char*)attr->name, "version") != 0)
687 : {
688 0 : g_warning ("unknown sx attribute [%s]", attr->name);
689 0 : continue;
690 : }
691 :
692 : // if version == 1.0.0: ensure freqspec, no recurrence
693 : // if version == 2.0.0: ensure recurrence, no freqspec.
694 5 : if (strcmp ((const char*)attr_value,
695 : schedxaction_version_string) == 0)
696 : {
697 0 : if (!sx_pdata.saw_freqspec)
698 0 : g_critical ("did not see freqspec in version 1 sx [%s]", sx_name);
699 0 : if (sx_pdata.saw_recurrence)
700 0 : g_warning ("saw recurrence in supposedly version 1 sx [%s]", sx_name);
701 : }
702 :
703 5 : if (strcmp ((const char*)attr_value,
704 : schedxaction_version2_string) == 0)
705 : {
706 5 : if (sx_pdata.saw_freqspec)
707 0 : g_warning ("saw freqspec in version 2 sx [%s]", sx_name);
708 5 : if (!sx_pdata.saw_recurrence)
709 0 : 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 5 : gdata->cb (tag, gdata->parsedata, sx);
716 :
717 : /* FIXME: this should be removed somewhere near 1.8 release time. */
718 5 : if (sx->template_acct == NULL)
719 : {
720 0 : Account* ra = NULL;
721 0 : Account* acct = NULL;
722 0 : sixtp_gdv2* sixdata = static_cast<decltype (sixdata)> (gdata->parsedata);
723 : QofBook* book;
724 : gchar guidstr[GUID_ENCODING_LENGTH + 1];
725 :
726 0 : 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. */
732 0 : guid_to_string_buff (xaccSchedXactionGetGUID (sx), guidstr);
733 0 : ra = gnc_book_get_template_root (book);
734 0 : if (ra == NULL)
735 : {
736 0 : g_warning ("Error getting template root account from being-parsed Book.");
737 0 : xmlFreeNode (tree);
738 0 : return FALSE;
739 : }
740 0 : acct = gnc_account_lookup_by_name (ra, guidstr);
741 0 : if (acct == NULL)
742 : {
743 0 : g_warning ("no template account with name [%s]", guidstr);
744 0 : xmlFreeNode (tree);
745 0 : return FALSE;
746 : }
747 0 : 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 0 : sx->template_acct = acct;
757 : }
758 :
759 5 : done:
760 5 : xmlFreeNode (tree);
761 :
762 5 : return successful;
763 : }
764 :
765 : sixtp*
766 44 : gnc_schedXaction_sixtp_parser_create (void)
767 : {
768 44 : return sixtp_dom_parser_new (gnc_schedXaction_end_handler, NULL, NULL);
769 : }
770 :
771 : static
772 : gboolean
773 8 : tt_act_handler (xmlNodePtr node, gpointer data)
774 : {
775 8 : gnc_template_xaction_data* txd = static_cast<decltype (txd)> (data);
776 : Account* acc;
777 : gnc_commodity* com;
778 :
779 8 : acc = dom_tree_to_account (node, txd->book);
780 :
781 8 : if (acc == NULL)
782 : {
783 0 : return FALSE;
784 : }
785 : else
786 : {
787 8 : xaccAccountBeginEdit (acc);
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 8 : if (xaccAccountGetCommodity (acc) == NULL)
793 : {
794 : #if 1
795 : gnc_commodity_table* table;
796 :
797 1 : table = gnc_commodity_table_get_table (txd->book);
798 1 : 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 1 : xaccAccountSetCommodity (acc, com);
814 : }
815 :
816 8 : txd->accts = g_list_append (txd->accts, acc);
817 : }
818 :
819 8 : return TRUE;
820 : }
821 :
822 : static
823 : gboolean
824 5 : tt_trn_handler (xmlNodePtr node, gpointer data)
825 : {
826 5 : gnc_template_xaction_data* txd = static_cast<decltype (txd)> (data);
827 : Transaction* trn;
828 :
829 5 : trn = dom_tree_to_transaction (node, txd->book);
830 :
831 5 : if (trn == NULL)
832 : {
833 0 : return FALSE;
834 : }
835 : else
836 : {
837 5 : txd->transactions = g_list_append (txd->transactions, trn);
838 : }
839 :
840 5 : return TRUE;
841 : }
842 :
843 : struct 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 :
850 : static gboolean
851 373 : gnc_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 373 : gboolean successful = FALSE;
860 373 : xmlNodePtr tree = static_cast<decltype (tree)> (data_for_children);
861 373 : gxpf_data* gdata = static_cast<decltype (gdata)> (global_data);
862 373 : QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
863 : GList* n;
864 : gnc_template_xaction_data txd;
865 :
866 373 : txd.book = book;
867 373 : txd.accts = NULL;
868 373 : 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 373 : if (parent_data)
879 : {
880 370 : return TRUE;
881 : }
882 :
883 3 : if (!tag)
884 : {
885 0 : return TRUE;
886 : }
887 :
888 3 : g_return_val_if_fail (tree, FALSE);
889 :
890 3 : successful = dom_tree_generic_parse (tree, tt_dom_handlers, &txd);
891 :
892 3 : if (successful)
893 : {
894 3 : gdata->cb (tag, gdata->parsedata, &txd);
895 : }
896 : else
897 : {
898 0 : g_warning ("failed to parse template transaction");
899 0 : xmlElemDump (stdout, NULL, tree);
900 : }
901 :
902 : /* cleanup */
903 11 : for (n = txd.accts; n; n = n->next)
904 : {
905 8 : n->data = NULL;
906 : }
907 8 : for (n = txd.transactions; n; n = n->next)
908 : {
909 5 : n->data = NULL;
910 : }
911 3 : g_list_free (txd.accts);
912 3 : g_list_free (txd.transactions);
913 :
914 3 : xmlFreeNode (tree);
915 :
916 3 : return successful;
917 : }
918 :
919 : sixtp*
920 44 : gnc_template_transaction_sixtp_parser_create (void)
921 : {
922 44 : return sixtp_dom_parser_new (gnc_template_transaction_end_handler,
923 44 : NULL, NULL);
924 : }
|