Remove the DhcpcdWi instance if the wpa_supplicant connection is lost.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-qt.cpp
index c38db32e4c6c68b7d8fe1c452acf32de55b40dc4..5e88aa7c4c14809add88332d6023685c3bb4f0e7 100644 (file)
@@ -37,6 +37,7 @@
 #include "dhcpcd-about.h"
 #include "dhcpcd-preferences.h"
 #include "dhcpcd-wi.h"
+#include "dhcpcd-ifmenu.h"
 #include "dhcpcd-ssidmenu.h"
 
 DhcpcdQt::DhcpcdQt()
@@ -68,8 +69,8 @@ DhcpcdQt::DhcpcdQt()
        dhcpcd_set_status_callback(con, dhcpcd_status_cb, this);
        dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
        dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
+       dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, this);
        tryOpen();
-
 }
 
 DhcpcdQt::~DhcpcdQt()
@@ -78,12 +79,12 @@ DhcpcdQt::~DhcpcdQt()
        qDeleteAll(*wis);
        delete wis;
 
-       free(lastStatus);
-
        if (con != NULL) {
                dhcpcd_close(con);
                dhcpcd_free(con);
        }
+
+       free(lastStatus);
 }
 
 void DhcpcdQt::animate()
@@ -150,6 +151,7 @@ void DhcpcdQt::updateOnline(bool showIf)
 
        if (onLine != isOn || carrier != isCarrier) {
                onLine = isOn;
+               carrier = isCarrier;
                aniTimer->stop();
                aniCounter = 0;
                if (isOn) {
@@ -186,9 +188,7 @@ void DhcpcdQt::statusCallback(const char *status)
                        refresh = true;
                } else
                        refresh = strcmp(lastStatus, "opened") ? false : true;
-               printf ("refresh %d\n", refresh);
                updateOnline(refresh);
-               printf ("updated\n");
        }
 
        free(lastStatus);
@@ -217,24 +217,25 @@ void DhcpcdQt::ifCallback(DHCPCD_IF *i)
        char *msg;
        bool new_msg;
 
-       updateOnline(false);
-
-       if (strcmp(i->reason, "RENEW") == 0 ||
-           strcmp(i->reason, "STOP") == 0 ||
-           strcmp(i->reason, "STOPPED") == 0)
-               return;
-
-       msg = dhcpcd_if_message(i, &new_msg);
-       if (msg) {
-               qDebug("%s", msg);
-               if (new_msg) {
-                       QSystemTrayIcon::MessageIcon icon =
-                           i->up ? QSystemTrayIcon::Information :
-                           QSystemTrayIcon::Warning;
-                       trayIcon->showMessage(tr("Network Event"), msg, icon);
+       if (strcmp(i->reason, "RENEW") &&
+           strcmp(i->reason, "STOP") &&
+           strcmp(i->reason, "STOPPED"))
+       {
+               msg = dhcpcd_if_message(i, &new_msg);
+               if (msg) {
+                       qDebug("%s", msg);
+                       if (new_msg) {
+                               QSystemTrayIcon::MessageIcon icon =
+                                   i->up ? QSystemTrayIcon::Information :
+                                   QSystemTrayIcon::Warning;
+                               trayIcon->showMessage(tr("Network Event"),
+                                   msg, icon);
+                       }
+                       free(msg);
                }
-               free(msg);
        }
+
+       updateOnline(false);
 }
 
 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
@@ -292,19 +293,22 @@ void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
                        for (s2 = wi->getScans(); s2; s2 = s2->next) {
                                if (strcmp(s1->ssid, s2->ssid) == 0)
                                        break;
-                               if (s2 == NULL) {
-                                       if (!txt.isEmpty()) {
-                                               title = tr("New Access Points");
-                                               txt += '\n';
-                                       }
-                                       txt += s1->ssid;
+                       }
+                       if (s2 == NULL) {
+                               if (!txt.isEmpty()) {
+                                       title = tr("New Access Points");
+                                       txt += '\n';
                                }
+                               txt += s1->ssid;
                        }
                }
-               if (!txt.isEmpty())
+               if (!txt.isEmpty() &&
+                   (ssidMenu == NULL || !ssidMenu->isVisible()))
                        notify(title, txt);
        }
-       wi->setScans(scans);
+
+       if (wi->setScans(scans) && ssidMenu->isVisible())
+               ssidMenu->popup(ssidMenuPos);
 }
 
 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
@@ -314,6 +318,29 @@ void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
        dhcpcdQt->scanCallback(wpa);
 }
 
+void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa, const char *status)
+{
+       DHCPCD_IF *i;
+
+       i = dhcpcd_wpa_if(wpa);
+       qDebug("%s: WPA status %s", i->ifname, status);
+       if (strcmp(status, "down") == 0) {
+               DhcpcdWi *wi = findWi(wpa);
+               if (wi) {
+                       wis->removeOne(wi);
+                       delete wi;
+               }
+       }
+}
+
+void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status,
+    void *d)
+{
+       DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
+
+       dhcpcdQt->wpaStatusCallback(wpa, status);
+}
+
 void DhcpcdQt::tryOpen() {
        int fd = dhcpcd_open(con);
        static int last_error;
@@ -396,31 +423,8 @@ QIcon DhcpcdQt::icon()
        return getIcon("status", "network-transmit-receive");
 }
 
-void DhcpcdQt::addSsidMenu(QMenu *&menu, DHCPCD_IF *ifp, DhcpcdWi *&wi)
-{
-       DHCPCD_WI_SCAN *scan;
-
-       for (scan = wi->getScans(); scan; scan = scan->next) {
-               QWidgetAction *wa = new QWidgetAction(menu);
-               DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, ifp, scan);
-               wa->setDefaultWidget(ssidMenu);
-               menu->addAction(wa);
-               connect(ssidMenu, SIGNAL(selected(DHCPCD_IF *, DHCPCD_WI_SCAN *)),
-                   this, SLOT(connectSsid(DHCPCD_IF *, DHCPCD_WI_SCAN *)));
-       }
-}
-
-void DhcpcdQt::connectSsid(DHCPCD_IF *, DHCPCD_WI_SCAN *)
-{
-
-       QMessageBox::information(this, "Not implemented",
-           "SSID selection is not yet implemented");
-}
-
 void DhcpcdQt::createSsidMenu()
 {
-       DHCPCD_WPA *wpa;
-       DHCPCD_IF *ifp;
 
        if (ssidMenu) {
                delete ssidMenu;
@@ -430,22 +434,14 @@ void DhcpcdQt::createSsidMenu()
                return;
 
        ssidMenu = new QMenu(this);
-       if (wis->size() == 1) {
-               DhcpcdWi *wi = wis->first();
-               wpa = wi->getWpa();
-               ifp = dhcpcd_wpa_if(wpa);
-               addSsidMenu(ssidMenu, ifp, wi);
-       } else {
-               for (auto &wi : *wis) {
-                       wpa = wi->getWpa();
-                       ifp = dhcpcd_wpa_if(wpa);
-                       if (ifp) {
-                               QMenu *ifmenu = ssidMenu->addMenu(ifp->ifname);
-                               addSsidMenu(ifmenu, ifp, wi);
-                       }
-               }
+       if (wis->size() == 1)
+               wis->first()->createMenu(ssidMenu);
+       else {
+               for (auto &wi : *wis)
+                       ssidMenu->addMenu(wi->createIfMenu(ssidMenu));
        }
-       ssidMenu->popup(QCursor::pos());
+       ssidMenuPos = QCursor::pos();
+       ssidMenu->popup(ssidMenuPos);
 }
 
 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)