event.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Handle events in application.
  3. Interface functions: init/deinit; start/stop
  4. Copyright (C) 2011 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software; you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be
  13. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. MA 02110-1301, USA.
  20. */
  21. #include <config.h>
  22. #include "lib/global.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. if (mc_event_grouplist != NULL)
  38. {
  39. g_propagate_error (mcerror,
  40. g_error_new (MC_ERROR, 1,
  41. _("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. g_propagate_error (mcerror,
  50. g_error_new (MC_ERROR, 2,
  51. _("Failed to initialize event system")));
  52. return FALSE;
  53. }
  54. return TRUE;
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. gboolean
  58. mc_event_deinit (GError ** mcerror)
  59. {
  60. if (mc_event_grouplist == NULL)
  61. {
  62. g_propagate_error (mcerror,
  63. g_error_new (MC_ERROR, 1,
  64. _("Event system not initialized")));
  65. return FALSE;
  66. }
  67. g_tree_destroy (mc_event_grouplist);
  68. mc_event_grouplist = NULL;
  69. return TRUE;
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. gboolean
  73. mc_event_mass_add (event_init_t * events, GError ** mcerror)
  74. {
  75. size_t array_index;
  76. for (array_index = 0; events[array_index].event_group_name != NULL; array_index++)
  77. {
  78. if (!mc_event_add (events[array_index].event_group_name,
  79. events[array_index].event_name,
  80. events[array_index].cb, events[array_index].init_data, mcerror))
  81. {
  82. return FALSE;
  83. }
  84. }
  85. return TRUE;
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. gboolean
  89. mc_event_present (const gchar *event_group_name, const gchar *event_name)
  90. {
  91. GTree *event_group;
  92. GPtrArray *callbacks;
  93. if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
  94. return FALSE;
  95. event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
  96. if (event_group == NULL)
  97. return FALSE;
  98. callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
  99. if (callbacks == NULL)
  100. return FALSE;
  101. return TRUE;
  102. }
  103. /* --------------------------------------------------------------------------------------------- */