[gnome-control-center] shell: Add PrettyHostname to SSID helper



commit 1ce4af426763fdbd09873f68636afaf248ffd69e
Author: Bastien Nocera <hadess hadess net>
Date:   Fri Jun 10 16:15:52 2016 +0200

    shell: Add PrettyHostname to SSID helper
    
    For use in the network panel. Comes with tests!

 po/POTFILES.in          |    1 +
 shell/Makefile.am       |    3 +-
 shell/hostname-helper.c |   48 +++++++++++++++++++++++++++++++++++++++++++++++
 shell/hostname-helper.h |    1 +
 shell/ssids-test.txt    |    3 ++
 shell/test-hostname.c   |   41 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 96 insertions(+), 1 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ced3fee..1449c20 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -215,4 +215,5 @@ shell/cc-application.c
 shell/cc-window.c
 shell/gnome-control-center.desktop.in.in
 [type: gettext/glade]shell/help-overlay.ui
+shell/hostname-helper.c
 [type: gettext/glade]shell/window.ui
diff --git a/shell/Makefile.am b/shell/Makefile.am
index 45819f0..d10520e 100644
--- a/shell/Makefile.am
+++ b/shell/Makefile.am
@@ -5,6 +5,7 @@ SUBDIRS = appdata alt
 AM_CPPFLAGS =                                  \
        -DGNOMELOCALEDIR="\"$(datadir)/locale\""\
        -DTEST_SRCDIR=\""$(srcdir)/"\"          \
+       -DTEST_TOPSRCDIR=\""$(top_srcdir)/"\"   \
        -I$(top_srcdir)                         \
        $(SHELL_CFLAGS)                         \
        $(CHEESE_CFLAGS)                        \
@@ -164,6 +165,6 @@ noinst_PROGRAMS += test-hostname
 test_hostname_SOURCES = hostname-helper.c hostname-helper.h test-hostname.c
 test_hostname_LDADD = $(PANEL_LIBS) $(INFO_PANEL_LIBS)
 
-EXTRA_DIST += hostnames-test.txt
+EXTRA_DIST += hostnames-test.txt ssids-test.txt
 
 -include $(top_srcdir)/git.mk
diff --git a/shell/hostname-helper.c b/shell/hostname-helper.c
index 3fffb95..45baf51 100644
--- a/shell/hostname-helper.c
+++ b/shell/hostname-helper.c
@@ -17,6 +17,7 @@
  */
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <string.h>
 
 #include "hostname-helper.h"
@@ -168,3 +169,50 @@ bail:
        return g_strdup ("localhost");
 }
 #undef CHECK
+
+/* Max length of an SSID in bytes */
+#define SSID_MAX_LEN 32
+char *
+pretty_hostname_to_ssid (const char *pretty)
+{
+       const char *p, *prev;
+       char *ret = NULL;
+
+       if (pretty == NULL) {
+               pretty = g_get_host_name ();
+               if (g_strcmp0 (pretty, "localhost") == 0)
+                       pretty = NULL;
+       }
+
+       if (pretty == NULL) {
+               /* translators: This is the default hotspot name, need to be less than 32-bytes */
+               ret = g_strdup (C_("hotspot", "Hotspot"));
+               g_assert (strlen (ret) <= 32);
+               return ret;
+       }
+
+       g_return_val_if_fail (g_utf8_validate (pretty, -1, NULL), NULL);
+
+       p = pretty;
+       prev = NULL;
+       while ((p = g_utf8_find_next_char (p, NULL)) != NULL) {
+               if (p == prev)
+                       break;
+
+               if (p - pretty > 32) {
+                       ret = g_strndup (pretty, prev - pretty);
+                       break;
+               }
+               if (p - pretty == 32) {
+                       ret = g_strndup (pretty, p - pretty);
+                       break;
+               }
+
+               prev = p;
+       }
+
+       if (ret == NULL)
+               ret = g_strdup (pretty);
+
+       return ret;
+}
diff --git a/shell/hostname-helper.h b/shell/hostname-helper.h
index 2ed2c5f..9f96c12 100644
--- a/shell/hostname-helper.h
+++ b/shell/hostname-helper.h
@@ -18,3 +18,4 @@
 
 char *pretty_hostname_to_static (const char *pretty,
                                 gboolean    for_display);
+char *pretty_hostname_to_ssid   (const char *pretty);
diff --git a/shell/ssids-test.txt b/shell/ssids-test.txt
new file mode 100644
index 0000000..0545437
--- /dev/null
+++ b/shell/ssids-test.txt
@@ -0,0 +1,3 @@
+GNOME  GNOME
+0123456789abcdefghijklmnopqrstuvwxyz   0123456789abcdefghijklmnopqrstuv
+レナート   レナート
diff --git a/shell/test-hostname.c b/shell/test-hostname.c
index d8cee19..3df2930 100644
--- a/shell/test-hostname.c
+++ b/shell/test-hostname.c
@@ -70,6 +70,46 @@ test_hostname (void)
        g_free (contents);
 }
 
+static void
+test_ssid (void)
+{
+       char *contents;
+       guint i;
+       char **lines;
+
+       if (g_file_get_contents (TEST_SRCDIR "/ssids-test.txt", &contents, NULL, NULL) == FALSE) {
+               g_warning ("Failed to load '%s'", TEST_SRCDIR "/ssids-test.txt");
+               g_test_fail ();
+               return;
+       }
+
+       lines = g_strsplit (contents, "\n", -1);
+       if (lines == NULL) {
+               g_warning ("Test file is empty");
+               g_test_fail ();
+               return;
+       }
+
+       for (i = 0; lines[i] != NULL; i++) {
+               char *ssid;
+               char **items;
+
+               if (*lines[i] == '#')
+                       continue;
+               if (*lines[i] == '\0')
+                       break;
+
+               items = g_strsplit (lines[i], "\t", -1);
+               ssid = pretty_hostname_to_ssid (items[0]);
+               g_assert_cmpstr (ssid, ==, items[1]);
+               g_free (ssid);
+               g_strfreev (items);
+       }
+
+       g_strfreev (lines);
+       g_free (contents);
+}
+
 int main (int argc, char **argv)
 {
        char *locale;
@@ -90,6 +130,7 @@ int main (int argc, char **argv)
        g_test_init (&argc, &argv, NULL);
 
        g_test_add_func ("/shell/hostname", test_hostname);
+       g_test_add_func ("/shell/ssid", test_ssid);
 
        return g_test_run ();
 }


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