Updated libpurple/pounce.pxd
[python-purple] / account.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 Account:
23     """
24     Account class
25     @param username
26     @param protocol Protocol class instance
27     @param core Purple class instance
28     """
29
30     cdef object __username
31     cdef object __protocol
32     cdef object __core
33     cdef object __exists
34
35     def __init__(self, username, protocol, core):
36         self.__username = username
37         self.__protocol = protocol
38         self.__core = core
39
40         if protocol.exists and self._get_structure() != NULL:
41             self.__exists = True
42         else:
43             self.__exists = False
44
45     cdef account.PurpleAccount *_get_structure(self):
46         return account.purple_accounts_find(self.__username, \
47                 self.__protocol.id)
48
49     def __is_connected(self):
50         if self.__exists:
51             return account.purple_account_is_connected(self._get_structure())
52         else:
53             return None
54     is_connected = property(__is_connected)
55
56     def __is_connecting(self):
57         if self.__exists:
58             return account.purple_account_is_connecting(self._get_structure())
59         else:
60             return None
61     is_connecting = property(__is_connecting)
62
63     def __is_disconnected(self):
64         if self.__exists:
65             return account.purple_account_is_disconnected( \
66                     self._get_structure())
67         else:
68             return None
69     is_disconnected = property(__is_disconnected)
70
71     def __get_core(self):
72         return self.__core
73     core = property(__get_core)
74
75     def __get_exists(self):
76         return self.__exists
77     exists = property(__get_exists)
78
79     def __get_username(self):
80         cdef char *username = NULL
81         if self.__exists:
82             username = <char *> account.purple_account_get_username( \
83                     self._get_structure())
84             if username:
85                 return username
86             else:
87                 return None
88         else:
89             return self.__username
90     username = property(__get_username)
91
92     def __get_protocol(self):
93         return self.__protocol
94     protocol = property(__get_protocol)
95
96     def __get_password(self):
97         cdef char *password = NULL
98         if self.__exists:
99             password = <char *> account.purple_account_get_password( \
100                     self._get_structure())
101             if password:
102                 return password
103             else:
104                 return None
105         else:
106             return None
107     password = property(__get_password)
108
109     def __get_alias(self):
110         cdef char *alias = NULL
111         if self.__exists:
112             alias = <char *> account.purple_account_get_alias(self._get_structure())
113             if alias:
114                 return alias
115             else:
116                 return None
117         else:
118             return None
119     alias = property(__get_alias)
120
121     def __get_user_info(self):
122         cdef char *user_info = NULL
123         if self.__exists:
124             user_info = <char *> account.purple_account_get_user_info(self._get_structure())
125             if user_info:
126                 return user_info
127             else:
128                 return None
129         else:
130             return None
131     user_info = property(__get_user_info)
132
133     def __get_remember_password(self):
134         if self.__exists:
135             return account.purple_account_get_remember_password( \
136                     self._get_structure())
137         else:
138             return None
139     remember_password = property(__get_remember_password)
140
141     def __get_enabled(self):
142         if self.__exists:
143             return account.purple_account_get_enabled(self._get_structure(), \
144                     self.__core.ui_name)
145         else:
146             return None
147     enabled = property(__get_enabled)
148
149     def set_username(self, username):
150         """
151         Sets the account's username.
152
153         @param username The username
154         @return True if successful, False if account doesn't exists
155         """
156         if self.__exists:
157             account.purple_account_set_username(self._get_structure(), \
158                     username)
159             return True
160         else:
161             return False
162
163     def set_protocol(self, protocol):
164         """
165         Sets the account's protocol.
166
167         @param protocol A Protocol class instance
168         @return True if successful, False if account doesn't exists
169         """
170         if protocol.exists and self.__exists:
171             account.purple_account_set_protocol_id(self._get_structure(), \
172                         protocol.id)
173             self.__protocol = protocol
174             return True
175         else:
176             return False
177
178     def set_password(self, password):
179         """
180         Sets the account's password.
181
182         @param password The password
183         @return True if successful, False if account doesn't exists
184         """
185         if self.__exists:
186             account.purple_account_set_password(self._get_structure(), \
187                     password)
188             return True
189         else:
190             return False
191
192     def set_alias(self, alias):
193         """
194         Sets the account's alias
195
196         @param alias The alias
197         @return True if successful, False if account doesn't exists
198         """
199         if self.__exists:
200             account.purple_account_set_alias(self._get_structure(), \
201                     alias)
202             return True
203         else:
204             return False
205
206     def set_user_info(self, user_info):
207         """
208         Sets the account's user information
209
210         @param user_info The user information
211         @return True if successful, False if account doesn't exists
212         """
213         if self.__exists:
214             account.purple_account_set_user_info(self._get_structure(), \
215                     user_info)
216             return True
217         else:
218             return False
219
220     def set_remember_password(self, remember_password):
221         """
222         Sets whether or not this account should save its password.
223
224         @param remember_password True if should remember the password,
225                                  or False otherwise
226         @return True if successful, False if account doesn't exists
227         """
228         if self.__exists:
229             account.purple_account_set_remember_password( \
230                 self._get_structure(), remember_password)
231             return True
232         else:
233             return False
234
235     def set_enabled(self, value):
236         """
237         Sets wheter or not this account is enabled.
238
239         @param value True if it is enabled, or False otherwise
240         @return True if successful, False if account doesn't exists
241         """
242         if self.__exists:
243             account.purple_account_set_enabled(self._get_structure(), \
244                     self.__core.ui_name, bool(value))
245             return True
246         else:
247             return False
248
249     def new(self):
250         """
251         Creates a new account.
252
253         @return True if successful, False if account already exists
254         """
255         if self.__exists:
256             return False
257         else:
258             account.purple_account_new(self.__username, self.__protocol.id)
259             self.__exists = True
260             return True
261
262     def connect(self):
263         """
264         Connects to an account.
265
266         @return True if successful, False if account doesn't exists
267         """
268         if self.__exists:
269             account.purple_account_connect(self._get_structure())
270             return True
271         else:
272             return False
273
274     def disconnect(self):
275         """
276         Disconnects from an account.
277
278         @return True if successful, False if account doesn't exists
279         """
280         if self.__exists:
281             account.purple_account_disconnect(self._get_structure())
282             return True
283         else:
284             return False