Use theme icons here rather than stock icons.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-wi.cpp
index a02907911657bfe5ccfe714d9c1e9d6ad223a928..b6326e280350dd1c42ad5f1cd0da447968769cc0 100644 (file)
@@ -58,9 +58,17 @@ DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
 DhcpcdWi::~DhcpcdWi()
 {
 
-       dhcpcd_wi_scans_free(scans);
-       if (notifier != NULL)
+       if (menu) {
+               delete menu;
+               menu = NULL;
+       }
+
+       if (notifier) {
                delete notifier;
+               notifier = NULL;
+       }
+
+       dhcpcd_wi_scans_free(scans);
 }
 
 DHCPCD_WPA *DhcpcdWi::getWpa()
@@ -75,8 +83,9 @@ DHCPCD_WI_SCAN *DhcpcdWi::getScans()
        return scans;
 }
 
-void DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
+bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
 {
+       int changed = 0;
 
        if (menu) {
                QList<DhcpcdSsidMenu*> lst;
@@ -84,34 +93,61 @@ void DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
 
                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);
+                                       found = true;
                                        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;
+
+       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;
 
-       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)
@@ -124,10 +160,12 @@ void DhcpcdWi::createMenu(QMenu *menu)
 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
 {
        DHCPCD_IF *ifp;
+       QIcon icon;
 
        ifp = dhcpcd_wpa_if(wpa);
        menu = new DhcpcdIfMenu(ifp, parent);
-       menu->setIcon(QIcon::fromTheme("network-wireless"));
+       icon = DhcpcdQt::getIcon("devices", "network-wireless");
+       menu->setIcon(icon);
        createMenu1(menu);
        return menu;
 }