diff src/pounce.c @ 5864:417b1001d2b1

[gaim-migrate @ 6295] Rewrote the pounce API again. Now it's even MORE core/UI-split, and will allow for loading/saving. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Sat, 14 Jun 2003 11:14:49 +0000
parents 2fa4aa9c1885
children d6b5cab288bb
line wrap: on
line diff
--- a/src/pounce.c	Sat Jun 14 11:14:26 2003 +0000
+++ b/src/pounce.c	Sat Jun 14 11:14:49 2003 +0000
@@ -1,9 +1,9 @@
 /**
- * @file pounce.h Buddy pounce API
+ * @file pounce.c Buddy Pounce API
  *
  * gaim
  *
- * Copyright (C) 2003, Christian Hammond <chipx86@gnupdate.org>
+ * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org>
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -23,15 +23,52 @@
 #include <string.h>
 #include "gaim.h"
 
+typedef struct
+{
+	char *name;
+
+	gboolean enabled;
+
+	GHashTable *atts;
+
+} GaimPounceActionData;
+
+
 static GList *pounces = NULL;
 
+static GaimPounceActionData *
+find_action_data(const GaimPounce *pounce, const char *name)
+{
+	GaimPounceActionData *action;
+
+	g_return_val_if_fail(pounce != NULL, NULL);
+	g_return_val_if_fail(name   != NULL, NULL);
+
+	action = g_hash_table_lookup(pounce->actions, name);
+
+	return action;
+}
+
+static void
+free_action_data(gpointer data)
+{
+	GaimPounceActionData *action_data = data;
+
+	g_free(action_data->name);
+
+	g_hash_table_destroy(action_data->atts);
+
+	g_free(action_data);
+}
+
 GaimPounce *
-gaim_pounce_new(GaimAccount *pouncer, const char *pouncee,
-				GaimPounceEvent event, GaimPounceCb cb,
-				void *data, void (*free)(void *))
+gaim_pounce_new(const char *ui_type, GaimAccount *pouncer,
+				const char *pouncee, GaimPounceEvent event,
+				GaimPounceCb cb, void *data, void (*free)(void *))
 {
 	GaimPounce *pounce;
 
+	g_return_val_if_fail(ui_type != NULL, NULL);
 	g_return_val_if_fail(pouncer != NULL, NULL);
 	g_return_val_if_fail(pouncee != NULL, NULL);
 	g_return_val_if_fail(event   != 0,    NULL);
@@ -39,6 +76,7 @@
 
 	pounce = g_new0(GaimPounce, 1);
 
+	pounce->ui_type  = g_strdup(ui_type);
 	pounce->pouncer  = pouncer;
 	pounce->pouncee  = g_strdup(pouncee);
 	pounce->events   = event;
@@ -46,6 +84,9 @@
 	pounce->data     = data;
 	pounce->free     = free;
 
+	pounce->actions  = g_hash_table_new_full(g_str_hash, g_str_equal,
+											 g_free, free_action_data);
+
 	pounces = g_list_append(pounces, pounce);
 
 	return pounce;
@@ -56,10 +97,12 @@
 {
 	g_return_if_fail(pounce != NULL);
 
-	if (pounce->pouncee != NULL)
-		g_free(pounce->pouncee);
+	pounces = g_list_remove(pounces, pounce);
 
-	pounces = g_list_remove(pounces, pounce);
+	if (pounce->ui_type != NULL) g_free(pounce->ui_type);
+	if (pounce->pouncee != NULL) g_free(pounce->pouncee);
+
+	g_hash_table_destroy(pounce->actions);
 
 	if (pounce->free != NULL && pounce->data != NULL)
 		pounce->free(pounce->data);
@@ -98,6 +141,69 @@
 }
 
 void
+gaim_pounce_set_save(GaimPounce *pounce, gboolean save)
+{
+	g_return_if_fail(pounce != NULL);
+
+	pounce->save = save;
+}
+
+void
+gaim_pounce_action_register(GaimPounce *pounce, const char *name)
+{
+	GaimPounceActionData *action_data;
+
+	g_return_if_fail(pounce != NULL);
+	g_return_if_fail(name   != NULL);
+
+	action_data = g_new0(GaimPounceActionData, 1);
+
+	action_data->name    = g_strdup(name);
+	action_data->enabled = FALSE;
+	action_data->atts    = g_hash_table_new_full(g_str_hash, g_str_equal,
+												 g_free, g_free);
+
+	g_hash_table_insert(pounce->actions, g_strdup(name), action_data);
+}
+
+void
+gaim_pounce_action_set_enabled(GaimPounce *pounce, const char *action,
+							   gboolean enabled)
+{
+	GaimPounceActionData *action_data;
+
+	g_return_if_fail(pounce != NULL);
+	g_return_if_fail(action != NULL);
+
+	action_data = find_action_data(pounce, action);
+
+	g_return_if_fail(action_data != NULL);
+
+	action_data->enabled = enabled;
+}
+
+void
+gaim_pounce_action_set_attribute(GaimPounce *pounce, const char *action,
+								 const char *attr, const char *value)
+{
+	GaimPounceActionData *action_data;
+
+	g_return_if_fail(pounce != NULL);
+	g_return_if_fail(action != NULL);
+	g_return_if_fail(attr   != NULL);
+
+	action_data = find_action_data(pounce, action);
+
+	g_return_if_fail(action_data != NULL);
+
+	if (value == NULL)
+		g_hash_table_remove(action_data->atts, attr);
+	else
+		g_hash_table_insert(action_data->atts, g_strdup(attr),
+							g_strdup(value));
+}
+
+void
 gaim_pounce_set_data(GaimPounce *pounce, void *data)
 {
 	g_return_if_fail(pounce != NULL);
@@ -129,6 +235,46 @@
 	return pounce->pouncee;
 }
 
+gboolean
+gaim_pounce_get_save(const GaimPounce *pounce)
+{
+	g_return_val_if_fail(pounce != NULL, FALSE);
+
+	return pounce->save;
+}
+
+gboolean
+gaim_pounce_action_is_enabled(const GaimPounce *pounce, const char *action)
+{
+	GaimPounceActionData *action_data;
+
+	g_return_val_if_fail(pounce != NULL, FALSE);
+	g_return_val_if_fail(action != NULL, FALSE);
+
+	action_data = find_action_data(pounce, action);
+
+	g_return_val_if_fail(action_data != NULL, FALSE);
+
+	return action_data->enabled;
+}
+
+const char *
+gaim_pounce_action_get_attribute(const GaimPounce *pounce,
+								 const char *action, const char *attr)
+{
+	GaimPounceActionData *action_data;
+
+	g_return_val_if_fail(pounce != NULL, NULL);
+	g_return_val_if_fail(action != NULL, NULL);
+	g_return_val_if_fail(attr   != NULL, NULL);
+
+	action_data = find_action_data(pounce, action);
+
+	g_return_val_if_fail(action_data != NULL, NULL);
+
+	return g_hash_table_lookup(action_data->atts, attr);
+}
+
 void *
 gaim_pounce_get_data(const GaimPounce *pounce)
 {
@@ -142,21 +288,26 @@
 					GaimPounceEvent events)
 {
 	GaimPounce *pounce;
-	GList *l;
+	GList *l, *l_next;
 
 	g_return_if_fail(pouncer != NULL);
 	g_return_if_fail(pouncee != NULL);
 	g_return_if_fail(events  != GAIM_POUNCE_NONE);
 
-	for (l = gaim_get_pounces(); l != NULL; l = l->next) {
+	for (l = gaim_get_pounces(); l != NULL; l = l_next) {
 		pounce = (GaimPounce *)l->data;
+		l_next = l->next;
 
 		if ((gaim_pounce_get_events(pounce) & events) &&
 			(gaim_pounce_get_pouncer(pounce) == pouncer) &&
 			!strcmp(gaim_pounce_get_pouncee(pounce), pouncee)) {
 
-			if (pounce->callback != NULL)
+			if (pounce->callback != NULL) {
 				pounce->callback(pounce, events, gaim_pounce_get_data(pounce));
+
+				if (!gaim_pounce_get_save(pounce))
+					gaim_pounce_destroy(pounce);
+			}
 		}
 	}
 }