summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Winship <danw@gnome.org>2014-09-19 16:51:07 -0400
committerDan Winship <danw@gnome.org>2014-09-27 11:49:15 -0400
commite684f363654892754a12ec172a14689c425dad88 (patch)
tree31baa9431e1db412f48c8fce20f035beed6e9eaf
parent1b74e77f9f5062d7542c367baa75185c27093d95 (diff)
downloadNetworkManager-e684f363654892754a12ec172a14689c425dad88.tar.gz
libnm-core: add nm_utils_wifi_strength_bars(), use it in nmcli and nmtui
Add nm_utils_wifi_strength_bars(), which figures out whether the terminal can display graphical wifi strength bars, and converts a numerical value to the appropriate Unicode or ASCII characters. This also now takes into consideration the fact that the console font doesn't contain all of the necessary characters, so we can't display the graphical bars there. (rh #1131491)
-rw-r--r--clients/cli/devices.c11
-rw-r--r--clients/tui/nmt-connect-connection-list.c31
-rw-r--r--libnm-core/nm-utils.c60
-rw-r--r--libnm-core/nm-utils.h2
-rw-r--r--libnm/libnm.ver1
5 files changed, 65 insertions, 40 deletions
diff --git a/clients/cli/devices.c b/clients/cli/devices.c
index 2d512ee212..fc88309212 100644
--- a/clients/cli/devices.c
+++ b/clients/cli/devices.c
@@ -501,11 +501,6 @@ fill_output_access_point (gpointer data, gpointer user_data)
*bitrate_str, *strength_str, *wpa_flags_str, *rsn_flags_str;
GString *security_str;
char *ap_name;
- const char *sig_level_0 = "____";
- const char *sig_level_1 = "▂___";
- const char *sig_level_2 = "▂▄__";
- const char *sig_level_3 = "▂▄▆_";
- const char *sig_level_4 = "▂▄▆█";
const char *sig_bars;
if (info->active_bssid) {
@@ -540,11 +535,7 @@ fill_output_access_point (gpointer data, gpointer user_data)
strength_str = g_strdup_printf ("%u", strength);
wpa_flags_str = ap_wpa_rsn_flags_to_string (wpa_flags);
rsn_flags_str = ap_wpa_rsn_flags_to_string (rsn_flags);
- sig_bars = strength > 80 ? sig_level_4 :
- strength > 55 ? sig_level_3 :
- strength > 30 ? sig_level_2 :
- strength > 5 ? sig_level_1 :
- sig_level_0;
+ sig_bars = nm_utils_wifi_strength_bars (strength);
security_str = g_string_new (NULL);
diff --git a/clients/tui/nmt-connect-connection-list.c b/clients/tui/nmt-connect-connection-list.c
index 98e46ba14f..a596364533 100644
--- a/clients/tui/nmt-connect-connection-list.c
+++ b/clients/tui/nmt-connect-connection-list.c
@@ -61,8 +61,6 @@ typedef struct {
GSList *nmt_devices;
} NmtConnectConnectionListPrivate;
-static const char *strength_full, *strength_high, *strength_med, *strength_low, *strength_none;
-
/**
* nmt_connect_connection_list_new:
*
@@ -527,16 +525,7 @@ nmt_connect_connection_list_rebuild (NmtConnectConnectionList *list)
if (nmtconn->ap) {
guint8 strength = nm_access_point_get_strength (nmtconn->ap);
- if (strength > 80)
- strength_col = strength_full;
- else if (strength > 55)
- strength_col = strength_high;
- else if (strength > 30)
- strength_col = strength_med;
- else if (strength > 5)
- strength_col = strength_low;
- else
- strength_col = strength_none;
+ strength_col = nm_utils_wifi_strength_bars (strength);
} else
strength_col = NULL;
@@ -608,30 +597,12 @@ static void
nmt_connect_connection_list_class_init (NmtConnectConnectionListClass *list_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (list_class);
- char *tmp;
g_type_class_add_private (list_class, sizeof (NmtConnectConnectionListPrivate));
/* virtual methods */
object_class->constructed = nmt_connect_connection_list_constructed;
object_class->finalize = nmt_connect_connection_list_finalize;
-
- /* globals */
- tmp = nmt_newt_locale_from_utf8 ("\342\226\202\342\226\204\342\226\206\342\226\210");
- if (*tmp) {
- strength_full = /* ▂▄▆█ */ "\342\226\202\342\226\204\342\226\206\342\226\210";
- strength_high = /* ▂▄▆_ */ "\342\226\202\342\226\204\342\226\206_";
- strength_med = /* ▂▄__ */ "\342\226\202\342\226\204__";
- strength_low = /* ▂___ */ "\342\226\202___";
- strength_none = /* ____ */ "____";
- } else {
- strength_full = "****";
- strength_high = "*** ";
- strength_med = "** ";
- strength_low = "* ";
- strength_none = " ";
- }
- g_free (tmp);
}
/**
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index 617b0b4df8..f1e592f535 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -2101,6 +2101,66 @@ nm_utils_wifi_is_channel_valid (guint32 channel, const char *band)
}
/**
+ * nm_utils_wifi_strength_bars:
+ * @strength: the access point strength, from 0 to 100
+ *
+ * Converts @strength into a 4-character-wide graphical representation of
+ * strength suitable for printing to stdout. If the current locale and terminal
+ * support it, this will use unicode graphics characters to represent
+ * "bars". Otherwise it will use 0 to 4 asterisks.
+ *
+ * Returns: the graphical representation of the access point strength
+ */
+const char *
+nm_utils_wifi_strength_bars (guint8 strength)
+{
+ static const char *strength_full, *strength_high, *strength_med, *strength_low, *strength_none;
+
+ if (G_UNLIKELY (strength_full == NULL)) {
+ gboolean can_show_graphics = TRUE;
+ char *locale_str;
+
+ if (!g_get_charset (NULL)) {
+ /* Non-UTF-8 locale */
+ locale_str = g_locale_from_utf8 ("\342\226\202\342\226\204\342\226\206\342\226\210", -1, NULL, NULL, NULL);
+ if (locale_str)
+ g_free (locale_str);
+ else
+ can_show_graphics = FALSE;
+ }
+
+ /* The linux console font doesn't have these characters */
+ if (g_strcmp0 (g_getenv ("TERM"), "linux") == 0)
+ can_show_graphics = FALSE;
+
+ if (can_show_graphics) {
+ strength_full = /* ▂▄▆█ */ "\342\226\202\342\226\204\342\226\206\342\226\210";
+ strength_high = /* ▂▄▆_ */ "\342\226\202\342\226\204\342\226\206_";
+ strength_med = /* ▂▄__ */ "\342\226\202\342\226\204__";
+ strength_low = /* ▂___ */ "\342\226\202___";
+ strength_none = /* ____ */ "____";
+ } else {
+ strength_full = "****";
+ strength_high = "*** ";
+ strength_med = "** ";
+ strength_low = "* ";
+ strength_none = " ";
+ }
+ }
+
+ if (strength > 80)
+ return strength_full;
+ else if (strength > 55)
+ return strength_high;
+ else if (strength > 30)
+ return strength_med;
+ else if (strength > 5)
+ return strength_low;
+ else
+ return strength_none;
+}
+
+/**
* nm_utils_hwaddr_len:
* @type: the type of address; either %ARPHRD_ETHER or %ARPHRD_INFINIBAND
*
diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h
index 3bb4b9ff49..7402cd921d 100644
--- a/libnm-core/nm-utils.h
+++ b/libnm-core/nm-utils.h
@@ -131,6 +131,8 @@ guint32 nm_utils_wifi_channel_to_freq (guint32 channel, const char *band);
guint32 nm_utils_wifi_find_next_channel (guint32 channel, int direction, char *band);
gboolean nm_utils_wifi_is_channel_valid (guint32 channel, const char *band);
+const char *nm_utils_wifi_strength_bars (guint8 strength);
+
/**
* NM_UTILS_HWADDR_LEN_MAX:
*
diff --git a/libnm/libnm.ver b/libnm/libnm.ver
index 58da6c3be6..98a80082df 100644
--- a/libnm/libnm.ver
+++ b/libnm/libnm.ver
@@ -931,6 +931,7 @@ global:
nm_utils_wifi_find_next_channel;
nm_utils_wifi_freq_to_channel;
nm_utils_wifi_is_channel_valid;
+ nm_utils_wifi_strength_bars;
nm_utils_wpa_psk_valid;
nm_vlan_flags_get_type;
nm_vlan_priority_map_get_type;