manage.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Handle any events in application.
  3. Manage events: add, delete, destroy, search
  4. Copyright (C) 2011-2024
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2011.
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include "lib/global.h"
  22. #include "lib/util.h"
  23. #include "lib/event.h"
  24. #include "internal.h"
  25. /*** global variables ****************************************************************************/
  26. /*** file scope macro definitions ****************************************************************/
  27. /*** file scope type declarations ****************************************************************/
  28. /*** forward declarations (file scope functions) *************************************************/
  29. /*** file scope variables ************************************************************************/
  30. /* --------------------------------------------------------------------------------------------- */
  31. /*** file scope functions ************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. static void
  34. mc_event_group_destroy_value (gpointer data)
  35. {
  36. g_ptr_array_free ((GPtrArray *) data, TRUE);
  37. }
  38. /* --------------------------------------------------------------------------------------------- */
  39. /*** public functions ****************************************************************************/
  40. /* --------------------------------------------------------------------------------------------- */
  41. gboolean
  42. mc_event_add (const gchar *event_group_name, const gchar *event_name,
  43. mc_event_callback_func_t event_callback, gpointer event_init_data, GError **mcerror)
  44. {
  45. GTree *event_group;
  46. GPtrArray *callbacks;
  47. mc_event_callback_t *cb;
  48. mc_return_val_if_error (mcerror, FALSE);
  49. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
  50. || event_callback == NULL)
  51. {
  52. mc_propagate_error (mcerror, 0, "%s", _ ("Check input data! Some of parameters are NULL!"));
  53. return FALSE;
  54. }
  55. event_group = mc_event_get_event_group_by_name (event_group_name, TRUE, mcerror);
  56. if (event_group == NULL)
  57. return FALSE;
  58. callbacks = mc_event_get_event_by_name (event_group, event_name, TRUE, mcerror);
  59. if (callbacks == NULL)
  60. return FALSE;
  61. cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
  62. if (cb == NULL)
  63. {
  64. cb = g_new0 (mc_event_callback_t, 1);
  65. cb->callback = event_callback;
  66. g_ptr_array_add (callbacks, (gpointer) cb);
  67. }
  68. cb->init_data = event_init_data;
  69. return TRUE;
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. void
  73. mc_event_del (const gchar *event_group_name, const gchar *event_name,
  74. mc_event_callback_func_t event_callback, gpointer event_init_data)
  75. {
  76. GTree *event_group;
  77. GPtrArray *callbacks;
  78. mc_event_callback_t *cb;
  79. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
  80. || event_callback == NULL)
  81. return;
  82. event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
  83. if (event_group == NULL)
  84. return;
  85. callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
  86. if (callbacks == NULL)
  87. return;
  88. cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
  89. if (cb != NULL)
  90. g_ptr_array_remove (callbacks, (gpointer) cb);
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. void
  94. mc_event_destroy (const gchar *event_group_name, const gchar *event_name)
  95. {
  96. GTree *event_group;
  97. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
  98. return;
  99. event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
  100. g_tree_remove (event_group, (gconstpointer) event_name);
  101. }
  102. /* --------------------------------------------------------------------------------------------- */
  103. void
  104. mc_event_group_del (const gchar *event_group_name)
  105. {
  106. if (mc_event_grouplist != NULL && event_group_name != NULL)
  107. g_tree_remove (mc_event_grouplist, (gconstpointer) event_group_name);
  108. }
  109. /* --------------------------------------------------------------------------------------------- */
  110. GTree *
  111. mc_event_get_event_group_by_name (const gchar *event_group_name, gboolean create_new,
  112. GError **mcerror)
  113. {
  114. GTree *event_group;
  115. mc_return_val_if_error (mcerror, FALSE);
  116. event_group = (GTree *) g_tree_lookup (mc_event_grouplist, (gconstpointer) event_group_name);
  117. if (event_group == NULL && create_new)
  118. {
  119. event_group = g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp, NULL,
  120. (GDestroyNotify) g_free,
  121. (GDestroyNotify) mc_event_group_destroy_value);
  122. if (event_group == NULL)
  123. {
  124. mc_propagate_error (mcerror, 0, _ ("Unable to create group '%s' for events!"),
  125. event_group_name);
  126. return NULL;
  127. }
  128. g_tree_insert (mc_event_grouplist, g_strdup (event_group_name), (gpointer) event_group);
  129. }
  130. return event_group;
  131. }
  132. /* --------------------------------------------------------------------------------------------- */
  133. GPtrArray *
  134. mc_event_get_event_by_name (GTree *event_group, const gchar *event_name, gboolean create_new,
  135. GError **mcerror)
  136. {
  137. GPtrArray *callbacks;
  138. mc_return_val_if_error (mcerror, FALSE);
  139. callbacks = (GPtrArray *) g_tree_lookup (event_group, (gconstpointer) event_name);
  140. if (callbacks == NULL && create_new)
  141. {
  142. callbacks = g_ptr_array_new_with_free_func (g_free);
  143. if (callbacks == NULL)
  144. {
  145. mc_propagate_error (mcerror, 0, _ ("Unable to create event '%s'!"), event_name);
  146. return NULL;
  147. }
  148. g_tree_insert (event_group, g_strdup (event_name), (gpointer) callbacks);
  149. }
  150. return callbacks;
  151. }
  152. /* --------------------------------------------------------------------------------------------- */
  153. mc_event_callback_t *
  154. mc_event_is_callback_in_array (GPtrArray *callbacks, mc_event_callback_func_t event_callback,
  155. gpointer event_init_data)
  156. {
  157. guint array_index;
  158. for (array_index = 0; array_index < callbacks->len; array_index++)
  159. {
  160. mc_event_callback_t *cb = g_ptr_array_index (callbacks, array_index);
  161. if (cb->callback == event_callback && cb->init_data == event_init_data)
  162. return cb;
  163. }
  164. return NULL;
  165. }
  166. /* --------------------------------------------------------------------------------------------- */