0.9.3claws23
[claws.git] / src / common / hooks.c
index 634f705cee4dbb696ccf866d035c0abcf504c8d1..cfca1b3f6bf961a7fb11b271bce70c6e9eab6083 100644 (file)
 
 #include <glib.h>
 
+#include "utils.h"
 #include "hooks.h"
 
-GHashTable *hooklist_table;
+static GHashTable *hooklist_table;
 
 GHookList *hooks_get_hooklist(gchar *hooklist_name)
 {
@@ -45,33 +46,49 @@ GHookList *hooks_get_hooklist(gchar *hooklist_name)
        return hooklist;
 }
 
-gint hooks_register_hook(gchar *hooklist_name,
-                        SylpheedHookFunction hook_func,
-                        gpointer userdata)
+guint hooks_register_hook(gchar *hooklist_name,
+                         SylpheedHookFunction hook_func,
+                         gpointer userdata)
 {
        GHookList *hooklist;
        GHook *hook;
 
-       g_return_val_if_fail(hooklist_name != NULL, -1);
-       g_return_val_if_fail(hook_func != NULL, -1);
+       g_return_val_if_fail(hooklist_name != NULL, (guint)-1);
+       g_return_val_if_fail(hook_func != NULL, (guint)-1);
        
        hooklist = hooks_get_hooklist(hooklist_name);
-       g_return_val_if_fail(hooklist != NULL, -1);
+       g_return_val_if_fail(hooklist != NULL, (guint)-1);
 
        hook = g_hook_alloc(hooklist);
-       g_return_val_if_fail(hook != NULL, -1);
+       g_return_val_if_fail(hook != NULL, (guint)-1);
 
        hook->func = hook_func;
        hook->data = userdata;
 
        g_hook_append(hooklist, hook);
 
+       debug_print("registed new hook for '%s' as id %d\n", hooklist_name, hook->hook_id);
+
        return hook->hook_id;
 }
 
 void hooks_unregister_hook(gchar *hooklist_name,
                           guint hook_id)
 {
+       GHookList *hooklist;
+       GHook *hook;
+
+       g_return_if_fail(hooklist_name != NULL);
+
+       hooklist = hooks_get_hooklist(hooklist_name);
+       g_return_if_fail(hooklist != NULL);
+
+       hook = g_hook_get(hooklist, hook_id);
+       g_return_if_fail(hook != NULL);
+
+       debug_print("unregisted hook %d in '%s'\n", hook->hook_id, hooklist_name);
+
+       g_hook_destroy(hooklist, hook);
 }
 
 struct MarshalData
@@ -91,19 +108,21 @@ static void hooks_marshal(GHook *hook, gpointer data)
        }
 }
 
-void hooks_invoke(gchar *hooklist_name,
+gboolean hooks_invoke(gchar *hooklist_name,
                  gpointer source)
 {
        GHookList *hooklist;
        struct MarshalData marshal_data;
        
-       g_return_if_fail(hooklist_name != NULL);
+       g_return_val_if_fail(hooklist_name != NULL, FALSE);
 
        hooklist = hooks_get_hooklist(hooklist_name);
-       g_return_if_fail(hooklist != NULL);
+       g_return_val_if_fail(hooklist != NULL, FALSE);
 
        marshal_data.source = source;
        marshal_data.abort = FALSE;
 
        g_hook_list_marshal(hooklist, TRUE, hooks_marshal, &marshal_data);
+
+       return marshal_data.abort;
 }