Re: has anyone had a chance to look at my nm-applet patch?



On 10/29/07, Katelin Ganzer <katelin ganzer gmail com> wrote:
> I was thinking that if the developers are opposed to having the
> channel displayed inside the progress bar, that maybe they would be
> okay with displaying a nicely formatted tooltip with a bunch of useful
> info?
>
> For example, maybe something like:
>
> <b>ESSID:</b> Katie's Network
> <b>Address:</b> 00:19:5B:4C:F7:D1
> <b>Protocols:</b> 802.11bg
> <b>Frequency:</b> 2.437 GHz
> <b>Channel:</b> 6
> <b>Security:</b> None
>
> I don't know what else might be considered useful, and I'm not
> necessarily volunteering to go to the lengths of writing the code to
> get/display all of that info, but it would at least be a convenient
> place for other people to add more info that they'd like t see in th
> future.
>
> Of course, I've never actually implemented tooltips in Gtk before so
> I'm not sure how to do it =)
>
> Katie
>

It turns out that tooltips in Gtk aren't that hard, so I was able to
implement this fairly easily. Attached is a patch that displays this
information in a tooltip. Hopefully this will make it so that no one
feels that too much info is displayed in the menu =)

I think my new patch makes it all nice and pretty =)

Katie
diff -urp nm-applet-0.6.5/ChangeLog nm-applet-0.6.5-kg/ChangeLog
--- nm-applet-0.6.5/ChangeLog	2007-04-19 14:01:22.000000000 -0400
+++ nm-applet-0.6.5-kg/ChangeLog	2007-10-30 20:28:50.000000000 -0400
@@ -1,3 +1,29 @@
+2007-10-20  Katelin Ganzer
+
+	* src/menu-items.c (network_menu_item_update): Set a tooltip on
+	the check_item that displays useful information such as ESSID,
+	hw_addr, frequency, channel, and signal strength.
+
+	* src/applet-dbus-devices.c (nma_dbus_net_properties_cb): Store
+	the hw_adr, frequency, and channel information on the
+	WirelessNetwork structure.
+	(channel_from_frequency): New function to map a frequency to a
+	channel.
+
+	* src/wireless-network.c (wireless_network_new): Pass a hw_addr
+	string as an argument.
+	(wireless_network_get_hw_addr): New function to get the hw_addr
+	string.
+	(wireless_network_get_channel): New function to get the stored
+	channel # that his wireles network is using.
+	(wireless_network_set_channel): New function to store the channel
+	# that this network is using.
+	(wireless_network_get_frequency): New function to get the stored
+	frequency (in GHz) that his wireles network is on.
+	(wireless_network_set_frequency): New function to store the
+	frequency (in GHz) that this network is on.
+	(wireless_network_copy): Copy the channel, frequency and hw_addr.
+
 2007-03-21  Pema Geyleg <pema geyleg gmail com>
 
 	* configure.ac: Added 'dz' to ALL_LINGUAS
diff -urp nm-applet-0.6.5/src/applet-dbus-devices.c nm-applet-0.6.5-kg/src/applet-dbus-devices.c
--- nm-applet-0.6.5/src/applet-dbus-devices.c	2007-04-19 14:01:13.000000000 -0400
+++ nm-applet-0.6.5-kg/src/applet-dbus-devices.c	2007-10-30 19:50:27.000000000 -0400
@@ -504,6 +504,68 @@ static void free_net_prop_cb_data (NetPr
 	g_free (data);
 }
 
