root/lib/event/manage.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_event_group_destroy_value
  2. mc_event_add
  3. mc_event_del
  4. mc_event_destroy
  5. mc_event_group_del
  6. mc_event_get_event_group_by_name
  7. mc_event_get_event_by_name
  8. mc_event_is_callback_in_array

   1 /*
   2    Handle any events in application.
   3    Manage events: add, delete, destroy, search
   4 
   5    Copyright (C) 2011-2024
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Slava Zanko <slavazanko@gmail.com>, 2011.
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25  */
  26 
  27 #include <config.h>
  28 
  29 #include "lib/global.h"
  30 #include "lib/util.h"
  31 #include "lib/event.h"
  32 
  33 #include "internal.h"
  34 
  35 /*** global variables ****************************************************************************/
  36 
  37 /*** file scope macro definitions ****************************************************************/
  38 
  39 /*** file scope type declarations ****************************************************************/
  40 
  41 /*** forward declarations (file scope functions) *************************************************/
  42 
  43 /*** file scope variables ************************************************************************/
  44 
  45 /* --------------------------------------------------------------------------------------------- */
  46 /*** file scope functions ************************************************************************/
  47 /* --------------------------------------------------------------------------------------------- */
  48 
  49 static void
  50 mc_event_group_destroy_value (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  51 {
  52     g_ptr_array_free ((GPtrArray *) data, TRUE);
  53 }
  54 
  55 /* --------------------------------------------------------------------------------------------- */
  56 /*** public functions ****************************************************************************/
  57 /* --------------------------------------------------------------------------------------------- */
  58 
  59 gboolean
  60 mc_event_add (const gchar *event_group_name, const gchar *event_name,
     /* [previous][next][first][last][top][bottom][index][help]  */
  61               mc_event_callback_func_t event_callback, gpointer event_init_data, GError **mcerror)
  62 {
  63     GTree *event_group;
  64     GPtrArray *callbacks;
  65     mc_event_callback_t *cb;
  66 
  67     mc_return_val_if_error (mcerror, FALSE);
  68 
  69     if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
  70         || event_callback == NULL)
  71     {
  72         mc_propagate_error (mcerror, 0, "%s", _("Check input data! Some of parameters are NULL!"));
  73         return FALSE;
  74     }
  75 
  76     event_group = mc_event_get_event_group_by_name (event_group_name, TRUE, mcerror);
  77     if (event_group == NULL)
  78         return FALSE;
  79 
  80     callbacks = mc_event_get_event_by_name (event_group, event_name, TRUE, mcerror);
  81     if (callbacks == NULL)
  82         return FALSE;
  83 
  84     cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
  85     if (cb == NULL)
  86     {
  87         cb = g_new0 (mc_event_callback_t, 1);
  88         cb->callback = event_callback;
  89         g_ptr_array_add (callbacks, (gpointer) cb);
  90     }
  91     cb->init_data = event_init_data;
  92     return TRUE;
  93 }
  94 
  95 /* --------------------------------------------------------------------------------------------- */
  96 
  97 void
  98 mc_event_del (const gchar *event_group_name, const gchar *event_name,
     /* [previous][next][first][last][top][bottom][index][help]  */
  99               mc_event_callback_func_t event_callback, gpointer event_init_data)
 100 {
 101     GTree *event_group;
 102     GPtrArray *callbacks;
 103     mc_event_callback_t *cb;
 104 
 105     if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
 106         || event_callback == NULL)
 107         return;
 108 
 109     event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
 110     if (event_group == NULL)
 111         return;
 112 
 113     callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
 114     if (callbacks == NULL)
 115         return;
 116 
 117     cb = mc_event_is_callback_in_array (callbacks, event_callback, event_init_data);
 118     if (cb != NULL)
 119         g_ptr_array_remove (callbacks, (gpointer) cb);
 120 }
 121 
 122 /* --------------------------------------------------------------------------------------------- */
 123 
 124 void
 125 mc_event_destroy (const gchar *event_group_name, const gchar *event_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 126 {
 127     GTree *event_group;
 128 
 129     if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
 130         return;
 131 
 132     event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
 133     g_tree_remove (event_group, (gconstpointer) event_name);
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 void
 139 mc_event_group_del (const gchar *event_group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 {
 141 
 142     if (mc_event_grouplist != NULL && event_group_name != NULL)
 143         g_tree_remove (mc_event_grouplist, (gconstpointer) event_group_name);
 144 }
 145 
 146 /* --------------------------------------------------------------------------------------------- */
 147 
 148 GTree *
 149 mc_event_get_event_group_by_name (const gchar *event_group_name, gboolean create_new,
     /* [previous][next][first][last][top][bottom][index][help]  */
 150                                   GError **mcerror)
 151 {
 152     GTree *event_group;
 153 
 154     mc_return_val_if_error (mcerror, FALSE);
 155 
 156     event_group = (GTree *) g_tree_lookup (mc_event_grouplist, (gconstpointer) event_group_name);
 157     if (event_group == NULL && create_new)
 158     {
 159         event_group =
 160             g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
 161                              NULL,
 162                              (GDestroyNotify) g_free,
 163                              (GDestroyNotify) mc_event_group_destroy_value);
 164         if (event_group == NULL)
 165         {
 166             mc_propagate_error (mcerror, 0, _("Unable to create group '%s' for events!"),
 167                                 event_group_name);
 168             return NULL;
 169         }
 170         g_tree_insert (mc_event_grouplist, g_strdup (event_group_name), (gpointer) event_group);
 171     }
 172     return event_group;
 173 }
 174 
 175 /* --------------------------------------------------------------------------------------------- */
 176 
 177 GPtrArray *
 178 mc_event_get_event_by_name (GTree *event_group, const gchar *event_name, gboolean create_new,
     /* [previous][next][first][last][top][bottom][index][help]  */
 179                             GError **mcerror)
 180 {
 181     GPtrArray *callbacks;
 182 
 183     mc_return_val_if_error (mcerror, FALSE);
 184 
 185     callbacks = (GPtrArray *) g_tree_lookup (event_group, (gconstpointer) event_name);
 186     if (callbacks == NULL && create_new)
 187     {
 188         callbacks = g_ptr_array_new_with_free_func (g_free);
 189         if (callbacks == NULL)
 190         {
 191             mc_propagate_error (mcerror, 0, _("Unable to create event '%s'!"), event_name);
 192             return NULL;
 193         }
 194         g_tree_insert (event_group, g_strdup (event_name), (gpointer) callbacks);
 195     }
 196     return callbacks;
 197 }
 198 
 199 /* --------------------------------------------------------------------------------------------- */
 200 
 201 mc_event_callback_t *
 202 mc_event_is_callback_in_array (GPtrArray *callbacks, mc_event_callback_func_t event_callback,
     /* [previous][next][first][last][top][bottom][index][help]  */
 203                                gpointer event_init_data)
 204 {
 205     guint array_index;
 206 
 207     for (array_index = 0; array_index < callbacks->len; array_index++)
 208     {
 209         mc_event_callback_t *cb = g_ptr_array_index (callbacks, array_index);
 210         if (cb->callback == event_callback && cb->init_data == event_init_data)
 211             return cb;
 212     }
 213     return NULL;
 214 }
 215 
 216 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */