Fix notifications
[dhcpcd-ui] / src / dhcpcd-gtk / main.c
index 9734ad50e152675f51a7dbe9c44f09ed833e2bf7..3319cacfec059b842b5fb808523e936427cfac07 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * dhcpcd-gtk
- * Copyright 2009-2010 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
@@ -31,6 +31,9 @@
 
 #ifdef NOTIFY
 #  include <libnotify/notify.h>
+#ifndef NOTIFY_CHECK_VERSION
+#  define NOTIFY_CHECK_VERSION(a,b,c) 0
+#endif
 static NotifyNotification *nn;
 #endif
 
@@ -38,14 +41,14 @@ static NotifyNotification *nn;
 #include "dhcpcd-gtk.h"
 
 static GtkStatusIcon *status_icon;
-static int ani_timer;
+static guint ani_timer;
 static int ani_counter;
 static bool online;
 static bool carrier;
 
 struct watch {
        struct pollfd pollfd;
-       int eventid;
+       guint eventid;
        GIOChannel *gio;
        struct watch *next;
 };
@@ -73,7 +76,7 @@ static gboolean
 animate_carrier(_unused gpointer data)
 {
        const char *icon;
-       
+
        if (ani_timer == 0)
                return false;
 
@@ -97,7 +100,7 @@ static gboolean
 animate_online(_unused gpointer data)
 {
        const char *icon;
-       
+
        if (ani_timer == 0)
                return false;
 
@@ -126,23 +129,23 @@ update_online(DHCPCD_CONNECTION *con, bool showif)
        msgs = NULL;
        ifs = dhcpcd_interfaces(con);
        for (i = ifs; i; i = i->next) {
-               if (showif)
-                       g_message("%s: %s", i->ifname, i->reason);
-               if (strcmp(i->reason, "RELEASE") == 0 ||
-                   strcmp(i->reason, "STOP") == 0)
-                       continue;
-               if (dhcpcd_if_up(i))
+               if (i->up)
                        ison = iscarrier = true;
                if (!iscarrier && g_strcmp0(i->reason, "CARRIER") == 0)
                        iscarrier = true;
                msg = dhcpcd_if_message(i);
-               if (msgs) {
-                       tmp = g_strconcat(msgs, "\n", msg, NULL);
-                       g_free(msgs);
-                       g_free(msg);
-                       msgs = tmp;
-               } else
-                       msgs = msg;
+               if (msg) {
+                       if (showif)
+                               g_message("%s", msg);
+                       if (msgs) {
+                               tmp = g_strconcat(msgs, "\n", msg, NULL);
+                               g_free(msgs);
+                               g_free(msg);
+                               msgs = tmp;
+                       } else
+                               msgs = msg;
+               } else if (showif)
+                       g_message("%s: %s", i->ifname, i->reason);
        }
 
        if (online != ison || carrier != iscarrier) {
@@ -163,7 +166,7 @@ update_online(DHCPCD_CONNECTION *con, bool showif)
                            "network-offline");
                }
        }
-       gtk_status_icon_set_tooltip(status_icon, msgs);
+       gtk_status_icon_set_tooltip_text(status_icon, msgs);
        g_free(msgs);
 }
 
@@ -177,6 +180,8 @@ notify_close(void)
 }
 
 #ifdef NOTIFY
+static char *notify_last_msg;
+
 static void
 notify_closed(void)
 {
@@ -188,17 +193,36 @@ notify(const char *title, const char *msg, const char *icon)
 {
        char **msgs, **m;
 
+       if (msg == NULL)
+               return;
+       /* Don't spam the same message */
+       if (notify_last_msg) {
+               if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
+                       return;
+               g_free(notify_last_msg);
+       }
+       notify_last_msg = g_strdup(msg);
+
        msgs = g_strsplit(msg, "\n", 0);
        for (m = msgs; *m; m++)
                g_message("%s", *m);
        g_strfreev(msgs);
+
        if (nn != NULL)
                notify_notification_close(nn, NULL);
+
+#if NOTIFY_CHECK_VERSION(0,7,0)
+       nn = notify_notification_new(title, msg, icon);
+       notify_notification_set_hint(nn, "transient",
+           g_variant_new_boolean(TRUE));
+#else
        if (gtk_status_icon_get_visible(status_icon))
                nn = notify_notification_new_with_status_icon(title,
                    msg, icon, status_icon);
        else
                nn = notify_notification_new(title, msg, icon, NULL);
+#endif
+
        notify_notification_set_timeout(nn, 5000);
        g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
        notify_notification_show(nn, NULL);
@@ -213,23 +237,26 @@ event_cb(DHCPCD_CONNECTION *con, DHCPCD_IF *i, _unused void *data)
        char *msg;
        const char *icon;
 
-       g_message("%s: %s", i->ifname, i->reason);
+       g_message("interface event: %s: %s", i->ifname, i->reason);
        update_online(con, false);
-       
+
        /* We should ignore renew and stop so we don't annoy the user */
        if (g_strcmp0(i->reason, "RENEW") == 0 ||
            g_strcmp0(i->reason, "STOP") == 0)
                return;
 
        msg = dhcpcd_if_message(i);
-       if (dhcpcd_if_up(i))
-               icon = "network-transmit-receive";
-       else
-               icon = "network-transmit";
-       if (dhcpcd_if_down(i))
-               icon = "network-offline";
-       notify(_("Network event"), msg, icon);
-       g_free(msg);
+       if (msg) {
+               g_message("%s", msg);
+               if (i->up)
+                       icon = "network-transmit-receive";
+               //else
+               //      icon = "network-transmit";
+               if (!i->up)
+                       icon = "network-offline";
+               notify(_("Network event"), msg, icon);
+               g_free(msg);
+       }
 }
 
 static void
@@ -245,7 +272,14 @@ status_cb(DHCPCD_CONNECTION *con, const char *status, _unused void *data)
        if (g_strcmp0(status, "down") == 0) {
                msg = N_(last ?
                    "Connection to dhcpcd lost" : "dhcpcd not running");
-               gtk_status_icon_set_tooltip(status_icon, msg);
+               if (ani_timer != 0) {
+                       g_source_remove(ani_timer);
+                       ani_timer = 0;
+                       ani_counter = 0;
+               }
+               gtk_status_icon_set_from_icon_name(status_icon,
+                   "network-offline");
+               gtk_status_icon_set_tooltip_text(status_icon, msg);
                notify(_("No network"), msg, "network-offline");
                dhcpcd_prefs_abort();
                while (wi_scans) {
@@ -356,14 +390,15 @@ add_watch_cb(DHCPCD_CONNECTION *con, const struct pollfd *fd,
 {
        struct watch *w;
        GIOChannel *gio;
-       int flags, eventid;
+       GIOCondition flags;
+       guint eventid;
 
        /* Remove any existing watch */
        delete_watch_cb(con, fd, data);
-       
+
        gio = g_io_channel_unix_new(fd->fd);
        if (gio == NULL) {
-               g_error(_("Error creating new GIO Channel\n"));
+               g_warning(_("Error creating new GIO Channel\n"));
                return;
        }
        flags = 0;
@@ -377,7 +412,7 @@ add_watch_cb(DHCPCD_CONNECTION *con, const struct pollfd *fd,
                flags |= G_IO_HUP;
        if ((eventid = g_io_add_watch(gio, flags, gio_callback, con)) == 0) {
                g_io_channel_unref(gio);
-               g_error(_("Error creating watch\n"));
+               g_warning(_("Error creating watch\n"));
                return;
        }
        w = g_malloc(sizeof(*w));
@@ -394,19 +429,19 @@ main(int argc, char *argv[])
        char *error = NULL;
        char *version = NULL;
        DHCPCD_CONNECTION *con;
-               
+
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, NULL);
        bind_textdomain_codeset(PACKAGE, "UTF-8");
-       textdomain(PACKAGE); 
+       textdomain(PACKAGE);
 
        gtk_init(&argc, &argv);
        g_set_application_name("Network Configurator");
        gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
            ICONDIR);
        status_icon = gtk_status_icon_new_from_icon_name("network-offline");
-       
-       gtk_status_icon_set_tooltip(status_icon,
+
+       gtk_status_icon_set_tooltip_text(status_icon,
            _("Connecting to dhcpcd ..."));
        gtk_status_icon_set_visible(status_icon, true);
 
@@ -421,7 +456,8 @@ main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
 
-       gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
+       gtk_status_icon_set_tooltip_text(status_icon,
+           _("Triggering dhcpcd ..."));
        online = false;
 
        if (!dhcpcd_command(con, "GetVersion", NULL, &version)) {
@@ -433,8 +469,10 @@ main(int argc, char *argv[])
 
        dhcpcd_set_watch_functions(con, add_watch_cb, delete_watch_cb, NULL);
        dhcpcd_set_signal_functions(con, event_cb, status_cb, scan_cb, NULL);
-       if (dhcpcd_error(con))
-               g_error("libdhcpcd: %s", dhcpcd_error(con));
+       if (dhcpcd_error(con)) {
+               g_critical("libdhcpcd: %s", dhcpcd_error(con));
+               exit(EXIT_FAILURE);
+       }
 
        menu_init(status_icon, con);