2 Handle any events in application.
3 Manage events: add, delete, destroy, search
5 Copyright (C) 2011-2024
6 Free Software Foundation, Inc.
9 Slava Zanko <slavazanko@gmail.com>, 2011.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "lib/global.h"
31 #include "lib/event.h"
35 /*** global variables ****************************************************************************/
37 /*** file scope macro definitions ****************************************************************/
39 /*** file scope type declarations ****************************************************************/
41 /*** forward declarations (file scope functions) *************************************************/
43 /*** file scope variables ************************************************************************/
45 /* --------------------------------------------------------------------------------------------- */
46 /*** file scope functions ************************************************************************/
47 /* --------------------------------------------------------------------------------------------- */
50 mc_event_group_destroy_value (gpointer data
)
52 g_ptr_array_free ((GPtrArray
*) data
, TRUE
);
55 /* --------------------------------------------------------------------------------------------- */
56 /*** public functions ****************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
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
)
65 mc_event_callback_t
*cb
;
67 mc_return_val_if_error (mcerror
, FALSE
);
69 if (mc_event_grouplist
== NULL
|| event_group_name
== NULL
|| event_name
== NULL
70 || event_callback
== NULL
)
72 mc_propagate_error (mcerror
, 0, "%s", _("Check input data! Some of parameters are NULL!"));
76 event_group
= mc_event_get_event_group_by_name (event_group_name
, TRUE
, mcerror
);
77 if (event_group
== NULL
)
80 callbacks
= mc_event_get_event_by_name (event_group
, event_name
, TRUE
, mcerror
);
81 if (callbacks
== NULL
)
84 cb
= mc_event_is_callback_in_array (callbacks
, event_callback
, event_init_data
);
87 cb
= g_new0 (mc_event_callback_t
, 1);
88 cb
->callback
= event_callback
;
89 g_ptr_array_add (callbacks
, (gpointer
) cb
);
91 cb
->init_data
= event_init_data
;
95 /* --------------------------------------------------------------------------------------------- */
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
)
102 GPtrArray
*callbacks
;
103 mc_event_callback_t
*cb
;
105 if (mc_event_grouplist
== NULL
|| event_group_name
== NULL
|| event_name
== NULL
106 || event_callback
== NULL
)
109 event_group
= mc_event_get_event_group_by_name (event_group_name
, FALSE
, NULL
);
110 if (event_group
== NULL
)
113 callbacks
= mc_event_get_event_by_name (event_group
, event_name
, FALSE
, NULL
);
114 if (callbacks
== NULL
)
117 cb
= mc_event_is_callback_in_array (callbacks
, event_callback
, event_init_data
);
119 g_ptr_array_remove (callbacks
, (gpointer
) cb
);
122 /* --------------------------------------------------------------------------------------------- */
125 mc_event_destroy (const gchar
*event_group_name
, const gchar
*event_name
)
129 if (mc_event_grouplist
== NULL
|| event_group_name
== NULL
|| event_name
== NULL
)
132 event_group
= mc_event_get_event_group_by_name (event_group_name
, FALSE
, NULL
);
133 g_tree_remove (event_group
, (gconstpointer
) event_name
);
136 /* --------------------------------------------------------------------------------------------- */
139 mc_event_group_del (const gchar
*event_group_name
)
142 if (mc_event_grouplist
!= NULL
&& event_group_name
!= NULL
)
143 g_tree_remove (mc_event_grouplist
, (gconstpointer
) event_group_name
);
146 /* --------------------------------------------------------------------------------------------- */
149 mc_event_get_event_group_by_name (const gchar
*event_group_name
, gboolean create_new
,
154 mc_return_val_if_error (mcerror
, FALSE
);
156 event_group
= (GTree
*) g_tree_lookup (mc_event_grouplist
, (gconstpointer
) event_group_name
);
157 if (event_group
== NULL
&& create_new
)
160 g_tree_new_full ((GCompareDataFunc
) g_ascii_strcasecmp
,
162 (GDestroyNotify
) g_free
,
163 (GDestroyNotify
) mc_event_group_destroy_value
);
164 if (event_group
== NULL
)
166 mc_propagate_error (mcerror
, 0, _("Unable to create group '%s' for events!"),
170 g_tree_insert (mc_event_grouplist
, g_strdup (event_group_name
), (gpointer
) event_group
);
175 /* --------------------------------------------------------------------------------------------- */
178 mc_event_get_event_by_name (GTree
*event_group
, const gchar
*event_name
, gboolean create_new
,
181 GPtrArray
*callbacks
;
183 mc_return_val_if_error (mcerror
, FALSE
);
185 callbacks
= (GPtrArray
*) g_tree_lookup (event_group
, (gconstpointer
) event_name
);
186 if (callbacks
== NULL
&& create_new
)
188 callbacks
= g_ptr_array_new_with_free_func (g_free
);
189 if (callbacks
== NULL
)
191 mc_propagate_error (mcerror
, 0, _("Unable to create event '%s'!"), event_name
);
194 g_tree_insert (event_group
, g_strdup (event_name
), (gpointer
) callbacks
);
199 /* --------------------------------------------------------------------------------------------- */
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
)
207 for (array_index
= 0; array_index
< callbacks
->len
; array_index
++)
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
)
216 /* --------------------------------------------------------------------------------------------- */