1 /********************************************************************\
2  * io-gncxml-v2.h -- api for gnucash xml i/o                        *
3  *                                                                  *
4  * Copyright (c) 2001 Gnumatic Incorporated                         *
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 /**
26  * @file io-gncxml-v2.h
27  * @brief api for GnuCash version 2 XML-based file format
28  * @author Initial code by James LewisMoss, 2001
29  */
30 
31 #ifndef IO_GNCXML_V2_H
32 #define IO_GNCXML_V2_H
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #endif
37 #include <glib.h>
38 
39 #include "gnc-engine.h"
40 #ifdef __cplusplus
41 }
42 #include "gnc-backend-xml.h"
43 #include "sixtp.h"
44 #include <vector>
45 
46 class GncXmlBackend;
47 
48 /**
49  * Struct used to pass in a new data type for XML storage.  This contains
50  * the set of callbacks to read and write XML for new data objects..  New
51  * types should register an instance of this object with the engine.
52  *
53  * The create_parser() method will create a new sixtp parser for this
54  *   data type.
55  *
56  * The add_item() method takes a local state and a new object of this type
57  *   and the method implementation should do whatever is necessary to cleanup
58  *   the object and (maybe) add it into the book stored in the local-state.
59  *
60  * The get_count() method returns the number of items of this type.
61  *
62  * The write() method writes out all the objects of this particular type
63  *   in the book and stores the XML in the FILE.
64  *
65  * The scrub() method will take a completed, parsed QofBook* and post process
66  *   the data, allowing you to 'scrub' the data.
67  *
68  * The ns() method will output XML namespace information for the selected
69  *   plug-in object.
70  */
71 #define GNC_FILE_BACKEND    "gnc:file:2"
72 #define GNC_FILE_BACKEND_VERS   2
73 
74 /**
75  * Struct used to pass the account group/accounts and trasnactions in
76  * the <gnc:template-transactions> section between the parser in
77  * gnc-schedxactions-xml-v2.c and the add-to-book callback in
78  * io-gncxml-v2.c.
79  **/
80 typedef struct
81 {
82     AccountList* accts;
83     TransList*   transactions;
84     QofBook* book;
85 } gnc_template_xaction_data;
86 
87 /** read in an account group from a file */
88 gboolean qof_session_load_from_xml_file_v2 (GncXmlBackend*, QofBook*,
89                                             QofBookFileType);
90 
91 /* write all book info to a file */
92 gboolean gnc_book_write_to_xml_filehandle_v2 (QofBook* book, FILE* fh);
93 gboolean gnc_book_write_to_xml_file_v2 (QofBook* book, const char* filename,
94                                         gboolean compress);
95 
96 /** write just the commodities and accounts to a file */
97 gboolean gnc_book_write_accounts_to_xml_filehandle_v2 (QofBackend* be,
98                                                        QofBook* book, FILE* fh);
99 gboolean gnc_book_write_accounts_to_xml_file_v2 (QofBackend* be, QofBook* book,
100                                                  const char* filename);
101 
102 /** The is_gncxml_file() routine checks to see if the first few
103  * chars of the file look like gnc-xml data.
104  */
105 QofBookFileType gnc_is_xml_data_file_v2 (const gchar* name,
106                                          gboolean* with_encoding);
107 
108 /** Write a name-space declaration for the provided namespace data type
109  * within the GNC XML namespace at http://www.gnucash.org/XML.
110  */
111 gboolean gnc_xml2_write_namespace_decl (FILE* out, const char* name_space);
112 
113 extern "C"
114 {
115 #endif /* __cplusplus. The next two functions are used (only) by
116         * src/gnome-utils/assistant-xml-encoding.c and so need C linkage;
117         * they're also the only part of this file that the C compiler needs to
118         * see.
119         */
120 
121 typedef struct
122 {
123     GQuark encoding;
124     gchar* utf8_string;
125 } conv_type;
126 
127 /** Read a file as plain byte stream to find words that are not completely ASCII.
128  * On error, @a unique, @a ambiguous and @a impossible will be filled up to
129  * that point, -1 will be returned.
130  *
131  * @param filename Name of the file to read.
132  *
133  * @param encodings List of encodings to check words for, each begin one a GQuark
134  * in a pointer.
135  *
136  * @param unique Location used for a hash table for unique solutions, if not
137  * NULL. The byte sequence is the key, successful_conversion the value.
138  *
139  * @param ambiguous Location used for a hash table for ambiguous byte sequences,
140  * if not NULL. The byte sequences is the key, a list of successful_conversions
141  * the value.
142  *
143  * @param impossible Location used for a list for undecodable byte sequences,
144  * if not NULL.
145  *
146  * @return Size of impossible, -1 on error.
147  */
148 gint gnc_xml2_find_ambiguous (
149     const gchar* filename, GList* encodings, GHashTable** unique,
150     GHashTable** ambiguous, GList** impossible);
151 
152 /** Parse a file in push mode, but replace byte sequences in the file given a
153  * hash table of substitutions
154  *
155  * @param subst hash table with keys and values of type gchar*
156  */
157 gboolean gnc_xml2_parse_with_subst (
158     GncXmlBackend* xml_be, QofBook* book, GHashTable* subst);
159 #ifdef __cplusplus
160 }
161 typedef struct
162 {
163     int         version;        /* backend version number */
164     const char *        type_name;      /* The XML tag for this type */
165 
166     sixtp *     (*create_parser) (void);
167     gboolean    (*add_item)(sixtp_gdv2 *, gpointer obj);
168     int       (*get_count) (QofBook *);
169     gboolean    (*write) (FILE*, QofBook*);
170     void                (*scrub) (QofBook *);
171     gboolean    (*ns) (FILE*);
172 } GncXmlDataType_t;
173 
174 void gnc_xml_register_backend(GncXmlDataType_t&);
175 #endif /* __cplusplus */
176 #endif /* __IO_GNCXML_V2_H__ */
177