GnuCash 2.4.99
gnc-budget-xml-v2.c
00001 /*
00002  * gnc-budget-xml-v2.c -- budget xml i/o implementation
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 <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "gnc-xml-helper.h"
00032 #include "sixtp.h"
00033 #include "sixtp-utils.h"
00034 #include "sixtp-parsers.h"
00035 #include "sixtp-utils.h"
00036 #include "sixtp-dom-parsers.h"
00037 #include "sixtp-dom-generators.h"
00038 
00039 #include "gnc-xml.h"
00040 #include "io-gncxml-gen.h"
00041 #include "io-gncxml-v2.h"
00042 
00043 static QofLogModule log_module = GNC_MOD_IO;
00044 
00045 const gchar *budget_version_string = "2.0.0";
00046 
00047 /* ids */
00048 #define gnc_budget_string       "gnc:budget"
00049 #define bgt_id_string           "bgt:id"
00050 #define bgt_name_string         "bgt:name"
00051 #define bgt_description_string  "bgt:description"
00052 #define bgt_num_periods_string  "bgt:num-periods"
00053 #define bgt_recurrence_string   "bgt:recurrence"
00054 #define bgt_slots_string        "bgt:slots"
00055 
00056 xmlNodePtr
00057 gnc_budget_dom_tree_create(GncBudget *bgt)
00058 {
00059     xmlNodePtr ret;
00060     KvpFrame *kf;
00061 
00062     ENTER ("(budget=%p)", bgt);
00063 
00064     ret = xmlNewNode(NULL, BAD_CAST gnc_budget_string);
00065     xmlSetProp(ret, BAD_CAST "version", BAD_CAST budget_version_string);
00066 
00067     /* field: GncGUID */
00068     xmlAddChild(ret, guid_to_dom_tree(bgt_id_string,
00069                                       gnc_budget_get_guid(bgt)));
00070     /* field: char* name */
00071     xmlAddChild(ret, text_to_dom_tree(bgt_name_string,
00072                                       gnc_budget_get_name(bgt)));
00073     /* field: char* description */
00074     xmlAddChild(ret, text_to_dom_tree(bgt_description_string,
00075                                       gnc_budget_get_description(bgt)));
00076     /* field: guint num_periods */
00077     xmlAddChild(ret, guint_to_dom_tree(bgt_num_periods_string,
00078                                        gnc_budget_get_num_periods(bgt)));
00079     /* field: Recurrence*  */
00080     xmlAddChild(ret, recurrence_to_dom_tree(bgt_recurrence_string,
00081                                             gnc_budget_get_recurrence(bgt)));
00082     /* slots */
00083     kf = qof_instance_get_slots(QOF_INSTANCE(bgt));
00084     if (kf)
00085     {
00086         xmlNodePtr kvpnode = kvp_frame_to_dom_tree(bgt_slots_string, kf);
00087         if (kvpnode)
00088             xmlAddChild(ret, kvpnode);
00089     }
00090 
00091     LEAVE (" ");
00092     return ret;
00093 }
00094 
00095 /***********************************************************************/
00096 static inline gboolean
00097 set_string(xmlNodePtr node, GncBudget* bgt,
00098            void (*func)(GncBudget *bgt, const gchar *txt))
00099 {
00100     gchar* txt = dom_tree_to_text(node);
00101     g_return_val_if_fail(txt, FALSE);
00102 
00103     func(bgt, txt);
00104     g_free(txt);
00105     return TRUE;
00106 }
00107 
00108 static gboolean
00109 budget_id_handler (xmlNodePtr node, gpointer bgt)
00110 {
00111     GncGUID *guid;
00112 
00113     guid = dom_tree_to_guid(node);
00114     g_return_val_if_fail(guid, FALSE);
00115     qof_instance_set_guid(QOF_INSTANCE(bgt), guid);
00116     g_free(guid);
00117     return TRUE;
00118 }
00119 
00120 static gboolean
00121 budget_name_handler (xmlNodePtr node, gpointer bgt)
00122 {
00123     return set_string(node, GNC_BUDGET(bgt), gnc_budget_set_name);
00124 }
00125 
00126 static gboolean
00127 budget_description_handler (xmlNodePtr node, gpointer bgt)
00128 {
00129     return set_string(node, GNC_BUDGET(bgt), gnc_budget_set_description);
00130 }
00131 
00132 static gboolean
00133 budget_num_periods_handler (xmlNodePtr node, gpointer bgt)
00134 {
00135     guint num_periods;
00136 
00137     if (dom_tree_to_guint(node, &num_periods))
00138     {
00139         gnc_budget_set_num_periods(GNC_BUDGET(bgt), num_periods);
00140         return TRUE;
00141     }
00142     else
00143         return FALSE;
00144 }
00145 
00146 static gboolean
00147 budget_recurrence_handler (xmlNodePtr node, gpointer bgt)
00148 {
00149     Recurrence *r;
00150 
00151     if ((r = dom_tree_to_recurrence(node)) == NULL)
00152         return FALSE;
00153 
00154     gnc_budget_set_recurrence(GNC_BUDGET(bgt), r);
00155     g_free(r);
00156     return TRUE;
00157 }
00158 
00159 static gboolean
00160 budget_slots_handler (xmlNodePtr node, gpointer bgt)
00161 {
00162     return dom_tree_to_kvp_frame_given(
00163                node, qof_instance_get_slots(QOF_INSTANCE(bgt)));
00164 }
00165 
00166 static struct dom_tree_handler budget_handlers[] =
00167 {
00168     { bgt_id_string, budget_id_handler, 1, 0 },
00169     { bgt_name_string, budget_name_handler, 0, 0 },
00170     { bgt_description_string, budget_description_handler, 0, 0 },
00171     { bgt_num_periods_string, budget_num_periods_handler, 1, 0 },
00172     { bgt_recurrence_string, budget_recurrence_handler, 1, 0 },
00173     { bgt_slots_string, budget_slots_handler, 0, 0},
00174     { NULL, 0, 0, 0 }
00175 };
00176 
00177 static gboolean
00178 gnc_budget_end_handler(gpointer data_for_children,
00179                        GSList* data_from_children, GSList* sibling_data,
00180                        gpointer parent_data, gpointer global_data,
00181                        gpointer *result, const gchar *tag)
00182 {
00183     GncBudget *bgt;
00184     xmlNodePtr tree = (xmlNodePtr)data_for_children;
00185     gxpf_data *gdata = (gxpf_data*)global_data;
00186     QofBook *book = gdata->bookdata;
00187 
00188     if (parent_data)
00189     {
00190         return TRUE;
00191     }
00192 
00193     /* OK.  For some messed up reason this is getting called again with a
00194        NULL tag.  So we ignore those cases */
00195     if (!tag)
00196     {
00197         return TRUE;
00198     }
00199 
00200     g_return_val_if_fail(tree, FALSE);
00201 
00202     bgt = dom_tree_to_budget(tree, book);
00203     xmlFreeNode(tree);
00204     if (bgt != NULL)
00205     {
00206         /* ends up calling book_callback */
00207         gdata->cb(tag, gdata->parsedata, bgt);
00208     }
00209 
00210     return bgt != NULL;
00211 }
00212 
00213 
00214 GncBudget*
00215 dom_tree_to_budget (xmlNodePtr node, QofBook *book)
00216 {
00217     GncBudget *bgt;
00218 
00219     bgt = gnc_budget_new(book);
00220     if (!dom_tree_generic_parse (node, budget_handlers, bgt))
00221     {
00222         PERR ("failed to parse budget tree");
00223         gnc_budget_destroy(bgt);
00224         bgt = NULL;
00225     }
00226     return bgt;
00227 }
00228 
00229 sixtp*
00230 gnc_budget_sixtp_parser_create(void)
00231 {
00232     return sixtp_dom_parser_new(gnc_budget_end_handler, NULL, NULL);
00233 }
00234 /* ======================  END OF FILE ===================*/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines