Removed ecore necessity from python-purple.
[python-purple] / proxy.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 class ProxyInfoType:
23     cdef proxy.PurpleProxyType c_type
24
25     def __init__(self):
26         self.c_type = proxy.PURPLE_PROXY_NONE
27
28     def get_NONE(self):
29         self.c_type = proxy.PURPLE_PROXY_NONE
30         return self
31     NONE = property(get_NONE)
32
33     def get_USE_GLOBAL(self):
34         self.c_type = proxy.PURPLE_PROXY_USE_GLOBAL
35         return self
36     USE_GLOBAL = property(get_USE_GLOBAL)
37
38     def get_HTTP(self):
39         self.c_type = proxy.PURPLE_PROXY_HTTP
40         return self
41     HTTP = property(get_HTTP)
42
43     def get_SOCKS4(self):
44         self.c_type = proxy.PURPLE_PROXY_SOCKS4
45         return self
46
47     def get_SOCKS5(self):
48         self.c_type = proxy.PURPLE_PROXY_SOCKS5
49         return self
50
51     def get_USE_ENVVAR (self):
52         self.c_type = proxy.PURPLE_PROXY_USE_ENVVAR
53         return self
54     USE_ENVVAR = property(get_USE_ENVVAR)
55
56 cdef class ProxyInfo:
57
58     cdef proxy.PurpleProxyInfo *c_proxyinfo
59     cdef object types
60
61     def __init__(self):
62         self.c_proxyinfo = NULL
63
64         self.types = {"HTTP": proxy.PURPLE_PROXY_HTTP,
65                 "USER_GLOBAL": proxy.PURPLE_PROXY_USE_GLOBAL,
66                 "USE_ENVVAR": proxy.PURPLE_PROXY_USE_ENVVAR,
67                 "SOCKS4": proxy.PURPLE_PROXY_SOCKS4,
68                 "SOCKS5": proxy.PURPLE_PROXY_SOCKS5,
69                 "NONE": proxy.PURPLE_PROXY_NONE}
70
71
72     def set_type(self, ProxyInfoType type):
73         if self.c_proxyinfo:
74             proxy.c_purple_proxy_info_set_type(self.c_proxyinfo, type.c_type)
75
76     def set_host(self, char *host):
77         if self.c_proxyinfo:
78             proxy.c_purple_proxy_info_set_host(self.c_proxyinfo, host)
79
80     def set_port(self, int port):
81         if self.c_proxyinfo:
82             proxy.c_purple_proxy_info_set_port(self.c_proxyinfo, port)
83
84     def set_username(self, char *username):
85         if self.c_proxyinfo:
86             proxy.c_purple_proxy_info_set_username(self.c_proxyinfo, username)
87
88     def set_password(self, char *password):
89         if self.c_proxyinfo:
90             proxy.c_purple_proxy_info_set_password(self.c_proxyinfo, password)
91
92     def get_types(self):
93         return self.types.keys()
94
95     def set_info(self, acc, info):
96         ''' @param acc Tuple (username, protocol id) '''
97         ''' @param info Dictionary {'type': "HTTP", 'port': "1234", '''
98         '''   'host': "1.2.3.4", 'username': "foo", 'passworld': "foo123"} '''
99
100         cdef account.PurpleAccount *c_account
101         cdef proxy.PurpleProxyInfo *c_proxyinfo
102         c_account = account.purple_accounts_find(acc[0], acc[1])
103
104         if c_account == NULL:
105             #FIXME: Message error or call a callback handle to error
106             return False
107
108         c_proxyinfo = account.purple_account_get_proxy_info(c_account)
109         if c_proxyinfo == NULL:
110                 c_proxyinfo = proxy.c_purple_proxy_info_new()
111                 account.purple_account_set_proxy_info(c_account, c_proxyinfo)
112
113         if 'type' in info and info['type']:
114             type = info['type']
115             if not type in self.types:
116                 type = 'HTTP'
117             proxy.c_purple_proxy_info_set_type(c_proxyinfo, self.types[type])
118
119         if 'host' in info and info['host']:
120             host = info['host']
121             proxy.c_purple_proxy_info_set_host(c_proxyinfo, host)
122
123         if 'port' in info and info['port']:
124             port = int(info['port'])
125             proxy.c_purple_proxy_info_set_port(c_proxyinfo, port)
126
127         if 'username' in info and info['username']:
128             username = info['username']
129             proxy.c_purple_proxy_info_set_username(c_proxyinfo, username)
130
131         if 'password' in info and info['password']:
132             password = info['password']
133             proxy.c_purple_proxy_info_set_password(c_proxyinfo, password)
134
135         return True