Line data Source code
1 : /********************************************************************
2 : * gnc-engine.c -- top-level initialization for GnuCash Engine *
3 : * Copyright 2000 Bill Gribble <grib@billgribble.com> *
4 : * *
5 : * This program is free software; you can redistribute it and/or *
6 : * modify it under the terms of the GNU General Public License as *
7 : * published by the Free Software Foundation; either version 2 of *
8 : * the License, or (at your option) any later version. *
9 : * *
10 : * This program is distributed in the hope that it will be useful, *
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 : * GNU General Public License for more details. *
14 : * *
15 : * You should have received a copy of the GNU General Public License*
16 : * along with this program; if not, contact: *
17 : * *
18 : * Free Software Foundation Voice: +1-617-542-5942 *
19 : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 : * Boston, MA 02110-1301, USA gnu@gnu.org *
21 : * *
22 : ********************************************************************/
23 :
24 : #include <config.h>
25 :
26 : #include <glib.h>
27 : #include "gnc-engine.h"
28 : #include "qof.h"
29 : #include "cashobjects.h"
30 : #include "AccountP.hpp"
31 : #include "SX-book-p.h"
32 : #include "gnc-budget.h"
33 : #include "TransactionP.hpp"
34 : #include "gnc-commodity.h"
35 : #include "gnc-pricedb-p.h"
36 :
37 : /** gnc file backend library name */
38 : #define GNC_LIB_NAME "gncmod-backend-xml"
39 :
40 : /* gnc-backend-file location */
41 : #include "gnc-path.h"
42 :
43 : static GList * engine_init_hooks = nullptr;
44 : static int engine_is_initialized = 0;
45 :
46 : EngineCommitErrorCallback g_error_cb;
47 : gpointer g_error_cb_data;
48 :
49 : // static QofLogModule log_module = GNC_MOD_ENGINE;
50 :
51 : /********************************************************************
52 : * gnc_engine_init
53 : * initialize backend, load any necessary databases, etc.
54 : ********************************************************************/
55 :
56 : static void
57 59 : gnc_engine_init_part1()
58 : {
59 : /* initialize QOF */
60 59 : qof_init();
61 :
62 : /* Now register our core types */
63 59 : cashobjects_register();
64 59 : }
65 :
66 : static void
67 59 : gnc_engine_init_part2()
68 : {
69 : static struct
70 : {
71 : const gchar* subdir;
72 : const gchar* lib;
73 : gboolean required;
74 : } libs[] =
75 : {
76 : #if defined( HAVE_DBI_DBI_H )
77 : { "", "gncmod-backend-dbi", TRUE },
78 : #endif
79 : { "", "gncmod-backend-xml", TRUE },
80 : { nullptr, nullptr, FALSE }
81 : }, *lib;
82 :
83 177 : for (lib = libs; lib->lib ; lib++)
84 : {
85 118 : if (qof_load_backend_library(lib->subdir, lib->lib))
86 : {
87 20 : engine_is_initialized = 1;
88 : }
89 : else
90 : {
91 98 : g_warning("failed to load %s from relative path %s\n", lib->lib, lib->subdir);
92 : /* If this is a required library, stop now! */
93 98 : if (lib->required)
94 : {
95 98 : g_critical("required library %s not found.\n", lib->lib);
96 : }
97 : }
98 : }
99 59 : }
100 :
101 : static void
102 59 : gnc_engine_init_part3(int argc, char ** argv)
103 : {
104 : GList * cur;
105 : /* call any engine hooks */
106 59 : for (cur = engine_init_hooks; cur; cur = cur->next)
107 : {
108 0 : gnc_engine_init_hook_t hook = (gnc_engine_init_hook_t)cur->data;
109 :
110 0 : if (hook)
111 0 : (*hook)(argc, argv);
112 : }
113 59 : }
114 :
115 : void
116 60 : gnc_engine_init(int argc, char ** argv)
117 : {
118 60 : if (1 == engine_is_initialized) return;
119 :
120 59 : gnc_engine_init_part1();
121 59 : gnc_engine_init_part2();
122 59 : gnc_engine_init_part3(argc, argv);
123 : }
124 :
125 : void
126 0 : gnc_engine_init_static(int argc, char ** argv)
127 : {
128 0 : if (1 == engine_is_initialized) return;
129 :
130 0 : gnc_engine_init_part1();
131 0 : gnc_engine_init_part3(argc, argv);
132 : }
133 :
134 :
135 : /********************************************************************
136 : * gnc_engine_shutdown
137 : * shutdown backend, destroy any global data, etc.
138 : ********************************************************************/
139 :
140 : void
141 0 : gnc_engine_shutdown (void)
142 : {
143 0 : qof_log_shutdown();
144 0 : qof_close();
145 0 : engine_is_initialized = 0;
146 0 : }
147 :
148 : /********************************************************************
149 : * gnc_engine_add_init_hook
150 : * add a startup hook
151 : ********************************************************************/
152 :
153 : void
154 0 : gnc_engine_add_init_hook(gnc_engine_init_hook_t h)
155 : {
156 0 : engine_init_hooks = g_list_append(engine_init_hooks, (gpointer)h);
157 0 : }
158 :
159 : gboolean
160 0 : gnc_engine_is_initialized (void)
161 : {
162 0 : return (engine_is_initialized == 1) ? TRUE : FALSE;
163 : }
164 :
165 : void
166 2 : gnc_engine_add_commit_error_callback( EngineCommitErrorCallback cb, gpointer data )
167 : {
168 2 : g_error_cb = cb;
169 2 : g_error_cb_data = data;
170 2 : }
171 :
172 : void
173 1 : gnc_engine_signal_commit_error( QofBackendError errcode )
174 : {
175 1 : if ( g_error_cb != nullptr )
176 : {
177 1 : (*g_error_cb)( g_error_cb_data, errcode );
178 : }
179 1 : }
|