GnuCash  4.12-527-g05ffd3d4eb
gnc-engine.c
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.h"
31 #include "SX-book-p.h"
32 #include "gnc-budget.h"
33 #include "TransactionP.h"
34 #include "gnc-commodity.h"
35 #include "gnc-pricedb-p.h"
36 
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 = NULL;
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 gnc_engine_init_part1()
58 {
59  /* initialize QOF */
60  qof_init();
61 
62  /* Now register our core types */
63  cashobjects_register();
64 }
65 
66 static void
67 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  { NULL, NULL, FALSE }
81  }, *lib;
82 
83  for (lib = libs; lib->lib ; lib++)
84  {
85  if (qof_load_backend_library(lib->subdir, lib->lib))
86  {
87  engine_is_initialized = 1;
88  }
89  else
90  {
91  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  if (lib->required)
94  {
95  g_critical("required library %s not found.\n", lib->lib);
96  }
97  }
98  }
99 }
100 
101 static void
102 gnc_engine_init_part3(int argc, char ** argv)
103 {
104  GList * cur;
105  /* call any engine hooks */
106  for (cur = engine_init_hooks; cur; cur = cur->next)
107  {
109 
110  if (hook)
111  (*hook)(argc, argv);
112  }
113 }
114 
115 void
116 gnc_engine_init(int argc, char ** argv)
117 {
118  if (1 == engine_is_initialized) return;
119 
120  gnc_engine_init_part1();
121  gnc_engine_init_part2();
122  gnc_engine_init_part3(argc, argv);
123 }
124 
125 void
126 gnc_engine_init_static(int argc, char ** argv)
127 {
128  if (1 == engine_is_initialized) return;
129 
130  gnc_engine_init_part1();
131  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
142 {
144  qof_close();
145  engine_is_initialized = 0;
146 }
147 
148 /********************************************************************
149  * gnc_engine_add_init_hook
150  * add a startup hook
151  ********************************************************************/
152 
153 void
155 {
156  engine_init_hooks = g_list_append(engine_init_hooks, (gpointer)h);
157 }
158 
159 gboolean
161 {
162  return (engine_is_initialized == 1) ? TRUE : FALSE;
163 }
164 
165 void
166 gnc_engine_add_commit_error_callback( EngineCommitErrorCallback cb, gpointer data )
167 {
168  g_error_cb = cb;
169  g_error_cb_data = data;
170 }
171 
172 void
173 gnc_engine_signal_commit_error( QofBackendError errcode )
174 {
175  if ( g_error_cb != NULL )
176  {
177  (*g_error_cb)( g_error_cb_data, errcode );
178  }
179 }
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:57
void gnc_engine_shutdown(void)
Called to shutdown the engine.
Definition: gnc-engine.c:141
GnuCash Budgets.
gboolean gnc_engine_is_initialized(void)
check the engine is fully initialized
Definition: gnc-engine.c:160
void gnc_engine_add_commit_error_callback(EngineCommitErrorCallback cb, gpointer data)
Set a callback function to be called in case an engine commit fails.
Definition: gnc-engine.c:166
void qof_log_shutdown(void)
Be nice, close the logfile if possible.
Definition: qoflog.cpp:268
void(* gnc_engine_init_hook_t)(int, char **)
Function type for init hooks in the engine.
Definition: gnc-engine.h:225
void gnc_engine_init_static(int argc, char **argv)
This is the statically linked-in version of gnc_engine_init.
Definition: gnc-engine.c:126
void gnc_engine_init(int argc, char **argv)
PROTOTYPES.
Definition: gnc-engine.c:116
void gnc_engine_add_init_hook(gnc_engine_init_hook_t h)
Pass a function pointer to gnc_engine_add_init_hook and it will be called during the evaluation of gn...
Definition: gnc-engine.c:154
All type declarations for the whole Gnucash engine.
gboolean qof_load_backend_library(const gchar *directory, const gchar *module_name)
Load a QOF-compatible backend shared library.
void qof_close(void)
Safely close down the Query Object Framework.
Definition: qofutil.cpp:269
This is the private header for the account structure.
void qof_init(void)
Initialise the Query Object Framework.
Definition: qofutil.cpp:259
Commodity handling public routines.