fixed libnotify version check
[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 #include "ui.h"
26 #include "ui_notify.h"
27
28 void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
29 {
30         struct timeval *t = malloc(sizeof(struct timeval));
31         char *name;
32         NotifyNotification *notif;
33
34         if (gettimeofday(t, NULL) != 0) {
35                 fprintf(stderr, _("ERROR: failed gettimeofday\n"));
36                 free(t);
37
38                 return;
39         }
40
41         if (!ui->notification_last_time) {
42                 /* first notification */
43                 ui->notification_last_time = t;
44         } else {
45
46                 if (t->tv_sec - ui->notification_last_time->tv_sec < 60) {
47                         /* last notification less than 1mn ago */
48                         free(t);
49                         return;
50                 } else {
51                         /* last notification more than 1mn ago */
52                         free(ui->notification_last_time);
53                         ui->notification_last_time = t;
54                 }
55         }
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                  * NOTIFY_CHECK_VERSION exists since 0.5.2.
68                  */             
69 #ifdef NOTIFY_CHECK_VERSION
70 #if NOTIFY_CHECK_VERSION(0, 7, 0)
71                 notif = notify_notification_new(_("Temperature alert"),
72                                                 name,
73                                                 NULL);
74 #else
75                 notif = notify_notification_new(_("Temperature alert"),
76                                                 name,
77                                                 NULL,
78                                                 GTK_WIDGET(ui->main_window));
79 #endif
80 #else           
81                 notif = notify_notification_new(_("Temperature alert"),
82                                                 name,
83                                                 NULL,
84                                                 GTK_WIDGET(ui->main_window));
85 #endif
86
87                 notify_notification_show(notif, NULL);
88
89                 g_object_unref(notif);
90         }
91 }