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/>.
25 @param name Buddy's name.
26 @param account Buddy's account.
33 def __init__(self, name, account):
35 self.__account = account
37 if self._get_structure() != NULL:
42 cdef blist.PurpleBuddy *_get_structure(self):
43 '''Returns the buddy's C struct from purple.
45 @return A pointer to buddy's C struct from purple.
48 return blist.purple_find_buddy(account.purple_accounts_find( \
49 self.__account.username, self.__account.protocol.id), \
52 def __get_exists(self):
53 '''Answer if exists corresponding buddy in the purple.
55 @return True if buddy is a valid buddy of False otherwise.
59 exists = property(__get_exists)
62 '''Returns the buddy's name.
68 return <char *> blist.purple_buddy_get_name(self._get_structure())
71 name = property(__get_name)
73 def __get_alias(self):
74 '''Returns the buddy's alias
76 @return Buddy alias(if set) or None
79 cdef char *c_alias = NULL
80 c_alias = <char *> blist.purple_buddy_get_alias_only( \
81 self._get_structure())
83 return unicode(c_alias, 'utf-8')
86 alias = property(__get_alias)
88 def __get_account(self):
89 '''Returns the buddy's account.
91 @return The account(if buddy exists) or None.
98 account = property(__get_account)
100 def __get_group(self):
101 '''Returns the buddy's group.
103 @return The group or None if buddy is not in a group.
106 cdef blist.PurpleGroup *c_group = NULL
108 c_group = blist.purple_buddy_get_group(self._get_structure())
109 return <char *> blist.purple_group_get_name(c_group)
112 group = property(__get_group)
114 def __get_server_alias(self):
115 '''Gets the server alias of the buddy.
117 @return The server alias, or None if it is not set.
120 cdef char *c_server_alias = NULL
121 c_server_alias = <char *> blist.purple_buddy_get_server_alias( \
122 self._get_structure())
124 return c_server_alias
127 server_alias = property(__get_server_alias)
129 def __get_contact_alias(self):
130 '''Returns the correct name to display for a buddy, taking the contact
131 alias into account. In order of precedence: the buddy's alias;
132 the buddy's contact alias; the buddy's server alias; the buddy's
135 @return The appropriate name or alias, or None.
138 cdef char *c_contact_alias = NULL
139 c_contact_alias = <char *> blist.purple_buddy_get_contact_alias( \
140 self._get_structure())
142 return c_contact_alias
145 contact_alias = property(__get_contact_alias)
147 def __get_local_alias(self):
148 '''Returns the correct alias for this user, ignoring server aliases.
149 Used when a user-recognizable name is required. In order: buddy's
150 alias; buddy's contact alias; buddy's user name.
152 @return The appropriate name or alias, or None.
155 cdef char *c_local_alias = NULL
156 c_local_alias = <char *> blist.purple_buddy_get_local_alias( \
157 self._get_structure())
162 local_alias = property(__get_local_alias)
164 def __get_available(self):
165 '''Returns whether or not buddy's presence is available.
166 Available presences are online and possibly invisible, but not away or idle.
168 @return True if the buddy's presence is available, or False otherwise.
172 return status.purple_presence_is_available( \
173 blist.purple_buddy_get_presence(self._get_structure()))
176 available = property(__get_available)
178 def __get_online(self):
179 '''Returns whether or not the buddy's presence is online.
181 @return True if the buddy's presence is online, of False otherwise.
185 return status.purple_presence_is_online( \
186 blist.purple_buddy_get_presence(self._get_structure()))
189 online = property(__get_online)
191 def __get_idle(self):
192 '''Returns whether or not the buddy presence is idle.
194 @return True if the presence is idle, or False otherwise.
198 return status.purple_presence_is_idle( \
199 blist.purple_buddy_get_presence(self._get_structure()))
202 idle = property(__get_idle)
204 def __get_active_status(self):
205 '''Returns the buddy's active status.
207 @return The active status.
210 cdef status.PurpleStatus* c_status = NULL
211 cdef char *type = NULL
212 cdef char *name = NULL
213 cdef char *msg = NULL
216 c_status = status.purple_presence_get_active_status( \
217 blist.purple_buddy_get_presence(self._get_structure()))
218 type = <char *> status.purple_status_get_id(c_status)
219 name = <char *> status.purple_status_get_name(c_status)
220 msg = <char *> status.purple_status_get_attr_string(c_status,
223 active['type'] = type
224 active['name'] = name
226 active['message'] = msg
231 active_status = property(__get_active_status)
233 def set_alias(self, alias):
234 '''Sets the buddy's alias.
236 @param alias Buddy alias
237 @return True if success or False if failure to set.
241 blist.purple_blist_alias_buddy(self._get_structure(), alias)
246 def set_group(self, group):
247 '''Sets the buddy's group.
249 @param group Buddy group
250 @return True if success or False if failure to set.
253 cdef blist.PurpleContact *c_contact = NULL
254 cdef blist.PurpleGroup *c_group = NULL
255 if self.__exists and group:
256 c_group = blist.purple_find_group(group)
258 c_group = blist.purple_group_new(group)
260 c_contact = blist.purple_buddy_get_contact(self._get_structure())
261 blist.purple_blist_add_contact(c_contact, c_group, NULL)