+static struct ChanFreqRange {
+	guint8 chan_min;        /* first channel in range */
+	guint8 chan_max;        /* last channel in range */
+	guint8 chan_step;       /* channel step */
+	double freq_min;        /* first channel's frequency in GHz */
+	double freq_max;        /* last channel's frequency in GHz */
+	double freq_step;       /* frequency step in GHz */
+} chan_freq_ranges[] = {
+	{   1,  11, 1, 2.412f, 2.462f, 0.005f },
+	{  36,  64, 4, 5.18f,  5.320f, 0.020f },
+	{ 149, 165, 4, 5.745f, 5.825f, 0.020f }
+};
+
+/**
+ * channel_from_frequency:
+ * @freq: Frequency in Hz
+ *
+ * Maps the provided frequency with a channel.
+ *
+ * Returns the channel on success or 0 if unknown.
+ **/
+static guint8
+channel_from_frequency (double freq)
+{
+	struct ChanFreqRange *range = chan_freq_ranges;
+	double min, max;
+	guint8 chan;
+	int i;
+	
+	/* convert to GHz */
+	freq /= 1000000000.0;
+	
+	for (i = 0; i < G_N_ELEMENTS (chan_freq_ranges); i++, range++) {
+		max = range->freq_max + (range->freq_step / 2.0);
+		
+		if (freq > max) {
+			/* frequency not in this range, maybe in the next range? */
+			continue;
+		}
+		
+		min = range->freq_min - (range->freq_step / 2.0);
+		
+		if (freq < min) {
+			/* ranges go up in frequency, so if this is too high then later ranges won't match either */
+			break;
+		}
+		
+		max = min + range->freq_step;
+		for (chan = range->chan_min; chan <= range->chan_max; chan += range->chan_step) {
+			if (freq > min && freq < max)
+				return chan;
+			
+			min = max;
+			max += range->freq_step;
+		}
+	}
+	
+	/* unknown channel frequency */
+	
+	return 0;
+}
+
 
 /*
  * nma_dbus_net_properties_cb
@@ -566,10 +628,10 @@ static void nma_dbus_net_properties_cb (
 									DBUS_TYPE_INVALID))
 	{
 		NetworkDevice *	dev;
-
+		
 		if ((dev = nma_get_device_for_nm_path (applet->device_list, cb_data->dev_op)))
 		{
-			WirelessNetwork *	net = wireless_network_new (essid, op);
+			WirelessNetwork *	net = wireless_network_new (hw_addr, essid, op);
 			WirelessNetwork *	tmp_net;
 			char *			act_net = cb_data->act_net ? g_strdup (cb_data->act_net) : NULL;
 
@@ -584,6 +646,8 @@ static void nma_dbus_net_properties_cb (
 			wireless_network_set_mode (net, mode);
 			wireless_network_set_capabilities (net, capabilities);
 			wireless_network_set_strength (net, strength);
+			wireless_network_set_frequency (net, freq / 1000000000.0);
+			wireless_network_set_channel (net, channel_from_frequency (freq));
 			if (act_net && strlen (act_net) && (strcmp (act_net, op) == 0))
 				wireless_network_set_active (net, TRUE);
 			g_free (act_net);
diff -urp nm-applet-0.6.5/src/menu-items.c nm-applet-0.6.5-kg/src/menu-items.c
--- nm-applet-0.6.5/src/menu-items.c	2007-04-19 14:01:13.000000000 -0400
+++ nm-applet-0.6.5-kg/src/menu-items.c	2007-10-30 20:24:24.000000000 -0400
@@ -235,6 +235,58 @@ GtkCheckMenuItem *network_menu_item_get_
 	return item->check_item;
 }
 
+static char *
+markup_encode (const char *str)
+{
+	const char *s = str;
+	int extra = 0;
+	char *buf, *d;
+	
+	while (*s) {
+		switch (*s) {
+		case '<':
+		case '>':
+			extra += 4;
+			break;
+		case '&':
+			extra += 5;
+			break;
+		default:
+			break;
+		}
+		
+		s++;
+	}
+	
+	if (extra == 0)
+		return g_strdup (str);
+	
+	d = buf = g_malloc ((s - str) + extra + 1);
+	s = str;
+	
+	while (*s) {
+		switch (*s) {
+		case '<':
+			*d++ = '&'; *d++ = 'l'; *d++ = 't'; *d++ = ';';
+			break;
+		case '>':
+			*d++ = '&'; *d++ = 'g'; *d++ = 't'; *d++ = ';';
+			break;
+		case '&':
+			*d++ = '&'; *d++ = 'a'; *d++ = 'm'; *d++ = 'p'; *d++ = ';';
+			break;
+		default:
+			*d++ = *s;
+			break;
+		}
+		
+		s++;
+	}
+	
+	*d = '\0';
+	
+	return buf;
+}
 
 /* is_encrypted means that the wireless network has an encrypted
  * area, and thus we need to allow for spacing.
@@ -246,17 +298,35 @@ void network_menu_item_update (NMApplet 
 	gdouble	percent;
 	gboolean	encrypted = FALSE;
 	gboolean	adhoc = FALSE;
+	char *tooltip, *tooltip_essid;
 
 	g_return_if_fail (item != NULL);
 	g_return_if_fail (network != NULL);
 
 	display_essid = nm_menu_network_escape_essid_for_display (wireless_network_get_essid (network));
 	gtk_label_set_text (GTK_LABEL (item->label), display_essid);
-	g_free (display_essid);
-
+	
 	percent = (double) CLAMP (wireless_network_get_strength (network), 0, 100) / 100.0;
 	gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (item->progress), percent);
-
+	
+	tooltip_essid = markup_encode (display_essid);
+	g_free (display_essid);
+	
+	tooltip = g_strdup_printf (_("<b>ESSID:</b> %s\n"
+				     "<b>HW Addr:</b> %s\n"
+				     "<b>Frequency:</b> %.3f GHz\n"
+				     "<b>Channel:</b> %d\n"
+				     "<b>Strength:</b> %d%%"),
+				   tooltip_essid,
+				   wireless_network_get_hw_addr (network),
+				   wireless_network_get_frequency (network),
+				   wireless_network_get_channel (network),
+				   CLAMP (wireless_network_get_strength (network), 0, 100));
+	
+	gtk_widget_set_tooltip_markup (GTK_WIDGET (item->check_item), tooltip);
+	g_free (tooltip_essid);
+	g_free (tooltip);
+	
 	/* Deal with the encrypted icon */
 	g_object_set (item->security_image, "visible", is_encrypted, NULL);
 
