Remove vanished SSID's from menu.
authorRoy Marples <roy@marples.name>
Fri, 12 Sep 2014 10:58:37 +0000 (10:58 +0000)
committerRoy Marples <roy@marples.name>
Fri, 12 Sep 2014 10:58:37 +0000 (10:58 +0000)
Fix notifications.
Fix menu alignment.

src/dhcpcd-qt/dhcpcd-qt.cpp
src/dhcpcd-qt/dhcpcd-qt.h
src/dhcpcd-qt/dhcpcd-ssidmenu.cpp
src/dhcpcd-qt/dhcpcd-ssidmenu.h
src/dhcpcd-qt/dhcpcd-wi.cpp
src/dhcpcd-qt/dhcpcd-wi.h

index 4d0ead8f7fd3db7fc85868876f2d6e769c8d0295..57c278e5cbfd4326ffe6c4b559da12981b148347 100644 (file)
@@ -70,7 +70,6 @@ DhcpcdQt::DhcpcdQt()
        dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
        dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
        tryOpen();
        dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
        dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
        tryOpen();
-
 }
 
 DhcpcdQt::~DhcpcdQt()
 }
 
 DhcpcdQt::~DhcpcdQt()
@@ -151,6 +150,7 @@ void DhcpcdQt::updateOnline(bool showIf)
 
        if (onLine != isOn || carrier != isCarrier) {
                onLine = isOn;
 
        if (onLine != isOn || carrier != isCarrier) {
                onLine = isOn;
+               carrier = isCarrier;
                aniTimer->stop();
                aniCounter = 0;
                if (isOn) {
                aniTimer->stop();
                aniCounter = 0;
                if (isOn) {
@@ -216,24 +216,25 @@ void DhcpcdQt::ifCallback(DHCPCD_IF *i)
        char *msg;
        bool new_msg;
 
        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)
 }
 
 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
@@ -300,11 +301,13 @@ void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
                                txt += s1->ssid;
                        }
                }
                                txt += s1->ssid;
                        }
                }
-               if (!txt.isEmpty())
+               if (!txt.isEmpty() &&
+                   (ssidMenu == NULL || !ssidMenu->isVisible()))
                        notify(title, txt);
        }
 
                        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)
 }
 
 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
@@ -413,7 +416,8 @@ void DhcpcdQt::createSsidMenu()
                for (auto &wi : *wis)
                        ssidMenu->addMenu(wi->createIfMenu(ssidMenu));
        }
                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)
 }
 
 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
index 1ab7aa3517efe9d89c9aae59dd1fa7d6e7013ff6..a6c70e7d9dd518dd2aef8ca18d956b02d0592f95 100644 (file)
@@ -43,6 +43,7 @@ class QAction;
 class QDialog;
 class QLabel;
 class QMenu;
 class QDialog;
 class QLabel;
 class QMenu;
+class QPoint;
 class QPushButton;
 class QSocketNotifier;
 class QTimer;
 class QPushButton;
 class QSocketNotifier;
 class QTimer;
@@ -114,6 +115,7 @@ private:
        QAction *aboutAction;
        QMenu *trayIconMenu;
        QMenu *ssidMenu;
        QAction *aboutAction;
        QMenu *trayIconMenu;
        QMenu *ssidMenu;
+       QPoint ssidMenuPos;
 
        void notify(QString &title, QString &msg,
            QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information);
 
        void notify(QString &title, QString &msg,
            QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information);
index 855b289aa0bfd26d87430225b7ab672a55217a23..fbb0d1775424d1a958dd6994f508513d1fbcd443 100644 (file)
 #include "dhcpcd-qt.h"
 #include "dhcpcd-ssidmenu.h"
 
 #include "dhcpcd-qt.h"
 #include "dhcpcd-ssidmenu.h"
 
