Change home directory fallback to "C:\Claws-mail" on Windows.
[claws.git] / src / common / hooks.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26
27 #include "utils.h"
28 #include "hooks.h"
29
30 static GHashTable *hooklist_table;
31
32 static GHookList *hooks_get_hooklist(const gchar *hooklist_name)
33 {
34         GHookList *hooklist;
35
36         if (hooklist_table == NULL)
37                 hooklist_table = g_hash_table_new(g_str_hash, g_str_equal);
38         
39         hooklist = (GHookList *) g_hash_table_lookup(hooklist_table, hooklist_name);
40         if (hooklist != NULL)
41                 return hooklist;
42         
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);
46         
47         return hooklist;
48 }
49
50 guint hooks_register_hook(const gchar *hooklist_name,
51                           SylpheedHookFunction hook_func,
52                           gpointer userdata)
53 {
54         GHookList *hooklist;
55         GHook *hook;
56
57         cm_return_val_if_fail(hooklist_name != NULL, (guint)-1);
58         cm_return_val_if_fail(hook_func != NULL, (guint)-1);
59         
60         hooklist = hooks_get_hooklist(hooklist_name);
61         cm_return_val_if_fail(hooklist != NULL, (guint)-1);
62
63         hook = g_hook_alloc(hooklist);
64         cm_return_val_if_fail(hook != NULL, (guint)-1);
65
66         hook->func = hook_func;
67         hook->data = userdata;
68
69         g_hook_append(hooklist, hook);
70
71         debug_print("registed new hook for '%s' as id %lu\n", hooklist_name, hook->hook_id);
72
73         return hook->hook_id;
74 }
75
76 void hooks_unregister_hook(const gchar *hooklist_name,
77                            guint hook_id)
78 {
79         GHookList *hooklist;
80         GHook *hook;
81
82         cm_return_if_fail(hooklist_name != NULL);
83
84         hooklist = hooks_get_hooklist(hooklist_name);
85         cm_return_if_fail(hooklist != NULL);
86
87         hook = g_hook_get(hooklist, hook_id);
88         cm_return_if_fail(hook != NULL);
89
90         debug_print("unregisted hook %lu in '%s'\n", hook->hook_id, hooklist_name);
91
92         g_hook_destroy(hooklist, hook_id);
93 }
94
95 struct MarshalData
96 {
97         gpointer        source;
98         gboolean        abort;
99 };
100
101 static void hooks_marshal(GHook *hook, gpointer data)
102 {
103         gboolean (*func) (gpointer source, gpointer data);
104         struct MarshalData *marshal_data = (struct MarshalData *)data;
105
106         if (!marshal_data->abort) {
107                 func = hook->func;
108                 marshal_data->abort = func(marshal_data->source, hook->data);
109         }
110 }
111
112 gboolean hooks_invoke(const gchar *hooklist_name,
113                   gpointer source)
114 {
115         GHookList *hooklist;
116         struct MarshalData marshal_data;
117         
118         cm_return_val_if_fail(hooklist_name != NULL, FALSE);
119
120         hooklist = hooks_get_hooklist(hooklist_name);
121         cm_return_val_if_fail(hooklist != NULL, FALSE);
122
123         marshal_data.source = source;
124         marshal_data.abort = FALSE;
125
126         g_hook_list_marshal(hooklist, TRUE, hooks_marshal, &marshal_data);
127
128         return marshal_data.abort;
129 }