Sort the list of interface names alphabetically for the preferences screen.
authorRoy Marples <roy@marples.name>
Wed, 19 Nov 2014 17:50:06 +0000 (17:50 +0000)
committerRoy Marples <roy@marples.name>
Wed, 19 Nov 2014 17:50:06 +0000 (17:50 +0000)
src/dhcpcd-gtk/prefs.c
src/dhcpcd-qt/dhcpcd-preferences.cpp
src/libdhcpcd/config.c
src/libdhcpcd/dhcpcd.c
src/libdhcpcd/dhcpcd.h

index 2bbd16623a25016f765dca2a3843339ba5b3a73c..fac72047887c07e8090b05d5a24b9c2badc0cab4 100644 (file)
@@ -37,6 +37,7 @@ static GtkWidget *autoconf, *address, *router, *dns_servers, *dns_search;
 static DHCPCD_OPTION *config;
 static char *block, *name;
 static DHCPCD_IF *iface;
+static char **ifaces;
 
 static void
 config_err_dialog(DHCPCD_CONNECTION *con, bool writing, const char *txt)
@@ -210,12 +211,13 @@ static GSList *
 list_interfaces(DHCPCD_CONNECTION *con)
 {
        GSList *list;
-       DHCPCD_IF *i;
+       char **i;
 
        list = NULL;
-       for (i = dhcpcd_interfaces(con); i; i = i->next)
-               if (strcmp(i->type, "link") == 0)
-                       list = g_slist_append(list, UNCONST(i->ifname));
+       dhcpcd_freev(ifaces);
+       ifaces = dhcpcd_interface_names_sorted(con);
+       for (i = ifaces; i && *i; i++)
+               list = g_slist_append(list, *i);
        return list;
 }
 
@@ -306,7 +308,7 @@ blocks_on_change(GtkWidget *widget, gpointer data)
        }
        gtk_widget_set_sensitive(names, n);
        g_slist_free(new_names);
-       dhcpcd_config_blocks_free(list);
+       dhcpcd_freev(list);
 }
 
 static void
@@ -466,7 +468,10 @@ on_destroy(_unused GObject *o, gpointer data)
        }
        dhcpcd_config_free(config);
        config = NULL;
+       dhcpcd_freev(ifaces);
+       ifaces = NULL;
        dialog = NULL;
+
 }
 
 static void
index 86708c41a22e53c379386a17a34e55561631aee7..cb01c1f165860d7ff64a56d34ed0f7b258b7d4fb 100644 (file)
@@ -185,23 +185,21 @@ void DhcpcdPreferences::listBlocks(const QString &txt)
            txt.toLower().toAscii());
 
        if (txt == "interface") {
-               DHCPCD_IF *i;
+               char **ifaces, **i;
 
                blocks->addItem(tr("Select an interface"));
-               for (i = dhcpcd_interfaces(parent->getConnection());
-                   i; i = i->next)
-               {
-                       if (strcmp(i->type, "link") == 0) {
-                               for (lp = list; lp && *lp; lp++) {
-                                       if (strcmp(i->ifname, *lp) == 0)
-                                               break;
-                               }
-                               icon = DhcpcdQt::getIcon("devices",
-                                   lp && *lp ?
-                                   "document-save" : "document-new");
-                               blocks->addItem(icon, i->ifname);
+               ifaces = dhcpcd_interface_names_sorted(parent->getConnection());
+               for (i = ifaces; i && *i; i++) {
+                       for (lp = list; lp && *lp; lp++) {
+                               if (strcmp(*i, *lp) == 0)
+                                       break;
                        }
+                       icon = DhcpcdQt::getIcon("actions",
+                           lp && *lp ?
+                           "document-save" : "document-new");
+                       blocks->addItem(icon, *i);
                }
+               dhcpcd_freev(ifaces);
        } else {
                QList<DhcpcdWi *> *wis = parent->getWis();
 
@@ -215,7 +213,7 @@ void DhcpcdPreferences::listBlocks(const QString &txt)
                                        if (strcmp(scan->ssid, *lp) == 0)
                                                break;
                                }
-                               icon = DhcpcdQt::getIcon("devices",
+                               icon = DhcpcdQt::getIcon("actions",
                                    lp && *lp ?
                                    "document-save" : "document-new");
                                blocks->addItem(icon, scan->ssid);
@@ -223,7 +221,7 @@ void DhcpcdPreferences::listBlocks(const QString &txt)
                }
        }
 
-       dhcpcd_config_blocks_free(list);
+       dhcpcd_freev(list);
 
        /* Now make the 1st item unselectable and reconnect */
        qobject_cast<QStandardItemModel *>
index 3fc6ed74ac3122593119ea050caa42624e29dbba..92aad29f6225faac19f5fcb918414b3569d15f91 100644 (file)
@@ -429,15 +429,3 @@ dhcpcd_config_blocks(DHCPCD_CONNECTION *con, const char *block)
        config(con, ACT_LIST, block, NULL, NULL, &blocks);
        return blocks;
 }
