0.9.3claws23
[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 "utils.h"
27 #include "hooks.h"
28
29 static GHashTable *hooklist_table;
30
31 GHookList *hooks_get_hooklist(gchar *hooklist_name)
32 {
33         GHookList *hooklist;
34
35         if (hooklist_table == NULL)
36                 hooklist_table = g_hash_table_new(g_str_hash, g_str_equal);
37         
38         hooklist = (GHookList *) g_hash_table_lookup(hooklist_table, hooklist_name);
39         if (hooklist != NULL)
40                 return hooklist;
41         
42         hooklist = g_new0(GHookList, 1);
43         g_hook_list_init(hooklist, sizeof(GHook));
44         g_hash_table_insert(hooklist_table, hooklist_name, hooklist);
45         
46         return hooklist;
47 }
48
49 guint hooks_register_hook(gchar *hooklist_name,
50                           SylpheedHookFunction hook_func,
51                           gpointer userdata)
52 {
53         GHookList *hooklist;
54         GHook *hook;
55
56         g_return_val_if_fail(hooklist_name != NULL, (guint)-1);
57         g_return_val_if_fail(hook_func != NULL, (guint)-1);
58         
59         hooklist = hooks_get_hooklist(hooklist_name);
60         g_return_val_if_fail(hooklist != NULL, (guint)-1);
61
62         hook = g_hook_alloc(hooklist);
63         g_return_val_if_fail(hook != NULL, (guint)-1);
64
65         hook->func = hook_func;
66         hook->data = userdata;
67
68         g_hook_append(hooklist, hook);
69
70         debug_print("registed new hook for '%s' as id %d\n", hooklist_name, hook->hook_id);
71
72         return hook->hook_id;
73 }
74
75 void hooks_unregister_hook(gchar *hooklist_name,
76                            guint hook_id)
77 {
78         GHookList *hooklist;
79         GHook *hook;
80
81         g_return_if_fail(hooklist_name != NULL);
82
83         hooklist = hooks_get_hooklist(hooklist_name);
84         g_return_if_fail(hooklist != NULL);
85
86         hook = g_hook_get(hooklist, hook_id);
87         g_return_if_fail(hook != NULL);
88
89         debug_print("unregisted hook %d in '%s'\n", hook->hook_id, hooklist_name);
90
91         g_hook_destroy(hooklist, hook);
92 }
93
94 struct MarshalData
95 {
96         gpointer        source;
97         gboolean        abort;
98 };
99
100 static void hooks_marshal(GHook *hook, gpointer data)
101 {
102         gboolean (*func) (gpointer source, gpointer data);
103         struct MarshalData *marshal_data = (struct MarshalData *)data;
104
105         if (!marshal_data->abort) {
106                 func = hook->func;
107                 marshal_data->abort = func(marshal_data->source, hook->data);
108         }
109 }
110
111 gboolean hooks_invoke(gchar *hooklist_name,
112                   gpointer source)
113 {
114         GHookList *hooklist;
115         struct MarshalData marshal_data;
116         
117         g_return_val_if_fail(hooklist_name != NULL, FALSE);
118
119         hooklist = hooks_get_hooklist(hooklist_name);
120         g_return_val_if_fail(hooklist != NULL, FALSE);
121
122         marshal_data.source = source;
123         marshal_data.abort = FALSE;
124
125         g_hook_list_marshal(hooklist, TRUE, hooks_marshal, &marshal_data);
126
127         return marshal_data.abort;
128 }