event.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Handle events in application.
  3. Interface functions: init/deinit; start/stop
  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. GTree *mc_event_grouplist = NULL;
  29. /*** file scope functions ************************************************************************/
  30. /* --------------------------------------------------------------------------------------------- */
  31. /*** public functions ****************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. gboolean
  34. mc_event_init (GError ** mcerror)
  35. {
  36. if (mc_event_grouplist != NULL)
  37. {
  38. g_propagate_error (mcerror,
  39. g_error_new (MC_ERROR, 1, _("Event system already initialized")));
  40. return FALSE;
  41. }
  42. mc_event_grouplist =
  43. g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
  44. NULL, (GDestroyNotify) g_free, (GDestroyNotify) g_tree_destroy);
  45. if (mc_event_grouplist == NULL)
  46. {
  47. g_propagate_error (mcerror,
  48. g_error_new (MC_ERROR, 2, _("Failed to initialize event system")));
  49. return FALSE;
  50. }
  51. return TRUE;
  52. }
  53. /* --------------------------------------------------------------------------------------------- */
  54. gboolean
  55. mc_event_deinit (GError ** mcerror)
  56. {
  57. if (mc_event_grouplist == NULL)
  58. {
  59. g_propagate_error (mcerror, g_error_new (MC_ERROR, 1, _("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 (event_init_t * events, GError ** mcerror)
  69. {
  70. size_t array_index;
  71. for (array_index = 0; events[array_index].event_group_name != NULL; array_index++)
  72. {
  73. if (!mc_event_add (events[array_index].event_group_name,
  74. 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. /* --------------------------------------------------------------------------------------------- */