28 #include "gnc-hooks.h" 31 static QofLogModule log_module = GNC_MOD_ENGINE;
33 static GHashTable* gnc_hooks_list = NULL;
34 static gboolean gnc_hooks_initialized = FALSE;
45 gnc_hook_create (
const gchar *name, gint num_args,
const gchar *desc)
49 g_return_val_if_fail(name != NULL, NULL);
50 g_return_val_if_fail(num_args <= 1, NULL);
51 g_return_val_if_fail(desc != NULL, NULL);
53 ENTER(
"name %s", name);
54 if (gnc_hooks_list == NULL)
56 gnc_hooks_list = g_hash_table_new(g_str_hash, g_str_equal);
59 if (!gnc_hooks_initialized)
63 hook_list = g_hash_table_lookup(gnc_hooks_list, name);
66 LEAVE(
"List %s(%p) already exists", name, hook_list);
71 hook_list->desc = g_strdup(desc);
72 hook_list->danglers = g_malloc(
sizeof(GHookList));
73 g_hook_list_init(hook_list->danglers,
sizeof(GHook));
74 hook_list->num_args = num_args;
75 g_hash_table_insert(gnc_hooks_list, (gchar *)name, hook_list);
77 LEAVE(
"created list %s(%p)", name, hook_list);
82 gnc_hook_lookup (
const gchar *name)
86 ENTER(
"name %s", name);
87 if (gnc_hooks_list == NULL)
89 PINFO(
"no hook lists");
93 hook = g_hash_table_lookup(gnc_hooks_list, name);
94 LEAVE(
"hook list %p", hook);
99 gnc_hook_num_args (
const gchar *name)
104 ENTER(
"name %s", name);
105 if (gnc_hooks_list == NULL)
107 PINFO(
"no hook lists");
111 hook = g_hash_table_lookup(gnc_hooks_list, name);
113 num_args = hook->num_args;
114 LEAVE(
"hook list %p, num_args %i", hook, num_args);
119 gnc_hook_add_dangler (
const gchar *name, GFunc callback,
120 GDestroyNotify destroy, gpointer cb_arg)
125 ENTER(
"list %s, function %p, cbarg %p", name, callback, cb_arg);
126 gnc_hook = gnc_hook_lookup(name);
127 g_return_if_fail(gnc_hook != NULL);
128 hook = g_hook_alloc(gnc_hook->danglers);
129 hook->func = callback;
131 hook->destroy = destroy;
132 g_hook_append(gnc_hook->danglers, hook);
137 gnc_hook_remove_dangler (
const gchar *name, GFunc callback)
142 ENTER(
"name %s, function %p", name, callback);
143 gnc_hook = gnc_hook_lookup(name);
144 if (gnc_hook == NULL)
146 LEAVE(
"Unknown hook list %s", name);
150 hook = g_hook_find_func(gnc_hook->danglers, TRUE, callback);
153 LEAVE(
"Hook %p not found in %s", callback, name);
157 g_hook_destroy_link(gnc_hook->danglers, hook);
158 LEAVE(
"Removed %p from %s", hook, name);
162 call_hook (GHook *hook, gpointer data)
164 ENTER(
"hook %p (func %p), data %p, cbarg %p", hook, hook->func, data,
166 ((GFunc)hook->func)(data, hook->data);
171 gnc_hook_run (
const gchar *name, gpointer data)
175 ENTER(
"list %s, data %p", (name == NULL ?
"(null)" : name), data);
176 hook = gnc_hook_lookup(name);
179 LEAVE(
"No such hook list");
182 g_hook_list_marshal(hook->danglers, TRUE, call_hook, data);
191 if (gnc_hooks_initialized)
193 LEAVE(
"Hooks already initialized");
197 gnc_hooks_initialized = TRUE;
199 gnc_hook_create(HOOK_STARTUP, 0,
200 "Functions to run at startup. Hook args: ()");
201 gnc_hook_create(HOOK_SHUTDOWN, 0,
202 "Functions to run at guile shutdown. Hook args: ()");
203 gnc_hook_create(HOOK_UI_STARTUP, 0,
204 "Functions to run when the ui comes up. Hook args: ()");
205 gnc_hook_create(HOOK_UI_POST_STARTUP, 0,
206 "Functions to run after the ui comes up. Hook args: ()");
207 gnc_hook_create(HOOK_UI_SHUTDOWN, 0,
208 "Functions to run at ui shutdown. Hook args: ()");
209 gnc_hook_create(HOOK_NEW_BOOK, 0,
210 "Run after a new (empty) book is opened, before the" 211 " book-opened-hook. Hook args: ()");
212 gnc_hook_create(HOOK_REPORT, 0,
213 "Run just before the reports are pushed into the menus." 215 gnc_hook_create(HOOK_CURRENCY_CHANGED, 0,
216 "Functions to run when the user changes currency settings. Hook args: ()");
217 gnc_hook_create(HOOK_SAVE_OPTIONS, 0,
218 "Functions to run when saving options. Hook args: ()");
219 gnc_hook_create(HOOK_ADD_EXTENSION, 0,
220 "Functions to run when the extensions menu is created." 223 gnc_hook_create(HOOK_BOOK_OPENED, 1,
224 "Run after book open. Hook args: <gnc:Session*>.");
225 gnc_hook_create(HOOK_BOOK_CLOSED, 1,
226 "Run before file close. Hook args: <gnc:Session*>");
227 gnc_hook_create(HOOK_BOOK_SAVED, 1,
228 "Run after file saved. Hook args: <gnc:Session*>");
#define PINFO(format, args...)
Print an informational note.
#define ENTER(format, args...)
Print a function entry debugging message.
All type declarations for the whole Gnucash engine.
#define LEAVE(format, args...)
Print a function exit debugging message.