=?y?q?=5BPATCH=204/4=5D=20Add=20switches=20to=20the=20Applet=20menu?=



this is actually quite a bigger change, we factorize a bit of code adding the menu to the function (though it's not strictly necesary), we give a callback to toggle the device state,
note that the callback is unimplemented for bt and wired.

Signed-off-by: Niv Sardi <xaiki debian org>
---
 src/applet-device-bt.c    |   12 +++--
 src/applet-device-cdma.c  |   15 ++++--
 src/applet-device-gsm.c   |   15 ++++--
 src/applet-device-wifi.c  |   16 ++++--
 src/applet-device-wimax.c |   15 ++++--
 src/applet-device-wired.c |   12 +++--
 src/applet.c              |  119 +++++++++++++++++++++++++++++++++++++++++++--
 src/applet.h              |   10 ++--
 8 files changed, 178 insertions(+), 36 deletions(-)

diff --git a/src/applet-device-bt.c b/src/applet-device-bt.c
index 4f1cac2..cbba5b9 100644
--- a/src/applet-device-bt.c
+++ b/src/applet-device-bt.c
@@ -129,6 +129,12 @@ add_connection_items (NMDevice *device,
 	}
 }
 
+static gboolean
+bt_toggle (GtkWidget *sw, gpointer user_data)
+{
+	g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "UNIMPLEMENTED: toggle_bt\n");
+}
+
 static void
 bt_add_menu_item (NMDevice *device,
                   guint32 n_devices,
@@ -152,11 +158,7 @@ bt_add_menu_item (NMDevice *device,
 		g_assert (text);
 	}
 
-	item = applet_menu_item_create_device_item_helper (device, applet, text);
-
-	gtk_widget_set_sensitive (item, FALSE);
-	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-	gtk_widget_show (item);
+	applet_menu_item_add_device_item_helper (device, applet, menu, bt_toggle, text);
 
 	if (g_slist_length (connections))
 		add_connection_items (device, connections, active, ADD_ACTIVE, menu, applet);
diff --git a/src/applet-device-cdma.c b/src/applet-device-cdma.c
index 4c18131..f22ba03 100644
--- a/src/applet-device-cdma.c
+++ b/src/applet-device-cdma.c
@@ -316,6 +316,16 @@ cdma_act_to_mb_act (CdmaDeviceInfo *info)
 	return MB_TECH_UNKNOWN;
 }
 
+static gboolean
+cdma_toggle (GtkWidget *widget, gpointer user_data)
+{
+	NMApplet *applet = (NMApplet *) user_data;
+	gboolean enabled = ! nm_client_wwan_get_enabled (applet->nm_client);
+	nm_client_wwan_set_enabled (applet->nm_client, enabled);
+
+	return TRUE;
+}
+
 static void
 cdma_add_menu_item (NMDevice *device,
                     guint32 n_devices,
@@ -347,10 +357,7 @@ cdma_add_menu_item (NMDevice *device,
 		text = g_strdup (_("Mobile Broadband"));
 	}
 
-	item = applet_menu_item_create_device_item_helper (device, applet, text);
-	gtk_widget_set_sensitive (item, FALSE);
-	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-	gtk_widget_show (item);
+	applet_menu_item_add_device_item_helper (device, applet, menu, cdma_toggle, text);
 	g_free (text);
 
 	/* Add the active connection */
diff --git a/src/applet-device-gsm.c b/src/applet-device-gsm.c
index d0da7e4..d5297e9 100644
--- a/src/applet-device-gsm.c
+++ b/src/applet-device-gsm.c
@@ -363,6 +363,16 @@ gsm_act_to_mb_act (GsmDeviceInfo *info)
 	return MB_TECH_GSM;
 }
 
