manage.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Handle any events in application.
  3. Manage events: add, delete, destroy, search
  4. Copyright (C) 2011-2017
  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. /*** file scope variables ************************************************************************/
  29. /*** file scope functions ************************************************************************/
  30. /* --------------------------------------------------------------------------------------------- */
  31. static void
  32. mc_event_group_destroy_value (gpointer data)
  33. {
  34. GPtrArray *callbacks;
  35. callbacks = (GPtrArray *) data;
  36. g_ptr_array_foreach (callbacks, (GFunc) g_free, NULL);
  37. g_ptr_array_free (callbacks, TRUE);
  38. }
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** public functions ****************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. gboolean
  44. mc_event_add (const gchar * event_group_name, const gchar * event_name,
  45. mc_event_callback_func_t event_callback, gpointer event_init_data, GError ** mcerror)
  46. {
  47. GTree *event_group;
  48. GPtrArray *callbacks;
  49. mc_event_callback_t *cb;
  50. mc_return_val_if_error (mcerror, FALSE);
  51. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
  52. || event_callback == NULL)
  53. {
  54. mc_propagate_error (mcerror, 0, "%s", _("Check input data! Some of parameters are NULL!"));
  55. return FALSE;
  56. }
  57. event_group = mc_event_get_event_group_by_name (event_group_name, TRUE, mcerror);
  58. if (event_group == NULL)
  59. return FALSE;
  60. callbacks = mc_event_get_event_by_name (event_group, event_name, TRUE, mcerror);
  61. if (callbacks == NULL)
  62. return FALSE;
  63. cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
  64. if (cb == NULL)
  65. {
  66. cb = g_new0 (mc_event_callback_t, 1);
  67. cb->callback = event_callback;
  68. g_ptr_array_add (callbacks, (gpointer) cb);
  69. }
  70. cb->init_data = event_init_data;
  71. return TRUE;
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. void
  75. mc_event_del (const gchar * event_group_name, const gchar * event_name,
  76. mc_event_callback_func_t event_callback, gpointer event_init_data)
  77. {
  78. GTree *event_group;
  79. GPtrArray *callbacks;
  80. mc_event_callback_t *cb;
  81. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
  82. || event_callback == NULL)
  83. return;
  84. event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
  85. if (event_group == NULL)
  86. return;
  87. callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
  88. if (callbacks == NULL)
  89. return;
  90. cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
  91. if (cb == NULL)
  92. return;
  93. g_ptr_array_remove (callbacks, (gpointer) cb);
  94. g_free ((gpointer) cb);
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. void
  98. mc_event_destroy (const gchar * event_group_name, const gchar * event_name)
  99. {
  100. GTree *event_group;
  101. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
  102. return;
  103. event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
  104. g_tree_remove (event_group, (gconstpointer) event_name);
  105. }
  106. /* --------------------------------------------------------------------------------------------- */
  107. void
  108. mc_event_group_del (const gchar * event_group_name)
  109. {
  110. if (mc_event_grouplist != NULL && event_group_name != NULL)
  111. g_tree_remove (mc_event_grouplist, (gconstpointer) event_group_name);
  112. }
  113. /* --------------------------------------------------------------------------------------------- */
  114. GTree *
  115. mc_event_get_event_group_by_name (const gchar * event_group_name, gboolean create_new,
  116. GError ** mcerror)
  117. {
  118. GTree *event_group;
  119. mc_return_val_if_error (mcerror, FALSE);
  120. event_group = (GTree *) g_tree_lookup (mc_event_grouplist, (gconstpointer) event_group_name);
  121. if (event_group == NULL && create_new)
  122. {
  123. event_group =
  124. g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
  125. NULL,
  126. (GDestroyNotify) g_free,
  127. (GDestroyNotify) mc_event_group_destroy_value);
  128. if (event_group == NULL)
  129. {
  130. mc_propagate_error (mcerror, 0, _("Unable to create group '%s' for events!"),
  131. event_group_name);
  132. return NULL;
  133. }
  134. g_tree_insert (mc_event_grouplist, g_strdup (event_group_name), (gpointer) event_group);
  135. }
  136. return event_group;
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. GPtrArray *
  140. mc_event_get_event_by_name (GTree * event_group, const gchar * event_name, gboolean create_new,
  141. GError ** mcerror)
  142. {
  143. GPtrArray *callbacks;
  144. mc_return_val_if_error (mcerror, FALSE);
  145. callbacks = (GPtrArray *) g_tree_lookup (event_group, (gconstpointer) event_name);
  146. if (callbacks == NULL && create_new)
  147. {
  148. callbacks = g_ptr_array_new ();
  149. if (callbacks == NULL)
  150. {
  151. mc_propagate_error (mcerror, 0, _("Unable to create event '%s'!"), event_name);
  152. return NULL;
  153. }
  154. g_tree_insert (event_group, g_strdup (event_name), (gpointer) callbacks);
  155. }
  156. return callbacks;
  157. }
  158. /* --------------------------------------------------------------------------------------------- */
  159. mc_event_callback_t *
  160. mc_event_is_callback_in_array (GPtrArray * callbacks, mc_event_callback_func_t event_callback,
  161. gpointer event_init_data)
  162. {
  163. guint array_index;
  164. for (array_index = 0; array_index < callbacks->len; array_index++)
  165. {
  166. mc_event_callback_t *cb = g_ptr_array_index (callbacks, array_index);
  167. if (cb->callback == event_callback && cb->init_data == event_init_data)
  168. return cb;
  169. }
  170. return NULL;
  171. }
  172. /* --------------------------------------------------------------------------------------------- */