2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 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/>.
21 #include "claws-features.h"
29 static GHashTable
*hooklist_table
;
31 static GHookList
*hooks_get_hooklist(const gchar
*hooklist_name
)
35 if (hooklist_table
== NULL
)
36 hooklist_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
38 hooklist
= (GHookList
*) g_hash_table_lookup(hooklist_table
, hooklist_name
);
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
);
49 gulong
hooks_register_hook(const gchar
*hooklist_name
,
50 SylpheedHookFunction hook_func
,
56 cm_return_val_if_fail(hooklist_name
!= NULL
, HOOK_NONE
);
57 cm_return_val_if_fail(hook_func
!= NULL
, HOOK_NONE
);
59 hooklist
= hooks_get_hooklist(hooklist_name
);
60 cm_return_val_if_fail(hooklist
!= NULL
, HOOK_NONE
);
62 hook
= g_hook_alloc(hooklist
);
63 cm_return_val_if_fail(hook
!= NULL
, HOOK_NONE
);
65 hook
->func
= hook_func
;
66 hook
->data
= userdata
;
68 g_hook_append(hooklist
, hook
);
70 debug_print("registered new hook for '%s' as id %lu\n", hooklist_name
, hook
->hook_id
);
71 if (hook
->hook_id
== HOOK_NONE
)
72 g_error("unexpected hook ID 0");
77 void hooks_unregister_hook(const gchar
*hooklist_name
,
83 cm_return_if_fail(hooklist_name
!= NULL
);
85 hooklist
= hooks_get_hooklist(hooklist_name
);
86 cm_return_if_fail(hooklist
!= NULL
);
88 hook
= g_hook_get(hooklist
, hook_id
);
89 cm_return_if_fail(hook
!= NULL
);
91 debug_print("unregistered hook %lu in '%s'\n", hook
->hook_id
, hooklist_name
);
93 g_hook_destroy(hooklist
, hook_id
);
102 static void hooks_marshal(GHook
*hook
, gpointer data
)
104 gboolean (*func
) (gpointer source
, gpointer data
);
105 struct MarshalData
*marshal_data
= (struct MarshalData
*)data
;
107 if (!marshal_data
->abort
) {
109 marshal_data
->abort
= func(marshal_data
->source
, hook
->data
);
113 gboolean
hooks_invoke(const gchar
*hooklist_name
,
117 struct MarshalData marshal_data
;
119 cm_return_val_if_fail(hooklist_name
!= NULL
, FALSE
);
121 hooklist
= hooks_get_hooklist(hooklist_name
);
122 cm_return_val_if_fail(hooklist
!= NULL
, FALSE
);
124 marshal_data
.source
= source
;
125 marshal_data
.abort
= FALSE
;
127 g_hook_list_marshal(hooklist
, TRUE
, hooks_marshal
, &marshal_data
);
129 return marshal_data
.abort
;