[frogr] Support for using the GNOME general proxy settings (#644841)



commit 5772e42c6c6963bf779fb18c968555c94938b6da
Author: Mario Sanchez Prada <msanchez igalia com>
Date:   Fri May 27 18:47:00 2011 +0200

    Support for using the GNOME general proxy settings (#644841)
    
    Now a new checkbox is present in the settings dialog, which allows
    avoiding the need to manually set the proxy configuration by providing
    a way to use the GNOME general proxy settings.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=644841

 src/flicksoup/fsp-session.c |  110 ++++++++++++++++++++++++++-----------------
 src/flicksoup/fsp-session.h |    1 +
 src/frogr-config.c          |   31 ++++++++++++
 src/frogr-config.h          |    4 ++
 src/frogr-controller.c      |   13 ++++--
 src/frogr-controller.h      |    1 +
 src/frogr-settings-dialog.c |   78 +++++++++++++++++++++++++-----
 7 files changed, 178 insertions(+), 60 deletions(-)
---
diff --git a/src/flicksoup/fsp-session.c b/src/flicksoup/fsp-session.c
index b568b76..44c82e2 100644
--- a/src/flicksoup/fsp-session.c
+++ b/src/flicksoup/fsp-session.c
@@ -66,6 +66,7 @@ struct _FspSessionPrivate
   gchar *token;
   gchar *frob;
 
+  gboolean using_gnome_proxy;
   SoupURI *proxy_uri;
   SoupSession *soup_session;
 };
@@ -403,6 +404,7 @@ fsp_session_init                        (FspSession *self)
   self->priv->token = NULL;
   self->priv->frob = NULL;
 
+  self->priv->using_gnome_proxy = FALSE;
   self->priv->proxy_uri = NULL;
 
   self->priv->soup_session = soup_session_async_new ();
@@ -1269,65 +1271,87 @@ fsp_session_new                         (const gchar *api_key,
 
 gboolean
 fsp_session_set_http_proxy              (FspSession *self,
+                                         gboolean use_gnome_proxy,
                                          const char *host, const char *port,
                                          const char *username, const char *password)
 {
   SoupURI *proxy_uri = NULL;
+  gboolean using_gnome_proxy_before = FALSE;
 
   g_return_val_if_fail (FSP_IS_SESSION (self), FALSE);
 
-  if (host != NULL)
-    {
-      const gchar *actual_user = NULL;
-      const gchar *actual_password = NULL;
-      guint actual_port = 0;
-
-      /* Ensure no garbage is used for username */
-      if (username == NULL || *username == '\0')
-        actual_user = NULL;
-      else
-        actual_user = username;
+  /* We're gonna need this to make a good decision later */
+  using_gnome_proxy_before = self->priv->using_gnome_proxy;
 
-      /* Ensure no garbage is used for password */
-      if (password == NULL || *password == '\0')
-        actual_password = NULL;
-      else
-        actual_password = password;
-
-      /* We need a numeric port */
-      actual_port = (guint) g_ascii_strtoll ((gchar *) port, NULL, 10);
-
-      /* Build the actual SoupURI object */
-      proxy_uri = soup_uri_new (NULL);
-      soup_uri_set_scheme (proxy_uri, SOUP_URI_SCHEME_HTTP);
-      soup_uri_set_host (proxy_uri, host);
-      soup_uri_set_port (proxy_uri, actual_port);
-      soup_uri_set_user (proxy_uri, actual_user);
-      soup_uri_set_password (proxy_uri, actual_password);
+  if (use_gnome_proxy)
+    {
+      /* If GNOME proxy is gonna be used, set up the feature. */
+      soup_session_add_feature_by_type (self->priv->soup_session,
+                                        SOUP_TYPE_PROXY_RESOLVER_GNOME);
     }
+  else
+    {
+      /* Remove the feature if not using it anymore */
+      soup_session_remove_feature_by_type (self->priv->soup_session,
+                                           SOUP_TYPE_PROXY_RESOLVER_GNOME);
+    }
+  self->priv->using_gnome_proxy = use_gnome_proxy;
 
-  /* Set/unset the proxy if needed */
-  if ((!self->priv->proxy_uri && proxy_uri)
-      || (self->priv->proxy_uri && !proxy_uri)
-      || (self->priv->proxy_uri && proxy_uri && !soup_uri_equal (self->priv->proxy_uri, proxy_uri)))
+  if (!use_gnome_proxy)
     {
-      g_object_set (G_OBJECT (self->priv->soup_session),
-                    SOUP_SESSION_PROXY_URI,
-                    proxy_uri,
-                    NULL);
+      /* If using the GNOME proxy we just forget about the rest. */
+      if (host != NULL)
+        {
+          const gchar *actual_user = NULL;
+          const gchar *actual_password = NULL;
+          guint actual_port = 0;
 
-      /* Save internal reference to the proxy */
-      if (self->priv->proxy_uri)
-        soup_uri_free (self->priv->proxy_uri);
+          /* Ensure no garbage is used for username */
+          if (username == NULL || *username == '\0')
+            actual_user = NULL;
+          else
+            actual_user = username;
 
-      self->priv->proxy_uri = proxy_uri;
+          /* Ensure no garbage is used for password */
+          if (password == NULL || *password == '\0')
+            actual_password = NULL;
+          else
+            actual_password = password;
+
+          /* We need a numeric port */
+          actual_port = (guint) g_ascii_strtoll ((gchar *) port, NULL, 10);
+
+          /* Build the actual SoupURI object */
+          proxy_uri = soup_uri_new (NULL);
+          soup_uri_set_scheme (proxy_uri, SOUP_URI_SCHEME_HTTP);
+          soup_uri_set_host (proxy_uri, host);
+          soup_uri_set_port (proxy_uri, actual_port);
+          soup_uri_set_user (proxy_uri, actual_user);
+          soup_uri_set_password (proxy_uri, actual_password);
+        }
+
+      /* Set/unset the proxy if needed */
+      if ((!self->priv->proxy_uri && proxy_uri)
+          || (self->priv->proxy_uri && !proxy_uri)
+          || (self->priv->proxy_uri && proxy_uri && !soup_uri_equal (self->priv->proxy_uri, proxy_uri)))
+        {
+          g_object_set (G_OBJECT (self->priv->soup_session),
+                        SOUP_SESSION_PROXY_URI,
+                        proxy_uri,
+                        NULL);
 
-      /* Proxy configuration actually changed */
-      return TRUE;
+          /* Save internal reference to the proxy */
+          if (self->priv->proxy_uri)
+            soup_uri_free (self->priv->proxy_uri);
+
+          self->priv->proxy_uri = proxy_uri;
+
+          /* Proxy configuration actually changed */
+          return TRUE;
+        }
     }
 
-  /* Proxy configuration has not changed */
-  return FALSE;
+  return using_gnome_proxy_before != use_gnome_proxy;
 }
 
 const gchar *
diff --git a/src/flicksoup/fsp-session.h b/src/flicksoup/fsp-session.h
index 8fcfee6..7023edc 100644
--- a/src/flicksoup/fsp-session.h
+++ b/src/flicksoup/fsp-session.h
@@ -69,6 +69,7 @@ fsp_session_new                         (const gchar *api_key,
                                          const gchar *token);
 gboolean
 fsp_session_set_http_proxy              (FspSession *self,
+                                         gboolean use_gnome_proxy,
                                          const char *host, const char *port,
                                          const char *username, const char *password);
 
diff --git a/src/frogr-config.c b/src/frogr-config.c
index 4cb0d7d..d6acc4f 100644
--- a/src/frogr-config.c
+++ b/src/frogr-config.c
@@ -65,6 +65,7 @@ struct _FrogrConfigPrivate
   gboolean mainview_enable_tooltips;
 
   gboolean use_proxy;
+  gboolean use_gnome_proxy;
   gchar *proxy_host;
   gchar *proxy_port;
   gchar *proxy_username;
@@ -442,6 +443,12 @@ _load_proxy_data_xml (FrogrConfig *self,
             g_strstrip (priv->proxy_password);
         }
 
+      if (!xmlStrcmp (node->name, (const xmlChar*) "use-gnome-proxy"))
+        {
+          content = xmlNodeGetContent (node);
+          priv->use_gnome_proxy = !xmlStrcmp (content, (const xmlChar*) "1");
+        }
+
       if (content)
         xmlFree (content);
     }
@@ -630,6 +637,7 @@ _save_settings (FrogrConfig *self)
   _xml_add_string_child (node, "proxy-port", priv->proxy_port);
   _xml_add_string_child (node, "proxy-username", priv->proxy_username);
   _xml_add_string_child (node, "proxy-password", priv->proxy_password);
+  _xml_add_bool_child (node, "use-gnome-proxy", priv->use_gnome_proxy);
   xmlAddChild (root, node);
 
   xml_path = g_build_filename (g_get_user_config_dir (),
@@ -877,6 +885,7 @@ frogr_config_init (FrogrConfig *self)
   priv->mainview_sorting_reversed = FALSE;
   priv->mainview_enable_tooltips = TRUE;
   priv->use_proxy = FALSE;
+  priv->use_gnome_proxy = FALSE;
   priv->proxy_host = NULL;
   priv->proxy_port = NULL;
   priv->proxy_username = NULL;
@@ -1305,6 +1314,28 @@ frogr_config_get_use_proxy (FrogrConfig *self)
 }
 
 void
+frogr_config_set_use_gnome_proxy (FrogrConfig *self, gboolean value)
+{
+  FrogrConfigPrivate * priv = NULL;
+
+  g_return_if_fail (FROGR_IS_CONFIG (self));
+
+  priv = FROGR_CONFIG_GET_PRIVATE (self);
+  priv->use_gnome_proxy = value;
+}
+
+gboolean
+frogr_config_get_use_gnome_proxy (FrogrConfig *self)
+{
+  FrogrConfigPrivate *priv = NULL;
+
+  g_return_val_if_fail (FROGR_IS_CONFIG (self), FALSE);
+
+  priv = FROGR_CONFIG_GET_PRIVATE (self);
+  return priv->use_gnome_proxy;
+}
+
+void
 frogr_config_set_proxy_host (FrogrConfig *self, const gchar *host)
 {
   FrogrConfigPrivate * priv = NULL;
diff --git a/src/frogr-config.h b/src/frogr-config.h
index fa89b7e..e133cb7 100644
--- a/src/frogr-config.h
+++ b/src/frogr-config.h
@@ -133,6 +133,10 @@ void frogr_config_set_use_proxy (FrogrConfig *self, gboolean value);
 
 gboolean frogr_config_get_use_proxy (FrogrConfig *self);
 
+void frogr_config_set_use_gnome_proxy (FrogrConfig *self, gboolean value);
+
+gboolean frogr_config_get_use_gnome_proxy (FrogrConfig *self);
+
 void frogr_config_set_proxy_host (FrogrConfig *self, const gchar *host);
 
 const gchar *frogr_config_get_proxy_host (FrogrConfig *self);
diff --git a/src/frogr-controller.c b/src/frogr-controller.c
index 50563f5..7203f6e 100644
--- a/src/frogr-controller.c
+++ b/src/frogr-controller.c
@@ -1802,11 +1802,13 @@ frogr_controller_init (FrogrController *self)
   /* Set HTTP proxy if needed */
   if (frogr_config_get_use_proxy (priv->config))
     {
+      const gboolean use_gnome_proxy = frogr_config_get_use_gnome_proxy (priv->config);
       const gchar *host = frogr_config_get_proxy_host (priv->config);
       const gchar *port = frogr_config_get_proxy_port (priv->config);
       const gchar *username = frogr_config_get_proxy_username (priv->config);
       const gchar *password = frogr_config_get_proxy_password (priv->config);
-      frogr_controller_set_proxy (self, host, port, username, password);
+      frogr_controller_set_proxy (self, use_gnome_proxy,
+                                  host, port, username, password);
     }
 }
 
@@ -1992,6 +1994,7 @@ frogr_controller_get_state (FrogrController *self)
 
 void
 frogr_controller_set_proxy (FrogrController *self,
+                            gboolean use_gnome_proxy,
                             const char *host, const char *port,
                             const char *username, const char *password)
 {
@@ -2002,9 +2005,10 @@ frogr_controller_set_proxy (FrogrController *self,
   priv = FROGR_CONTROLLER_GET_PRIVATE (self);
 
   /* The host is mandatory to set up a proxy */
-  if (host == NULL || *host == '\0') {
-    fsp_session_set_http_proxy (priv->session, NULL, NULL, NULL, NULL);
-    DEBUG ("%s", "Not using HTTP proxy");
+  if (!use_gnome_proxy && (host == NULL || *host == '\0')) {
+    fsp_session_set_http_proxy (priv->session, FALSE,
+                                NULL, NULL, NULL, NULL);
+    DEBUG ("%s", "Not enabling the HTTP proxy");
   } else {
     gboolean has_username = FALSE;
     gboolean has_password = FALSE;
@@ -2021,6 +2025,7 @@ frogr_controller_set_proxy (FrogrController *self,
     g_free (auth_part);
 
     proxy_changed = fsp_session_set_http_proxy (priv->session,
+                                                use_gnome_proxy,
                                                 host, port,
                                                 username, password);
 
diff --git a/src/frogr-controller.h b/src/frogr-controller.h
index fa7950a..e75cedd 100644
--- a/src/frogr-controller.h
+++ b/src/frogr-controller.h
@@ -81,6 +81,7 @@ GSList *frogr_controller_get_all_accounts (FrogrController *self);
 FrogrControllerState frogr_controller_get_state (FrogrController *self);
 
 void frogr_controller_set_proxy (FrogrController *self,
+                                 gboolean use_gnome_proxy,
                                  const char *host, const char *port,
                                  const char *username, const char *password);
 
diff --git a/src/frogr-settings-dialog.c b/src/frogr-settings-dialog.c
index 434e668..e5e3797 100644
--- a/src/frogr-settings-dialog.c
+++ b/src/frogr-settings-dialog.c
@@ -57,6 +57,7 @@ typedef struct _FrogrSettingsDialogPrivate {
   GtkWidget *keep_file_extensions_cb;
 
   GtkWidget *use_proxy_cb;
+  GtkWidget *use_gnome_proxy_cb;
   GtkWidget *proxy_host_label;
   GtkWidget *proxy_host_entry;
   GtkWidget *proxy_port_label;
@@ -76,6 +77,7 @@ typedef struct _FrogrSettingsDialogPrivate {
   FspContentType content_type;
 
   gboolean use_proxy;
+  gboolean use_gnome_proxy;
   gchar *proxy_host;
   gchar *proxy_port;
   gchar *proxy_username;
@@ -275,13 +277,27 @@ _add_connection_page (FrogrSettingsDialog *self, GtkNotebook *notebook)
   GtkWidget *cbutton = NULL;
   GtkWidget *label = NULL;
   GtkWidget *entry = NULL;
+  gchar *markup = NULL;
 
   priv = FROGR_SETTINGS_DIALOG_GET_PRIVATE (self);
   vbox = gtk_vbox_new (FALSE, 6);
 
   /* Proxy settings */
 
-  cbutton = gtk_check_button_new_with_mnemonic (_("_Use HTTP Proxy"));
+  label = gtk_label_new (NULL);
+  gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
+  markup = g_markup_printf_escaped ("<span weight=\"bold\">%s</span>",
+                                    _("Proxy Settings"));
+  gtk_label_set_markup (GTK_LABEL (label), markup);
+  g_free (markup);
+
+  align = gtk_alignment_new (0, 0, 0, 1);
+  gtk_container_add (GTK_CONTAINER (align), label);
+  gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 6);
+
+  /* Enable proxy */
+
+  cbutton = gtk_check_button_new_with_mnemonic (_("_Enable HTTP Proxy"));
   align = gtk_alignment_new (0, 0, 0, 0);
   gtk_container_add (GTK_CONTAINER (align), cbutton);
   gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
@@ -290,7 +306,6 @@ _add_connection_page (FrogrSettingsDialog *self, GtkNotebook *notebook)
   /* Proxy host */
 
   table = gtk_table_new (2, 4, FALSE);
-  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);
 
   label = gtk_label_new_with_mnemonic (_("_Host:"));
   align = gtk_alignment_new (0, 0, 0, 0);
@@ -363,11 +378,25 @@ _add_connection_page (FrogrSettingsDialog *self, GtkNotebook *notebook)
                     GTK_EXPAND | GTK_FILL, 0, 6, 6);
   priv->proxy_password_entry = entry;
 
+  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
+
+  /* Use GNOME General Proxy Settings */
+
+  cbutton = gtk_check_button_new_with_mnemonic (_("_Use GNOME General Proxy Settings"));
+  align = gtk_alignment_new (0, 0, 0, 0);
+  gtk_container_add (GTK_CONTAINER (align), cbutton);
+  gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
+  priv->use_gnome_proxy_cb = cbutton;
+
   /* Connect signals */
   g_signal_connect (G_OBJECT (priv->use_proxy_cb), "toggled",
                     G_CALLBACK (_on_button_toggled),
                     self);
 
+  g_signal_connect (G_OBJECT (priv->use_gnome_proxy_cb), "toggled",
+                    G_CALLBACK (_on_button_toggled),
+                    self);
+
   g_signal_connect (G_OBJECT (priv->proxy_port_entry), "insert-text",
                     G_CALLBACK (_proxy_port_inserted_cb),
                     NULL);
@@ -392,6 +421,7 @@ _fill_dialog_with_data (FrogrSettingsDialog *self)
   priv->disable_tags_autocompletion = !frogr_config_get_tags_autocompletion (priv->config);
   priv->keep_file_extensions = frogr_config_get_keep_file_extensions (priv->config);
   priv->use_proxy = frogr_config_get_use_proxy (priv->config);
+  priv->use_gnome_proxy = frogr_config_get_use_gnome_proxy (priv->config);
 
   g_free (priv->proxy_host);
   priv->proxy_host = g_strdup (frogr_config_get_proxy_host (priv->config));
@@ -448,6 +478,10 @@ _fill_dialog_with_data (FrogrSettingsDialog *self)
 
   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->use_proxy_cb),
                                 priv->use_proxy);
+
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->use_gnome_proxy_cb),
+                                priv->use_gnome_proxy);
+
   if (priv->proxy_host)
     gtk_entry_set_text (GTK_ENTRY (priv->proxy_host_entry), priv->proxy_host);
 
@@ -483,6 +517,7 @@ _save_data (FrogrSettingsDialog *self)
   frogr_config_set_keep_file_extensions (priv->config, priv->keep_file_extensions);
 
   frogr_config_set_use_proxy (priv->config, priv->use_proxy);
+  frogr_config_set_use_gnome_proxy (priv->config, priv->use_gnome_proxy);
 
   g_free (priv->proxy_host);
   priv->proxy_host = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->proxy_host_entry)));