-DhcpcdSsidMenu::DhcpcdSsidMenu(QWidget *parent, DhcpcdWi *wi, DHCPCD_WI_SCAN *scan)
+DhcpcdSsidMenu::DhcpcdSsidMenu(QWidget *parent, QWidgetAction *wa,
+    DhcpcdWi *wi, DHCPCD_WI_SCAN *scan)
     : QWidget(parent, NULL)
 {
 
     : QWidget(parent, NULL)
 {
 
+       this->wa = wa;
        this->wi = wi;
 
        QHBoxLayout *layout = new QHBoxLayout(this);
        this->wi = wi;
 
        QHBoxLayout *layout = new QHBoxLayout(this);
@@ -48,8 +50,10 @@ DhcpcdSsidMenu::DhcpcdSsidMenu(QWidget *parent, DhcpcdWi *wi, DHCPCD_WI_SCAN *sc
        layout->addWidget(button);
        licon = new QLabel(this);
        layout->addWidget(licon);
        layout->addWidget(button);
        licon = new QLabel(this);
        layout->addWidget(licon);
+       layout->setAlignment(licon, Qt::AlignRight);
        bar = new QProgressBar(this);
        layout->addWidget(bar);
        bar = new QProgressBar(this);
        layout->addWidget(bar);
+       layout->setAlignment(bar, Qt::AlignRight);
        setScan(scan);
 
        button->installEventFilter(this);
        setScan(scan);
 
        button->installEventFilter(this);
@@ -57,6 +61,12 @@ DhcpcdSsidMenu::DhcpcdSsidMenu(QWidget *parent, DhcpcdWi *wi, DHCPCD_WI_SCAN *sc
        bar->installEventFilter(this);
 }
 
        bar->installEventFilter(this);
 }
 
+QWidgetAction *DhcpcdSsidMenu::getWidgetAction()
+{
+
+       return wa;
+}
+
 DHCPCD_WI_SCAN *DhcpcdSsidMenu::getScan()
 {
 
 DHCPCD_WI_SCAN *DhcpcdSsidMenu::getScan()
 {
 
@@ -66,20 +76,21 @@ DHCPCD_WI_SCAN *DhcpcdSsidMenu::getScan()
 void DhcpcdSsidMenu::setScan(DHCPCD_WI_SCAN *scan)
 {
        DHCPCD_WPA *wpa;
 void DhcpcdSsidMenu::setScan(DHCPCD_WI_SCAN *scan)
 {
        DHCPCD_WPA *wpa;
-       DHCPCD_IF *ifp;
+       DHCPCD_IF *i;
        QIcon icon;
 
        this->scan = scan;
        wpa = wi->getWpa();
        QIcon icon;
 
        this->scan = scan;
        wpa = wi->getWpa();
-       ifp = dhcpcd_wpa_if(wpa);
+       i = dhcpcd_wpa_if(wpa);
 
 
-       button->setChecked(strcmp(scan->ssid, ifp->ssid) == 0);
+       button->setChecked(i->up && strcmp(scan->ssid, i->ssid) == 0);
        button->setText(scan->ssid);
        if (scan->flags[0] == '\0') {
                icon = DhcpcdQt::getIcon("devices", "network-wireless");
                setToolTip(scan->bssid);
        } else {
        button->setText(scan->ssid);
        if (scan->flags[0] == '\0') {
                icon = DhcpcdQt::getIcon("devices", "network-wireless");
                setToolTip(scan->bssid);
        } else {
-               icon = DhcpcdQt::getIcon("status", "network-wireless-encrypted");
+               icon = DhcpcdQt::getIcon("status",
+                   "network-wireless-encrypted");
                QString tip = QString::fromAscii(scan->bssid);
                tip += " " + QString::fromAscii(scan->flags);
                setToolTip(tip);
                QString tip = QString::fromAscii(scan->bssid);
                tip += " " + QString::fromAscii(scan->flags);
                setToolTip(tip);
index a9c9ec1cb323cadb86b5b508f9602e8336770c20..74f115b5fa978acb46961d0fde4515cd8deb198c 100644 (file)
@@ -31,6 +31,7 @@
 class QRadioButton;
 class QLabel;
 class QProgressBar;
 class QRadioButton;
 class QLabel;
 class QProgressBar;
+class QWidgetAction;
 
 class DhcpcdWi;
 
 
 class DhcpcdWi;
 
@@ -39,9 +40,11 @@ class DhcpcdSsidMenu : public QWidget
        Q_OBJECT
 
 public:
        Q_OBJECT
 
 public:
-       DhcpcdSsidMenu(QWidget *parent, DhcpcdWi *wi, DHCPCD_WI_SCAN *scan);
+       DhcpcdSsidMenu(QWidget *parent, QWidgetAction *wa,
+           DhcpcdWi *wi, DHCPCD_WI_SCAN *scan);
        ~DhcpcdSsidMenu() {};
 
        ~DhcpcdSsidMenu() {};
 
+       QWidgetAction *getWidgetAction();
        DHCPCD_WI_SCAN *getScan();
        void setScan(DHCPCD_WI_SCAN *scan);
 
        DHCPCD_WI_SCAN *getScan();
        void setScan(DHCPCD_WI_SCAN *scan);
 
@@ -52,6 +55,7 @@ private slots:
        bool eventFilter(QObject *obj, QEvent *event);
 
 private:
        bool eventFilter(QObject *obj, QEvent *event);
 
 private:
+       QWidgetAction *wa;
        DhcpcdWi *wi;
        DHCPCD_WI_SCAN *scan;
 
        DhcpcdWi *wi;
        DHCPCD_WI_SCAN *scan;
 
index a02907911657bfe5ccfe714d9c1e9d6ad223a928..5aa46638e60c0f50230dde3df7cceb510d675ea9 100644 (file)
@@ -75,8 +75,9 @@ DHCPCD_WI_SCAN *DhcpcdWi::getScans()
        return scans;
 }
 
        return scans;
 }
 
-void DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
+bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
 {
 {
+       int changed = 0;
 
        if (menu) {
                QList<DhcpcdSsidMenu*> lst;
 
        if (menu) {
                QList<DhcpcdSsidMenu*> lst;
@@ -84,34 +85,61 @@ void DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
 
                lst = menu->findChildren<DhcpcdSsidMenu*>();
                for (scan = scans; scan; scan = scan->next) {
 
                lst = menu->findChildren<DhcpcdSsidMenu*>();
                for (scan = scans; scan; scan = scan->next) {
+                       bool found = false;
+
                        foreach(DhcpcdSsidMenu *sm, lst) {
                                DHCPCD_WI_SCAN *s = sm->getScan();
                                if (memcmp(scan->bssid, s->bssid,
                                    sizeof(scan->bssid)) == 0)
                                {
                                        sm->setScan(scan);
                        foreach(DhcpcdSsidMenu *sm, lst) {
                                DHCPCD_WI_SCAN *s = sm->getScan();
                                if (memcmp(scan->bssid, s->bssid,
                                    sizeof(scan->bssid)) == 0)
                                {
                                        sm->setScan(scan);
+                                       found = true;
                                        break;
                                }
                        }
                                        break;
                                }
                        }
+
+                       if (!found) {
+                               createMenuItem(menu, scan);
+                               changed++;
+                       }
+               }
+
+               foreach(DhcpcdSsidMenu *sm, lst) {
+                       DHCPCD_WI_SCAN *s = sm->getScan();
+                       for (scan = scans; scan; scan = scan->next) {
+                               if (memcmp(scan->bssid, s->bssid,
+                                   sizeof(scan->bssid)) == 0)
+                                       break;
+                       }
+                       if (scan == NULL) {
+                               menu->removeAction(sm->getWidgetAction());
+                               changed--;
+                       }
                }
        }
                }
        }
-               
+
        dhcpcd_wi_scans_free(this->scans);
        this->scans = scans;
        dhcpcd_wi_scans_free(this->scans);
        this->scans = scans;
+
+       return !(changed == 0);
+}
+
+void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan)
+{
+       QWidgetAction *wa = new QWidgetAction(menu);
+       DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, wa, this, scan);
+       wa->setDefaultWidget(ssidMenu);
+       menu->addAction(wa);
+       connect(ssidMenu, SIGNAL(selected(DHCPCD_WI_SCAN *)),
+           this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
 }
 
 void DhcpcdWi::createMenu1(QMenu *menu)
 {
        DHCPCD_WI_SCAN *scan;
 
 }
 
 void DhcpcdWi::createMenu1(QMenu *menu)
 {
        DHCPCD_WI_SCAN *scan;
 
-       for (scan = scans; scan; scan = scan->next) {
-               QWidgetAction *wa = new QWidgetAction(menu);
-               DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
-               wa->setDefaultWidget(ssidMenu);
-               menu->addAction(wa);
-               connect(ssidMenu, SIGNAL(selected(DHCPCD_WI_SCAN *)),
-                   this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
-       }
+       for (scan = scans; scan; scan = scan->next)
+               createMenuItem(menu, scan);
 }
 
 void DhcpcdWi::createMenu(QMenu *menu)
 }
 
 void DhcpcdWi::createMenu(QMenu *menu)
index 6ec15e0ed2928cf9ca06f3d8d1ed58dcda7654f3..e484f59270b4119fbe10816f260bf2a3a29f2616 100644 (file)
@@ -46,7 +46,7 @@ public:
        DHCPCD_WPA *getWpa();
 
        DHCPCD_WI_SCAN *getScans();
        DHCPCD_WPA *getWpa();
 
        DHCPCD_WI_SCAN *getScans();
-       void setScans(DHCPCD_WI_SCAN *scans);
+       bool setScans(DHCPCD_WI_SCAN *scans);
 
        void createMenu(QMenu *parent);
        QMenu *createIfMenu(QMenu *parent);
 
        void createMenu(QMenu *parent);
        QMenu *createIfMenu(QMenu *parent);
@@ -65,6 +65,7 @@ private:
        QTimer *retryOpenTimer;
 
        QMenu *menu;
        QTimer *retryOpenTimer;
 
        QMenu *menu;
+       void createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan);
        void createMenu1(QMenu *parent);
 };
 
        void createMenu1(QMenu *parent);
 };