25#include <glib/gi18n.h>
26#include <glib/gstdio.h>
57#include "io-example-account.h"
58#include "io-gncxml-gen.h"
60#include "sixtp-dom-generators.h"
61#include "sixtp-dom-parsers.h"
62#include "sixtp-parsers.h"
65static QofLogModule log_module = GNC_MOD_IO;
67#define GNC_ACCOUNT_STRING "gnc-account-example"
68#define GNC_ACCOUNT_SHORT "gnc-act:short-description"
69#define GNC_ACCOUNT_LONG "gnc-act:long-description"
70#define GNC_ACCOUNT_TITLE "gnc-act:title"
71#define GNC_ACCOUNT_EXCLUDEP "gnc-act:exclude-from-select-all"
72#define GNC_ACCOUNT_SELECTED "gnc-act:start-selected"
75gnc_destroy_example_account (GncExampleAccount* gea)
77 if (gea->title != NULL)
82 if (gea->filename != NULL)
84 g_free (gea->filename);
87 if (gea->root != NULL)
93 if (gea->short_description != NULL)
95 g_free (gea->short_description);
96 gea->short_description = NULL;
98 if (gea->long_description != NULL)
100 g_free (gea->long_description);
101 gea->long_description = NULL;
103 if (gea->book != NULL)
112clear_up_account_commodity (
113 gnc_commodity_table* tbl,
Account* act,
114 gnc_commodity * (*getter) (
const Account* account),
115 void (*setter) (
Account* account, gnc_commodity* comm))
118 gnc_commodity* com = getter (act);
125 g_return_if_fail (tbl != NULL);
127 gcom = gnc_commodity_table_lookup (tbl,
137 PWARN (
"unable to find global commodity for %s adding new",
149add_account_local (GncExampleAccount* gea,
Account* act)
151 gnc_commodity_table*
table;
155 clear_up_account_commodity (
table, act,
169 g_warning (
"The example account file should declared a ROOT "
170 "account before declaring any other accounts.");
171 gea->root = gnc_book_get_root_account (gea->book);
178generic_callback (
const char* tag, gpointer globaldata, gpointer data)
180 GncExampleAccount* gea = (GncExampleAccount*)globaldata;
182 if (g_strcmp0 (tag,
"gnc:account") == 0)
184 add_account_local (gea, (
Account*)data);
190squash_extra_whitespace (
char* text)
193 int length = strlen (text);
195 for (spot = 1; spot < length; spot++)
197 if (isspace (text[spot]) && isspace (text[spot - 1]))
200 memmove (text + spot, text + spot + 1, length - spot);
212grab_clean_string (xmlNodePtr tree)
214 auto txt = dom_tree_to_text (tree);
215 auto str = g_strdup (txt ? txt->c_str() :
"");
216 return squash_extra_whitespace (g_strstrip (str));
220gnc_short_descrip_end_handler (gpointer data_for_children,
221 GSList* data_from_children, GSList* sibling_data,
222 gpointer parent_data, gpointer global_data,
223 gpointer* result,
const gchar* tag)
225 GncExampleAccount* gea =
226 (GncExampleAccount*) ((gxpf_data*)global_data)->parsedata;
228 gea->short_description = grab_clean_string ((xmlNodePtr)data_for_children);
234gnc_short_descrip_sixtp_parser_create (
void)
236 return sixtp_dom_parser_new (gnc_short_descrip_end_handler, NULL, NULL);
240gnc_long_descrip_end_handler (gpointer data_for_children,
241 GSList* data_from_children, GSList* sibling_data,
242 gpointer parent_data, gpointer global_data,
243 gpointer* result,
const gchar* tag)
245 GncExampleAccount* gea =
246 (GncExampleAccount*) ((gxpf_data*)global_data)->parsedata;
248 gea->long_description = grab_clean_string ((xmlNodePtr)data_for_children);
254gnc_long_descrip_sixtp_parser_create (
void)
256 return sixtp_dom_parser_new (gnc_long_descrip_end_handler, NULL, NULL);
260gnc_excludep_end_handler (gpointer data_for_children,
261 GSList* data_from_children, GSList* sibling_data,
262 gpointer parent_data, gpointer global_data,
263 gpointer* result,
const gchar* tag)
265 GncExampleAccount* gea =
266 (GncExampleAccount*) ((gxpf_data*)global_data)->parsedata;
269 dom_tree_to_integer ((xmlNodePtr)data_for_children, &val);
270 gea->exclude_from_select_all = (val ? TRUE : FALSE);
276gnc_excludep_sixtp_parser_create (
void)
278 return sixtp_dom_parser_new (gnc_excludep_end_handler, NULL, NULL);
282gnc_selected_end_handler (gpointer data_for_children,
283 GSList* data_from_children, GSList* sibling_data,
284 gpointer parent_data, gpointer global_data,
285 gpointer* result,
const gchar* tag)
287 GncExampleAccount* gea =
288 (GncExampleAccount*) ((gxpf_data*)global_data)->parsedata;
291 dom_tree_to_integer ((xmlNodePtr)data_for_children, &val);
292 gea->start_selected = (val ? TRUE : FALSE);
298gnc_selected_sixtp_parser_create (
void)
300 return sixtp_dom_parser_new (gnc_selected_end_handler, NULL, NULL);
304gnc_title_end_handler (gpointer data_for_children,
305 GSList* data_from_children, GSList* sibling_data,
306 gpointer parent_data, gpointer global_data,
307 gpointer* result,
const gchar* tag)
309 GncExampleAccount* gea =
310 (GncExampleAccount*) ((gxpf_data*)global_data)->parsedata;
312 gea->title = grab_clean_string ((xmlNodePtr)data_for_children);
318gnc_titse_sixtp_parser_create (
void)
320 return sixtp_dom_parser_new (gnc_title_end_handler, NULL, NULL);
325gnc_read_example_account (
const gchar* filename)
327 GncExampleAccount* gea;
331 g_return_val_if_fail (filename != NULL, NULL);
333 gea = g_new0 (GncExampleAccount, 1);
336 gea->filename = g_strdup (filename);
338 top_parser = sixtp_new ();
339 main_parser = sixtp_new ();
341 if (!sixtp_add_some_sub_parsers (
343 GNC_ACCOUNT_STRING, main_parser,
346 gnc_destroy_example_account (gea);
350 if (!sixtp_add_some_sub_parsers (
352 GNC_ACCOUNT_TITLE, gnc_titse_sixtp_parser_create (),
353 GNC_ACCOUNT_SHORT, gnc_short_descrip_sixtp_parser_create (),
354 GNC_ACCOUNT_LONG, gnc_long_descrip_sixtp_parser_create (),
355 GNC_ACCOUNT_EXCLUDEP, gnc_excludep_sixtp_parser_create (),
356 GNC_ACCOUNT_SELECTED, gnc_selected_sixtp_parser_create (),
357 "gnc:account", gnc_account_sixtp_parser_create (),
360 gnc_destroy_example_account (gea);
364 if (!gnc_xml_parse_file (top_parser, filename,
365 generic_callback, gea, gea->book))
367 sixtp_destroy (top_parser);
369 gnc_destroy_example_account (gea);
377write_string_part (FILE* out,
const char* tag,
const char* data)
381 node = text_to_dom_tree (tag, data);
383 xmlElemDump (out, NULL, node);
390write_bool_part (FILE* out,
const char* tag, gboolean data)
394 node = int_to_dom_tree (tag, data);
396 xmlElemDump (out, NULL, node);
403gnc_write_example_account (GncExampleAccount* gea,
const gchar* filename)
408 out = g_fopen (filename,
"w");
414 fprintf (out,
"<?xml version=\"1.0\"?>\n");
415 fprintf (out,
"<" GNC_ACCOUNT_STRING
">\n");
417 write_string_part (out, GNC_ACCOUNT_TITLE, gea->title);
419 write_string_part (out, GNC_ACCOUNT_SHORT, gea->short_description);
421 write_string_part (out, GNC_ACCOUNT_LONG, gea->long_description);
423 write_bool_part (out, GNC_ACCOUNT_EXCLUDEP, gea->exclude_from_select_all);
425 write_account_tree (out, gea->root, &data);
427 fprintf (out,
"</" GNC_ACCOUNT_STRING
">\n\n");
438slist_destroy_example_account (gpointer data, gpointer user_data)
442 gnc_destroy_example_account ((GncExampleAccount*)data);
446 PWARN (
"GncExampleAccount pointer in slist was NULL");
451gnc_free_example_account_list (GSList* list)
453 g_slist_foreach (list, slist_destroy_example_account, NULL);
458gnc_load_example_account_list (
const char* dirname)
462 const gchar* direntry;
464 dir = g_dir_open (dirname, 0, NULL);
473 for (direntry = g_dir_read_name (dir); direntry != NULL;
474 direntry = g_dir_read_name (dir))
477 GncExampleAccount* gea;
478 if (!g_str_has_suffix (direntry,
"xea"))
481 filename = g_build_filename (dirname, direntry, (gchar*) NULL);
483 if (!g_file_test (filename, G_FILE_TEST_IS_DIR))
485 gea = gnc_read_example_account (filename);
490 PWARN (
"Skipping unreadable account template %s", filename);
492 ret = g_slist_append (ret, gea);
convert single-entry accounts to clean double-entry
API for the transaction logger.
All type declarations for the whole Gnucash engine.
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
void gnc_account_append_child(Account *new_parent, Account *child)
This function will remove from the child account any pre-existing parent relationship,...
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account's account type.
void xaccAccountDestroy(Account *acc)
The xaccAccountDestroy() routine can be used to get rid of an account.
Account * gnc_account_get_parent(const Account *acc)
This routine returns a pointer to the parent of the specified account.
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
void xaccAccountSetCommodity(Account *acc, gnc_commodity *com)
Set the account's commodity.
@ ACCT_TYPE_ROOT
The hidden root account of an account tree.
void qof_book_destroy(QofBook *book)
End any editing sessions associated with book, and free all memory associated with it.
QofBook * qof_book_new(void)
Allocate, initialise and return a new QofBook.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
gnc_commodity * gnc_commodity_table_insert(gnc_commodity_table *table, gnc_commodity *comm)
Add a new commodity to the commodity table.
void gnc_commodity_destroy(gnc_commodity *cm)
Destroy a commodity.
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
const char * gnc_commodity_get_unique_name(const gnc_commodity *cm)
Retrieve the 'unique' name for the specified commodity.
#define PWARN(format, args...)
Log a warning.
void xaccAccountScrubCommodity(Account *account)
The xaccAccountScrubCommodity method fixed accounts without a commodity by using the old account curr...
void xaccLogEnable(void)
document me