Don't popup a menu if no scan results.
[dhcpcd-ui] / menu.c
diff --git a/menu.c b/menu.c
index 1a658088277aba983d3c6feafe98b00c4d688f02..a826c9f34fe6d6d6cdd7a41a8cc3a7ff5844fdee 100644 (file)
--- a/menu.c
+++ b/menu.c
@@ -26,6 +26,7 @@
 
 #include "dhcpcd-gtk.h"
 #include "menu.h"
+#include "wpa.h"
 
 static const char *copyright = "Copyright (c) 2009 Roy Marples";
 
@@ -71,6 +72,12 @@ url_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer p)
 }
 
 static void
+ssid_hook(_unused GtkMenuItem *item, gpointer data)
+{
+       wpa_configure((const struct if_ap *)data);
+}
+
+static void
 on_about(_unused GtkMenuItem *item, _unused gpointer data)
 {
        gtk_window_set_default_icon_name(GTK_STOCK_NETWORK);
@@ -91,21 +98,48 @@ add_scan_results(GtkMenu *menu, const struct if_msg *ifm)
 {
        GSList *gl;
        const struct if_ap *ifa;
-       GtkWidget *item, *image;
+       GtkWidget *item, *image, *box, *label, *bar;
+       double perc;
+       int strength;
+       const char *icon;
+       char *tip;
 
        for (gl = ifm->scan_results; gl; gl = gl->next) {
                ifa = (const struct if_ap *)gl->data;
-               item = gtk_image_menu_item_new_with_label(ifa->ssid);
-               image = NULL;
+               item = gtk_check_menu_item_new();
+               gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(item), TRUE); 
+               box = gtk_hbox_new(FALSE, 6);
+               gtk_container_add(GTK_CONTAINER(item), box); 
+               label = gtk_label_new(ifa->ssid);
+               gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
+
                if (g_strcmp0(ifm->ssid, ifa->ssid) == 0)
-                       image = gtk_image_new_from_icon_name("network-wireless",
-                                                            GTK_ICON_SIZE_MENU);
-               if (!image && ifa->flags != NULL)
-                       image = gtk_image_new_from_icon_name("lock",
-                                                            GTK_ICON_SIZE_MENU);
-               if (image)
-                       gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
-                                                     image);
+                       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
+               if (ifa->flags == NULL)
+                       icon = "network-wireless";
+               else
+                       icon = "network-wireless-encrypted";
+               image = gtk_image_new_from_icon_name(icon,
+                                                    GTK_ICON_SIZE_MENU);
+               gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
+
+               bar = gtk_progress_bar_new();
+               gtk_widget_set_size_request(bar, 100, -1);
+               gtk_box_pack_end(GTK_BOX(box), bar, FALSE, TRUE, 0);
+               strength = CLAMP(ifa->quality, 0, 100);
+               perc = strength / 100.0;
+               gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), perc);
+
+               tip = g_strconcat(ifa->bssid, " ", ifa->flags, NULL);
+               gtk_widget_set_tooltip_text(item, tip);
+               g_free(tip);
+
+               gtk_widget_show(label);
+               gtk_widget_show(bar);
+               gtk_widget_show(image);
+               gtk_widget_show(box);
+               g_signal_connect(G_OBJECT(item), "activate",
+                               G_CALLBACK(ssid_hook), gl->data);
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
        }
 }
@@ -116,18 +150,21 @@ on_activate(GtkStatusIcon *icon, _unused guint button, _unused guint32 atime, _u
        GtkMenu *menu;
        const struct if_msg *ifm;
        GList *gl;
-       size_t n;
+       size_t n, na;
 
        notify_close();
 
-       n = 0;
+       n = na =0;
        for (gl = interfaces; gl; gl = gl->next) {
                ifm = (const struct if_msg *)gl->data;
-               if (ifm->wireless)
-                       if (++n > 1)
+               if (ifm->wireless) {
+                       if (ifm->scan_results != NULL)
+                               ++na;
+                       if (++n > 1 && na != 0)
                                break;
+               }
        }
-       if (n == 0)
+       if (n == 0 || (n == 1 && na == 0))
                return;
 
        menu = (GtkMenu *)gtk_menu_new();