Associated scans are displayed first in a bold font.
authorRoy Marples <roy@marples.name>
Tue, 2 Dec 2014 13:42:43 +0000 (13:42 +0000)
committerRoy Marples <roy@marples.name>
Tue, 2 Dec 2014 13:42:43 +0000 (13:42 +0000)
src/dhcpcd-gtk/dhcpcd-gtk.h
src/dhcpcd-gtk/menu.c
src/dhcpcd-qt/dhcpcd-qt.cpp
src/dhcpcd-qt/dhcpcd-ssidmenu.cpp
src/dhcpcd-qt/dhcpcd-ssidmenu.h
src/dhcpcd-qt/dhcpcd-ssidmenuwidget.cpp
src/dhcpcd-qt/dhcpcd-ssidmenuwidget.h
src/dhcpcd-qt/dhcpcd-wi.cpp
src/libdhcpcd/dhcpcd.h
src/libdhcpcd/wpa.c

index 6b885b41f1ddf133841b0cf8aff8f3f935611c8a..e343dcb15a1e2798d886f1cc33734f8f36fdf3b5 100644 (file)
@@ -50,6 +50,7 @@
 typedef struct wi_menu {
        TAILQ_ENTRY(wi_menu) next;
        DHCPCD_WI_SCAN *scan;
+       bool associated;
        GtkWidget *menu;
        GtkWidget *ssid;
        GtkWidget *icon;
@@ -63,7 +64,6 @@ typedef struct wi_scan {
        DHCPCD_WI_SCAN *scans;
 
        GtkWidget *ifmenu;
-       GtkWidget *sep;
        WI_MENUS menus;
 } WI_SCAN;
 
index 0300b20ef2338c0d5f94b8f8c16afe11427a17e5..db41fa4d5974b0ec0acea60c6c3ea1e77b28c3f3 100644 (file)
@@ -108,6 +108,13 @@ get_strength_icon_name(int strength)
                return "network-wireless-connected-00";
 }
 
