Updated internal class attributes with correct naming.
[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         buddies = []
144         iter = blist.c_purple_find_buddies(self.c_account, NULL)
145         while iter:
146             buddy = <blist.PurpleBuddy *> iter.data
147             if <blist.PurpleBuddy *>buddy and \
148                 account.c_purple_account_is_connected(blist.c_purple_buddy_get_account(buddy)) and \
149                 status.c_purple_presence_is_online(blist.c_purple_buddy_get_presence(buddy)):
150                 buddies += [buddy.name]
151             iter = iter.next
152         return buddies
153
154     def get_protocol_options(self):
155         ''' FIXME: It is just a hack, to set the XMPP's options. '''
156         cdef glib.GList *iter
157         cdef accountopt.PurpleAccountOption *option
158         cdef prefs.PurplePrefType type
159         cdef const_char *label_name
160         cdef const_char *str_value
161         cdef const_char *setting
162         cdef int int_value
163         cdef glib.gboolean bool_value
164         iter = self.c_prpl_info.protocol_options
165         while iter:
166             option = <accountopt.PurpleAccountOption *> iter.data
167             type = accountopt.c_purple_account_option_get_type(option)
168             label_name = accountopt.c_purple_account_option_get_text(option)
169             setting = accountopt.c_purple_account_option_get_setting(option)
170             if type == prefs.PURPLE_PREF_STRING:
171                 str_value = accountopt.c_purple_account_option_get_default_string(option)
172
173                 # Google Talk default domain hackery!
174                 if str_value == NULL and str(<char *> label_name) == "Connect server":
175                     str_value = "talk.google.com"
176
177                 if self.c_account != NULL:
178                     str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
179                     account.c_purple_account_set_string(self.c_account, setting, str_value)
180
181             elif type == prefs.PURPLE_PREF_INT:
182                 int_value = accountopt.c_purple_account_option_get_default_int(option)
183                 if self.c_account != NULL:
184                    int_value = account.c_purple_account_get_int(self.c_account, setting, int_value)
185                    if str(<char *> setting) == "port":
186                         account.c_purple_account_set_int(self.c_account, setting, 443)
187
188             elif type == prefs.PURPLE_PREF_BOOLEAN:
189                 bool_value = accountopt.c_purple_account_option_get_default_bool(option)
190                 if self.c_account != NULL:
191                     bool_value = account.c_purple_account_get_bool(self.c_account, setting, bool_value)
192                     if str(<char *> setting) == "old_ssl":
193                         account.c_purple_account_set_bool(self.c_account, setting, True)
194
195             elif type == prefs.PURPLE_PREF_STRING_LIST:
196                 str_value = accountopt.c_purple_account_option_get_default_list_value(option)
197                 if self.c_account != NULL:
198                     str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
199
200             iter = iter.next
201
202     def save_into_xml(self):
203         account.c_purple_accounts_add(self.c_account)