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