[Sugar-devel] [PATCH sugar v5 3/3] sugar-session: export manual Gnome proxy settings as $http_proxy

Sascha Silbe silbe at activitycentral.com
Tue Aug 7 09:32:36 EDT 2012


Some applications and tools and even some parts of Sugar will use the
http_proxy and no_proxy environment variables [1] if set, but don't use the
Gnome (GConf) proxy settings.

After changing the GConf proxy settings, Sugar needs to be restarted for these
environment variables to be updated. The Network Control Panel will now show
a restart alert that informs the user about the need to restart and offer to
do the restart immediately (other options are undo and restart later).

[1] https://www.gnu.org/software/wget/manual/html_node/Proxies.html

Based on a patch by Jerry Vonau <jvonau at shaw.ca>.

Signed-off-by: Sascha Silbe <silbe at activitycentral.com>
---
v4->v5: no changes
original version->v4: Add support for no_proxy, some minor changes

 bin/sugar-session                    |   29 +++++++++++++++++++++++++++++
 extensions/cpsection/network/view.py |   34 +++++++++++++++++++++++++++++++++-
 2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/bin/sugar-session b/bin/sugar-session
index e9c8d50..a2c4204 100755
--- a/bin/sugar-session
+++ b/bin/sugar-session
@@ -208,6 +208,34 @@ def set_fonts():
     settings = gtk.settings_get_default()
     settings.set_property("gtk-font-name", "%s %f" % (face, size))
 
+
+def export_proxy_settings():
+    """Export manual proxy settings from GConf as environment variables
+
+    Some applications and tools and even some parts of Sugar will use
+    the http_proxy environment variable if set, but don't use the Gnome
+    (GConf) proxy settings.
+    """
+    client = gconf.client_get_default()
+    if client.get_string('/system/proxy/mode') != 'manual':
+        return
+
+    http_host = client.get_string('/system/http_proxy/host')
+    http_port = client.get_int('/system/http_proxy/port')
+    use_auth = client.get_bool('/system/http_proxy/use_authentication')
+    if use_auth:
+        user = client.get_string('/system/http_proxy/authentication_user')
+        pw = client.get_string('/system/http_proxy/authentication_password')
+        http_proxy = 'http://%s:%s@%s:%d/' % (user, pw, http_host, http_port)
+    else:
+        http_proxy = 'http://%s:%d/' % (http_host, http_port)
+
+    os.environ['http_proxy'] = http_proxy
+    ignore_hosts = client.get_list('/system/http_proxy/ignore_hosts',
+                                   gconf.VALUE_STRING)
+    os.environ['no_proxy'] = ','.join(ignore_hosts)
+
+
 def main():
     try:
         from sugar import env
@@ -243,6 +271,7 @@ def main():
     if timezone is not None and timezone:
         os.environ['TZ'] = timezone
 
+    export_proxy_settings()
     set_fonts()
 
     # this must be added early, so that it executes and unfreezes the screen
diff --git a/extensions/cpsection/network/view.py b/extensions/cpsection/network/view.py
index e3dd48d..5c46ae3 100644
--- a/extensions/cpsection/network/view.py
+++ b/extensions/cpsection/network/view.py
@@ -140,6 +140,10 @@ class GConfMixin(object):
         client = gconf.client_get_default()
         return _gconf_value_to_python(client.get(self._gconf_key))
 
+    @property
+    def changed(self):
+        return self._undo_value != self.get_value_for_gconf()
+
 
 class GConfEntry(gtk.Entry, GConfMixin):
     """Text entry backed by GConf
@@ -241,6 +245,10 @@ class GConfHostListSettingBox(GConfStringSettingBox):
         """Revert to original value if modified"""
         self.hosts_entry.undo()
 
+    @property
+    def changed(self):
+        return self.hosts_entry.changed
+
 
 class GConfHostPortSettingBox(SettingBox):
     """A configuration line for a combined host name and port GConf setting"""
@@ -263,6 +271,10 @@ class GConfHostPortSettingBox(SettingBox):
         self.host_name_entry.undo()
         self.port_spin_button.undo()
 
+    @property
+    def changed(self):
+        return self.host_name_entry.changed or self.port_spin_button.changed
+
 
 class ExclusiveOptionSetsBox(gtk.VBox):
     """
@@ -695,7 +707,6 @@ class Network(SectionView):
 
         self._jabber_valid = True
         self._radio_valid = True
-        self.needs_restart = False
         self._radio_change_handler = self._button.connect( \
                 'toggled', self.__radio_toggled_cb)
         self._jabber_change_handler = self._entry.connect( \
@@ -713,6 +724,27 @@ class Network(SectionView):
         for setting in self._undo_objects:
             setting.undo()
 
+    # pylint: disable=E0202
+    @property
+    def needs_restart(self):
+        # Some parts of Sugar as well as many non-Gnome applications
+        # use environment variables rather than gconf for proxy
+        # settings, so we need to restart for the changes to take
+        # _full_ effect.
+        for setting in self._undo_objects:
+            if setting.changed:
+                return True
+
+        return False
+
+    # pylint: disable=E0102,E1101
+    @needs_restart.setter
+    def needs_restart(self, value):
+        # needs_restart is a property (i.e. gets calculated) in this Control
+        # Panel, but SectionView.__init__() wants to initialise it to False,
+        # so we need to provide a (fake) setter.
+        pass
+
     def _validate(self):
         if self._jabber_valid and self._radio_valid:
             self.props.is_valid = True
-- 
1.7.10.4



More information about the Sugar-devel mailing list