[gnome-software] Use proxy settings
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software] Use proxy settings
- Date: Sat, 2 Nov 2013 04:22:46 +0000 (UTC)
commit 75e1e9066d90e09274197b0d582adc31be1b94cd
Author: Matthias Clasen <mclasen redhat com>
Date: Sat Nov 2 00:21:31 2013 -0400
Use proxy settings
This moves more functionality from the gsd updates plugin.
We pass the session proxy settings to PackageKit, so it can
use the right network for downloading.
src/Makefile.am | 2 +
src/gs-application.c | 4 +
src/gs-proxy-settings.c | 206 +++++++++++++++++++++++++++++++++++++++++++++++
src/gs-proxy-settings.h | 46 +++++++++++
4 files changed, 258 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 7b2b8df..e41459f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -134,6 +134,8 @@ gnome_software_service_SOURCES = \
gs-shell-search.h \
gs-update-monitor.c \
gs-update-monitor.h \
+ gs-proxy-settings.c \
+ gs-proxy-settings.h \
gs-offline-updates.c \
gs-offline-updates.h \
gs-plugin-loader.c \
diff --git a/src/gs-application.c b/src/gs-application.c
index e1e9300..53e3026 100644
--- a/src/gs-application.c
+++ b/src/gs-application.c
@@ -32,6 +32,7 @@
#include "gs-box.h"
#include "gs-shell.h"
#include "gs-update-monitor.h"
+#include "gs-proxy-settings.h"
#include "gs-plugin-loader.h"
#include "gs-profile.h"
#include "gs-shell-search-provider.h"
@@ -48,6 +49,7 @@ struct _GsApplication {
gint pending_apps;
GsShell *shell;
GsUpdateMonitor *update_monitor;
+ GsProxySettings *proxy_settings;
GsShellSearchProvider *search_provider;
GNetworkMonitor *network_monitor;
};
@@ -406,6 +408,7 @@ gs_application_startup (GApplication *application)
actions, G_N_ELEMENTS (actions),
application);
+ GS_APPLICATION (application)->proxy_settings = gs_proxy_settings_new ();
gs_application_monitor_updates (GS_APPLICATION (application));
gs_application_provide_search (GS_APPLICATION (application));
gs_application_monitor_network (GS_APPLICATION (application));
@@ -429,6 +432,7 @@ gs_application_finalize (GObject *object)
g_clear_object (&app->shell);
g_clear_object (&app->provider);
g_clear_object (&app->update_monitor);
+ g_clear_object (&app->proxy_settings);
g_clear_object (&app->profile);
g_clear_object (&app->search_provider);
g_clear_object (&app->network_monitor);
diff --git a/src/gs-proxy-settings.c b/src/gs-proxy-settings.c
new file mode 100644
index 0000000..abfb072
--- /dev/null
+++ b/src/gs-proxy-settings.c
@@ -0,0 +1,206 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2013 Matthias Clasen <mclasen redhat com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <packagekit-glib2/packagekit.h>
+#include <gsettings-desktop-schemas/gdesktop-enums.h>
+
+#include "gs-proxy-settings.h"
+
+struct _GsProxySettings {
+ GObject parent;
+
+ PkControl *control;
+ GSettings *settings;
+ GSettings *settings_http;
+ GSettings *settings_ftp;
+};
+
+struct _GsProxySettingsClass {
+ GObjectClass parent_class;
+};
+
+G_DEFINE_TYPE (GsProxySettings, gs_proxy_settings, G_TYPE_OBJECT)
+
+static gchar *
+get_proxy_http (GsProxySettings *proxy_settings)
+{
+ gboolean ret;
+ gchar *host = NULL;
+ gchar *password = NULL;
+ gchar *proxy = NULL;
+ gchar *username = NULL;
+ GString *string = NULL;
+ guint port;
+ GDesktopProxyMode proxy_mode;
+
+ proxy_mode = g_settings_get_enum (proxy_settings->settings, "mode");
+ if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL)
+ goto out;
+
+ host = g_settings_get_string (proxy_settings->settings_http, "host");
+ if (host == NULL)
+ goto out;
+
+ port = g_settings_get_int (proxy_settings->settings_http, "port");
+
+ ret = g_settings_get_boolean (proxy_settings->settings_http,
+ "use-authentication");
+ if (ret) {
+ username = g_settings_get_string (proxy_settings->settings_http,
+ "authentication-user");
+ password = g_settings_get_string (proxy_settings->settings_http,
+ "authentication-password");
+ }
+
+ /* make PackageKit proxy string */
+ string = g_string_new (host);
+ if (port > 0)
+ g_string_append_printf (string, ":%i", port);
+ if (username != NULL && password != NULL)
+ g_string_append_printf (string, "@%s:%s", username, password);
+ else if (username != NULL)
+ g_string_append_printf (string, "@%s", username);
+ else if (password != NULL)
+ g_string_append_printf (string, "@:%s", password);
+ proxy = g_string_free (string, FALSE);
+
+out:
+ g_free (host);
+ g_free (username);
+ g_free (password);
+
+ return proxy;
+}
+
+static gchar *
+get_proxy_ftp (GsProxySettings *proxy_settings)
+{
+ gchar *host = NULL;
+ gchar *proxy = NULL;
+ GString *string = NULL;
+ guint port;
+ GDesktopProxyMode proxy_mode;
+
+ proxy_mode = g_settings_get_enum (proxy_settings->settings, "mode");
+ if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL)
+ goto out;
+
+ host = g_settings_get_string (proxy_settings->settings_ftp, "host");
+ if (host == NULL)
+ goto out;
+ port = g_settings_get_int (proxy_settings->settings_ftp, "port");
+ if (port == 0)
+ goto out;
+
+ /* make PackageKit proxy string */
+ string = g_string_new (host);
+ if (port > 0)
+ g_string_append_printf (string, ":%i", port);
+ proxy = g_string_free (string, FALSE);
+
+out:
+ g_free (host);
+
+ return proxy;
+}
+
+static void
+set_proxy_cb (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ gboolean ret;
+ GError *error = NULL;
+
+ ret = pk_control_set_proxy_finish (PK_CONTROL (object), res, &error);
+ if (!ret) {
+ if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ g_warning ("failed to set proxies: %s", error->message);
+ g_error_free (error);
+ }
+}
+
+static void
+reload_proxy_settings (GsProxySettings *proxy_settings)
+{
+ gchar *proxy_http;
+ gchar *proxy_ftp;
+
+ proxy_http = get_proxy_http (proxy_settings);
+ proxy_ftp = get_proxy_ftp (proxy_settings);
+
+ g_debug ("Setting proxies (http: %s, ftp: %s)", proxy_http, proxy_ftp);
+
+ pk_control_set_proxy_async (proxy_settings->control,
+ proxy_http,
+ proxy_ftp,
+ NULL,
+ set_proxy_cb,
+ proxy_settings);
+ g_free (proxy_http);
+ g_free (proxy_ftp);
+}
+
+static void
+gs_proxy_settings_init (GsProxySettings *proxy_settings)
+{
+ proxy_settings->control = pk_control_new ();
+ proxy_settings->settings = g_settings_new ("org.gnome.system.proxy");
+ g_signal_connect_swapped (proxy_settings->settings, "changed",
+ G_CALLBACK (reload_proxy_settings), proxy_settings);
+ proxy_settings->settings_http = g_settings_new ("org.gnome.system.proxy.http");
+ g_signal_connect_swapped (proxy_settings->settings_http, "changed",
+ G_CALLBACK (reload_proxy_settings), proxy_settings);
+ proxy_settings->settings_ftp = g_settings_new ("org.gnome.system.proxy.ftp");
+ g_signal_connect_swapped (proxy_settings->settings_ftp, "changed",
+ G_CALLBACK (reload_proxy_settings), proxy_settings);
+ reload_proxy_settings (proxy_settings);
+}
+
+static void
+gs_proxy_settings_finalize (GObject *object)
+{
+ GsProxySettings *proxy_settings = GS_PROXY_SETTINGS (object);
+
+ g_clear_object (&proxy_settings->control);
+ g_clear_object (&proxy_settings->settings);
+ g_clear_object (&proxy_settings->settings_http);
+ g_clear_object (&proxy_settings->settings_ftp);
+
+ G_OBJECT_CLASS (gs_proxy_settings_parent_class)->finalize (object);
+}
+
+static void
+gs_proxy_settings_class_init (GsProxySettingsClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ object_class->finalize = gs_proxy_settings_finalize;
+}
+
+GsProxySettings *
+gs_proxy_settings_new (void)
+{
+ return GS_PROXY_SETTINGS (g_object_new (GS_TYPE_PROXY_SETTINGS, NULL));
+}
+
+/* vim: set noexpandtab: */
diff --git a/src/gs-proxy-settings.h b/src/gs-proxy-settings.h
new file mode 100644
index 0000000..e258252
--- /dev/null
+++ b/src/gs-proxy-settings.h
@@ -0,0 +1,46 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __GS_PROXY_SETTINGS_H
+#define __GS_PROXY_SETTINGS_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_PROXY_SETTINGS (gs_proxy_settings_get_type ())
+#define GS_PROXY_SETTINGS(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_PROXY_SETTINGS,
GsProxySettings))
+#define GS_PROXY_SETTINGS_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_PROXY_SETTINGS,
GsProxySettingsClass))
+#define GS_IS_PROXY_SETTINGS(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_PROXY_SETTINGS))
+#define GS_IS_PROXY_SETTINGS_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_PROXY_SETTINGS))
+#define GS_PROXY_SETTINGS_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_PROXY_SETTINGS,
GsProxySettingsClass))
+
+typedef struct _GsProxySettings GsProxySettings;
+typedef struct _GsProxySettingsClass GsProxySettingsClass;
+
+GType gs_proxy_settings_get_type (void);
+GsProxySettings *gs_proxy_settings_new (void);
+
+G_END_DECLS
+
+#endif /* __GS_PROXY_SETTINGS_H */
+
+/* vim: set noexpandtab: */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]