Re: [PATCH] nm-applet, update bitrate every 2s in connection info dialog



Thanks Dan, 
I've updated the patch, seems to work pretty good.

PS. Any word on the libnl bitrates patch, will it be included? Because
by default now, in 2.6.31 nm-applet shows Unknown if i'm connected to
11n network.

On Thu, 2009-10-01 at 12:59 -0700, Dan Williams wrote:
> On Thu, 2009-09-24 at 22:06 +0300, Valmantas Palikša wrote:
> > I've seen myself opening and closing connection info to see if bitrate
> > changed, this should fix it. Now the bitrate label will be updated every
> > 2s if the connection info dialog is open.
> 
> Nice; though there's an easier way; use the 'bitrate' property of the
> device instead and listen for changes, then update the label.  We don't
> want to ref the device though, because that makes the device stick
> around and it'll still be in the menu then as long as the dialog is
> open.  So this gets a bit trickier; the signal handler that we attach to
> get notifications of the bitrate changes needs to be disconnected when
> the label goes away (otherwise the label's change callback will be
> called with invalid data).  So I think it would be something like this:
> 
> typedef struct {
> 	NMDevice *device;
>         GtkWidget *label;
> 	guint32 id;
> } SpeedInfo;
> 
> static void
> label_destroyed (gpointer data, GObject *label_ptr)
> {
> 	SpeedInfo *info = data;
> 
> 	/* Remove the notify handler from the device */
> 	if (info->device) {
> 		if (info->id)
> 			g_signal_handler_disconnect (info->device, info->id);
> 		/* destroy our info data */
> 		g_object_weak_unref (info->device, device_destroyed, info);
> 		memset (info, 0, sizeof (SpeedInfo));
> 		g_free (info);
> 	}
> }
> 
> static void
> device_destroyed (gpointer data, GObject *device_ptr)
> {
> 	SpeedInfo *info = data;
> 
> 	/* Device is destroyed, notify handler won't fire
> 	 * anymore anyway.  Let the label destroy handler
> 	 * know it doesn't have to disconnect the callback.
> 	 */
> 	info->device = NULL;
> 	info->id = 0;
> }
> 
> static void
> bitrate_changed_cb (GObject *device, GParamSpec *pspec, gpointer user_data)
> {
> 	GtkWidget *speed_label = GTK_WIDGET (user_data)
> 	guint32 bitrate;
> 
> 	if (NM_IS_DEVICE_WIFI (device))
> 		bitrate = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device));
> 	else if (NM_IS_DEVICE_ETHERNET (device))
> 		bitrate = nm_device_ethernet_get_bitrate (NM_DEVICE_ETHERNET (device));
> 
> 	/* udpate label text here */
> }
> 
> static void info_dialog_add_page (...)
> {
> 	GtkWidget *speed_label;
> 
> 	if (NM_IS_DEVICE_WIFI (device) || NM_IS_DEVICE_ETHERNET (device)) {
> 		/* create speed label */
> 
> 		info = g_malloc0 (sizeof (SpeedInfo));
> 		info->device = device;
> 		info->label = speed_label;
> 		info->id = g_signal_connect (device,
> 		                             "notify::" NM_DEVICE_WIFI_BITRATE,
> 		                             bitrate_changed_cb,
> 		                             speed_label);
> 
> 		g_object_weak_ref (speed_label, label_destroyed, info);
> 		g_object_weak_ref (device, device_destroyed, info);
> 	} else {
> 		/* create old-style label */
> 	}
> }
> 
> something like that actually.
> 
> Dan
> 
> 
diff --git a/src/applet-dialogs.c b/src/applet-dialogs.c
index e1dc530..da35c1b 100644
--- a/src/applet-dialogs.c
+++ b/src/applet-dialogs.c
@@ -231,6 +231,60 @@ create_info_notebook_label (NMConnection *connection, gboolean is_default)
 	return label;
 }
 
