NetworkConstants.py + ConfigOptions.py = Constants.py
[straw.git] / straw / Config.py
blobcb6493e9ae93627f16caf63b13f725821323131c
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"
56 def __init__(self, client):
57 self.client = client
59 def save_option(self, option, value):
60 getattr(self.client, 'set_' + self.SAVERS[type(value)])(
61 GCONF_STRAW_ROOT + option, value)
63 def load_option(self, key, option_type):
64 return getattr(self.client, 'get_' + self.SAVERS[option_type])(
65 GCONF_STRAW_ROOT + key)
67 class ConfigPersistence:
68 def __init__(self, *backends):
69 self.backends = backends
71 def save_option(self, option, value):
72 for b in self.backends:
73 if option in b[1]:
74 b[0].save_option(option, value)
76 def load_option(self, key, option_type):
77 for b in self.backends:
78 if key in b[1]:
79 return b[0].load_option(key, option_type)
81 class ProxyConfig(object):
82 def __init__(self):
83 self.active = False
84 self.host = None
85 self.port = None
86 self.auth = None
87 self.username = None
88 self.password = None
90 class GconfProxyConfig(ProxyConfig):
91 """Encapsulate proxy use and location information (host, port, ip),
92 gconf reading and name lookup logic"""
94 GCONF_HTTP_PROXY = "/system/http_proxy"
95 GCONF_HTTP_PROXY_USE = GCONF_HTTP_PROXY + "/use_http_proxy"
96 GCONF_HTTP_PROXY_HOST = GCONF_HTTP_PROXY + "/host"
97 GCONF_HTTP_PROXY_PORT = GCONF_HTTP_PROXY + "/port"
98 GCONF_HTTP_PROXY_AUTHENTICATION = GCONF_HTTP_PROXY + "/use_authentication"
99 GCONF_HTTP_PROXY_USER = GCONF_HTTP_PROXY + "/authentication_user"
100 GCONF_HTTP_PROXY_PASSWORD = GCONF_HTTP_PROXY + "/authentication_password"
102 def __init__(self):
103 ProxyConfig.__init__(self)
104 client = gconf.client_get_default()
105 self.active = client.dir_exists(self.GCONF_HTTP_PROXY)
107 if not self.active:
108 return
110 client.add_dir(self.GCONF_HTTP_PROXY, gconf.CLIENT_PRELOAD_RECURSIVE)
111 client.notify_add(self.GCONF_HTTP_PROXY_USE, self.proxy_mode_changed)
112 client.notify_add(self.GCONF_HTTP_PROXY_HOST, self.proxy_host_changed)
113 client.notify_add(self.GCONF_HTTP_PROXY_PORT, self.proxy_port_changed)
114 client.notify_add(self.GCONF_HTTP_PROXY_AUTHENTICATION, self.proxy_auth_changed)
115 client.notify_add(self.GCONF_HTTP_PROXY_USER, self.proxy_auth_username_changed)
116 client.notify_add(self.GCONF_HTTP_PROXY_PASSWORD, self.proxy_auth_password_changed)
118 self.active = client.get_bool(self.GCONF_HTTP_PROXY_USE)
119 self.host = client.get_string(self.GCONF_HTTP_PROXY_HOST)
120 self.port = client.get_int(self.GCONF_HTTP_PROXY_PORT)
121 self.auth = client.get_bool(self.GCONF_HTTP_PROXY_AUTHENTICATION)
122 if self.auth:
123 self.username = client.get_string(self.GCONF_HTTP_PROXY_USER)
124 self.password = client.get_string(self.GCONF_HTTP_PROXY_PASSWORD)
126 def proxy_mode_changed(self, client, notify_id, entry, *args):
127 self.active = entry.value.get_bool()
129 def proxy_host_changed(self, client, notify_id, entry, *args):
130 value = entry.value.get_string()
131 if value:
132 self.host = value
133 else:
134 self.active = False
136 def proxy_port_changed(self, client, notify_id, entry, *args):
137 value = entry.value.get_int()
138 if value:
139 self.port = value
140 else:
141 self.active = False
143 def proxy_auth_changed(self, client, notify_id, entry, *args):
144 value = entry.value.get_bool()
145 if value:
146 self.auth = value
148 def proxy_auth_username_changed(self, client, notify_id, entry, *args):
149 value = entry.value.get_string()
150 self.username = value
152 def proxy_auth_password_changed(self, client, notify_id, entry, *args):
153 value = entry.value.get_string()
154 self.password = value
156 class EnvironmentProxyConfig(ProxyConfig):
157 """Encapsulate proxy use and location information, environment reading"""
159 def __init__(self):
160 ProxyConfig.__init__(self)
161 self._read_env()
163 def _read_env(self):
164 proxy = None
165 proxies = urllib.getproxies()
166 if not proxies:
167 return
169 for key, value in proxies.iteritems():
170 proxy = proxies.get(key)
171 user, authority = urllib.splituser(proxy)
172 if authority:
173 self.host, self.port = urllib.splitport(authority)
174 self.active = True
175 if user:
176 self.username, self.password = urllib.splitpasswd(user)
177 self.auth = True
178 return
180 class Config(gobject.GObject):
181 __gsignals__ = {
182 'item-order-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
183 'offline-mode-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
184 'refresh-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
185 'item-stored-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
188 def __init__(self, persistence, defaults):
189 gobject.GObject.__init__(self)
191 self._defaults = defaults
192 self._options = defaults
193 self.persistence = persistence
195 self.initialize_proxy()
197 self._apply_defaults()
199 def _apply_defaults(self):
200 pass
202 def initialize_proxy(self):
203 # EnvironmentProxy has precedence
204 self.proxy = EnvironmentProxyConfig()
206 if not self.proxy.active:
207 # .. defaults to GConfProxyConfig so we can listen for network
208 # setting changes (e.g, changes in network preferences)
209 self.proxy = GconfProxyConfig()
211 def initialize_options(self):
212 for key in self._options.keys():
213 self._options[key] = self.persistence.load_option(key, type(self._defaults[key]))
215 def get(self, key):
216 if self._options.has_key(key) and self._options[key] != None:
217 return self._options[key]
218 else:
219 return self._defaults[key]
221 def set(self, key, value):
222 if self._options.has_key(key) and self._options[key] != value:
223 self.persistence.save_option(key, value)
224 self._options[key] = value
226 def create_gconf_persistence():
227 client = gconf.client_get_default()
228 client.add_dir(GCONF_STRAW_ROOT, gconf.CLIENT_PRELOAD_ONELEVEL)
229 return ConfigGConfPersistence(client)
231 def create_instance():
232 defaults = config_options_defaults
234 cp = ConfigPersistence((create_gconf_persistence(), defaults.keys()))
235 config = Config(cp, defaults)
237 config.initialize_proxy()
238 config.initialize_options()
240 return config
242 config_instance = None
244 def _get_instance():
245 global config_instance
247 if not config_instance:
248 config_instance = create_instance()
250 return config_instance
252 def get(key):
253 config = _get_instance()
254 return config.get(key)
256 def set(key, value):
257 config = _get_instance()
258 return config.set(key, value)
260 @property
261 def proxy():
262 config = _get_instance()
263 return config.proxy