[network-manager-applet] applet: provide simple D-Bus API for GNOME Shell indicator (bgo #642503)



commit afd0ee59a962f6eedc3dc1c41e065f9480b8468e
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Wed Mar 2 14:24:33 2011 -0600

    applet: provide simple D-Bus API for GNOME Shell indicator (bgo #642503)
    
    Since the Shell's indicator is lagging a bit in functionality due
    to missing bindings for stuff like gnome-keyring expose a few small
    dbus methods to help it out.  This is a *PRIVATE API* that is
    not guaranteed to be stable.
    
    [cleaned up, refactored, and permissions checking added by dcbw]

 .gitignore                      |    2 +
 src/Makefile.am                 |    7 ++-
 src/applet-device-wifi.c        |  110 ++++++++++++++++++++++-----------------
 src/applet.c                    |   99 +++++++++++++++++++++++++++++++----
 src/applet.h                    |    5 ++
 src/nm-applet-introspection.xml |   11 ++++
 6 files changed, 176 insertions(+), 58 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 477a214..df5f115 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,4 +37,6 @@ src/connection-editor/nm-connection-editor
 src/connection-editor/nm-connection-editor-service-glue.h
 src/marshallers/nma-marshal.[ch]
 src/nm-applet
+src/applet-dbus-bindings.h
+
 
diff --git a/src/Makefile.am b/src/Makefile.am
index d057eae..3349481 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -21,6 +21,11 @@ nm_applet_CPPFLAGS = \
 	-I${top_srcdir}/src/gconf-helpers  \
 	-I${top_srcdir}/src/wireless-security
 
+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 \
@@ -68,7 +73,7 @@ nm_applet_LDADD = \
 uidir = $(datadir)/nm-applet
 ui_DATA = applet.ui keyring.png
 
-CLEANFILES = *.bak
+CLEANFILES = *.bak $(BUILT_SOURCES)
 
 EXTRA_DIST = 				\
 	$(ui_DATA)
diff --git a/src/applet-device-wifi.c b/src/applet-device-wifi.c
index 5db9560..97a03a5 100644
--- a/src/applet-device-wifi.c
+++ b/src/applet-device-wifi.c
@@ -56,27 +56,27 @@ static NMAccessPoint *update_active_ap (NMDevice *device, NMDeviceState state, N
 static void
 show_ignore_focus_stealing_prevention (GtkWidget *widget)
 {
+	GdkWindow *window;
+
 	gtk_widget_realize (widget);
 	gtk_widget_show (widget);
-	gtk_window_present_with_time (GTK_WINDOW (widget),
-		gdk_x11_get_server_time (gtk_widget_get_window (widget)));
+	window = gtk_widget_get_window (widget);
+	gtk_window_present_with_time (GTK_WINDOW (widget), gdk_x11_get_server_time (window));
 }
 
-static void
-other_wireless_activate_cb (GtkWidget *menu_item,
-                            NMApplet *applet)
+gboolean
+applet_wifi_connect_to_hidden_network (NMApplet *applet)
 {
 	GtkWidget *dialog;
 
 	dialog = nma_wireless_dialog_new_for_other (applet);
-	if (!dialog)
-		return;
-
-	g_signal_connect (dialog, "response",
-	                  G_CALLBACK (wireless_dialog_response_cb),
-	                  applet);
-
-	show_ignore_focus_stealing_prevention (dialog);
+	if (dialog) {
+		g_signal_connect (dialog, "response",
+		                  G_CALLBACK (wireless_dialog_response_cb),
+		                  applet);
+		show_ignore_focus_stealing_prevention (dialog);
+	}
+	return !!dialog;
 }
 
 void
@@ -91,23 +91,45 @@ nma_menu_add_hidden_network_item (GtkWidget *menu, NMApplet *applet)
 	gtk_container_add (GTK_CONTAINER (menu_item), label);
 	gtk_widget_show_all (menu_item);
 	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
-	g_signal_connect (menu_item, "activate", G_CALLBACK (other_wireless_activate_cb), applet);
+	g_signal_connect_swapped (menu_item, "activate",
+	                          G_CALLBACK (applet_wifi_connect_to_hidden_network),
+	                          applet);
 }
 
-static void
-new_network_activate_cb (GtkWidget *menu_item, NMApplet *applet)
+gboolean
+applet_wifi_can_create_wifi_network (NMApplet *applet)
 {
-	GtkWidget *dialog;
+	gboolean disabled, allowed = FALSE;
+	NMClientPermissionResult perm;
+	GError *error = NULL;
 
-	dialog = nma_wireless_dialog_new_for_create (applet);
-	if (!dialog)
-		return;
+	/* FIXME: check WIFI_SHARE_PROTECTED too, and make the wireless dialog
+	 * handle the permissions as well so that admins can restrict open network
+	 * creation separately from protected network creation.
+	 */
+	perm = nm_client_get_permission_result (applet->nm_client, NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN);
+	if (perm == NM_CLIENT_PERMISSION_RESULT_YES || perm == NM_CLIENT_PERMISSION_RESULT_AUTH) {
+		disabled = gconf_client_get_bool (applet->gconf_client, PREF_DISABLE_WIFI_CREATE, &error);
+		if (!disabled && !error)
+			allowed = TRUE;
+		g_clear_error (&error);
+	}
+	return allowed;
+}
 
-	g_signal_connect (dialog, "response",
-	                  G_CALLBACK (wireless_dialog_response_cb),
-	                  applet);
+gboolean
+applet_wifi_create_wifi_network (NMApplet *applet)
+{
+	GtkWidget *dialog;
 
-	show_ignore_focus_stealing_prevention (dialog);
+	dialog = nma_wireless_dialog_new_for_other (applet);
+	if (dialog) {
+		g_signal_connect (dialog, "response",
+		                  G_CALLBACK (wireless_dialog_response_cb),
+		                  applet);
+		show_ignore_focus_stealing_prevention (dialog);
+	}
+	return !!dialog;
 }
 
 void
@@ -115,8 +137,6 @@ nma_menu_add_create_network_item (GtkWidget *menu, NMApplet *applet)
 {
 	GtkWidget *menu_item;
 	GtkWidget *label;
-	GError *error = NULL;
-	gboolean disabled;
 
 	menu_item = gtk_menu_item_new ();
 	label = gtk_label_new_with_mnemonic (_("Create _New Wireless Network..."));
@@ -124,15 +144,12 @@ nma_menu_add_create_network_item (GtkWidget *menu, NMApplet *applet)
 	gtk_container_add (GTK_CONTAINER (menu_item), label);
 	gtk_widget_show_all (menu_item);
 	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
-	g_signal_connect (menu_item, "activate", G_CALLBACK (new_network_activate_cb), applet);
+	g_signal_connect_swapped (menu_item, "activate",
+	                          G_CALLBACK (applet_wifi_create_wifi_network),
+	                          applet);
 
-	/* FIXME: should really use PolicyKit and NM permissions here instead
-	 * using using GConf mandatory settings.  But this works for now.
-	 */
-	disabled = gconf_client_get_bool (applet->gconf_client, PREF_DISABLE_WIFI_CREATE, &error);
-	if (error || disabled)
+	if (!applet_wifi_can_create_wifi_network (applet))
 		gtk_widget_set_sensitive (GTK_WIDGET (menu_item), FALSE);
-	g_clear_error (&error);
 }
 
 
@@ -1475,13 +1492,12 @@ wireless_get_more_info (NMDevice *device,
 	GtkWidget *dialog;
 
 	dialog = nma_wireless_dialog_new (applet, connection, device, info->ap);
-	g_return_if_fail (dialog != NULL);
-
-	g_signal_connect (dialog, "response",
-	                  G_CALLBACK (wireless_dialog_response_cb),
-	                  applet);
-
-	show_ignore_focus_stealing_prevention (dialog);
+	if (dialog) {
+		g_signal_connect (dialog, "response",
+			              G_CALLBACK (wireless_dialog_response_cb),
+			              applet);
+		show_ignore_focus_stealing_prevention (dialog);
+	}
 }
 
 static gboolean
@@ -1659,19 +1675,19 @@ wireless_get_secrets (SecretsRequest *req, GError **error)
 	applet_secrets_request_set_free_func (req, free_wifi_info);
 
 	info->dialog = nma_wireless_dialog_new (req->applet, req->connection, NULL, NULL);
-	if (!info->dialog) {
+	if (info->dialog) {
+		g_signal_connect (info->dialog, "response",
+		                  G_CALLBACK (get_secrets_dialog_response_cb),
+		                  info);
+		show_ignore_focus_stealing_prevention (info->dialog);
+	} else {
 		g_set_error (error,
 		             NM_SECRET_AGENT_ERROR,
 		             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
 		             "%s.%d (%s): couldn't display secrets UI",
 		             __FILE__, __LINE__, __func__);
-		return FALSE;
 	}
-
-	g_signal_connect (info->dialog, "response", G_CALLBACK (get_secrets_dialog_response_cb), info);
-	show_ignore_focus_stealing_prevention (info->dialog);
-
-	return TRUE;
+	return !!info->dialog;
 }
 
 NMADeviceClass *
diff --git a/src/applet.c b/src/applet.c
index ffda279..54ecbbe 100644
--- a/src/applet.c
+++ b/src/applet.c
@@ -75,6 +75,7 @@
 #include "applet-device-bt.h"
 #include "applet-device-wimax.h"
 #include "applet-dialogs.h"
+#include "wireless-dialog.h"
 #include "applet-vpn-request.h"
 #include "utils.h"
 #include "gconf-helpers.h"
@@ -83,6 +84,49 @@
 
 G_DEFINE_TYPE(NMApplet, nma, G_TYPE_OBJECT)
 
+/********************************************************************/
+/* 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_INTERNAL_ERROR,
+		                     "Failed to create wireless 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_NOT_AUTHORIZED,
+		                     "Creation of wifi 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_INTERNAL_ERROR,
+		                     "Failed to create wireless dialog");
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+#include "applet-dbus-bindings.h"
+
+/********************************************************************/
+
 static NMActiveConnection *
 applet_get_best_activating_connection (NMApplet *applet, NMDevice **device)
 {
@@ -3098,6 +3142,44 @@ applet_embedded_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
 	           embedded ? "embedded in" : "removed from");
 }
 
+static gboolean
+dbus_setup (NMApplet *applet, GError **error)
+{
+	DBusConnection *connection;
+	DBusGProxy *proxy;
+	guint result;
+	gboolean success;
+
+	applet->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, error);
+	if (!applet->bus)
+		return FALSE;
+
+	connection = dbus_g_connection_get_connection (applet->bus);
+	dbus_connection_set_exit_on_disconnect (connection, FALSE);
+
+	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 GObject *
 constructor (GType type,
              guint n_props,
@@ -3105,7 +3187,6 @@ constructor (GType type,
 {
 	NMApplet *applet;
 	GError* error = NULL;
-	DBusConnection *connection;
 
 	applet = NM_APPLET (G_OBJECT_CLASS (nma_parent_class)->constructor (type, n_props, construct_props));
 
@@ -3147,18 +3228,11 @@ constructor (GType type,
 	if (!notify_is_initted ())
 		notify_init ("NetworkManager");
 
-	applet->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
-	if (!applet->bus) {
-		g_warning ("Could not get the system bus.  Make sure the message "
-		           "bus daemon is running!  Message: %s",
-		           error->message);
+	if (!dbus_setup (applet, &error)) {
+		g_warning ("Failed to initialize D-Bus: %s", error->message);
 		g_error_free (error);
 		goto error;
 	}
-
-	connection = dbus_g_connection_get_connection (applet->bus);
-	dbus_connection_set_exit_on_disconnect (connection, FALSE);
-
 	applet->settings = nm_remote_settings_new (applet->bus);
 
 	applet->agent = applet_agent_new ();
@@ -3266,6 +3340,9 @@ static void finalize (GObject *object)
 	if (applet->bus)
 		dbus_g_connection_unref (applet->bus);
 
+	if (applet->session_bus)
+		dbus_g_connection_unref (applet->session_bus);
+
 	G_OBJECT_CLASS (nma_parent_class)->finalize (object);
 }
 
@@ -3311,6 +3388,8 @@ static void nma_class_init (NMAppletClass *klass)
 
 	pspec = g_param_spec_pointer ("loop", "Loop", "Applet mainloop", G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
 	g_object_class_install_property (oclass, PROP_LOOP, pspec);
+
+	dbus_g_object_type_install_info (NM_TYPE_APPLET, &dbus_glib_nma_object_info);
 }
 
 NMApplet *
diff --git a/src/applet.h b/src/applet.h
index fe6ba79..aafdc4d 100644
--- a/src/applet.h
+++ b/src/applet.h
@@ -82,6 +82,7 @@ typedef struct
 
 	GMainLoop *loop;
 	DBusGConnection *bus;
+	DBusGConnection *session_bus;
 
 	NMClient *nm_client;
 	NMRemoteSettings *settings;
@@ -309,4 +310,8 @@ GdkPixbuf * nma_icon_check_and_load (const char *name,
                                      GdkPixbuf **icon,
                                      NMApplet *applet);
 
+gboolean applet_wifi_connect_to_hidden_network (NMApplet *applet);
+gboolean applet_wifi_create_wifi_network (NMApplet *applet);
+gboolean applet_wifi_can_create_wifi_network (NMApplet *applet);
+
 #endif
diff --git a/src/nm-applet-introspection.xml b/src/nm-applet-introspection.xml
new file mode 100644
index 0000000..907eecf
--- /dev/null
+++ b/src/nm-applet-introspection.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<node name="/org/gnome/network-manager-applet">
+  <interface name="org.gnome.network-manager-applet">
+    <method name="ConnectToHiddenNetwork">
+      <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_dbus_connect_to_hidden_network"/>
+    </method>
+    <method name="CreateWifiNetwork">
+      <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_dbus_create_wifi_network"/>
+    </method>
+  </interface>
+</node>



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