manage.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Handle any events in application.
  3. Manage events: add, delete, destroy, search
  4. Copyright (C) 2011
  5. The 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/event.h"
  23. #include "internal.h"
  24. /*** global variables ****************************************************************************/
  25. /*** file scope macro definitions ****************************************************************/
  26. /*** file scope type declarations ****************************************************************/
  27. /*** file scope variables ************************************************************************/
  28. /*** file scope functions ************************************************************************/
  29. /* --------------------------------------------------------------------------------------------- */
  30. static void
  31. mc_event_group_destroy_value (gpointer data)
  32. {
  33. GPtrArray *callbacks;
  34. callbacks = (GPtrArray *) data;
  35. g_ptr_array_foreach (callbacks, (GFunc) g_free, NULL);
  36. g_ptr_array_free (callbacks, TRUE);
  37. }
  38. /* --------------------------------------------------------------------------------------------- */
  39. /* --------------------------------------------------------------------------------------------- */
  40. /*** public functions ****************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. gboolean
  43. mc_event_add (const gchar * event_group_name, const gchar * event_name,
  44. mc_event_callback_func_t event_callback, gpointer event_init_data, GError ** mcerror)
  45. {
  46. GTree *event_group;
  47. GPtrArray *callbacks;
  48. mc_event_callback_t *cb;
  49. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
  50. || event_callback == NULL)
  51. {
  52. g_propagate_error (mcerror,
  53. g_error_new (MC_ERROR, 1,
  54. _("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. event_group = (GTree *) g_tree_lookup (mc_event_grouplist, (gconstpointer) event_group_name);
  120. if (event_group == NULL && create_new)
  121. {
  122. event_group =
  123. g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
  124. NULL,
  125. (GDestroyNotify) g_free,
  126. (GDestroyNotify) mc_event_group_destroy_value);
  127. if (event_group == NULL)
  128. {
  129. g_propagate_error (mcerror,
  130. g_error_new (MC_ERROR, 1,
  131. _("Unable to create group '%s' for events!"),
  132. event_group_name));
  133. return NULL;
  134. }
  135. g_tree_insert (mc_event_grouplist, g_strdup (event_group_name), (gpointer) event_group);
  136. }
  137. return event_group;
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. GPtrArray *
  141. mc_event_get_event_by_name (GTree * event_group, const gchar * event_name, gboolean create_new,
  142. GError ** mcerror)
  143. {
  144. GPtrArray *callbacks;
  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. g_propagate_error (mcerror,
  152. g_error_new (MC_ERROR, 1,
  153. _("Unable to create event '%s'!"), event_name));
  154. return NULL;
  155. }
  156. g_tree_insert (event_group, g_strdup (event_name), (gpointer) callbacks);
  157. }
  158. return callbacks;
  159. }
  160. /* --------------------------------------------------------------------------------------------- */
  161. mc_event_callback_t *
  162. mc_event_is_callback_in_array (GPtrArray * callbacks, mc_event_callback_func_t event_callback,
  163. gpointer event_init_data)
  164. {
  165. guint array_index;
  166. for (array_index = 0; array_index < callbacks->len; array_index++)
  167. {
  168. mc_event_callback_t *cb = g_ptr_array_index (callbacks, array_index);
  169. if (cb->callback == event_callback && cb->init_data == event_init_data)
  170. return cb;
  171. }
  172. return NULL;
  173. }
  174. /* --------------------------------------------------------------------------------------------- */