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