comparison 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
comparison
equal deleted inserted replaced
5863:98ec3e394f59 5864:417b1001d2b1
1 /** 1 /**
2 * @file pounce.h Buddy pounce API 2 * @file pounce.c Buddy Pounce API
3 * 3 *
4 * gaim 4 * gaim
5 * 5 *
6 * Copyright (C) 2003, Christian Hammond <chipx86@gnupdate.org> 6 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org>
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
21 * 21 *
22 */ 22 */
23 #include <string.h> 23 #include <string.h>
24 #include "gaim.h" 24 #include "gaim.h"
25 25
26 typedef struct
27 {
28 char *name;
29
30 gboolean enabled;
31
32 GHashTable *atts;
33
34 } GaimPounceActionData;
35
36
26 static GList *pounces = NULL; 37 static GList *pounces = NULL;
27 38
39 static GaimPounceActionData *
40 find_action_data(const GaimPounce *pounce, const char *name)
41 {
42 GaimPounceActionData *action;
43
44 g_return_val_if_fail(pounce != NULL, NULL);
45 g_return_val_if_fail(name != NULL, NULL);
46
47 action = g_hash_table_lookup(pounce->actions, name);
48
49 return action;
50 }
51
52 static void
53 free_action_data(gpointer data)
54 {
55 GaimPounceActionData *action_data = data;
56
57 g_free(action_data->name);
58
59 g_hash_table_destroy(action_data->atts);
60
61 g_free(action_data);
62 }
63
28 GaimPounce * 64 GaimPounce *
29 gaim_pounce_new(GaimAccount *pouncer, const char *pouncee, 65 gaim_pounce_new(const char *ui_type, GaimAccount *pouncer,
30 GaimPounceEvent event, GaimPounceCb cb, 66 const char *pouncee, GaimPounceEvent event,
31 void *data, void (*free)(void *)) 67 GaimPounceCb cb, void *data, void (*free)(void *))
32 { 68 {
33 GaimPounce *pounce; 69 GaimPounce *pounce;
34 70
71 g_return_val_if_fail(ui_type != NULL, NULL);
35 g_return_val_if_fail(pouncer != NULL, NULL); 72 g_return_val_if_fail(pouncer != NULL, NULL);
36 g_return_val_if_fail(pouncee != NULL, NULL); 73 g_return_val_if_fail(pouncee != NULL, NULL);
37 g_return_val_if_fail(event != 0, NULL); 74 g_return_val_if_fail(event != 0, NULL);
38 g_return_val_if_fail(cb != NULL, NULL); 75 g_return_val_if_fail(cb != NULL, NULL);
39 76
40 pounce = g_new0(GaimPounce, 1); 77 pounce = g_new0(GaimPounce, 1);
41 78
79 pounce->ui_type = g_strdup(ui_type);
42 pounce->pouncer = pouncer; 80 pounce->pouncer = pouncer;
43 pounce->pouncee = g_strdup(pouncee); 81 pounce->pouncee = g_strdup(pouncee);
44 pounce->events = event; 82 pounce->events = event;
45 pounce->callback = cb; 83 pounce->callback = cb;
46 pounce->data = data; 84 pounce->data = data;
47 pounce->free = free; 85 pounce->free = free;
48 86
87 pounce->actions = g_hash_table_new_full(g_str_hash, g_str_equal,
88 g_free, free_action_data);
89
49 pounces = g_list_append(pounces, pounce); 90 pounces = g_list_append(pounces, pounce);
50 91
51 return pounce; 92 return pounce;
52 } 93 }
53 94
54 void 95 void
55 gaim_pounce_destroy(GaimPounce *pounce) 96 gaim_pounce_destroy(GaimPounce *pounce)
56 { 97 {
57 g_return_if_fail(pounce != NULL); 98 g_return_if_fail(pounce != NULL);
99
100 pounces = g_list_remove(pounces, pounce);
101
102 if (pounce->ui_type != NULL) g_free(pounce->ui_type);
103 if (pounce->pouncee != NULL) g_free(pounce->pouncee);
104
105 g_hash_table_destroy(pounce->actions);
106
107 if (pounce->free != NULL && pounce->data != NULL)
108 pounce->free(pounce->data);
109
110 g_free(pounce);
111 }
112
113 void
114 gaim_pounce_set_events(GaimPounce *pounce, GaimPounceEvent events)
115 {
116 g_return_if_fail(pounce != NULL);
117 g_return_if_fail(pounce != GAIM_POUNCE_NONE);
118
119 pounce->events = events;
120 }
121
122 void
123 gaim_pounce_set_pouncer(GaimPounce *pounce, GaimAccount *pouncer)
124 {
125 g_return_if_fail(pounce != NULL);
126 g_return_if_fail(pouncer != NULL);
127
128 pounce->pouncer = pouncer;
129 }
130
131 void
132 gaim_pounce_set_pouncee(GaimPounce *pounce, const char *pouncee)
133 {
134 g_return_if_fail(pounce != NULL);
135 g_return_if_fail(pouncee != NULL);
58 136
59 if (pounce->pouncee != NULL) 137 if (pounce->pouncee != NULL)
60 g_free(pounce->pouncee); 138 g_free(pounce->pouncee);
61 139
62 pounces = g_list_remove(pounces, pounce);
63
64 if (pounce->free != NULL && pounce->data != NULL)
65 pounce->free(pounce->data);
66
67 g_free(pounce);
68 }
69
70 void
71 gaim_pounce_set_events(GaimPounce *pounce, GaimPounceEvent events)
72 {
73 g_return_if_fail(pounce != NULL);
74 g_return_if_fail(pounce != GAIM_POUNCE_NONE);
75
76 pounce->events = events;
77 }
78
79 void
80 gaim_pounce_set_pouncer(GaimPounce *pounce, GaimAccount *pouncer)
81 {
82 g_return_if_fail(pounce != NULL);
83 g_return_if_fail(pouncer != NULL);
84
85 pounce->pouncer = pouncer;
86 }
87
88 void
89 gaim_pounce_set_pouncee(GaimPounce *pounce, const char *pouncee)
90 {
91 g_return_if_fail(pounce != NULL);
92 g_return_if_fail(pouncee != NULL);
93
94 if (pounce->pouncee != NULL)
95 g_free(pounce->pouncee);
96
97 pounce->pouncee = (pouncee == NULL ? NULL : g_strdup(pouncee)); 140 pounce->pouncee = (pouncee == NULL ? NULL : g_strdup(pouncee));
141 }
142
143 void
144 gaim_pounce_set_save(GaimPounce *pounce, gboolean save)
145 {
146 g_return_if_fail(pounce != NULL);
147
148 pounce->save = save;
149 }
150
151 void
152 gaim_pounce_action_register(GaimPounce *pounce, const char *name)
153 {
154 GaimPounceActionData *action_data;
155
156 g_return_if_fail(pounce != NULL);
157 g_return_if_fail(name != NULL);
158
159 action_data = g_new0(GaimPounceActionData, 1);
160
161 action_data->name = g_strdup(name);
162 action_data->enabled = FALSE;
163 action_data->atts = g_hash_table_new_full(g_str_hash, g_str_equal,
164 g_free, g_free);
165
166 g_hash_table_insert(pounce->actions, g_strdup(name), action_data);
167 }
168
169 void
170 gaim_pounce_action_set_enabled(GaimPounce *pounce, const char *action,
171 gboolean enabled)
172 {
173 GaimPounceActionData *action_data;
174
175 g_return_if_fail(pounce != NULL);
176 g_return_if_fail(action != NULL);
177
178 action_data = find_action_data(pounce, action);
179
180 g_return_if_fail(action_data != NULL);
181
182 action_data->enabled = enabled;
183 }
184
185 void
186 gaim_pounce_action_set_attribute(GaimPounce *pounce, const char *action,
187 const char *attr, const char *value)
188 {
189 GaimPounceActionData *action_data;
190
191 g_return_if_fail(pounce != NULL);
192 g_return_if_fail(action != NULL);
193 g_return_if_fail(attr != NULL);
194
195 action_data = find_action_data(pounce, action);
196
197 g_return_if_fail(action_data != NULL);
198
199 if (value == NULL)
200 g_hash_table_remove(action_data->atts, attr);
201 else
202 g_hash_table_insert(action_data->atts, g_strdup(attr),
203 g_strdup(value));
98 } 204 }
99 205
100 void 206 void
101 gaim_pounce_set_data(GaimPounce *pounce, void *data) 207 gaim_pounce_set_data(GaimPounce *pounce, void *data)
102 { 208 {
127 g_return_val_if_fail(pounce != NULL, NULL); 233 g_return_val_if_fail(pounce != NULL, NULL);
128 234
129 return pounce->pouncee; 235 return pounce->pouncee;
130 } 236 }
131 237
238 gboolean
239 gaim_pounce_get_save(const GaimPounce *pounce)
240 {
241 g_return_val_if_fail(pounce != NULL, FALSE);
242
243 return pounce->save;
244 }
245
246 gboolean
247 gaim_pounce_action_is_enabled(const GaimPounce *pounce, const char *action)
248 {
249 GaimPounceActionData *action_data;
250
251 g_return_val_if_fail(pounce != NULL, FALSE);
252 g_return_val_if_fail(action != NULL, FALSE);
253
254 action_data = find_action_data(pounce, action);
255
256 g_return_val_if_fail(action_data != NULL, FALSE);
257
258 return action_data->enabled;
259 }
260
261 const char *
262 gaim_pounce_action_get_attribute(const GaimPounce *pounce,
263 const char *action, const char *attr)
264 {
265 GaimPounceActionData *action_data;
266
267 g_return_val_if_fail(pounce != NULL, NULL);
268 g_return_val_if_fail(action != NULL, NULL);
269 g_return_val_if_fail(attr != NULL, NULL);
270
271 action_data = find_action_data(pounce, action);
272
273 g_return_val_if_fail(action_data != NULL, NULL);
274
275 return g_hash_table_lookup(action_data->atts, attr);
276 }
277
132 void * 278 void *
133 gaim_pounce_get_data(const GaimPounce *pounce) 279 gaim_pounce_get_data(const GaimPounce *pounce)
134 { 280 {
135 g_return_val_if_fail(pounce != NULL, NULL); 281 g_return_val_if_fail(pounce != NULL, NULL);
136 282
140 void 286 void
141 gaim_pounce_execute(const GaimAccount *pouncer, const char *pouncee, 287 gaim_pounce_execute(const GaimAccount *pouncer, const char *pouncee,
142 GaimPounceEvent events) 288 GaimPounceEvent events)
143 { 289 {
144 GaimPounce *pounce; 290 GaimPounce *pounce;
145 GList *l; 291 GList *l, *l_next;
146 292
147 g_return_if_fail(pouncer != NULL); 293 g_return_if_fail(pouncer != NULL);
148 g_return_if_fail(pouncee != NULL); 294 g_return_if_fail(pouncee != NULL);
149 g_return_if_fail(events != GAIM_POUNCE_NONE); 295 g_return_if_fail(events != GAIM_POUNCE_NONE);
150 296
151 for (l = gaim_get_pounces(); l != NULL; l = l->next) { 297 for (l = gaim_get_pounces(); l != NULL; l = l_next) {
152 pounce = (GaimPounce *)l->data; 298 pounce = (GaimPounce *)l->data;
299 l_next = l->next;
153 300
154 if ((gaim_pounce_get_events(pounce) & events) && 301 if ((gaim_pounce_get_events(pounce) & events) &&
155 (gaim_pounce_get_pouncer(pounce) == pouncer) && 302 (gaim_pounce_get_pouncer(pounce) == pouncer) &&
156 !strcmp(gaim_pounce_get_pouncee(pounce), pouncee)) { 303 !strcmp(gaim_pounce_get_pouncee(pounce), pouncee)) {
157 304
158 if (pounce->callback != NULL) 305 if (pounce->callback != NULL) {
159 pounce->callback(pounce, events, gaim_pounce_get_data(pounce)); 306 pounce->callback(pounce, events, gaim_pounce_get_data(pounce));
307
308 if (!gaim_pounce_get_save(pounce))
309 gaim_pounce_destroy(pounce);
310 }
160 } 311 }
161 } 312 }
162 } 313 }
163 314
164 GaimPounce * 315 GaimPounce *