[gnome-software] Move the PackageKit proxy settings functionality to a plugin



commit 178ea6827e840891773ce04bc51cacddf3f85c31
Author: Richard Hughes <richard hughsie com>
Date:   Wed Jan 27 08:42:55 2016 +0000

    Move the PackageKit proxy settings functionality to a plugin
    
    This is 100% PackageKit specific and doesn't need to bloat the core app.

 src/Makefile.am                          |    2 -
 src/gs-application.c                     |    4 -
 src/gs-proxy-settings.c                  |  189 ------------------------------
 src/gs-proxy-settings.h                  |   39 ------
 src/plugins/Makefile.am                  |    6 +
 src/plugins/gs-plugin-packagekit-proxy.c |  189 ++++++++++++++++++++++++++++++
 6 files changed, 195 insertions(+), 234 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 5f46c47..a0c4c1f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -174,8 +174,6 @@ gnome_software_SOURCES =                            \
        gs-update-monitor.h                             \
        gs-vendor.c                                     \
        gs-vendor.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 6a9862f..8f14396 100644
--- a/src/gs-application.c
+++ b/src/gs-application.c
@@ -40,7 +40,6 @@
 #include "gs-first-run-dialog.h"
 #include "gs-shell.h"
 #include "gs-update-monitor.h"
-#include "gs-proxy-settings.h"
 #include "gs-shell-search-provider.h"
 #include "gs-offline-updates.h"
 #include "gs-folders.h"
@@ -57,7 +56,6 @@ struct _GsApplication {
        gint             pending_apps;
        GsShell         *shell;
        GsUpdateMonitor *update_monitor;
-       GsProxySettings *proxy_settings;
        GsDbusHelper    *dbus_helper;
        GsShellSearchProvider *search_provider;
        GNetworkMonitor *network_monitor;
@@ -593,7 +591,6 @@ gs_application_startup (GApplication *application)
                                         actions, G_N_ELEMENTS (actions),
                                         application);
 
-       GS_APPLICATION (application)->proxy_settings = gs_proxy_settings_new ();
        GS_APPLICATION (application)->dbus_helper = gs_dbus_helper_new ();
        GS_APPLICATION (application)->settings = g_settings_new ("org.gnome.software");
        gs_application_monitor_permission (GS_APPLICATION (application));
@@ -625,7 +622,6 @@ gs_application_dispose (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->network_monitor);
        g_clear_object (&app->dbus_helper);
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index ab62818..dee6142 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -47,6 +47,7 @@ plugin_LTLIBRARIES =                                  \
        libgs_plugin_packagekit-refresh.la              \
        libgs_plugin_packagekit-offline.la              \
        libgs_plugin_packagekit-origin.la               \
+       libgs_plugin_packagekit-proxy.la                \
        libgs_plugin_packagekit-history.la              \
        libgs_plugin_packagekit.la
 
@@ -211,6 +212,11 @@ libgs_plugin_packagekit_origin_la_LIBADD = $(GS_PLUGIN_LIBS)
 libgs_plugin_packagekit_origin_la_LDFLAGS = -module -avoid-version
 libgs_plugin_packagekit_origin_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(WARN_CFLAGS)
 
+libgs_plugin_packagekit_proxy_la_SOURCES = gs-plugin-packagekit-proxy.c
+libgs_plugin_packagekit_proxy_la_LIBADD = $(GS_PLUGIN_LIBS)
+libgs_plugin_packagekit_proxy_la_LDFLAGS = -module -avoid-version
+libgs_plugin_packagekit_proxy_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(WARN_CFLAGS)
+
 check_PROGRAMS =                                       \
        gs-self-test
 
