=?utf-8?q?Bug=20507683=20=E2=80=93=20Store=20application=20UI=20state
[straw.git] / straw / Config.py
blob6d5cd8aedc00c258c269ec1933c4ee74e9365671
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 Constants 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 from straw import *
32 GCONF_STRAW_ROOT = "/apps/straw"
34 # straw's home directory
35 def straw_home():
36 home = os.getenv('HOME')
37 if not home:
38 home = os.path.expanduser('~')
39 return os.path.join(home, '.straw')
41 def ensure_directory(strawdir):
42 if os.path.exists(strawdir):
43 if not os.path.isdir(strawdir):
44 raise Exception
45 return 0
46 os.mkdir(strawdir)
47 return 1
49 class ConfigGConfPersistence:
50 SAVERS = { int: "int",
51 bool: "bool",
52 str: "string",
53 float: "float" }
55 CASTS = { gconf.VALUE_BOOL: gconf.Value.get_bool,
56 gconf.VALUE_INT: gconf.Value.get_int,
57 gconf.VALUE_FLOAT: gconf.Value.get_float,
58 gconf.VALUE_STRING: gconf.Value.get_string }
60 def __init__(self, client):
61 self.client = client
63 def save_option(self, option, value):
64 getattr(self.client, 'set_' + self.SAVERS[type(value)])(
65 GCONF_STRAW_ROOT + option, value)
67 def load_option(self, key, option_type):
68 gval = self.client.get(GCONF_STRAW_ROOT + key)
70 if gval == None:
71 return None
73 value = None
75 try:
76 value = getattr(gconf.Value, 'get_' + self.SAVERS[option_type])(gval)
77 except TypeError, te:
78 print "FIXME! TypeError logging (option = %s): %s" % (key, str(te))
80 return value
82 class ConfigPersistence:
83 def __init__(self, *backends):
84 self.backends = backends
86 def save_option(self, option, value):
87 for b in self.backends:
88 if option in b[1]:
89 b[0].save_option(option, value)
91 def load_option(self, key, option_type):
92 for b in self.backends:
93 if key in b[1]:
94 return b[0].load_option(key, option_type)
96 class ProxyConfig(object):
97 def __init__(self):
98 self.active = False
99 self.host = None
100 self.port = None
101 self.auth = None
102 self.username = None
103 self.password = None
105 class GconfProxyConfig(ProxyConfig):
106 """Encapsulate proxy use and location information (host, port, ip),
107 gconf reading and name lookup logic"""
109 GCONF_HTTP_PROXY = "/system/http_proxy"
110 GCONF_HTTP_PROXY_USE = GCONF_HTTP_PROXY + "/use_http_proxy"
111 GCONF_HTTP_PROXY_HOST = GCONF_HTTP_PROXY + "/host"
112 GCONF_HTTP_PROXY_PORT = GCONF_HTTP_PROXY + "/port"
113 GCONF_HTTP_PROXY_AUTHENTICATION = GCONF_HTTP_PROXY + "/use_authentication"
114 GCONF_HTTP_PROXY_USER = GCONF_HTTP_PROXY + "/authentication_user"
115 GCONF_HTTP_PROXY_PASSWORD = GCONF_HTTP_PROXY + "/authentication_password"
117 def __init__(self):
118 ProxyConfig.__init__(self)
119 client = gconf.client_get_default()
120 self.active = client.dir_exists(self.GCONF_HTTP_PROXY)
122 if not self.active:
123 return
125 client.add_dir(self.GCONF_HTTP_PROXY, gconf.CLIENT_PRELOAD_RECURSIVE)
126 client.notify_add(self.GCONF_HTTP_PROXY_USE, self.proxy_mode_changed)
127 client.notify_add(self.GCONF_HTTP_PROXY_HOST, self.proxy_host_changed)
128 client.notify_add(self.GCONF_HTTP_PROXY_PORT, self.proxy_port_changed)
129 client.notify_add(self.GCONF_HTTP_PROXY_AUTHENTICATION, self.proxy_auth_changed)
130 client.notify_add(self.GCONF_HTTP_PROXY_USER, self.proxy_auth_username_changed)
131 client.notify_add(self.GCONF_HTTP_PROXY_PASSWORD, self.proxy_auth_password_changed)
133 self.active = client.get_bool(self.GCONF_HTTP_PROXY_USE)
134 self.host = client.get_string(self.GCONF_HTTP_PROXY_HOST)
135 self.port = client.get_int(self.GCONF_HTTP_PROXY_PORT)
136 self.auth = client.get_bool(self.GCONF_HTTP_PROXY_AUTHENTICATION)
137 if self.auth:
138 self.username = client.get_string(self.GCONF_HTTP_PROXY_USER)
139 self.password = client.get_string(self.GCONF_HTTP_PROXY_PASSWORD)
141 def proxy_mode_changed(self, client, notify_id, entry, *args):
142 self.active = entry.value.get_bool()
144 def proxy_host_changed(self, client, notify_id, entry, *args):
145 value = entry.value.get_string()
146 if value:
147 self.host = value
148 else:
149 self.active = False
151 def proxy_port_changed(self, client, notify_id, entry, *args):
152 value = entry.value.get_int()
153 if value:
154 self.port = value
155 else:
156 self.active = False
158 def proxy_auth_changed(self, client, notify_id, entry, *args):
159 value = entry.value.get_bool()
160 if value:
161 self.auth = value
163 def proxy_auth_username_changed(self, client, notify_id, entry, *args):
164 value = entry.value.get_string()
165 self.username = value
167 def proxy_auth_password_changed(self, client, notify_id, entry, *args):
168 value = entry.value.get_string()
169 self.password = value
171 class EnvironmentProxyConfig(ProxyConfig):
172 """Encapsulate proxy use and location information, environment reading"""
174 def __init__(self):
175 ProxyConfig.__init__(self)
176 self._read_env()
178 def _read_env(self):
179 proxy = None
180 proxies = urllib.getproxies()
181 if not proxies:
182 return
184 for key, value in proxies.iteritems():
185 proxy = proxies.get(key)
186 user, authority = urllib.splituser(proxy)
187 if authority:
188 self.host, self.port = urllib.splitport(authority)
189 self.active = True
190 if user:
191 self.username, self.password = urllib.splitpasswd(user)
192 self.auth = True
193 return
195 class Config(gobject.GObject):
196 __gsignals__ = {
197 'item-order-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
198 'offline-mode-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
199 'refresh-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
200 'item-stored-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
203 def __init__(self, persistence, defaults):
204 gobject.GObject.__init__(self)
206 self._defaults = dict(defaults)
207 self._options = dict(defaults)
208 self.persistence = persistence
210 self.initialize_options()
211 self.initialize_proxy()
213 self._apply_defaults()
215 def _apply_defaults(self):
216 for name, value in self._defaults.iteritems():
217 if not name in self._options or self._options[name] == None:
218 self._options[name] = value
220 def initialize_proxy(self):
221 # EnvironmentProxy has precedence
222 self.proxy = EnvironmentProxyConfig()
224 if not self.proxy.active:
225 # .. defaults to GConfProxyConfig so we can listen for network
226 # setting changes (e.g, changes in network preferences)
227 self.proxy = GconfProxyConfig()
229 def initialize_options(self):
230 for key in self._options.keys():
231 self._options[key] = self.persistence.load_option(key, type(self._defaults[key]))
233 def get(self, key):
234 if key in self._options and self._options[key] != None:
235 return self._options[key]
236 else:
237 return self._defaults[key]
239 def set(self, key, value):
240 if self._options.has_key(key):# and self._options[key] != value:
241 if value:
242 value = type(self._defaults[key])(value)
244 self.persistence.save_option(key, value)
245 self._options[key] = value
247 def create_gconf_persistence():
248 client = gconf.client_get_default()
249 client.add_dir(GCONF_STRAW_ROOT, gconf.CLIENT_PRELOAD_ONELEVEL)
250 return ConfigGConfPersistence(client)
252 def create_instance():
253 defaults = config_options_defaults
255 cp = ConfigPersistence((create_gconf_persistence(), defaults.keys()))
256 config = Config(cp, defaults)
258 return config
260 config_instance = None
262 def _get_instance():
263 global config_instance
265 if not config_instance:
266 config_instance = create_instance()
268 return config_instance
270 def get(key):
271 config = _get_instance()
272 return config.get(key)
274 def set(key, value):
275 config = _get_instance()
276 return config.set(key, value)
278 @property
279 def proxy():
280 config = _get_instance()
281 return config.proxy