From 9e4de97967c67e41eaab288e0bd3bfabbda20797 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Fri, 15 Dec 2017 16:46:07 +0100 Subject: [PATCH] libnm-core: move detection of UTF-8 capable terminals to clients/ Having it in libnm doesn't make any sense and prevents using it for more internal functionality. Too bad nm_utils_wifi_strength_bars() is already a public API. No problem -- replace it with a compatible yet dumber equivalent. --- clients/cli/devices.c | 2 +- clients/common/nm-client-utils.c | 56 +++++++++++++++++++++++ clients/common/nm-client-utils.h | 2 + clients/tui/nmt-connect-connection-list.c | 5 +- libnm-core/nm-utils.c | 53 +++++---------------- 5 files changed, 73 insertions(+), 45 deletions(-) diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 44ed7b35d..a43861fae 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -831,7 +831,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 = nm_utils_wifi_strength_bars (strength); + sig_bars = nmc_wifi_strength_bars (strength); security_str = g_string_new (NULL); diff --git a/clients/common/nm-client-utils.c b/clients/common/nm-client-utils.c index 398c94cf1..dd46ecfcc 100644 --- a/clients/common/nm-client-utils.c +++ b/clients/common/nm-client-utils.c @@ -20,6 +20,7 @@ #include "nm-default.h" #include "nm-client-utils.h" +#include "nm-utils.h" #include "nm-device-bond.h" #include "nm-device-bridge.h" @@ -505,3 +506,58 @@ nmc_activation_get_effective_state (NMActiveConnection *active, return ac_state; } + +static gboolean +can_show_graphics (void) +{ + static gboolean can_show_graphics_set = FALSE; + gboolean can_show_graphics = TRUE; + char *locale_str; + + if (G_LIKELY (can_show_graphics_set)) + return can_show_graphics; + + 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 typically doesn't have characters we need */ + if (g_strcmp0 (g_getenv ("TERM"), "linux") == 0) + can_show_graphics = FALSE; + + return can_show_graphics; +} + +/** + * nmc_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 * +nmc_wifi_strength_bars (guint8 strength) +{ + if (!can_show_graphics ()) + return nm_utils_wifi_strength_bars (strength); + + if (strength > 80) + return /* ▂▄▆█ */ "\342\226\202\342\226\204\342\226\206\342\226\210"; + else if (strength > 55) + return /* ▂▄▆_ */ "\342\226\202\342\226\204\342\226\206_"; + else if (strength > 30) + return /* ▂▄__ */ "\342\226\202\342\226\204__"; + else if (strength > 5) + return /* ▂___ */ "\342\226\202___"; + else + return /* ____ */ "____"; +} diff --git a/clients/common/nm-client-utils.h b/clients/common/nm-client-utils.h index 5b2ba6783..37d472370 100644 --- a/clients/common/nm-client-utils.h +++ b/clients/common/nm-client-utils.h @@ -54,4 +54,6 @@ NMActiveConnectionState nmc_activation_get_effective_state (NMActiveConnection * NMDevice *device, const char **reason); +const char *nmc_wifi_strength_bars (guint8 strength); + #endif /* __NM_CLIENT_UTILS_H__ */ diff --git a/clients/tui/nmt-connect-connection-list.c b/clients/tui/nmt-connect-connection-list.c index 263bc96c8..7cecf7e22 100644 --- a/clients/tui/nmt-connect-connection-list.c +++ b/clients/tui/nmt-connect-connection-list.c @@ -13,7 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . * - * Copyright 2013 Red Hat, Inc. + * Copyright 2013 - 2017 Red Hat, Inc. */ /** @@ -32,6 +32,7 @@ #include "nmtui.h" #include "nmt-connect-connection-list.h" +#include "nm-client-utils.h" G_DEFINE_TYPE (NmtConnectConnectionList, nmt_connect_connection_list, NMT_TYPE_NEWT_LISTBOX) @@ -525,7 +526,7 @@ nmt_connect_connection_list_rebuild (NmtConnectConnectionList *list) if (nmtconn->ap) { guint8 strength = nm_access_point_get_strength (nmtconn->ap); - strength_col = nm_utils_wifi_strength_bars (strength); + strength_col = nmc_wifi_strength_bars (strength); } else strength_col = NULL; diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 9c8e0bcb0..f845d14cd 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -3438,59 +3438,28 @@ nm_utils_wifi_5ghz_freqs (void) * @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. + * strength suitable for printing to stdout. + * + * Previous versions used to take a guess at the terminal type and possibly + * return a wide UTF-8 encoded string. Now it always returns a 7-bit + * clean strings of one to 0 to 4 asterisks. Users that actually need + * the functionality are encouraged to make their implementations instead. * * 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; + return "****"; else if (strength > 55) - return strength_high; + return "*** "; else if (strength > 30) - return strength_med; + return "** "; else if (strength > 5) - return strength_low; + return "* "; else - return strength_none; + return " "; } /**