System tray could still be loading so just warn and continue.
[dhcpcd-ui] / src / dhcpcd-gtk / menu.c
index 51b74dd522de39e021dae9d31e11bb9e6b1b265e..2216ca01748b77c3573925eab80f8b19a0a1a319 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * dhcpcd-gtk
- * Copyright 2009 Roy Marples <roy@marples.name>
+ * Copyright 2009-2014 Roy Marples <roy@marples.name>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * SUCH DAMAGE.
  */
 
+#include "config.h"
 #include "dhcpcd-gtk.h"
 
-static const char *copyright = "Copyright (c) 2009 Roy Marples";
+static const char *copyright = "Copyright (c) 2009-2014 Roy Marples";
 static const char *authors[] = { "Roy Marples <roy@marples.name>", NULL };
 
+static GtkWidget *menu;
+
 static void
-on_pref(_unused GtkObject *o, gpointer data)
+on_pref(_unused GObject *o, gpointer data)
 {
+
        dhcpcd_prefs_show((DHCPCD_CONNECTION *)data);
 }
 
 static void
 on_quit(void)
 {
+
        gtk_main_quit();
 }
 
+#if GTK_MAJOR_VERSION == 2
 static void
 url_show(GtkAboutDialog *dialog, const char *url)
 {
@@ -60,30 +66,45 @@ email_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer data)
        g_free(address);
 }
 
+
 static void
 url_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer data)
 {
        url_show(dialog, url);
 }
+#endif
 
 static void
-ssid_hook(_unused GtkMenuItem *item, gpointer data)
+ssid_hook(GtkMenuItem *item, _unused gpointer data)
 {
        DHCPCD_WI_SCAN *scan;
        WI_SCAN *wi;
 
-       scan = (DHCPCD_WI_SCAN *)data;
+       scan = g_object_get_data(G_OBJECT(item), "dhcpcd_wi_scan");
        wi = wi_scan_find(scan);
-       if (wi)
-               wpa_configure(wi->connection, wi->interface, scan);
+       if (wi) {
+               DHCPCD_CONNECTION *con;
+
+               con = dhcpcd_if_connection(wi->interface);
+               if (con) {
+                       DHCPCD_WPA *wpa;
+
+                       wpa = dhcpcd_wpa_find(con, wi->interface->ifname);
+                       if (wpa)
+                               wpa_configure(wpa, scan);
+               }
+       }
 }
 
 static void
 on_about(_unused GtkMenuItem *item)
 {
+
        gtk_window_set_default_icon_name("network-transmit-receive");
+#if GTK_MAJOR_VERSION == 2
        gtk_about_dialog_set_email_hook(email_hook, NULL, NULL);
        gtk_about_dialog_set_url_hook(url_hook, NULL, NULL);
+#endif
        gtk_show_about_dialog(NULL,
            "version", VERSION,
            "copyright", copyright,
@@ -95,22 +116,48 @@ on_about(_unused GtkMenuItem *item)
            NULL);
 }
 
+void
+menu_update_scans(DHCPCD_IF *i, DHCPCD_WI_SCAN *scan)
+{
+       GList *lst, *l;
+       DHCPCD_WI_SCAN *s, *wis;
+
+       if (menu == NULL)
+               return;
+       lst = gtk_container_get_children(GTK_CONTAINER(menu));
+       for (s = scan; s; s = s->next) {
+               for (l = lst; l; l = l->next) {
+                       wis = g_object_get_data(G_OBJECT(l->data), "dhcpcd_wi_scan");
+                       if (wis) {
+                               if (memcmp(wis->bssid, s->bssid,
+                                   sizeof(s->bssid)) == 0)
+                               {
+                                       g_object_set_data(G_OBJECT(l->data),
+                                           "dhcpcd_wi_scan", s);
+                                       break;
+                               }
+                       }
+               }
+       }
+}
+
 static GtkWidget *
 add_scans(WI_SCAN *scan)
 {
        DHCPCD_WI_SCAN *wis;
-       GtkWidget *menu, *item, *image, *box, *label, *bar;
+       GtkWidget *item, *image, *box, *label, *bar;
        double perc;
-       int strength;
        const char *icon;
        char *tip;
 
+       if (scan->scans == NULL)
+               return NULL;
        menu = gtk_menu_new();
        for (wis = scan->scans; wis; wis = wis->next) {
                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);
+               box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
                gtk_container_add(GTK_CONTAINER(item), box); 
                label = gtk_label_new(wis->ssid);
                gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
@@ -118,7 +165,7 @@ add_scans(WI_SCAN *scan)
                if (g_strcmp0(wis->ssid, scan->interface->ssid) == 0)
                        gtk_check_menu_item_set_active(
                                GTK_CHECK_MENU_ITEM(item), true);
-               if (wis->flags == NULL)
+               if (wis->flags[0] == '\0')
                        icon = "network-wireless";
                else
                        icon = "network-wireless-encrypted";
@@ -129,20 +176,24 @@ add_scans(WI_SCAN *scan)
                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(wis->quality, 0, 100);
-               perc = strength / 100.0;
+               perc = wis->strength.value / 100.0;
                gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), perc);
 
