GnuCash 2.4.99
gnc-engine.c
00001 /********************************************************************
00002  * gnc-engine.c  -- top-level initialization for GnuCash Engine     *
00003  * Copyright 2000 Bill Gribble <grib@billgribble.com>               *
00004  *                                                                  *
00005  * This program is free software; you can redistribute it and/or    *
00006  * modify it under the terms of the GNU General Public License as   *
00007  * published by the Free Software Foundation; either version 2 of   *
00008  * the License, or (at your option) any later version.              *
00009  *                                                                  *
00010  * This program is distributed in the hope that it will be useful,  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00013  * GNU General Public License for more details.                     *
00014  *                                                                  *
00015  * You should have received a copy of the GNU General Public License*
00016  * along with this program; if not, contact:                        *
00017  *                                                                  *
00018  * Free Software Foundation           Voice:  +1-617-542-5942       *
00019  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00020  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00021  *                                                                  *
00022  ********************************************************************/
00023 
00024 #include "config.h"
00025 
00026 #include <glib.h>
00027 #include "gnc-engine.h"
00028 #include "qof.h"
00029 #include "cashobjects.h"
00030 #include "AccountP.h"
00031 #include "SX-book-p.h"
00032 #include "gnc-budget.h"
00033 #include "TransactionP.h"
00034 #include "gnc-commodity.h"
00035 #include "gnc-pricedb-p.h"
00036 
00038 #define GNC_LIB_NAME "gncmod-backend-xml"
00039 
00040 /* gnc-backend-file location */
00041 #include "gnc-path.h"
00042 
00043 static GList * engine_init_hooks = NULL;
00044 static int engine_is_initialized = 0;
00045 
00046 EngineCommitErrorCallback g_error_cb;
00047 gpointer g_error_cb_data;
00048 
00049 // static QofLogModule log_module = GNC_MOD_ENGINE;
00050 
00051 /********************************************************************
00052  * gnc_engine_init
00053  * initialize backend, load any necessary databases, etc.
00054  ********************************************************************/
00055 
00056 static void
00057 gnc_engine_init_part1()
00058 {
00059     if (1 == engine_is_initialized) return;
00060 
00061     /* initialize QOF */
00062     qof_init();
00063     qof_set_alt_dirty_mode(TRUE);
00064 
00065     /* Now register our core types */
00066     cashobjects_register();
00067 }
00068 
00069 static void
00070 gnc_engine_init_part2()
00071 {
00072     gchar *pkglibdir;
00073     const gchar *builddir = g_getenv ("GNC_BUILDDIR");
00074     gboolean uninstalled = (g_getenv ("GNC_UNINSTALLED") != NULL
00075                             && builddir != NULL);
00076 
00077     static struct
00078     {
00079         const gchar* subdir;
00080         const gchar* lib;
00081         gboolean required;
00082     } libs[] =
00083     {
00084 #if defined( HAVE_DBI_DBI_H )
00085         { "dbi", "gncmod-backend-dbi", TRUE },
00086 #endif
00087         { "xml", "gncmod-backend-xml", TRUE },
00088         { NULL, FALSE }
00089     }, *lib;
00090 
00091     if (uninstalled)
00092         pkglibdir = g_build_path (G_DIR_SEPARATOR_S, builddir,
00093                                   "src", "backend", NULL);
00094     else
00095         pkglibdir = gnc_path_get_pkglibdir ();
00096 
00097     for (lib = libs; lib->lib ; lib++)
00098     {
00099         gchar *libdir;
00100         if (uninstalled)
00101             libdir = g_build_path (G_DIR_SEPARATOR_S, pkglibdir,
00102                                    lib->subdir, ".libs", NULL);
00103         else
00104             libdir = pkglibdir;
00105         if (qof_load_backend_library(libdir, lib->lib))
00106         {
00107             engine_is_initialized = 1;
00108         }
00109         else
00110         {
00111             g_warning("failed to load %s from %s\n", lib->lib, libdir);
00112             /* If this is a required library, stop now! */
00113             if (lib->required)
00114             {
00115                 g_critical("required library %s not found.\n", lib->lib);
00116             }
00117         }
00118         if (uninstalled)
00119             g_free (libdir);
00120     }
00121     g_free (pkglibdir);
00122 }
00123 
00124 static void
00125 gnc_engine_init_part3(int argc, char ** argv)
00126 {
00127     GList * cur;
00128     /* call any engine hooks */
00129     for (cur = engine_init_hooks; cur; cur = cur->next)
00130     {
00131         gnc_engine_init_hook_t hook = (gnc_engine_init_hook_t)cur->data;
00132 
00133         if (hook)
00134             (*hook)(argc, argv);
00135     }
00136 }
00137 
00138 void
00139 gnc_engine_init(int argc, char ** argv)
00140 {
00141     gnc_engine_init_part1();
00142     gnc_engine_init_part2();
00143     gnc_engine_init_part3(argc, argv);
00144 }
00145 
00146 void
00147 gnc_engine_init_static(int argc, char ** argv)
00148 {
00149     gnc_engine_init_part1();
00150     gnc_engine_init_part3(argc, argv);
00151 }
00152 
00153 
00154 /********************************************************************
00155  * gnc_engine_shutdown
00156  * shutdown backend, destroy any global data, etc.
00157  ********************************************************************/
00158 
00159 void
00160 gnc_engine_shutdown (void)
00161 {
00162     qof_log_shutdown();
00163     qof_close();
00164     engine_is_initialized = 0;
00165 }
00166 
00167 /********************************************************************
00168  * gnc_engine_add_init_hook
00169  * add a startup hook
00170  ********************************************************************/
00171 
00172 void
00173 gnc_engine_add_init_hook(gnc_engine_init_hook_t h)
00174 {
00175     engine_init_hooks = g_list_append(engine_init_hooks, (gpointer)h);
00176 }
00177 
00178 gboolean
00179 gnc_engine_is_initialized (void)
00180 {
00181     return (engine_is_initialized == 1) ? TRUE : FALSE;
00182 }
00183 
00184 /* replicate old gnc-trace enum behaviour
00185  *
00186  * these are only here as a convenience, they could be
00187  * initialised elsewhere as appropriate.
00188  * */
00189 void gnc_log_default(void)
00190 {
00191     qof_log_set_default(QOF_LOG_WARNING);
00192     qof_log_set_level(GNC_MOD_ROOT, QOF_LOG_WARNING);
00193     qof_log_set_level(GNC_MOD_TEST, QOF_LOG_DEBUG);
00194 }
00195 
00196 void
00197 gnc_engine_add_commit_error_callback( EngineCommitErrorCallback cb, gpointer data )
00198 {
00199     g_error_cb = cb;
00200     g_error_cb_data = data;
00201 }
00202 
00203 void
00204 gnc_engine_signal_commit_error( QofBackendError errcode )
00205 {
00206     if ( g_error_cb != NULL )
00207     {
00208         (*g_error_cb)( g_error_cb_data, errcode );
00209     }
00210 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines