16e5dcd6169e7b959af67e83ed43ae3a9681eb7a
[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 from protocol import Protocol
23
24 cdef class Account:
25     """
26     Account class
27     @param username
28     @param protocol_id
29     """
30
31     def __init__(self, username, protocol_id):
32         self.__username = username
33         self.__protocol = Protocol(self, protocol_id)
34
35         if self._get_structure() == NULL:
36             self.__exists = False
37         else:
38             self.__exists = True
39
40     cdef account.PurpleAccount *_get_structure(self):
41         return account.purple_accounts_find(self.username, \
42                 self.protocol_id)
43
44     def __is_connected(self):
45         if self.__exists:
46             return account.purple_account_is_connected(self._get_structure())
47         else:
48             return None
49     is_connected = property(__is_connected)
50
51     def __is_connecting(self):
52         if self.__exists:
53             return account.purple_account_is_connecting(self._get_structure())
54         else:
55             return None
56     is_connecting = property(__is_connecting)
57
58     def __is_disconnected(self):
59         if self.__exists:
60             return account.purple_account_is_disconnected( \
61                     self._get_structure())
62         else:
63             return None
64     is_disconnected = property(__is_disconnected)
65
66     def __get_exists(self):
67         return self.__exists
68     exists = property(__get_exists)
69
70     def __get_username(self):
71         cdef char *username = NULL
72         if self.__exists:
73             username = <char *> account.purple_account_get_username( \
74                     self._get_structure())
75             if username:
76                 return username
77             else:
78                 return None
79         else:
80             return self.__username
81     username = property(__get_username)
82
83     def __get_protocol_id(self):
84         cdef char *protocol_id = NULL
85         if self.__exists:
86             protocol_id = <char *> account.purple_account_get_protocol_id( \
87                     self._get_structure())
88             if protocol_id:
89                 return protocol_id
90             else:
91                 return None
92         else:
93             return self.protocol_id
94     protocol_id = property(__get_protocol_id)
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_id(self, protocol_id):
164         """
165         Sets the account's protocol ID.
166
167         @param protocol_id The protocol ID
168         @return True if successful, False if account doesn't exists
169         """
170         if self.__exists:
171             self.__protocol._set_protocol_id(protocol_id)
172             return True
173         else:
174             return False
175
176     def set_password(self, password):
177         """
178         Sets the account's password.
179
180         @param password The password
181         @return True if successful, False if account doesn't exists
182         """
183         if self.__exists:
184             account.purple_account_set_password(self._get_structure(), \
185                     password)
186             return True
187         else:
188             return False
189
190     def set_alias(self, alias):
191         """
192         Sets the account's alias
193
194         @param alias The alias
195         @return True if successful, False if account doesn't exists
196         """
197         if self.__exists:
198             account.purple_account_set_alias(self._get_structure(), \
199                     alias)
200             return True
201         else:
202             return False
203
204     def set_user_info(self, user_info):
205         """
206         Sets the account's user information
207
208         @param user_info The user information
209         @return True if successful, False if account doesn't exists
210         """
211         if self.__exists:
212             account.purple_account_set_user_info(self._get_structure(), \
213                     user_info)
214             return True
215         else:
216             return False
217
218     def set_remember_password(self, remember_password):
219         """
220         Sets whether or not this account should save its password.
221
222         @param remember_password True if should remember the password,
223                                  or False otherwise
224         @return True if successful, False if account doesn't exists
225         """
226         if self.__exists:
227             account.purple_account_set_remember_password( \
228                 self._get_structure(), remember_password)
229             return True
230         else:
231             return False
232
233     def set_enabled(self, value):
234         """
235         Sets wheter or not this account is enabled.
236
237         @param value True if it is enabled, or False otherwise
238         @return True if successful, False if account doesn't exists
239         """
240         if self.__exists:
241             account.purple_account_set_enabled(self._get_structure(), \
242                     self.core.ui_name, bool(value))
243             return True
244         else:
245             return False
246
247     def new(self):
248         """
249         Creates a new account.
250
251         @return True if successful, False if account already exists
252         """
253         if self.__exists:
254             return False
255         else:
256             account.purple_account_new(self.username, self.protocol_id)
257             self.__exists = True
258             return True
259
260     def connect(self):
261         """
262         Connects to an account.
263
264         @return True if successful, False if account doesn't exists
265         """
266         if self.__exists:
267             account.purple_account_connect(self._get_structure())
268             return True
269         else:
270             return False
271
272     def disconnect(self):
273         """
274         Disconnects from an account.
275
276         @return True if successful, False if account doesn't exists
277         """
278         if self.__exists:
279             account.purple_account_disconnect(self._get_structure())
280             return True
281         else:
282             return False