2005-11-01 21:32:36 -06:00
|
|
|
/*
|
|
|
|
* gnc-recurrence-xml-v2.c -- xml routines for Recurrence
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Chris Shoemaker <c.shoemaker@cox.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, contact:
|
|
|
|
*
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org
|
|
|
|
*/
|
|
|
|
|
2015-11-29 19:11:29 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
2015-11-29 19:11:29 -06:00
|
|
|
#include "qof.h"
|
|
|
|
#include "Recurrence.h"
|
|
|
|
}
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
#include "gnc-xml.h"
|
|
|
|
#include "gnc-xml-helper.h"
|
|
|
|
|
|
|
|
#include "sixtp.h"
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
#include "sixtp-parsers.h"
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
#include "sixtp-dom-generators.h"
|
|
|
|
#include "io-gncxml-v2.h"
|
|
|
|
|
|
|
|
static QofLogModule log_module = GNC_MOD_IO;
|
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
const gchar* recurrence_version_string = "1.0.0";
|
2005-11-01 21:32:36 -06:00
|
|
|
#define recurrence_root "gnc:recurrence"
|
|
|
|
#define recurrence_mult "recurrence:mult"
|
|
|
|
#define recurrence_period_type "recurrence:period_type"
|
|
|
|
#define recurrence_start "recurrence:start"
|
2008-12-06 15:54:16 -06:00
|
|
|
#define recurrence_weekend_adj "recurrence:weekend_adj"
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
//TODO: I think three of these functions rightly belong in Recurrence.c.
|
|
|
|
|
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
recurrence_period_type_handler (xmlNodePtr node, gpointer d)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
|
|
|
PeriodType pt;
|
2016-03-12 16:04:40 -06:00
|
|
|
char* nodeTxt;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
nodeTxt = dom_tree_to_text (node);
|
|
|
|
g_return_val_if_fail (nodeTxt, FALSE);
|
|
|
|
pt = recurrencePeriodTypeFromString (nodeTxt);
|
|
|
|
((Recurrence*) d)->ptype = pt;
|
|
|
|
g_free (nodeTxt);
|
2005-11-01 21:32:36 -06:00
|
|
|
return (pt != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
recurrence_start_date_handler (xmlNodePtr node, gpointer r)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
GDate* d;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
d = dom_tree_to_gdate (node);
|
|
|
|
g_return_val_if_fail (d, FALSE);
|
|
|
|
g_return_val_if_fail (g_date_valid (d), FALSE);
|
|
|
|
((Recurrence*) r)->start = *d;
|
|
|
|
g_date_free (d);
|
2005-11-01 21:32:36 -06:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
recurrence_mult_handler (xmlNodePtr node, gpointer r)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
2016-03-12 16:04:40 -06:00
|
|
|
return dom_tree_to_guint16 (node, & ((Recurrence*)r)->mult);
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
|
|
|
|
2008-12-06 15:54:16 -06:00
|
|
|
static gboolean
|
2016-03-12 16:04:40 -06:00
|
|
|
recurrence_weekend_adj_handler (xmlNodePtr node, gpointer d)
|
2008-12-06 15:54:16 -06:00
|
|
|
{
|
|
|
|
WeekendAdjust wadj;
|
2016-03-12 16:04:40 -06:00
|
|
|
char* nodeTxt;
|
2008-12-06 15:54:16 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
nodeTxt = dom_tree_to_text (node);
|
|
|
|
g_return_val_if_fail (nodeTxt, FALSE);
|
|
|
|
wadj = recurrenceWeekendAdjustFromString (nodeTxt);
|
|
|
|
((Recurrence*) d)->wadj = wadj;
|
|
|
|
g_free (nodeTxt);
|
2008-12-06 15:54:16 -06:00
|
|
|
return (wadj != -1);
|
|
|
|
}
|
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
static struct dom_tree_handler recurrence_dom_handlers[] =
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
{ recurrence_mult, recurrence_mult_handler, 1, 0 },
|
|
|
|
{ recurrence_period_type, recurrence_period_type_handler, 1, 0 },
|
|
|
|
{ recurrence_start, recurrence_start_date_handler, 1, 0 },
|
2008-12-06 15:54:16 -06:00
|
|
|
{ recurrence_weekend_adj, recurrence_weekend_adj_handler, 0, 0 },
|
2005-11-01 21:32:36 -06:00
|
|
|
{ NULL, NULL, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2007-02-18 11:39:54 -06:00
|
|
|
Recurrence*
|
2016-03-12 16:04:40 -06:00
|
|
|
dom_tree_to_recurrence (xmlNodePtr node)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
|
|
|
gboolean successful;
|
2016-03-12 16:04:40 -06:00
|
|
|
Recurrence* r;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
r = g_new (Recurrence, 1);
|
2009-01-31 13:49:49 -06:00
|
|
|
/* In case the file doesn't have a weekend adjustment element */
|
|
|
|
r->wadj = WEEKEND_ADJ_NONE;
|
2005-11-01 21:32:36 -06:00
|
|
|
successful = dom_tree_generic_parse (node, recurrence_dom_handlers, r);
|
2009-12-29 14:12:48 -06:00
|
|
|
if (!successful)
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
PERR ("failed to parse recurrence node");
|
2016-03-12 16:04:40 -06:00
|
|
|
xmlElemDump (stdout, NULL, node);
|
|
|
|
g_free (r);
|
2005-11-01 21:32:36 -06:00
|
|
|
r = NULL;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlNodePtr
|
2016-03-12 16:04:40 -06:00
|
|
|
recurrence_to_dom_tree (const gchar* tag, const Recurrence* r)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
|
|
|
xmlNodePtr n;
|
|
|
|
PeriodType pt;
|
|
|
|
GDate d;
|
2008-12-06 15:54:16 -06:00
|
|
|
WeekendAdjust wadj;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2016-03-12 16:04:40 -06:00
|
|
|
n = xmlNewNode (NULL, BAD_CAST tag);
|
|
|
|
xmlSetProp (n, BAD_CAST "version", BAD_CAST recurrence_version_string);
|
|
|
|
xmlAddChild (n, guint_to_dom_tree (recurrence_mult,
|
|
|
|
recurrenceGetMultiplier (r)));
|
|
|
|
pt = recurrenceGetPeriodType (r);
|
|
|
|
xmlAddChild (n, text_to_dom_tree (recurrence_period_type,
|
|
|
|
recurrencePeriodTypeToString (pt)));
|
|
|
|
d = recurrenceGetDate (r);
|
|
|
|
xmlAddChild (n, gdate_to_dom_tree (recurrence_start, &d));
|
|
|
|
wadj = recurrenceGetWeekendAdjust (r);
|
2009-10-07 15:45:00 -05:00
|
|
|
if (wadj != WEEKEND_ADJ_NONE)
|
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
/* In r17725 and r17751, I introduced this extra XML child
|
|
|
|
element, but this means a gnucash-2.2.x cannot read the SX
|
|
|
|
recurrence of a >=2.3.x file anymore, which is bad. In order
|
|
|
|
to improve this broken backward compatibility for most of the
|
|
|
|
cases, we don't write out this XML element as long as it is
|
|
|
|
only "none". */
|
2016-03-12 16:04:40 -06:00
|
|
|
xmlAddChild (n, text_to_dom_tree (recurrence_weekend_adj,
|
|
|
|
recurrenceWeekendAdjustToString (wadj)));
|
2009-10-07 15:45:00 -05:00
|
|
|
}
|
2005-11-01 21:32:36 -06:00
|
|
|
return n;
|
|
|
|
}
|