Fix a garbage condition
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-preferences.cpp
index 02974a7a912c93f9fd7bf5513561c7986bb5df52..3d8b6dff3a42197a8f6b74312c72c8d789a8bd6d 100644 (file)
@@ -132,7 +132,7 @@ DhcpcdPreferences::DhcpcdPreferences(DhcpcdQt *parent)
        layout->addWidget(buttons);
 
        QIcon wired = DhcpcdQt::getIcon("devices", "network-wired");
-       what->addItem(wired, tr("Interface"));
+       what->addItem(wired, tr("interface"));
        QIcon wireless = DhcpcdQt::getIcon("devices", "network-wireless");
        what->addItem(wireless, tr("SSID"));
 
@@ -171,28 +171,35 @@ void DhcpcdPreferences::closeEvent(QCloseEvent *e)
 
 void DhcpcdPreferences::listBlocks(const QString &txt)
 {
+       char **list, **lp;
+       QIcon icon;
 
        /* clear and then disconnect so we trigger a save */
        blocks->clear();
        blocks->disconnect(this);
-       
+
        free(eWhat);
        eWhat = strdup(txt.toLower().toAscii());
 
-       if (txt == "Interface") {
-               DHCPCD_IF *i;
+       list = dhcpcd_config_blocks(parent->getConnection(),
+           txt.toLower().toAscii());
+
+       if (txt == "interface") {
+               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) {
-                               QIcon icon = DhcpcdQt::getIcon("devices",
-                                   i->wireless ?
-                                   "network-wireless" : "network-wired");
-                               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();
 
@@ -202,19 +209,23 @@ void DhcpcdPreferences::listBlocks(const QString &txt)
                        DhcpcdWi *wi = wis->at(i);
 
                        for (scan = wi->getScans(); scan; scan = scan->next) {
-                               QIcon icon;
-
-                               icon = DhcpcdQt::getIcon(
-                                   scan->flags[0] == '\0' ?"devices" :"status",
-                                   scan->flags[0] == '\0' ?"network-wireless" :
-                                       "network-wireless-encrypted");
+                               for (lp = list; lp && *lp; lp++) {
+                                       if (strcmp(scan->ssid, *lp) == 0)
+                                               break;
+                               }
+                               icon = DhcpcdQt::getIcon("actions",
+                                   lp && *lp ?
+                                   "document-save" : "document-new");
                                blocks->addItem(icon, scan->ssid);
                        }
                }
        }
 
+       dhcpcd_freev(list);
+
        /* Now make the 1st item unselectable and reconnect */
-       qobject_cast<QStandardItemModel *>(blocks->model())->item(0)->setEnabled(false);
+       qobject_cast<QStandardItemModel *>
+           (blocks->model())->item(0)->setEnabled(false);
        connect(blocks, SIGNAL(currentIndexChanged(const QString &)),
            this, SLOT(showBlock(const QString &)));
 
@@ -311,7 +322,7 @@ bool DhcpcdPreferences::makeConfig()
        a = autoConf->isChecked();
        ret = true;
        if (iface && iface->flags & IFF_POINTOPOINT)
-               setOption("ip_address=", a ? NULL : ns, &ret);
+               setOption("ip_address=", a ? NULL : ns, &ret);
         else {
                val = getString(ip);
                 setOption("inform", a ? val : NULL, &ret);
@@ -340,6 +351,7 @@ bool DhcpcdPreferences::writeConfig(bool *cancel)
                *cancel = true;
                return false;
        case QMessageBox::Discard:
+               *cancel = true;
                return true;
        default:
                break;
@@ -420,7 +432,7 @@ bool DhcpcdPreferences::tryRebind(const char *ifname)
 
        if (dhcpcd_rebind(parent->getConnection(), ifname) == 0)
                return true;
-       
+
        qCritical("dhcpcd_rebind: %s", strerror(errno));
        QMessageBox::critical(this,
            tr("Rebind failed"),
@@ -428,7 +440,7 @@ bool DhcpcdPreferences::tryRebind(const char *ifname)
            .arg(ifname).arg(strerror(errno)) :
            tr("Failed to rebind: %1")
            .arg(strerror(errno)));
-       return false; 
+       return false;
 }
 
 void DhcpcdPreferences::rebind()
@@ -443,18 +455,18 @@ void DhcpcdPreferences::rebind()
 
        DHCPCD_CONNECTION *con = parent->getConnection();
        DHCPCD_IF *i;
-       bool found = false;
        bool worked;
-       if (iface && strcmp(eWhat, "interface") == 0) {
+       bool found;
+       if (eBlock == NULL || strcmp(eWhat, "interface") == 0) {
                worked = tryRebind(iface ? iface->ifname : NULL);
                goto done;
        }
-       
+
+       found = false;
        worked = true;
        for (i = dhcpcd_interfaces(con); i; i = i->next) {
                if (strcmp(i->type, "link") == 0 &&
-                   (eBlock == NULL ||
-                   (i->ssid && strcmp(i->ssid, eBlock) == 0)))
+                   (i->ssid && strcmp(i->ssid, eBlock) == 0))
                {
                        found = true;
                        if (!tryRebind(i->ifname))
@@ -468,7 +480,7 @@ void DhcpcdPreferences::rebind()
                return;
        }
 
-done:          
+done:
        if (worked)
                close();
 }