diff --git a/configure.in.in b/configure.in.in index 682838f..8bf2f78 100644 --- a/configure.in.in +++ b/configure.in.in @@ -64,6 +64,12 @@ XDT_CHECK_PACKAGE([HAL], [hal], [0.5.6]) XDT_CHECK_PACKAGE([DBUS], [dbus-glib-1], [0.34]) dnl *********************************** +dnl *** Check for optional packages *** +dnl *********************************** +XDT_CHECK_OPTIONAL_PACKAGE([LIBNOTIFY], [libnotify], [0.4.0], [libnotify], + [Notification daemon support]) + +dnl *********************************** dnl *** Check for debugging support *** dnl *********************************** XDT_FEATURE_DEBUG() diff --git a/panel-plugin/Makefile.am b/panel-plugin/Makefile.am index 0b73785..60034f5 100644 --- a/panel-plugin/Makefile.am +++ b/panel-plugin/Makefile.am @@ -28,6 +28,7 @@ xfce4_battery_plugin_CFLAGS = \ $(LIBXFCE4UTIL_CFLAGS) \ $(LIBXFCEGUI4_CFLAGS) \ $(LIBXFCE4PANEL_CFLAGS) \ + $(LIBNOTIFY_CFLAGS) \ $(PLATFORM_CFLAGS) xfce4_battery_plugin_LDADD = \ @@ -36,6 +37,7 @@ xfce4_battery_plugin_LDADD = \ $(LIBXFCE4UTIL_LIBS) \ $(LIBXFCEGUI4_LIBS) \ $(LIBXFCE4PANEL_LIBS) \ + $(LIBNOTIFY_LIBS) \ $(PLATFORM_LDFLAGS) desktopdir = $(datadir)/xfce4/panel-plugins diff --git a/panel-plugin/battery-plugin.c b/panel-plugin/battery-plugin.c index 59fad9a..eb09bd5 100644 --- a/panel-plugin/battery-plugin.c +++ b/panel-plugin/battery-plugin.c @@ -36,6 +36,16 @@ #define FRAME_PADDING (1) #define SPACING (2) +#define DEFAULT_PERCENT_WARN1 (15) +#define DEFAULT_PERCENT_WARN2 (7) +#define DEFAULT_PERCENT_CRITICAL (3) + +typedef enum +{ + NOTIFICATION_WARN1 = 0, + NOTIFICATION_WARN2, + NOTIFICATION_CRITICAL, +} NotificationType; static gchar * @@ -442,6 +452,93 @@ battery_plugin_widget_visibility (GtkWidget *widget, void +battery_plugin_notify (BatteryPlugin *plugin, + BatteryInfo *info, + NotificationType type) +{ + guint percent = 0; + gchar *primary; + const gchar *secondary; + + switch (type) + { + case NOTIFICATION_WARN1: + percent = plugin->percent_warn1; + secondary = _("You should probably consider plugging your computer into a power source."); + break; + case NOTIFICATION_WARN2: + percent = plugin->percent_warn2; + secondary = _("Your computer may shut down inadvertently if it is not plugged in."); + break; + case NOTIFICATION_CRITICAL: + percent = plugin->percent_critical; + secondary = _("If your computer loses power, you may lose data."); + break; + default: + return; + } + + primary = g_strdup_printf (_("Your battery level has dropped below %d percent."), + percent); + +#ifdef HAVE_LIBNOTIFY + if (notify_is_initted ()) + { + NotifyNotification *n = plugin->last_notification; + + if (n == NULL) + { + n = notify_notification_new (primary, secondary, "battery", GTK_WIDGET (plugin->panel_plugin)); + g_signal_connect (n, "closed", G_CALLBACK (g_object_unref), NULL); + g_object_add_weak_pointer (G_OBJECT (n), (gpointer)&plugin->last_notification); + } + else + { + notify_notification_update (n, primary, secondary, "battery"); + } + + if (type == NOTIFICATION_CRITICAL) + { + notify_notification_set_timeout (n, NOTIFY_EXPIRES_NEVER); + notify_notification_set_urgency (n, NOTIFY_URGENCY_CRITICAL); + } + else + { + notify_notification_set_timeout (n, 60000); + notify_notification_set_urgency (n, NOTIFY_URGENCY_NORMAL); + } + + if (!notify_notification_show (n, NULL)) + goto notify_failed; + + plugin->last_notification = n; + } + else +#endif + { + GtkWidget *dialog; +#ifdef HAVE_LIBNOTIFY +notify_failed: +#endif + + dialog = gtk_message_dialog_new (GTK_WINDOW (plugin->panel_plugin), + GTK_DIALOG_NO_SEPARATOR + | GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_CLOSE, + primary); + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), + secondary); + g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL); + gtk_widget_show (dialog); + } + + g_free (primary); +} + + + +void battery_plugin_update (BatteryPlugin *plugin) { BatteryInfo *info; @@ -488,6 +585,44 @@ battery_plugin_update (BatteryPlugin *plugin) /* update the tooltip */ battery_plugin_update_tooltip (plugin); #endif + + if (info->is_discharging) + { + if (info->percentage <= plugin->percent_critical) + { + if (!plugin->thrown_critical) + { + battery_plugin_notify (plugin, info, NOTIFICATION_CRITICAL); + plugin->thrown_critical = TRUE; + } + } + else if (info->percentage <= plugin->percent_warn2) + { + if (!plugin->thrown_warn2) + { + battery_plugin_notify (plugin, info, NOTIFICATION_WARN2); + plugin->thrown_warn2 = TRUE; + } + } + else if (info->percentage <= plugin->percent_warn1) + { + if (!plugin->thrown_warn1) + { + battery_plugin_notify (plugin, info, NOTIFICATION_WARN1); + plugin->thrown_warn1 = TRUE; + } + } + } + else + { + plugin->thrown_warn1 = FALSE; + plugin->thrown_warn2 = FALSE; + plugin->thrown_critical = FALSE; +#ifdef HAVE_LIBNOTIFY + if (plugin->last_notification) + notify_notification_close (plugin->last_notification, NULL); +#endif + } } @@ -525,6 +660,9 @@ battery_plugin_load (BatteryPlugin *plugin) plugin->show_status_icon = xfce_rc_read_bool_entry (rc, "ShowStatusIcon", TRUE); plugin->show_progressbar = xfce_rc_read_bool_entry (rc, "ShowProgressBar", FALSE); plugin->show_label = xfce_rc_read_bool_entry (rc, "ShowLabel",TRUE); + plugin->percent_warn1 = xfce_rc_read_int_entry (rc, "PercentWarn1", DEFAULT_PERCENT_WARN1); + plugin->percent_warn2 = xfce_rc_read_int_entry (rc, "PercentWarn2", DEFAULT_PERCENT_WARN2); + plugin->percent_critical = xfce_rc_read_int_entry (rc, "PercentCritical", DEFAULT_PERCENT_CRITICAL); /* close the rc file */ xfce_rc_close (rc); @@ -561,6 +699,9 @@ battery_plugin_save (BatteryPlugin *plugin) xfce_rc_write_bool_entry (rc, "ShowStatusIcon", plugin->show_status_icon); xfce_rc_write_bool_entry (rc, "ShowProgressBar", plugin->show_progressbar); xfce_rc_write_bool_entry (rc, "ShowLabel", plugin->show_label); + xfce_rc_write_int_entry (rc, "PercentWarn1", plugin->percent_warn1); + xfce_rc_write_int_entry (rc, "PercentWarn2", plugin->percent_warn2); + xfce_rc_write_int_entry (rc, "PercentCritical", plugin->percent_critical); /* close the rc file */ xfce_rc_close (rc); @@ -738,6 +879,11 @@ battery_plugin_construct (XfcePanelPlugin *panel_plugin) /* setup translation domain */ xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8"); +#ifdef HAVE_LIBNOTIFY + if (!notify_init (_("Battery Monitor"))) + g_warning ("Unable to contact notification daemon; falling back to normal dialogs"); +#endif + /* create a new battery plugin */ plugin = battery_plugin_new (panel_plugin); diff --git a/panel-plugin/battery-plugin.h b/panel-plugin/battery-plugin.h index e30ff0b..3f135b5 100644 --- a/panel-plugin/battery-plugin.h +++ b/panel-plugin/battery-plugin.h @@ -24,6 +24,10 @@ #include #include +#ifdef HAVE_LIBNOTIFY +#include +#endif + G_BEGIN_DECLS typedef struct _BatteryPlugin BatteryPlugin; @@ -60,6 +64,16 @@ struct _BatteryPlugin guint show_status_icon : 1; guint show_progressbar : 1; guint show_label : 1; + + guint thrown_warn1 : 1, + thrown_warn2 : 1, + thrown_critical : 1; + gint percent_warn1 : 8, + percent_warn2 : 8, + percent_critical : 8; +#ifdef HAVE_LIBNOTIFY + NotifyNotification *last_notification; +#endif };