Close WPA listeners when we lose the connection.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-qt.cpp
index 4d0ead8f7fd3db7fc85868876f2d6e769c8d0295..6cb9d9a9acad2a9a1acdd137edddaa34b30ec8f2 100644 (file)
@@ -66,25 +66,39 @@ DhcpcdQt::DhcpcdQt()
                exit(EXIT_FAILURE);
                return;
        }
+       dhcpcd_set_progname(con, "dhcpcd-qt");
        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()
 {
 
-       qDeleteAll(*wis);
-       delete wis;
-
        if (con != NULL) {
                dhcpcd_close(con);
                dhcpcd_free(con);
        }
 
        free(lastStatus);
+
+       qDeleteAll(*wis);
+       delete wis;
+
+}
+
+DHCPCD_CONNECTION *DhcpcdQt::getConnection()
+{
+
+       return con;
+}
+
+QList<DhcpcdWi *> *DhcpcdQt::getWis()
+{
+
+       return wis;
 }
 
 void DhcpcdQt::animate()
@@ -151,6 +165,7 @@ void DhcpcdQt::updateOnline(bool showIf)
 
        if (onLine != isOn || carrier != isCarrier) {
                onLine = isOn;
+               carrier = isCarrier;
                aniTimer->stop();
                aniCounter = 0;
                if (isOn) {
@@ -173,12 +188,23 @@ void DhcpcdQt::statusCallback(const char *status)
        if (strcmp(status, "down") == 0) {
                aniTimer->stop();
                aniCounter = 0;
+               onLine = carrier = false;
                setIcon("status", "network-offline");
+               trayIcon->setToolTip(tr("Not connected to dhcpcd"));
+               /* Close down everything */
                if (notifier) {
                        delete notifier;
                        notifier = NULL;
                }
-               trayIcon->setToolTip(tr("Not connected to dhcpcd"));
+               if (ssidMenu) {
+                       delete ssidMenu;
+                       ssidMenu = NULL;
+               }
+               if (preferences) {
+                       delete preferences;
+                       preferences = NULL;
+               }
+               preferencesAction->setEnabled(false);
        } else {
                bool refresh;
 
@@ -216,24 +242,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)
@@ -300,11 +327,13 @@ void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
                                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,14 +343,44 @@ 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);
+       int fd = dhcpcd_open(con, true);
        static int last_error;
 
        if (fd == -1) {
+               if (errno == EACCES || errno == EPERM) {
+                       if ((fd = dhcpcd_open(con, false)) != -1)
+                               goto unprived;
+               }
                if (errno != last_error) {
                        last_error = errno;
-                       qCritical("dhcpcd_open: %s", strerror(errno));
+                       const char *errt = strerror(errno);
+                       qCritical("dhcpcd_open: %s", errt);
+                       trayIcon->setToolTip(
+                           tr("Error connecting to dhcpcd: %1").arg(errt));
                }
                if (retryOpenTimer == NULL) {
                        retryOpenTimer = new QTimer(this);
@@ -332,6 +391,7 @@ void DhcpcdQt::tryOpen() {
                return;
        }
 
+unprived:
        /* Start listening to WPA events */
        dhcpcd_wpa_start(con);
 
@@ -342,6 +402,8 @@ void DhcpcdQt::tryOpen() {
 
        notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
        connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
+
+       preferencesAction->setEnabled(dhcpcd_privileged(con));
 }
 
 void DhcpcdQt::dispatch() {
@@ -413,7 +475,8 @@ void DhcpcdQt::createSsidMenu()
                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)
@@ -457,6 +520,7 @@ void DhcpcdQt::createActions()
 
        preferencesAction = new QAction(tr("&Preferences"), this);
        preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
+       preferencesAction->setEnabled(false);
        connect(preferencesAction, SIGNAL(triggered()),
            this, SLOT(showPreferences()));