@@ -519,20 +554,28 @@ static void
 _update_ui (FrogrSettingsDialog *self)
 {
   FrogrSettingsDialogPrivate *priv = NULL;
+  gboolean using_gnome_proxy = FALSE;
 
   priv = FROGR_SETTINGS_DIALOG_GET_PRIVATE (self);
 
-  /* Sensitiveness */
+  /* Sensititveness of default visibility related widgets */
+
   gtk_widget_set_sensitive (priv->friend_cb, !priv->public_visibility);
   gtk_widget_set_sensitive (priv->family_cb, !priv->public_visibility);
-  gtk_widget_set_sensitive (priv->proxy_host_label, priv->use_proxy);
-  gtk_widget_set_sensitive (priv->proxy_host_entry, priv->use_proxy);
-  gtk_widget_set_sensitive (priv->proxy_port_label, priv->use_proxy);
-  gtk_widget_set_sensitive (priv->proxy_port_entry, priv->use_proxy);
-  gtk_widget_set_sensitive (priv->proxy_username_label, priv->use_proxy);
-  gtk_widget_set_sensitive (priv->proxy_username_entry, priv->use_proxy);
-  gtk_widget_set_sensitive (priv->proxy_password_label, priv->use_proxy);
-  gtk_widget_set_sensitive (priv->proxy_password_entry, priv->use_proxy);
+
+  /* Sensititveness of proxy settings related widgets */
+
+  using_gnome_proxy = priv->use_proxy && !priv->use_gnome_proxy;
+
+  gtk_widget_set_sensitive (priv->use_gnome_proxy_cb, priv->use_proxy);
+  gtk_widget_set_sensitive (priv->proxy_host_label, using_gnome_proxy);
+  gtk_widget_set_sensitive (priv->proxy_host_entry, using_gnome_proxy);
+  gtk_widget_set_sensitive (priv->proxy_port_label, using_gnome_proxy);
+  gtk_widget_set_sensitive (priv->proxy_port_entry, using_gnome_proxy);
+  gtk_widget_set_sensitive (priv->proxy_username_label, using_gnome_proxy);
+  gtk_widget_set_sensitive (priv->proxy_username_entry, using_gnome_proxy);
+  gtk_widget_set_sensitive (priv->proxy_password_label, using_gnome_proxy);
+  gtk_widget_set_sensitive (priv->proxy_password_entry, using_gnome_proxy);
 }
 
 static void
