Fixing support to request_action callback
[python-purple] / buddy.pyx
1 #
2 #  Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
3 #
4 #  This file is part of python-purple.
5 #
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.
10 #
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.
15 #
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/>.
18 #
19
20 cimport purple
21
22 cdef class Buddy:
23     '''Buddy class
24
25     @param name Buddy's name.
26     @param account Buddy's account.
27     '''
28
29     cdef object __account
30     cdef object __name
31     cdef object __exists
32
33     def __init__(self, name, account):
34         self.__name = name
35         self.__account = account
36
37         if self._get_structure() != NULL:
38             self.__exists = True
39         else:
40             self.__exists = False
41
42     cdef blist.PurpleBuddy *_get_structure(self):
43         '''Returns the buddy's C struct from purple.
44
45         @return A pointer to buddy's C struct from purple.
46         '''
47
48         return blist.purple_find_buddy(account.purple_accounts_find( \
49                 self.__account.username, self.__account.protocol.id), \
50                 self.__name)
51
52     def __get_exists(self):
53         '''Answer if exists corresponding buddy in the purple.
54
55         @return True if buddy is a valid buddy of False otherwise.
56         '''
57
58         return self.__exists
59     exists = property(__get_exists)
60
61     def __get_name(self):
62         '''Returns the buddy's name.
63
64         @return Buddy's name.
65         '''
66
67         if self.__exists:
68             return <char *> blist.purple_buddy_get_name(self._get_structure())
69         else:
70             return self.__name
71     name = property(__get_name)
72
73     def __get_alias(self):
74         '''Returns the buddy's alias
75
76         @return Buddy alias(if set) or None
77         '''
78
79         cdef char *c_alias = NULL
80         c_alias = <char *> blist.purple_buddy_get_alias_only( \
81                 self._get_structure())
82         if c_alias:
83             return unicode(c_alias, 'utf-8')
84         else:
85             return None
86     alias = property(__get_alias)
87
88     def __get_account(self):
89         '''Returns the buddy's account.
90
91         @return The account(if buddy exists) or None.
92         '''
93
94         if self.__exists:
95             return self.__account
96         else:
97             return None
98     account = property(__get_account)
99
100     def __get_group(self):
101         '''Returns the buddy's group.
102
103         @return The group or None if buddy is not in a group.
104         '''
105
106         cdef blist.PurpleGroup *c_group = NULL
107         if self.__exists:
108             c_group = blist.purple_buddy_get_group(self._get_structure())
109             return <char *> blist.purple_group_get_name(c_group)
110         else:
111             return None
112     group = property(__get_group)
113
114     def __get_server_alias(self):
115         '''Gets the server alias of the buddy.
116
117         @return  The server alias, or None if it is not set.
118         '''
119
120         cdef char *c_server_alias = NULL
121         c_server_alias = <char *> blist.purple_buddy_get_server_alias( \
122                 self._get_structure())
123         if c_server_alias:
124             return c_server_alias
125         else:
126             return None
127     server_alias = property(__get_server_alias)
128
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
133            user name.
134
135         @return The appropriate name or alias, or None.
136         '''
137
138         cdef char *c_contact_alias = NULL
139         c_contact_alias = <char *> blist.purple_buddy_get_contact_alias( \
140                 self._get_structure())
141         if c_contact_alias:
142             return c_contact_alias
143         else:
144             return None
145     contact_alias = property(__get_contact_alias)
146
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.
151
152         @return The appropriate name or alias, or None.
153         '''
154
155         cdef char *c_local_alias = NULL
156         c_local_alias = <char *> blist.purple_buddy_get_local_alias( \
157                 self._get_structure())
158         if c_local_alias:
159             return c_local_alias
160         else:
161             return None
162     local_alias = property(__get_local_alias)
163
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.
167
168         @return True if the buddy's presence is available, or False otherwise.
169         '''
170
171         if self.__exists:
172             return status.purple_presence_is_available( \
173                     blist.purple_buddy_get_presence(self._get_structure()))
174         else:
175             return False
176     available = property(__get_available)
177
178     def __get_online(self):
179         '''Returns whether or not the buddy's presence is online.
180
181         @return True if the buddy's presence is online, of False otherwise.
182         '''
183
184         if self.__exists:
185             return status.purple_presence_is_online( \
186                     blist.purple_buddy_get_presence(self._get_structure()))
187         else:
188             return False
189     online = property(__get_online)
190
191     def __get_idle(self):
192         '''Returns whether or not the buddy presence is idle.
193
194         @return True if the presence is idle, or False otherwise.
195         '''
196
197         if self.__exists:
198             return status.purple_presence_is_idle( \
199                     blist.purple_buddy_get_presence(self._get_structure()))
200         else:
201             return False
202     idle = property(__get_idle)
203
204     def __get_active_status(self):
205         '''Returns the buddy's active status.
206
207         @return The active status.
208         '''
209
210         cdef status.PurpleStatus* c_status = NULL
211         cdef char *type = NULL
212         cdef char *name = NULL
213         cdef char *msg = NULL
214         if self.__exists:
215             active = {}
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,
221                 "message")
222
223             active['type'] = type
224             active['name'] = name
225             if msg:
226                 active['message'] = msg
227
228             return active
229         else:
230             return None
231     active_status = property(__get_active_status)
232
233     def set_alias(self, alias):
234         '''Sets the buddy's alias.
235
236         @param alias Buddy alias
237         @return True if success or False if failure to set.
238         '''
239
240         if self.__exists:
241             blist.purple_blist_alias_buddy(self._get_structure(), alias)
242             return True
243         else:
244             return False
245
246     def set_group(self, group):
247         '''Sets the buddy's group.
248
249         @param group Buddy group
250         @return True if success or False if failure to set.
251         '''
252
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)
257             if c_group == NULL:
258                 c_group = blist.purple_group_new(group)
259
260             c_contact = blist.purple_buddy_get_contact(self._get_structure())
261             blist.purple_blist_add_contact(c_contact, c_group, NULL)
262             return True
263         else:
264             return False