X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fui_notify.c;h=f55821cabc24f982bcb08a8d71f6d1d80b491180;hb=cc35e177d57bc4ee2cc57ae25f130dfc168aa61d;hp=716dcfcb0100443001598aa9e25208c3866bb632;hpb=f51bc6299cb7a2439b21f3ba43185f56e2311092;p=psensor.git diff --git a/src/ui_notify.c b/src/ui_notify.c index 716dcfc..f55821c 100644 --- a/src/ui_notify.c +++ b/src/ui_notify.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2011 jeanfi@gmail.com + * Copyright (C) 2010-2013 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -22,61 +22,82 @@ #include +/* Macro defined since libnotify 0.5.2 */ +#ifndef NOTIFY_CHECK_VERSION +#define NOTIFY_CHECK_VERSION(x, y, z) 0 +#endif + +#include "cfg.h" #include "ui.h" #include "ui_notify.h" +/* Time of the last notification. */ +static struct timeval last_notification_tv; + void ui_notify(struct psensor *sensor, struct ui_psensor *ui) { - struct timeval *t = malloc(sizeof(struct timeval)); - char *name; + struct timeval t; + char *body, *svalue; + const char *summary; NotifyNotification *notif; + unsigned int use_celcius; - if (gettimeofday(t, NULL) != 0) { - fprintf(stderr, _("ERROR: failed gettimeofday\n")); - free(t); + log_debug("last_notification %d", last_notification_tv.tv_sec); + if (gettimeofday(&t, NULL) != 0) { + log_err(_("gettimeofday failed.")); return; } - if (!ui->notification_last_time) { - /* first notification */ - ui->notification_last_time = t; - } else { - - if (t->tv_sec - ui->notification_last_time->tv_sec < 60) { - /* last notification less than 1mn ago */ - free(t); - return; - } else { - /* last notification more than 1mn ago */ - free(ui->notification_last_time); - ui->notification_last_time = t; - } - } + if (!last_notification_tv.tv_sec + || t.tv_sec - last_notification_tv.tv_sec >= 60) + last_notification_tv = t; + else + return ; if (notify_is_initted() == FALSE) notify_init("psensor"); if (notify_is_initted() == TRUE) { - name = strdup(sensor->name); + if (ui->config->temperature_unit == CELCIUS) + use_celcius = 1; + else + use_celcius = 0; + + svalue = psensor_measure_to_str + (psensor_get_current_measure(sensor), + sensor->type, + use_celcius); + + body = malloc(strlen(sensor->name) + 3 + strlen(svalue) + 1); + sprintf(body, "%s : %s", sensor->name, svalue); + free(svalue); + + if (is_temp_type(sensor->type)) + summary = _("Temperature alert"); + else if (is_fan_type(sensor->type)) + summary = _("Fan alert"); + else + summary = _("N/A"); -#if defined(NOTIFY_CHECK_VERSION) && NOTIFY_CHECK_VERSION(0, 7, 0) /* - * since libnotify 0.7 notify_notification_new has + * Since libnotify 0.7 notify_notification_new has * only 3 parameters. */ - notif = notify_notification_new(_("Temperature alert"), - name, - NULL); +#if NOTIFY_CHECK_VERSION(0, 7, 0) + notif = notify_notification_new(summary, body, PSENSOR_ICON); #else - notif = notify_notification_new(_("Temperature alert"), - name, - NULL, + notif = notify_notification_new(summary, + body, + PSENSOR_ICON, GTK_WIDGET(ui->main_window)); #endif + log_debug("notif_notification_new %s", body); notify_notification_show(notif, NULL); g_object_unref(notif); + } else { + log_err("notify not initialized"); } }