diff --git a/src/plugins/gs-plugin-packagekit-proxy.c b/src/plugins/gs-plugin-packagekit-proxy.c
new file mode 100644
index 0000000..a3fcbd1
--- /dev/null
+++ b/src/plugins/gs-plugin-packagekit-proxy.c
@@ -0,0 +1,189 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2013-2016 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>
+
+#define I_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE
+#include <packagekit-glib2/packagekit.h>
+#include <string.h>
+#include <gsettings-desktop-schemas/gdesktop-enums.h>
+
+#include <gs-plugin.h>
+
+/*
+ * SECTION:
+ * Sets the session proxy on the system PackageKit instance
+ */
+
+struct GsPluginPrivate {
+       PkControl               *control;
+       GCancellable            *cancellable;
+       GSettings               *settings;
+       GSettings               *settings_http;
+       GSettings               *settings_ftp;
+};
+
+/**
+ * gs_plugin_get_name:
+ */
+const gchar *
+gs_plugin_get_name (void)
+{
+       return "packagekit-proxy";
+}
+
+static gchar *
+get_proxy_http (GsPlugin *plugin)
+{
+       gboolean ret;
+       GString *string = NULL;
+       guint port;
+       GDesktopProxyMode proxy_mode;
+       g_autofree gchar *host = NULL;
+       g_autofree gchar *password = NULL;
+       g_autofree gchar *username = NULL;
+
+       proxy_mode = g_settings_get_enum (plugin->priv->settings, "mode");
+       if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL)
+               return NULL;
+
+       host = g_settings_get_string (plugin->priv->settings_http, "host");
+       if (host == NULL)
+               return NULL;
+
+       port = g_settings_get_int (plugin->priv->settings_http, "port");
+
+       ret = g_settings_get_boolean (plugin->priv->settings_http,
+                                     "use-authentication");
+       if (ret) {
+               username = g_settings_get_string (plugin->priv->settings_http,
+                                                 "authentication-user");
+               password = g_settings_get_string (plugin->priv->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);
+       return g_string_free (string, FALSE);
+}
+
+static gchar *
+get_proxy_ftp (GsPlugin *plugin)
+{
+       GString *string = NULL;
+       guint port;
+       GDesktopProxyMode proxy_mode;
+       g_autofree gchar *host = NULL;
+
+       proxy_mode = g_settings_get_enum (plugin->priv->settings, "mode");
+       if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL)
+               return NULL;
+
+       host = g_settings_get_string (plugin->priv->settings_ftp, "host");
+       if (host == NULL)
+               return NULL;
+       port = g_settings_get_int (plugin->priv->settings_ftp, "port");
+       if (port == 0)
+               return NULL;
+
+       /* make PackageKit proxy string */
+       string = g_string_new (host);
+       if (port > 0)
+               g_string_append_printf (string, ":%i", port);
+       return g_string_free (string, FALSE);
+}
+
+static void
+set_proxy_cb (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+       g_autoptr(GError) error = NULL;
+       if (!pk_control_set_proxy_finish (PK_CONTROL (object), res, &error)) {
+               if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+                       g_warning ("failed to set proxies: %s", error->message);
+       }
+}
+
+static void
+reload_proxy_settings (GsPlugin *plugin)
+{
+       g_autofree gchar *proxy_http = NULL;
+       g_autofree gchar *proxy_ftp = NULL;
+
+       proxy_http = get_proxy_http (plugin);
+       proxy_ftp = get_proxy_ftp (plugin);
+
+       g_debug ("Setting proxies (http: %s, ftp: %s)", proxy_http, proxy_ftp);
+
+       pk_control_set_proxy_async (plugin->priv->control,
+                                   proxy_http,
+                                   proxy_ftp,
+                                   plugin->priv->cancellable,
+                                   set_proxy_cb,
+                                   plugin);
+}
+
+/**
+ * gs_plugin_initialize:
+ */
+void
+gs_plugin_initialize (GsPlugin *plugin)
+{
+       /* create private area */
+       plugin->priv = GS_PLUGIN_GET_PRIVATE (GsPluginPrivate);
+       plugin->priv->cancellable = g_cancellable_new ();
+       plugin->priv->control = pk_control_new ();
+       plugin->priv->settings = g_settings_new ("org.gnome.system.proxy");
+       g_signal_connect_swapped (plugin->priv->settings, "changed",
+                                 G_CALLBACK (reload_proxy_settings), plugin);
+       plugin->priv->settings_http = g_settings_new ("org.gnome.system.proxy.http");
+       g_signal_connect_swapped (plugin->priv->settings_http, "changed",
+                                 G_CALLBACK (reload_proxy_settings), plugin);
+       plugin->priv->settings_ftp = g_settings_new ("org.gnome.system.proxy.ftp");
+       g_signal_connect_swapped (plugin->priv->settings_ftp, "changed",
+                                 G_CALLBACK (reload_proxy_settings), plugin);
+       reload_proxy_settings (plugin);
+}
+
+/**
+ * gs_plugin_destroy:
+ */
+void
+gs_plugin_destroy (GsPlugin *plugin)
+{
+       if (plugin->priv->cancellable != NULL) {
+               g_cancellable_cancel (plugin->priv->cancellable);
+               g_object_unref (plugin->priv->cancellable);
+       }
+
+       g_object_unref (plugin->priv->control);
+       g_object_unref (plugin->priv->settings);
+       g_object_unref (plugin->priv->settings_http);
+       g_object_unref (plugin->priv->settings_ftp);
+}


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