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