[gnome-terminal] Use gsettings for proxy settings



commit 342b8d1959d85ec1142fc0f4b7564455d9f734a9
Author: Christian Persch <chpe gnome org>
Date:   Thu Apr 7 22:53:38 2011 +0200

    Use gsettings for proxy settings
    
    It sucks to construct four GSettings object to just get a few settings
    and then immediately dispose them again, but the fix for that depends on
    fixes that exist only in my glib gsettingslist branch.
    
    Bug #647077.

 src/terminal-app.c  |   25 ++++--
 src/terminal-app.h  |    2 +
 src/terminal-util.c |  230 ++++++++++++++++++++-------------------------------
 3 files changed, 110 insertions(+), 147 deletions(-)
---
diff --git a/src/terminal-app.c b/src/terminal-app.c
index ed79541..0f198e7 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -52,6 +52,8 @@
 #define DESKTOP_INTERFACE_SETTINGS_SCHEMA       "org.gnome.desktop.interface"
 #define MONOSPACE_FONT_KEY_NAME                 "monospace-font-name"
 
+#define SYSTEM_PROXY_SETTINGS_SCHEMA            "org.gnome.system.proxy"
+
 /* Settings storage works as follows:
  *   /apps/gnome-terminal/global/
  *   /apps/gnome-terminal/profiles/Foo/
@@ -103,6 +105,7 @@ struct _TerminalApp
   guint enable_menu_accels_notify_id;
 
   GSettings *desktop_interface_settings;
+  GSettings *system_proxy_settings;
 
   GHashTable *profiles;
   char* default_profile_id;
@@ -1379,6 +1382,9 @@ terminal_app_init (TerminalApp *app)
 
   gtk_window_set_default_icon_name (GNOME_TERMINAL_ICON_NAME);
 
+  /* Desktop proxy settings */
+  app->system_proxy_settings = g_settings_new (SYSTEM_PROXY_SETTINGS_SCHEMA);
+
   /* Terminal global settings */
   app->desktop_interface_settings = g_settings_new (DESKTOP_INTERFACE_SETTINGS_SCHEMA);
   terminal_app_system_font_notify_cb (app->desktop_interface_settings,
@@ -1402,12 +1408,6 @@ terminal_app_init (TerminalApp *app)
   gconf_client_add_dir (app->conf, CONF_GLOBAL_PREFIX,
                         GCONF_CLIENT_PRELOAD_ONELEVEL,
                         NULL);
-  gconf_client_add_dir (app->conf, CONF_PROXY_PREFIX,
-                        GCONF_CLIENT_PRELOAD_ONELEVEL,
-                        NULL);
-  gconf_client_add_dir (app->conf, CONF_HTTP_PROXY_PREFIX,
-                        GCONF_CLIENT_PRELOAD_ONELEVEL,
-                        NULL);
 
   app->profile_list_notify_id =
     gconf_client_notify_add (app->conf, PROFILE_LIST_KEY,
@@ -1511,6 +1511,7 @@ terminal_app_finalize (GObject *object)
   pango_font_description_free (app->system_font_desc);
 
   g_object_unref (app->desktop_interface_settings);
+  g_object_unref (app->system_proxy_settings);
 
   terminal_accels_shutdown ();
 
@@ -2102,3 +2103,15 @@ terminal_app_save_config_file (TerminalApp *app,
 
   return result;
 }
+
+/**
+ * terminal_app_get_proxy_settings:
+ * @app: a #TerminalApp
+ *
+ * Returns: (tranfer none): the cached #GSettings object for the org.gnome.system.proxy schema
+ */
+GSettings *
+terminal_app_get_proxy_settings (TerminalApp *app)
+{
+  return app->system_proxy_settings;
+}
diff --git a/src/terminal-app.h b/src/terminal-app.h
index 5c489c3..0590a4e 100644
--- a/src/terminal-app.h
+++ b/src/terminal-app.h
@@ -154,6 +154,8 @@ gboolean terminal_app_save_config_file (TerminalApp *app,
                                         const char *file_name,
                                         GError **error);
 
+GSettings *terminal_app_get_proxy_settings (TerminalApp *app);
+
 G_END_DECLS
 
 #endif /* !TERMINAL_APP_H */
diff --git a/src/terminal-util.c b/src/terminal-util.c
index 78c869b..89db419 100644
--- a/src/terminal-util.c
+++ b/src/terminal-util.c
@@ -3,7 +3,7 @@
  * Copyright © 2002 Red Hat, Inc.
  * Copyright © 2002 Sun Microsystems
  * Copyright © 2003 Mariano Suarez-Alvarez
- * Copyright © 2008 Christian Persch
+ * Copyright © 2008, 2011 Christian Persch
  *
  * Gnome-terminal is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -32,13 +32,13 @@
 #include <gio/gio.h>
 #include <gtk/gtk.h>
 
-#include <gconf/gconf.h>
-
 #ifdef GDK_WINDOWING_X11
 #include <gdk/gdkx.h>
 #include <X11/Xatom.h>
 #endif
 
+#include <gdesktop-enums.h>
+
 #include "terminal-accels.h"
 #include "terminal-app.h"
 #include "terminal-intl.h"
@@ -514,20 +514,6 @@ terminal_util_key_file_get_argv (GKeyFile *key_file,
 
 /* Proxy stuff */
 
-static char *
-conf_get_string (GConfClient *conf,
-                 const char *key)
-{
-  char *value;
-  value = gconf_client_get_string (conf, key, NULL);
-  if (G_UNLIKELY (value && *value == '\0'))
-    {
-      g_free (value);
-      value = NULL;
-    }
-  return value;
-}
-
 /*
  * set_proxy_env:
  * @env_table: a #GHashTable
@@ -540,7 +526,7 @@ conf_get_string (GConfClient *conf,
 static void
 set_proxy_env (GHashTable *env_table,
                const char *key,
-               char *value)
+               char *value /* consumed */)
 {
   char *key1 = NULL, *key2 = NULL;
   char *value1 = NULL, *value2 = NULL;
@@ -577,139 +563,102 @@ set_proxy_env (GHashTable *env_table,
 }
 
 static void
-setup_http_proxy_env (GHashTable *env_table,
-                      GConfClient *conf)
+setup_proxy_env (GSettings  *proxy_settings,
+                 const char *child_schema_id,
+                 const char *proxy_scheme,
+                 const char *env_name,
+                 GHashTable *env_table)
 {
-  gchar *host;
-  gint port;
-  GSList *ignore;
+  GSettings *child_settings;
+  GString *buf;
+  const char *host;
+  int port;
+  gboolean is_http;
 
-  if (!gconf_client_get_bool (conf, CONF_HTTP_PROXY_PREFIX "/use_http_proxy", NULL))
-    return;
+  is_http = (strcmp (child_schema_id, "http") == 0);
 
-  host = conf_get_string (conf, CONF_HTTP_PROXY_PREFIX "/host");
-  port = gconf_client_get_int (conf, CONF_HTTP_PROXY_PREFIX "/port", NULL);
-  if (host && port)
-    {
-      GString *buf = g_string_sized_new (64);
-      g_string_append (buf, "http://";);
+  child_settings = g_settings_get_child (proxy_settings, child_schema_id);
 
-      if (gconf_client_get_bool (conf, CONF_HTTP_PROXY_PREFIX "/use_authentication", NULL))
-	{
-	  char *user, *password;
-	  user = conf_get_string (conf, CONF_HTTP_PROXY_PREFIX "/authentication_user");
-	  if (user)
-	    {
-	      g_string_append_uri_escaped (buf, user, NULL, TRUE);
-	      password = conf_get_string (conf, CONF_HTTP_PROXY_PREFIX "/authentication_password");
-	      if (password)
-		{
-		  g_string_append_c (buf, ':');
-		  g_string_append_uri_escaped (buf, password, NULL, TRUE);
-		  g_free (password);
-		}
-	      g_free (user);
-	      g_string_append_c (buf, '@');
-	    }
-	}
-      g_string_append_printf (buf, "%s:%d/", host, port);
-      set_proxy_env (env_table, "http_proxy", g_string_free (buf, FALSE));
-    }
-  g_free (host);
+  if (is_http && !g_settings_get_boolean (child_settings, "enabled"))
+    goto out;
+
+  g_settings_get (child_settings, "host", "&s", &host);
+  port = g_settings_get_int (child_settings, "port");
+  if (host[0] == '\0' || port == 0)
+    goto out;
 
-  ignore = gconf_client_get_list (conf, CONF_HTTP_PROXY_PREFIX "/ignore_hosts", GCONF_VALUE_STRING, NULL);
-  if (ignore)
+  buf = g_string_sized_new (64);
+
+  g_string_append_printf (buf, "%s://", proxy_scheme);
+
+  if (is_http &&
+      g_settings_get_boolean (child_settings, "use-authentication"))
     {
-      GString *buf = g_string_sized_new (64);
-      while (ignore != NULL)
-	{
-	  GSList *old;
+      const char *user, *password;
 
-	  if (buf->len)
-	    g_string_append_c (buf, ',');
-	  g_string_append (buf, ignore->data);
+      g_settings_get (child_settings, "authentication-user", "&s", &user);
 
-	  old = ignore;
-	  ignore = g_slist_next (ignore);
-	  g_free (old->data);
-	  g_slist_free_1 (old);
-	}
-      set_proxy_env (env_table, "no_proxy", g_string_free (buf, FALSE));
-    }
-}
+      if (user[0])
+        {
+          g_string_append_uri_escaped (buf, user, NULL, TRUE);
 
-static void
-setup_https_proxy_env (GHashTable *env_table,
-                       GConfClient *conf)
-{
-  gchar *host;
-  gint port;
+          g_settings_get (child_settings, "authentication-password", "&s", &password);
 
-  host = conf_get_string (conf, CONF_PROXY_PREFIX "/secure_host");
-  port = gconf_client_get_int (conf, CONF_PROXY_PREFIX "/secure_port", NULL);
-  if (host && port)
-    {
-      char *proxy;
-      /* Even though it's https, the proxy scheme is 'http'. See bug #624440. */
-      proxy = g_strdup_printf ("http://%s:%d/";, host, port);
-      set_proxy_env (env_table, "https_proxy", proxy);
+          if (password[0])
+            {
+              g_string_append_c (buf, ':');
+              g_string_append_uri_escaped (buf, password, NULL, TRUE);
+            }
+          g_string_append_c (buf, '@');
+        }
     }
-  g_free (host);
-}
 
-static void
-setup_ftp_proxy_env (GHashTable *env_table,
-                     GConfClient *conf)
-{
-  gchar *host;
-  gint port;
+  g_string_append_printf (buf, "%s:%d/", host, port);
+  set_proxy_env (env_table, env_name, g_string_free (buf, FALSE));
 
-  host = conf_get_string (conf, CONF_PROXY_PREFIX "/ftp_host");
-  port = gconf_client_get_int (conf, CONF_PROXY_PREFIX "/ftp_port", NULL);
-  if (host && port)
-    {
-      char *proxy;
-      /* Even though it's ftp, the proxy scheme is 'http'. See bug #624440. */
-      proxy = g_strdup_printf ("http://%s:%d/";, host, port);
-      set_proxy_env (env_table, "ftp_proxy", proxy);
-    }
-  g_free (host);
+out:
+  g_object_unref (child_settings);
 }
 
 static void
-setup_socks_proxy_env (GHashTable *env_table,
-                       GConfClient *conf)
+setup_autoconfig_proxy_env (GSettings *proxy_settings,
+                            GHashTable *env_table)
 {
-  gchar *host;
-  gint port;
+  /* XXX  Not sure what to do with this.  See bug #596688.
+  const char *url;
 
-  host = conf_get_string (conf, CONF_PROXY_PREFIX "/socks_host");
-  port = gconf_client_get_int (conf, CONF_PROXY_PREFIX "/socks_port", NULL);
-  if (host && port)
+  g_settings_get (proxy_settings, "autoconfig-url", "&s", &url);
+  if (url[0])
     {
       char *proxy;
-      proxy = g_strdup_printf ("socks://%s:%d/", host, port);
-      set_proxy_env (env_table, "all_proxy", proxy);
+      proxy = g_strdup_printf ("pac+%s", url);
+      set_proxy_env (env_table, "http_proxy", proxy);
     }
-  g_free (host);
+  */
 }
 
 static void
-setup_autoconfig_proxy_env (GHashTable *env_table,
-                            GConfClient *conf)
+setup_ignore_proxy_env (GSettings *proxy_settings,
+                        GHashTable *env_table)
 {
-  /* XXX  Not sure what to do with this.  See bug #596688.
-  gchar *url;
+  GString *buf;
+  char **ignore;
+  int i;
+
+  g_settings_get (proxy_settings, "ignore-hosts", "^a&s", &ignore);
+  if (ignore == NULL)
+    return;
 
-  url = conf_get_string (conf, CONF_PROXY_PREFIX "/autoconfig_url");
-  if (url)
+  buf = g_string_sized_new (64);
+  for (i = 0; ignore[i] != NULL; ++i)
     {
-      char *proxy;
-      proxy = g_strdup_printf ("pac+%s", url);
-      set_proxy_env (env_table, "http_proxy", proxy);
+      if (buf->len)
+        g_string_append_c (buf, ',');
+      g_string_append (buf, ignore[i]);
     }
-  g_free (url);
-  */
+  g_free (ignore);
+
+  set_proxy_env (env_table, "no_proxy", g_string_free (buf, FALSE));
 }
 
 /**
@@ -721,27 +670,26 @@ setup_autoconfig_proxy_env (GHashTable *env_table,
 void
 terminal_util_add_proxy_env (GHashTable *env_table)
 {
-  char *proxymode;
+  GSettings *proxy_settings;
+  GDesktopProxyMode mode;
 
-  GConfClient *conf;
-  conf = gconf_client_get_default ();
+  proxy_settings = terminal_app_get_proxy_settings (terminal_app_get ());
+  mode = g_settings_get_enum (proxy_settings, "mode");
 
-  /* If mode is not manual, nothing to set */
-  proxymode = conf_get_string (conf, CONF_PROXY_PREFIX "/mode");
-  if (proxymode && 0 == strcmp (proxymode, "manual"))
+  if (mode == G_DESKTOP_PROXY_MODE_MANUAL)
     {
-      setup_http_proxy_env (env_table, conf);
-      setup_https_proxy_env (env_table, conf);
-      setup_ftp_proxy_env (env_table, conf);
-      setup_socks_proxy_env (env_table, conf);
+      setup_proxy_env (proxy_settings, "http", "http", "http_proxy", env_table);
+      /* Even though it's https, the proxy scheme is 'http'. See bug #624440. */
+      setup_proxy_env (proxy_settings, "https", "http", "https_proxy", env_table);
+      /* Even though it's ftp, the proxy scheme is 'http'. See bug #624440. */
+      setup_proxy_env (proxy_settings, "ftp", "http", "ftp_proxy", env_table);
+      setup_proxy_env (proxy_settings, "socks", "socks", "all_proxy", env_table);
+      setup_ignore_proxy_env (proxy_settings, env_table);
     }
-  else if (proxymode && 0 == strcmp (proxymode, "auto"))
+  else if (mode == G_DESKTOP_PROXY_MODE_AUTO)
     {
-      setup_autoconfig_proxy_env (env_table, conf);
+      setup_autoconfig_proxy_env (proxy_settings, env_table);
     }
-
-  g_free (proxymode);
-  g_object_unref (conf);
 }
 
 /* Bidirectional object/widget binding */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]