+typedef struct {
+	NMDevice *device;
+	GtkWidget *label;
+	guint32 id;
+} SpeedInfo;
+
+static void
+device_destroyed (gpointer data, GObject *device_ptr)
+{
+	SpeedInfo *info = data;
+	/* Device is destroyed, notify handler won't fire
+	 * anymore anyway.  Let the label destroy handler
+	 * know it doesn't have to disconnect the callback.
+	 */
+	info->device = NULL;
+	info->id = 0;
+}
+
+static void
+label_destroyed (gpointer data, GObject *label_ptr)
+{
+	SpeedInfo *info = data;
+	/* Remove the notify handler from the device */
+	if (info->device) {
+		if (info->id)
+			g_signal_handler_disconnect (info->device, info->id);
+		/* destroy our info data */
+		g_object_weak_unref (G_OBJECT(info->device), device_destroyed, info);
+		memset (info, 0, sizeof (SpeedInfo));
+		g_free (info);
+	}
+}
+
+static void
+bitrate_changed_cb (GObject *device, GParamSpec *pspec, gpointer user_data)
+{
+	
+	GtkWidget *speed_label = GTK_WIDGET (user_data);
+	guint32 bitrate = 0;
+	gchar* str;
+	
+	if (NM_IS_DEVICE_WIFI (device)) {
+		bitrate = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device));
+		bitrate /= 1000;
+	}
+	if (bitrate) {
+		str = g_strdup_printf (_("%u Mb/s"), bitrate);
+	} else {
+		str = NULL;
+	}
+	gtk_label_set_text(GTK_LABEL(speed_label), str ? str : _("Unknown"));
+	g_free(str);
+}
+
 static void
 info_dialog_add_page (GtkNotebook *notebook,
 					  NMConnection *connection,
@@ -246,6 +300,9 @@ info_dialog_add_page (GtkNotebook *notebook,
 	NMIP4Address *def_addr;
 	guint32 hostmask, network, bcast, netmask;
 	int row = 0;
+	
+	SpeedInfo* info = NULL;
+	GtkWidget* speed_label;
 
 	table = GTK_TABLE (gtk_table_new (12, 2, FALSE));
 	gtk_table_set_col_spacings (table, 12);
@@ -273,7 +330,7 @@ info_dialog_add_page (GtkNotebook *notebook,
 							   1, 2, row, row + 1);
 	g_free (str);
 	row++;
-
+	
 	/* Hardware address */
 	str = NULL;
 	if (NM_IS_DEVICE_ETHERNET (device))
@@ -299,8 +356,11 @@ info_dialog_add_page (GtkNotebook *notebook,
 							   1, 2, row, row + 1);
 	row++;
 
+	speed_label = create_info_label ("", TRUE);
+
 	/* Speed */
 	speed = 0;
+	
 	if (NM_IS_DEVICE_ETHERNET (device)) {
 		/* Wired speed in Mb/s */
 		speed = nm_device_ethernet_get_speed (NM_DEVICE_ETHERNET (device));
@@ -308,18 +368,31 @@ info_dialog_add_page (GtkNotebook *notebook,
 		/* Wireless speed in Kb/s */
 		speed = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device));
 		speed /= 1000;
+        
+		info = g_malloc0 (sizeof (SpeedInfo));
+		info->device = device;
+		info->label = speed_label;
+		info->id = g_signal_connect (device,
+									"notify::" NM_DEVICE_WIFI_BITRATE,
+									(GCallback)bitrate_changed_cb,
+									speed_label);
+
+		g_object_weak_ref (G_OBJECT(speed_label), label_destroyed, info);
+		g_object_weak_ref (G_OBJECT(device), device_destroyed, info);
 	}
 
 	if (speed)
 		str = g_strdup_printf (_("%u Mb/s"), speed);
 	else
 		str = NULL;
+		
+	gtk_label_set_text(GTK_LABEL(speed_label), str ? str : _("Unknown"));
 
 	gtk_table_attach_defaults (table,
 							   create_info_label (_("Speed:"), FALSE),
 							   0, 1, row, row + 1);
 	gtk_table_attach_defaults (table,
-							   create_info_label (str ? str : _("Unknown"), TRUE),
+							   speed_label,
 							   1, 2, row, row + 1);
 	g_free (str);
 	row++;


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