@@ -621,7 +664,13 @@ _on_button_toggled (GtkToggleButton *button, gpointer data)
   if (GTK_WIDGET (button) == priv->use_proxy_cb)
     {
       priv->use_proxy = active;
-      DEBUG ("Use HTTP proxy: %s", active ? "YES" : "NO");
+      DEBUG ("Enable HTTP Proxy: %s", active ? "YES" : "NO");
+    }
+
+  if (GTK_WIDGET (button) == priv->use_gnome_proxy_cb)
+    {
+      priv->use_gnome_proxy = active;
+      DEBUG ("Use GNOME General Proxy Settings: %s", active ? "YES" : "NO");
     }
 
   _update_ui (self);
@@ -671,10 +720,11 @@ static void _dialog_response_cb (GtkDialog *dialog, gint response, gpointer data
   /* Update proxy status */
   if (priv->use_proxy)
     frogr_controller_set_proxy (priv->controller,
+                                priv->use_gnome_proxy,
                                 priv->proxy_host, priv->proxy_port,
                                 priv->proxy_username, priv->proxy_password);
   else
-    frogr_controller_set_proxy (priv->controller, NULL, NULL, NULL, NULL);
+    frogr_controller_set_proxy (priv->controller, FALSE, NULL, NULL, NULL, NULL);
 
   gtk_widget_hide (GTK_WIDGET (self));
 }
@@ -747,6 +797,7 @@ frogr_settings_dialog_init (FrogrSettingsDialog *self)
   priv->disable_tags_autocompletion_cb = NULL;
   priv->keep_file_extensions_cb = NULL;
   priv->use_proxy_cb = NULL;
+  priv->use_gnome_proxy_cb = NULL;
   priv->proxy_host_label = NULL;
   priv->proxy_host_entry = NULL;
   priv->proxy_port_label = NULL;
@@ -764,6 +815,7 @@ frogr_settings_dialog_init (FrogrSettingsDialog *self)
   priv->disable_tags_autocompletion = FALSE;
   priv->keep_file_extensions = FALSE;
   priv->use_proxy = FALSE;
+  priv->use_gnome_proxy = FALSE;
   priv->proxy_host = NULL;
   priv->proxy_port = NULL;
   priv->proxy_username = NULL;



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