Mercurial > pidgin
annotate libgaim/blist.c @ 14696:34083fe39891
[gaim-migrate @ 17448]
From Paul Betts, NetworkManager integration. This will smartly disconnect and reconnect
accounts according to network availability. Probably needs some more tweaking.
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Sun, 08 Oct 2006 08:40:09 +0000 |
parents | 11fd4148f9da |
children | 4124030c3f3a |
rev | line source |
---|---|
14192 | 1 /* |
2 * gaim | |
3 * | |
4 * Gaim is the legal property of its developers, whose names are too numerous | |
5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
6 * source distribution. | |
7 * | |
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 | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 #include "internal.h" | |
24 #include "blist.h" | |
25 #include "conversation.h" | |
26 #include "dbus-maybe.h" | |
27 #include "debug.h" | |
28 #include "notify.h" | |
29 #include "prefs.h" | |
30 #include "privacy.h" | |
31 #include "prpl.h" | |
32 #include "server.h" | |
33 #include "signals.h" | |
34 #include "util.h" | |
35 #include "value.h" | |
36 #include "xmlnode.h" | |
37 | |
38 #define PATHSIZE 1024 | |
39 | |
40 static GaimBlistUiOps *blist_ui_ops = NULL; | |
41 | |
42 static GaimBuddyList *gaimbuddylist = NULL; | |
43 static guint save_timer = 0; | |
44 static gboolean blist_loaded = FALSE; | |
45 | |
46 | |
47 /********************************************************************* | |
48 * Private utility functions * | |
49 *********************************************************************/ | |
50 | |
51 static GaimBlistNode *gaim_blist_get_last_sibling(GaimBlistNode *node) | |
52 { | |
53 GaimBlistNode *n = node; | |
54 if (!n) | |
55 return NULL; | |
56 while (n->next) | |
57 n = n->next; | |
58 return n; | |
59 } | |
60 | |
61 static GaimBlistNode *gaim_blist_get_last_child(GaimBlistNode *node) | |
62 { | |
63 if (!node) | |
64 return NULL; | |
65 return gaim_blist_get_last_sibling(node->child); | |
66 } | |
67 | |
68 struct _gaim_hbuddy { | |
69 char *name; | |
70 GaimAccount *account; | |
71 GaimBlistNode *group; | |
72 }; | |
73 | |
74 static guint _gaim_blist_hbuddy_hash(struct _gaim_hbuddy *hb) | |
75 { | |
76 return g_str_hash(hb->name); | |
77 } | |
78 | |
79 static guint _gaim_blist_hbuddy_equal(struct _gaim_hbuddy *hb1, struct _gaim_hbuddy *hb2) | |
80 { | |
81 return ((!strcmp(hb1->name, hb2->name)) && hb1->account == hb2->account && hb1->group == hb2->group); | |
82 } | |
83 | |
84 static void _gaim_blist_hbuddy_free_key(struct _gaim_hbuddy *hb) | |
85 { | |
86 g_free(hb->name); | |
87 g_free(hb); | |
88 } | |
89 | |
90 | |
91 /********************************************************************* | |
92 * Writing to disk * | |
93 *********************************************************************/ | |
94 | |
95 static void | |
96 value_to_xmlnode(gpointer key, gpointer hvalue, gpointer user_data) | |
97 { | |
98 const char *name; | |
99 GaimValue *value; | |
100 xmlnode *node, *child; | |
101 char buf[20]; | |
102 | |
103 name = (const char *)key; | |
104 value = (GaimValue *)hvalue; | |
105 node = (xmlnode *)user_data; | |
106 | |
107 g_return_if_fail(value != NULL); | |
108 | |
109 child = xmlnode_new_child(node, "setting"); | |
110 xmlnode_set_attrib(child, "name", name); | |
111 | |
112 if (gaim_value_get_type(value) == GAIM_TYPE_INT) { | |
113 xmlnode_set_attrib(child, "type", "int"); | |
114 snprintf(buf, sizeof(buf), "%d", gaim_value_get_int(value)); | |
115 xmlnode_insert_data(child, buf, -1); | |
116 } | |
117 else if (gaim_value_get_type(value) == GAIM_TYPE_STRING) { | |
118 xmlnode_set_attrib(child, "type", "string"); | |
119 xmlnode_insert_data(child, gaim_value_get_string(value), -1); | |
120 } | |
121 else if (gaim_value_get_type(value) == GAIM_TYPE_BOOLEAN) { | |
122 xmlnode_set_attrib(child, "type", "bool"); | |
123 snprintf(buf, sizeof(buf), "%d", gaim_value_get_boolean(value)); | |
124 xmlnode_insert_data(child, buf, -1); | |
125 } | |
126 } | |
127 | |
128 static void | |
129 chat_component_to_xmlnode(gpointer key, gpointer value, gpointer user_data) | |
130 { | |
131 const char *name; | |
132 const char *data; | |
133 xmlnode *node, *child; | |
134 | |
135 name = (const char *)key; | |
136 data = (const char *)value; | |
137 node = (xmlnode *)user_data; | |
138 | |
139 g_return_if_fail(data != NULL); | |
140 | |
141 child = xmlnode_new_child(node, "component"); | |
142 xmlnode_set_attrib(child, "name", name); | |
143 xmlnode_insert_data(child, data, -1); | |
144 } | |
145 | |
146 static xmlnode * | |
147 buddy_to_xmlnode(GaimBlistNode *bnode) | |
148 { | |
149 xmlnode *node, *child; | |
150 GaimBuddy *buddy; | |
151 | |
152 buddy = (GaimBuddy *)bnode; | |
153 | |
154 node = xmlnode_new("buddy"); | |
155 xmlnode_set_attrib(node, "account", gaim_account_get_username(buddy->account)); | |
156 xmlnode_set_attrib(node, "proto", gaim_account_get_protocol_id(buddy->account)); | |
157 | |
158 child = xmlnode_new_child(node, "name"); | |
159 xmlnode_insert_data(child, buddy->name, -1); | |
160 | |
161 if (buddy->alias != NULL) | |
162 { | |
163 child = xmlnode_new_child(node, "alias"); | |
164 xmlnode_insert_data(child, buddy->alias, -1); | |
165 } | |
166 | |
167 /* Write buddy settings */ | |
168 g_hash_table_foreach(buddy->node.settings, value_to_xmlnode, node); | |
169 | |
170 return node; | |
171 } | |
172 | |
173 static xmlnode * | |
174 contact_to_xmlnode(GaimBlistNode *cnode) | |
175 { | |
176 xmlnode *node, *child; | |
177 GaimContact *contact; | |
178 GaimBlistNode *bnode; | |
179 | |
180 contact = (GaimContact *)cnode; | |
181 | |
182 node = xmlnode_new("contact"); | |
183 | |
184 if (contact->alias != NULL) | |
185 { | |
186 xmlnode_set_attrib(node, "alias", contact->alias); | |
187 } | |
188 | |
189 /* Write buddies */ | |
190 for (bnode = cnode->child; bnode != NULL; bnode = bnode->next) | |
191 { | |
192 if (!GAIM_BLIST_NODE_SHOULD_SAVE(bnode)) | |
193 continue; | |
194 if (GAIM_BLIST_NODE_IS_BUDDY(bnode)) | |
195 { | |
196 child = buddy_to_xmlnode(bnode); | |
197 xmlnode_insert_child(node, child); | |
198 } | |
199 } | |
200 | |
201 /* Write contact settings */ | |
202 g_hash_table_foreach(cnode->settings, value_to_xmlnode, node); | |
203 | |
204 return node; | |
205 } | |
206 | |
207 static xmlnode * | |
208 chat_to_xmlnode(GaimBlistNode *cnode) | |
209 { | |
210 xmlnode *node, *child; | |
211 GaimChat *chat; | |
212 | |
213 chat = (GaimChat *)cnode; | |
214 | |
215 node = xmlnode_new("chat"); | |
216 xmlnode_set_attrib(node, "proto", gaim_account_get_protocol_id(chat->account)); | |
217 xmlnode_set_attrib(node, "account", gaim_account_get_username(chat->account)); | |
218 | |
219 if (chat->alias != NULL) | |
220 { | |
221 child = xmlnode_new_child(node, "alias"); | |
222 xmlnode_insert_data(child, chat->alias, -1); | |
223 } | |
224 | |
225 /* Write chat components */ | |
226 g_hash_table_foreach(chat->components, chat_component_to_xmlnode, node); | |
227 | |
228 /* Write chat settings */ | |
229 g_hash_table_foreach(chat->node.settings, value_to_xmlnode, node); | |
230 | |
231 return node; | |
232 } | |
233 | |
234 static xmlnode * | |
235 group_to_xmlnode(GaimBlistNode *gnode) | |
236 { | |
237 xmlnode *node, *child; | |
238 GaimGroup *group; | |
239 GaimBlistNode *cnode; | |
240 | |
241 group = (GaimGroup *)gnode; | |
242 | |
243 node = xmlnode_new("group"); | |
244 xmlnode_set_attrib(node, "name", group->name); | |
245 | |
246 /* Write settings */ | |
247 g_hash_table_foreach(group->node.settings, value_to_xmlnode, node); | |
248 | |
249 /* Write contacts and chats */ | |
250 for (cnode = gnode->child; cnode != NULL; cnode = cnode->next) | |
251 { | |
252 if (!GAIM_BLIST_NODE_SHOULD_SAVE(cnode)) | |
253 continue; | |
254 if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) | |
255 { | |
256 child = contact_to_xmlnode(cnode); | |
257 xmlnode_insert_child(node, child); | |
258 } | |
259 else if (GAIM_BLIST_NODE_IS_CHAT(cnode)) | |
260 { | |
261 child = chat_to_xmlnode(cnode); | |
262 xmlnode_insert_child(node, child); | |
263 } | |
264 } | |
265 | |
266 return node; | |
267 } | |
268 | |
269 static xmlnode * | |
270 accountprivacy_to_xmlnode(GaimAccount *account) | |
271 { | |
272 xmlnode *node, *child; | |
273 GSList *cur; | |
274 char buf[10]; | |
275 | |
276 node = xmlnode_new("account"); | |
277 xmlnode_set_attrib(node, "proto", gaim_account_get_protocol_id(account)); | |
278 xmlnode_set_attrib(node, "name", gaim_account_get_username(account)); | |
279 snprintf(buf, sizeof(buf), "%d", account->perm_deny); | |
280 xmlnode_set_attrib(node, "mode", buf); | |
281 | |
282 for (cur = account->permit; cur; cur = cur->next) | |
283 { | |
284 child = xmlnode_new_child(node, "permit"); | |
285 xmlnode_insert_data(child, cur->data, -1); | |
286 } | |
287 | |
288 for (cur = account->deny; cur; cur = cur->next) | |
289 { | |
290 child = xmlnode_new_child(node, "block"); | |
291 xmlnode_insert_data(child, cur->data, -1); | |
292 } | |
293 | |
294 return node; | |
295 } | |
296 | |
297 static xmlnode * | |
298 blist_to_xmlnode() | |
299 { | |
300 xmlnode *node, *child, *grandchild; | |
301 GaimBlistNode *gnode; | |
302 GList *cur; | |
303 | |
304 node = xmlnode_new("gaim"); | |
305 xmlnode_set_attrib(node, "version", "1.0"); | |
306 | |
307 /* Write groups */ | |
308 child = xmlnode_new_child(node, "blist"); | |
309 for (gnode = gaimbuddylist->root; gnode != NULL; gnode = gnode->next) | |
310 { | |
311 if (!GAIM_BLIST_NODE_SHOULD_SAVE(gnode)) | |
312 continue; | |
313 if (GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
314 { | |
315 grandchild = group_to_xmlnode(gnode); | |
316 xmlnode_insert_child(child, grandchild); | |
317 } | |
318 } | |
319 | |
320 /* Write privacy settings */ | |
321 child = xmlnode_new_child(node, "privacy"); | |
322 for (cur = gaim_accounts_get_all(); cur != NULL; cur = cur->next) | |
323 { | |
324 grandchild = accountprivacy_to_xmlnode(cur->data); | |
325 xmlnode_insert_child(child, grandchild); | |
326 } | |
327 | |
328 return node; | |
329 } | |
330 | |
331 static void | |
332 gaim_blist_sync() | |
333 { | |
334 xmlnode *node; | |
335 char *data; | |
336 | |
337 if (!blist_loaded) | |
338 { | |
339 gaim_debug_error("blist", "Attempted to save buddy list before it " | |
340 "was read!\n"); | |
341 return; | |
342 } | |
343 | |
344 node = blist_to_xmlnode(); | |
345 data = xmlnode_to_formatted_str(node, NULL); | |
346 gaim_util_write_data_to_file("blist.xml", data, -1); | |
347 g_free(data); | |
348 xmlnode_free(node); | |
349 } | |
350 | |
351 static gboolean | |
352 save_cb(gpointer data) | |
353 { | |
354 gaim_blist_sync(); | |
355 save_timer = 0; | |
356 return FALSE; | |
357 } | |
358 | |
359 void | |
360 gaim_blist_schedule_save() | |
361 { | |
362 if (save_timer == 0) | |
363 save_timer = gaim_timeout_add(5000, save_cb, NULL); | |
364 } | |
365 | |
366 | |
367 /********************************************************************* | |
368 * Reading from disk * | |
369 *********************************************************************/ | |
370 | |
371 static void | |
372 parse_setting(GaimBlistNode *node, xmlnode *setting) | |
373 { | |
374 const char *name = xmlnode_get_attrib(setting, "name"); | |
375 const char *type = xmlnode_get_attrib(setting, "type"); | |
376 char *value = xmlnode_get_data(setting); | |
377 | |
378 if (!value) | |
379 return; | |
380 | |
381 if (!type || !strcmp(type, "string")) | |
382 gaim_blist_node_set_string(node, name, value); | |
383 else if (!strcmp(type, "bool")) | |
384 gaim_blist_node_set_bool(node, name, atoi(value)); | |
385 else if (!strcmp(type, "int")) | |
386 gaim_blist_node_set_int(node, name, atoi(value)); | |
387 | |
388 g_free(value); | |
389 } | |
390 | |
391 static void | |
392 parse_buddy(GaimGroup *group, GaimContact *contact, xmlnode *bnode) | |
393 { | |
394 GaimAccount *account; | |
395 GaimBuddy *buddy; | |
396 char *name = NULL, *alias = NULL; | |
397 const char *acct_name, *proto, *protocol; | |
398 xmlnode *x; | |
399 | |
400 acct_name = xmlnode_get_attrib(bnode, "account"); | |
401 protocol = xmlnode_get_attrib(bnode, "protocol"); | |
402 proto = xmlnode_get_attrib(bnode, "proto"); | |
403 | |
404 if (!acct_name || (!proto && !protocol)) | |
405 return; | |
406 | |
407 account = gaim_accounts_find(acct_name, proto ? proto : protocol); | |
408 | |
409 if (!account) | |
410 return; | |
411 | |
412 if ((x = xmlnode_get_child(bnode, "name"))) | |
413 name = xmlnode_get_data(x); | |
414 | |
415 if (!name) | |
416 return; | |
417 | |
418 if ((x = xmlnode_get_child(bnode, "alias"))) | |
419 alias = xmlnode_get_data(x); | |
420 | |
421 buddy = gaim_buddy_new(account, name, alias); | |
422 gaim_blist_add_buddy(buddy, contact, group, | |
423 gaim_blist_get_last_child((GaimBlistNode*)contact)); | |
424 | |
425 for (x = xmlnode_get_child(bnode, "setting"); x; x = xmlnode_get_next_twin(x)) { | |
426 parse_setting((GaimBlistNode*)buddy, x); | |
427 } | |
428 | |
429 g_free(name); | |
430 g_free(alias); | |
431 } | |
432 | |
433 static void | |
434 parse_contact(GaimGroup *group, xmlnode *cnode) | |
435 { | |
436 GaimContact *contact = gaim_contact_new(); | |
437 xmlnode *x; | |
438 const char *alias; | |
439 | |
440 gaim_blist_add_contact(contact, group, | |
441 gaim_blist_get_last_child((GaimBlistNode*)group)); | |
442 | |
443 if ((alias = xmlnode_get_attrib(cnode, "alias"))) { | |
444 gaim_contact_set_alias(contact, alias); | |
445 } | |
446 | |
447 for (x = cnode->child; x; x = x->next) { | |
448 if (x->type != XMLNODE_TYPE_TAG) | |
449 continue; | |
450 if (!strcmp(x->name, "buddy")) | |
451 parse_buddy(group, contact, x); | |
452 else if (!strcmp(x->name, "setting")) | |
453 parse_setting((GaimBlistNode*)contact, x); | |
454 } | |
455 | |
456 /* if the contact is empty, don't keep it around. it causes problems */ | |
457 if (!((GaimBlistNode*)contact)->child) | |
458 gaim_blist_remove_contact(contact); | |
459 } | |
460 | |
461 static void | |
462 parse_chat(GaimGroup *group, xmlnode *cnode) | |
463 { | |
464 GaimChat *chat; | |
465 GaimAccount *account; | |
466 const char *acct_name, *proto, *protocol; | |
467 xmlnode *x; | |
468 char *alias = NULL; | |
469 GHashTable *components; | |
470 | |
471 acct_name = xmlnode_get_attrib(cnode, "account"); | |
472 protocol = xmlnode_get_attrib(cnode, "protocol"); | |
473 proto = xmlnode_get_attrib(cnode, "proto"); | |
474 | |
475 if (!acct_name || (!proto && !protocol)) | |
476 return; | |
477 | |
478 account = gaim_accounts_find(acct_name, proto ? proto : protocol); | |
479 | |
480 if (!account) | |
481 return; | |
482 | |
483 if ((x = xmlnode_get_child(cnode, "alias"))) | |
484 alias = xmlnode_get_data(x); | |
485 | |
486 components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
487 | |
488 for (x = xmlnode_get_child(cnode, "component"); x; x = xmlnode_get_next_twin(x)) { | |
489 const char *name; | |
490 char *value; | |
491 | |
492 name = xmlnode_get_attrib(x, "name"); | |
493 value = xmlnode_get_data(x); | |
494 g_hash_table_replace(components, g_strdup(name), value); | |
495 } | |
496 | |
497 chat = gaim_chat_new(account, alias, components); | |
498 gaim_blist_add_chat(chat, group, | |
499 gaim_blist_get_last_child((GaimBlistNode*)group)); | |
500 | |
501 for (x = xmlnode_get_child(cnode, "setting"); x; x = xmlnode_get_next_twin(x)) { | |
502 parse_setting((GaimBlistNode*)chat, x); | |
503 } | |
504 | |
505 g_free(alias); | |
506 } | |
507 | |
508 static void | |
509 parse_group(xmlnode *groupnode) | |
510 { | |
511 const char *name = xmlnode_get_attrib(groupnode, "name"); | |
512 GaimGroup *group; | |
513 xmlnode *cnode; | |
514 | |
515 if (!name) | |
516 name = _("Buddies"); | |
517 | |
518 group = gaim_group_new(name); | |
519 gaim_blist_add_group(group, | |
520 gaim_blist_get_last_sibling(gaimbuddylist->root)); | |
521 | |
522 for (cnode = groupnode->child; cnode; cnode = cnode->next) { | |
523 if (cnode->type != XMLNODE_TYPE_TAG) | |
524 continue; | |
525 if (!strcmp(cnode->name, "setting")) | |
526 parse_setting((GaimBlistNode*)group, cnode); | |
527 else if (!strcmp(cnode->name, "contact") || | |
528 !strcmp(cnode->name, "person")) | |
529 parse_contact(group, cnode); | |
530 else if (!strcmp(cnode->name, "chat")) | |
531 parse_chat(group, cnode); | |
532 } | |
533 } | |
534 | |
535 /* TODO: Make static and rename to load_blist */ | |
536 void | |
537 gaim_blist_load() | |
538 { | |
539 xmlnode *gaim, *blist, *privacy; | |
540 | |
541 blist_loaded = TRUE; | |
542 | |
543 gaim = gaim_util_read_xml_from_file("blist.xml", _("buddy list")); | |
544 | |
545 if (gaim == NULL) | |
546 return; | |
547 | |
548 blist = xmlnode_get_child(gaim, "blist"); | |
549 if (blist) { | |
550 xmlnode *groupnode; | |
551 for (groupnode = xmlnode_get_child(blist, "group"); groupnode != NULL; | |
552 groupnode = xmlnode_get_next_twin(groupnode)) { | |
553 parse_group(groupnode); | |
554 } | |
555 } | |
556 | |
557 privacy = xmlnode_get_child(gaim, "privacy"); | |
558 if (privacy) { | |
559 xmlnode *anode; | |
560 for (anode = privacy->child; anode; anode = anode->next) { | |
561 xmlnode *x; | |
562 GaimAccount *account; | |
563 int imode; | |
564 const char *acct_name, *proto, *mode, *protocol; | |
565 | |
566 acct_name = xmlnode_get_attrib(anode, "name"); | |
567 protocol = xmlnode_get_attrib(anode, "protocol"); | |
568 proto = xmlnode_get_attrib(anode, "proto"); | |
569 mode = xmlnode_get_attrib(anode, "mode"); | |
570 | |
571 if (!acct_name || (!proto && !protocol) || !mode) | |
572 continue; | |
573 | |
574 account = gaim_accounts_find(acct_name, proto ? proto : protocol); | |
575 | |
576 if (!account) | |
577 continue; | |
578 | |
579 imode = atoi(mode); | |
580 account->perm_deny = (imode != 0 ? imode : GAIM_PRIVACY_ALLOW_ALL); | |
581 | |
582 for (x = anode->child; x; x = x->next) { | |
583 char *name; | |
584 if (x->type != XMLNODE_TYPE_TAG) | |
585 continue; | |
586 | |
587 if (!strcmp(x->name, "permit")) { | |
588 name = xmlnode_get_data(x); | |
589 gaim_privacy_permit_add(account, name, TRUE); | |
590 g_free(name); | |
591 } else if (!strcmp(x->name, "block")) { | |
592 name = xmlnode_get_data(x); | |
593 gaim_privacy_deny_add(account, name, TRUE); | |
594 g_free(name); | |
595 } | |
596 } | |
597 } | |
598 } | |
599 | |
600 xmlnode_free(gaim); | |
601 } | |
602 | |
603 | |
604 /********************************************************************* | |
605 * Stuff * | |
606 *********************************************************************/ | |
607 | |
608 static void | |
609 gaim_contact_compute_priority_buddy(GaimContact *contact) | |
610 { | |
611 GaimBlistNode *bnode; | |
612 GaimBuddy *new_priority = NULL; | |
613 | |
614 g_return_if_fail(contact != NULL); | |
615 | |
616 contact->priority = NULL; | |
617 for (bnode = ((GaimBlistNode*)contact)->child; | |
618 bnode != NULL; | |
619 bnode = bnode->next) | |
620 { | |
621 GaimBuddy *buddy; | |
622 | |
623 if (!GAIM_BLIST_NODE_IS_BUDDY(bnode)) | |
624 continue; | |
625 | |
626 buddy = (GaimBuddy*)bnode; | |
627 | |
628 if (!gaim_account_is_connected(buddy->account)) | |
629 continue; | |
630 if (new_priority == NULL) | |
631 new_priority = buddy; | |
632 else | |
633 { | |
634 int cmp; | |
635 | |
636 cmp = gaim_presence_compare(gaim_buddy_get_presence(new_priority), | |
637 gaim_buddy_get_presence(buddy)); | |
638 | |
639 if (cmp > 0 || (cmp == 0 && | |
640 gaim_prefs_get_bool("/core/contact/last_match"))) | |
641 { | |
642 new_priority = buddy; | |
643 } | |
644 } | |
645 } | |
646 | |
647 contact->priority = new_priority; | |
648 contact->priority_valid = TRUE; | |
649 } | |
650 | |
651 | |
652 /***************************************************************************** | |
653 * Public API functions * | |
654 *****************************************************************************/ | |
655 | |
656 GaimBuddyList *gaim_blist_new() | |
657 { | |
658 GaimBlistUiOps *ui_ops; | |
659 GaimBuddyList *gbl = g_new0(GaimBuddyList, 1); | |
660 GAIM_DBUS_REGISTER_POINTER(gbl, GaimBuddyList); | |
661 | |
662 ui_ops = gaim_blist_get_ui_ops(); | |
663 | |
664 gbl->buddies = g_hash_table_new_full((GHashFunc)_gaim_blist_hbuddy_hash, | |
665 (GEqualFunc)_gaim_blist_hbuddy_equal, | |
666 (GDestroyNotify)_gaim_blist_hbuddy_free_key, NULL); | |
667 | |
668 if (ui_ops != NULL && ui_ops->new_list != NULL) | |
669 ui_ops->new_list(gbl); | |
670 | |
671 return gbl; | |
672 } | |
673 | |
674 void | |
675 gaim_set_blist(GaimBuddyList *list) | |
676 { | |
677 gaimbuddylist = list; | |
678 } | |
679 | |
680 GaimBuddyList * | |
681 gaim_get_blist() | |
682 { | |
683 return gaimbuddylist; | |
684 } | |
685 | |
686 GaimBlistNode * | |
687 gaim_blist_get_root() | |
688 { | |
689 return gaimbuddylist ? gaimbuddylist->root : NULL; | |
690 } | |
691 | |
692 void gaim_blist_show() | |
693 { | |
694 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
695 | |
696 if (ops && ops->show) | |
697 ops->show(gaimbuddylist); | |
698 } | |
699 | |
700 void gaim_blist_destroy() | |
701 { | |
702 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
703 | |
704 gaim_debug(GAIM_DEBUG_INFO, "blist", "Destroying\n"); | |
705 | |
706 if (ops && ops->destroy) | |
707 ops->destroy(gaimbuddylist); | |
708 } | |
709 | |
710 void gaim_blist_set_visible(gboolean show) | |
711 { | |
712 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
713 | |
714 if (ops && ops->set_visible) | |
715 ops->set_visible(gaimbuddylist, show); | |
716 } | |
717 | |
718 static GaimBlistNode *get_next_node(GaimBlistNode *node, gboolean godeep) | |
719 { | |
720 if (node == NULL) | |
721 return NULL; | |
722 | |
723 if (godeep && node->child) | |
724 return node->child; | |
725 | |
726 if (node->next) | |
727 return node->next; | |
728 | |
729 return get_next_node(node->parent, FALSE); | |
730 } | |
731 | |
732 GaimBlistNode *gaim_blist_node_next(GaimBlistNode *node, gboolean offline) | |
733 { | |
734 GaimBlistNode *ret = node; | |
735 | |
736 if (offline) | |
737 return get_next_node(ret, TRUE); | |
738 do | |
739 { | |
740 ret = get_next_node(ret, TRUE); | |
741 } while (ret && GAIM_BLIST_NODE_IS_BUDDY(ret) && | |
742 !gaim_account_is_connected(gaim_buddy_get_account((GaimBuddy *)ret))); | |
743 | |
744 return ret; | |
745 } | |
746 | |
747 void | |
748 gaim_blist_update_buddy_status(GaimBuddy *buddy, GaimStatus *old_status) | |
749 { | |
750 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
751 GaimPresence *presence; | |
752 GaimStatus *status; | |
753 | |
754 g_return_if_fail(buddy != NULL); | |
755 | |
756 presence = gaim_buddy_get_presence(buddy); | |
757 status = gaim_presence_get_active_status(presence); | |
758 | |
759 gaim_debug_info("blist", "Updating buddy status for %s (%s)\n", | |
760 buddy->name, gaim_account_get_protocol_name(buddy->account)); | |
761 | |
762 if (gaim_status_is_online(status) && | |
763 !gaim_status_is_online(old_status)) { | |
764 | |
765 gaim_signal_emit(gaim_blist_get_handle(), "buddy-signed-on", buddy); | |
766 | |
767 ((GaimContact*)((GaimBlistNode*)buddy)->parent)->online++; | |
768 if (((GaimContact*)((GaimBlistNode*)buddy)->parent)->online == 1) | |
769 ((GaimGroup *)((GaimBlistNode *)buddy)->parent->parent)->online++; | |
770 } else if (!gaim_status_is_online(status) && | |
771 gaim_status_is_online(old_status)) { | |
772 gaim_blist_node_set_int(&buddy->node, "last_seen", time(NULL)); | |
773 gaim_signal_emit(gaim_blist_get_handle(), "buddy-signed-off", buddy); | |
774 ((GaimContact*)((GaimBlistNode*)buddy)->parent)->online--; | |
775 if (((GaimContact*)((GaimBlistNode*)buddy)->parent)->online == 0) | |
776 ((GaimGroup *)((GaimBlistNode *)buddy)->parent->parent)->online--; | |
777 } else { | |
778 gaim_signal_emit(gaim_blist_get_handle(), | |
779 "buddy-status-changed", buddy, old_status, | |
780 status); | |
781 } | |
782 | |
783 /* | |
784 * This function used to only call the following two functions if one of | |
785 * the above signals had been triggered, but that's not good, because | |
786 * if someone's away message changes and they don't go from away to back | |
787 * to away then no signal is triggered. | |
788 * | |
789 * It's a safe assumption that SOMETHING called this function. PROBABLY | |
790 * because something, somewhere changed. Calling the stuff below | |
791 * certainly won't hurt anything. Unless you're on a K6-2 300. | |
792 */ | |
793 gaim_contact_invalidate_priority_buddy(gaim_buddy_get_contact(buddy)); | |
794 if (ops && ops->update) | |
795 ops->update(gaimbuddylist, (GaimBlistNode *)buddy); | |
796 } | |
797 | |
798 void gaim_blist_update_buddy_icon(GaimBuddy *buddy) | |
799 { | |
800 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
801 | |
802 g_return_if_fail(buddy != NULL); | |
803 | |
804 if (ops && ops->update) | |
805 ops->update(gaimbuddylist, (GaimBlistNode *)buddy); | |
806 } | |
807 | |
808 /* | |
809 * TODO: Maybe remove the call to this from server.c and call it | |
810 * from oscar.c and toc.c instead? | |
811 */ | |
812 void gaim_blist_rename_buddy(GaimBuddy *buddy, const char *name) | |
813 { | |
814 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
815 struct _gaim_hbuddy *hb; | |
816 | |
817 g_return_if_fail(buddy != NULL); | |
818 | |
819 hb = g_new(struct _gaim_hbuddy, 1); | |
820 hb->name = g_strdup(gaim_normalize(buddy->account, buddy->name)); | |
821 hb->account = buddy->account; | |
822 hb->group = ((GaimBlistNode *)buddy)->parent->parent; | |
823 g_hash_table_remove(gaimbuddylist->buddies, hb); | |
824 | |
825 g_free(hb->name); | |
826 hb->name = g_strdup(gaim_normalize(buddy->account, name)); | |
827 g_hash_table_replace(gaimbuddylist->buddies, hb, buddy); | |
828 | |
829 g_free(buddy->name); | |
830 buddy->name = g_strdup(name); | |
831 | |
832 gaim_blist_schedule_save(); | |
833 | |
834 if (ops && ops->update) | |
835 ops->update(gaimbuddylist, (GaimBlistNode *)buddy); | |
836 } | |
837 | |
838 void gaim_blist_alias_contact(GaimContact *contact, const char *alias) | |
839 { | |
840 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
841 GaimConversation *conv; | |
842 GaimBlistNode *bnode; | |
14670 | 843 char *old_alias; |
14192 | 844 |
845 g_return_if_fail(contact != NULL); | |
846 | |
14670 | 847 old_alias = contact->alias; |
848 | |
14192 | 849 if ((alias != NULL) && (*alias != '\0')) |
850 contact->alias = g_strdup(alias); | |
851 else | |
852 contact->alias = NULL; | |
853 | |
854 gaim_blist_schedule_save(); | |
855 | |
856 if (ops && ops->update) | |
857 ops->update(gaimbuddylist, (GaimBlistNode *)contact); | |
858 | |
859 for(bnode = ((GaimBlistNode *)contact)->child; bnode != NULL; bnode = bnode->next) | |
860 { | |
861 GaimBuddy *buddy = (GaimBuddy *)bnode; | |
862 | |
863 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, buddy->name, | |
864 buddy->account); | |
865 if (conv) | |
866 gaim_conversation_autoset_title(conv); | |
867 } | |
868 | |
869 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased", | |
870 contact, old_alias); | |
871 g_free(old_alias); | |
872 } | |
873 | |
874 void gaim_blist_alias_chat(GaimChat *chat, const char *alias) | |
875 { | |
876 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
14670 | 877 char *old_alias; |
14192 | 878 |
879 g_return_if_fail(chat != NULL); | |
880 | |
14670 | 881 old_alias = chat->alias; |
882 | |
14192 | 883 if ((alias != NULL) && (*alias != '\0')) |
884 chat->alias = g_strdup(alias); | |
885 else | |
886 chat->alias = NULL; | |
887 | |
888 gaim_blist_schedule_save(); | |
889 | |
890 if (ops && ops->update) | |
891 ops->update(gaimbuddylist, (GaimBlistNode *)chat); | |
892 | |
893 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased", | |
894 chat, old_alias); | |
895 g_free(old_alias); | |
896 } | |
897 | |
898 void gaim_blist_alias_buddy(GaimBuddy *buddy, const char *alias) | |
899 { | |
900 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
901 GaimConversation *conv; | |
14670 | 902 char *old_alias; |
14192 | 903 |
904 g_return_if_fail(buddy != NULL); | |
905 | |
14670 | 906 old_alias = buddy->alias; |
907 | |
14192 | 908 if ((alias != NULL) && (*alias != '\0')) |
909 buddy->alias = g_strdup(alias); | |
910 else | |
911 buddy->alias = NULL; | |
912 | |
913 gaim_blist_schedule_save(); | |
914 | |
915 if (ops && ops->update) | |
916 ops->update(gaimbuddylist, (GaimBlistNode *)buddy); | |
917 | |
918 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, buddy->name, | |
919 buddy->account); | |
920 if (conv) | |
921 gaim_conversation_autoset_title(conv); | |
922 | |
923 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased", | |
924 buddy, old_alias); | |
925 g_free(old_alias); | |
926 } | |
927 | |
928 void gaim_blist_server_alias_buddy(GaimBuddy *buddy, const char *alias) | |
929 { | |
930 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
931 GaimConversation *conv; | |
14670 | 932 char *old_alias; |
14192 | 933 |
934 g_return_if_fail(buddy != NULL); | |
935 | |
14670 | 936 old_alias = buddy->server_alias; |
937 | |
14192 | 938 if ((alias != NULL) && (*alias != '\0') && g_utf8_validate(alias, -1, NULL)) |
939 buddy->server_alias = g_strdup(alias); | |
940 else | |
941 buddy->server_alias = NULL; | |
942 | |
943 gaim_blist_schedule_save(); | |
944 | |
945 if (ops && ops->update) | |
946 ops->update(gaimbuddylist, (GaimBlistNode *)buddy); | |
947 | |
948 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, buddy->name, | |
949 buddy->account); | |
950 if (conv) | |
951 gaim_conversation_autoset_title(conv); | |
952 | |
953 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased", | |
954 buddy, old_alias); | |
955 g_free(old_alias); | |
956 } | |
957 | |
958 /* | |
959 * TODO: If merging, prompt the user if they want to merge. | |
960 */ | |
961 void gaim_blist_rename_group(GaimGroup *source, const char *new_name) | |
962 { | |
963 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
964 GaimGroup *dest; | |
965 gchar *old_name; | |
966 GList *moved_buddies = NULL; | |
967 GSList *accts; | |
968 | |
969 g_return_if_fail(source != NULL); | |
970 g_return_if_fail(new_name != NULL); | |
971 | |
972 if (*new_name == '\0' || !strcmp(new_name, source->name)) | |
973 return; | |
974 | |
975 dest = gaim_find_group(new_name); | |
976 if (dest != NULL) { | |
977 /* We're merging two groups */ | |
978 GaimBlistNode *prev, *child, *next; | |
979 | |
980 prev = gaim_blist_get_last_child((GaimBlistNode*)dest); | |
981 child = ((GaimBlistNode*)source)->child; | |
982 | |
983 /* | |
984 * TODO: This seems like a dumb way to do this... why not just | |
985 * append all children from the old group to the end of the new | |
986 * one? PRPLs might be expecting to receive an add_buddy() for | |
987 * each moved buddy... | |
988 */ | |
989 while (child) | |
990 { | |
991 next = child->next; | |
992 if (GAIM_BLIST_NODE_IS_CONTACT(child)) { | |
993 GaimBlistNode *bnode; | |
994 gaim_blist_add_contact((GaimContact *)child, dest, prev); | |
995 for (bnode = child->child; bnode != NULL; bnode = bnode->next) { | |
996 gaim_blist_add_buddy((GaimBuddy *)bnode, (GaimContact *)child, | |
997 NULL, bnode->prev); | |
998 moved_buddies = g_list_append(moved_buddies, bnode); | |
999 } | |
1000 prev = child; | |
1001 } else if (GAIM_BLIST_NODE_IS_CHAT(child)) { | |
1002 gaim_blist_add_chat((GaimChat *)child, dest, prev); | |
1003 prev = child; | |
1004 } else { | |
1005 gaim_debug(GAIM_DEBUG_ERROR, "blist", | |
1006 "Unknown child type in group %s\n", source->name); | |
1007 } | |
1008 child = next; | |
1009 } | |
1010 | |
1011 /* Make a copy of the old group name and then delete the old group */ | |
1012 old_name = g_strdup(source->name); | |
1013 gaim_blist_remove_group(source); | |
1014 source = dest; | |
1015 } else { | |
1016 /* A simple rename */ | |
1017 GaimBlistNode *cnode, *bnode; | |
1018 | |
1019 /* Build a GList of all buddies in this group */ | |
1020 for (cnode = ((GaimBlistNode *)source)->child; cnode != NULL; cnode = cnode->next) { | |
1021 if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) | |
1022 for (bnode = cnode->child; bnode != NULL; bnode = bnode->next) | |
1023 moved_buddies = g_list_append(moved_buddies, bnode); | |
1024 } | |
1025 | |
1026 old_name = source->name; | |
1027 source->name = g_strdup(new_name); | |
1028 } | |
1029 | |
1030 /* Save our changes */ | |
1031 gaim_blist_schedule_save(); | |
1032 | |
1033 /* Update the UI */ | |
1034 if (ops && ops->update) | |
1035 ops->update(gaimbuddylist, (GaimBlistNode*)source); | |
1036 | |
1037 /* Notify all PRPLs */ | |
1038 /* TODO: Is this condition needed? Seems like it would always be TRUE */ | |
1039 if(old_name && source && strcmp(source->name, old_name)) { | |
1040 for (accts = gaim_group_get_accounts(source); accts; accts = g_slist_remove(accts, accts->data)) { | |
1041 GaimAccount *account = accts->data; | |
1042 GaimPluginProtocolInfo *prpl_info = NULL; | |
1043 GList *l = NULL, *buddies = NULL; | |
1044 | |
1045 if(account->gc && account->gc->prpl) | |
1046 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(account->gc->prpl); | |
1047 | |
1048 if(!prpl_info) | |
1049 continue; | |
1050 | |
1051 for(l = moved_buddies; l; l = l->next) { | |
1052 GaimBuddy *buddy = (GaimBuddy *)l->data; | |
1053 | |
1054 if(buddy && buddy->account == account) | |
1055 buddies = g_list_append(buddies, (GaimBlistNode *)buddy); | |
1056 } | |
1057 | |
1058 if(prpl_info->rename_group) { | |
1059 prpl_info->rename_group(account->gc, old_name, source, buddies); | |
1060 } else { | |
1061 GList *cur, *groups = NULL; | |
1062 | |
1063 /* Make a list of what the groups each buddy is in */ | |
1064 for(cur = buddies; cur; cur = cur->next) { | |
1065 GaimBlistNode *node = (GaimBlistNode *)cur->data; | |
1066 groups = g_list_prepend(groups, node->parent->parent); | |
1067 } | |
1068 | |
1069 gaim_account_remove_buddies(account, buddies, groups); | |
1070 g_list_free(groups); | |
1071 gaim_account_add_buddies(account, buddies); | |
1072 } | |
1073 | |
1074 g_list_free(buddies); | |
1075 } | |
1076 } | |
1077 g_list_free(moved_buddies); | |
1078 g_free(old_name); | |
1079 } | |
1080 | |
1081 static void gaim_blist_node_initialize_settings(GaimBlistNode *node); | |
1082 | |
1083 GaimChat *gaim_chat_new(GaimAccount *account, const char *alias, GHashTable *components) | |
1084 { | |
1085 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1086 GaimChat *chat; | |
1087 | |
1088 g_return_val_if_fail(account != NULL, FALSE); | |
1089 g_return_val_if_fail(components != NULL, FALSE); | |
1090 | |
1091 chat = g_new0(GaimChat, 1); | |
1092 chat->account = account; | |
1093 if ((alias != NULL) && (*alias != '\0')) | |
1094 chat->alias = g_strdup(alias); | |
1095 chat->components = components; | |
1096 gaim_blist_node_initialize_settings((GaimBlistNode *)chat); | |
1097 ((GaimBlistNode *)chat)->type = GAIM_BLIST_CHAT_NODE; | |
1098 | |
1099 if (ops != NULL && ops->new_node != NULL) | |
1100 ops->new_node((GaimBlistNode *)chat); | |
1101 | |
1102 GAIM_DBUS_REGISTER_POINTER(chat, GaimChat); | |
1103 return chat; | |
1104 } | |
1105 | |
1106 GaimBuddy *gaim_buddy_new(GaimAccount *account, const char *screenname, const char *alias) | |
1107 { | |
1108 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1109 GaimBuddy *buddy; | |
1110 | |
1111 g_return_val_if_fail(account != NULL, FALSE); | |
1112 g_return_val_if_fail(screenname != NULL, FALSE); | |
1113 | |
1114 buddy = g_new0(GaimBuddy, 1); | |
1115 buddy->account = account; | |
1116 buddy->name = g_strdup(screenname); | |
1117 buddy->alias = g_strdup(alias); | |
1118 buddy->presence = gaim_presence_new_for_buddy(buddy); | |
1119 ((GaimBlistNode *)buddy)->type = GAIM_BLIST_BUDDY_NODE; | |
1120 | |
1121 gaim_presence_set_status_active(buddy->presence, "offline", TRUE); | |
1122 | |
1123 gaim_blist_node_initialize_settings((GaimBlistNode *)buddy); | |
1124 | |
1125 if (ops && ops->new_node) | |
1126 ops->new_node((GaimBlistNode *)buddy); | |
1127 | |
1128 GAIM_DBUS_REGISTER_POINTER(buddy, GaimBuddy); | |
1129 return buddy; | |
1130 } | |
1131 | |
1132 void | |
1133 gaim_buddy_set_icon(GaimBuddy *buddy, GaimBuddyIcon *icon) | |
1134 { | |
1135 g_return_if_fail(buddy != NULL); | |
1136 | |
1137 if (buddy->icon != icon) { | |
1138 if (buddy->icon != NULL) | |
1139 gaim_buddy_icon_unref(buddy->icon); | |
1140 | |
1141 buddy->icon = (icon != NULL ? gaim_buddy_icon_ref(icon) : NULL); | |
1142 } | |
1143 | |
1144 if (buddy->icon) | |
1145 gaim_buddy_icon_cache(icon, buddy); | |
1146 else | |
1147 gaim_buddy_icon_uncache(buddy); | |
1148 | |
1149 gaim_blist_schedule_save(); | |
1150 | |
1151 gaim_signal_emit(gaim_blist_get_handle(), "buddy-icon-changed", buddy); | |
1152 | |
1153 gaim_blist_update_buddy_icon(buddy); | |
1154 } | |
1155 | |
1156 GaimAccount * | |
1157 gaim_buddy_get_account(const GaimBuddy *buddy) | |
1158 { | |
1159 g_return_val_if_fail(buddy != NULL, NULL); | |
1160 | |
1161 return buddy->account; | |
1162 } | |
1163 | |
1164 const char * | |
1165 gaim_buddy_get_name(const GaimBuddy *buddy) | |
1166 { | |
1167 g_return_val_if_fail(buddy != NULL, NULL); | |
1168 | |
1169 return buddy->name; | |
1170 } | |
1171 | |
1172 GaimBuddyIcon * | |
1173 gaim_buddy_get_icon(const GaimBuddy *buddy) | |
1174 { | |
1175 g_return_val_if_fail(buddy != NULL, NULL); | |
1176 | |
1177 return buddy->icon; | |
1178 } | |
1179 | |
1180 void gaim_blist_add_chat(GaimChat *chat, GaimGroup *group, GaimBlistNode *node) | |
1181 { | |
1182 GaimBlistNode *cnode = (GaimBlistNode*)chat; | |
1183 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1184 | |
1185 g_return_if_fail(chat != NULL); | |
1186 g_return_if_fail(GAIM_BLIST_NODE_IS_CHAT((GaimBlistNode *)chat)); | |
1187 | |
1188 if (node == NULL) { | |
1189 if (group == NULL) { | |
1190 group = gaim_group_new(_("Chats")); | |
1191 gaim_blist_add_group(group, | |
1192 gaim_blist_get_last_sibling(gaimbuddylist->root)); | |
1193 } | |
1194 } else { | |
1195 group = (GaimGroup*)node->parent; | |
1196 } | |
1197 | |
1198 /* if we're moving to overtop of ourselves, do nothing */ | |
1199 if (cnode == node) | |
1200 return; | |
1201 | |
1202 if (cnode->parent) { | |
1203 /* This chat was already in the list and is | |
1204 * being moved. | |
1205 */ | |
1206 ((GaimGroup *)cnode->parent)->totalsize--; | |
1207 if (gaim_account_is_connected(chat->account)) { | |
1208 ((GaimGroup *)cnode->parent)->online--; | |
1209 ((GaimGroup *)cnode->parent)->currentsize--; | |
1210 } | |
1211 if (cnode->next) | |
1212 cnode->next->prev = cnode->prev; | |
1213 if (cnode->prev) | |
1214 cnode->prev->next = cnode->next; | |
1215 if (cnode->parent->child == cnode) | |
1216 cnode->parent->child = cnode->next; | |
1217 | |
1218 if (ops && ops->remove) | |
1219 ops->remove(gaimbuddylist, cnode); | |
1220 /* ops->remove() cleaned up the cnode's ui_data, so we need to | |
1221 * reinitialize it */ | |
1222 if (ops && ops->new_node) | |
1223 ops->new_node(cnode); | |
1224 | |
1225 gaim_blist_schedule_save(); | |
1226 } | |
1227 | |
1228 if (node != NULL) { | |
1229 if (node->next) | |
1230 node->next->prev = cnode; | |
1231 cnode->next = node->next; | |
1232 cnode->prev = node; | |
1233 cnode->parent = node->parent; | |
1234 node->next = cnode; | |
1235 ((GaimGroup *)node->parent)->totalsize++; | |
1236 if (gaim_account_is_connected(chat->account)) { | |
1237 ((GaimGroup *)node->parent)->online++; | |
1238 ((GaimGroup *)node->parent)->currentsize++; | |
1239 } | |
1240 } else { | |
1241 if (((GaimBlistNode *)group)->child) | |
1242 ((GaimBlistNode *)group)->child->prev = cnode; | |
1243 cnode->next = ((GaimBlistNode *)group)->child; | |
1244 cnode->prev = NULL; | |
1245 ((GaimBlistNode *)group)->child = cnode; | |
1246 cnode->parent = (GaimBlistNode *)group; | |
1247 group->totalsize++; | |
1248 if (gaim_account_is_connected(chat->account)) { | |
1249 group->online++; | |
1250 group->currentsize++; | |
1251 } | |
1252 } | |
1253 | |
1254 gaim_blist_schedule_save(); | |
1255 | |
1256 if (ops && ops->update) | |
1257 ops->update(gaimbuddylist, (GaimBlistNode *)cnode); | |
1258 } | |
1259 | |
1260 void gaim_blist_add_buddy(GaimBuddy *buddy, GaimContact *contact, GaimGroup *group, GaimBlistNode *node) | |
1261 { | |
1262 GaimBlistNode *cnode, *bnode; | |
1263 GaimGroup *g; | |
1264 GaimContact *c; | |
1265 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1266 struct _gaim_hbuddy *hb; | |
1267 | |
1268 g_return_if_fail(buddy != NULL); | |
1269 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY((GaimBlistNode*)buddy)); | |
1270 | |
1271 bnode = (GaimBlistNode *)buddy; | |
1272 | |
1273 /* if we're moving to overtop of ourselves, do nothing */ | |
1274 if (bnode == node || (!node && bnode->parent && | |
1275 contact && bnode->parent == (GaimBlistNode*)contact | |
1276 && bnode == bnode->parent->child)) | |
1277 return; | |
1278 | |
1279 if (node && GAIM_BLIST_NODE_IS_BUDDY(node)) { | |
1280 c = (GaimContact*)node->parent; | |
1281 g = (GaimGroup*)node->parent->parent; | |
1282 } else if (contact) { | |
1283 c = contact; | |
1284 g = (GaimGroup *)((GaimBlistNode *)c)->parent; | |
1285 } else { | |
1286 if (group) { | |
1287 g = group; | |
1288 } else { | |
1289 g = gaim_group_new(_("Buddies")); | |
1290 gaim_blist_add_group(g, | |
1291 gaim_blist_get_last_sibling(gaimbuddylist->root)); | |
1292 } | |
1293 c = gaim_contact_new(); | |
1294 gaim_blist_add_contact(c, g, | |
1295 gaim_blist_get_last_child((GaimBlistNode*)g)); | |
1296 } | |
1297 | |
1298 cnode = (GaimBlistNode *)c; | |
1299 | |
1300 if (bnode->parent) { | |
1301 if (GAIM_BUDDY_IS_ONLINE(buddy)) { | |
1302 ((GaimContact*)bnode->parent)->online--; | |
1303 if (((GaimContact*)bnode->parent)->online == 0) | |
1304 ((GaimGroup*)bnode->parent->parent)->online--; | |
1305 } | |
1306 if (gaim_account_is_connected(buddy->account)) { | |
1307 ((GaimContact*)bnode->parent)->currentsize--; | |
1308 if (((GaimContact*)bnode->parent)->currentsize == 0) | |
1309 ((GaimGroup*)bnode->parent->parent)->currentsize--; | |
1310 } | |
1311 ((GaimContact*)bnode->parent)->totalsize--; | |
1312 /* the group totalsize will be taken care of by remove_contact below */ | |
1313 | |
1314 if (bnode->parent->parent != (GaimBlistNode*)g) | |
1315 serv_move_buddy(buddy, (GaimGroup *)bnode->parent->parent, g); | |
1316 | |
1317 if (bnode->next) | |
1318 bnode->next->prev = bnode->prev; | |
1319 if (bnode->prev) | |
1320 bnode->prev->next = bnode->next; | |
1321 if (bnode->parent->child == bnode) | |
1322 bnode->parent->child = bnode->next; | |
1323 | |
1324 if (ops && ops->remove) | |
1325 ops->remove(gaimbuddylist, bnode); | |
1326 | |
1327 gaim_blist_schedule_save(); | |
1328 | |
1329 if (bnode->parent->parent != (GaimBlistNode*)g) { | |
1330 hb = g_new(struct _gaim_hbuddy, 1); | |
1331 hb->name = g_strdup(gaim_normalize(buddy->account, buddy->name)); | |
1332 hb->account = buddy->account; | |
1333 hb->group = bnode->parent->parent; | |
1334 g_hash_table_remove(gaimbuddylist->buddies, hb); | |
1335 g_free(hb->name); | |
1336 g_free(hb); | |
1337 } | |
1338 | |
1339 if (!bnode->parent->child) { | |
1340 gaim_blist_remove_contact((GaimContact*)bnode->parent); | |
1341 } else { | |
1342 gaim_contact_invalidate_priority_buddy((GaimContact*)bnode->parent); | |
1343 if (ops && ops->update) | |
1344 ops->update(gaimbuddylist, bnode->parent); | |
1345 } | |
1346 } | |
1347 | |
1348 if (node && GAIM_BLIST_NODE_IS_BUDDY(node)) { | |
1349 if (node->next) | |
1350 node->next->prev = bnode; | |
1351 bnode->next = node->next; | |
1352 bnode->prev = node; | |
1353 bnode->parent = node->parent; | |
1354 node->next = bnode; | |
1355 } else { | |
1356 if (cnode->child) | |
1357 cnode->child->prev = bnode; | |
1358 bnode->prev = NULL; | |
1359 bnode->next = cnode->child; | |
1360 cnode->child = bnode; | |
1361 bnode->parent = cnode; | |
1362 } | |
1363 | |
1364 if (GAIM_BUDDY_IS_ONLINE(buddy)) { | |
1365 ((GaimContact*)bnode->parent)->online++; | |
1366 if (((GaimContact*)bnode->parent)->online == 1) | |
1367 ((GaimGroup*)bnode->parent->parent)->online++; | |
1368 } | |
1369 if (gaim_account_is_connected(buddy->account)) { | |
1370 ((GaimContact*)bnode->parent)->currentsize++; | |
1371 if (((GaimContact*)bnode->parent)->currentsize == 1) | |
1372 ((GaimGroup*)bnode->parent->parent)->currentsize++; | |
1373 } | |
1374 ((GaimContact*)bnode->parent)->totalsize++; | |
1375 | |
1376 hb = g_new(struct _gaim_hbuddy, 1); | |
1377 hb->name = g_strdup(gaim_normalize(buddy->account, buddy->name)); | |
1378 hb->account = buddy->account; | |
1379 hb->group = ((GaimBlistNode*)buddy)->parent->parent; | |
1380 | |
1381 g_hash_table_replace(gaimbuddylist->buddies, hb, buddy); | |
1382 | |
1383 gaim_contact_invalidate_priority_buddy(gaim_buddy_get_contact(buddy)); | |
1384 | |
1385 gaim_blist_schedule_save(); | |
1386 | |
1387 if (ops && ops->update) | |
1388 ops->update(gaimbuddylist, (GaimBlistNode*)buddy); | |
1389 | |
1390 /* Signal that the buddy has been added */ | |
1391 gaim_signal_emit(gaim_blist_get_handle(), "buddy-added", buddy); | |
1392 } | |
1393 | |
1394 GaimContact *gaim_contact_new() | |
1395 { | |
1396 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1397 | |
1398 GaimContact *contact = g_new0(GaimContact, 1); | |
1399 contact->totalsize = 0; | |
1400 contact->currentsize = 0; | |
1401 contact->online = 0; | |
1402 gaim_blist_node_initialize_settings((GaimBlistNode *)contact); | |
1403 ((GaimBlistNode *)contact)->type = GAIM_BLIST_CONTACT_NODE; | |
1404 | |
1405 if (ops && ops->new_node) | |
1406 ops->new_node((GaimBlistNode *)contact); | |
1407 | |
1408 GAIM_DBUS_REGISTER_POINTER(contact, GaimContact); | |
1409 return contact; | |
1410 } | |
1411 | |
1412 void gaim_contact_set_alias(GaimContact *contact, const char *alias) | |
1413 { | |
1414 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
14670 | 1415 char *old_alias; |
14192 | 1416 |
1417 g_return_if_fail(contact != NULL); | |
1418 | |
14670 | 1419 old_alias = contact->alias; |
1420 | |
14192 | 1421 if ((alias != NULL) && (*alias != '\0')) |
1422 contact->alias = g_strdup(alias); | |
1423 else | |
1424 contact->alias = NULL; | |
1425 | |
1426 gaim_blist_schedule_save(); | |
1427 | |
1428 if (ops && ops->update) | |
1429 ops->update(gaimbuddylist, (GaimBlistNode*)contact); | |
1430 | |
1431 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased", | |
1432 contact, old_alias); | |
1433 g_free(old_alias); | |
1434 } | |
1435 | |
1436 const char *gaim_contact_get_alias(GaimContact* contact) | |
1437 { | |
1438 g_return_val_if_fail(contact != NULL, NULL); | |
1439 | |
1440 if (contact->alias) | |
1441 return contact->alias; | |
1442 | |
1443 return gaim_buddy_get_alias(gaim_contact_get_priority_buddy(contact)); | |
1444 } | |
1445 | |
1446 gboolean gaim_contact_on_account(GaimContact *c, GaimAccount *account) | |
1447 { | |
1448 GaimBlistNode *bnode, *cnode = (GaimBlistNode *) c; | |
1449 | |
1450 g_return_val_if_fail(c != NULL, FALSE); | |
1451 g_return_val_if_fail(account != NULL, FALSE); | |
1452 | |
1453 for (bnode = cnode->child; bnode; bnode = bnode->next) { | |
1454 GaimBuddy *buddy; | |
1455 | |
1456 if (! GAIM_BLIST_NODE_IS_BUDDY(bnode)) | |
1457 continue; | |
1458 | |
1459 buddy = (GaimBuddy *)bnode; | |
1460 if (buddy->account == account) | |
1461 return TRUE; | |
1462 } | |
1463 return FALSE; | |
1464 } | |
1465 | |
1466 void gaim_contact_invalidate_priority_buddy(GaimContact *contact) | |
1467 { | |
1468 g_return_if_fail(contact != NULL); | |
1469 | |
1470 contact->priority_valid = FALSE; | |
1471 } | |
1472 | |
1473 GaimGroup *gaim_group_new(const char *name) | |
1474 { | |
1475 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1476 GaimGroup *group; | |
1477 | |
1478 g_return_val_if_fail(name != NULL, NULL); | |
1479 g_return_val_if_fail(*name != '\0', NULL); | |
1480 | |
1481 group = gaim_find_group(name); | |
1482 if (group != NULL) | |
1483 return group; | |
1484 | |
1485 group = g_new0(GaimGroup, 1); | |
1486 group->name = g_strdup(name); | |
1487 group->totalsize = 0; | |
1488 group->currentsize = 0; | |
1489 group->online = 0; | |
1490 gaim_blist_node_initialize_settings((GaimBlistNode *)group); | |
1491 ((GaimBlistNode *)group)->type = GAIM_BLIST_GROUP_NODE; | |
1492 | |
1493 if (ops && ops->new_node) | |
1494 ops->new_node((GaimBlistNode *)group); | |
1495 | |
1496 GAIM_DBUS_REGISTER_POINTER(group, GaimGroup); | |
1497 return group; | |
1498 } | |
1499 | |
1500 void gaim_blist_add_contact(GaimContact *contact, GaimGroup *group, GaimBlistNode *node) | |
1501 { | |
1502 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1503 GaimGroup *g; | |
1504 GaimBlistNode *gnode, *cnode, *bnode; | |
1505 | |
1506 g_return_if_fail(contact != NULL); | |
1507 g_return_if_fail(GAIM_BLIST_NODE_IS_CONTACT((GaimBlistNode*)contact)); | |
1508 | |
1509 if ((GaimBlistNode*)contact == node) | |
1510 return; | |
1511 | |
1512 if (node && (GAIM_BLIST_NODE_IS_CONTACT(node) || | |
1513 GAIM_BLIST_NODE_IS_CHAT(node))) | |
1514 g = (GaimGroup*)node->parent; | |
1515 else if (group) | |
1516 g = group; | |
1517 else { | |
1518 g = gaim_group_new(_("Buddies")); | |
1519 gaim_blist_add_group(g, | |
1520 gaim_blist_get_last_sibling(gaimbuddylist->root)); | |
1521 } | |
1522 | |
1523 gnode = (GaimBlistNode*)g; | |
1524 cnode = (GaimBlistNode*)contact; | |
1525 | |
1526 if (cnode->parent) { | |
1527 if (cnode->parent->child == cnode) | |
1528 cnode->parent->child = cnode->next; | |
1529 if (cnode->prev) | |
1530 cnode->prev->next = cnode->next; | |
1531 if (cnode->next) | |
1532 cnode->next->prev = cnode->prev; | |
1533 | |
1534 if (cnode->parent != gnode) { | |
1535 bnode = cnode->child; | |
1536 while (bnode) { | |
1537 GaimBlistNode *next_bnode = bnode->next; | |
1538 GaimBuddy *b = (GaimBuddy*)bnode; | |
1539 | |
1540 struct _gaim_hbuddy *hb = g_new(struct _gaim_hbuddy, 1); | |
1541 hb->name = g_strdup(gaim_normalize(b->account, b->name)); | |
1542 hb->account = b->account; | |
1543 hb->group = cnode->parent; | |
1544 | |
1545 g_hash_table_remove(gaimbuddylist->buddies, hb); | |
1546 | |
1547 if (!gaim_find_buddy_in_group(b->account, b->name, g)) { | |
1548 hb->group = gnode; | |
1549 g_hash_table_replace(gaimbuddylist->buddies, hb, b); | |
1550 | |
1551 if (b->account->gc) | |
1552 serv_move_buddy(b, (GaimGroup *)cnode->parent, g); | |
1553 } else { | |
1554 gboolean empty_contact = FALSE; | |
1555 | |
1556 /* this buddy already exists in the group, so we're | |
1557 * gonna delete it instead */ | |
1558 g_free(hb->name); | |
1559 g_free(hb); | |
1560 if (b->account->gc) | |
1561 gaim_account_remove_buddy(b->account, b, (GaimGroup *)cnode->parent); | |
1562 | |
1563 if (!cnode->child->next) | |
1564 empty_contact = TRUE; | |
1565 gaim_blist_remove_buddy(b); | |
1566 | |
1567 /** in gaim_blist_remove_buddy(), if the last buddy in a | |
1568 * contact is removed, the contact is cleaned up and | |
1569 * g_free'd, so we mustn't try to reference bnode->next */ | |
1570 if (empty_contact) | |
1571 return; | |
1572 } | |
1573 bnode = next_bnode; | |
1574 } | |
1575 } | |
1576 | |
1577 if (contact->online > 0) | |
1578 ((GaimGroup*)cnode->parent)->online--; | |
1579 if (contact->currentsize > 0) | |
1580 ((GaimGroup*)cnode->parent)->currentsize--; | |
1581 ((GaimGroup*)cnode->parent)->totalsize--; | |
1582 | |
1583 if (ops && ops->remove) | |
1584 ops->remove(gaimbuddylist, cnode); | |
1585 | |
1586 gaim_blist_schedule_save(); | |
1587 } | |
1588 | |
1589 if (node && (GAIM_BLIST_NODE_IS_CONTACT(node) || | |
1590 GAIM_BLIST_NODE_IS_CHAT(node))) { | |
1591 if (node->next) | |
1592 node->next->prev = cnode; | |
1593 cnode->next = node->next; | |
1594 cnode->prev = node; | |
1595 cnode->parent = node->parent; | |
1596 node->next = cnode; | |
1597 } else { | |
1598 if (gnode->child) | |
1599 gnode->child->prev = cnode; | |
1600 cnode->prev = NULL; | |
1601 cnode->next = gnode->child; | |
1602 gnode->child = cnode; | |
1603 cnode->parent = gnode; | |
1604 } | |
1605 | |
1606 if (contact->online > 0) | |
1607 g->online++; | |
1608 if (contact->currentsize > 0) | |
1609 g->currentsize++; | |
1610 g->totalsize++; | |
1611 | |
1612 gaim_blist_schedule_save(); | |
1613 | |
1614 if (ops && ops->update) | |
1615 { | |
1616 if (cnode->child) | |
1617 ops->update(gaimbuddylist, cnode); | |
1618 | |
1619 for (bnode = cnode->child; bnode; bnode = bnode->next) | |
1620 ops->update(gaimbuddylist, bnode); | |
1621 } | |
1622 } | |
1623 | |
1624 void gaim_blist_merge_contact(GaimContact *source, GaimBlistNode *node) | |
1625 { | |
1626 GaimBlistNode *sourcenode = (GaimBlistNode*)source; | |
1627 GaimBlistNode *targetnode; | |
1628 GaimBlistNode *prev, *cur, *next; | |
1629 GaimContact *target; | |
1630 | |
1631 g_return_if_fail(source != NULL); | |
1632 g_return_if_fail(node != NULL); | |
1633 | |
1634 if (GAIM_BLIST_NODE_IS_CONTACT(node)) { | |
1635 target = (GaimContact *)node; | |
1636 prev = gaim_blist_get_last_child(node); | |
1637 } else if (GAIM_BLIST_NODE_IS_BUDDY(node)) { | |
1638 target = (GaimContact *)node->parent; | |
1639 prev = node; | |
1640 } else { | |
1641 return; | |
1642 } | |
1643 | |
1644 if (source == target || !target) | |
1645 return; | |
1646 | |
1647 targetnode = (GaimBlistNode *)target; | |
1648 next = sourcenode->child; | |
1649 | |
1650 while (next) { | |
1651 cur = next; | |
1652 next = cur->next; | |
1653 if (GAIM_BLIST_NODE_IS_BUDDY(cur)) { | |
1654 gaim_blist_add_buddy((GaimBuddy *)cur, target, NULL, prev); | |
1655 prev = cur; | |
1656 } | |
1657 } | |
1658 } | |
1659 | |
1660 void gaim_blist_add_group(GaimGroup *group, GaimBlistNode *node) | |
1661 { | |
1662 GaimBlistUiOps *ops; | |
1663 GaimBlistNode *gnode = (GaimBlistNode*)group; | |
1664 | |
1665 g_return_if_fail(group != NULL); | |
1666 g_return_if_fail(GAIM_BLIST_NODE_IS_GROUP((GaimBlistNode *)group)); | |
1667 | |
1668 ops = gaim_blist_get_ui_ops(); | |
1669 | |
1670 if (!gaimbuddylist->root) { | |
1671 gaimbuddylist->root = gnode; | |
1672 return; | |
1673 } | |
1674 | |
1675 /* if we're moving to overtop of ourselves, do nothing */ | |
1676 if (gnode == node) | |
1677 return; | |
1678 | |
1679 if (gaim_find_group(group->name)) { | |
1680 /* This is just being moved */ | |
1681 | |
1682 if (ops && ops->remove) | |
1683 ops->remove(gaimbuddylist, (GaimBlistNode *)group); | |
1684 | |
1685 if (gnode == gaimbuddylist->root) | |
1686 gaimbuddylist->root = gnode->next; | |
1687 if (gnode->prev) | |
1688 gnode->prev->next = gnode->next; | |
1689 if (gnode->next) | |
1690 gnode->next->prev = gnode->prev; | |
1691 } | |
1692 | |
1693 if (node && GAIM_BLIST_NODE_IS_GROUP(node)) { | |
1694 gnode->next = node->next; | |
1695 gnode->prev = node; | |
1696 if (node->next) | |
1697 node->next->prev = gnode; | |
1698 node->next = gnode; | |
1699 } else { | |
1700 if (gaimbuddylist->root) | |
1701 gaimbuddylist->root->prev = gnode; | |
1702 gnode->next = gaimbuddylist->root; | |
1703 gnode->prev = NULL; | |
1704 gaimbuddylist->root = gnode; | |
1705 } | |
1706 | |
1707 gaim_blist_schedule_save(); | |
1708 | |
1709 if (ops && ops->update) { | |
1710 ops->update(gaimbuddylist, gnode); | |
1711 for (node = gnode->child; node; node = node->next) | |
1712 ops->update(gaimbuddylist, node); | |
1713 } | |
1714 } | |
1715 | |
1716 void gaim_blist_remove_contact(GaimContact *contact) | |
1717 { | |
1718 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1719 GaimBlistNode *node, *gnode; | |
1720 | |
1721 g_return_if_fail(contact != NULL); | |
1722 | |
1723 node = (GaimBlistNode *)contact; | |
1724 gnode = node->parent; | |
1725 | |
1726 if (node->child) { | |
1727 /* | |
1728 * If this contact has children then remove them. When the last | |
1729 * buddy is removed from the contact, the contact is automatically | |
1730 * deleted. | |
1731 */ | |
1732 while (node->child->next) { | |
1733 gaim_blist_remove_buddy((GaimBuddy*)node->child); | |
1734 } | |
1735 /* | |
1736 * Remove the last buddy and trigger the deletion of the contact. | |
1737 * It would probably be cleaner if contact-deletion was done after | |
1738 * a timeout? Or if it had to be done manually, like below? | |
1739 */ | |
1740 gaim_blist_remove_buddy((GaimBuddy*)node->child); | |
1741 } else { | |
1742 /* Remove the node from its parent */ | |
1743 if (gnode->child == node) | |
1744 gnode->child = node->next; | |
1745 if (node->prev) | |
1746 node->prev->next = node->next; | |
1747 if (node->next) | |
1748 node->next->prev = node->prev; | |
1749 | |
1750 gaim_blist_schedule_save(); | |
1751 | |
1752 /* Update the UI */ | |
1753 if (ops && ops->remove) | |
1754 ops->remove(gaimbuddylist, node); | |
1755 | |
1756 /* Delete the node */ | |
1757 g_hash_table_destroy(contact->node.settings); | |
1758 GAIM_DBUS_UNREGISTER_POINTER(contact); | |
1759 g_free(contact); | |
1760 } | |
1761 } | |
1762 | |
1763 void gaim_blist_remove_buddy(GaimBuddy *buddy) | |
1764 { | |
1765 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1766 GaimBlistNode *node, *cnode, *gnode; | |
1767 GaimContact *contact; | |
1768 GaimGroup *group; | |
1769 struct _gaim_hbuddy hb; | |
1770 | |
1771 g_return_if_fail(buddy != NULL); | |
1772 | |
1773 node = (GaimBlistNode *)buddy; | |
1774 cnode = node->parent; | |
1775 gnode = cnode->parent; | |
1776 contact = (GaimContact *)cnode; | |
1777 group = (GaimGroup *)gnode; | |
1778 | |
1779 /* Delete any buddy icon. */ | |
1780 gaim_buddy_icon_uncache(buddy); | |
1781 | |
1782 /* Remove the node from its parent */ | |
1783 if (node->prev) | |
1784 node->prev->next = node->next; | |
1785 if (node->next) | |
1786 node->next->prev = node->prev; | |
1787 if (cnode->child == node) | |
1788 cnode->child = node->next; | |
1789 | |
1790 /* Adjust size counts */ | |
1791 if (GAIM_BUDDY_IS_ONLINE(buddy)) { | |
1792 contact->online--; | |
1793 if (contact->online == 0) | |
1794 group->online--; | |
1795 } | |
1796 if (gaim_account_is_connected(buddy->account)) { | |
1797 contact->currentsize--; | |
1798 if (contact->currentsize == 0) | |
1799 group->currentsize--; | |
1800 } | |
1801 contact->totalsize--; | |
1802 | |
1803 gaim_blist_schedule_save(); | |
1804 | |
1805 /* Re-sort the contact */ | |
1806 if (contact->priority == buddy) { | |
1807 gaim_contact_invalidate_priority_buddy(contact); | |
1808 if (ops && ops->update) | |
1809 ops->update(gaimbuddylist, cnode); | |
1810 } | |
1811 | |
1812 /* Remove this buddy from the buddies hash table */ | |
1813 hb.name = g_strdup(gaim_normalize(buddy->account, buddy->name)); | |
1814 hb.account = buddy->account; | |
1815 hb.group = ((GaimBlistNode*)buddy)->parent->parent; | |
1816 g_hash_table_remove(gaimbuddylist->buddies, &hb); | |
1817 g_free(hb.name); | |
1818 | |
1819 /* Update the UI */ | |
1820 if (ops && ops->remove) | |
1821 ops->remove(gaimbuddylist, node); | |
1822 | |
1823 /* Signal that the buddy has been removed before freeing the memory for it */ | |
1824 gaim_signal_emit(gaim_blist_get_handle(), "buddy-removed", buddy); | |
1825 | |
1826 /* Delete the node */ | |
1827 if (buddy->icon != NULL) | |
1828 gaim_buddy_icon_unref(buddy->icon); | |
1829 g_hash_table_destroy(buddy->node.settings); | |
1830 gaim_presence_remove_buddy(buddy->presence, buddy); | |
1831 gaim_presence_destroy(buddy->presence); | |
1832 g_free(buddy->name); | |
1833 g_free(buddy->alias); | |
1834 g_free(buddy->server_alias); | |
1835 | |
1836 GAIM_DBUS_UNREGISTER_POINTER(buddy); | |
1837 g_free(buddy); | |
1838 | |
1839 /* FIXME: Once GaimBuddy is a GObject, timeout callbacks can | |
1840 * g_object_ref() it when connecting the callback and | |
1841 * g_object_unref() it in the handler. That way, it won't | |
1842 * get freed while the timeout is pending and this line can | |
1843 * be removed. */ | |
1844 while (g_source_remove_by_user_data((gpointer *)buddy)); | |
1845 | |
1846 /* If the contact is empty then remove it */ | |
1847 if (!cnode->child) | |
1848 gaim_blist_remove_contact(contact); | |
1849 } | |
1850 | |
1851 void gaim_blist_remove_chat(GaimChat *chat) | |
1852 { | |
1853 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1854 GaimBlistNode *node, *gnode; | |
1855 GaimGroup *group; | |
1856 | |
1857 g_return_if_fail(chat != NULL); | |
1858 | |
1859 node = (GaimBlistNode *)chat; | |
1860 gnode = node->parent; | |
1861 group = (GaimGroup *)gnode; | |
1862 | |
1863 if (gnode != NULL) | |
1864 { | |
1865 /* Remove the node from its parent */ | |
1866 if (gnode->child == node) | |
1867 gnode->child = node->next; | |
1868 if (node->prev) | |
1869 node->prev->next = node->next; | |
1870 if (node->next) | |
1871 node->next->prev = node->prev; | |
1872 | |
1873 /* Adjust size counts */ | |
1874 if (gaim_account_is_connected(chat->account)) { | |
1875 group->online--; | |
1876 group->currentsize--; | |
1877 } | |
1878 group->totalsize--; | |
1879 | |
1880 gaim_blist_schedule_save(); | |
1881 } | |
1882 | |
1883 /* Update the UI */ | |
1884 if (ops && ops->remove) | |
1885 ops->remove(gaimbuddylist, node); | |
1886 | |
1887 /* Delete the node */ | |
1888 g_hash_table_destroy(chat->components); | |
1889 g_hash_table_destroy(chat->node.settings); | |
1890 g_free(chat->alias); | |
1891 GAIM_DBUS_UNREGISTER_POINTER(chat); | |
1892 g_free(chat); | |
1893 } | |
1894 | |
1895 void gaim_blist_remove_group(GaimGroup *group) | |
1896 { | |
1897 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
1898 GaimBlistNode *node; | |
1899 GList *l; | |
1900 | |
1901 g_return_if_fail(group != NULL); | |
1902 | |
1903 node = (GaimBlistNode *)group; | |
1904 | |
1905 /* Make sure the group is empty */ | |
1906 if (node->child) { | |
1907 char *buf; | |
1908 int count = 0; | |
1909 GaimBlistNode *child; | |
1910 | |
1911 for (child = node->child; child != NULL; child = child->next) | |
1912 count++; | |
1913 | |
1914 buf = g_strdup_printf(ngettext("%d buddy from group %s was not removed " | |
1915 "because it belongs to an account which is " | |
1916 "disabled or offline. This buddy and the " | |
1917 "group were not removed.\n", | |
1918 "%d buddies from group %s were not " | |
1919 "removed because they belong to accounts " | |
1920 "which are currently disabled or offline. " | |
1921 "These buddies and the group were not " | |
1922 "removed.\n", count), | |
1923 count, group->name); | |
1924 gaim_notify_error(NULL, NULL, _("Group not removed"), buf); | |
1925 g_free(buf); | |
1926 return; | |
1927 } | |
1928 | |
1929 /* Remove the node from its parent */ | |
1930 if (gaimbuddylist->root == node) | |
1931 gaimbuddylist->root = node->next; | |
1932 if (node->prev) | |
1933 node->prev->next = node->next; | |
1934 if (node->next) | |
1935 node->next->prev = node->prev; | |
1936 | |
1937 gaim_blist_schedule_save(); | |
1938 | |
1939 /* Update the UI */ | |
1940 if (ops && ops->remove) | |
1941 ops->remove(gaimbuddylist, node); | |
1942 | |
1943 /* Remove the group from all accounts that are online */ | |
1944 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
1945 { | |
1946 GaimConnection *gc = (GaimConnection *)l->data; | |
1947 | |
1948 if (gaim_connection_get_state(gc) == GAIM_CONNECTED) | |
1949 gaim_account_remove_group(gaim_connection_get_account(gc), group); | |
1950 } | |
1951 | |
1952 /* Delete the node */ | |
1953 g_hash_table_destroy(group->node.settings); | |
1954 g_free(group->name); | |
1955 GAIM_DBUS_UNREGISTER_POINTER(group); | |
1956 g_free(group); | |
1957 } | |
1958 | |
1959 GaimBuddy *gaim_contact_get_priority_buddy(GaimContact *contact) | |
1960 { | |
1961 g_return_val_if_fail(contact != NULL, NULL); | |
1962 | |
1963 if (!contact->priority_valid) | |
1964 gaim_contact_compute_priority_buddy(contact); | |
1965 | |
1966 return contact->priority; | |
1967 } | |
1968 | |
1969 const char *gaim_buddy_get_alias_only(GaimBuddy *buddy) | |
1970 { | |
1971 g_return_val_if_fail(buddy != NULL, NULL); | |
1972 | |
1973 if ((buddy->alias != NULL) && (*buddy->alias != '\0')) { | |
1974 return buddy->alias; | |
1975 } else if ((buddy->server_alias != NULL) && | |
1976 (*buddy->server_alias != '\0')) { | |
1977 | |
1978 return buddy->server_alias; | |
1979 } | |
1980 | |
1981 return NULL; | |
1982 } | |
1983 | |
1984 | |
1985 const char *gaim_buddy_get_contact_alias(GaimBuddy *buddy) | |
1986 { | |
1987 GaimContact *c; | |
1988 | |
1989 g_return_val_if_fail(buddy != NULL, NULL); | |
1990 | |
1991 /* Search for an alias for the buddy. In order of precedence: */ | |
1992 /* The buddy alias */ | |
1993 if (buddy->alias != NULL) | |
1994 return buddy->alias; | |
1995 | |
1996 /* The contact alias */ | |
1997 c = gaim_buddy_get_contact(buddy); | |
1998 if ((c != NULL) && (c->alias != NULL)) | |
1999 return c->alias; | |
2000 | |
2001 /* The server alias */ | |
2002 if ((buddy->server_alias) && (*buddy->server_alias)) | |
2003 return buddy->server_alias; | |
2004 | |
2005 /* The buddy's user name (i.e. no alias) */ | |
2006 return buddy->name; | |
2007 } | |
2008 | |
2009 | |
2010 const char *gaim_buddy_get_alias(GaimBuddy *buddy) | |
2011 { | |
2012 g_return_val_if_fail(buddy != NULL, NULL); | |
2013 | |
2014 /* Search for an alias for the buddy. In order of precedence: */ | |
2015 /* The buddy alias */ | |
2016 if (buddy->alias != NULL) | |
2017 return buddy->alias; | |
2018 | |
2019 /* The server alias */ | |
2020 if ((buddy->server_alias) && (*buddy->server_alias)) | |
2021 return buddy->server_alias; | |
2022 | |
2023 /* The buddy's user name (i.e. no alias) */ | |
2024 return buddy->name; | |
2025 } | |
2026 | |
14491
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2027 const char *gaim_buddy_get_server_alias(GaimBuddy *buddy) |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2028 { |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2029 g_return_val_if_fail(buddy != NULL, NULL); |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2030 |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2031 if ((buddy->server_alias) && (*buddy->server_alias)) |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2032 return buddy->server_alias; |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2033 |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2034 return NULL; |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2035 } |
8a1c6ed3dde6
[gaim-migrate @ 17210]
Richard Laager <rlaager@wiktel.com>
parents:
14192
diff
changeset
|
2036 |
14192 | 2037 const char *gaim_buddy_get_local_alias(GaimBuddy *buddy) |
2038 { | |
2039 GaimContact *c; | |
2040 | |
2041 g_return_val_if_fail(buddy != NULL, NULL); | |
2042 | |
2043 /* Search for an alias for the buddy. In order of precedence: */ | |
2044 /* The buddy alias */ | |
2045 if (buddy->alias != NULL) | |
2046 return buddy->alias; | |
2047 | |
2048 /* The contact alias */ | |
2049 c = gaim_buddy_get_contact(buddy); | |
2050 if ((c != NULL) && (c->alias != NULL)) | |
2051 return c->alias; | |
2052 | |
2053 /* The buddy's user name (i.e. no alias) */ | |
2054 return buddy->name; | |
2055 } | |
2056 | |
2057 const char *gaim_chat_get_name(GaimChat *chat) | |
2058 { | |
2059 struct proto_chat_entry *pce; | |
2060 GList *parts; | |
2061 char *ret; | |
2062 | |
2063 g_return_val_if_fail(chat != NULL, NULL); | |
2064 | |
2065 if ((chat->alias != NULL) && (*chat->alias != '\0')) | |
2066 return chat->alias; | |
2067 | |
2068 parts = GAIM_PLUGIN_PROTOCOL_INFO(chat->account->gc->prpl)->chat_info(chat->account->gc); | |
2069 pce = parts->data; | |
2070 ret = g_hash_table_lookup(chat->components, pce->identifier); | |
2071 g_list_foreach(parts, (GFunc)g_free, NULL); | |
2072 g_list_free(parts); | |
2073 | |
2074 return ret; | |
2075 } | |
2076 | |
2077 GaimBuddy *gaim_find_buddy(GaimAccount *account, const char *name) | |
2078 { | |
2079 GaimBuddy *buddy; | |
2080 struct _gaim_hbuddy hb; | |
2081 GaimBlistNode *group; | |
2082 | |
2083 g_return_val_if_fail(gaimbuddylist != NULL, NULL); | |
2084 g_return_val_if_fail(account != NULL, NULL); | |
2085 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL); | |
2086 | |
2087 hb.account = account; | |
2088 hb.name = g_strdup(gaim_normalize(account, name)); | |
2089 | |
2090 for (group = gaimbuddylist->root; group; group = group->next) { | |
2091 hb.group = group; | |
2092 if ((buddy = g_hash_table_lookup(gaimbuddylist->buddies, &hb))) { | |
2093 g_free(hb.name); | |
2094 return buddy; | |
2095 } | |
2096 } | |
2097 g_free(hb.name); | |
2098 | |
2099 return NULL; | |
2100 } | |
2101 | |
2102 GaimBuddy *gaim_find_buddy_in_group(GaimAccount *account, const char *name, | |
2103 GaimGroup *group) | |
2104 { | |
2105 struct _gaim_hbuddy hb; | |
2106 GaimBuddy *ret; | |
2107 | |
2108 g_return_val_if_fail(gaimbuddylist != NULL, NULL); | |
2109 g_return_val_if_fail(account != NULL, NULL); | |
2110 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL); | |
2111 | |
2112 hb.name = g_strdup(gaim_normalize(account, name)); | |
2113 hb.account = account; | |
2114 hb.group = (GaimBlistNode*)group; | |
2115 | |
2116 ret = g_hash_table_lookup(gaimbuddylist->buddies, &hb); | |
2117 g_free(hb.name); | |
2118 | |
2119 return ret; | |
2120 } | |
2121 | |
2122 GSList *gaim_find_buddies(GaimAccount *account, const char *name) | |
2123 { | |
2124 struct buddy *buddy; | |
2125 struct _gaim_hbuddy hb; | |
2126 GaimBlistNode *node; | |
2127 GSList *ret = NULL; | |
2128 | |
2129 g_return_val_if_fail(gaimbuddylist != NULL, NULL); | |
2130 g_return_val_if_fail(account != NULL, NULL); | |
2131 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL); | |
2132 | |
2133 hb.name = g_strdup(gaim_normalize(account, name)); | |
2134 hb.account = account; | |
2135 | |
2136 for (node = gaimbuddylist->root; node != NULL; node = node->next) { | |
2137 hb.group = node; | |
2138 if ((buddy = g_hash_table_lookup(gaimbuddylist->buddies, &hb)) != NULL) | |
2139 ret = g_slist_append(ret, buddy); | |
2140 } | |
2141 g_free(hb.name); | |
2142 | |
2143 return ret; | |
2144 } | |
2145 | |
2146 GaimGroup *gaim_find_group(const char *name) | |
2147 { | |
2148 GaimBlistNode *node; | |
2149 | |
2150 g_return_val_if_fail(gaimbuddylist != NULL, NULL); | |
2151 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL); | |
2152 | |
2153 for (node = gaimbuddylist->root; node != NULL; node = node->next) { | |
2154 if (!strcmp(((GaimGroup *)node)->name, name)) | |
2155 return (GaimGroup *)node; | |
2156 } | |
2157 | |
2158 return NULL; | |
2159 } | |
2160 | |
2161 GaimChat * | |
2162 gaim_blist_find_chat(GaimAccount *account, const char *name) | |
2163 { | |
2164 char *chat_name; | |
2165 GaimChat *chat; | |
2166 GaimPlugin *prpl; | |
2167 GaimPluginProtocolInfo *prpl_info = NULL; | |
2168 struct proto_chat_entry *pce; | |
2169 GaimBlistNode *node, *group; | |
2170 GList *parts; | |
2171 | |
2172 g_return_val_if_fail(gaimbuddylist != NULL, NULL); | |
2173 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL); | |
2174 | |
2175 if (!gaim_account_is_connected(account)) | |
2176 return NULL; | |
2177 | |
2178 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); | |
2179 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
2180 | |
2181 if (prpl_info->find_blist_chat != NULL) | |
2182 return prpl_info->find_blist_chat(account, name); | |
2183 | |
2184 for (group = gaimbuddylist->root; group != NULL; group = group->next) { | |
2185 for (node = group->child; node != NULL; node = node->next) { | |
2186 if (GAIM_BLIST_NODE_IS_CHAT(node)) { | |
2187 | |
2188 chat = (GaimChat*)node; | |
2189 | |
2190 if (account != chat->account) | |
2191 continue; | |
2192 | |
2193 parts = prpl_info->chat_info( | |
2194 gaim_account_get_connection(chat->account)); | |
2195 | |
2196 pce = parts->data; | |
2197 chat_name = g_hash_table_lookup(chat->components, | |
2198 pce->identifier); | |
2199 | |
2200 if (chat->account == account && chat_name != NULL && | |
2201 name != NULL && !strcmp(chat_name, name)) { | |
2202 | |
2203 return chat; | |
2204 } | |
2205 } | |
2206 } | |
2207 } | |
2208 | |
2209 return NULL; | |
2210 } | |
2211 | |
2212 GaimGroup * | |
2213 gaim_chat_get_group(GaimChat *chat) | |
2214 { | |
2215 g_return_val_if_fail(chat != NULL, NULL); | |
2216 | |
2217 return (GaimGroup *)(((GaimBlistNode *)chat)->parent); | |
2218 } | |
2219 | |
2220 GaimContact *gaim_buddy_get_contact(GaimBuddy *buddy) | |
2221 { | |
2222 g_return_val_if_fail(buddy != NULL, NULL); | |
2223 | |
2224 return (GaimContact*)((GaimBlistNode*)buddy)->parent; | |
2225 } | |
2226 | |
2227 GaimPresence *gaim_buddy_get_presence(const GaimBuddy *buddy) | |
2228 { | |
2229 g_return_val_if_fail(buddy != NULL, NULL); | |
2230 return buddy->presence; | |
2231 } | |
2232 | |
2233 GaimGroup *gaim_buddy_get_group(GaimBuddy *buddy) | |
2234 { | |
2235 g_return_val_if_fail(buddy != NULL, NULL); | |
2236 | |
2237 if (((GaimBlistNode *)buddy)->parent == NULL) | |
2238 return NULL; | |
2239 | |
2240 return (GaimGroup *)(((GaimBlistNode*)buddy)->parent->parent); | |
2241 } | |
2242 | |
2243 GSList *gaim_group_get_accounts(GaimGroup *group) | |
2244 { | |
2245 GSList *l = NULL; | |
2246 GaimBlistNode *gnode, *cnode, *bnode; | |
2247 | |
2248 gnode = (GaimBlistNode *)group; | |
2249 | |
2250 for (cnode = gnode->child; cnode; cnode = cnode->next) { | |
2251 if (GAIM_BLIST_NODE_IS_CHAT(cnode)) { | |
2252 if (!g_slist_find(l, ((GaimChat *)cnode)->account)) | |
2253 l = g_slist_append(l, ((GaimChat *)cnode)->account); | |
2254 } else if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) { | |
2255 for (bnode = cnode->child; bnode; bnode = bnode->next) { | |
2256 if (GAIM_BLIST_NODE_IS_BUDDY(bnode)) { | |
2257 if (!g_slist_find(l, ((GaimBuddy *)bnode)->account)) | |
2258 l = g_slist_append(l, ((GaimBuddy *)bnode)->account); | |
2259 } | |
2260 } | |
2261 } | |
2262 } | |
2263 | |
2264 return l; | |
2265 } | |
2266 | |
2267 void gaim_blist_add_account(GaimAccount *account) | |
2268 { | |
2269 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
2270 GaimBlistNode *gnode, *cnode, *bnode; | |
2271 | |
2272 g_return_if_fail(gaimbuddylist != NULL); | |
2273 | |
2274 if (!ops || !ops->update) | |
2275 return; | |
2276 | |
2277 for (gnode = gaimbuddylist->root; gnode; gnode = gnode->next) { | |
2278 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
2279 continue; | |
2280 for (cnode = gnode->child; cnode; cnode = cnode->next) { | |
2281 if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) { | |
2282 gboolean recompute = FALSE; | |
2283 for (bnode = cnode->child; bnode; bnode = bnode->next) { | |
2284 if (GAIM_BLIST_NODE_IS_BUDDY(bnode) && | |
2285 ((GaimBuddy*)bnode)->account == account) { | |
2286 recompute = TRUE; | |
2287 ((GaimContact*)cnode)->currentsize++; | |
2288 if (((GaimContact*)cnode)->currentsize == 1) | |
2289 ((GaimGroup*)gnode)->currentsize++; | |
2290 ops->update(gaimbuddylist, bnode); | |
2291 } | |
2292 } | |
2293 if (recompute || | |
2294 gaim_blist_node_get_bool(cnode, "show_offline")) { | |
2295 gaim_contact_invalidate_priority_buddy((GaimContact*)cnode); | |
2296 ops->update(gaimbuddylist, cnode); | |
2297 } | |
2298 } else if (GAIM_BLIST_NODE_IS_CHAT(cnode) && | |
2299 ((GaimChat*)cnode)->account == account) { | |
2300 ((GaimGroup *)gnode)->online++; | |
2301 ((GaimGroup *)gnode)->currentsize++; | |
2302 ops->update(gaimbuddylist, cnode); | |
2303 } | |
2304 } | |
2305 ops->update(gaimbuddylist, gnode); | |
2306 } | |
2307 } | |
2308 | |
2309 void gaim_blist_remove_account(GaimAccount *account) | |
2310 { | |
2311 GaimBlistUiOps *ops = gaim_blist_get_ui_ops(); | |
2312 GaimBlistNode *gnode, *cnode, *bnode; | |
2313 GaimBuddy *buddy; | |
2314 GaimChat *chat; | |
2315 GaimContact *contact; | |
2316 GaimGroup *group; | |
2317 GList *list = NULL, *iter = NULL; | |
2318 | |
2319 g_return_if_fail(gaimbuddylist != NULL); | |
2320 | |
2321 for (gnode = gaimbuddylist->root; gnode; gnode = gnode->next) { | |
2322 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
2323 continue; | |
2324 | |
2325 group = (GaimGroup *)gnode; | |
2326 | |
2327 for (cnode = gnode->child; cnode; cnode = cnode->next) { | |
2328 if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) { | |
2329 gboolean recompute = FALSE; | |
2330 contact = (GaimContact *)cnode; | |
2331 | |
2332 for (bnode = cnode->child; bnode; bnode = bnode->next) { | |
2333 if (!GAIM_BLIST_NODE_IS_BUDDY(bnode)) | |
2334 continue; | |
2335 | |
2336 buddy = (GaimBuddy *)bnode; | |
2337 if (account == buddy->account) { | |
2338 GaimPresence *presence; | |
2339 recompute = TRUE; | |
2340 | |
2341 presence = gaim_buddy_get_presence(buddy); | |
2342 | |
2343 if(gaim_presence_is_online(presence)) { | |
2344 contact->online--; | |
2345 if (contact->online == 0) | |
2346 group->online--; | |
2347 | |
2348 gaim_blist_node_set_int(&buddy->node, | |
2349 "last_seen", time(NULL)); | |
2350 } | |
2351 | |
2352 contact->currentsize--; | |
2353 if (contact->currentsize == 0) | |
2354 group->currentsize--; | |
2355 | |
2356 if (!g_list_find(list, presence)) | |
2357 list = g_list_prepend(list, presence); | |
2358 | |
2359 if (ops && ops->remove) | |
2360 ops->remove(gaimbuddylist, bnode); | |
2361 } | |
2362 } | |
2363 if (recompute) { | |
2364 gaim_contact_invalidate_priority_buddy(contact); | |
2365 if (ops && ops->update) | |
2366 ops->update(gaimbuddylist, cnode); | |
2367 } | |
2368 } else if (GAIM_BLIST_NODE_IS_CHAT(cnode)) { | |
2369 chat = (GaimChat *)cnode; | |
2370 | |
2371 if(chat->account == account) { | |
2372 group->currentsize--; | |
2373 group->online--; | |
2374 | |
2375 if (ops && ops->remove) | |
2376 ops->remove(gaimbuddylist, cnode); | |
2377 } | |
2378 } | |
2379 } | |
2380 } | |
2381 | |
2382 for (iter = list; iter; iter = iter->next) | |
2383 { | |
2384 gaim_presence_set_status_active(iter->data, "offline", TRUE); | |
2385 } | |
2386 g_list_free(list); | |
2387 } | |
2388 | |
2389 gboolean gaim_group_on_account(GaimGroup *g, GaimAccount *account) | |
2390 { | |
2391 GaimBlistNode *cnode; | |
2392 for (cnode = ((GaimBlistNode *)g)->child; cnode; cnode = cnode->next) { | |
2393 if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) { | |
2394 if(gaim_contact_on_account((GaimContact *) cnode, account)) | |
2395 return TRUE; | |
2396 } else if (GAIM_BLIST_NODE_IS_CHAT(cnode)) { | |
2397 GaimChat *chat = (GaimChat *)cnode; | |
2398 if ((!account && gaim_account_is_connected(chat->account)) | |
2399 || chat->account == account) | |
2400 return TRUE; | |
2401 } | |
2402 } | |
2403 return FALSE; | |
2404 } | |
2405 | |
2406 void | |
2407 gaim_blist_request_add_buddy(GaimAccount *account, const char *username, | |
2408 const char *group, const char *alias) | |
2409 { | |
2410 GaimBlistUiOps *ui_ops; | |
2411 | |
2412 ui_ops = gaim_blist_get_ui_ops(); | |
2413 | |
2414 if (ui_ops != NULL && ui_ops->request_add_buddy != NULL) | |
2415 ui_ops->request_add_buddy(account, username, group, alias); | |
2416 } | |
2417 | |
2418 void | |
2419 gaim_blist_request_add_chat(GaimAccount *account, GaimGroup *group, | |
2420 const char *alias, const char *name) | |
2421 { | |
2422 GaimBlistUiOps *ui_ops; | |
2423 | |
2424 ui_ops = gaim_blist_get_ui_ops(); | |
2425 | |
2426 if (ui_ops != NULL && ui_ops->request_add_chat != NULL) | |
2427 ui_ops->request_add_chat(account, group, alias, name); | |
2428 } | |
2429 | |
2430 void | |
2431 gaim_blist_request_add_group(void) | |
2432 { | |
2433 GaimBlistUiOps *ui_ops; | |
2434 | |
2435 ui_ops = gaim_blist_get_ui_ops(); | |
2436 | |
2437 if (ui_ops != NULL && ui_ops->request_add_group != NULL) | |
2438 ui_ops->request_add_group(); | |
2439 } | |
2440 | |
2441 static void | |
2442 gaim_blist_node_setting_free(gpointer data) | |
2443 { | |
2444 GaimValue *value; | |
2445 | |
2446 value = (GaimValue *)data; | |
2447 | |
2448 gaim_value_destroy(value); | |
2449 } | |
2450 | |
2451 static void gaim_blist_node_initialize_settings(GaimBlistNode *node) | |
2452 { | |
2453 if (node->settings) | |
2454 return; | |
2455 | |
2456 node->settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
2457 (GDestroyNotify)gaim_blist_node_setting_free); | |
2458 } | |
2459 | |
2460 void gaim_blist_node_remove_setting(GaimBlistNode *node, const char *key) | |
2461 { | |
2462 g_return_if_fail(node != NULL); | |
2463 g_return_if_fail(node->settings != NULL); | |
2464 g_return_if_fail(key != NULL); | |
2465 | |
2466 g_hash_table_remove(node->settings, key); | |
2467 | |
2468 gaim_blist_schedule_save(); | |
2469 } | |
2470 | |
2471 void | |
2472 gaim_blist_node_set_flags(GaimBlistNode *node, GaimBlistNodeFlags flags) | |
2473 { | |
2474 g_return_if_fail(node != NULL); | |
2475 | |
2476 node->flags = flags; | |
2477 } | |
2478 | |
2479 GaimBlistNodeFlags | |
2480 gaim_blist_node_get_flags(GaimBlistNode *node) | |
2481 { | |
2482 g_return_val_if_fail(node != NULL, 0); | |
2483 | |
2484 return node->flags; | |
2485 } | |
2486 | |
2487 void | |
2488 gaim_blist_node_set_bool(GaimBlistNode* node, const char *key, gboolean data) | |
2489 { | |
2490 GaimValue *value; | |
2491 | |
2492 g_return_if_fail(node != NULL); | |
2493 g_return_if_fail(node->settings != NULL); | |
2494 g_return_if_fail(key != NULL); | |
2495 | |
2496 value = gaim_value_new(GAIM_TYPE_BOOLEAN); | |
2497 gaim_value_set_boolean(value, data); | |
2498 | |
2499 g_hash_table_replace(node->settings, g_strdup(key), value); | |
2500 | |
2501 gaim_blist_schedule_save(); | |
2502 } | |
2503 | |
2504 gboolean | |
2505 gaim_blist_node_get_bool(GaimBlistNode* node, const char *key) | |
2506 { | |
2507 GaimValue *value; | |
2508 | |
2509 g_return_val_if_fail(node != NULL, FALSE); | |
2510 g_return_val_if_fail(node->settings != NULL, FALSE); | |
2511 g_return_val_if_fail(key != NULL, FALSE); | |
2512 | |
2513 value = g_hash_table_lookup(node->settings, key); | |
2514 | |
2515 if (value == NULL) | |
2516 return FALSE; | |
2517 | |
2518 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_BOOLEAN, FALSE); | |
2519 | |
2520 return gaim_value_get_boolean(value); | |
2521 } | |
2522 | |
2523 void | |
2524 gaim_blist_node_set_int(GaimBlistNode* node, const char *key, int data) | |
2525 { | |
2526 GaimValue *value; | |
2527 | |
2528 g_return_if_fail(node != NULL); | |
2529 g_return_if_fail(node->settings != NULL); | |
2530 g_return_if_fail(key != NULL); | |
2531 | |
2532 value = gaim_value_new(GAIM_TYPE_INT); | |
2533 gaim_value_set_int(value, data); | |
2534 | |
2535 g_hash_table_replace(node->settings, g_strdup(key), value); | |
2536 | |
2537 gaim_blist_schedule_save(); | |
2538 } | |
2539 | |
2540 int | |
2541 gaim_blist_node_get_int(GaimBlistNode* node, const char *key) | |
2542 { | |
2543 GaimValue *value; | |
2544 | |
2545 g_return_val_if_fail(node != NULL, 0); | |
2546 g_return_val_if_fail(node->settings != NULL, 0); | |
2547 g_return_val_if_fail(key != NULL, 0); | |
2548 | |
2549 value = g_hash_table_lookup(node->settings, key); | |
2550 | |
2551 if (value == NULL) | |
2552 return 0; | |
2553 | |
2554 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_INT, 0); | |
2555 | |
2556 return gaim_value_get_int(value); | |
2557 } | |
2558 | |
2559 void | |
2560 gaim_blist_node_set_string(GaimBlistNode* node, const char *key, const char *data) | |
2561 { | |
2562 GaimValue *value; | |
2563 | |
2564 g_return_if_fail(node != NULL); | |
2565 g_return_if_fail(node->settings != NULL); | |
2566 g_return_if_fail(key != NULL); | |
2567 | |
2568 value = gaim_value_new(GAIM_TYPE_STRING); | |
2569 gaim_value_set_string(value, data); | |
2570 | |
2571 g_hash_table_replace(node->settings, g_strdup(key), value); | |
2572 | |
2573 gaim_blist_schedule_save(); | |
2574 } | |
2575 | |
2576 const char * | |
2577 gaim_blist_node_get_string(GaimBlistNode* node, const char *key) | |
2578 { | |
2579 GaimValue *value; | |
2580 | |
2581 g_return_val_if_fail(node != NULL, NULL); | |
2582 g_return_val_if_fail(node->settings != NULL, NULL); | |
2583 g_return_val_if_fail(key != NULL, NULL); | |
2584 | |
2585 value = g_hash_table_lookup(node->settings, key); | |
2586 | |
2587 if (value == NULL) | |
2588 return NULL; | |
2589 | |
2590 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_STRING, NULL); | |
2591 | |
2592 return gaim_value_get_string(value); | |
2593 } | |
2594 | |
2595 GList * | |
2596 gaim_blist_node_get_extended_menu(GaimBlistNode *n) | |
2597 { | |
2598 GList *menu = NULL; | |
2599 | |
2600 g_return_val_if_fail(n != NULL, NULL); | |
2601 | |
2602 gaim_signal_emit(gaim_blist_get_handle(), | |
2603 "blist-node-extended-menu", | |
2604 n, &menu); | |
2605 return menu; | |
2606 } | |
2607 | |
2608 int gaim_blist_get_group_size(GaimGroup *group, gboolean offline) | |
2609 { | |
2610 if (!group) | |
2611 return 0; | |
2612 | |
2613 return offline ? group->totalsize : group->currentsize; | |
2614 } | |
2615 | |
2616 int gaim_blist_get_group_online_count(GaimGroup *group) | |
2617 { | |
2618 if (!group) | |
2619 return 0; | |
2620 | |
2621 return group->online; | |
2622 } | |
2623 | |
2624 void | |
2625 gaim_blist_set_ui_ops(GaimBlistUiOps *ops) | |
2626 { | |
2627 blist_ui_ops = ops; | |
2628 } | |
2629 | |
2630 GaimBlistUiOps * | |
2631 gaim_blist_get_ui_ops(void) | |
2632 { | |
2633 return blist_ui_ops; | |
2634 } | |
2635 | |
2636 | |
2637 void * | |
2638 gaim_blist_get_handle(void) | |
2639 { | |
2640 static int handle; | |
2641 | |
2642 return &handle; | |
2643 } | |
2644 | |
2645 void | |
2646 gaim_blist_init(void) | |
2647 { | |
2648 void *handle = gaim_blist_get_handle(); | |
2649 | |
2650 gaim_signal_register(handle, "buddy-status-changed", | |
2651 gaim_marshal_VOID__POINTER_POINTER_POINTER, NULL, | |
2652 3, | |
2653 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2654 GAIM_SUBTYPE_BLIST_BUDDY), | |
2655 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2656 GAIM_SUBTYPE_STATUS), | |
2657 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2658 GAIM_SUBTYPE_STATUS)); | |
2659 gaim_signal_register(handle, "buddy-privacy-changed", | |
2660 gaim_marshal_VOID__POINTER, NULL, | |
2661 1, | |
2662 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2663 GAIM_SUBTYPE_BLIST_BUDDY)); | |
2664 | |
2665 gaim_signal_register(handle, "buddy-idle-changed", | |
2666 gaim_marshal_VOID__POINTER_INT_INT, NULL, | |
2667 3, | |
2668 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2669 GAIM_SUBTYPE_BLIST_BUDDY), | |
2670 gaim_value_new(GAIM_TYPE_INT), | |
2671 gaim_value_new(GAIM_TYPE_INT)); | |
2672 | |
2673 | |
2674 gaim_signal_register(handle, "buddy-signed-on", | |
2675 gaim_marshal_VOID__POINTER, NULL, 1, | |
2676 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2677 GAIM_SUBTYPE_BLIST_BUDDY)); | |
2678 | |
2679 gaim_signal_register(handle, "buddy-signed-off", | |
2680 gaim_marshal_VOID__POINTER, NULL, 1, | |
2681 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2682 GAIM_SUBTYPE_BLIST_BUDDY)); | |
2683 | |
2684 gaim_signal_register(handle, "buddy-added", | |
2685 gaim_marshal_VOID__POINTER, NULL, 1, | |
2686 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2687 GAIM_SUBTYPE_BLIST_BUDDY)); | |
2688 | |
2689 gaim_signal_register(handle, "buddy-removed", | |
2690 gaim_marshal_VOID__POINTER, NULL, 1, | |
2691 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2692 GAIM_SUBTYPE_BLIST_BUDDY)); | |
2693 | |
2694 gaim_signal_register(handle, "buddy-icon-changed", | |
2695 gaim_marshal_VOID__POINTER, NULL, 1, | |
2696 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2697 GAIM_SUBTYPE_BLIST_BUDDY)); | |
2698 | |
2699 gaim_signal_register(handle, "update-idle", gaim_marshal_VOID, NULL, 0); | |
2700 | |
2701 gaim_signal_register(handle, "blist-node-extended-menu", | |
2702 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
2703 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2704 GAIM_SUBTYPE_BLIST_NODE), | |
2705 gaim_value_new(GAIM_TYPE_BOXED, "GList **")); | |
2706 | |
2707 gaim_signal_register(handle, "blist-node-aliased", | |
2708 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, | |
2709 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
2710 GAIM_SUBTYPE_BLIST_NODE), | |
2711 gaim_value_new(GAIM_TYPE_STRING)); | |
2712 } | |
2713 | |
2714 void | |
2715 gaim_blist_uninit(void) | |
2716 { | |
2717 if (save_timer != 0) | |
2718 { | |
2719 gaim_timeout_remove(save_timer); | |
2720 save_timer = 0; | |
2721 gaim_blist_sync(); | |
2722 } | |
2723 | |
2724 gaim_signals_unregister_by_instance(gaim_blist_get_handle()); | |
2725 } |