event.c 4.6 KB

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