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