source: buddylist.c @ 67f6d7d

release-1.10release-1.8release-1.9
Last change on this file since 67f6d7d 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
RevLine 
[aa5f725]1#include "owl.h"
2
[f4d0975]3void owl_buddylist_init(owl_buddylist *bl)
[aa5f725]4{
[d191f45]5  bl->buddies = g_ptr_array_new();
[f4d0975]6}
7
8/* add a (logged-in) AIM buddy to the buddy list
9 */
[e19eb97]10void owl_buddylist_add_aim_buddy(owl_buddylist *bl, const char *screenname)
[f4d0975]11{
12  owl_buddy *b;
[96828e4]13  b=g_new(owl_buddy, 1);
[f4d0975]14 
15  owl_buddy_create(b, OWL_PROTOCOL_AIM, screenname);
[d191f45]16  g_ptr_array_add(bl->buddies, b);
[f4d0975]17}
18
19/* remove an AIM buddy from the buddy list
20 */
[e19eb97]21int owl_buddylist_remove_aim_buddy(owl_buddylist *bl, const char *name)
[f4d0975]22{
[d191f45]23  int i;
[f4d0975]24  owl_buddy *b;
25
[d191f45]26  for (i = 0; i < bl->buddies->len; i++) {
27    b = bl->buddies->pdata[i];
[f4d0975]28    if (!strcasecmp(name, owl_buddy_get_name(b)) && owl_buddy_is_proto_aim(b)) {
[d191f45]29      owl_buddy_delete(g_ptr_array_remove_index(bl->buddies, i));
[f4d0975]30      return(0);
31    }
32  }
33  return(1);
[aa5f725]34}
35
36/* Deal with an "oncoming" message.  This means recognizing the user
[a352335c]37 * has logged in, and displaying a message if they were not already
[aa5f725]38 * logged in.
39 */
[e19eb97]40void owl_buddylist_oncoming(owl_buddylist *bl, const char *screenname)
[aa5f725]41{
[a352335c]42  owl_message *m;
[aa5f725]43
[f4d0975]44  if (!owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) {
[aa5f725]45
[f4d0975]46    owl_buddylist_add_aim_buddy(bl, screenname);
[aa5f725]47
[ceb8cfb]48    /* are we ingoring login messages for a while? */
[b7bb454]49    if (owl_global_is_ignore_aimlogin(&g)) return;
[ceb8cfb]50
[a352335c]51    /* if not, create the login message */
[96828e4]52    m=g_new(owl_message, 1);
[d559df9]53    owl_message_create_aim(m,
54                           screenname,
55                           owl_global_get_aim_screenname(&g),
56                           "",
57                           OWL_MESSAGE_DIRECTION_IN,
58                           1);
[aa5f725]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 */
[e19eb97]66void owl_buddylist_offgoing(owl_buddylist *bl, const char *screenname)
[aa5f725]67{
68  owl_message *m;
69
[fe6f1d3]70  if (owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) {
[96828e4]71    m=g_new(owl_message, 1);
[fe6f1d3]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  }
[257a22f]80
81  owl_buddylist_remove_aim_buddy(bl, screenname);
[aa5f725]82}
83
[f4d0975]84/* return the number of logged in buddies */
[35b3518]85int owl_buddylist_get_size(const owl_buddylist *bl)
[de03334]86{
[d191f45]87  return bl->buddies->len;
[de03334]88}
89
[f4d0975]90/* return the buddy with index N.  If out of range, return NULL
[a352335c]91 */
[35b3518]92owl_buddy *owl_buddylist_get_buddy_n(const owl_buddylist *bl, int index)
[a352335c]93{
[f4d0975]94  if (index<0) return(NULL);
95  if (index>(owl_buddylist_get_size(bl)-1)) return(NULL);
[a352335c]96
[d191f45]97  return bl->buddies->pdata[index];
[aa5f725]98}
99
[f4d0975]100/* return the AIM buddy with screenname 'name'.  If
101 * no such buddy is logged in, return NULL.
[a352335c]102 */
[35b3518]103owl_buddy *owl_buddylist_get_aim_buddy(const owl_buddylist *bl, const char *name)
[a352335c]104{
[d191f45]105  int i;
[f4d0975]106  owl_buddy *b;
107
[d191f45]108  for (i = 0; i < bl->buddies->len; i++) {
109    b = bl->buddies->pdata[i];
[f4d0975]110    if (!strcasecmp(name, owl_buddy_get_name(b))) return(b);
[a352335c]111  }
[f4d0975]112  return(NULL);
[a352335c]113}
114
115/* return 1 if the buddy 'screenname' is logged in,
116 * otherwise return 0
117 */
[35b3518]118int owl_buddylist_is_aim_buddy_loggedin(const owl_buddylist *bl, const char *screenname)
[a352335c]119{
[476faed]120  const owl_buddy *b;
[a352335c]121
[f4d0975]122  b=owl_buddylist_get_aim_buddy(bl, screenname);
123  if (b==NULL) return(0);
124  return(1);
[de03334]125}
126
[f4d0975]127/* remove all buddies from the list */
128void owl_buddylist_clear(owl_buddylist *bl)
[de03334]129{
[d191f45]130  g_ptr_array_foreach(bl->buddies, (GFunc)owl_buddy_delete, NULL);
131  g_ptr_array_set_size(bl->buddies, 0);
[de03334]132}
133
[1c4b4ca]134void owl_buddylist_cleanup(owl_buddylist *bl)
[f4d0975]135{
[3cdd6d2]136  owl_ptr_array_free(bl->buddies, (GDestroyNotify)owl_buddy_delete);
[aa5f725]137}
Note: See TracBrowser for help on using the repository browser.