2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
30 static GHashTable *hooklist_table;
32 static GHookList *hooks_get_hooklist(const gchar *hooklist_name)
36 if (hooklist_table == NULL)
37 hooklist_table = g_hash_table_new(g_str_hash, g_str_equal);
39 hooklist = (GHookList *) g_hash_table_lookup(hooklist_table, hooklist_name);
43 hooklist = g_new0(GHookList, 1);
44 g_hook_list_init(hooklist, sizeof(GHook));
45 g_hash_table_insert(hooklist_table, g_strdup(hooklist_name), hooklist);
50 guint hooks_register_hook(const gchar *hooklist_name,
51 SylpheedHookFunction hook_func,
57 cm_return_val_if_fail(hooklist_name != NULL, (guint)-1);
58 cm_return_val_if_fail(hook_func != NULL, (guint)-1);
60 hooklist = hooks_get_hooklist(hooklist_name);
61 cm_return_val_if_fail(hooklist != NULL, (guint)-1);
63 hook = g_hook_alloc(hooklist);
64 cm_return_val_if_fail(hook != NULL, (guint)-1);
66 hook->func = hook_func;
67 hook->data = userdata;
69 g_hook_append(hooklist, hook);
71 debug_print("registed new hook for '%s' as id %lu\n", hooklist_name, hook->hook_id);
76 void hooks_unregister_hook(const gchar *hooklist_name,
82 cm_return_if_fail(hooklist_name != NULL);
84 hooklist = hooks_get_hooklist(hooklist_name);
85 cm_return_if_fail(hooklist != NULL);
87 hook = g_hook_get(hooklist, hook_id);
88 cm_return_if_fail(hook != NULL);
90 debug_print("unregisted hook %lu in '%s'\n", hook->hook_id, hooklist_name);
92 g_hook_destroy(hooklist, hook_id);
101 static void hooks_marshal(GHook *hook, gpointer data)
103 gboolean (*func) (gpointer source, gpointer data);
104 struct MarshalData *marshal_data = (struct MarshalData *)data;
106 if (!marshal_data->abort) {
108 marshal_data->abort = func(marshal_data->source, hook->data);
112 gboolean hooks_invoke(const gchar *hooklist_name,
116 struct MarshalData marshal_data;
118 cm_return_val_if_fail(hooklist_name != NULL, FALSE);
120 hooklist = hooks_get_hooklist(hooklist_name);
121 cm_return_val_if_fail(hooklist != NULL, FALSE);
123 marshal_data.source = source;
124 marshal_data.abort = FALSE;
126 g_hook_list_marshal(hooklist, TRUE, hooks_marshal, &marshal_data);
128 return marshal_data.abort;