+static bool
+is_associated(WI_SCAN *wi, DHCPCD_WI_SCAN *scan)
+{
+
+       return dhcpcd_wi_associated(wi->interface, scan);
+}
+
 static void
 update_item(WI_SCAN *wi, WI_MENU *m, DHCPCD_WI_SCAN *scan)
 {
@@ -118,8 +125,8 @@ update_item(WI_SCAN *wi, WI_MENU *m, DHCPCD_WI_SCAN *scan)
 
        g_object_set_data(G_OBJECT(m->menu), "dhcpcd_wi_scan", scan);
 
-       if (wi->interface->up &&
-           g_strcmp0(scan->ssid, wi->interface->ssid) == 0)
+       m->associated = is_associated(wi, scan);
+       if (m->associated)
                sel = gtk_image_new_from_icon_name("dialog-ok-apply",
                    GTK_ICON_SIZE_MENU);
        else
@@ -127,7 +134,15 @@ update_item(WI_SCAN *wi, WI_MENU *m, DHCPCD_WI_SCAN *scan)
        gtk_image_menu_item_set_image(
            GTK_IMAGE_MENU_ITEM(m->menu), sel);
 
-       gtk_label_set_text(GTK_LABEL(m->ssid), scan->ssid);
+       if (m->associated) {
+               gchar *lbl;
+
+               lbl = g_markup_printf_escaped("<b>%s</b>",
+                   scan->ssid);
+               gtk_label_set_markup(GTK_LABEL(m->ssid), lbl);
+               g_free(lbl);
+       } else
+               gtk_label_set_text(GTK_LABEL(m->ssid), scan->ssid);
        if (scan->flags & WSF_SECURE)
                icon = "network-wireless-encrypted";
        else
@@ -157,7 +172,7 @@ static WI_MENU *
 create_menu(WI_SCAN *wis, DHCPCD_WI_SCAN *scan)
 {
        WI_MENU *wim;
-       GtkWidget *box, *sel;
+       GtkWidget *box;
        const char *icon;
 
        wim = g_malloc(sizeof(*wim));
@@ -166,16 +181,7 @@ create_menu(WI_SCAN *wis, DHCPCD_WI_SCAN *scan)
        box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
        gtk_container_add(GTK_CONTAINER(wim->menu), box);
 
-       if (wis->interface->up &&
-           g_strcmp0(scan->ssid, wis->interface->ssid) == 0)
-               sel = gtk_image_new_from_icon_name("dialog-ok-apply",
-                   GTK_ICON_SIZE_MENU);
-       else
-               sel = NULL;
-       gtk_image_menu_item_set_image(
-           GTK_IMAGE_MENU_ITEM(wim->menu), sel);
-
-       wim->ssid = gtk_label_new(scan->ssid);
+       wim->ssid = gtk_label_new(NULL);
        gtk_misc_set_alignment(GTK_MISC(wim->ssid), 0.0, 0.5);
        gtk_box_pack_start(GTK_BOX(box), wim->ssid, TRUE, TRUE, 0);
 
@@ -201,6 +207,7 @@ create_menu(WI_SCAN *wis, DHCPCD_WI_SCAN *scan)
                g_free(tip);
        }
 #endif
+       update_item(wis, wim, scan);
 
        g_signal_connect(G_OBJECT(wim->menu), "activate",
            G_CALLBACK(ssid_hook), NULL);
@@ -226,11 +233,16 @@ menu_update_scans(WI_SCAN *wi, DHCPCD_WI_SCAN *scans)
        TAILQ_FOREACH_SAFE(wim, &wi->menus, next, win) {
                found = false;
                for (s = scans; s; s = s->next) {
-                       if (memcmp(wim->scan->ssid, s->ssid,
-                           sizeof(s->ssid)) == 0)
-                       {
-                               found = true;
-                               update_item(wi, wim, s);
+                       if (strcmp(wim->scan->ssid, s->ssid) == 0) {
+                               /* If assoication changes, we
+                                * need to remove the item to replace it */
+                               if (wim->associated ==
+                                   is_associated(wi, s))
+                               {
+                                       found = true;
+                                       update_item(wi, wim, s);
+                               }
+                               break;
                        }
                }
                if (!found) {
@@ -244,13 +256,13 @@ menu_update_scans(WI_SCAN *wi, DHCPCD_WI_SCAN *scans)
                found = false;
                position = 0;
                TAILQ_FOREACH(wim, &wi->menus, next) {
-                       if (memcmp(wim->scan->ssid, s->ssid,
-                           sizeof(s->ssid)) == 0)
-                       {
+                       if (strcmp(wim->scan->ssid, s->ssid) == 0) {
                                found = true;
                                break;
                        }
-                       if (dhcpcd_wi_scan_compare(wim->scan, s) < 0)
+                       /* Assoicated scans are always first */
+                       if (!is_associated(wi, s) &&
+                           dhcpcd_wi_scan_compare(wim->scan, s) < 0)
                                position++;
                }
                if (!found) {
@@ -297,15 +309,19 @@ add_scans(WI_SCAN *wi)
        GtkWidget *m;
        DHCPCD_WI_SCAN *wis;
        WI_MENU *wim;
+       int position;
 
        if (wi->scans == NULL)
                return NULL;
 
        m = gtk_menu_new();
+       position = 0;
        for (wis = wi->scans; wis; wis = wis->next) {
                wim = create_menu(wi, wis);
                TAILQ_INSERT_TAIL(&wi->menus, wim, next);
-               gtk_menu_shell_append(GTK_MENU_SHELL(m), wim->menu);
+               gtk_menu_shell_insert(GTK_MENU_SHELL(m),
+                   wim->menu, is_associated(wi, wis) ? 0 : position);
+               position++;
        }
 
        return m;
index 143e841cba0452b4a592a39fb7357994da120ba6..2b740264d22c4622ab49823bed80b7b45ab110b8 100644 (file)
@@ -282,7 +282,6 @@ void DhcpcdQt::ifCallback(DHCPCD_IF *i)
                        }
                }
        }
-       
 }
 
 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
@@ -462,7 +461,7 @@ void DhcpcdQt::notify(QString &title, QString &msg,
        n->setText(msg);
        n->sendEvent();
 #else
-       trayIcon->showMessage(title, msg, icon);
+       //trayIcon->showMessage(title, msg, icon);
 #endif
 }
 
index bb1e0ac9c671d23202ae384a1d590ab6ae9c80bd..b1995d90744c56df7dafdb27f8bbcee666cbfb50 100644 (file)
@@ -69,6 +69,14 @@ void DhcpcdSsidMenu::setScan(DHCPCD_WI_SCAN *scan)
                ssidWidget->setScan(scan);
 }
 
+bool DhcpcdSsidMenu::isAssociated()
+{
+
+       if (ssidWidget)
+               return ssidWidget->isAssociated();
+       return false;
+}
+
 void DhcpcdSsidMenu::hover()
 {
 
index 672fb8cb88eb9ac5695ad47f63d8712dbd96c917..108409ca224910f097bf77e0286be7bffa9f5a69 100644 (file)
@@ -45,6 +45,7 @@ public:
        QWidget *createWidget(QWidget *parent);
        DHCPCD_WI_SCAN *getScan();
        void setScan(DHCPCD_WI_SCAN *scan);
+       bool isAssociated();
 
 public slots:
        void hover();
index df07fdf468bd6af79d944c9001e78c1b1b17729c..5f62d88a71f9c4ca7410703ad20dfc6f6cb43829 100644 (file)
@@ -80,6 +80,12 @@ DHCPCD_WI_SCAN *DhcpcdSsidMenuWidget::getScan()
        return scan;
 }
 
+bool DhcpcdSsidMenuWidget::isAssociated()
+{
+
+       return associated;
+}
+
 void DhcpcdSsidMenuWidget::setScan(DHCPCD_WI_SCAN *scan)
 {
        DHCPCD_WPA *wpa;
@@ -90,13 +96,17 @@ void DhcpcdSsidMenuWidget::setScan(DHCPCD_WI_SCAN *scan)
        this->scan = scan;
        wpa = wi->getWpa();
        i = dhcpcd_wpa_if(wpa);
+       associated = dhcpcd_wi_associated(i, scan);
 
-       if (i->up && i->ssid && strcmp(scan->ssid, i->ssid) == 0) {
+       if (associated) {
                icon = DhcpcdQt::getIcon("actions", "dialog-ok-apply");
                picon = icon.pixmap(16, 16);
                selicon->setPixmap(picon);
-       } else
+               ssid->setStyleSheet("font:bold;");
+       } else {
                selicon->setPixmap(NULL);
+               ssid->setStyleSheet(NULL);
+       }
        ssid->setText(scan->ssid);
        if (scan->flags & WSF_SECURE)
                icon = DhcpcdQt::getIcon("status",
@@ -136,12 +146,18 @@ bool DhcpcdSsidMenuWidget::eventFilter(QObject *, QEvent *event)
 void DhcpcdSsidMenuWidget::enterEvent(QEvent *)
 {
 
-       ssid->setStyleSheet("color:palette(highlighted-text);");
+       if (associated)
+               ssid->setStyleSheet("color:palette(highlighted-text); font:bold;");
+       else
+               ssid->setStyleSheet("color:palette(highlighted-text);");
        emit hovered();
 }
 
 void DhcpcdSsidMenuWidget::leaveEvent(QEvent *)
 {
 
-       ssid->setStyleSheet("color:palette(text);");
+       if (associated)
+               ssid->setStyleSheet("color:palette(text); font:bold;");
+       else
+               ssid->setStyleSheet("color:palette(text);");
 }
index 26090e1491070d1ab2db6d12448bc586b7caf3ab..833d9e071318f86f13061b65399ebe2529ea2741 100644 (file)
@@ -43,6 +43,7 @@ public:
 
        DHCPCD_WI_SCAN *getScan();
        void setScan(DHCPCD_WI_SCAN *scan);
+       bool isAssociated();
 
 signals:
        void triggered();
@@ -58,6 +59,7 @@ protected slots:
 private:
        DhcpcdWi *wi;
        DHCPCD_WI_SCAN *scan;
+       bool associated;
 
        QLabel *selicon;
        QLabel *ssid;
index 69988d3ec87464998d64cd19693e07abd2b1521a..264743babddb9d8c45b9f6e2d53ea6f40502f426 100644 (file)
@@ -86,33 +86,53 @@ DHCPCD_WI_SCAN *DhcpcdWi::getScans()
 
 bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
 {
-       int changed = 0;
+       bool changed = false;
 
        if (menu) {
                QList<DhcpcdSsidMenu*> lst;
                DHCPCD_WI_SCAN *scan;
+               DHCPCD_IF *i;
+               bool found, associated;
+               QAction *before;
 
+               i = dhcpcd_wpa_if(wpa);
                for (scan = scans; scan; scan = scan->next) {
-                       bool found = false;
-                       QAction *before = NULL;
+                       found = false;
+                       before = NULL;
+                       associated = dhcpcd_wi_associated(i, scan);
 
                        lst = menu->findChildren<DhcpcdSsidMenu*>();
                        foreach(DhcpcdSsidMenu *sm, lst) {
                                DHCPCD_WI_SCAN *s = sm->getScan();
-                               if (memcmp(scan->ssid, s->ssid,
-                                   sizeof(scan->ssid)) == 0)
-                               {
+
+                               if (strcmp(scan->ssid, s->ssid) == 0) {
+                                       /* If association changes, remove
+                                        * the entry and re-create it
+                                        * so assoicates entries appear at
+                                        * the top */
+                                       if (associated != sm->isAssociated()) {
+                                               menu->removeAction(sm);
+                                               break;
+                                       }
                                        sm->setScan(scan);
                                        found = true;
                                        break;
                                }
-                               if (dhcpcd_wi_scan_compare(scan, s) < 0)
+                               if (!associated &&
+                                   dhcpcd_wi_scan_compare(scan, s) < 0)
                                        before = sm;
                        }
 
                        if (!found) {
+                               if (associated) {
+                                       lst = menu->findChildren<DhcpcdSsidMenu*>();
+                                       if (lst.empty())
+                                               before = NULL;
+                                       else
+                                               before = lst.at(0);
+                               }
                                createMenuItem(menu, scan, before);
-                               changed++;
+                               changed = true;
                        }
                }
 
@@ -120,13 +140,12 @@ bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
                foreach(DhcpcdSsidMenu *sm, lst) {
                        DHCPCD_WI_SCAN *s = sm->getScan();
                        for (scan = scans; scan; scan = scan->next) {
-                               if (memcmp(scan->ssid, s->ssid,
-                                   sizeof(scan->ssid)) == 0)
+                               if (strcmp(scan->ssid, s->ssid) == 0)
                                        break;
                        }
                        if (scan == NULL) {
                                menu->removeAction(sm);
-                               changed--;
+                               changed = true;
                        }
                }
        }
@@ -134,7 +153,7 @@ bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
        dhcpcd_wi_scans_free(this->scans);
        this->scans = scans;
 
-       return !(changed == 0);
+       return changed;
 }
 
 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan,
index 5b90036b5eafc74dece8b9a59669c9bd63bd4619..c980aa30cbdbbf5fd6b4268bd22aa1a10c9785d4 100644 (file)
@@ -235,6 +235,7 @@ void dhcpcd_wpa_set_status_callback(DHCPCD_CONNECTION *,
     void (*)(DHCPCD_WPA *, const char *, void *), void *);
 int dhcpcd_wi_scan_compare(DHCPCD_WI_SCAN *a, DHCPCD_WI_SCAN *b);
 DHCPCD_WI_SCAN * dhcpcd_wi_scans(DHCPCD_IF *);
+bool dhcpcd_wi_associated(DHCPCD_IF *i, DHCPCD_WI_SCAN *s);
 void dhcpcd_wi_scans_free(DHCPCD_WI_SCAN *);
 void dhcpcd_wi_history_clear(DHCPCD_CONNECTION *);
 bool dhcpcd_wpa_set_network(DHCPCD_WPA *, int, const char *, const char *);
index 358b37333d653a69ba9fbef6012bedc57dbbf98c..8d53351c504ea74282b7b6674c0b7e5233376a99 100644 (file)
@@ -168,6 +168,16 @@ dhcpcd_wpa_scan(DHCPCD_WPA *wpa)
        return dhcpcd_wpa_command(wpa, "SCAN");
 }
 
+bool
+dhcpcd_wi_associated(DHCPCD_IF *i, DHCPCD_WI_SCAN *scan)
+{
+
+       assert(i);
+       assert(scan);
+
+       return (i->up && i->ssid && strcmp(i->ssid, scan->ssid) == 0);
+}
+
 void
 dhcpcd_wi_scans_free(DHCPCD_WI_SCAN *wis)
 {