GnuCash 2.4.99
gnc-recurrence-xml-v2.c
00001 /*
00002  * gnc-recurrence-xml-v2.c -- xml routines for Recurrence
00003  *
00004  * Copyright (C) 2005 Chris Shoemaker <c.shoemaker@cox.net>
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, contact:
00018  *
00019  * Free Software Foundation           Voice:  +1-617-542-5942
00020  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00021  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00022  */
00023 
00024 
00025 #include "config.h"
00026 
00027 #include <glib.h>
00028 #include <string.h>
00029 
00030 #include "gnc-xml.h"
00031 #include "gnc-xml-helper.h"
00032 #include "qof.h"
00033 
00034 #include "sixtp.h"
00035 #include "sixtp-utils.h"
00036 #include "sixtp-parsers.h"
00037 #include "sixtp-utils.h"
00038 #include "sixtp-dom-parsers.h"
00039 #include "sixtp-dom-generators.h"
00040 #include "io-gncxml-v2.h"
00041 #include "Recurrence.h"
00042 
00043 static QofLogModule log_module = GNC_MOD_IO;
00044 
00045 const gchar *recurrence_version_string = "1.0.0";
00046 #define recurrence_root          "gnc:recurrence"
00047 #define recurrence_mult          "recurrence:mult"
00048 #define recurrence_period_type   "recurrence:period_type"
00049 #define recurrence_start         "recurrence:start"
00050 #define recurrence_weekend_adj   "recurrence:weekend_adj"
00051 
00052 //TODO: I think three of these functions rightly belong in Recurrence.c.
00053 
00054 static gboolean
00055 recurrence_period_type_handler(xmlNodePtr node, gpointer d)
00056 {
00057     PeriodType pt;
00058     char *nodeTxt;
00059 
00060     nodeTxt = dom_tree_to_text(node);
00061     g_return_val_if_fail(nodeTxt, FALSE);
00062     pt = recurrencePeriodTypeFromString(nodeTxt);
00063     ((Recurrence *) d)->ptype = pt;
00064     g_free(nodeTxt);
00065     return (pt != -1);
00066 }
00067 
00068 static gboolean
00069 recurrence_start_date_handler(xmlNodePtr node, gpointer r)
00070 {
00071     GDate *d;
00072 
00073     d = dom_tree_to_gdate(node);
00074     g_return_val_if_fail(d, FALSE);
00075     g_return_val_if_fail(g_date_valid(d), FALSE);
00076     ((Recurrence *) r)->start = *d;
00077     g_date_free(d);
00078     return TRUE;
00079 }
00080 
00081 static gboolean
00082 recurrence_mult_handler(xmlNodePtr node, gpointer r)
00083 {
00084     return dom_tree_to_guint16(node, &((Recurrence *)r)->mult);
00085 }
00086 
00087 static gboolean
00088 recurrence_weekend_adj_handler(xmlNodePtr node, gpointer d)
00089 {
00090     WeekendAdjust wadj;
00091     char *nodeTxt;
00092 
00093     nodeTxt = dom_tree_to_text(node);
00094     g_return_val_if_fail(nodeTxt, FALSE);
00095     wadj = recurrenceWeekendAdjustFromString(nodeTxt);
00096     ((Recurrence *) d)->wadj = wadj;
00097     g_free(nodeTxt);
00098     return (wadj != -1);
00099 }
00100 
00101 static struct dom_tree_handler recurrence_dom_handlers[] =
00102 {
00103     { recurrence_mult, recurrence_mult_handler, 1, 0 },
00104     { recurrence_period_type, recurrence_period_type_handler, 1, 0 },
00105     { recurrence_start, recurrence_start_date_handler, 1, 0 },
00106     { recurrence_weekend_adj, recurrence_weekend_adj_handler, 0, 0 },
00107     { NULL, NULL, 0, 0 }
00108 };
00109 
00110 Recurrence*
00111 dom_tree_to_recurrence(xmlNodePtr node)
00112 {
00113     gboolean successful;
00114     Recurrence *r;
00115 
00116     r = g_new(Recurrence, 1);
00117     /* In case the file doesn't have a weekend adjustment element */
00118     r->wadj = WEEKEND_ADJ_NONE;
00119     successful = dom_tree_generic_parse (node, recurrence_dom_handlers, r);
00120     if (!successful)
00121     {
00122         PERR ("failed to parse recurrence node");
00123         xmlElemDump(stdout, NULL, node);
00124         g_free(r);
00125         r = NULL;
00126     }
00127     return r;
00128 }
00129 
00130 xmlNodePtr
00131 recurrence_to_dom_tree(const gchar *tag, const Recurrence *r)
00132 {
00133     xmlNodePtr n;
00134     PeriodType pt;
00135     GDate d;
00136     WeekendAdjust wadj;
00137 
00138     n = xmlNewNode(NULL, BAD_CAST tag);
00139     xmlSetProp(n, BAD_CAST "version", BAD_CAST recurrence_version_string );
00140     xmlAddChild(n, guint_to_dom_tree(recurrence_mult,
00141                                      recurrenceGetMultiplier(r)));
00142     pt = recurrenceGetPeriodType(r);
00143     xmlAddChild(n, text_to_dom_tree(recurrence_period_type,
00144                                     recurrencePeriodTypeToString(pt)));
00145     d = recurrenceGetDate(r);
00146     xmlAddChild(n, gdate_to_dom_tree(recurrence_start, &d));
00147     wadj = recurrenceGetWeekendAdjust(r);
00148     if (wadj != WEEKEND_ADJ_NONE)
00149     {
00150         /* In r17725 and r17751, I introduced this extra XML child
00151         element, but this means a gnucash-2.2.x cannot read the SX
00152         recurrence of a >=2.3.x file anymore, which is bad. In order
00153         to improve this broken backward compatibility for most of the
00154         cases, we don't write out this XML element as long as it is
00155         only "none". */
00156         xmlAddChild(n, text_to_dom_tree(recurrence_weekend_adj,
00157                                         recurrenceWeekendAdjustToString(wadj)));
00158     }
00159     return n;
00160 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines