Disconnect when same SSID selected (#1)
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-wi.cpp
index aeec2826bd765e53cb920b0b2f7a09237c45035a..4e4d86a8a5fbeaa2a11a77f28d95a4cd0376c802 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * dhcpcd-qt
- * Copyright 2014-2015 Roy Marples <roy@marples.name>
+ * Copyright 2014-2017 Roy Marples <roy@marples.name>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -295,13 +295,23 @@ void DhcpcdWi::ping()
 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
 {
        DHCPCD_WI_SCAN s;
+       DHCPCD_IF *i;
        int err;
 
        /* Take a copy of scan incase it's destroyed by a scan update */
        memcpy(&s, scan, sizeof(s));
        s.next = NULL;
 
-       if (s.flags & WSF_PSK) {
+       i = dhcpcd_wpa_if(wpa);
+       if (i == NULL)
+               err = DHCPCD_WPA_ERR;
+       else if (dhcpcd_wi_associated(i, &s)) {
+               /* Disconnect if same interface selected */
+               if (!dhcpcd_wpa_disconnect(wpa))
+                       err = DHCPCD_WPA_ERR_DISCONN;
+               else
+                       err = DHCPCD_WPA_SUCCESS;
+       } else if (s.flags & WSF_PSK) {
                bool ok;
                QString pwd;
 
@@ -314,7 +324,7 @@ void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
                if (pwd.isNull() || pwd.isEmpty())
                        err = dhcpcd_wpa_select(wpa, &s);
                else
-                       err = dhcpcd_wpa_configure(wpa, &s, pwd.toAscii());
+                       err = dhcpcd_wpa_configure(wpa, &s, pwd.toLatin1());
        } else
                err = dhcpcd_wpa_configure(wpa, &s, NULL);
 
@@ -322,11 +332,14 @@ void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
        switch (err) {
        case DHCPCD_WPA_SUCCESS:
                return;
+       case DHCPCD_WPA_ERR:
+               errt = tr("Failed.");
+               break;
        case DHCPCD_WPA_ERR_DISCONN:
                errt = tr("Failed to disconnect.");
                break;
        case DHCPCD_WPA_ERR_RECONF:
-               errt = tr("Faile to reconfigure.");
+               errt = tr("Failed to reconfigure.");
                break;
        case DHCPCD_WPA_ERR_SET:
                errt = tr("Failed to set key management.");