event.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 = g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp, NULL,
  44. (GDestroyNotify) g_free, (GDestroyNotify) g_tree_destroy);
  45. if (mc_event_grouplist == NULL)
  46. {
  47. mc_propagate_error (mcerror, 0, "%s", _ ("Failed to initialize event system"));
  48. return FALSE;
  49. }
  50. return TRUE;
  51. }
  52. /* --------------------------------------------------------------------------------------------- */
  53. gboolean
  54. mc_event_deinit (GError **mcerror)
  55. {
  56. mc_return_val_if_error (mcerror, FALSE);
  57. if (mc_event_grouplist == NULL)
  58. {
  59. mc_propagate_error (mcerror, 0, "%s", _ ("Event system not initialized"));
  60. return FALSE;
  61. }
  62. g_tree_destroy (mc_event_grouplist);
  63. mc_event_grouplist = NULL;
  64. return TRUE;
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. gboolean
  68. mc_event_mass_add (const event_init_t *events, GError **mcerror)
  69. {
  70. size_t array_index;
  71. mc_return_val_if_error (mcerror, FALSE);
  72. for (array_index = 0; events[array_index].event_group_name != NULL; array_index++)
  73. {
  74. if (!mc_event_add (events[array_index].event_group_name, events[array_index].event_name,
  75. events[array_index].cb, events[array_index].init_data, mcerror))
  76. {
  77. return FALSE;
  78. }
  79. }
  80. return TRUE;
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. gboolean
  84. mc_event_present (const gchar *event_group_name, const gchar *event_name)
  85. {
  86. GTree *event_group;
  87. GPtrArray *callbacks;
  88. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
  89. return FALSE;
  90. event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
  91. if (event_group == NULL)
  92. return FALSE;
  93. callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
  94. if (callbacks == NULL)
  95. return FALSE;
  96. return TRUE;
  97. }
  98. /* --------------------------------------------------------------------------------------------- */