fixed logging
[psensor.git] / src / ui_notify.c
1 /*
2  * Copyright (C) 2010-2011 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/time.h>
22
23 #include <libnotify/notify.h>
24
25 /* Macro defined since libnotify 0.5.2 */
26 #ifndef NOTIFY_CHECK_VERSION
27 #define NOTIFY_CHECK_VERSION(x, y, z) 0
28 #endif
29
30 #include "ui.h"
31 #include "ui_notify.h"
32
33 /* Time of the last notification. */
34 static struct timeval last_notification_tv;
35
36 void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
37 {
38         struct timeval t;
39         char *name;
40         NotifyNotification *notif;
41
42         log_printf(LOG_DEBUG,
43                    "ui_notify() last_notification %d",
44                    last_notification_tv.tv_sec);
45
46         if (gettimeofday(&t, NULL) != 0) {
47                 log_printf(LOG_ERR,  _("gettimeofday failed"));
48                 return;
49         }
50
51         if (!last_notification_tv.tv_sec
52             || t.tv_sec - last_notification_tv.tv_sec >= 60)
53                 last_notification_tv = t;
54         else
55                 return ;
56
57         if (notify_is_initted() == FALSE)
58                 notify_init("psensor");
59
60         if (notify_is_initted() == TRUE) {
61                 name = strdup(sensor->name);
62
63                 /*
64                  * Since libnotify 0.7 notify_notification_new has
65                  * only 3 parameters.
66                  */
67 #if NOTIFY_CHECK_VERSION(0, 7, 0)
68                 notif = notify_notification_new
69                         (_("Temperature alert"), name, NULL);
70 #else
71                 notif = notify_notification_new(_("Temperature alert"),
72                                                 name,
73                                                 NULL,
74                                                 GTK_WIDGET(ui->main_window));
75 #endif
76                 log_printf(LOG_DEBUG,
77                            "ui_notify() notif_notification_new %s",
78                            sensor->name);
79
80                 notify_notification_show(notif, NULL);
81
82                 g_object_unref(notif);
83         } else {
84                 log_printf(LOG_ERR, "notify not initted");
85         }
86 }