Merge tag 'upstream/0.8.0.6'
[psensor-pkg-debian.git] / src / ui_notify.c
1 /*
2  * Copyright (C) 2010-2014 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 "cfg.h"
31 #include "ui.h"
32 #include "ui_notify.h"
33
34 /* Time of the last notification. */
35 static struct timeval last_notification_tv;
36
37 void ui_notify(struct psensor *sensor, struct ui_psensor *ui)
38 {
39         struct timeval t;
40         char *body, *svalue;
41         const char *summary;
42         NotifyNotification *notif;
43         unsigned int use_celcius;
44
45         log_debug("last_notification %d", last_notification_tv.tv_sec);
46
47         if (gettimeofday(&t, NULL) != 0) {
48                 log_err(_("gettimeofday failed."));
49                 return;
50         }
51
52         if (!last_notification_tv.tv_sec
53             || t.tv_sec - last_notification_tv.tv_sec >= 60)
54                 last_notification_tv = t;
55         else
56                 return ;
57
58         if (notify_is_initted() == FALSE)
59                 notify_init("psensor");
60
61         if (notify_is_initted() == TRUE) {
62                 if (ui->config->temperature_unit == CELCIUS)
63                         use_celcius = 1;
64                 else
65                         use_celcius = 0;
66
67                 svalue = psensor_measure_to_str
68                         (psensor_get_current_measure(sensor),
69                          sensor->type,
70                          use_celcius);
71
72                 body = malloc(strlen(sensor->name) + 3 + strlen(svalue) + 1);
73                 sprintf(body, "%s : %s", sensor->name, svalue);
74                 free(svalue);
75
76                 if (is_temp_type(sensor->type))
77                         summary = _("Temperature alert");
78                 else if (is_fan_type(sensor->type))
79                         summary = _("Fan alert");
80                 else
81                         summary = _("N/A");
82
83                 /*
84                  * Since libnotify 0.7 notify_notification_new has
85                  * only 3 parameters.
86                  */
87 #if NOTIFY_CHECK_VERSION(0, 7, 0)
88                 notif = notify_notification_new(summary, body, PSENSOR_ICON);
89 #else
90                 notif = notify_notification_new(summary,
91                                                 body,
92                                                 PSENSOR_ICON,
93                                                 GTK_WIDGET(ui->main_window));
94 #endif
95                 log_debug("notif_notification_new %s", body);
96
97                 notify_notification_show(notif, NULL);
98
99                 g_object_unref(notif);
100         } else {
101                 log_err("notify not initialized");
102         }
103 }