GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-engine.c
1 /********************************************************************
2  * gnc-engine.c -- top-level initialization for GnuCash Engine *
3  * Copyright 2000 Bill Gribble <[email protected]> *
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 [email protected] *
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  if (1 == engine_is_initialized) return;
60 
61  /* initialize QOF */
62  qof_init();
64 
65  /* Now register our core types */
66  cashobjects_register();
67 }
68 
69 static void
70 gnc_engine_init_part2()
71 {
72  gchar *pkglibdir;
73  const gchar *builddir = g_getenv ("GNC_BUILDDIR");
74  gboolean uninstalled = (g_getenv ("GNC_UNINSTALLED") != NULL
75  && builddir != NULL);
76 
77  static struct
78  {
79  const gchar* subdir;
80  const gchar* lib;
81  gboolean required;
82  } libs[] =
83  {
84 #if defined( HAVE_DBI_DBI_H )
85  { "dbi", "gncmod-backend-dbi", TRUE },
86 #endif
87  { "xml", "gncmod-backend-xml", TRUE },
88  { NULL, FALSE }
89  }, *lib;
90 
91  if (uninstalled)
92  pkglibdir = g_build_path (G_DIR_SEPARATOR_S, builddir,
93  "src", "backend", NULL);
94  else
95  pkglibdir = gnc_path_get_pkglibdir ();
96 
97  for (lib = libs; lib->lib ; lib++)
98  {
99  gchar *libdir;
100  if (uninstalled)
101  libdir = g_build_path (G_DIR_SEPARATOR_S, pkglibdir,
102  lib->subdir, ".libs", NULL);
103  else
104  libdir = pkglibdir;
105  if (qof_load_backend_library(libdir, lib->lib))
106  {
107  engine_is_initialized = 1;
108  }
109  else
110  {
111  g_warning("failed to load %s from %s\n", lib->lib, libdir);
112  /* If this is a required library, stop now! */
113  if (lib->required)
114  {
115  g_critical("required library %s not found.\n", lib->lib);
116  }
117  }
118  if (uninstalled)
119  g_free (libdir);
120  }
121  g_free (pkglibdir);
122 }
123 
124 static void
125 gnc_engine_init_part3(int argc, char ** argv)
126 {
127  GList * cur;
128  /* call any engine hooks */
129  for (cur = engine_init_hooks; cur; cur = cur->next)
130  {
132 
133  if (hook)
134  (*hook)(argc, argv);
135  }
136 }
137 
138 void
139 gnc_engine_init(int argc, char ** argv)
140 {
141  gnc_engine_init_part1();
142  gnc_engine_init_part2();
143  gnc_engine_init_part3(argc, argv);
144 }
145 
146 void
147 gnc_engine_init_static(int argc, char ** argv)
148 {
149  gnc_engine_init_part1();
150  gnc_engine_init_part3(argc, argv);
151 }
152 
153 
154 /********************************************************************
155  * gnc_engine_shutdown
156  * shutdown backend, destroy any global data, etc.
157  ********************************************************************/
158 
159 void
161 {
163  qof_close();
164  engine_is_initialized = 0;
165 }
166 
167 /********************************************************************
168  * gnc_engine_add_init_hook
169  * add a startup hook
170  ********************************************************************/
171 
172 void
174 {
175  engine_init_hooks = g_list_append(engine_init_hooks, (gpointer)h);
176 }
177 
178 gboolean
180 {
181  return (engine_is_initialized == 1) ? TRUE : FALSE;
182 }
183 
184 /* replicate old gnc-trace enum behaviour
185  *
186  * these are only here as a convenience, they could be
187  * initialised elsewhere as appropriate.
188  * */
189 void gnc_log_default(void)
190 {
191  qof_log_set_default(QOF_LOG_WARNING);
192  qof_log_set_level(GNC_MOD_ROOT, QOF_LOG_WARNING);
193  qof_log_set_level(GNC_MOD_TEST, QOF_LOG_DEBUG);
194 }
195 
196 void
197 gnc_engine_add_commit_error_callback( EngineCommitErrorCallback cb, gpointer data )
198 {
199  g_error_cb = cb;
200  g_error_cb_data = data;
201 }
202 
203 void
204 gnc_engine_signal_commit_error( QofBackendError errcode )
205 {
206  if ( g_error_cb != NULL )
207  {
208  (*g_error_cb)( g_error_cb_data, errcode );
209  }
210 }
void(* gnc_engine_init_hook_t)(int, char **)
Definition: gnc-engine.h:217
void qof_log_set_level(QofLogModule module, QofLogLevel level)
void qof_log_set_default(QofLogLevel log_level)
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:59
void gnc_engine_shutdown(void)
Definition: gnc-engine.c:160
GnuCash Budgets.
void qof_set_alt_dirty_mode(gboolean enabled)
gboolean gnc_engine_is_initialized(void)
Definition: gnc-engine.c:179
void gnc_engine_add_commit_error_callback(EngineCommitErrorCallback cb, gpointer data)
Definition: gnc-engine.c:197
void qof_log_shutdown(void)
void gnc_engine_init_static(int argc, char **argv)
Definition: gnc-engine.c:147
void gnc_log_default(void)
Definition: gnc-engine.c:189
void gnc_engine_init(int argc, char **argv)
Definition: gnc-engine.c:139
void gnc_engine_add_init_hook(gnc_engine_init_hook_t h)
Definition: gnc-engine.c:173
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.
void qof_init(void)
Initialise the Query Object Framework.
Commodity handling public routines.