This source file includes following definitions.
- mc_event_group_destroy_value
- mc_event_add
- mc_event_del
- mc_event_destroy
- mc_event_group_del
- mc_event_get_event_group_by_name
- mc_event_get_event_by_name
- mc_event_is_callback_in_array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include <config.h>
28
29 #include "lib/global.h"
30 #include "lib/util.h"
31 #include "lib/event.h"
32
33 #include "internal.h"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 static void
50 mc_event_group_destroy_value (gpointer data)
51 {
52 g_ptr_array_free ((GPtrArray *) data, TRUE);
53 }
54
55
56
57
58
59 gboolean
60 mc_event_add (const gchar *event_group_name, const gchar *event_name,
61 mc_event_callback_func_t event_callback, gpointer event_init_data, GError **mcerror)
62 {
63 GTree *event_group;
64 GPtrArray *callbacks;
65 mc_event_callback_t *cb;
66
67 mc_return_val_if_error (mcerror, FALSE);
68
69 if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
70 || event_callback == NULL)
71 {
72 mc_propagate_error (mcerror, 0, "%s", _("Check input data! Some of parameters are NULL!"));
73 return FALSE;
74 }
75
76 event_group = mc_event_get_event_group_by_name (event_group_name, TRUE, mcerror);
77 if (event_group == NULL)
78 return FALSE;
79
80 callbacks = mc_event_get_event_by_name (event_group, event_name, TRUE, mcerror);
81 if (callbacks == NULL)
82 return FALSE;
83
84 cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
85 if (cb == NULL)
86 {
87 cb = g_new0 (mc_event_callback_t, 1);
88 cb->callback = event_callback;
89 g_ptr_array_add (callbacks, (gpointer) cb);
90 }
91 cb->init_data = event_init_data;
92 return TRUE;
93 }
94
95
96
97 void
98 mc_event_del (const gchar *event_group_name, const gchar *event_name,
99 mc_event_callback_func_t event_callback, gpointer event_init_data)
100 {
101 GTree *event_group;
102 GPtrArray *callbacks;
103 mc_event_callback_t *cb;
104
105 if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
106 || event_callback == NULL)
107 return;
108
109 event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
110 if (event_group == NULL)
111 return;
112
113 callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
114 if (callbacks == NULL)
115 return;
116
117 cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
118 if (cb != NULL)
119 g_ptr_array_remove (callbacks, (gpointer) cb);
120 }
121
122
123
124 void
125 mc_event_destroy (const gchar *event_group_name, const gchar *event_name)
126 {
127 GTree *event_group;
128
129 if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
130 return;
131
132 event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
133 g_tree_remove (event_group, (gconstpointer) event_name);
134 }
135
136
137
138 void
139 mc_event_group_del (const gchar *event_group_name)
140 {
141
142 if (mc_event_grouplist != NULL && event_group_name != NULL)
143 g_tree_remove (mc_event_grouplist, (gconstpointer) event_group_name);
144 }
145
146
147
148 GTree *
149 mc_event_get_event_group_by_name (const gchar *event_group_name, gboolean create_new,
150 GError **mcerror)
151 {
152 GTree *event_group;
153
154 mc_return_val_if_error (mcerror, FALSE);
155
156 event_group = (GTree *) g_tree_lookup (mc_event_grouplist, (gconstpointer) event_group_name);
157 if (event_group == NULL && create_new)
158 {
159 event_group =
160 g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
161 NULL,
162 (GDestroyNotify) g_free,
163 (GDestroyNotify) mc_event_group_destroy_value);
164 if (event_group == NULL)
165 {
166 mc_propagate_error (mcerror, 0, _("Unable to create group '%s' for events!"),
167 event_group_name);
168 return NULL;
169 }
170 g_tree_insert (mc_event_grouplist, g_strdup (event_group_name), (gpointer) event_group);
171 }
172 return event_group;
173 }
174
175
176
177 GPtrArray *
178 mc_event_get_event_by_name (GTree *event_group, const gchar *event_name, gboolean create_new,
179 GError **mcerror)
180 {
181 GPtrArray *callbacks;
182
183 mc_return_val_if_error (mcerror, FALSE);
184
185 callbacks = (GPtrArray *) g_tree_lookup (event_group, (gconstpointer) event_name);
186 if (callbacks == NULL && create_new)
187 {
188 callbacks = g_ptr_array_new_with_free_func (g_free);
189 if (callbacks == NULL)
190 {
191 mc_propagate_error (mcerror, 0, _("Unable to create event '%s'!"), event_name);
192 return NULL;
193 }
194 g_tree_insert (event_group, g_strdup (event_name), (gpointer) callbacks);
195 }
196 return callbacks;
197 }
198
199
200
201 mc_event_callback_t *
202 mc_event_is_callback_in_array (GPtrArray *callbacks, mc_event_callback_func_t event_callback,
203 gpointer event_init_data)
204 {
205 guint array_index;
206
207 for (array_index = 0; array_index < callbacks->len; array_index++)
208 {
209 mc_event_callback_t *cb = g_ptr_array_index (callbacks, array_index);
210 if (cb->callback == event_callback && cb->init_data == event_init_data)
211 return cb;
212 }
213 return NULL;
214 }
215
216