Added "connection-error" signal callback.
[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 extern from *:
23     ctypedef char const_char "const char"
24
25 cdef class Account:
26     """ Account class """
27     cdef account.PurpleAccount *c_account
28     cdef plugin.PurplePlugin *c_plugin
29     cdef prpl.PurplePluginProtocolInfo *c_prpl_info
30     cdef plugin.PurplePluginInfo *c_plugin_info
31     cdef savedstatuses.PurpleSavedStatus *__sstatus
32     cdef ProxyInfo __proxy
33
34     def __init__(self, char *username, char *protocol_id):
35         cdef proxy.PurpleProxyInfo *c_proxyinfo
36         cdef account.PurpleAccount *acc = NULL
37
38         acc = account.c_purple_accounts_find(username, protocol_id)
39         if acc:
40             self.c_account = acc
41             c_proxyinfo = account.c_purple_account_get_proxy_info(self.c_account)
42         else:
43             self.c_account = account.c_purple_account_new(username, protocol_id)
44             c_proxyinfo = account.c_purple_account_get_proxy_info(self.c_account)
45             if c_proxyinfo == NULL:
46                 c_proxyinfo = proxy.c_purple_proxy_info_new()
47                 proxy.c_purple_proxy_info_set_type(c_proxyinfo, proxy.PURPLE_PROXY_NONE)
48                 account.c_purple_account_set_proxy_info(self.c_account, c_proxyinfo)
49         self.__proxy = ProxyInfo()
50         self.__proxy.c_proxyinfo = c_proxyinfo
51         acc = NULL
52
53         self.c_plugin = plugin.c_purple_plugins_find_with_id(protocol_id)
54         self.c_prpl_info = plugin.c_PURPLE_PLUGIN_PROTOCOL_INFO(self.c_plugin)
55
56     def __get_username(self):
57         if self.c_account:
58             return account.c_purple_account_get_username(self.c_account)
59         else:
60             return None
61     def __set_username(self, username):
62         if self.c_account:
63             account.c_purple_account_set_username(self.c_account, username)
64     username = property(__get_password, __set_username)
65
66     def __get_password(self):
67         if self.c_account:
68             return account.c_purple_account_get_password(self.c_account)
69         else:
70             return None
71     def __set_password(self, password):
72         if self.c_account:
73             account.c_purple_account_set_password(self.c_account, password)
74     password = property(__get_password, __set_password)
75
76     def __get_alias(self):
77         if self.c_account:
78             return account.c_purple_account_get_alias(self.c_account)
79         else:
80             return None
81     def __set_alias(self, alias):
82         if self.c_account:
83             account.c_purple_account_set_alias(self.c_account, alias)
84     alias = property(__get_alias, __set_alias)
85
86     def __get_user_info(self):
87         if self.c_account:
88             return account.c_purple_account_get_user_info(self.c_account)
89         else:
90             return None
91     def __set_user_info(self, user_info):
92         if self.c_account:
93             account.c_purple_account_set_user_info(self.c_account, user_info)
94     user_info = property(__get_user_info, __set_user_info)
95
96     def __get_protocol_id(self):
97         if self.c_account:
98             return account.c_purple_account_get_protocol_id(self.c_account)
99         else:
100             return None
101     def __set_protocol_id(self, protocol_id):
102         if self.c_account:
103             account.c_purple_account_set_protocol_id(self.c_account, protocol_id)
104     protocol_id = property(__get_protocol_id, __set_protocol_id)
105
106     def __get_remember_password(self):
107         if self.c_account:
108             return account.c_purple_account_get_remember_password(self.c_account)
109         else:
110             return None
111     def __set_remember_password(self, value):
112         if self.c_account:
113             account.c_purple_account_set_remember_password(self.c_account, value)
114     remember_password = property(__get_remember_password, __set_remember_password)
115
116     def get_protocol_name(self):
117         if self.c_account:
118             return account.c_purple_account_get_protocol_name(self.c_account)
119         else:
120             return None
121
122     def get_enabled(self, ui):
123         if self.c_account:
124             return account.c_purple_account_get_enabled(self.c_account, ui)
125         else:
126             return None
127
128     def set_enabled(self, ui, value):
129         if self.c_account:
130             account.c_purple_account_set_enabled(self.c_account, ui, value)
131
132     def set_status(self):
133         self.__sstatus = savedstatuses.c_purple_savedstatus_new(NULL, status.PURPLE_STATUS_AVAILABLE)
134         savedstatuses.c_purple_savedstatus_activate(self.__sstatus)
135
136     def __get_proxy(self):
137         return self.__proxy
138     proxy = property(__get_proxy)
139
140     def get_buddies_online(self):
141         cdef glib.GSList *iter
142         cdef blist.PurpleBuddy *buddy
143         cdef char *c_name = NULL
144         cdef char *c_alias = NULL
145         buddies = []
146         iter = blist.c_purple_find_buddies(self.c_account, NULL)
147         while iter:
148             c_name = NULL
149             c_alias = NULL
150             buddy = <blist.PurpleBuddy *> iter.data
151             if <blist.PurpleBuddy *>buddy and \
152                 account.c_purple_account_is_connected(blist.c_purple_buddy_get_account(buddy)) and \
153                 status.c_purple_presence_is_online(blist.c_purple_buddy_get_presence(buddy)):
154                 c_name = <char *> blist.c_purple_buddy_get_name(buddy)
155                 if c_name == NULL:
156                     name = None
157                 else:
158                     name = c_name
159                 c_alias = <char *> blist.c_purple_buddy_get_alias_only(buddy)
160                 if c_alias == NULL:
161                     alias = None
162                 else:
163                     alias = c_alias
164                 buddies.append((name, alias))
165             iter = iter.next
166         return buddies
167
168     def get_protocol_options(self):
169         ''' FIXME: It is just a hack, to set the XMPP's options. '''
170         cdef glib.GList *iter
171         cdef accountopt.PurpleAccountOption *option
172         cdef prefs.PurplePrefType type
173         cdef const_char *label_name
174         cdef const_char *str_value
175         cdef const_char *setting
176         cdef int int_value
177         cdef glib.gboolean bool_value
178         iter = self.c_prpl_info.protocol_options
179         while iter:
180             option = <accountopt.PurpleAccountOption *> iter.data
181             type = accountopt.c_purple_account_option_get_type(option)
182             label_name = accountopt.c_purple_account_option_get_text(option)
183             setting = accountopt.c_purple_account_option_get_setting(option)
184             if type == prefs.PURPLE_PREF_STRING:
185                 str_value = accountopt.c_purple_account_option_get_default_string(option)
186
187                 # Google Talk default domain hackery!
188                 if str_value == NULL and str(<char *> label_name) == "Connect server":
189                     str_value = "talk.google.com"
190
191                 if self.c_account != NULL:
192                     str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
193                     account.c_purple_account_set_string(self.c_account, setting, str_value)
194
195             elif type == prefs.PURPLE_PREF_INT:
196                 int_value = accountopt.c_purple_account_option_get_default_int(option)
197                 if self.c_account != NULL:
198                    int_value = account.c_purple_account_get_int(self.c_account, setting, int_value)
199                    if str(<char *> setting) == "port":
200                         account.c_purple_account_set_int(self.c_account, setting, 443)
201
202             elif type == prefs.PURPLE_PREF_BOOLEAN:
203                 bool_value = accountopt.c_purple_account_option_get_default_bool(option)
204                 if self.c_account != NULL:
205                     bool_value = account.c_purple_account_get_bool(self.c_account, setting, bool_value)
206                     if str(<char *> setting) == "old_ssl":
207                         account.c_purple_account_set_bool(self.c_account, setting, True)
208
209             elif type == prefs.PURPLE_PREF_STRING_LIST:
210                 str_value = accountopt.c_purple_account_option_get_default_list_value(option)
211                 if self.c_account != NULL:
212                     str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
213
214             iter = iter.next
215
216     def save_into_xml(self):
217         account.c_purple_accounts_add(self.c_account)