Fix notifications
[dhcpcd-ui] / src / dhcpcd-gtk / main.c
index 1a349e9af20cf217ab6efc310ac06698cc5a7b78..3319cacfec059b842b5fb808523e936427cfac07 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * dhcpcd-gtk
- * Copyright 2009-2012 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
@@ -41,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;
 };
@@ -76,7 +76,7 @@ static gboolean
 animate_carrier(_unused gpointer data)
 {
        const char *icon;
-       
+
        if (ani_timer == 0)
                return false;
 
@@ -100,7 +100,7 @@ static gboolean
 animate_online(_unused gpointer data)
 {
        const char *icon;
-       
+
        if (ani_timer == 0)
                return false;
 
@@ -121,44 +121,31 @@ animate_online(_unused gpointer data)
 static void
 update_online(DHCPCD_CONNECTION *con, bool showif)
 {
-       bool ison, iscarrier, isstop;
+       bool ison, iscarrier;
        char *msg, *msgs, *tmp;
-       DHCPCD_IF *ifs, *i, *j;
+       DHCPCD_IF *ifs, *i;
 
        ison = iscarrier = false;
        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 (strcmp(i->type, "ipv4") != 0) {
-                       isstop = false;
-                       for (j = ifs; j; j = j->next)
-                               if (strcmp(j->ifname, i->ifname) == 0 &&
-                                   strcmp(j->type, "ipv4") == 0)
-                               {
-                                       if (strcmp(j->reason, "STOP") == 0)
-                                               isstop = true;
-                                       break;
-                               }
-                       if (isstop)
-                               continue;
-               }
                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) {
@@ -206,9 +193,11 @@ 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 (strcmp(msg, notify_last_msg) == 0)
+               if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
                        return;
                g_free(notify_last_msg);
        }
@@ -248,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 (i->up)
-               icon = "network-transmit-receive";
-       //else
-       //      icon = "network-transmit";
-       if (!i->up)
-               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
@@ -398,11 +390,12 @@ 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_warning(_("Error creating new GIO Channel\n"));
@@ -436,11 +429,11 @@ 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");