From: Roy Marples Date: Wed, 19 Nov 2014 17:50:06 +0000 (+0000) Subject: Sort the list of interface names alphabetically for the preferences screen. X-Git-Tag: dhcpcd-ui-0.7.5~34 X-Git-Url: https://roy.marples.name/git Sort the list of interface names alphabetically for the preferences screen. --- diff --git a/src/dhcpcd-gtk/prefs.c b/src/dhcpcd-gtk/prefs.c index 2bbd166..fac7204 100644 --- a/src/dhcpcd-gtk/prefs.c +++ b/src/dhcpcd-gtk/prefs.c @@ -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 diff --git a/src/dhcpcd-qt/dhcpcd-preferences.cpp b/src/dhcpcd-qt/dhcpcd-preferences.cpp index 86708c4..cb01c1f 100644 --- a/src/dhcpcd-qt/dhcpcd-preferences.cpp +++ b/src/dhcpcd-qt/dhcpcd-preferences.cpp @@ -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 *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 diff --git a/src/libdhcpcd/config.c b/src/libdhcpcd/config.c index 3fc6ed7..92aad29 100644 --- a/src/libdhcpcd/config.c +++ b/src/libdhcpcd/config.c @@ -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); - } -} diff --git a/src/libdhcpcd/dhcpcd.c b/src/libdhcpcd/dhcpcd.c index 4b9b61e..6c3be52 100644 --- a/src/libdhcpcd/dhcpcd.c +++ b/src/libdhcpcd/dhcpcd.c @@ -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) { diff --git a/src/libdhcpcd/dhcpcd.h b/src/libdhcpcd/dhcpcd.h index 8b898e5..0f491d2 100644 --- a/src/libdhcpcd/dhcpcd.h +++ b/src/libdhcpcd/dhcpcd.h @@ -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 *);