source: buddylist.c @ 39a531d

release-1.10release-1.8release-1.9
Last change on this file since 39a531d was 3cdd6d2, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Add owl_ptr_array_free convenience function Unfortunately, most uses of GPtrArray here require a two-step chant which is really annoying. Until we require glib 2.22 and get g_ptr_array_new_with_free_func, use this helper function.
  • Property mode set to 100644
File size: 3.4 KB
Line 
1#include "owl.h"
2
3void owl_buddylist_init(owl_buddylist *bl)
4{
5  bl->buddies = g_ptr_array_new();
6}
7
8/* add a (logged-in) AIM buddy to the buddy list
9 */
10void owl_buddylist_add_aim_buddy(owl_buddylist *bl, const char *screenname)
11{
12  owl_buddy *b;
13  b=g_new(owl_buddy, 1);
14 
15  owl_buddy_create(b, OWL_PROTOCOL_AIM, screenname);
16  g_ptr_array_add(bl->buddies, b);
17}
18
19/* remove an AIM buddy from the buddy list
20 */
21int owl_buddylist_remove_aim_buddy(owl_buddylist *bl, const char *name)
22{
23  int i;
24  owl_buddy *b;
25
26  for (i = 0; i < bl->buddies->len; i++) {
27    b = bl->buddies->pdata[i];
28    if (!strcasecmp(name, owl_buddy_get_name(b)) && owl_buddy_is_proto_aim(b)) {
29      owl_buddy_delete(g_ptr_array_remove_index(bl->buddies, i));
30      return(0);
31    }
32  }
33  return(1);
34}
35
36/* Deal with an "oncoming" message.  This means recognizing the user
37 * has logged in, and displaying a message if they were not already
38 * logged in.
39 */
40void owl_buddylist_oncoming(owl_buddylist *bl, const char *screenname)
41{
42  owl_message *m;
43
44  if (!owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) {
45
46    owl_buddylist_add_aim_buddy(bl, screenname);
47
48    /* are we ingoring login messages for a while? */
49    if (owl_global_is_ignore_aimlogin(&g)) return;
50
51    /* if not, create the login message */
52    m=g_new(owl_message, 1);
53    owl_message_create_aim(m,
54                           screenname,
55                           owl_global_get_aim_screenname(&g),
56                           "",
57                           OWL_MESSAGE_DIRECTION_IN,
58                           1);
59    owl_global_messagequeue_addmsg(&g, m);
60  }
61}
62
63/* Deal with an "offgoing" message.  This means recognizing the user
64 * has logged out, and sending a message if they were logged in.
65 */
66void owl_buddylist_offgoing(owl_buddylist *bl, const char *screenname)
67{
68  owl_message *m;
69
70  if (owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) {
71    m=g_new(owl_message, 1);
72    owl_message_create_aim(m,
73                           screenname,
74                           owl_global_get_aim_screenname(&g),
75                           "",
76                           OWL_MESSAGE_DIRECTION_IN,
77                           -1);
78    owl_global_messagequeue_addmsg(&g, m);
79  }
80
81  owl_buddylist_remove_aim_buddy(bl, screenname);
82}
83
84/* return the number of logged in buddies */
85int owl_buddylist_get_size(const owl_buddylist *bl)
86{
87  return bl->buddies->len;
88}
89
90/* return the buddy with index N.  If out of range, return NULL
91 */
92owl_buddy *owl_buddylist_get_buddy_n(const owl_buddylist *bl, int index)
93{
94  if (index<0) return(NULL);
95  if (index>(owl_buddylist_get_size(bl)-1)) return(NULL);
96
97  return bl->buddies->pdata[index];
98}
99
100/* return the AIM buddy with screenname 'name'.  If
101 * no such buddy is logged in, return NULL.
102 */
103owl_buddy *owl_buddylist_get_aim_buddy(const owl_buddylist *bl, const char *name)
104{
105  int i;
106  owl_buddy *b;
107
108  for (i = 0; i < bl->buddies->len; i++) {
109    b = bl->buddies->pdata[i];
110    if (!strcasecmp(name, owl_buddy_get_name(b))) return(b);
111  }
112  return(NULL);
113}
114
115/* return 1 if the buddy 'screenname' is logged in,
116 * otherwise return 0
117 */
118int owl_buddylist_is_aim_buddy_loggedin(const owl_buddylist *bl, const char *screenname)
119{
120  const owl_buddy *b;
121
122  b=owl_buddylist_get_aim_buddy(bl, screenname);
123  if (b==NULL) return(0);
124  return(1);
125}
126
127/* remove all buddies from the list */
128void owl_buddylist_clear(owl_buddylist *bl)
129{
130  g_ptr_array_foreach(bl->buddies, (GFunc)owl_buddy_delete, NULL);
131  g_ptr_array_set_size(bl->buddies, 0);
132}
133
134void owl_buddylist_cleanup(owl_buddylist *bl)
135{
136  owl_ptr_array_free(bl->buddies, (GDestroyNotify)owl_buddy_delete);
137}
Note: See TracBrowser for help on using the repository browser.