comparison libgaim/blist.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children 8a1c6ed3dde6
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
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 char *old_alias = contact->alias;
843 GaimBlistNode *bnode;
844
845 g_return_if_fail(contact != NULL);
846
847 if ((alias != NULL) && (*alias != '\0'))
848 contact->alias = g_strdup(alias);
849 else
850 contact->alias = NULL;
851
852 gaim_blist_schedule_save();
853
854 if (ops && ops->update)
855 ops->update(gaimbuddylist, (GaimBlistNode *)contact);
856
857 for(bnode = ((GaimBlistNode *)contact)->child; bnode != NULL; bnode = bnode->next)
858 {
859 GaimBuddy *buddy = (GaimBuddy *)bnode;
860
861 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, buddy->name,
862 buddy->account);
863 if (conv)
864 gaim_conversation_autoset_title(conv);
865 }
866
867 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased",
868 contact, old_alias);
869 g_free(old_alias);
870 }
871
872 void gaim_blist_alias_chat(GaimChat *chat, const char *alias)
873 {
874 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
875 char *old_alias = chat->alias;
876
877 g_return_if_fail(chat != NULL);
878
879 if ((alias != NULL) && (*alias != '\0'))
880 chat->alias = g_strdup(alias);
881 else
882 chat->alias = NULL;
883
884 gaim_blist_schedule_save();
885
886 if (ops && ops->update)
887 ops->update(gaimbuddylist, (GaimBlistNode *)chat);
888
889 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased",
890 chat, old_alias);
891 g_free(old_alias);
892 }
893
894 void gaim_blist_alias_buddy(GaimBuddy *buddy, const char *alias)
895 {
896 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
897 GaimConversation *conv;
898 char *old_alias = buddy->alias;
899
900 g_return_if_fail(buddy != NULL);
901
902 if ((alias != NULL) && (*alias != '\0'))
903 buddy->alias = g_strdup(alias);
904 else
905 buddy->alias = NULL;
906
907 gaim_blist_schedule_save();
908
909 if (ops && ops->update)
910 ops->update(gaimbuddylist, (GaimBlistNode *)buddy);
911
912 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, buddy->name,
913 buddy->account);
914 if (conv)
915 gaim_conversation_autoset_title(conv);
916
917 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased",
918 buddy, old_alias);
919 g_free(old_alias);
920 }
921
922 void gaim_blist_server_alias_buddy(GaimBuddy *buddy, const char *alias)
923 {
924 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
925 GaimConversation *conv;
926 char *old_alias = buddy->server_alias;
927
928 g_return_if_fail(buddy != NULL);
929
930 if ((alias != NULL) && (*alias != '\0') && g_utf8_validate(alias, -1, NULL))
931 buddy->server_alias = g_strdup(alias);
932 else
933 buddy->server_alias = NULL;
934
935 gaim_blist_schedule_save();
936
937 if (ops && ops->update)
938 ops->update(gaimbuddylist, (GaimBlistNode *)buddy);
939
940 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, buddy->name,
941 buddy->account);
942 if (conv)
943 gaim_conversation_autoset_title(conv);
944
945 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased",
946 buddy, old_alias);
947 g_free(old_alias);
948 }
949
950 /*
951 * TODO: If merging, prompt the user if they want to merge.
952 */
953 void gaim_blist_rename_group(GaimGroup *source, const char *new_name)
954 {
955 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
956 GaimGroup *dest;
957 gchar *old_name;
958 GList *moved_buddies = NULL;
959 GSList *accts;
960
961 g_return_if_fail(source != NULL);
962 g_return_if_fail(new_name != NULL);
963
964 if (*new_name == '\0' || !strcmp(new_name, source->name))
965 return;
966
967 dest = gaim_find_group(new_name);
968 if (dest != NULL) {
969 /* We're merging two groups */
970 GaimBlistNode *prev, *child, *next;
971
972 prev = gaim_blist_get_last_child((GaimBlistNode*)dest);
973 child = ((GaimBlistNode*)source)->child;
974
975 /*
976 * TODO: This seems like a dumb way to do this... why not just
977 * append all children from the old group to the end of the new
978 * one? PRPLs might be expecting to receive an add_buddy() for
979 * each moved buddy...
980 */
981 while (child)
982 {
983 next = child->next;
984 if (GAIM_BLIST_NODE_IS_CONTACT(child)) {
985 GaimBlistNode *bnode;
986 gaim_blist_add_contact((GaimContact *)child, dest, prev);
987 for (bnode = child->child; bnode != NULL; bnode = bnode->next) {
988 gaim_blist_add_buddy((GaimBuddy *)bnode, (GaimContact *)child,
989 NULL, bnode->prev);
990 moved_buddies = g_list_append(moved_buddies, bnode);
991 }
992 prev = child;
993 } else if (GAIM_BLIST_NODE_IS_CHAT(child)) {
994 gaim_blist_add_chat((GaimChat *)child, dest, prev);
995 prev = child;
996 } else {
997 gaim_debug(GAIM_DEBUG_ERROR, "blist",
998 "Unknown child type in group %s\n", source->name);
999 }
1000 child = next;
1001 }
1002
1003 /* Make a copy of the old group name and then delete the old group */
1004 old_name = g_strdup(source->name);
1005 gaim_blist_remove_group(source);
1006 source = dest;
1007 } else {
1008 /* A simple rename */
1009 GaimBlistNode *cnode, *bnode;
1010
1011 /* Build a GList of all buddies in this group */
1012 for (cnode = ((GaimBlistNode *)source)->child; cnode != NULL; cnode = cnode->next) {
1013 if (GAIM_BLIST_NODE_IS_CONTACT(cnode))
1014 for (bnode = cnode->child; bnode != NULL; bnode = bnode->next)
1015 moved_buddies = g_list_append(moved_buddies, bnode);
1016 }
1017
1018 old_name = source->name;
1019 source->name = g_strdup(new_name);
1020 }
1021
1022 /* Save our changes */
1023 gaim_blist_schedule_save();
1024
1025 /* Update the UI */
1026 if (ops && ops->update)
1027 ops->update(gaimbuddylist, (GaimBlistNode*)source);
1028
1029 /* Notify all PRPLs */
1030 /* TODO: Is this condition needed? Seems like it would always be TRUE */
1031 if(old_name && source && strcmp(source->name, old_name)) {
1032 for (accts = gaim_group_get_accounts(source); accts; accts = g_slist_remove(accts, accts->data)) {
1033 GaimAccount *account = accts->data;
1034 GaimPluginProtocolInfo *prpl_info = NULL;
1035 GList *l = NULL, *buddies = NULL;
1036
1037 if(account->gc && account->gc->prpl)
1038 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(account->gc->prpl);
1039
1040 if(!prpl_info)
1041 continue;
1042
1043 for(l = moved_buddies; l; l = l->next) {
1044 GaimBuddy *buddy = (GaimBuddy *)l->data;
1045
1046 if(buddy && buddy->account == account)
1047 buddies = g_list_append(buddies, (GaimBlistNode *)buddy);
1048 }
1049
1050 if(prpl_info->rename_group) {
1051 prpl_info->rename_group(account->gc, old_name, source, buddies);
1052 } else {
1053 GList *cur, *groups = NULL;
1054
1055 /* Make a list of what the groups each buddy is in */
1056 for(cur = buddies; cur; cur = cur->next) {
1057 GaimBlistNode *node = (GaimBlistNode *)cur->data;
1058 groups = g_list_prepend(groups, node->parent->parent);
1059 }
1060
1061 gaim_account_remove_buddies(account, buddies, groups);
1062 g_list_free(groups);
1063 gaim_account_add_buddies(account, buddies);
1064 }
1065
1066 g_list_free(buddies);
1067 }
1068 }
1069 g_list_free(moved_buddies);
1070 g_free(old_name);
1071 }
1072
1073 static void gaim_blist_node_initialize_settings(GaimBlistNode *node);
1074
1075 GaimChat *gaim_chat_new(GaimAccount *account, const char *alias, GHashTable *components)
1076 {
1077 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1078 GaimChat *chat;
1079
1080 g_return_val_if_fail(account != NULL, FALSE);
1081 g_return_val_if_fail(components != NULL, FALSE);
1082
1083 chat = g_new0(GaimChat, 1);
1084 chat->account = account;
1085 if ((alias != NULL) && (*alias != '\0'))
1086 chat->alias = g_strdup(alias);
1087 chat->components = components;
1088 gaim_blist_node_initialize_settings((GaimBlistNode *)chat);
1089 ((GaimBlistNode *)chat)->type = GAIM_BLIST_CHAT_NODE;
1090
1091 if (ops != NULL && ops->new_node != NULL)
1092 ops->new_node((GaimBlistNode *)chat);
1093
1094 GAIM_DBUS_REGISTER_POINTER(chat, GaimChat);
1095 return chat;
1096 }
1097
1098 GaimBuddy *gaim_buddy_new(GaimAccount *account, const char *screenname, const char *alias)
1099 {
1100 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1101 GaimBuddy *buddy;
1102
1103 g_return_val_if_fail(account != NULL, FALSE);
1104 g_return_val_if_fail(screenname != NULL, FALSE);
1105
1106 buddy = g_new0(GaimBuddy, 1);
1107 buddy->account = account;
1108 buddy->name = g_strdup(screenname);
1109 buddy->alias = g_strdup(alias);
1110 buddy->presence = gaim_presence_new_for_buddy(buddy);
1111 ((GaimBlistNode *)buddy)->type = GAIM_BLIST_BUDDY_NODE;
1112
1113 gaim_presence_set_status_active(buddy->presence, "offline", TRUE);
1114
1115 gaim_blist_node_initialize_settings((GaimBlistNode *)buddy);
1116
1117 if (ops && ops->new_node)
1118 ops->new_node((GaimBlistNode *)buddy);
1119
1120 GAIM_DBUS_REGISTER_POINTER(buddy, GaimBuddy);
1121 return buddy;
1122 }
1123
1124 void
1125 gaim_buddy_set_icon(GaimBuddy *buddy, GaimBuddyIcon *icon)
1126 {
1127 g_return_if_fail(buddy != NULL);
1128
1129 if (buddy->icon != icon) {
1130 if (buddy->icon != NULL)
1131 gaim_buddy_icon_unref(buddy->icon);
1132
1133 buddy->icon = (icon != NULL ? gaim_buddy_icon_ref(icon) : NULL);
1134 }
1135
1136 if (buddy->icon)
1137 gaim_buddy_icon_cache(icon, buddy);
1138 else
1139 gaim_buddy_icon_uncache(buddy);
1140
1141 gaim_blist_schedule_save();
1142
1143 gaim_signal_emit(gaim_blist_get_handle(), "buddy-icon-changed", buddy);
1144
1145 gaim_blist_update_buddy_icon(buddy);
1146 }
1147
1148 GaimAccount *
1149 gaim_buddy_get_account(const GaimBuddy *buddy)
1150 {
1151 g_return_val_if_fail(buddy != NULL, NULL);
1152
1153 return buddy->account;
1154 }
1155
1156 const char *
1157 gaim_buddy_get_name(const GaimBuddy *buddy)
1158 {
1159 g_return_val_if_fail(buddy != NULL, NULL);
1160
1161 return buddy->name;
1162 }
1163
1164 GaimBuddyIcon *
1165 gaim_buddy_get_icon(const GaimBuddy *buddy)
1166 {
1167 g_return_val_if_fail(buddy != NULL, NULL);
1168
1169 return buddy->icon;
1170 }
1171
1172 void gaim_blist_add_chat(GaimChat *chat, GaimGroup *group, GaimBlistNode *node)
1173 {
1174 GaimBlistNode *cnode = (GaimBlistNode*)chat;
1175 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1176
1177 g_return_if_fail(chat != NULL);
1178 g_return_if_fail(GAIM_BLIST_NODE_IS_CHAT((GaimBlistNode *)chat));
1179
1180 if (node == NULL) {
1181 if (group == NULL) {
1182 group = gaim_group_new(_("Chats"));
1183 gaim_blist_add_group(group,
1184 gaim_blist_get_last_sibling(gaimbuddylist->root));
1185 }
1186 } else {
1187 group = (GaimGroup*)node->parent;
1188 }
1189
1190 /* if we're moving to overtop of ourselves, do nothing */
1191 if (cnode == node)
1192 return;
1193
1194 if (cnode->parent) {
1195 /* This chat was already in the list and is
1196 * being moved.
1197 */
1198 ((GaimGroup *)cnode->parent)->totalsize--;
1199 if (gaim_account_is_connected(chat->account)) {
1200 ((GaimGroup *)cnode->parent)->online--;
1201 ((GaimGroup *)cnode->parent)->currentsize--;
1202 }
1203 if (cnode->next)
1204 cnode->next->prev = cnode->prev;
1205 if (cnode->prev)
1206 cnode->prev->next = cnode->next;
1207 if (cnode->parent->child == cnode)
1208 cnode->parent->child = cnode->next;
1209
1210 if (ops && ops->remove)
1211 ops->remove(gaimbuddylist, cnode);
1212 /* ops->remove() cleaned up the cnode's ui_data, so we need to
1213 * reinitialize it */
1214 if (ops && ops->new_node)
1215 ops->new_node(cnode);
1216
1217 gaim_blist_schedule_save();
1218 }
1219
1220 if (node != NULL) {
1221 if (node->next)
1222 node->next->prev = cnode;
1223 cnode->next = node->next;
1224 cnode->prev = node;
1225 cnode->parent = node->parent;
1226 node->next = cnode;
1227 ((GaimGroup *)node->parent)->totalsize++;
1228 if (gaim_account_is_connected(chat->account)) {
1229 ((GaimGroup *)node->parent)->online++;
1230 ((GaimGroup *)node->parent)->currentsize++;
1231 }
1232 } else {
1233 if (((GaimBlistNode *)group)->child)
1234 ((GaimBlistNode *)group)->child->prev = cnode;
1235 cnode->next = ((GaimBlistNode *)group)->child;
1236 cnode->prev = NULL;
1237 ((GaimBlistNode *)group)->child = cnode;
1238 cnode->parent = (GaimBlistNode *)group;
1239 group->totalsize++;
1240 if (gaim_account_is_connected(chat->account)) {
1241 group->online++;
1242 group->currentsize++;
1243 }
1244 }
1245
1246 gaim_blist_schedule_save();
1247
1248 if (ops && ops->update)
1249 ops->update(gaimbuddylist, (GaimBlistNode *)cnode);
1250 }
1251
1252 void gaim_blist_add_buddy(GaimBuddy *buddy, GaimContact *contact, GaimGroup *group, GaimBlistNode *node)
1253 {
1254 GaimBlistNode *cnode, *bnode;
1255 GaimGroup *g;
1256 GaimContact *c;
1257 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1258 struct _gaim_hbuddy *hb;
1259
1260 g_return_if_fail(buddy != NULL);
1261 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY((GaimBlistNode*)buddy));
1262
1263 bnode = (GaimBlistNode *)buddy;
1264
1265 /* if we're moving to overtop of ourselves, do nothing */
1266 if (bnode == node || (!node && bnode->parent &&
1267 contact && bnode->parent == (GaimBlistNode*)contact
1268 && bnode == bnode->parent->child))
1269 return;
1270
1271 if (node && GAIM_BLIST_NODE_IS_BUDDY(node)) {
1272 c = (GaimContact*)node->parent;
1273 g = (GaimGroup*)node->parent->parent;
1274 } else if (contact) {
1275 c = contact;
1276 g = (GaimGroup *)((GaimBlistNode *)c)->parent;
1277 } else {
1278 if (group) {
1279 g = group;
1280 } else {
1281 g = gaim_group_new(_("Buddies"));
1282 gaim_blist_add_group(g,
1283 gaim_blist_get_last_sibling(gaimbuddylist->root));
1284 }
1285 c = gaim_contact_new();
1286 gaim_blist_add_contact(c, g,
1287 gaim_blist_get_last_child((GaimBlistNode*)g));
1288 }
1289
1290 cnode = (GaimBlistNode *)c;
1291
1292 if (bnode->parent) {
1293 if (GAIM_BUDDY_IS_ONLINE(buddy)) {
1294 ((GaimContact*)bnode->parent)->online--;
1295 if (((GaimContact*)bnode->parent)->online == 0)
1296 ((GaimGroup*)bnode->parent->parent)->online--;
1297 }
1298 if (gaim_account_is_connected(buddy->account)) {
1299 ((GaimContact*)bnode->parent)->currentsize--;
1300 if (((GaimContact*)bnode->parent)->currentsize == 0)
1301 ((GaimGroup*)bnode->parent->parent)->currentsize--;
1302 }
1303 ((GaimContact*)bnode->parent)->totalsize--;
1304 /* the group totalsize will be taken care of by remove_contact below */
1305
1306 if (bnode->parent->parent != (GaimBlistNode*)g)
1307 serv_move_buddy(buddy, (GaimGroup *)bnode->parent->parent, g);
1308
1309 if (bnode->next)
1310 bnode->next->prev = bnode->prev;
1311 if (bnode->prev)
1312 bnode->prev->next = bnode->next;
1313 if (bnode->parent->child == bnode)
1314 bnode->parent->child = bnode->next;
1315
1316 if (ops && ops->remove)
1317 ops->remove(gaimbuddylist, bnode);
1318
1319 gaim_blist_schedule_save();
1320
1321 if (bnode->parent->parent != (GaimBlistNode*)g) {
1322 hb = g_new(struct _gaim_hbuddy, 1);
1323 hb->name = g_strdup(gaim_normalize(buddy->account, buddy->name));
1324 hb->account = buddy->account;
1325 hb->group = bnode->parent->parent;
1326 g_hash_table_remove(gaimbuddylist->buddies, hb);
1327 g_free(hb->name);
1328 g_free(hb);
1329 }
1330
1331 if (!bnode->parent->child) {
1332 gaim_blist_remove_contact((GaimContact*)bnode->parent);
1333 } else {
1334 gaim_contact_invalidate_priority_buddy((GaimContact*)bnode->parent);
1335 if (ops && ops->update)
1336 ops->update(gaimbuddylist, bnode->parent);
1337 }
1338 }
1339
1340 if (node && GAIM_BLIST_NODE_IS_BUDDY(node)) {
1341 if (node->next)
1342 node->next->prev = bnode;
1343 bnode->next = node->next;
1344 bnode->prev = node;
1345 bnode->parent = node->parent;
1346 node->next = bnode;
1347 } else {
1348 if (cnode->child)
1349 cnode->child->prev = bnode;
1350 bnode->prev = NULL;
1351 bnode->next = cnode->child;
1352 cnode->child = bnode;
1353 bnode->parent = cnode;
1354 }
1355
1356 if (GAIM_BUDDY_IS_ONLINE(buddy)) {
1357 ((GaimContact*)bnode->parent)->online++;
1358 if (((GaimContact*)bnode->parent)->online == 1)
1359 ((GaimGroup*)bnode->parent->parent)->online++;
1360 }
1361 if (gaim_account_is_connected(buddy->account)) {
1362 ((GaimContact*)bnode->parent)->currentsize++;
1363 if (((GaimContact*)bnode->parent)->currentsize == 1)
1364 ((GaimGroup*)bnode->parent->parent)->currentsize++;
1365 }
1366 ((GaimContact*)bnode->parent)->totalsize++;
1367
1368 hb = g_new(struct _gaim_hbuddy, 1);
1369 hb->name = g_strdup(gaim_normalize(buddy->account, buddy->name));
1370 hb->account = buddy->account;
1371 hb->group = ((GaimBlistNode*)buddy)->parent->parent;
1372
1373 g_hash_table_replace(gaimbuddylist->buddies, hb, buddy);
1374
1375 gaim_contact_invalidate_priority_buddy(gaim_buddy_get_contact(buddy));
1376
1377 gaim_blist_schedule_save();
1378
1379 if (ops && ops->update)
1380 ops->update(gaimbuddylist, (GaimBlistNode*)buddy);
1381
1382 /* Signal that the buddy has been added */
1383 gaim_signal_emit(gaim_blist_get_handle(), "buddy-added", buddy);
1384 }
1385
1386 GaimContact *gaim_contact_new()
1387 {
1388 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1389
1390 GaimContact *contact = g_new0(GaimContact, 1);
1391 contact->totalsize = 0;
1392 contact->currentsize = 0;
1393 contact->online = 0;
1394 gaim_blist_node_initialize_settings((GaimBlistNode *)contact);
1395 ((GaimBlistNode *)contact)->type = GAIM_BLIST_CONTACT_NODE;
1396
1397 if (ops && ops->new_node)
1398 ops->new_node((GaimBlistNode *)contact);
1399
1400 GAIM_DBUS_REGISTER_POINTER(contact, GaimContact);
1401 return contact;
1402 }
1403
1404 void gaim_contact_set_alias(GaimContact *contact, const char *alias)
1405 {
1406 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1407 char *old_alias = contact->alias;
1408
1409 g_return_if_fail(contact != NULL);
1410
1411 if ((alias != NULL) && (*alias != '\0'))
1412 contact->alias = g_strdup(alias);
1413 else
1414 contact->alias = NULL;
1415
1416 gaim_blist_schedule_save();
1417
1418 if (ops && ops->update)
1419 ops->update(gaimbuddylist, (GaimBlistNode*)contact);
1420
1421 gaim_signal_emit(gaim_blist_get_handle(), "blist-node-aliased",
1422 contact, old_alias);
1423 g_free(old_alias);
1424 }
1425
1426 const char *gaim_contact_get_alias(GaimContact* contact)
1427 {
1428 g_return_val_if_fail(contact != NULL, NULL);
1429
1430 if (contact->alias)
1431 return contact->alias;
1432
1433 return gaim_buddy_get_alias(gaim_contact_get_priority_buddy(contact));
1434 }
1435
1436 gboolean gaim_contact_on_account(GaimContact *c, GaimAccount *account)
1437 {
1438 GaimBlistNode *bnode, *cnode = (GaimBlistNode *) c;
1439
1440 g_return_val_if_fail(c != NULL, FALSE);
1441 g_return_val_if_fail(account != NULL, FALSE);
1442
1443 for (bnode = cnode->child; bnode; bnode = bnode->next) {
1444 GaimBuddy *buddy;
1445
1446 if (! GAIM_BLIST_NODE_IS_BUDDY(bnode))
1447 continue;
1448
1449 buddy = (GaimBuddy *)bnode;
1450 if (buddy->account == account)
1451 return TRUE;
1452 }
1453 return FALSE;
1454 }
1455
1456 void gaim_contact_invalidate_priority_buddy(GaimContact *contact)
1457 {
1458 g_return_if_fail(contact != NULL);
1459
1460 contact->priority_valid = FALSE;
1461 }
1462
1463 GaimGroup *gaim_group_new(const char *name)
1464 {
1465 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1466 GaimGroup *group;
1467
1468 g_return_val_if_fail(name != NULL, NULL);
1469 g_return_val_if_fail(*name != '\0', NULL);
1470
1471 group = gaim_find_group(name);
1472 if (group != NULL)
1473 return group;
1474
1475 group = g_new0(GaimGroup, 1);
1476 group->name = g_strdup(name);
1477 group->totalsize = 0;
1478 group->currentsize = 0;
1479 group->online = 0;
1480 gaim_blist_node_initialize_settings((GaimBlistNode *)group);
1481 ((GaimBlistNode *)group)->type = GAIM_BLIST_GROUP_NODE;
1482
1483 if (ops && ops->new_node)
1484 ops->new_node((GaimBlistNode *)group);
1485
1486 GAIM_DBUS_REGISTER_POINTER(group, GaimGroup);
1487 return group;
1488 }
1489
1490 void gaim_blist_add_contact(GaimContact *contact, GaimGroup *group, GaimBlistNode *node)
1491 {
1492 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1493 GaimGroup *g;
1494 GaimBlistNode *gnode, *cnode, *bnode;
1495
1496 g_return_if_fail(contact != NULL);
1497 g_return_if_fail(GAIM_BLIST_NODE_IS_CONTACT((GaimBlistNode*)contact));
1498
1499 if ((GaimBlistNode*)contact == node)
1500 return;
1501
1502 if (node && (GAIM_BLIST_NODE_IS_CONTACT(node) ||
1503 GAIM_BLIST_NODE_IS_CHAT(node)))
1504 g = (GaimGroup*)node->parent;
1505 else if (group)
1506 g = group;
1507 else {
1508 g = gaim_group_new(_("Buddies"));
1509 gaim_blist_add_group(g,
1510 gaim_blist_get_last_sibling(gaimbuddylist->root));
1511 }
1512
1513 gnode = (GaimBlistNode*)g;
1514 cnode = (GaimBlistNode*)contact;
1515
1516 if (cnode->parent) {
1517 if (cnode->parent->child == cnode)
1518 cnode->parent->child = cnode->next;
1519 if (cnode->prev)
1520 cnode->prev->next = cnode->next;
1521 if (cnode->next)
1522 cnode->next->prev = cnode->prev;
1523
1524 if (cnode->parent != gnode) {
1525 bnode = cnode->child;
1526 while (bnode) {
1527 GaimBlistNode *next_bnode = bnode->next;
1528 GaimBuddy *b = (GaimBuddy*)bnode;
1529
1530 struct _gaim_hbuddy *hb = g_new(struct _gaim_hbuddy, 1);
1531 hb->name = g_strdup(gaim_normalize(b->account, b->name));
1532 hb->account = b->account;
1533 hb->group = cnode->parent;
1534
1535 g_hash_table_remove(gaimbuddylist->buddies, hb);
1536
1537 if (!gaim_find_buddy_in_group(b->account, b->name, g)) {
1538 hb->group = gnode;
1539 g_hash_table_replace(gaimbuddylist->buddies, hb, b);
1540
1541 if (b->account->gc)
1542 serv_move_buddy(b, (GaimGroup *)cnode->parent, g);
1543 } else {
1544 gboolean empty_contact = FALSE;
1545
1546 /* this buddy already exists in the group, so we're
1547 * gonna delete it instead */
1548 g_free(hb->name);
1549 g_free(hb);
1550 if (b->account->gc)
1551 gaim_account_remove_buddy(b->account, b, (GaimGroup *)cnode->parent);
1552
1553 if (!cnode->child->next)
1554 empty_contact = TRUE;
1555 gaim_blist_remove_buddy(b);
1556
1557 /** in gaim_blist_remove_buddy(), if the last buddy in a
1558 * contact is removed, the contact is cleaned up and
1559 * g_free'd, so we mustn't try to reference bnode->next */
1560 if (empty_contact)
1561 return;
1562 }
1563 bnode = next_bnode;
1564 }
1565 }
1566
1567 if (contact->online > 0)
1568 ((GaimGroup*)cnode->parent)->online--;
1569 if (contact->currentsize > 0)
1570 ((GaimGroup*)cnode->parent)->currentsize--;
1571 ((GaimGroup*)cnode->parent)->totalsize--;
1572
1573 if (ops && ops->remove)
1574 ops->remove(gaimbuddylist, cnode);
1575
1576 gaim_blist_schedule_save();
1577 }
1578
1579 if (node && (GAIM_BLIST_NODE_IS_CONTACT(node) ||
1580 GAIM_BLIST_NODE_IS_CHAT(node))) {
1581 if (node->next)
1582 node->next->prev = cnode;
1583 cnode->next = node->next;
1584 cnode->prev = node;
1585 cnode->parent = node->parent;
1586 node->next = cnode;
1587 } else {
1588 if (gnode->child)
1589 gnode->child->prev = cnode;
1590 cnode->prev = NULL;
1591 cnode->next = gnode->child;
1592 gnode->child = cnode;
1593 cnode->parent = gnode;
1594 }
1595
1596 if (contact->online > 0)
1597 g->online++;
1598 if (contact->currentsize > 0)
1599 g->currentsize++;
1600 g->totalsize++;
1601
1602 gaim_blist_schedule_save();
1603
1604 if (ops && ops->update)
1605 {
1606 if (cnode->child)
1607 ops->update(gaimbuddylist, cnode);
1608
1609 for (bnode = cnode->child; bnode; bnode = bnode->next)
1610 ops->update(gaimbuddylist, bnode);
1611 }
1612 }
1613
1614 void gaim_blist_merge_contact(GaimContact *source, GaimBlistNode *node)
1615 {
1616 GaimBlistNode *sourcenode = (GaimBlistNode*)source;
1617 GaimBlistNode *targetnode;
1618 GaimBlistNode *prev, *cur, *next;
1619 GaimContact *target;
1620
1621 g_return_if_fail(source != NULL);
1622 g_return_if_fail(node != NULL);
1623
1624 if (GAIM_BLIST_NODE_IS_CONTACT(node)) {
1625 target = (GaimContact *)node;
1626 prev = gaim_blist_get_last_child(node);
1627 } else if (GAIM_BLIST_NODE_IS_BUDDY(node)) {
1628 target = (GaimContact *)node->parent;
1629 prev = node;
1630 } else {
1631 return;
1632 }
1633
1634 if (source == target || !target)
1635 return;
1636
1637 targetnode = (GaimBlistNode *)target;
1638 next = sourcenode->child;
1639
1640 while (next) {
1641 cur = next;
1642 next = cur->next;
1643 if (GAIM_BLIST_NODE_IS_BUDDY(cur)) {
1644 gaim_blist_add_buddy((GaimBuddy *)cur, target, NULL, prev);
1645 prev = cur;
1646 }
1647 }
1648 }
1649
1650 void gaim_blist_add_group(GaimGroup *group, GaimBlistNode *node)
1651 {
1652 GaimBlistUiOps *ops;
1653 GaimBlistNode *gnode = (GaimBlistNode*)group;
1654
1655 g_return_if_fail(group != NULL);
1656 g_return_if_fail(GAIM_BLIST_NODE_IS_GROUP((GaimBlistNode *)group));
1657
1658 ops = gaim_blist_get_ui_ops();
1659
1660 if (!gaimbuddylist->root) {
1661 gaimbuddylist->root = gnode;
1662 return;
1663 }
1664
1665 /* if we're moving to overtop of ourselves, do nothing */
1666 if (gnode == node)
1667 return;
1668
1669 if (gaim_find_group(group->name)) {
1670 /* This is just being moved */
1671
1672 if (ops && ops->remove)
1673 ops->remove(gaimbuddylist, (GaimBlistNode *)group);
1674
1675 if (gnode == gaimbuddylist->root)
1676 gaimbuddylist->root = gnode->next;
1677 if (gnode->prev)
1678 gnode->prev->next = gnode->next;
1679 if (gnode->next)
1680 gnode->next->prev = gnode->prev;
1681 }
1682
1683 if (node && GAIM_BLIST_NODE_IS_GROUP(node)) {
1684 gnode->next = node->next;
1685 gnode->prev = node;
1686 if (node->next)
1687 node->next->prev = gnode;
1688 node->next = gnode;
1689 } else {
1690 if (gaimbuddylist->root)
1691 gaimbuddylist->root->prev = gnode;
1692 gnode->next = gaimbuddylist->root;
1693 gnode->prev = NULL;
1694 gaimbuddylist->root = gnode;
1695 }
1696
1697 gaim_blist_schedule_save();
1698
1699 if (ops && ops->update) {
1700 ops->update(gaimbuddylist, gnode);
1701 for (node = gnode->child; node; node = node->next)
1702 ops->update(gaimbuddylist, node);
1703 }
1704 }
1705
1706 void gaim_blist_remove_contact(GaimContact *contact)
1707 {
1708 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1709 GaimBlistNode *node, *gnode;
1710
1711 g_return_if_fail(contact != NULL);
1712
1713 node = (GaimBlistNode *)contact;
1714 gnode = node->parent;
1715
1716 if (node->child) {
1717 /*
1718 * If this contact has children then remove them. When the last
1719 * buddy is removed from the contact, the contact is automatically
1720 * deleted.
1721 */
1722 while (node->child->next) {
1723 gaim_blist_remove_buddy((GaimBuddy*)node->child);
1724 }
1725 /*
1726 * Remove the last buddy and trigger the deletion of the contact.
1727 * It would probably be cleaner if contact-deletion was done after
1728 * a timeout? Or if it had to be done manually, like below?
1729 */
1730 gaim_blist_remove_buddy((GaimBuddy*)node->child);
1731 } else {
1732 /* Remove the node from its parent */
1733 if (gnode->child == node)
1734 gnode->child = node->next;
1735 if (node->prev)
1736 node->prev->next = node->next;
1737 if (node->next)
1738 node->next->prev = node->prev;
1739
1740 gaim_blist_schedule_save();
1741
1742 /* Update the UI */
1743 if (ops && ops->remove)
1744 ops->remove(gaimbuddylist, node);
1745
1746 /* Delete the node */
1747 g_hash_table_destroy(contact->node.settings);
1748 GAIM_DBUS_UNREGISTER_POINTER(contact);
1749 g_free(contact);
1750 }
1751 }
1752
1753 void gaim_blist_remove_buddy(GaimBuddy *buddy)
1754 {
1755 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1756 GaimBlistNode *node, *cnode, *gnode;
1757 GaimContact *contact;
1758 GaimGroup *group;
1759 struct _gaim_hbuddy hb;
1760
1761 g_return_if_fail(buddy != NULL);
1762
1763 node = (GaimBlistNode *)buddy;
1764 cnode = node->parent;
1765 gnode = cnode->parent;
1766 contact = (GaimContact *)cnode;
1767 group = (GaimGroup *)gnode;
1768
1769 /* Delete any buddy icon. */
1770 gaim_buddy_icon_uncache(buddy);
1771
1772 /* Remove the node from its parent */
1773 if (node->prev)
1774 node->prev->next = node->next;
1775 if (node->next)
1776 node->next->prev = node->prev;
1777 if (cnode->child == node)
1778 cnode->child = node->next;
1779
1780 /* Adjust size counts */
1781 if (GAIM_BUDDY_IS_ONLINE(buddy)) {
1782 contact->online--;
1783 if (contact->online == 0)
1784 group->online--;
1785 }
1786 if (gaim_account_is_connected(buddy->account)) {
1787 contact->currentsize--;
1788 if (contact->currentsize == 0)
1789 group->currentsize--;
1790 }
1791 contact->totalsize--;
1792
1793 gaim_blist_schedule_save();
1794
1795 /* Re-sort the contact */
1796 if (contact->priority == buddy) {
1797 gaim_contact_invalidate_priority_buddy(contact);
1798 if (ops && ops->update)
1799 ops->update(gaimbuddylist, cnode);
1800 }
1801
1802 /* Remove this buddy from the buddies hash table */
1803 hb.name = g_strdup(gaim_normalize(buddy->account, buddy->name));
1804 hb.account = buddy->account;
1805 hb.group = ((GaimBlistNode*)buddy)->parent->parent;
1806 g_hash_table_remove(gaimbuddylist->buddies, &hb);
1807 g_free(hb.name);
1808
1809 /* Update the UI */
1810 if (ops && ops->remove)
1811 ops->remove(gaimbuddylist, node);
1812
1813 /* Signal that the buddy has been removed before freeing the memory for it */
1814 gaim_signal_emit(gaim_blist_get_handle(), "buddy-removed", buddy);
1815
1816 /* Delete the node */
1817 if (buddy->icon != NULL)
1818 gaim_buddy_icon_unref(buddy->icon);
1819 g_hash_table_destroy(buddy->node.settings);
1820 gaim_presence_remove_buddy(buddy->presence, buddy);
1821 gaim_presence_destroy(buddy->presence);
1822 g_free(buddy->name);
1823 g_free(buddy->alias);
1824 g_free(buddy->server_alias);
1825
1826 GAIM_DBUS_UNREGISTER_POINTER(buddy);
1827 g_free(buddy);
1828
1829 /* FIXME: Once GaimBuddy is a GObject, timeout callbacks can
1830 * g_object_ref() it when connecting the callback and
1831 * g_object_unref() it in the handler. That way, it won't
1832 * get freed while the timeout is pending and this line can
1833 * be removed. */
1834 while (g_source_remove_by_user_data((gpointer *)buddy));
1835
1836 /* If the contact is empty then remove it */
1837 if (!cnode->child)
1838 gaim_blist_remove_contact(contact);
1839 }
1840
1841 void gaim_blist_remove_chat(GaimChat *chat)
1842 {
1843 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1844 GaimBlistNode *node, *gnode;
1845 GaimGroup *group;
1846
1847 g_return_if_fail(chat != NULL);
1848
1849 node = (GaimBlistNode *)chat;
1850 gnode = node->parent;
1851 group = (GaimGroup *)gnode;
1852
1853 if (gnode != NULL)
1854 {
1855 /* Remove the node from its parent */
1856 if (gnode->child == node)
1857 gnode->child = node->next;
1858 if (node->prev)
1859 node->prev->next = node->next;
1860 if (node->next)
1861 node->next->prev = node->prev;
1862
1863 /* Adjust size counts */
1864 if (gaim_account_is_connected(chat->account)) {
1865 group->online--;
1866 group->currentsize--;
1867 }
1868 group->totalsize--;
1869
1870 gaim_blist_schedule_save();
1871 }
1872
1873 /* Update the UI */
1874 if (ops && ops->remove)
1875 ops->remove(gaimbuddylist, node);
1876
1877 /* Delete the node */
1878 g_hash_table_destroy(chat->components);
1879 g_hash_table_destroy(chat->node.settings);
1880 g_free(chat->alias);
1881 GAIM_DBUS_UNREGISTER_POINTER(chat);
1882 g_free(chat);
1883 }
1884
1885 void gaim_blist_remove_group(GaimGroup *group)
1886 {
1887 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
1888 GaimBlistNode *node;
1889 GList *l;
1890
1891 g_return_if_fail(group != NULL);
1892
1893 node = (GaimBlistNode *)group;
1894
1895 /* Make sure the group is empty */
1896 if (node->child) {
1897 char *buf;
1898 int count = 0;
1899 GaimBlistNode *child;
1900
1901 for (child = node->child; child != NULL; child = child->next)
1902 count++;
1903
1904 buf = g_strdup_printf(ngettext("%d buddy from group %s was not removed "
1905 "because it belongs to an account which is "
1906 "disabled or offline. This buddy and the "
1907 "group were not removed.\n",
1908 "%d buddies from group %s were not "
1909 "removed because they belong to accounts "
1910 "which are currently disabled or offline. "
1911 "These buddies and the group were not "
1912 "removed.\n", count),
1913 count, group->name);
1914 gaim_notify_error(NULL, NULL, _("Group not removed"), buf);
1915 g_free(buf);
1916 return;
1917 }
1918
1919 /* Remove the node from its parent */
1920 if (gaimbuddylist->root == node)
1921 gaimbuddylist->root = node->next;
1922 if (node->prev)
1923 node->prev->next = node->next;
1924 if (node->next)
1925 node->next->prev = node->prev;
1926
1927 gaim_blist_schedule_save();
1928
1929 /* Update the UI */
1930 if (ops && ops->remove)
1931 ops->remove(gaimbuddylist, node);
1932
1933 /* Remove the group from all accounts that are online */
1934 for (l = gaim_connections_get_all(); l != NULL; l = l->next)
1935 {
1936 GaimConnection *gc = (GaimConnection *)l->data;
1937
1938 if (gaim_connection_get_state(gc) == GAIM_CONNECTED)
1939 gaim_account_remove_group(gaim_connection_get_account(gc), group);
1940 }
1941
1942 /* Delete the node */
1943 g_hash_table_destroy(group->node.settings);
1944 g_free(group->name);
1945 GAIM_DBUS_UNREGISTER_POINTER(group);
1946 g_free(group);
1947 }
1948
1949 GaimBuddy *gaim_contact_get_priority_buddy(GaimContact *contact)
1950 {
1951 g_return_val_if_fail(contact != NULL, NULL);
1952
1953 if (!contact->priority_valid)
1954 gaim_contact_compute_priority_buddy(contact);
1955
1956 return contact->priority;
1957 }
1958
1959 const char *gaim_buddy_get_alias_only(GaimBuddy *buddy)
1960 {
1961 g_return_val_if_fail(buddy != NULL, NULL);
1962
1963 if ((buddy->alias != NULL) && (*buddy->alias != '\0')) {
1964 return buddy->alias;
1965 } else if ((buddy->server_alias != NULL) &&
1966 (*buddy->server_alias != '\0')) {
1967
1968 return buddy->server_alias;
1969 }
1970
1971 return NULL;
1972 }
1973
1974
1975 const char *gaim_buddy_get_contact_alias(GaimBuddy *buddy)
1976 {
1977 GaimContact *c;
1978
1979 g_return_val_if_fail(buddy != NULL, NULL);
1980
1981 /* Search for an alias for the buddy. In order of precedence: */
1982 /* The buddy alias */
1983 if (buddy->alias != NULL)
1984 return buddy->alias;
1985
1986 /* The contact alias */
1987 c = gaim_buddy_get_contact(buddy);
1988 if ((c != NULL) && (c->alias != NULL))
1989 return c->alias;
1990
1991 /* The server alias */
1992 if ((buddy->server_alias) && (*buddy->server_alias))
1993 return buddy->server_alias;
1994
1995 /* The buddy's user name (i.e. no alias) */
1996 return buddy->name;
1997 }
1998
1999
2000 const char *gaim_buddy_get_alias(GaimBuddy *buddy)
2001 {
2002 g_return_val_if_fail(buddy != NULL, NULL);
2003
2004 /* Search for an alias for the buddy. In order of precedence: */
2005 /* The buddy alias */
2006 if (buddy->alias != NULL)
2007 return buddy->alias;
2008
2009 /* The server alias */
2010 if ((buddy->server_alias) && (*buddy->server_alias))
2011 return buddy->server_alias;
2012
2013 /* The buddy's user name (i.e. no alias) */
2014 return buddy->name;
2015 }
2016
2017 const char *gaim_buddy_get_local_alias(GaimBuddy *buddy)
2018 {
2019 GaimContact *c;
2020
2021 g_return_val_if_fail(buddy != NULL, NULL);
2022
2023 /* Search for an alias for the buddy. In order of precedence: */
2024 /* The buddy alias */
2025 if (buddy->alias != NULL)
2026 return buddy->alias;
2027
2028 /* The contact alias */
2029 c = gaim_buddy_get_contact(buddy);
2030 if ((c != NULL) && (c->alias != NULL))
2031 return c->alias;
2032
2033 /* The buddy's user name (i.e. no alias) */
2034 return buddy->name;
2035 }
2036
2037 const char *gaim_chat_get_name(GaimChat *chat)
2038 {
2039 struct proto_chat_entry *pce;
2040 GList *parts;
2041 char *ret;
2042
2043 g_return_val_if_fail(chat != NULL, NULL);
2044
2045 if ((chat->alias != NULL) && (*chat->alias != '\0'))
2046 return chat->alias;
2047
2048 parts = GAIM_PLUGIN_PROTOCOL_INFO(chat->account->gc->prpl)->chat_info(chat->account->gc);
2049 pce = parts->data;
2050 ret = g_hash_table_lookup(chat->components, pce->identifier);
2051 g_list_foreach(parts, (GFunc)g_free, NULL);
2052 g_list_free(parts);
2053
2054 return ret;
2055 }
2056
2057 GaimBuddy *gaim_find_buddy(GaimAccount *account, const char *name)
2058 {
2059 GaimBuddy *buddy;
2060 struct _gaim_hbuddy hb;
2061 GaimBlistNode *group;
2062
2063 g_return_val_if_fail(gaimbuddylist != NULL, NULL);
2064 g_return_val_if_fail(account != NULL, NULL);
2065 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL);
2066
2067 hb.account = account;
2068 hb.name = g_strdup(gaim_normalize(account, name));
2069
2070 for (group = gaimbuddylist->root; group; group = group->next) {
2071 hb.group = group;
2072 if ((buddy = g_hash_table_lookup(gaimbuddylist->buddies, &hb))) {
2073 g_free(hb.name);
2074 return buddy;
2075 }
2076 }
2077 g_free(hb.name);
2078
2079 return NULL;
2080 }
2081
2082 GaimBuddy *gaim_find_buddy_in_group(GaimAccount *account, const char *name,
2083 GaimGroup *group)
2084 {
2085 struct _gaim_hbuddy hb;
2086 GaimBuddy *ret;
2087
2088 g_return_val_if_fail(gaimbuddylist != NULL, NULL);
2089 g_return_val_if_fail(account != NULL, NULL);
2090 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL);
2091
2092 hb.name = g_strdup(gaim_normalize(account, name));
2093 hb.account = account;
2094 hb.group = (GaimBlistNode*)group;
2095
2096 ret = g_hash_table_lookup(gaimbuddylist->buddies, &hb);
2097 g_free(hb.name);
2098
2099 return ret;
2100 }
2101
2102 GSList *gaim_find_buddies(GaimAccount *account, const char *name)
2103 {
2104 struct buddy *buddy;
2105 struct _gaim_hbuddy hb;
2106 GaimBlistNode *node;
2107 GSList *ret = NULL;
2108
2109 g_return_val_if_fail(gaimbuddylist != NULL, NULL);
2110 g_return_val_if_fail(account != NULL, NULL);
2111 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL);
2112
2113 hb.name = g_strdup(gaim_normalize(account, name));
2114 hb.account = account;
2115
2116 for (node = gaimbuddylist->root; node != NULL; node = node->next) {
2117 hb.group = node;
2118 if ((buddy = g_hash_table_lookup(gaimbuddylist->buddies, &hb)) != NULL)
2119 ret = g_slist_append(ret, buddy);
2120 }
2121 g_free(hb.name);
2122
2123 return ret;
2124 }
2125
2126 GaimGroup *gaim_find_group(const char *name)
2127 {
2128 GaimBlistNode *node;
2129
2130 g_return_val_if_fail(gaimbuddylist != NULL, NULL);
2131 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL);
2132
2133 for (node = gaimbuddylist->root; node != NULL; node = node->next) {
2134 if (!strcmp(((GaimGroup *)node)->name, name))
2135 return (GaimGroup *)node;
2136 }
2137
2138 return NULL;
2139 }
2140
2141 GaimChat *
2142 gaim_blist_find_chat(GaimAccount *account, const char *name)
2143 {
2144 char *chat_name;
2145 GaimChat *chat;
2146 GaimPlugin *prpl;
2147 GaimPluginProtocolInfo *prpl_info = NULL;
2148 struct proto_chat_entry *pce;
2149 GaimBlistNode *node, *group;
2150 GList *parts;
2151
2152 g_return_val_if_fail(gaimbuddylist != NULL, NULL);
2153 g_return_val_if_fail((name != NULL) && (*name != '\0'), NULL);
2154
2155 if (!gaim_account_is_connected(account))
2156 return NULL;
2157
2158 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account));
2159 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl);
2160
2161 if (prpl_info->find_blist_chat != NULL)
2162 return prpl_info->find_blist_chat(account, name);
2163
2164 for (group = gaimbuddylist->root; group != NULL; group = group->next) {
2165 for (node = group->child; node != NULL; node = node->next) {
2166 if (GAIM_BLIST_NODE_IS_CHAT(node)) {
2167
2168 chat = (GaimChat*)node;
2169
2170 if (account != chat->account)
2171 continue;
2172
2173 parts = prpl_info->chat_info(
2174 gaim_account_get_connection(chat->account));
2175
2176 pce = parts->data;
2177 chat_name = g_hash_table_lookup(chat->components,
2178 pce->identifier);
2179
2180 if (chat->account == account && chat_name != NULL &&
2181 name != NULL && !strcmp(chat_name, name)) {
2182
2183 return chat;
2184 }
2185 }
2186 }
2187 }
2188
2189 return NULL;
2190 }
2191
2192 GaimGroup *
2193 gaim_chat_get_group(GaimChat *chat)
2194 {
2195 g_return_val_if_fail(chat != NULL, NULL);
2196
2197 return (GaimGroup *)(((GaimBlistNode *)chat)->parent);
2198 }
2199
2200 GaimContact *gaim_buddy_get_contact(GaimBuddy *buddy)
2201 {
2202 g_return_val_if_fail(buddy != NULL, NULL);
2203
2204 return (GaimContact*)((GaimBlistNode*)buddy)->parent;
2205 }
2206
2207 GaimPresence *gaim_buddy_get_presence(const GaimBuddy *buddy)
2208 {
2209 g_return_val_if_fail(buddy != NULL, NULL);
2210 return buddy->presence;
2211 }
2212
2213 GaimGroup *gaim_buddy_get_group(GaimBuddy *buddy)
2214 {
2215 g_return_val_if_fail(buddy != NULL, NULL);
2216
2217 if (((GaimBlistNode *)buddy)->parent == NULL)
2218 return NULL;
2219
2220 return (GaimGroup *)(((GaimBlistNode*)buddy)->parent->parent);
2221 }
2222
2223 GSList *gaim_group_get_accounts(GaimGroup *group)
2224 {
2225 GSList *l = NULL;
2226 GaimBlistNode *gnode, *cnode, *bnode;
2227
2228 gnode = (GaimBlistNode *)group;
2229
2230 for (cnode = gnode->child; cnode; cnode = cnode->next) {
2231 if (GAIM_BLIST_NODE_IS_CHAT(cnode)) {
2232 if (!g_slist_find(l, ((GaimChat *)cnode)->account))
2233 l = g_slist_append(l, ((GaimChat *)cnode)->account);
2234 } else if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) {
2235 for (bnode = cnode->child; bnode; bnode = bnode->next) {
2236 if (GAIM_BLIST_NODE_IS_BUDDY(bnode)) {
2237 if (!g_slist_find(l, ((GaimBuddy *)bnode)->account))
2238 l = g_slist_append(l, ((GaimBuddy *)bnode)->account);
2239 }
2240 }
2241 }
2242 }
2243
2244 return l;
2245 }
2246
2247 void gaim_blist_add_account(GaimAccount *account)
2248 {
2249 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
2250 GaimBlistNode *gnode, *cnode, *bnode;
2251
2252 g_return_if_fail(gaimbuddylist != NULL);
2253
2254 if (!ops || !ops->update)
2255 return;
2256
2257 for (gnode = gaimbuddylist->root; gnode; gnode = gnode->next) {
2258 if (!GAIM_BLIST_NODE_IS_GROUP(gnode))
2259 continue;
2260 for (cnode = gnode->child; cnode; cnode = cnode->next) {
2261 if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) {
2262 gboolean recompute = FALSE;
2263 for (bnode = cnode->child; bnode; bnode = bnode->next) {
2264 if (GAIM_BLIST_NODE_IS_BUDDY(bnode) &&
2265 ((GaimBuddy*)bnode)->account == account) {
2266 recompute = TRUE;
2267 ((GaimContact*)cnode)->currentsize++;
2268 if (((GaimContact*)cnode)->currentsize == 1)
2269 ((GaimGroup*)gnode)->currentsize++;
2270 ops->update(gaimbuddylist, bnode);
2271 }
2272 }
2273 if (recompute ||
2274 gaim_blist_node_get_bool(cnode, "show_offline")) {
2275 gaim_contact_invalidate_priority_buddy((GaimContact*)cnode);
2276 ops->update(gaimbuddylist, cnode);
2277 }
2278 } else if (GAIM_BLIST_NODE_IS_CHAT(cnode) &&
2279 ((GaimChat*)cnode)->account == account) {
2280 ((GaimGroup *)gnode)->online++;
2281 ((GaimGroup *)gnode)->currentsize++;
2282 ops->update(gaimbuddylist, cnode);
2283 }
2284 }
2285 ops->update(gaimbuddylist, gnode);
2286 }
2287 }
2288
2289 void gaim_blist_remove_account(GaimAccount *account)
2290 {
2291 GaimBlistUiOps *ops = gaim_blist_get_ui_ops();
2292 GaimBlistNode *gnode, *cnode, *bnode;
2293 GaimBuddy *buddy;
2294 GaimChat *chat;
2295 GaimContact *contact;
2296 GaimGroup *group;
2297 GList *list = NULL, *iter = NULL;
2298
2299 g_return_if_fail(gaimbuddylist != NULL);
2300
2301 for (gnode = gaimbuddylist->root; gnode; gnode = gnode->next) {
2302 if (!GAIM_BLIST_NODE_IS_GROUP(gnode))
2303 continue;
2304
2305 group = (GaimGroup *)gnode;
2306
2307 for (cnode = gnode->child; cnode; cnode = cnode->next) {
2308 if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) {
2309 gboolean recompute = FALSE;
2310 contact = (GaimContact *)cnode;
2311
2312 for (bnode = cnode->child; bnode; bnode = bnode->next) {
2313 if (!GAIM_BLIST_NODE_IS_BUDDY(bnode))
2314 continue;
2315
2316 buddy = (GaimBuddy *)bnode;
2317 if (account == buddy->account) {
2318 GaimPresence *presence;
2319 recompute = TRUE;
2320
2321 presence = gaim_buddy_get_presence(buddy);
2322
2323 if(gaim_presence_is_online(presence)) {
2324 contact->online--;
2325 if (contact->online == 0)
2326 group->online--;
2327
2328 gaim_blist_node_set_int(&buddy->node,
2329 "last_seen", time(NULL));
2330 }
2331
2332 contact->currentsize--;
2333 if (contact->currentsize == 0)
2334 group->currentsize--;
2335
2336 if (!g_list_find(list, presence))
2337 list = g_list_prepend(list, presence);
2338
2339 if (ops && ops->remove)
2340 ops->remove(gaimbuddylist, bnode);
2341 }
2342 }
2343 if (recompute) {
2344 gaim_contact_invalidate_priority_buddy(contact);
2345 if (ops && ops->update)
2346 ops->update(gaimbuddylist, cnode);
2347 }
2348 } else if (GAIM_BLIST_NODE_IS_CHAT(cnode)) {
2349 chat = (GaimChat *)cnode;
2350
2351 if(chat->account == account) {
2352 group->currentsize--;
2353 group->online--;
2354
2355 if (ops && ops->remove)
2356 ops->remove(gaimbuddylist, cnode);
2357 }
2358 }
2359 }
2360 }
2361
2362 for (iter = list; iter; iter = iter->next)
2363 {
2364 gaim_presence_set_status_active(iter->data, "offline", TRUE);
2365 }
2366 g_list_free(list);
2367 }
2368
2369 gboolean gaim_group_on_account(GaimGroup *g, GaimAccount *account)
2370 {
2371 GaimBlistNode *cnode;
2372 for (cnode = ((GaimBlistNode *)g)->child; cnode; cnode = cnode->next) {
2373 if (GAIM_BLIST_NODE_IS_CONTACT(cnode)) {
2374 if(gaim_contact_on_account((GaimContact *) cnode, account))
2375 return TRUE;
2376 } else if (GAIM_BLIST_NODE_IS_CHAT(cnode)) {
2377 GaimChat *chat = (GaimChat *)cnode;
2378 if ((!account && gaim_account_is_connected(chat->account))
2379 || chat->account == account)
2380 return TRUE;
2381 }
2382 }
2383 return FALSE;
2384 }
2385
2386 void
2387 gaim_blist_request_add_buddy(GaimAccount *account, const char *username,
2388 const char *group, const char *alias)
2389 {
2390 GaimBlistUiOps *ui_ops;
2391
2392 ui_ops = gaim_blist_get_ui_ops();
2393
2394 if (ui_ops != NULL && ui_ops->request_add_buddy != NULL)
2395 ui_ops->request_add_buddy(account, username, group, alias);
2396 }
2397
2398 void
2399 gaim_blist_request_add_chat(GaimAccount *account, GaimGroup *group,
2400 const char *alias, const char *name)
2401 {
2402 GaimBlistUiOps *ui_ops;
2403
2404 ui_ops = gaim_blist_get_ui_ops();
2405
2406 if (ui_ops != NULL && ui_ops->request_add_chat != NULL)
2407 ui_ops->request_add_chat(account, group, alias, name);
2408 }
2409
2410 void
2411 gaim_blist_request_add_group(void)
2412 {
2413 GaimBlistUiOps *ui_ops;
2414
2415 ui_ops = gaim_blist_get_ui_ops();
2416
2417 if (ui_ops != NULL && ui_ops->request_add_group != NULL)
2418 ui_ops->request_add_group();
2419 }
2420
2421 static void
2422 gaim_blist_node_setting_free(gpointer data)
2423 {
2424 GaimValue *value;
2425
2426 value = (GaimValue *)data;
2427
2428 gaim_value_destroy(value);
2429 }
2430
2431 static void gaim_blist_node_initialize_settings(GaimBlistNode *node)
2432 {
2433 if (node->settings)
2434 return;
2435
2436 node->settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
2437 (GDestroyNotify)gaim_blist_node_setting_free);
2438 }
2439
2440 void gaim_blist_node_remove_setting(GaimBlistNode *node, const char *key)
2441 {
2442 g_return_if_fail(node != NULL);
2443 g_return_if_fail(node->settings != NULL);
2444 g_return_if_fail(key != NULL);
2445
2446 g_hash_table_remove(node->settings, key);
2447
2448 gaim_blist_schedule_save();
2449 }
2450
2451 void
2452 gaim_blist_node_set_flags(GaimBlistNode *node, GaimBlistNodeFlags flags)
2453 {
2454 g_return_if_fail(node != NULL);
2455
2456 node->flags = flags;
2457 }
2458
2459 GaimBlistNodeFlags
2460 gaim_blist_node_get_flags(GaimBlistNode *node)
2461 {
2462 g_return_val_if_fail(node != NULL, 0);
2463
2464 return node->flags;
2465 }
2466
2467 void
2468 gaim_blist_node_set_bool(GaimBlistNode* node, const char *key, gboolean data)
2469 {
2470 GaimValue *value;
2471
2472 g_return_if_fail(node != NULL);
2473 g_return_if_fail(node->settings != NULL);
2474 g_return_if_fail(key != NULL);
2475
2476 value = gaim_value_new(GAIM_TYPE_BOOLEAN);
2477 gaim_value_set_boolean(value, data);
2478
2479 g_hash_table_replace(node->settings, g_strdup(key), value);
2480
2481 gaim_blist_schedule_save();
2482 }
2483
2484 gboolean
2485 gaim_blist_node_get_bool(GaimBlistNode* node, const char *key)
2486 {
2487 GaimValue *value;
2488
2489 g_return_val_if_fail(node != NULL, FALSE);
2490 g_return_val_if_fail(node->settings != NULL, FALSE);
2491 g_return_val_if_fail(key != NULL, FALSE);
2492
2493 value = g_hash_table_lookup(node->settings, key);
2494
2495 if (value == NULL)
2496 return FALSE;
2497
2498 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_BOOLEAN, FALSE);
2499
2500 return gaim_value_get_boolean(value);
2501 }
2502
2503 void
2504 gaim_blist_node_set_int(GaimBlistNode* node, const char *key, int data)
2505 {
2506 GaimValue *value;
2507
2508 g_return_if_fail(node != NULL);
2509 g_return_if_fail(node->settings != NULL);
2510 g_return_if_fail(key != NULL);
2511
2512 value = gaim_value_new(GAIM_TYPE_INT);
2513 gaim_value_set_int(value, data);
2514
2515 g_hash_table_replace(node->settings, g_strdup(key), value);
2516
2517 gaim_blist_schedule_save();
2518 }
2519
2520 int
2521 gaim_blist_node_get_int(GaimBlistNode* node, const char *key)
2522 {
2523 GaimValue *value;
2524
2525 g_return_val_if_fail(node != NULL, 0);
2526 g_return_val_if_fail(node->settings != NULL, 0);
2527 g_return_val_if_fail(key != NULL, 0);
2528
2529 value = g_hash_table_lookup(node->settings, key);
2530
2531 if (value == NULL)
2532 return 0;
2533
2534 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_INT, 0);
2535
2536 return gaim_value_get_int(value);
2537 }
2538
2539 void
2540 gaim_blist_node_set_string(GaimBlistNode* node, const char *key, const char *data)
2541 {
2542 GaimValue *value;
2543
2544 g_return_if_fail(node != NULL);
2545 g_return_if_fail(node->settings != NULL);
2546 g_return_if_fail(key != NULL);
2547
2548 value = gaim_value_new(GAIM_TYPE_STRING);
2549 gaim_value_set_string(value, data);
2550
2551 g_hash_table_replace(node->settings, g_strdup(key), value);
2552
2553 gaim_blist_schedule_save();
2554 }
2555
2556 const char *
2557 gaim_blist_node_get_string(GaimBlistNode* node, const char *key)
2558 {
2559 GaimValue *value;
2560
2561 g_return_val_if_fail(node != NULL, NULL);
2562 g_return_val_if_fail(node->settings != NULL, NULL);
2563 g_return_val_if_fail(key != NULL, NULL);
2564
2565 value = g_hash_table_lookup(node->settings, key);
2566
2567 if (value == NULL)
2568 return NULL;
2569
2570 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_STRING, NULL);
2571
2572 return gaim_value_get_string(value);
2573 }
2574
2575 GList *
2576 gaim_blist_node_get_extended_menu(GaimBlistNode *n)
2577 {
2578 GList *menu = NULL;
2579
2580 g_return_val_if_fail(n != NULL, NULL);
2581
2582 gaim_signal_emit(gaim_blist_get_handle(),
2583 "blist-node-extended-menu",
2584 n, &menu);
2585 return menu;
2586 }
2587
2588 int gaim_blist_get_group_size(GaimGroup *group, gboolean offline)
2589 {
2590 if (!group)
2591 return 0;
2592
2593 return offline ? group->totalsize : group->currentsize;
2594 }
2595
2596 int gaim_blist_get_group_online_count(GaimGroup *group)
2597 {
2598 if (!group)
2599 return 0;
2600
2601 return group->online;
2602 }
2603
2604 void
2605 gaim_blist_set_ui_ops(GaimBlistUiOps *ops)
2606 {
2607 blist_ui_ops = ops;
2608 }
2609
2610 GaimBlistUiOps *
2611 gaim_blist_get_ui_ops(void)
2612 {
2613 return blist_ui_ops;
2614 }
2615
2616
2617 void *
2618 gaim_blist_get_handle(void)
2619 {
2620 static int handle;
2621
2622 return &handle;
2623 }
2624
2625 void
2626 gaim_blist_init(void)
2627 {
2628 void *handle = gaim_blist_get_handle();
2629
2630 gaim_signal_register(handle, "buddy-status-changed",
2631 gaim_marshal_VOID__POINTER_POINTER_POINTER, NULL,
2632 3,
2633 gaim_value_new(GAIM_TYPE_SUBTYPE,
2634 GAIM_SUBTYPE_BLIST_BUDDY),
2635 gaim_value_new(GAIM_TYPE_SUBTYPE,
2636 GAIM_SUBTYPE_STATUS),
2637 gaim_value_new(GAIM_TYPE_SUBTYPE,
2638 GAIM_SUBTYPE_STATUS));
2639 gaim_signal_register(handle, "buddy-privacy-changed",
2640 gaim_marshal_VOID__POINTER, NULL,
2641 1,
2642 gaim_value_new(GAIM_TYPE_SUBTYPE,
2643 GAIM_SUBTYPE_BLIST_BUDDY));
2644
2645 gaim_signal_register(handle, "buddy-idle-changed",
2646 gaim_marshal_VOID__POINTER_INT_INT, NULL,
2647 3,
2648 gaim_value_new(GAIM_TYPE_SUBTYPE,
2649 GAIM_SUBTYPE_BLIST_BUDDY),
2650 gaim_value_new(GAIM_TYPE_INT),
2651 gaim_value_new(GAIM_TYPE_INT));
2652
2653
2654 gaim_signal_register(handle, "buddy-signed-on",
2655 gaim_marshal_VOID__POINTER, NULL, 1,
2656 gaim_value_new(GAIM_TYPE_SUBTYPE,
2657 GAIM_SUBTYPE_BLIST_BUDDY));
2658
2659 gaim_signal_register(handle, "buddy-signed-off",
2660 gaim_marshal_VOID__POINTER, NULL, 1,
2661 gaim_value_new(GAIM_TYPE_SUBTYPE,
2662 GAIM_SUBTYPE_BLIST_BUDDY));
2663
2664 gaim_signal_register(handle, "buddy-added",
2665 gaim_marshal_VOID__POINTER, NULL, 1,
2666 gaim_value_new(GAIM_TYPE_SUBTYPE,
2667 GAIM_SUBTYPE_BLIST_BUDDY));
2668
2669 gaim_signal_register(handle, "buddy-removed",
2670 gaim_marshal_VOID__POINTER, NULL, 1,
2671 gaim_value_new(GAIM_TYPE_SUBTYPE,
2672 GAIM_SUBTYPE_BLIST_BUDDY));
2673
2674 gaim_signal_register(handle, "buddy-icon-changed",
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, "update-idle", gaim_marshal_VOID, NULL, 0);
2680
2681 gaim_signal_register(handle, "blist-node-extended-menu",
2682 gaim_marshal_VOID__POINTER_POINTER, NULL, 2,
2683 gaim_value_new(GAIM_TYPE_SUBTYPE,
2684 GAIM_SUBTYPE_BLIST_NODE),
2685 gaim_value_new(GAIM_TYPE_BOXED, "GList **"));
2686
2687 gaim_signal_register(handle, "blist-node-aliased",
2688 gaim_marshal_VOID__POINTER_POINTER, NULL, 2,
2689 gaim_value_new(GAIM_TYPE_SUBTYPE,
2690 GAIM_SUBTYPE_BLIST_NODE),
2691 gaim_value_new(GAIM_TYPE_STRING));
2692 }
2693
2694 void
2695 gaim_blist_uninit(void)
2696 {
2697 if (save_timer != 0)
2698 {
2699 gaim_timeout_remove(save_timer);
2700 save_timer = 0;
2701 gaim_blist_sync();
2702 }
2703
2704 gaim_signals_unregister_by_instance(gaim_blist_get_handle());
2705 }