From: Antti Kaijanmäki <antti.kaijanmaki@canonical.com>
Subject: Implement support for hiding rather than desensitizing disallowed items

This is done using a new environment variable that can be set when nm-applet is
started: NM_APPLET_HIDE_POLICY_ITEMS.

---
 src/applet-device-cdma.c |    7 +++-
 src/applet-device-gsm.c  |    8 +++--
 src/applet-device-wifi.c |   20 ++++++++++--
 src/applet.c             |   74 +++++++++++++++++++++++++++++++++++++++++------
 src/applet.h             |    1 
 5 files changed, 95 insertions(+), 15 deletions(-)

Index: network-manager-applet-0.9.4.1/src/applet.c
===================================================================
--- network-manager-applet-0.9.4.1.orig/src/applet.c
+++ network-manager-applet-0.9.4.1/src/applet.c
@@ -1565,8 +1565,16 @@ nma_menu_device_get_menu_item (NMDevice
 		                       (GClosureNotify) applet_device_info_destroy, 0);
 		if (is_permission_yes (applet, NM_CLIENT_PERMISSION_NETWORK_CONTROL))
 			gtk_widget_set_sensitive (item, TRUE);
-		else
+		else {
 			gtk_widget_set_sensitive (item, FALSE);
+			if (applet->hide_policy_items) {
+				/* TODO: is this the final solution? */
+				g_object_ref_sink (item);
+				g_object_unref (item);
+				item = NULL; /* it's safe for this function to return NULL as all the
+								callers are handling it properly */
+			}
+		}
 		break;
 	}
 	default:
