added comment about libnotify 0.7 support
[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 modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/time.h>
23
24 #include <libnotify/notify.h>
25
26 #include "ui.h"
27 #include "ui_notify.h"
28
29 void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
30 {
31         struct timeval *t = malloc(sizeof(struct timeval));
32         char *name;
33         NotifyNotification *notif;
34
35         if (gettimeofday(t, NULL) != 0) {
36                 fprintf(stderr, _("ERROR: failed gettimeofday\n"));
37                 free(t);
38
39                 return;
40         }
41
42         if (!ui->notification_last_time) {
43                 /* first notification */
44                 ui->notification_last_time = t;
45         } else {
46
47                 if (t->tv_sec - ui->notification_last_time->tv_sec < 60) {
48                         /* last notification less than 1mn ago */
49                         free(t);
50                         return;
51                 } else {
52                         /* last notification more than 1mn ago */
53                         free(ui->notification_last_time);
54                         ui->notification_last_time = t;
55                 }
56         }
57
58         if (notify_is_initted() == FALSE)
59                 notify_init("psensor");
60
61         if (notify_is_initted() == TRUE) {
62                 name = strdup(sensor->name);
63
64 #ifdef NOTIFY_VERSION_MAJOR
65                 /*
66                    since libnotify 0.7 notify_notification_new has
67                    only 3 parameters.
68
69                    libnotify < 0.7 does not define
70                    NOTIFY_VERSION_MAJOR
71                 */
72                 notif = notify_notification_new(_("Temperature alert"),
73                                                 name,
74                                                 NULL);
75 #else
76                 notif = notify_notification_new(_("Temperature alert"),
77                                                 name,
78                                                 NULL,
79                                                 GTK_WIDGET(ui->main_window));
80 #endif
81
82
83                 notify_notification_show(notif, NULL);
84
85                 g_object_unref(notif);
86         }
87 }