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