2 # Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
4 # This file is part of python-purple.
6 # python-purple is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # python-purple is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 @param name Buddy's name.
28 @param account Buddy's account.
35 def __init__(self, name, account):
37 self.__account = account
39 if self._get_structure() != NULL:
44 cdef blist.PurpleBuddy *_get_structure(self):
45 '''Returns the buddy's C struct from purple.
47 @return A pointer to buddy's C struct from purple.
50 return blist.purple_find_buddy(account.purple_accounts_find( \
51 self.__account.username, self.__account.protocol.id), \
54 def __get_exists(self):
55 '''Answer if exists corresponding buddy in the purple.
57 @return True if buddy is a valid buddy of False otherwise.
61 exists = property(__get_exists)
64 '''Returns the buddy's name.
70 return <char *> blist.purple_buddy_get_name(self._get_structure())
73 name = property(__get_name)
75 def __get_alias(self):
76 '''Returns the buddy's alias
78 @return Buddy alias(if set) or None
81 cdef char *c_alias = NULL
82 c_alias = <char *> blist.purple_buddy_get_alias_only( \
83 self._get_structure())
85 return unicode(c_alias, 'utf-8')
88 alias = property(__get_alias)
90 def __get_account(self):
91 '''Returns the buddy's account.
93 @return The account(if buddy exists) or None.
100 account = property(__get_account)
102 def __get_group(self):
103 '''Returns the buddy's group.
105 @return The group or None if buddy is not in a group.
108 cdef blist.PurpleGroup *c_group = NULL
110 c_group = blist.purple_buddy_get_group(self._get_structure())
111 return <char *> blist.purple_group_get_name(c_group)
114 group = property(__get_group)
116 def __get_server_alias(self):
117 '''Gets the server alias of the buddy.
119 @return The server alias, or None if it is not set.
122 cdef char *c_server_alias = NULL
123 c_server_alias = <char *> blist.purple_buddy_get_server_alias( \
124 self._get_structure())
126 return c_server_alias
129 server_alias = property(__get_server_alias)
131 def __get_contact_alias(self):
132 '''Returns the correct name to display for a buddy, taking the contact
133 alias into account. In order of precedence: the buddy's alias;
134 the buddy's contact alias; the buddy's server alias; the buddy's
137 @return The appropriate name or alias, or None.
140 cdef char *c_contact_alias = NULL
141 c_contact_alias = <char *> blist.purple_buddy_get_contact_alias( \
142 self._get_structure())
144 return c_contact_alias
147 contact_alias = property(__get_contact_alias)
149 def __get_local_alias(self):
150 '''Returns the correct alias for this user, ignoring server aliases.
151 Used when a user-recognizable name is required. In order: buddy's
152 alias; buddy's contact alias; buddy's user name.
154 @return The appropriate name or alias, or None.
157 cdef char *c_local_alias = NULL
158 c_local_alias = <char *> blist.purple_buddy_get_local_alias( \
159 self._get_structure())
164 local_alias = property(__get_local_alias)
166 def __get_available(self):
167 '''Returns whether or not buddy's presence is available.
168 Available presences are online and possibly invisible, but not away or idle.
170 @return True if the buddy's presence is available, or False otherwise.
174 return status.purple_presence_is_available( \
175 blist.purple_buddy_get_presence(self._get_structure()))
178 available = property(__get_available)
180 def __get_online(self):
181 '''Returns whether or not the buddy's presence is online.
183 @return True if the buddy's presence is online, of False otherwise.
187 return status.purple_presence_is_online( \
188 blist.purple_buddy_get_presence(self._get_structure()))
191 online = property(__get_online)
193 def __get_idle(self):
194 '''Returns whether or not the buddy presence is idle.
196 @return True if the presence is idle, or False otherwise.
200 return status.purple_presence_is_idle( \
201 blist.purple_buddy_get_presence(self._get_structure()))
204 idle = property(__get_idle)
206 def __get_active_status(self):
207 '''Returns the buddy's active status.
209 @return The active status.
212 cdef status.PurpleStatus* c_status = NULL
213 cdef char *type = NULL
214 cdef char *name = NULL
215 cdef char *msg = NULL
218 c_status = status.purple_presence_get_active_status( \
219 blist.purple_buddy_get_presence(self._get_structure()))
220 type = <char *> status.purple_status_get_id(c_status)
221 name = <char *> status.purple_status_get_name(c_status)
222 msg = <char *> status.purple_status_get_attr_string(c_status,
225 active['type'] = type
226 active['name'] = name
228 active['message'] = msg
233 active_status = property(__get_active_status)
235 def set_alias(self, alias):
236 '''Sets the buddy's alias.
238 @param alias Buddy alias
239 @return True if success or False if failure to set.
243 blist.purple_blist_alias_buddy(self._get_structure(), alias)
248 def set_group(self, group):
249 '''Sets the buddy's group.
251 @param group Buddy group
252 @return True if success or False if failure to set.
255 cdef blist.PurpleContact *c_contact = NULL
256 cdef blist.PurpleGroup *c_group = NULL
257 if self.__exists and group:
258 c_group = blist.purple_find_group(group)
260 c_group = blist.purple_group_new(group)
262 c_contact = blist.purple_buddy_get_contact(self._get_structure())
263 blist.purple_blist_add_contact(c_contact, c_group, NULL)