+static gboolean
+gsm_toggle (GtkWidget *widget, gpointer user_data)
+{
+	NMApplet *applet = (NMApplet *) user_data;
+	gboolean enabled = ! nm_client_wwan_get_enabled (applet->nm_client);
+	nm_client_wwan_set_enabled (applet->nm_client, enabled);
+
+	return TRUE;
+}
+
 static void
 gsm_add_menu_item (NMDevice *device,
                    guint32 n_devices,
@@ -394,10 +404,7 @@ gsm_add_menu_item (NMDevice *device,
 		text = g_strdup (_("Mobile Broadband"));
 	}
 
-	item = applet_menu_item_create_device_item_helper (device, applet, text);
-	gtk_widget_set_sensitive (item, FALSE);
-	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-	gtk_widget_show (item);
+	applet_menu_item_add_device_item_helper (device, applet, menu, gsm_toggle, text);
 	g_free (text);
 
 	/* Add the active connection */
diff --git a/src/applet-device-wifi.c b/src/applet-device-wifi.c
index ff0e374..7284f46 100644
--- a/src/applet-device-wifi.c
+++ b/src/applet-device-wifi.c
@@ -758,6 +758,16 @@ sort_toplevel (gconstpointer tmpa, gconstpointer tmpb)
 	return sort_by_name (a, b);
 }
 
+static gboolean
+wifi_toggle (GtkWidget *sw, gpointer user_data)
+{
+	NMApplet *applet = (NMApplet *) user_data;
+	gboolean enabled = ! nm_client_wireless_get_enabled (applet->nm_client);
+	nm_client_wireless_set_enabled (applet->nm_client, enabled);
+
+	return TRUE;
+}
+
 static void
 wireless_add_menu_item (NMDevice *device,
                         guint32 n_devices,
@@ -795,13 +805,9 @@ wireless_add_menu_item (NMDevice *device,
 	} else
 		text = g_strdup (ngettext ("Wireless Network", "Wireless Networks", aps ? aps->len : 0));
 
-	widget = applet_menu_item_create_device_item_helper (device, applet, text);
+	applet_menu_item_add_device_item_helper (device, applet, menu, wifi_toggle, text);
 	g_free (text);
 
-	gtk_widget_set_sensitive (widget, FALSE);
-	gtk_menu_shell_append (GTK_MENU_SHELL (menu), widget);
-	gtk_widget_show (widget);
-
 	all = applet_get_all_connections (applet);
 	connections = nm_device_filter_connections (device, all);
 	g_slist_free (all);
diff --git a/src/applet-device-wimax.c b/src/applet-device-wimax.c
index 042653a..994bf5c 100644
--- a/src/applet-device-wimax.c
+++ b/src/applet-device-wimax.c
@@ -202,6 +202,16 @@ sort_nsps (gconstpointer a, gconstpointer b)
 	return g_strcmp0 (name_a, name_b);
 }
 