diff -urp nm-applet-0.6.5/src/wireless-network.c nm-applet-0.6.5-kg/src/wireless-network.c
--- nm-applet-0.6.5/src/wireless-network.c	2007-04-19 14:01:12.000000000 -0400
+++ nm-applet-0.6.5-kg/src/wireless-network.c	2007-10-30 19:51:48.000000000 -0400
@@ -34,9 +34,12 @@ struct WirelessNetwork
 {
 	int		refcount;
 	char *	nm_path;
+	char *    hw_addr;
 	char *	essid;
 	gboolean	active;
 	gint8	strength;
+	guint8    channel;
+	float     freq;
 	int		mode;
 	int		capabilities;
 };
@@ -48,7 +51,7 @@ struct WirelessNetwork
  * Create a new wireless network structure
  *
  */
-WirelessNetwork *wireless_network_new (const char *essid, const char *nm_path)
+WirelessNetwork *wireless_network_new (const char *hw_addr, const char *essid, const char *nm_path)
 {
 	WirelessNetwork *net;
 
@@ -56,6 +59,7 @@ WirelessNetwork *wireless_network_new (c
 	g_return_val_if_fail (nm_path != NULL, NULL);
 
 	net = g_malloc0 (sizeof (WirelessNetwork));
+	net->hw_addr = g_strdup (hw_addr);
 	net->essid = g_strdup (essid);
 	net->nm_path = g_strdup (nm_path);
 
@@ -78,11 +82,14 @@ WirelessNetwork *wireless_network_copy (
 	net = g_malloc0 (sizeof (WirelessNetwork));
 	net->refcount = 1;
 	net->nm_path = g_strdup (src->nm_path);
+	net->hw_addr = g_strdup (src->hw_addr);
 	net->essid = g_strdup (src->essid);
 	net->active = src->active;
 	net->capabilities = src->capabilities;
 	net->strength = src->strength;
-
+	net->channel = src->channel;
+	net->freq = src->freq;
+	
 	return net;
 }
 
@@ -115,6 +122,7 @@ void wireless_network_unref (WirelessNet
 	if (net->refcount < 1)
 	{
 		g_free (net->nm_path);
+		g_free (net->hw_addr);
 		g_free (net->essid);
 		g_free (net);
 	}
@@ -139,6 +147,16 @@ void wireless_network_set_active (Wirele
 }
 
 /*
+ * Accessors for hw_addr
+ */
+const char *wireless_network_get_hw_addr (WirelessNetwork *net)
+{
+	g_return_val_if_fail (net != NULL, FALSE);
+
+	return net->hw_addr;
+}
+
+/*
  * Accessors for essid
  */
 const char *wireless_network_get_essid (WirelessNetwork *net)
@@ -210,3 +228,37 @@ void wireless_network_set_strength (Wire
 	net->strength = strength;
 }
 
+/*
+ * Accessors for frequency (in GHz)
+ */
+float wireless_network_get_frequency (WirelessNetwork *net)
+{
+	g_return_val_if_fail (net != NULL, 0.0f);
+
+	return net->freq;
+}
+
+void wireless_network_set_frequency (WirelessNetwork *net, float freq)
+{
+	g_return_if_fail (net != NULL);
+
+	net->freq = freq;
+}
+
+
+/*
+ * Accessors for channel
+ */
+guint8 wireless_network_get_channel (WirelessNetwork *net)
+{
+	g_return_val_if_fail (net != NULL, 0);
+
+	return net->channel;
+}
+
+void wireless_network_set_channel (WirelessNetwork *net, guint8 channel)
+{
+	g_return_if_fail (net != NULL);
+
+	net->channel = channel;
+}
diff -urp nm-applet-0.6.5/src/wireless-network.h nm-applet-0.6.5-kg/src/wireless-network.h
--- nm-applet-0.6.5/src/wireless-network.h	2007-04-19 14:01:12.000000000 -0400
+++ nm-applet-0.6.5-kg/src/wireless-network.h	2007-10-30 19:45:49.000000000 -0400
@@ -25,7 +25,7 @@
 
 typedef struct WirelessNetwork WirelessNetwork;
 
-WirelessNetwork *	wireless_network_new			(const char *essid, const char *nm_path);
+WirelessNetwork *	wireless_network_new			(const char *hw_addr, const char *essid, const char *nm_path);
 WirelessNetwork *	wireless_network_copy			(WirelessNetwork *src);
 
 void				wireless_network_ref			(WirelessNetwork *net);
@@ -34,6 +34,8 @@ void				wireless_network_unref			(Wirele
 gboolean			wireless_network_get_active		(WirelessNetwork *net);
 void				wireless_network_set_active		(WirelessNetwork *net, gboolean active);
 
+const char *        wireless_network_get_hw_addr       (WirelessNetwork *net);
+
 const char *		wireless_network_get_essid		(WirelessNetwork *net);
 
 const char *		wireless_network_get_nm_path		(WirelessNetwork *net);
@@ -47,4 +49,10 @@ void				wireless_network_set_mode		(Wire
 gint8			wireless_network_get_strength		(WirelessNetwork *net);
 void				wireless_network_set_strength		(WirelessNetwork *net, gint8 strength);
 
+float			wireless_network_get_frequency	(WirelessNetwork *net);
+void				wireless_network_set_frequency	(WirelessNetwork *net, float freq);
+
+guint8			wireless_network_get_channel		(WirelessNetwork *net);
+void				wireless_network_set_channel		(WirelessNetwork *net, guint8 channel);
+
 #endif


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