GnuCash 7574033+
Loading...
Searching...
No Matches
gnc-freqspec-xml-v2.cpp
1/********************************************************************
2 * gnc-freqspec-xml-v2.c -- xml routines for FreqSpecs *
3 * Copyright (C) 2001 Joshua Sled <jsled@asynchronous.org> *
4 * Copyright (C) 2001 Ben Stanley <bds02@uow.edu.au> *
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
25#include <glib.h>
26
27#include <config.h>
28#include <string.h>
29#include "qof.h"
30#include "SchedXaction.h"
31#include "FreqSpec.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
43#include "io-gncxml-v2.h"
44
45#include "sixtp-dom-parsers.h"
46
47const gchar* freqspec_version_string = "1.0.0";
48
50{
51 const char* str;
52 FreqType ft;
53};
54
55struct freqTypeTuple freqTypeStrs[] =
56{
57 { "none", INVALID },
58 { "once", ONCE },
59 { "daily", DAILY },
60 { "weekly", WEEKLY },
61 { "monthly", MONTHLY },
62 { "month_relative", MONTH_RELATIVE },
63 { "composite", COMPOSITE },
64 { NULL, static_cast<FreqType> (-1) },
65};
66
68{
69 const char* str;
70 UIFreqType uift;
71};
72
73struct uiFreqTypeTuple uiFreqTypeStrs[] =
74{
75 { "none", UIFREQ_NONE },
76 { "once", UIFREQ_ONCE },
77 { "daily", UIFREQ_DAILY },
78 { "daily_mf", UIFREQ_DAILY_MF },
79 { "weekly", UIFREQ_WEEKLY },
80 { "bi_weekly", UIFREQ_BI_WEEKLY },
81 { "semi_monthly", UIFREQ_SEMI_MONTHLY },
82 { "monthly", UIFREQ_MONTHLY },
83 { "quarterly", UIFREQ_QUARTERLY },
84 { "tri_anually", UIFREQ_TRI_ANUALLY },
85 { "semi_yearly", UIFREQ_SEMI_YEARLY },
86 { "yearly", UIFREQ_YEARLY },
87 { NULL, static_cast<UIFreqType> (-1) }
88};
89
93typedef struct
94{
95 QofBook* book; /* Book we're loading into. */
96
97 Recurrence* recurrence;
98 GList* recurrence_list;
99
100 /* fields used in the union of unions... :) */
101 GDate once_day; /* once */
102 gint64 interval; /* all [except once] */
103 gint64 offset; /* all [except once] */
104 gint64 day; /* monthly or month-relative */
105 gint64 occurrence; /* month-relative */
106 gint64 weekend_adj; /* monthly/yearly */
107 GList* list; /* composite */
108 UIFreqType uift;
110
111static void
112fspd_init (fsParseData* fspd)
113{
114 fspd->list = NULL;
115 fspd->book = NULL;
116 fspd->recurrence = g_new0 (Recurrence, 1);
117 fspd->recurrence_list = NULL;
118 fspd->uift = UIFREQ_NONE;
119 fspd->interval
120 = fspd->offset
121 = fspd->day
122 = fspd->occurrence
123 = 0;
124 fspd->weekend_adj = WEEKEND_ADJ_NONE;
125 g_date_clear (&fspd->once_day, 1);
126}
127
128static
129gboolean
130gnc_fs_handler (xmlNodePtr node, gpointer d);
131
132static
133gboolean
134fs_uift_handler (xmlNodePtr node, gpointer data)
135{
136 fsParseData* fspd = static_cast<decltype (fspd)> (data);
137 int i;
138
139 auto nodeTxt = dom_tree_to_text (node);
140
141 g_return_val_if_fail (nodeTxt, FALSE);
142 for (i = 0; uiFreqTypeStrs[i].str != NULL; i++)
143 {
144 if (g_strcmp0 (nodeTxt->c_str(), uiFreqTypeStrs[i].str) == 0)
145 {
146 fspd->uift = uiFreqTypeStrs[i].uift;
147 return TRUE;
148 }
149 }
150 return FALSE;
151}
152
153static
154gboolean
155fs_date_handler (xmlNodePtr node, gpointer data)
156{
157 fsParseData* fspd = static_cast<decltype (fspd)> (data);
158 auto foo = dom_tree_to_gdate (node);
159 if (!foo)
160 return FALSE;
161 fspd->once_day = *foo;
162 return TRUE;
163}
164
165static
166gboolean
167fs_interval_handler (xmlNodePtr node, gpointer data)
168{
169 fsParseData* fspd = static_cast<decltype (fspd)> (data);
170 gboolean ret;
171 gint64 foo;
172
173 ret = dom_tree_to_integer (node, &foo);
174 if (! ret)
175 {
176 return ret;
177 }
178 fspd->interval = foo;
179 return TRUE;
180}
181
182static
183gboolean
184fs_offset_handler (xmlNodePtr node, gpointer data)
185{
186 fsParseData* fspd = static_cast<decltype (fspd)> (data);
187 gboolean ret;
188 gint64 foo;
189
190 ret = dom_tree_to_integer (node, &foo);
191 if (! ret)
192 return ret;
193 fspd->offset = foo;
194 return TRUE;
195}
196
197static
198gboolean
199fs_day_handler (xmlNodePtr node, gpointer data)
200{
201 fsParseData* fspd = static_cast<decltype (fspd)> (data);
202 gboolean ret;
203 gint64 foo;
204
205 ret = dom_tree_to_integer (node, &foo);
206 if (! ret)
207 return ret;
208 fspd->day = foo;
209 return TRUE;
210}
211
212static
213gboolean
214fs_weekday_handler (xmlNodePtr node, gpointer data)
215{
216 fsParseData* fspd = static_cast<decltype (fspd)> (data);
217 gboolean ret;
218 gint64 foo;
219 ret = dom_tree_to_integer (node, &foo);
220 if (!ret)
221 return ret;
222 fspd->day = foo;
223 return TRUE;
224}
225
226static
227gboolean
228fs_occurrence_handler (xmlNodePtr node, gpointer data)
229{
230 fsParseData* fspd = static_cast<decltype (fspd)> (data);
231 gboolean ret;
232 gint64 foo;
233 ret = dom_tree_to_integer (node, &foo);
234 if (!ret)
235 return ret;
236 fspd->occurrence = foo;
237 return TRUE;
238}
239
240static
241gboolean
242fs_weekend_adj_handler (xmlNodePtr node, gpointer data)
243{
244 fsParseData* fspd = static_cast<decltype (fspd)> (data);
245 gboolean ret;
246 gint64 foo;
247 ret = dom_tree_to_integer (node, &foo);
248 if (!ret)
249 return ret;
250 fspd->weekend_adj = foo;
251 return TRUE;
252}
253
254static
255gboolean
256fs_subelement_handler (xmlNodePtr node, gpointer data)
257{
258 fsParseData* fspd = static_cast<decltype (fspd)> (data);
259 GList* recurrences;
260
261 recurrences = dom_tree_freqSpec_to_recurrences (node, fspd->book);
262 if (recurrences == NULL)
263 return FALSE;
264
265 {
266 GList* r_iter;
267 for (r_iter = recurrences; r_iter != NULL; r_iter = r_iter->next)
268 {
269 Recurrence* r = (Recurrence*)r_iter->data;
270 GDate recurrence_date;
271 if (fspd->uift == UIFREQ_SEMI_MONTHLY)
272 {
273 // complementry hack around 'once' freqspects not being valid. :/
274 recurrence_date = recurrenceGetDate (r);
275 recurrenceSet (r, recurrenceGetMultiplier (r), PERIOD_MONTH, &recurrence_date,
276 recurrenceGetWeekendAdjust (r));
277 }
278 fspd->recurrence_list = g_list_append (fspd->recurrence_list, r);
279 }
280 }
281 return TRUE;
282}
283
284struct dom_tree_handler fs_union_dom_handlers[] =
285{
286 { "fs:date", fs_date_handler, 0, 0 },
287 { "fs:interval", fs_interval_handler, 0, 0 },
288 { "fs:offset", fs_offset_handler, 0, 0 },
289 { "fs:day", fs_day_handler, 0, 0 },
290 { "fs:weekday", fs_weekday_handler, 0, 0 },
291 { "fs:occurrence", fs_occurrence_handler, 0, 0 },
292 { "fs:weekend_adj", fs_weekend_adj_handler, 0, 0 },
293 { "gnc:freqspec", fs_subelement_handler, 0, 0 },
294 { NULL, NULL, 0, 0 },
295};
296
297static gboolean
298fs_none_handler (xmlNodePtr node, gpointer data)
299{
300 fsParseData* fspd = static_cast<decltype (fspd)> (data);
301 gboolean successful;
302 successful = dom_tree_generic_parse (node,
303 fs_union_dom_handlers,
304 fspd);
305 return successful;
306}
307
308static
309gboolean
310fs_once_handler (xmlNodePtr node, gpointer data)
311{
312 fsParseData* fspd = static_cast<decltype (fspd)> (data);
313 gboolean successful;
314
315 successful = dom_tree_generic_parse (node,
316 fs_union_dom_handlers,
317 fspd);
318 if (!successful)
319 return FALSE;
320 recurrenceSet (fspd->recurrence, 0, PERIOD_ONCE, &fspd->once_day,
321 WEEKEND_ADJ_NONE);
322
323 return TRUE;
324}
325
326static gboolean
327fs_daily_handler (xmlNodePtr node, gpointer data)
328{
329 fsParseData* fspd = static_cast<decltype (fspd)> (data);
330 GDate offset_date;
331 gboolean successful;
332 successful = dom_tree_generic_parse (node, fs_union_dom_handlers, fspd);
333 if (!successful)
334 return FALSE;
335
336 g_date_clear (&offset_date, 1);
337 g_date_set_julian (&offset_date, fspd->offset == 0 ? 7 : fspd->offset);
338 recurrenceSet (fspd->recurrence, fspd->interval, PERIOD_DAY, &offset_date,
339 WEEKEND_ADJ_NONE);
340
341 return TRUE;
342}
343
344static
345gboolean
346fs_weekly_handler (xmlNodePtr node, gpointer data)
347{
348 fsParseData* fspd = static_cast<decltype (fspd)> (data);
349 GDate offset_date;
350 gboolean successful;
351 successful = dom_tree_generic_parse (node,
352 fs_union_dom_handlers,
353 fspd);
354 if (!successful)
355 return FALSE;
356
357 g_date_clear (&offset_date, 1);
358 g_date_set_julian (&offset_date, fspd->offset == 0 ? 7 : fspd->offset);
359 recurrenceSet (fspd->recurrence, fspd->interval, PERIOD_WEEK, &offset_date,
360 WEEKEND_ADJ_NONE);
361
362 return TRUE;
363}
364
365static
366gboolean
367fs_monthly_handler (xmlNodePtr node, gpointer data)
368{
369 fsParseData* fspd = static_cast<decltype (fspd)> (data);
370 GDate offset_date;
371 gboolean successful;
372 successful = dom_tree_generic_parse (node,
373 fs_union_dom_handlers,
374 fspd);
375 if (!successful)
376 return FALSE;
377
378
379 g_date_clear (&offset_date, 1);
380 g_date_set_julian (&offset_date, 1);
381 g_date_add_months (&offset_date, fspd->offset);
382 g_date_set_day (&offset_date, fspd->day);
383 if (fspd->uift == UIFREQ_ONCE)
384 {
385 // hack...
386 recurrenceSet (fspd->recurrence, fspd->interval, PERIOD_ONCE, &offset_date,
387 WEEKEND_ADJ_NONE);
388 }
389 else
390 {
391 recurrenceSet (fspd->recurrence, fspd->interval,
392 PERIOD_MONTH, &offset_date,
393 static_cast<WeekendAdjust> (fspd->weekend_adj));
394 }
395
396 return successful;
397}
398
399static
400gboolean
401fs_month_relative_handler (xmlNodePtr node, gpointer data)
402{
403 g_critical ("this was never supported, how is it in the datafile?");
404 return FALSE;
405}
406
407static
408gboolean
409fs_guid_handler (xmlNodePtr node, gpointer data)
410{
411 return TRUE;
412}
413
414static
415gboolean
416fs_composite_handler (xmlNodePtr node, gpointer data)
417{
418 fsParseData* fspd = static_cast<decltype (fspd)> (data);
419 gboolean successful;
420 successful = dom_tree_generic_parse (node,
421 fs_union_dom_handlers,
422 fspd);
423 return successful;
424}
425
426static struct dom_tree_handler fs_dom_handlers[] =
427{
428 { "gnc:freqspec", gnc_fs_handler, 0, 0 },
429 { "fs:ui_type", fs_uift_handler, 1, 0 },
430 { "fs:id", fs_guid_handler, 1, 0 },
431 { "fs:none", fs_none_handler, 0, 0 },
432 { "fs:once", fs_once_handler, 0, 0 },
433 { "fs:daily", fs_daily_handler, 0, 0 },
434 { "fs:weekly", fs_weekly_handler, 0, 0 },
435 { "fs:monthly", fs_monthly_handler, 0, 0 },
436 { "fs:month_relative", fs_month_relative_handler, 0, 0 },
437 { "fs:composite", fs_composite_handler, 0, 0 },
438 { NULL, NULL, 0, 0 }
439};
440
441static
442gboolean
443gnc_fs_handler (xmlNodePtr node, gpointer d)
444{
445 return dom_tree_generic_parse (node, fs_dom_handlers, d);
446}
447
448static
449gboolean
450gnc_freqSpec_end_handler (gpointer data_for_children,
451 GSList* data_from_children, GSList* sibling_data,
452 gpointer parent_data, gpointer global_data,
453 gpointer* result, const gchar* tag)
454{
455 fsParseData fspd;
456 gboolean successful = FALSE;
457 xmlNodePtr tree = (xmlNodePtr)data_for_children;
458 sixtp_gdv2* globaldata = (sixtp_gdv2*)global_data;
459
460 fspd_init (&fspd);
461 fspd.book = globaldata->book;
462
463 /* this won't actually get invoked [FreqSpecs aren't top-level
464 elements]; see dom_tree_to_freqSpec(), below. */
465 if (parent_data)
466 return TRUE;
467
468 if (!tag)
469 return TRUE;
470
471 g_return_val_if_fail (tree, FALSE);
472
473 successful = dom_tree_generic_parse (tree, fs_dom_handlers, &fspd);
474 if (!successful)
475 {
476 xmlElemDump (stdout, NULL, tree);
477 }
478
479 xmlFreeNode (tree);
480
481 return successful;
482}
483
484sixtp*
485gnc_freqSpec_sixtp_parser_create (void)
486{
487 return sixtp_dom_parser_new (gnc_freqSpec_end_handler, NULL, NULL);
488}
489
490static void
491common_parse (fsParseData* fspd, xmlNodePtr node, QofBook* book)
492{
493 gboolean successful;
494
495 fspd->book = book;
496 successful = dom_tree_generic_parse (node, fs_dom_handlers, fspd);
497 if (!successful)
498 {
499 xmlElemDump (stdout, NULL, node);
500 }
501}
502
503GList*
504dom_tree_freqSpec_to_recurrences (xmlNodePtr node, QofBook* book)
505{
506 fsParseData fspd;
507 fspd_init (&fspd);
508 common_parse (&fspd, node, book);
509 if (fspd.recurrence_list == NULL)
510 {
511 fspd.recurrence_list = g_list_append (fspd.recurrence_list, fspd.recurrence);
512 }
513 return fspd.recurrence_list;
514}
Period / Date Frequency Specification.
Scheduled Transactions public handling routines.
FreqType
Frequency specification.
Definition FreqSpec.h:43
UIFreqType
The user's conception of the frequency.
Definition FreqSpec.h:68
api for GnuCash version 2 XML-based file format
QofBook reference.
Definition qofbook-p.hpp:47
Struct passed around as user-data when parsing the FreqSpec.
Definition sixtp.h:130