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



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




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