2011-02-16 [paul] 3.7.8cvs57
[claws.git] / src / common / hooks.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 static GHookList *hooks_get_hooklist(const 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, g_strdup(hooklist_name), hooklist);
45         
46         return hooklist;
47 }
48
49 guint hooks_register_hook(const gchar *hooklist_name,
50                           SylpheedHookFunction hook_func,
51                           gpointer userdata)
52 {
53         GHookList *hooklist;
54         GHook *hook;
55
56         cm_return_val_if_fail(hooklist_name != NULL, (guint)-1);
57         cm_return_val_if_fail(hook_func != NULL, (guint)-1);
58         
59         hooklist = hooks_get_hooklist(hooklist_name);
60         cm_return_val_if_fail(hooklist != NULL, (guint)-1);
61
62         hook = g_hook_alloc(hooklist);
63         cm_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 %lu\n", hooklist_name, hook->hook_id);
71
72         return hook->hook_id;
73 }
74
75 void hooks_unregister_hook(const gchar *hooklist_name,
76                            guint hook_id)
77 {
78         GHookList *hooklist;
79         GHook *hook;
80
81         cm_return_if_fail(hooklist_name != NULL);
82
83         hooklist = hooks_get_hooklist(hooklist_name);
84         cm_return_if_fail(hooklist != NULL);
85
86         hook = g_hook_get(hooklist, hook_id);
87         cm_return_if_fail(hook != NULL);
88
89         debug_print("unregisted hook %lu in '%s'\n", hook->hook_id, hooklist_name);
90
91         g_hook_destroy(hooklist, hook_id);
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(const gchar *hooklist_name,
112                   gpointer source)
113 {
114         GHookList *hooklist;
115         struct MarshalData marshal_data;
116         
117         cm_return_val_if_fail(hooklist_name != NULL, FALSE);
118
119         hooklist = hooks_get_hooklist(hooklist_name);
120         cm_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 }