-
-void
-dhcpcd_config_blocks_free(char **blocks)
-{
-       char **b;
-
-       if (blocks) {
-               for (b = blocks; *b; b++)
-                       free(*b);
-               free(blocks);
-       }
-}
index 4b9b61e02b7f13c9936f8095d27838deb1e58879..6c3be52c7f86999f8776bf256a7762acceb0c2c1 100644 (file)
@@ -511,6 +511,80 @@ dhcpcd_interfaces(DHCPCD_CONNECTION *con)
        return con->interfaces;
 }
 
+char **
+dhcpcd_interface_names(DHCPCD_CONNECTION *con, size_t *nnames)
+{
+       char **names;
+       size_t n;
+       DHCPCD_IF *i;
+
+       assert(con);
+       if (con->interfaces == NULL)
+               return NULL;
+
+       n = 0;
+       for (i = con->interfaces; i; i = i->next) {
+               if (strcmp(i->type, "link") == 0)
+                       n++;
+       }
+       names = malloc(sizeof(char *) * (n + 1));
+       if (names == NULL)
+               return NULL;
+       n = 0;
+       for (i = con->interfaces; i; i = i->next) {
+               if (strcmp(i->type, "link") == 0) {
+                       names[n] = strdup(i->ifname);
+                       if (names[n] == NULL) {
+                               dhcpcd_freev(names);
+                               return NULL;
+                       }
+                       n++;
+               }
+       }
+       names[n] = NULL;
+       if (nnames)
+               *nnames = n;
+
+       return names;
+}
+
+void
+dhcpcd_freev(char **argv)
+{
+       char **v;
+
+       if (argv) {
+               for (v = argv; *v; v++)
+                       free(*v);
+               free(argv);
+       }
+}
+
+static int
+dhcpcd_cmpstring(const void *p1, const void *p2)
+{
+       const char *s1, *s2;
+       int cmp;
+
+       s1 = *(char * const *)p1;
+       s2 = *(char * const *)p2;
+       if ((cmp = strcasecmp(s1, s2)) == 0)
+               cmp = strcmp(s1, s2);
+       return cmp;
+}
+
+char **
+dhcpcd_interface_names_sorted(DHCPCD_CONNECTION *con)
+{
+       char **names;
+       size_t nnames;
+
+       names = dhcpcd_interface_names(con, &nnames);
+       if (names)
+               qsort(names, nnames, sizeof(char *), dhcpcd_cmpstring);
+       return names;
+}
+
 DHCPCD_IF *
 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
 {
index 8b898e5eeb26d95dfb340187e0973a3fffea9942..0f491d2b04421ddc16e51bd7a85c125a825c7020 100644 (file)
@@ -195,6 +195,9 @@ int dhcpcd_get_fd(DHCPCD_CONNECTION *);
 bool dhcpcd_privileged(DHCPCD_CONNECTION *);
 void dhcpcd_dispatch(DHCPCD_CONNECTION *);
 DHCPCD_IF * dhcpcd_interfaces(DHCPCD_CONNECTION *);
+char **dhcpcd_interface_names(DHCPCD_CONNECTION *, size_t *);
+void dhcpcd_freev(char **);
+char **dhcpcd_interface_names_sorted(DHCPCD_CONNECTION *);
 DHCPCD_IF * dhcpcd_get_if(DHCPCD_CONNECTION *, const char *, const char *);
 DHCPCD_CONNECTION * dhcpcd_if_connection(DHCPCD_IF *);
 const char *dhcpcd_get_value(const DHCPCD_IF *, const char *);
@@ -254,7 +257,6 @@ bool dhcpcd_wpa_network_set(DHCPCD_WPA *, int, const char *, const char *);
 int dhcpcd_wpa_configure_psk(DHCPCD_WPA *w, DHCPCD_WI_SCAN *s, const char *p);
 
 char ** dhcpcd_config_blocks(DHCPCD_CONNECTION *, const char *);
-void dhcpcd_config_blocks_free(char **);
 DHCPCD_OPTION *dhcpcd_config_read(DHCPCD_CONNECTION *,
     const char *, const char *);
 void dhcpcd_config_free(DHCPCD_OPTION *);