Removed URLFetch module (its functionality is now provided by Fetcher module).
[straw.git] / straw / Config.py
blob656c8712bfcfb7e2968622618af48c96ab26b991
1 """ Config.py
3 Module for Straw's configuration settings.
4 """
5 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__ = """ GNU General Public License
8 This program is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA. """
21 from ConfigOptions import *
22 from error import log, logtb, logparam
23 import gconf
24 import gobject
25 import os
26 import pygtk
27 import urllib
28 pygtk.require('2.0')
30 GCONF_STRAW_ROOT = "/apps/straw"
32 # straw's home directory
33 def straw_home():
34 home = os.getenv('HOME')
35 if not home:
36 home = os.path.expanduser('~')
37 return os.path.join(home, '.straw')
39 def ensure_directory(strawdir):
40 if os.path.exists(strawdir):
41 if not os.path.isdir(strawdir):
42 raise Exception
43 return 0
44 os.mkdir(strawdir)
45 return 1
47 class ConfigGConfPersistence:
48 SAVERS = { int: "int",
49 bool: "bool",
50 str: "string",
51 float: "float"
54 def __init__(self, client):
55 self.client = client
57 def save_option(self, option, value):
58 getattr(self.client, 'set_' + self.SAVERS[type(value)])(
59 GCONF_STRAW_ROOT + option, value)
61 def load_option(self, key, option_type):
62 return getattr(self.client, 'get_' + self.SAVERS[option_type])(
63 GCONF_STRAW_ROOT + key)
65 class ConfigPersistence:
66 def __init__(self, *backends):
67 self.backends = backends
69 def save_option(self, option, value):
70 for b in self.backends:
71 if option in b[1]:
72 b[0].save_option(option, value)
74 def load_option(self, key, option_type):
75 for b in self.backends:
76 if key in b[1]:
77 return b[0].load_option(key, option_type)
79 class ProxyConfig(object):
80 def __init__(self):
81 self._active = False
82 self._host = None
83 self._port = None
84 self._auth = None
85 self._username = None
86 self._password = None
88 @apply
89 def active():
90 doc = "this will be true when one of the subclasses is used"
91 def fget(self):
92 return self._active
93 def fset(self, active):
94 self._active = active
95 return property(**locals())
97 @apply
98 def host():
99 doc = "the host name where the proxy server lives"
100 def fget(self):
101 return self._host
102 def fset(self, host):
103 self._host = host
104 return property(**locals())
106 @apply
107 def port():
108 doc = "the port of the proxy server to connect to"
109 def fget(self):
110 return self._port
111 def fset(self, port):
112 self._port = port
113 return property(**locals())
115 @apply
116 def auth():
117 doc = "true if we need to authenticate to the proxy server"
118 def fget(self):
119 return self._auth
120 def fset(self, isauth):
121 self._auth = isauth
122 return property(**locals())
124 @apply
125 def username():
126 doc = "the username to use when authenticating to the proxy"
127 def fget(self):
128 return self._username
129 def fset(self, username):
130 self._username = username
131 return property(**locals())
133 @apply
134 def password():
135 doc = "the password to use when authenticating to the proxy"
136 def fget(self):
137 return self._password
138 def fset(self, password):
139 self._password = password
140 return property(**locals())
143 class GconfProxyConfig(ProxyConfig):
144 """Encapsulate proxy use and location information (host, port, ip),
145 gconf reading and name lookup logic"""
147 GCONF_HTTP_PROXY = "/system/http_proxy"
148 GCONF_HTTP_PROXY_USE = GCONF_HTTP_PROXY + "/use_http_proxy"
149 GCONF_HTTP_PROXY_HOST = GCONF_HTTP_PROXY + "/host"
150 GCONF_HTTP_PROXY_PORT = GCONF_HTTP_PROXY + "/port"
151 GCONF_HTTP_PROXY_AUTHENTICATION = GCONF_HTTP_PROXY + "/use_authentication"
152 GCONF_HTTP_PROXY_USER = GCONF_HTTP_PROXY + "/authentication_user"
153 GCONF_HTTP_PROXY_PASSWORD = GCONF_HTTP_PROXY + "/authentication_password"
155 def __init__(self):
156 ProxyConfig.__init__(self)
157 client = gconf.client_get_default()
158 self.active = client.dir_exists(self.GCONF_HTTP_PROXY)
159 if not self.active:
160 return
162 client.add_dir(self.GCONF_HTTP_PROXY, gconf.CLIENT_PRELOAD_RECURSIVE)
163 client.notify_add(self.GCONF_HTTP_PROXY_USE, self.proxy_mode_changed)
164 client.notify_add(self.GCONF_HTTP_PROXY_HOST, self.proxy_host_changed)
165 client.notify_add(self.GCONF_HTTP_PROXY_PORT, self.proxy_port_changed)
166 client.notify_add(self.GCONF_HTTP_PROXY_AUTHENTICATION, self.proxy_auth_changed)
167 client.notify_add(self.GCONF_HTTP_PROXY_USER, self.proxy_auth_username_changed)
168 client.notify_add(self.GCONF_HTTP_PROXY_PASSWORD, self.proxy_auth_password_changed)
170 self.active = client.get_bool(self.GCONF_HTTP_PROXY_USE)
171 self.host = client.get_string(self.GCONF_HTTP_PROXY_HOST)
172 self.port = client.get_int(self.GCONF_HTTP_PROXY_PORT)
173 self.auth = client.get_bool(self.GCONF_HTTP_PROXY_AUTHENTICATION)
174 if self.auth:
175 self.username = client.get_string(self.GCONF_HTTP_PROXY_USER)
176 self.password = client.get_string(self.GCONF_HTTP_PROXY_PASSWORD)
179 # here be gconf logic
180 def proxy_mode_changed(self, client, notify_id, entry, *args):
181 self.active = entry.value.get_bool()
183 def proxy_host_changed(self, client, notify_id, entry, *args):
184 value = entry.value.get_string()
185 if value:
186 self.host = value
187 else:
188 self.active = False
190 def proxy_port_changed(self, client, notify_id, entry, *args):
191 value = entry.value.get_int()
192 if value:
193 self.port = value
194 else:
195 self.active = False
197 def proxy_auth_changed(self, client, notify_id, entry, *args):
198 value = entry.value.get_bool()
199 if value:
200 self.auth = value
202 def proxy_auth_username_changed(self, client, notify_id, entry, *args):
203 value = entry.value.get_string()
204 self.username = value
206 def proxy_auth_password_changed(self, client, notify_id, entry, *args):
207 value = entry.value.get_string()
208 self.password = value
210 class EnvironmentProxyConfig(ProxyConfig):
211 """Encapsulate proxy use and location information, environment reading"""
213 def __init__(self):
214 ProxyConfig.__init__(self)
215 self._read_env()
217 def _read_env(self):
218 proxy = None
219 proxies = urllib.getproxies()
220 if not proxies:
221 return
223 for key, value in proxies.iteritems():
224 proxy = proxies.get(key)
225 user, authority = urllib.splituser(proxy)
226 if authority:
227 self.host, self.port = urllib.splitport(authority)
228 self.active = True
229 if user:
230 self.username, self.password = urllib.splitpasswd(user)
231 self.auth = True
232 return
234 class Config(gobject.GObject):
235 __gsignals__ = {
236 'item-order-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
237 'offline-mode-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
238 'refresh-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
239 'item-stored-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
242 def __init__(self, persistence, defaults):
243 gobject.GObject.__init__(self)
245 self._defaults = defaults
246 self._options = defaults
247 self.persistence = persistence
249 self.initialize_proxy()
251 self._apply_defaults()
253 def _apply_defaults(self):
254 pass
256 def initialize_proxy(self):
257 # EnvironmentProxy has precedence
258 self._proxy = EnvironmentProxyConfig()
259 if not self._proxy.active:
260 # .. defaults to GConfProxyConfig so we can listen for network
261 # setting changes (e.g, changes in network preference)
262 self._proxy = GconfProxyConfig()
264 def initialize_options(self):
265 for key in self._options.keys():
266 self._options[key] = self.persistence.load_option(key, type(self._defaults[key]))
268 def get(self, key):
269 if self._options.has_key(key) and self._options[key] != None:
270 return self._options[key]
271 else:
272 return self._defaults[key]
274 def set(self, key, value):
275 if self._options.has_key(key) and self._options[key] != value:
276 self.persistence.save_option(key, value)
277 self._options[key] = value
279 def create_gconf_persistence():
280 client = gconf.client_get_default()
281 client.add_dir(GCONF_STRAW_ROOT, gconf.CLIENT_PRELOAD_ONELEVEL)
282 return ConfigGConfPersistence(client)
284 def create_instance():
285 defaults = config_options_defaults
287 cp = ConfigPersistence((create_gconf_persistence(), defaults.keys()))
288 config = Config(cp, defaults)
290 config.initialize_proxy()
291 config.initialize_options()
293 return config
295 config_instance = None
297 def _get_instance():
298 global config_instance
300 if not config_instance:
301 config_instance = create_instance()
303 return config_instance
305 def get(key):
306 config = _get_instance()
307 return config.get(key)
309 def set(key, value):
310 config = _get_instance()
311 return config.set(key, value)