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