Display the PSK entry box near the mouse.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-qt.cpp
index e7a3d808f83218be811f8c9600d10d91592655a0..f1589c4fdabe93578e7225374bf2db75f0610c7b 100644 (file)
 #include "dhcpcd-ifmenu.h"
 #include "dhcpcd-ssidmenu.h"
 
+#ifdef NOTIFY
+#include <knotification.h>
+#endif
+
 DhcpcdQt::DhcpcdQt()
 {
 
@@ -77,6 +81,11 @@ DhcpcdQt::DhcpcdQt()
 DhcpcdQt::~DhcpcdQt()
 {
 
+       if (ssidMenu) {
+               ssidMenu->setVisible(false);
+               ssidMenu->deleteLater();
+       }
+
        if (con != NULL) {
                dhcpcd_close(con);
                dhcpcd_free(con);
@@ -84,9 +93,9 @@ DhcpcdQt::~DhcpcdQt()
 
        free(lastStatus);
 
-       qDeleteAll(*wis);
+       for (auto &wi : *wis)
+               wi->deleteLater();
        delete wis;
-
 }
 
 DHCPCD_CONNECTION *DhcpcdQt::getConnection()
@@ -101,9 +110,48 @@ QList<DhcpcdWi *> *DhcpcdQt::getWis()
        return wis;
 }
 
+const char * DhcpcdQt::signalStrengthIcon(DHCPCD_WI_SCAN *scan)
+{
+
+       if (scan->strength.value > 80)
+               return "network-wireless-connected-100";
+       else if (scan->strength.value > 55)
+               return "network-wireless-connected-75";
+       else if (scan->strength.value > 30)
+               return "network-wireless-connected-50";
+       else if (scan->strength.value > 5)
+               return "network-wireless-connected-25";
+       else
+               return "network-wireless-connected-00";
+}
+
+DHCPCD_WI_SCAN * DhcpcdQt::getStrongestSignal()
+{
+       DHCPCD_WI_SCAN *scan, *scans, *s;
+       DHCPCD_WPA *wpa;
+       DHCPCD_IF *i;
+
+       scan = NULL;
+       for (auto &wi : *wis) {
+               wpa = wi->getWpa();
+               i = dhcpcd_wpa_if(wpa);
+               scans = wi->getScans();
+               for (s = scans; s; s = s->next) {
+                       if (dhcpcd_wi_associated(i, s) &&
+                           (scan == NULL ||
+                           s->strength.value > scan->strength.value))
+                               scan = s;
+               }
+       }
+       return scan;
+}
+
 void DhcpcdQt::animate()
 {
        const char *icon;
+       DHCPCD_WI_SCAN *scan;
+
+       scan = getStrongestSignal();
 
        if (onLine) {
                if (aniCounter++ > 6) {
@@ -113,20 +161,42 @@ void DhcpcdQt::animate()
                }
 
                if (aniCounter % 2 == 0)
-                       icon = "network-idle";
+                       icon = scan ? "network-wireless-connected-00" :
+                           "network-idle";
                else
-                       icon = "network-transmit-receive";
+                       icon = scan ? DhcpcdQt::signalStrengthIcon(scan) :
+                           "network-transmit-receive";
        } else {
-               switch(aniCounter++) {
-               case 0:
-                       icon = "network-transmit";
-                       break;
-               case 1:
-                       icon = "network-receive";
-                       break;
-               default:
-                       icon = "network-idle";
-                       aniCounter = 0;
+               if (scan) {
+                       switch(aniCounter++) {
+                       case 0:
+                               icon = "network-wireless-connected-00";
+                               break;
+                       case 1:
+                               icon = "network-wireless-connected-25";
+                               break;
+                       case 2:
+                               icon = "network-wireless-connected-50";
+                               break;
+                       case 3:
+                               icon = "network-wireless-connected-75";
+                               break;
+                       default:
+                               icon = "network-wireless-connected-100";
+                               aniCounter = 0;
+                       }
+               } else {
+                       switch(aniCounter++) {
+                       case 0:
+                               icon = "network-transmit";
+                               break;
+                       case 1:
+                               icon = "network-receive";
+                               break;
+                       default:
+                               icon = "network-idle";
+                               aniCounter = 0;
+                       }
                }
        }
 
@@ -190,11 +260,22 @@ void DhcpcdQt::statusCallback(const char *status)
                aniCounter = 0;
                onLine = carrier = false;
                setIcon("status", "network-offline");
+               trayIcon->setToolTip(tr("Not connected to dhcpcd"));
+               /* Close down everything */
                if (notifier) {
-                       delete notifier;
+                       notifier->setEnabled(false);
+                       notifier->deleteLater();
                        notifier = NULL;
                }
-               trayIcon->setToolTip(tr("Not connected to dhcpcd"));
+               if (ssidMenu) {
+                       ssidMenu->deleteLater();
+                       ssidMenu = NULL;
+               }
+               preferencesAction->setEnabled(false);
+               if (preferences) {
+                       preferences->deleteLater();
+                       preferences = NULL;
+               }
        } else {
                bool refresh;
 
@@ -243,14 +324,27 @@ void DhcpcdQt::ifCallback(DHCPCD_IF *i)
                                QSystemTrayIcon::MessageIcon icon =
                                    i->up ? QSystemTrayIcon::Information :
                                    QSystemTrayIcon::Warning;
-                               trayIcon->showMessage(tr("Network Event"),
-                                   msg, icon);
+                               QString t = tr("Network Event");
+                               QString m = msg;
+                               notify(t, m, icon);
                        }
                        free(msg);
                }
        }
 
        updateOnline(false);