+static gboolean
+wimax_toggle (GtkWidget *widget, gpointer user_data)
+{
+	NMApplet *applet = (NMApplet *) user_data;
+	gboolean enabled = ! nm_client_wimax_get_enabled (applet->nm_client);
+	nm_client_wimax_set_enabled (applet->nm_client, enabled);
+
+	return TRUE;
+}
+
 static void
 wimax_add_menu_item (NMDevice *device,
                      guint32 n_devices,
@@ -233,10 +243,7 @@ wimax_add_menu_item (NMDevice *device,
 		text = g_strdup (_("WiMAX Mobile Broadband"));
 	}
 
-	item = applet_menu_item_create_device_item_helper (device, applet, text);
-	gtk_widget_set_sensitive (item, FALSE);
-	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-	gtk_widget_show (item);
+	applet_menu_item_add_device_item_helper (device, applet, menu, wimax_toggle, text);
 	g_free (text);
 
 	/* Add the active NSP if we're connected to something and the device is available */
diff --git a/src/applet-device-wired.c b/src/applet-device-wired.c
index ba381f5..db36a58 100644
--- a/src/applet-device-wired.c
+++ b/src/applet-device-wired.c
@@ -177,6 +177,12 @@ add_default_connection_item (NMDevice *device,
 	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
 }
 
+static gboolean
+wired_toggle (GtkWidget *widget, gpointer user_data)
+{
+	g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "UNIMPLEMENTED wired_toggle\n");
+}
+
 static void
 wired_add_menu_item (NMDevice *device,
                      guint32 n_devices,
@@ -212,7 +218,7 @@ wired_add_menu_item (NMDevice *device,
 			text = g_strdup (_("Wired Network"));
 	}
 
-	item = applet_menu_item_create_device_item_helper (device, applet, text);
+	applet_menu_item_add_device_item_helper (device, applet, menu, wired_toggle, text);
 	g_free (text);
 
 	/* Only dim the item if the device supports carrier detection AND
@@ -221,10 +227,6 @@ wired_add_menu_item (NMDevice *device,
  	if (nm_device_get_capabilities (device) & NM_DEVICE_CAP_CARRIER_DETECT)
 		carrier = nm_device_ethernet_get_carrier (NM_DEVICE_ETHERNET (device));
 
-	gtk_widget_set_sensitive (item, FALSE);
-	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
-	gtk_widget_show (item);
-
 	if (g_slist_length (connections))
 		add_connection_items (device, connections, carrier, active, ADD_ACTIVE, menu, applet);
 
diff --git a/src/applet.c b/src/applet.c
index 7b3a97b..347ed51 100644
--- a/src/applet.c
+++ b/src/applet.c
@@ -631,7 +631,7 @@ applet_menu_create_switch_item_helper (NMApplet  *applet,
 
 	g_object_set (G_OBJECT (menu_item),
 	              "child", box,
-	              "sensitive", FALSE,
+	              "sensitive", TRUE,
 	              NULL);
 
 	return menu_item;
@@ -822,14 +822,121 @@ menu_title_item_dont_expose (GtkWidget *widget, GdkEventExpose *event, gpointer
 }
 #endif
 
-GtkWidget *
-applet_menu_item_create_device_item_helper (NMDevice *device,
+static inline gboolean
+applet_menu_item_toggle_switch (GtkWidget *widget, gpointer user_data)
+{
+	GtkSwitch *sw = GTK_SWITCH(user_data);
+	gtk_widget_activate(GTK_WIDGET(sw));
+}
+
+static inline gboolean
+applet_menu_item_draw_only_if_active_cb (GtkWidget *d, gpointer data)
+{
+	gboolean active;
+	g_object_get (G_OBJECT(d), "active", &active, NULL);
+	if (active)
+		return FALSE;
+	return TRUE;
+}
+
+static inline gboolean
+applet_menu_item_sync_state (NMState state,
+							 GtkWidget *spinner,
+							 GtkWidget *sw)
+{
+	gboolean spin = FALSE;
+	gboolean sws;
+
+	switch (state) {
+	case NM_DEVICE_STATE_DEACTIVATING:
+		spin = TRUE;
+	case NM_DEVICE_STATE_UNKNOWN:
+	case NM_DEVICE_STATE_UNMANAGED:
+	case NM_DEVICE_STATE_UNAVAILABLE:
+		sws = FALSE;
+		break;
+	case NM_DEVICE_STATE_PREPARE:
+	case NM_DEVICE_STATE_CONFIG:
+	case NM_DEVICE_STATE_NEED_AUTH:
+	case NM_DEVICE_STATE_IP_CONFIG:
+	case NM_DEVICE_STATE_IP_CHECK:
+	case NM_DEVICE_STATE_SECONDARIES:
+		spin = TRUE;
+	case NM_DEVICE_STATE_ACTIVATED:
+	case NM_DEVICE_STATE_DISCONNECTED:
+	case NM_DEVICE_STATE_FAILED:
+		sws = TRUE;
+		break;
+	default:
+		g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Got unmanaged state: %d", state);
+		break;
+	}
+
+	gtk_switch_set_active(GTK_SWITCH(sw), sws);
+	if (spin)
+		gtk_spinner_start(GTK_SPINNER(spinner));
+	else
+		gtk_spinner_stop(GTK_SPINNER(spinner));
+
+	return sws && spin;
+}
+
+static gboolean
+applet_menu_item_handle_state_changed_cb (NMDevice *device,
+										  NMDeviceState   state,
+										  NMDeviceState   old_state,
+										  NMDeviceStateReason   reason,
+										  gpointer user_data)
+{
+	GtkWidget *box  = GTK_WIDGET (user_data);
+	GList     *list = gtk_container_get_children (GTK_CONTAINER(box));
+	GtkWidget *sw      = list->data;
+	GtkWidget *spinner = g_list_next(list)->data;
+
+	applet_menu_item_sync_state (state, sw, spinner);
+	return FALSE;
+}
+
+/* A little note: this is needed because the menu actually goes away when not shown,
+   hence, the box goes away too… nasty things happen if we let the handler connected */
+static gboolean
+applet_menu_item_disconnect_signal (GtkWidget *box, gpointer user_data)
+{
+	NMDevice *device = NM_DEVICE (user_data);
+	g_object_disconnect (G_OBJECT (device), "any_signal", G_CALLBACK(applet_menu_item_handle_state_changed_cb), box, NULL);
+	return FALSE;
+}
+
+void
+applet_menu_item_add_device_item_helper (NMDevice *device,
                                             NMApplet *applet,
+											GtkWidget *menu,
+											gboolean (*cb) (GtkWidget *, gpointer user_data),
                                             const gchar *text)
 {
 	GtkWidget *item;
 	GtkWidget *label    = gtk_label_new (NULL);
-	item = applet_menu_create_switch_item_helper (applet, label, NULL);
+	GtkWidget *sw       = gtk_switch_new ();
+	GtkWidget *spinner  = gtk_spinner_new ();
+	GtkWidget *box      = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+	NMDeviceState state = nm_device_get_state(device);
+
+	applet_menu_item_sync_state (state, spinner, sw);
+
+	gtk_box_pack_start (GTK_BOX(box), spinner, FALSE, FALSE, 0);
+	gtk_box_pack_start (GTK_BOX(box), sw, FALSE, FALSE, 0);
+
+	g_signal_connect (spinner, "draw", G_CALLBACK (applet_menu_item_draw_only_if_active_cb), NULL);
+	g_signal_connect (device, "state-changed", G_CALLBACK(applet_menu_item_handle_state_changed_cb), box);
+	g_signal_connect (box, "destroy", G_CALLBACK (applet_menu_item_disconnect_signal), device);
+
+	gtk_label_set_markup (GTK_LABEL (label), text);
+
+	item = applet_menu_create_switch_item_helper (applet, label, box);
+	if (cb) {
+		g_signal_connect (item, "activate", G_CALLBACK(applet_menu_item_toggle_switch), sw);
+		g_signal_connect (sw,   "activate", G_CALLBACK (cb), applet);
+	}
 
 #if GTK_CHECK_VERSION(2,90,7)
 	g_signal_connect (item,  "draw", G_CALLBACK (menu_title_item_draw), label);
@@ -838,7 +945,9 @@ applet_menu_item_create_device_item_helper (NMDevice *device,
 	g_signal_connect (item,  "expose-event", G_CALLBACK (menu_title_item_expose), label);
 	g_signal_connect (label, "expose-event", G_CALLBACK (menu_title_item_dont_expose), NULL);
 #endif
-	return item;
+
+	gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+	gtk_widget_show (item);
 }
 
 static void
diff --git a/src/applet.h b/src/applet.h
index efc2eff..ad82107 100644
--- a/src/applet.h
+++ b/src/applet.h
@@ -276,10 +276,12 @@ void applet_menu_item_add_complex_separator_helper (GtkWidget *menu,
                                                     const gchar* label,
                                                     int pos);
 
-GtkWidget*
-applet_menu_item_create_device_item_helper (NMDevice *device,
-                                            NMApplet *applet,
-                                            const gchar *text);
+void
+applet_menu_item_add_device_item_helper (NMDevice *device,
+										 NMApplet *applet,
+										 GtkWidget *menu,
+										 gboolean (*cb) (GtkWidget *, gpointer user_data),
+										 const gchar *text);
 
 NMRemoteConnection *applet_get_exported_connection_for_device (NMDevice *device, NMApplet *applet);
 
-- 
1.7.10



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