event.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Handle events in application.
  3. Interface functions: init/deinit; start/stop
  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. /*** file scope variables ************************************************************************/
  29. GTree *mc_event_grouplist = NULL;
  30. /*** file scope functions ************************************************************************/
  31. /* --------------------------------------------------------------------------------------------- */
  32. /*** public functions ****************************************************************************/
  33. /* --------------------------------------------------------------------------------------------- */
  34. gboolean
  35. mc_event_init (GError **mcerror)
  36. {
  37. mc_return_val_if_error (mcerror, FALSE);
  38. if (mc_event_grouplist != NULL)
  39. {
  40. mc_propagate_error (mcerror, 0, "%s", _("Event system already initialized"));
  41. return FALSE;
  42. }
  43. mc_event_grouplist =
  44. g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
  45. NULL, (GDestroyNotify) g_free, (GDestroyNotify) g_tree_destroy);
  46. if (mc_event_grouplist == NULL)
  47. {
  48. mc_propagate_error (mcerror, 0, "%s", _("Failed to initialize event system"));
  49. return FALSE;
  50. }
  51. return TRUE;
  52. }
  53. /* --------------------------------------------------------------------------------------------- */
  54. gboolean
  55. mc_event_deinit (GError **mcerror)
  56. {
  57. mc_return_val_if_error (mcerror, FALSE);
  58. if (mc_event_grouplist == NULL)
  59. {
  60. mc_propagate_error (mcerror, 0, "%s", _("Event system not initialized"));
  61. return FALSE;
  62. }
  63. g_tree_destroy (mc_event_grouplist);
  64. mc_event_grouplist = NULL;
  65. return TRUE;
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. gboolean
  69. mc_event_mass_add (const event_init_t *events, GError **mcerror)
  70. {
  71. size_t array_index;
  72. mc_return_val_if_error (mcerror, FALSE);
  73. for (array_index = 0; events[array_index].event_group_name != NULL; array_index++)
  74. {
  75. if (!mc_event_add (events[array_index].event_group_name,
  76. events[array_index].event_name,
  77. events[array_index].cb, events[array_index].init_data, mcerror))
  78. {
  79. return FALSE;
  80. }
  81. }
  82. return TRUE;
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. gboolean
  86. mc_event_present (const gchar *event_group_name, const gchar *event_name)
  87. {
  88. GTree *event_group;
  89. GPtrArray *callbacks;
  90. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
  91. return FALSE;
  92. event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
  93. if (event_group == NULL)
  94. return FALSE;
  95. callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
  96. if (callbacks == NULL)
  97. return FALSE;
  98. return TRUE;
  99. }
  100. /* --------------------------------------------------------------------------------------------- */