-               tip = g_strconcat(wis->bssid, " ", wis->flags, NULL);
-               gtk_widget_set_tooltip_text(item, tip);
-               g_free(tip);
+               if (wis->flags[0] == '\0')
+                       gtk_widget_set_tooltip_text(item, wis->bssid);
+               else {
+                       tip = g_strconcat(wis->bssid, " ", wis->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), wis);
+                   G_CALLBACK(ssid_hook), NULL);
+               g_object_set_data(G_OBJECT(item), "dhcpcd_wi_scan", wis);
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
        }
        return menu;
@@ -152,7 +203,7 @@ static void
 on_activate(GtkStatusIcon *icon)
 {
        WI_SCAN *w;
-       GtkWidget *menu, *imenu, *item, *image;
+       GtkWidget *item, *image;
 
        notify_close();
        if (wi_scans == NULL)
@@ -160,7 +211,6 @@ on_activate(GtkStatusIcon *icon)
        if (wi_scans->next) {
                menu = gtk_menu_new();
                for (w = wi_scans; w; w = w->next) {
-                       imenu = gtk_menu_new();
                        item = gtk_image_menu_item_new_with_label(
                                w->interface->ifname);
                        image = gtk_image_new_from_icon_name(
@@ -173,24 +223,26 @@ on_activate(GtkStatusIcon *icon)
                }
        } else
                menu = add_scans(wi_scans);
-       
-       gtk_widget_show_all(GTK_WIDGET(menu));
-       gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
-           gtk_status_icon_position_menu, icon,
-           1, gtk_get_current_event_time());
+
+       if (menu) {
+               gtk_widget_show_all(GTK_WIDGET(menu));
+               gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
+                   gtk_status_icon_position_menu, icon,
+                   1, gtk_get_current_event_time());
+       }
 }
 
 static void
 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
 {
        DHCPCD_CONNECTION *con;
-       GtkMenu *menu;
+       GtkMenu *mnu;
        GtkWidget *item, *image;
 
        notify_close();
 
        con = (DHCPCD_CONNECTION *)data;
-       menu = (GtkMenu *)gtk_menu_new();
+       mnu = (GtkMenu *)gtk_menu_new();
 
        item = gtk_image_menu_item_new_with_mnemonic(_("_Preferences"));
        image = gtk_image_new_from_icon_name(GTK_STOCK_PREFERENCES,
@@ -201,10 +253,10 @@ on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
        else
                g_signal_connect(G_OBJECT(item), "activate",
                    G_CALLBACK(on_pref), data);
-       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+       gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
 
        item = gtk_separator_menu_item_new();
-       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+       gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
 
        item = gtk_image_menu_item_new_with_mnemonic(_("_About"));
        image = gtk_image_new_from_icon_name(GTK_STOCK_ABOUT,
@@ -212,7 +264,7 @@ on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
        gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
        g_signal_connect(G_OBJECT(item), "activate",
            G_CALLBACK(on_about), icon);
-       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+       gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
 
        item = gtk_image_menu_item_new_with_mnemonic(_("_Quit"));
        image = gtk_image_new_from_icon_name(GTK_STOCK_QUIT,
@@ -220,20 +272,34 @@ on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
        gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
        g_signal_connect(G_OBJECT(item), "activate",
            G_CALLBACK(on_quit), icon);
-       gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
+       gtk_menu_shell_append(GTK_MENU_SHELL(mnu), item);
 
-       gtk_widget_show_all(GTK_WIDGET(menu));
-       gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
+       gtk_widget_show_all(GTK_WIDGET(mnu));
+       gtk_menu_popup(GTK_MENU(mnu), NULL, NULL,
            gtk_status_icon_position_menu, icon, button, atime);
        if (button == 0)
-               gtk_menu_shell_select_first(GTK_MENU_SHELL(menu), FALSE);
+               gtk_menu_shell_select_first(GTK_MENU_SHELL(mnu), FALSE);
 }
 
 void
 menu_init(GtkStatusIcon *icon, DHCPCD_CONNECTION *con)
 {
+
        g_signal_connect(G_OBJECT(icon), "activate",
            G_CALLBACK(on_activate), con);
        g_signal_connect(G_OBJECT(icon), "popup_menu",
            G_CALLBACK(on_popup), con);
 }
+
+
+#if GTK_MAJOR_VERSION == 2
+GtkWidget *
+gtk_box_new(GtkOrientation o, gint s)
+{
+
+       if (o == GTK_ORIENTATION_HORIZONTAL)
+               return gtk_hbox_new(false, s);
+       else
+               return gtk_vbox_new(false, s);
+}
+#endif