+
+       if (i->wireless) {
+               for (auto &wi : *wis) {
+                       DHCPCD_WPA *wpa = wi->getWpa();
+                       if (dhcpcd_wpa_if(wpa) == i) {
+                               DHCPCD_WI_SCAN *scans;
+
+                               scans = dhcpcd_wi_scans(i);
+                               processScans(wi, scans);
+                       }
+               }
+       }
 }
 
 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
@@ -270,9 +364,36 @@ DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
        return NULL;
 }
 
+void DhcpcdQt::processScans(DhcpcdWi *wi, DHCPCD_WI_SCAN *scans)
+{
+       DHCPCD_WI_SCAN *s1, *s2;
+
+       QString title = tr("New Access Point");
+       QString txt;
+       for (s1 = scans; s1; s1 = s1->next) {
+               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 (!txt.isEmpty() &&
+           (ssidMenu == NULL || !ssidMenu->isVisible()))
+               notify(title, txt);
+
+       if (wi->setScans(scans) && ssidMenu && ssidMenu->isVisible())
+               ssidMenu->popup(ssidMenuPos);
+}
+
 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
 {
-       DHCPCD_WI_SCAN *scans, *s1, *s2;
+       DHCPCD_WI_SCAN *scans;
        int fd = dhcpcd_wpa_get_fd(wpa);
        DhcpcdWi *wi;
 
@@ -281,7 +402,7 @@ void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
                qCritical("No fd for WPA");
                if (wi) {
                        wis->removeOne(wi);
-                       delete wi;
+                       wi->deleteLater();
                }
                return;
        }
@@ -291,7 +412,7 @@ void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
                qCritical("No interface for WPA");
                if (wi) {
                        wis->removeOne(wi);
-                       delete wi;
+                       wi->deleteLater();
                }
                return;
        }
@@ -300,30 +421,21 @@ void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
        scans = dhcpcd_wi_scans(i);
        if (wi == NULL) {
                wi = new DhcpcdWi(this, wpa);
-               wis->append(wi);
-       } else {
-               QString title = tr("New Access Point");
-               QString txt;
-               for (s1 = scans; s1; s1 = s1->next) {
-                       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 (!txt.isEmpty() &&
-                   (ssidMenu == NULL || !ssidMenu->isVisible()))
-                       notify(title, txt);
-       }
+               if (wi->open()) {
+                       wis->append(wi);
+                       wi->setScans(scans);
+               } else
+                       wi->deleteLater();
+       } else
+               processScans(wi, scans);
 
-       if (wi->setScans(scans) && ssidMenu->isVisible())
-               ssidMenu->popup(ssidMenuPos);
+       if (!aniTimer->isActive()) {
+               DHCPCD_WI_SCAN *scan;
+
+               scan = getStrongestSignal();
+               if (scan)
+                       setIcon("status", DhcpcdQt::signalStrengthIcon(scan));
+       }
 }
 
 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
@@ -343,7 +455,7 @@ void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa, const char *status)
                DhcpcdWi *wi = findWi(wpa);
                if (wi) {
                        wis->removeOne(wi);
-                       delete wi;
+                       wi->deleteLater();
                }
        }
 }
@@ -386,7 +498,8 @@ unprived:
        dhcpcd_wpa_start(con);
 
        if (retryOpenTimer) {
-               delete retryOpenTimer;
+               retryOpenTimer->stop();
+               retryOpenTimer->deleteLater();
                retryOpenTimer = NULL;
        }
 
@@ -396,25 +509,31 @@ unprived:
        preferencesAction->setEnabled(dhcpcd_privileged(con));
 }
 
-void DhcpcdQt::dispatch() {
-
-       if (dhcpcd_get_fd(con) == -1) {
-               qWarning("dhcpcd connection lost");
-               return;
-       }
+void DhcpcdQt::dispatch()
+{
 
        dhcpcd_dispatch(con);
 }
 
 void DhcpcdQt::notify(QString &title, QString &msg,
-    QSystemTrayIcon::MessageIcon icon)
+#ifdef NOTIFY
+    QSystemTrayIcon::MessageIcon
+#else
+    QSystemTrayIcon::MessageIcon icon
+#endif
+    )
 {
 
-       qDebug("%s", qPrintable(msg));
-       trayIcon->showMessage(title, msg, icon);
+#ifdef NOTIFY
+       KNotification *n = new KNotification("event", this);
+       n->setTitle(title);
+       n->setText(msg);
+       n->sendEvent();
+#else
+       //trayIcon->showMessage(title, msg, icon);
+#endif
 }
 
-
 void DhcpcdQt::closeEvent(QCloseEvent *event)
 {
 
@@ -432,6 +551,7 @@ QIcon DhcpcdQt::getIcon(QString category, QString name)
                icon = QIcon::fromTheme(name);
        else
                icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
+
        return icon;
 }
 
@@ -448,11 +568,18 @@ QIcon DhcpcdQt::icon()
        return getIcon("status", "network-transmit-receive");
 }
 
+void DhcpcdQt::menuDeleted(QMenu *menu)
+{
+
+       if (ssidMenu == menu)
+               ssidMenu = NULL;
+}
+
 void DhcpcdQt::createSsidMenu()
 {
 
        if (ssidMenu) {
-               delete ssidMenu;
+               ssidMenu->deleteLater();
                ssidMenu = NULL;
        }
        if (wis->size() == 0)