improve code (libnotify version checking)
[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 void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
34 {
35         struct timeval *t = malloc(sizeof(struct timeval));
36         char *name;
37         NotifyNotification *notif;
38
39         if (gettimeofday(t, NULL) != 0) {
40                 fprintf(stderr, _("ERROR: failed gettimeofday\n"));
41                 free(t);
42
43                 return;
44         }
45
46         if (!ui->notification_last_time) {
47                 /* first notification */
48                 ui->notification_last_time = t;
49         } else {
50
51                 if (t->tv_sec - ui->notification_last_time->tv_sec < 60) {
52                         /* last notification less than 1mn ago */
53                         free(t);
54                         return;
55                 } else {
56                         /* last notification more than 1mn ago */
57                         free(ui->notification_last_time);
58                         ui->notification_last_time = t;
59                 }
60         }
61
62         if (notify_is_initted() == FALSE)
63                 notify_init("psensor");
64
65         if (notify_is_initted() == TRUE) {
66                 name = strdup(sensor->name);
67
68                 /*
69                  * Since libnotify 0.7 notify_notification_new has
70                  * only 3 parameters.
71                  */             
72 #if NOTIFY_CHECK_VERSION(0, 7, 0)
73                 notif = notify_notification_new(_("Temperature alert"),
74                                                 name,
75                                                 NULL);
76 #else
77                 notif = notify_notification_new(_("Temperature alert"),
78                                                 name,
79                                                 NULL,
80                                                 GTK_WIDGET(ui->main_window));
81 #endif
82
83                 notify_notification_show(notif, NULL);
84
85                 g_object_unref(notif);
86         }
87 }