regen build files
[psensor.git] / src / ui_notify.c
index 57eb63a..c92b5fb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2011 jeanfi@gmail.com
+ * Copyright (C) 2010-2012 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
 
 #include <libnotify/notify.h>
 
+/* Macro defined since libnotify 0.5.2 */
+#ifndef NOTIFY_CHECK_VERSION
+#define NOTIFY_CHECK_VERSION(x, y, z) 0
+#endif
+
 #include "ui.h"
 #include "ui_notify.h"
 
+/* Time of the last notification. */
+static struct timeval last_notification_tv;
+
 void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
 {
-       struct timeval *t = malloc(sizeof(struct timeval));
+       struct timeval t;
        char *name;
        NotifyNotification *notif;
 
-       if (gettimeofday(t, NULL) != 0) {
-               fprintf(stderr, _("ERROR: failed gettimeofday\n"));
-               free(t);
+       log_debug("last_notification %d", last_notification_tv.tv_sec);
 
+       if (gettimeofday(&t, NULL) != 0) {
+               log_printf(LOG_ERR,  _("gettimeofday failed"));
                return;
        }
 
-       if (!ui->notification_last_time) {
-               /* first notification */
-               ui->notification_last_time = t;
-       } else {
-
-               if (t->tv_sec - ui->notification_last_time->tv_sec < 60) {
-                       /* last notification less than 1mn ago */
-                       free(t);
-                       return;
-               } else {
-                       /* last notification more than 1mn ago */
-                       free(ui->notification_last_time);
-                       ui->notification_last_time = t;
-               }
-       }
+       if (!last_notification_tv.tv_sec
+           || t.tv_sec - last_notification_tv.tv_sec >= 60)
+               last_notification_tv = t;
+       else
+               return ;
 
        if (notify_is_initted() == FALSE)
                notify_init("psensor");
@@ -63,29 +61,22 @@ void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
                /*
                 * Since libnotify 0.7 notify_notification_new has
                 * only 3 parameters.
-                *
-                * NOTIFY_CHECK_VERSION exists since 0.5.2.
-                */             
-#ifdef NOTIFY_CHECK_VERSION
+                */
 #if NOTIFY_CHECK_VERSION(0, 7, 0)
-               notif = notify_notification_new(_("Temperature alert"),
-                                               name,
-                                               NULL);
+               notif = notify_notification_new
+                       (_("Temperature alert"), name, NULL);
 #else
                notif = notify_notification_new(_("Temperature alert"),
                                                name,
                                                NULL,
                                                GTK_WIDGET(ui->main_window));
 #endif
-#else          
-               notif = notify_notification_new(_("Temperature alert"),
-                                               name,
-                                               NULL,
-                                               GTK_WIDGET(ui->main_window));
-#endif
+               log_debug("notif_notification_new %s", sensor->name);
 
                notify_notification_show(notif, NULL);
 
                g_object_unref(notif);
+       } else {
+               log_printf(LOG_ERR, "notify not initted");
        }
 }