@@ -1704,6 +1712,8 @@ nma_menu_add_vpn_submenu (GtkWidget *men
 	GtkMenuItem *item;
 	GSList *list, *iter;
 	int num_vpn_active = 0;
+	gboolean configure_allowed;
+	gboolean disconnect_allowed;
 
 	vpn_menu = GTK_MENU (gtk_menu_new ());
 
@@ -1771,30 +1781,58 @@ nma_menu_add_vpn_submenu (GtkWidget *men
 		gtk_menu_shell_append (GTK_MENU_SHELL (vpn_menu), GTK_WIDGET (item));
 	}
 
+	if (   is_permission_yes (applet, NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM)
+		|| is_permission_yes (applet, NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN))
+		configure_allowed = TRUE;
+	else
+		configure_allowed = FALSE;
+
+	if (is_permission_yes (applet, NM_CLIENT_PERMISSION_NETWORK_CONTROL))
+		disconnect_allowed = TRUE;
+	else
+		disconnect_allowed = FALSE;
+
+
 	/* Draw a seperator, but only if we have VPN connections above it */
 	if (list)
-		nma_menu_add_separator_item (GTK_WIDGET (vpn_menu));
+		if (   !applet->hide_policy_items
+			|| configure_allowed
+			|| disconnect_allowed)
+			nma_menu_add_separator_item (GTK_WIDGET (vpn_menu)); /* separator is added if there
+																	will be items under it */
 
 	item = GTK_MENU_ITEM (gtk_menu_item_new_with_mnemonic (_("_Configure VPN...")));
 	g_signal_connect (item, "activate", G_CALLBACK (nma_menu_configure_vpn_item_activate), applet);
-	gtk_menu_shell_append (GTK_MENU_SHELL (vpn_menu), GTK_WIDGET (item));
-	if (   is_permission_yes (applet, NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM)
-		|| is_permission_yes (applet, NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN)) {
+	if (configure_allowed) {
 		gtk_widget_set_sensitive (GTK_WIDGET (item), TRUE);
+		gtk_menu_shell_append (GTK_MENU_SHELL (vpn_menu), GTK_WIDGET (item));
 	} else {
 		gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
+		if (!applet->hide_policy_items) {
+			gtk_menu_shell_append (GTK_MENU_SHELL (vpn_menu), GTK_WIDGET (item));
+		} else {
+			/* TODO: is this the final solution? */
+			g_object_ref_sink (item);
+			g_object_unref (item);
+		}
 	}
 
 	item = GTK_MENU_ITEM (gtk_menu_item_new_with_mnemonic (_("_Disconnect VPN")));
 	g_signal_connect (item, "activate", G_CALLBACK (nma_menu_disconnect_vpn_item_activate), applet);
-	gtk_menu_shell_append (GTK_MENU_SHELL (vpn_menu), GTK_WIDGET (item));
-	if (is_permission_yes (applet, NM_CLIENT_PERMISSION_NETWORK_CONTROL)) {
+	if (disconnect_allowed) {
 		gtk_widget_set_sensitive (GTK_WIDGET (item), TRUE);
 		if (num_vpn_active == 0)
 			gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
-
+		gtk_menu_shell_append (GTK_MENU_SHELL (vpn_menu), GTK_WIDGET (item));
 	} else {
 		gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
+		if (!applet->hide_policy_items)
+			gtk_menu_shell_append (GTK_MENU_SHELL (vpn_menu), GTK_WIDGET (item));
+		else {
+			/* TODO: is this the final solution? */
+			g_object_ref_sink (item);
+			g_object_unref (item);
+		}
 	}
 
 	g_slist_free (list);
@@ -1884,6 +1922,8 @@ nma_set_notifications_enabled_cb (GtkWid
 static void nma_menu_show_cb (GtkWidget *menu, NMApplet *applet)
 {
 	guint32 n_wireless;
+	guint n_children;
+	GList *children = NULL;
 
 	g_return_if_fail (menu != NULL);
 	g_return_if_fail (applet != NULL);
@@ -1903,10 +1943,21 @@ static void nma_menu_show_cb (GtkWidget
 	n_wireless = nma_menu_add_devices (menu, applet);
 
 	if (n_wireless > 0 && nm_client_wireless_get_enabled (applet->nm_client)) {
+		children = gtk_container_get_children (GTK_CONTAINER (menu));
+		n_children = g_list_length (children);
+		g_list_free (children);
+
 		/* Add the "Hidden wireless network..." entry */
 		nma_menu_add_hidden_network_item (menu, applet);
 		nma_menu_add_create_network_item (menu, applet);
-		nma_menu_add_separator_item (menu);
+
+		children = gtk_container_get_children (GTK_CONTAINER (menu));
+		if (g_list_length (children) > n_children) {
+			/* items were added. Add a separator */
+			/* TODO: see TODO comments in the above add_*_network_item functions */
+			nma_menu_add_separator_item (menu);
+		}
+		g_list_free (children);
 	}
 
 	nma_menu_add_vpn_submenu (menu, applet);
@@ -2083,6 +2134,7 @@ nma_context_menu_update (NMApplet *apple
 		 * so set the "Edit Connections..." menu item insensitive.
 		 */
 		gtk_widget_set_sensitive (applet->connections_menu_item, FALSE);
+		gtk_widget_set_visible (applet->connections_menu_item, !applet->hide_policy_items);
 	}
 }
 
@@ -4055,6 +4107,10 @@ static void nma_init (NMApplet *applet)
 	applet->icon_theme = NULL;
 	applet->notification = NULL;
 	applet->icon_size = 16;
+
+	applet->hide_policy_items = FALSE;
+	if (getenv ("NM_APPLET_HIDE_POLICY_ITEMS"))
+		applet->hide_policy_items = TRUE;
 }
 
 enum {
Index: network-manager-applet-0.9.4.1/src/applet-device-wifi.c
===================================================================
--- network-manager-applet-0.9.4.1.orig/src/applet-device-wifi.c
+++ network-manager-applet-0.9.4.1/src/applet-device-wifi.c
@@ -101,7 +101,6 @@ nma_menu_add_hidden_network_item (GtkWid
 	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 	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_swapped (menu_item, "activate",
 	                          G_CALLBACK (applet_wifi_connect_to_hidden_network),
 	                          applet);
@@ -132,6 +131,13 @@ nma_menu_add_hidden_network_item (GtkWid
 	}
 
 	gtk_widget_set_sensitive (GTK_WIDGET (menu_item), allowed);
+	if (!allowed && applet->hide_policy_items) {
+		/* don't add the item if it should be hidden */
+		/* TODO: is this the final solution? */
+		g_object_ref_sink (menu_item);
+		g_object_unref (menu_item);
+	} else
+		gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
 }
 
 gboolean
@@ -181,13 +187,21 @@ nma_menu_add_create_network_item (GtkWid
 	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 	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_swapped (menu_item, "activate",
 	                          G_CALLBACK (applet_wifi_create_wifi_network),
 	                          applet);
 
-	if (!applet_wifi_can_create_wifi_network (applet))
+	if (!applet_wifi_can_create_wifi_network (applet)) {
 		gtk_widget_set_sensitive (GTK_WIDGET (menu_item), FALSE);
+		if (applet->hide_policy_items) {
+			/* don't add the item if it should be hidden */
+			/* TODO: is this the final solution? */
+			g_object_ref_sink (menu_item);
+			g_object_unref (menu_item);
+			return;
+		}
+	}
+	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
 }
 
 static void
Index: network-manager-applet-0.9.4.1/src/applet.h
===================================================================
--- network-manager-applet-0.9.4.1.orig/src/applet.h
+++ network-manager-applet-0.9.4.1/src/applet.h
@@ -102,6 +102,7 @@ typedef struct
 	GConfClient *	gconf_client;
 
 	gboolean visible;
+	gboolean hide_policy_items;
 
 	/* Permissions */
 	NMClientPermissionResult permissions[NM_CLIENT_PERMISSION_LAST + 1];
Index: network-manager-applet-0.9.4.1/src/applet-device-cdma.c
===================================================================
--- network-manager-applet-0.9.4.1.orig/src/applet-device-cdma.c
+++ network-manager-applet-0.9.4.1/src/applet-device-cdma.c
@@ -473,7 +473,12 @@ cdma_add_menu_item (NMDevice *device,
 			item = gtk_check_menu_item_new_with_label (_("New Mobile Broadband (CDMA) connection..."));
 
 			gtk_widget_set_sensitive (GTK_WIDGET (item), allowed);
-			add_connection_item (device, NULL, item, menu, applet);
+			if (!allowed && applet->hide_policy_items) {
+				/* don't add the item if should be hidden */
+				g_object_ref_sink (item);
+				g_object_unref (item);
+			} else
+				add_connection_item (device, NULL, item, menu, applet);
 		}
 	}
 
Index: network-manager-applet-0.9.4.1/src/applet-device-gsm.c
===================================================================
--- network-manager-applet-0.9.4.1.orig/src/applet-device-gsm.c
+++ network-manager-applet-0.9.4.1/src/applet-device-gsm.c
@@ -632,8 +632,12 @@ gsm_add_menu_item (NMDevice *device,
 			/* Default connection item */
 			item = gtk_check_menu_item_new_with_label (_("New Mobile Broadband (GSM) connection..."));
 			gtk_widget_set_sensitive (GTK_WIDGET (item), allowed);
-			add_connection_item (device, NULL, item, menu, applet);
-
+			if (!allowed && applet->hide_policy_items) {
+				/* don't add the item if it should be hidden */
+				g_object_ref_sink (item);
+				g_object_unref (item);
+			} else
+				add_connection_item (device, NULL, item, menu, applet);
 		}
 	}
 
