95dc0a23bc07f6aaaec006dcb49395c77ad21b24
[claws.git] / src / common / hooks.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
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 2 of the License, or
8  * (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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25
26 #include "hooks.h"
27
28 GHashTable *hooklist_table;
29
30 GHookList *hooks_get_hooklist(gchar *hooklist_name)
31 {
32         GHookList *hooklist;
33
34         if (hooklist_table == NULL)
35                 hooklist_table = g_hash_table_new(g_str_hash, g_str_equal);
36         
37         hooklist = (GHookList *) g_hash_table_lookup(hooklist_table, hooklist_name);
38         if (hooklist != NULL)
39                 return hooklist;
40         
41         hooklist = g_new0(GHookList, 1);
42         g_hook_list_init(hooklist, sizeof(GHook));
43         g_hash_table_insert(hooklist_table, hooklist_name, hooklist);
44         
45         return hooklist;
46 }
47
48 gint hooks_register_hook(gchar *hooklist_name,
49                          SylpheedHookFunction hook_func,
50                          gpointer userdata)
51 {
52         GHookList *hooklist;
53         GHook *hook;
54
55         g_return_val_if_fail(hooklist_name != NULL, -1);
56         g_return_val_if_fail(hook_func != NULL, -1);
57         
58         hooklist = hooks_get_hooklist(hooklist_name);
59         g_return_val_if_fail(hooklist != NULL, -1);
60
61         hook = g_hook_alloc(hooklist);
62         g_return_val_if_fail(hook != NULL, -1);
63
64         hook->func = hook_func;
65         hook->data = userdata;
66
67         g_hook_append(hooklist, hook);
68
69         return hook->hook_id;
70 }
71
72 void hooks_unregister_hook(gchar *hooklist_name,
73                            guint hook_id)
74 {
75         GHookList *hooklist;
76         GHook *hook;
77
78         g_return_if_fail(hooklist_name != NULL);
79
80         hooklist = hooks_get_hooklist(hooklist_name);
81         g_return_if_fail(hooklist != NULL);
82
83         hook = g_hook_get(hooklist, hook_id);
84         g_return_if_fail(hook != NULL);
85
86         g_hook_destroy_link(hooklist, hook);
87 }
88
89 struct MarshalData
90 {
91         gpointer        source;
92         gboolean        abort;
93 };
94
95 static void hooks_marshal(GHook *hook, gpointer data)
96 {
97         gboolean (*func) (gpointer source, gpointer data);
98         struct MarshalData *marshal_data = (struct MarshalData *)data;
99
100         if (!marshal_data->abort) {
101                 func = hook->func;
102                 marshal_data->abort = func(marshal_data->source, hook->data);
103         }
104 }
105
106 void hooks_invoke(gchar *hooklist_name,
107                   gpointer source)
108 {
109         GHookList *hooklist;
110         struct MarshalData marshal_data;
111         
112         g_return_if_fail(hooklist_name != NULL);
113
114         hooklist = hooks_get_hooklist(hooklist_name);
115         g_return_if_fail(hooklist != NULL);
116
117         marshal_data.source = source;
118         marshal_data.abort = FALSE;
119
120         g_hook_list_marshal(hooklist, TRUE, hooks_marshal, &marshal_data);
121 }