[network-manager-applet/dcbw/more-kill-dbus-glib-bgo649318: 2/3] applet: remove usage of dbus-glib and private session D-Bus API



commit a7e94fdde19b1cff01d897734e75b4b648df2a1f
Author: Dan Williams <dcbw redhat com>
Date:   Thu Feb 18 14:17:14 2016 -0600

    applet: remove usage of dbus-glib and private session D-Bus API
    
    Since 0.8.9xx-something the applet has exported a private D-Bus API
    for GNOME Shell (bgo #642503).  GNOME Shell stopped using that API
    back in 2011 (bgo #650244, d896248ff8e6b9858932ca54ecfdfc2b30fbd5ae)
    so we can definitely remove the private API.
    
    That means we no longer need dbus-glib, but the D-Bus API also
    guaranteed that only one instance of nm-applet ran at any given time.
    To keep that behavior, port the applet to GApplication (but not
    to GtkApplication since that assumes things like the Application
    menu, a main window, and other stuff the applet doesn't have).

 src/Makefile.am                 |   13 +--
 src/applet-vpn-request.c        |    2 -
 src/applet.c                    |  219 +++++----------------------------------
 src/applet.h                    |   13 +--
 src/main.c                      |   23 ++---
 src/nm-applet-introspection.xml |   20 ----
 6 files changed, 38 insertions(+), 252 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 6578f01..258558e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -21,17 +21,11 @@ nm_applet_CPPFLAGS = \
        -DVPN_NAME_FILES_DIR=\""$(sysconfdir)/NetworkManager/VPN"\"     \
        -DNMALOCALEDIR=\"$(datadir)/locale\"                                    \
        -DG_LOG_DOMAIN=\""nm-applet"\" \
-       $(DBUS_GLIB_CFLAGS) \
        "-I${top_srcdir}/shared/" \
        -I${top_srcdir}/src/utils \
        -I${top_srcdir}/src/wireless-security \
        -I${top_srcdir}/src/libnma
 
-BUILT_SOURCES = applet-dbus-bindings.h
-
-applet-dbus-bindings.h: nm-applet-introspection.xml
-       $(AM_V_GEN) dbus-binding-tool --mode=glib-server --prefix=nma --output=$@ $<
-
 nm_applet_SOURCES = \
        main.c \
        applet.c \
@@ -67,7 +61,6 @@ endif
 nm_applet_LDADD = \
        -lm \
        $(GTK_LIBS) \
-       $(DBUS_GLIB_LIBS) \
        $(LIBNM_LIBS) \
        $(LIBSECRET_LIBS) \
        $(NOTIFY_LIBS) \
@@ -80,9 +73,5 @@ nm_applet_LDADD = \
 uidir = $(datadir)/nm-applet
 ui_DATA = gsm-unlock.ui info.ui 8021x.ui keyring.png
 
-CLEANFILES = *.bak $(BUILT_SOURCES)
-
-EXTRA_DIST = \
-       $(ui_DATA) \
-       nm-applet-introspection.xml
+EXTRA_DIST = $(ui_DATA)
 
diff --git a/src/applet-vpn-request.c b/src/applet-vpn-request.c
index 541151f..8ba826a 100644
--- a/src/applet-vpn-request.c
+++ b/src/applet-vpn-request.c
@@ -87,8 +87,6 @@ typedef struct {
        AppletVpnRequest *vpn;
 } VpnSecretsInfo;
 
-#define DBUS_TYPE_G_MAP_OF_STRING (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING))
-
 static void 
 child_finished_cb (GPid pid, gint status, gpointer user_data)
 {
diff --git a/src/applet.c b/src/applet.c
index 7ba826b..df8b960 100644
--- a/src/applet.c
+++ b/src/applet.c
@@ -41,8 +41,6 @@
 #include <stdlib.h>
 
 #include <gio/gio.h>
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
 
 #include <libnotify/notify.h>
 
@@ -64,131 +62,7 @@
 extern gboolean shell_debug;
 extern gboolean with_agent;
 
-static void nma_initable_interface_init (GInitableIface *iface, gpointer iface_data);
-
-G_DEFINE_TYPE_WITH_CODE (NMApplet, nma, G_TYPE_OBJECT,
-                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
-                                                nma_initable_interface_init)
-                         )
-
-/********************************************************************/
-/* Temporary dbus interface stuff */
-
-static gboolean
-impl_dbus_connect_to_hidden_network (NMApplet *applet, GError **error)
-{
-       if (!applet_wifi_connect_to_hidden_network (applet)) {
-               g_set_error_literal (error,
-                                    NM_SECRET_AGENT_ERROR,
-                                    NM_SECRET_AGENT_ERROR_FAILED,
-                                    "Failed to create Wi-Fi dialog");
-               return FALSE;
-       }
-
-       return TRUE;
-}
-
-static gboolean
-impl_dbus_create_wifi_network (NMApplet *applet, GError **error)
-{
-       if (!applet_wifi_can_create_wifi_network (applet)) {
-               g_set_error_literal (error,
-                                    NM_SECRET_AGENT_ERROR,
-                                    NM_SECRET_AGENT_ERROR_PERMISSION_DENIED,
-                                    "Creation of Wi-Fi networks has been disabled by system policy.");
-               return FALSE;
-       }
-
-       if (!applet_wifi_create_wifi_network (applet)) {
-               g_set_error_literal (error,
-                                    NM_SECRET_AGENT_ERROR,
-                                    NM_SECRET_AGENT_ERROR_FAILED,
-                                    "Failed to create Wi-Fi dialog");
-               return FALSE;
-       }
-
-       return TRUE;
-}
-
-static gboolean
-impl_dbus_connect_to_8021x_network (NMApplet *applet,
-                                    const char *device_path,
-                                    const char *ap_path,
-                                    GError **error)
-{
-       NMDevice *device;
-       NMAccessPoint *ap;
-
-       device = nm_client_get_device_by_path (applet->nm_client, device_path);
-       if (!device || NM_IS_DEVICE_WIFI (device) == FALSE) {
-               g_set_error_literal (error,
-                                    NM_SECRET_AGENT_ERROR,
-                                    NM_SECRET_AGENT_ERROR_FAILED,
-                                    "The device could not be found.");
-               return FALSE;
-       }
-
-       ap = nm_device_wifi_get_access_point_by_path (NM_DEVICE_WIFI (device), ap_path);
-       if (!ap) {
-               g_set_error_literal (error,
-                                    NM_SECRET_AGENT_ERROR,
-                                    NM_SECRET_AGENT_ERROR_FAILED,
-                                    "The access point could not be found.");
-               return FALSE;
-       }
-
-       /* FIXME: this doesn't account for Dynamic WEP */
-       if (   !(nm_access_point_get_wpa_flags (ap) & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
-           && !(nm_access_point_get_rsn_flags (ap) & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) {
-               g_set_error_literal (error,
-                                    NM_SECRET_AGENT_ERROR,
-                                    NM_SECRET_AGENT_ERROR_FAILED,
-                                    "The access point had no 802.1x capabilities");
-               return FALSE;
-       }
-
-       if (!applet_wifi_connect_to_8021x_network (applet, device, ap)) {
-               g_set_error_literal (error,
-                                    NM_SECRET_AGENT_ERROR,
-                                    NM_SECRET_AGENT_ERROR_FAILED,
-                                    "Failed to create Wi-Fi dialog");
-               return FALSE;
-       }
-
-       return TRUE;
-}
-
-static gboolean
-impl_dbus_connect_to_3g_network (NMApplet *applet,
-                                 const char *device_path,
-                                 GError **error)
-{
-       NMDevice *device;
-
-       device = nm_client_get_device_by_path (applet->nm_client, device_path);
-       if (!device || NM_IS_DEVICE_MODEM (device) == FALSE) {
-               g_set_error_literal (error,
-                                    NM_SECRET_AGENT_ERROR,
-                                    NM_SECRET_AGENT_ERROR_FAILED,
-                                    "The device could not be found.");
-               return FALSE;
-       }
-
-#if WITH_WWAN
-       if (applet->mm1_running) {
-               applet_broadband_connect_network (applet, device);
-               return TRUE;
-       }
-#endif
-
-       g_set_error_literal (error,
-                            NM_SECRET_AGENT_ERROR,
-                            NM_SECRET_AGENT_ERROR_FAILED,
-                            "ModemManager was not found");
-       return FALSE;
-}
-
-#include "applet-dbus-bindings.h"
+G_DEFINE_TYPE (NMApplet, nma, G_TYPE_APPLICATION)
 
 /********************************************************************/
 
@@ -3329,36 +3203,6 @@ register_agent (NMApplet *applet)
 #endif
 }
 
-static gboolean
-dbus_setup (NMApplet *applet, GError **error)
-{
-       DBusGProxy *proxy;
-       guint result;
-       gboolean success;
-
-       applet->session_bus = dbus_g_bus_get (DBUS_BUS_SESSION, error);
-       if (!applet->session_bus)
-               return FALSE;
-
-       dbus_g_connection_register_g_object (applet->session_bus,
-                                            "/org/gnome/network_manager_applet",
-                                            G_OBJECT (applet));
-
-       proxy = dbus_g_proxy_new_for_name (applet->session_bus,
-                                          DBUS_SERVICE_DBUS,
-                                          DBUS_PATH_DBUS,
-                                          DBUS_INTERFACE_DBUS);
-       success = dbus_g_proxy_call (proxy, "RequestName", error,
-                                    G_TYPE_STRING, "org.gnome.network_manager_applet",
-                                    G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
-                                    G_TYPE_INVALID,
-                                    G_TYPE_UINT, &result,
-                                    G_TYPE_INVALID);
-       g_object_unref (proxy);
-
-       return success;
-}
-
 static void
 applet_gsettings_show_changed (GSettings *settings,
                                gchar *key,
@@ -3376,19 +3220,29 @@ applet_gsettings_show_changed (GSettings *settings,
 #endif
 }
 
-static gboolean
-initable_init (GInitable *initable, GCancellable *cancellable, GError **error)
+/****************************************************************/
+
+static void
+applet_activate (GApplication *app, gpointer user_data)
+{
+       /* Nothing to do, but glib requires this handler */
+}
+
+static void
+applet_startup (GApplication *app, gpointer user_data)
 {
-       NMApplet *applet = NM_APPLET (initable);
+       NMApplet *applet = NM_APPLET (app);
+       gs_free_error GError *error = NULL;
 
        g_set_application_name (_("NetworkManager Applet"));
        gtk_window_set_default_icon_name (GTK_STOCK_NETWORK);
 
        applet->info_dialog_ui = gtk_builder_new ();
 
-       if (!gtk_builder_add_from_file (applet->info_dialog_ui, UIDIR "/info.ui", error)) {
-               g_prefix_error (error, "Couldn't load info dialog ui file: ");
-               return FALSE;
+       if (!gtk_builder_add_from_file (applet->info_dialog_ui, UIDIR "/info.ui", &error)) {
+               g_warning ("Could not load info dialog UI file: %s", error->message);
+               g_application_quit (app);
+               return;
        }
 
        applet->gsettings = g_settings_new (APPLET_PREFS_SCHEMA);
@@ -3400,9 +3254,9 @@ initable_init (GInitable *initable, GCancellable *cancellable, GError **error)
 
        /* Load pixmaps and create applet widgets */
        if (!setup_widgets (applet)) {
-               g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC,
-                                    "Could not initialize widgets");
-               return FALSE;
+               g_warning ("Could not initialize applet widgets.");
+               g_application_quit (app);
+               return;
        }
        applet->icon_cache = g_hash_table_new_full (g_str_hash,
                                                    g_str_equal,
@@ -3413,11 +3267,6 @@ initable_init (GInitable *initable, GCancellable *cancellable, GError **error)
        if (!notify_is_initted ())
                notify_init ("NetworkManager");
 
-       if (!dbus_setup (applet, error)) {
-               g_prefix_error (error, "Failed to initialize D-Bus: ");
-               return FALSE;
-       }
-
        /* Initialize device classes */
        applet->ethernet_class = applet_device_ethernet_get_class (applet);
        g_assert (applet->ethernet_class);
@@ -3449,7 +3298,7 @@ initable_init (GInitable *initable, GCancellable *cancellable, GError **error)
        if (with_agent)
                register_agent (applet);
 
-       return TRUE;
+       g_application_hold (G_APPLICATION (applet));
 }
 
 static void finalize (GObject *object)
@@ -3496,7 +3345,6 @@ static void finalize (GObject *object)
 #endif
 
        g_clear_object (&applet->agent);
-       g_clear_pointer (&applet->session_bus, dbus_g_connection_unref);
 
        G_OBJECT_CLASS (nma_parent_class)->finalize (object);
 }
@@ -3504,6 +3352,9 @@ static void finalize (GObject *object)
 static void nma_init (NMApplet *applet)
 {
        applet->icon_size = 16;
+
+       g_signal_connect (applet, "startup", G_CALLBACK (applet_startup), NULL);
+       g_signal_connect (applet, "activate", G_CALLBACK (applet_activate), NULL);
 }
 
 static void nma_class_init (NMAppletClass *klass)
@@ -3511,27 +3362,5 @@ static void nma_class_init (NMAppletClass *klass)
        GObjectClass *oclass = G_OBJECT_CLASS (klass);
 
        oclass->finalize = finalize;
-
-       dbus_g_object_type_install_info (NM_TYPE_APPLET, &dbus_glib_nma_object_info);
-}
-
-static void
-nma_initable_interface_init (GInitableIface *iface, gpointer iface_data)
-{
-       iface->init = initable_init;
-}
-
-NMApplet *
-nm_applet_new (void)
-{
-       NMApplet *applet;
-       GError *error = NULL;
-
-       applet = g_initable_new (NM_TYPE_APPLET, NULL, &error, NULL);
-       if (!applet) {
-               g_warning ("%s", error->message);
-               g_error_free (error);
-       }
-       return applet;
 }
 
diff --git a/src/applet.h b/src/applet.h
index 40aef69..33a8870 100644
--- a/src/applet.h
+++ b/src/applet.h
@@ -27,7 +27,6 @@
 #include <gtk/gtk.h>
 #include <gdk/gdkx.h>
 
-#include <dbus/dbus-glib.h>
 #include <net/ethernet.h>
 
 #include <libnotify/notify.h>
@@ -51,9 +50,8 @@
 #define NM_IS_APPLET_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_APPLET))
 #define NM_APPLET_GET_CLASS(object)(G_TYPE_INSTANCE_GET_CLASS((object), NM_TYPE_APPLET, NMAppletClass))
 
-typedef struct
-{
-       GObjectClass    parent_class;
+typedef struct {
+       GApplicationClass       parent_class;
 } NMAppletClass; 
 
 #define APPLET_PREFS_SCHEMA "org.gnome.nm-applet"
@@ -74,11 +72,8 @@ typedef struct NMADeviceClass NMADeviceClass;
  * Applet instance data
  *
  */
-typedef struct
-{
-       GObject parent_instance;
-
-       DBusGConnection *session_bus;
+typedef struct {
+       GApplication parent;
 
        NMClient *nm_client;
        AppletAgent *agent;
diff --git a/src/main.c b/src/main.c
index 3c29ed6..59d7b87 100644
--- a/src/main.c
+++ b/src/main.c
@@ -26,15 +26,12 @@
 
 #include <string.h>
 #include <stdlib.h>
-#include <signal.h>
-#include <unistd.h>
 
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 
 #include "applet.h"
 
-static GMainLoop *loop = NULL;
 gboolean shell_debug = FALSE;
 gboolean with_agent = TRUE;
 
@@ -54,8 +51,10 @@ usage (const char *progname)
 
 int main (int argc, char *argv[])
 {
-       NMApplet *applet;
+       GApplication *applet;
+       char *fake_args[1] = { argv[0] };
        guint32 i;
+       int status;
 
        for (i = 1; i < argc; i++) {
                if (!strcmp (argv[i], "--help")) {
@@ -73,16 +72,12 @@ int main (int argc, char *argv[])
        gtk_init (&argc, &argv);
        textdomain (GETTEXT_PACKAGE);
 
-       loop = g_main_loop_new (NULL, FALSE);
+       applet = g_object_new (NM_TYPE_APPLET,
+                              "application-id", "org.freedesktop.network-manager-applet",
+                              NULL);
+       status = g_application_run (applet, 1, fake_args);
+       g_object_unref (applet);
 
-       applet = nm_applet_new ();
-       if (applet == NULL)
-               exit (1);
-
-       g_main_loop_run (loop);
-
-       g_object_unref (G_OBJECT (applet));
-
-       exit (0);
+       return status;
 }
 


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