6de9af84be96fed45fb305ccfd90d831de3940c5
[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
49     def set_password(self, password):
50         account.c_purple_account_set_password(self.c_account, password)
51
52     def set_enabled(self, ui, value):
53         account.c_purple_account_set_enabled(self.c_account, ui, value)
54
55     def get_acc_username(self):
56         if self.c_account:
57             return account.c_purple_account_get_username(self.c_account)
58
59     def get_password(self):
60         if self.c_account:
61             return account.c_purple_account_get_password(self.c_account)
62
63     def set_status(self):
64         self.__sstatus = savedstatuses.c_purple_savedstatus_new(NULL, status.PURPLE_STATUS_AVAILABLE)
65         savedstatuses.c_purple_savedstatus_activate(self.__sstatus)
66
67     def get_proxy(self):
68         return self.__proxy
69
70     proxy = property(get_proxy)
71
72     def get_buddies_online(self):
73         cdef glib.GSList *iter
74         cdef blist.PurpleBuddy *buddy
75         buddies = []
76         iter = blist.c_purple_find_buddies(self.c_account, NULL)
77         while iter:
78             buddy = <blist.PurpleBuddy *> iter.data
79             if <blist.PurpleBuddy *>buddy and \
80                 account.c_purple_account_is_connected(blist.c_purple_buddy_get_account(buddy)) and \
81                 status.c_purple_presence_is_online(blist.c_purple_buddy_get_presence(buddy)):
82                 buddies += [buddy.name]
83             iter = iter.next
84         return buddies
85
86     def get_protocol_options(self):
87         ''' FIXME: It is just a hack, to set the XMPP's options. '''
88         cdef glib.GList *iter
89         cdef accountopt.PurpleAccountOption *option
90         cdef prefs.PurplePrefType type
91         cdef const_char *label_name
92         cdef const_char *str_value
93         cdef const_char *setting
94         cdef int int_value
95         cdef glib.gboolean bool_value
96         iter = self.c_prpl_info.protocol_options
97         while iter:
98             option = <accountopt.PurpleAccountOption *> iter.data
99             type = accountopt.c_purple_account_option_get_type(option)
100             label_name = accountopt.c_purple_account_option_get_text(option)
101             setting = accountopt.c_purple_account_option_get_setting(option)
102             if type == prefs.PURPLE_PREF_STRING:
103                 str_value = accountopt.c_purple_account_option_get_default_string(option)
104
105                 # Google Talk default domain hackery!
106                 if str_value == NULL and str(<char *> label_name) == "Connect server":
107                     str_value = "talk.google.com"
108
109                 if self.c_account != NULL:
110                     str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
111                     account.c_purple_account_set_string(self.c_account, setting, str_value );
112
113             elif type == prefs.PURPLE_PREF_INT:
114                 int_value = accountopt.c_purple_account_option_get_default_int(option)
115                 if self.c_account != NULL:
116                    int_value = account.c_purple_account_get_int(self.c_account, setting, int_value)
117                    if str(<char *> setting) == "port":
118                         account.c_purple_account_set_int(self.c_account, setting, 443);
119
120             elif type == prefs.PURPLE_PREF_BOOLEAN:
121                 bool_value = accountopt.c_purple_account_option_get_default_bool(option)
122                 if self.c_account != NULL:
123                     bool_value = account.c_purple_account_get_bool(self.c_account, setting, bool_value)
124                     if str(<char *> setting) == "old_ssl":
125                         account.c_purple_account_set_bool(self.c_account, setting, True);
126
127             elif type == prefs.PURPLE_PREF_STRING_LIST:
128                 str_value = accountopt.c_purple_account_option_get_default_list_value(option)
129                 if self.c_account != NULL:
130                     str_value = account.c_purple_account_get_string(self.c_account, setting, str_value)
131
132             iter = iter.next
133