257c42555271617e290a877b0b68d320690e8562
[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 *__account
28     cdef plugin.PurplePlugin *c_plugin
29     cdef prpl.PurplePluginProtocolInfo *c_prpl_info
30     cdef plugin.PurplePluginInfo *c_plugin_info
31
32     cdef savedstatuses.PurpleSavedStatus *__sstatus
33
34     def __init__(self, char *username, char *protocol_id):
35         self.__account = account.c_purple_account_new(username, protocol_id)
36         self.c_plugin = plugin.c_purple_plugins_find_with_id(protocol_id)
37         self.c_prpl_info = plugin.c_PURPLE_PLUGIN_PROTOCOL_INFO(self.c_plugin)
38
39
40     def set_password(self, password):
41         account.c_purple_account_set_password(self.__account, password)
42
43     def set_enabled(self, ui, value):
44         account.c_purple_account_set_enabled(self.__account, ui, value)
45
46     def get_acc_username(self):
47         if self.__account:
48             return account.c_purple_account_get_username(self.__account)
49
50     def get_password(self):
51         if self.__account:
52             return account.c_purple_account_get_password(self.__account)
53
54     def set_status(self):
55         self.__sstatus = savedstatuses.c_purple_savedstatus_new(NULL, status.PURPLE_STATUS_AVAILABLE)
56         savedstatuses.c_purple_savedstatus_activate(self.__sstatus)
57
58     def get_buddies_online(self):
59         cdef glib.GSList *iter
60         cdef blist.PurpleBuddy *buddy
61         buddies = []
62         iter = blist.c_purple_find_buddies(self.__account, NULL)
63         while iter:
64             buddy = <blist.PurpleBuddy *> iter.data
65             if <blist.PurpleBuddy *>buddy and \
66                 account.c_purple_account_is_connected(blist.c_purple_buddy_get_account(buddy)) and \
67                 status.c_purple_presence_is_online(blist.c_purple_buddy_get_presence(buddy)):
68                 buddies += [buddy.name]
69             iter = iter.next
70         return buddies
71
72     def get_proxyinfo(self):
73         cdef proxy.PurpleProxyInfo *c_proxyinfo
74         c_proxyinfo = account.c_purple_account_get_proxy_info(self.__account)
75         if c_proxyinfo == NULL:
76             return None
77         cdef ProxyInfo proxyinfo
78         proxyinfo = proxy.ProxyInfo()
79         proxyinfo.c_proxyinfo = c_proxyinfo
80         return proxyinfo
81
82     def set_proxyinfo(self, ProxyInfo proxyinf):
83         account.c_purple_account_set_proxy_info(self.__account, proxyinf.c_proxyinfo)
84
85     def get_protocol_options(self):
86         ''' FIXME: It is just a hack, to set the XMPP's options. '''
87         cdef glib.GList *iter
88         cdef account.PurpleAccountOption *option
89         cdef prefs.PurplePrefType type
90         cdef const_char *label_name
91         cdef const_char *str_value
92         cdef const_char *setting
93         cdef int int_value
94         cdef glib.gboolean bool_value
95         iter = self.c_prpl_info.protocol_options
96         while iter:
97             option = <account.PurpleAccountOption *> iter.data
98             type = account.c_purple_account_option_get_type(option)
99             label_name = account.c_purple_account_option_get_text(option)
100             setting = account.c_purple_account_option_get_setting(option)
101             if type == prefs.PURPLE_PREF_STRING:
102                 str_value = account.c_purple_account_option_get_default_string(option)
103
104                 # Google Talk default domain hackery!
105                 if str_value == NULL and str(<char *> label_name) == "Connect server":
106                     str_value = "talk.google.com"
107
108                 if self.__account != NULL:
109                     str_value = account.c_purple_account_get_string(self.__account, setting, str_value)
110                     account.c_purple_account_set_string(self.__account, setting, str_value );
111
112             elif type == prefs.PURPLE_PREF_INT:
113                 int_value = account.c_purple_account_option_get_default_int(option)
114                 if self.__account != NULL:
115                    int_value = account.c_purple_account_get_int(self.__account, setting, int_value)
116                    if str(<char *> setting) == "port":
117                         account.c_purple_account_set_int(self.__account, setting, 443);
118
119             elif type == prefs.PURPLE_PREF_BOOLEAN:
120                 bool_value = account.c_purple_account_option_get_default_bool(option)
121                 if self.__account != NULL:
122                     bool_value = account.c_purple_account_get_bool(self.__account, setting, bool_value)
123                     if str(<char *> setting) == "old_ssl":
124                         account.c_purple_account_set_bool(self.__account, setting, True);
125
126             elif type == prefs.PURPLE_PREF_STRING_LIST:
127                 str_value = account.c_purple_account_option_get_default_list_value(option)
128                 if self.__account != NULL:
129                     str_value = account.c_purple_account_get_string(self.__account, setting, str_value)
130
131             iter = iter.next
132