diff --git a/ChangeLog b/ChangeLog index 7782fe2749c2cf9998b784a77f516484d7670740..46ef8caaaadef20db490a54d2a46d09b0fc3d1d8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ +2009-03-09 23:09 Ali aliov@xfce.org + * : Changes in the battery tooltips/notifications + 2009-03-09 13:22 Ali aliov@xfce.org * : Bump xfce dependencies version to 4.6.0 diff --git a/libxfpm/hal-device.c b/libxfpm/hal-device.c index 06702c2e1f16e3ce044e4004e0703cdb2805e235..bc12734b49921f37566b04a6b83cda7a04b2d0e0 100644 --- a/libxfpm/hal-device.c +++ b/libxfpm/hal-device.c @@ -81,7 +81,7 @@ struct HalDevicePrivate guint32 reporting_design; guint32 reporting_last_full; - gint time; + guint time; }; @@ -325,7 +325,7 @@ static void hal_device_get_property(GObject *object, g_value_set_uint (value, device->priv->reporting_last_full); break; case PROP_TIME: - g_value_set_int (value, device->priv->time); + g_value_set_uint (value, device->priv->time); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object,prop_id,pspec); diff --git a/libxfpm/hal-power.c b/libxfpm/hal-power.c index 3a4f808712cf2726698e49d6d418f4e87327c399..ad08e4c7071d87f667ab62516555f26e30e0d298 100644 --- a/libxfpm/hal-power.c +++ b/libxfpm/hal-power.c @@ -241,8 +241,9 @@ hal_power_init(HalPower *power) hal_ctx_set_device_added_callback (power->priv->ctx, hal_power_device_added_cb); hal_ctx_set_device_removed_callback (power->priv->ctx, hal_power_device_removed_cb); - hal_power_get_batteries (power); + hal_power_get_adapter (power); + hal_power_get_batteries (power); out: ; diff --git a/src/xfpm-battery.c b/src/xfpm-battery.c index bc0d849b3c5b5c5e346f464b443647f207b0f2a4..f609a07b46545166924a500e403325ad16df4c32 100644 --- a/src/xfpm-battery.c +++ b/src/xfpm-battery.c @@ -55,8 +55,7 @@ struct XfpmBatteryPrivate { XfpmTrayIcon *icon; HalDevice *device; - XfpmNotify *notify; - + HalDeviceType type; gchar *icon_prefix; @@ -117,7 +116,6 @@ xfpm_battery_init(XfpmBattery *battery) battery->priv->icon = xfpm_tray_icon_new(); battery->priv->show_icon = SHOW_ICON_ALWAYS; - battery->priv->notify = xfpm_notify_new (); } static void @@ -235,8 +233,6 @@ xfpm_battery_refresh_icon (XfpmBattery *battery, g_free(icon); return; } - - } static void @@ -250,69 +246,153 @@ xfpm_battery_refresh_state (XfpmBattery *battery, XfpmBatteryState state) } } -static void -xfpm_battery_refresh_tooltip (XfpmBattery *battery, - gboolean is_present, - gboolean is_charging, - gboolean is_discharging, - guint32 last_full, - guint32 current_charge, - guint percentage) +const gchar * +_get_battery_name (HalDeviceType type) { - gchar tip[256]; - XfpmBatteryState state = battery->priv->state; - - if ( !is_present ) - { - sprintf(tip, "%s",_("Not present")); - xfpm_tray_icon_set_tooltip(battery->priv->icon, tip); - return; - } - - sprintf(tip, "%i", percentage); - strcat(tip,_("%")); - strcat(tip," "); + if ( type == HAL_DEVICE_TYPE_UPS) + return _("Your UPS"); + else if ( type == HAL_DEVICE_TYPE_MOUSE ) + return _("Your Mouse battery"); + else if ( type == HAL_DEVICE_TYPE_KEYBOARD ) + return _("Your Keyboard battery"); + else if ( type == HAL_DEVICE_TYPE_CAMERA ) + return _("Your Camera battery"); + else if ( type == HAL_DEVICE_TYPE_PDA) + return _("Your PDA battery"); + + return _("Your Battery"); +} +const gchar * +xfpm_battery_get_battery_state (XfpmBatteryState *state, + gboolean is_charging, + gboolean is_discharging, + guint32 last_full, + guint32 current_charge, + guint percentage) +{ if ( !is_charging && !is_discharging && last_full == current_charge ) { - sprintf(tip, "%s",_("Battery fully charged")); - state = BATTERY_FULLY_CHARGED; - xfpm_tray_icon_set_tooltip(battery->priv->icon, tip); + *state = BATTERY_FULLY_CHARGED; + return _("is fully charged"); } else if ( !is_charging && !is_discharging && last_full != current_charge ) { - strcat(tip,_("Battery charge level")); - state = BATTERY_NOT_FULLY_CHARGED; - xfpm_tray_icon_set_tooltip(battery->priv->icon, tip); + *state = BATTERY_NOT_FULLY_CHARGED; + return _("charge level"); } else if ( is_charging && !is_discharging ) { - strcat(tip,_("Battery is charging")); - state = BATTERY_IS_CHARGING; - xfpm_tray_icon_set_tooltip(battery->priv->icon, tip); + *state = BATTERY_IS_CHARGING; + return _("is charging"); } else if ( !is_charging && is_discharging ) { if ( percentage >= 10 ) { - if ( battery->priv->type == HAL_DEVICE_TYPE_PRIMARY ) - strcat(tip, - battery->priv->adapter_present ? - _("Battery is discharging") : - _("System is running on battery")); - else - strcat(tip, _("Battery is discharging") ); - - state = BATTERY_IS_DISCHARGING; + *state = BATTERY_IS_DISCHARGING; + return _("is discharging"); } else { - strcat(tip, _("Battery is almost empty")); - state = BATTERY_CRITICALLY_LOW; + *state = BATTERY_CRITICALLY_LOW; + return _("is almost empty"); } - xfpm_tray_icon_set_tooltip(battery->priv->icon, tip); } + return ""; +} + +static void +xfpm_battery_refresh_tooltip_misc (XfpmBattery *battery, gboolean is_present, + gboolean is_charging, gboolean is_discharging, + guint32 last_full, guint32 current_charge, + guint percentage, guint time) +{ + gchar *tip; + if ( !is_present ) + { + tip = g_strdup_printf ("%s %s", _get_battery_name(battery->priv->type), _("is not present")); + xfpm_tray_icon_set_tooltip (battery->priv->icon, tip); + g_free(tip); + return; + } + + XfpmBatteryState state = battery->priv->state; + const gchar *str = xfpm_battery_get_battery_state (&state, is_charging, is_discharging, + last_full, current_charge, percentage); + tip = g_strdup_printf("%i%% %s %s", percentage, _get_battery_name(battery->priv->type), str); + //FIXME: Time for misc batteries + xfpm_tray_icon_set_tooltip (battery->priv->icon, tip); + g_free (tip); + xfpm_battery_refresh_state (battery, state); +} + +static void +xfpm_battery_refresh_tooltip_primary (XfpmBattery *battery, gboolean is_present, + gboolean is_charging, gboolean is_discharging, + guint32 last_full, guint32 current_charge, + guint percentage, guint time) +{ + gchar *tip; + const gchar *str; + XfpmBatteryState state = battery->priv->state; + + if ( !is_present ) + { + xfpm_tray_icon_set_tooltip(battery->priv->icon, _("Battery not present")); + return; + } + + str = xfpm_battery_get_battery_state (&state, is_charging, is_discharging, + last_full, current_charge, percentage); + if ( time != 0 && time <= 28800 /* 8 hours */ && + state != BATTERY_FULLY_CHARGED && state != BATTERY_NOT_FULLY_CHARGED ) + { + gchar *time_str; + const gchar *est_time; + + gint minutes, hours, minutes_left; + hours = time / 3600; + minutes = time / 60; + minutes_left = minutes % 60; + + if ( state == BATTERY_IS_DISCHARGING || + state == BATTERY_CHARGE_LOW || + state == BATTERY_CRITICALLY_LOW ) + { + est_time = _("Estimated time left"); + } + else if ( state == BATTERY_IS_CHARGING ) + { + est_time = _("Estimated time to be fully charged"); + } + time_str = g_strdup_printf("%s: %d %s %d %s",est_time, + hours,hours > 1 ? _("hours") : _("hour") , + minutes_left, minutes_left > 1 ? _("minutes") : _("minute")); + + tip = g_strdup_printf ("%i%% %s \n%s %s\n%s", + percentage, + _("Battery"), + str, + battery->priv->adapter_present ? + _("System is running on AC power") : + _("System is running on battery power"), + time_str); + g_free (time_str); + } + else + { + tip = g_strdup_printf ("%i%% %s \n%s", + percentage, + str, + battery->priv->adapter_present ? + _("System is running on AC power") : + _("System is running on battery power")); + } + + xfpm_tray_icon_set_tooltip(battery->priv->icon, tip); + g_free(tip); xfpm_battery_refresh_state (battery, state); } @@ -322,6 +402,7 @@ xfpm_battery_refresh (XfpmBattery *battery) gboolean is_present, is_charging, is_discharging = FALSE; guint percentage = 0; guint32 last_full, current_charge = 0; + guint time = 0; g_object_get (G_OBJECT(battery->priv->device), "is-present", &is_present, @@ -330,13 +411,20 @@ xfpm_battery_refresh (XfpmBattery *battery) "percentage", &percentage, "last-full", &last_full, "current-charge", ¤t_charge, + "time", &time, NULL); xfpm_battery_refresh_icon (battery, is_present, is_charging, is_discharging, percentage); - xfpm_battery_refresh_tooltip (battery, is_present, - is_charging, is_discharging, - last_full, current_charge, - percentage); + battery->priv->type == HAL_DEVICE_TYPE_PRIMARY ? + xfpm_battery_refresh_tooltip_primary (battery, is_present, + is_charging, is_discharging, + last_full, current_charge, + percentage, time) + : + xfpm_battery_refresh_tooltip_misc (battery, is_present, + is_charging, is_discharging, + last_full, current_charge, + percentage, time); } static void @@ -417,6 +505,7 @@ void xfpm_battery_set_adapter_presence (XfpmBattery *battery, gboolean adapter_p g_return_if_fail ( XFPM_IS_BATTERY(battery)); battery->priv->adapter_present = adapter_present; + xfpm_battery_refresh (battery); } void xfpm_battery_set_show_icon (XfpmBattery *battery, XfpmShowIcon show_icon) @@ -452,13 +541,6 @@ GtkStatusIcon *xfpm_battery_get_status_icon (XfpmBattery *battery) } -XfpmNotify * xfpm_battery_get_notify_obj (XfpmBattery *battery) -{ - g_return_val_if_fail (XFPM_IS_BATTERY (battery), NULL); - - return battery->priv->notify; -} - const gchar *xfpm_battery_get_icon_name (XfpmBattery *battery) { g_return_val_if_fail (XFPM_IS_BATTERY (battery), NULL); diff --git a/src/xfpm-battery.h b/src/xfpm-battery.h index 63d1b0c871ff9fa89c14481d84b5b56f658f4598..4f469933fb01da51845aa8203a52003e53c6d841 100644 --- a/src/xfpm-battery.h +++ b/src/xfpm-battery.h @@ -64,7 +64,6 @@ void xfpm_battery_set_show_icon (XfpmBattery *battery, const HalDevice *xfpm_battery_get_device (XfpmBattery *battery); XfpmBatteryState xfpm_battery_get_state (XfpmBattery *battery); GtkStatusIcon *xfpm_battery_get_status_icon (XfpmBattery *battery); -XfpmNotify *xfpm_battery_get_notify_obj (XfpmBattery *battery); const gchar *xfpm_battery_get_icon_name (XfpmBattery *battery); void xfpm_battery_show_info (XfpmBattery *battery); diff --git a/src/xfpm-notify.c b/src/xfpm-notify.c index 0b1e51c4f88cfe0f68d8b0ecceabec3f6a43dc34..3a233499debc7dd49f92986c7fda431c83ef585c 100644 --- a/src/xfpm-notify.c +++ b/src/xfpm-notify.c @@ -144,23 +144,26 @@ xfpm_notify_new(void) void xfpm_notify_show_notification (XfpmNotify *notify, const gchar *title, const gchar *text, const gchar *icon_name, - gint timeout, XfpmNotifyUrgency urgency, - GtkStatusIcon *icon) + gint timeout, gboolean simple, + XfpmNotifyUrgency urgency, GtkStatusIcon *icon) { - xfpm_notify_close_notification (notify); + if ( !simple ) + xfpm_notify_close_notification (notify); - notify->priv->notification = - xfpm_notify_new_notification_internal (notify, title, text); + NotifyNotification *n = xfpm_notify_new_notification_internal (notify, title, text); if ( icon_name ) - xfpm_notify_set_icon (notify, notify->priv->notification, icon_name); + xfpm_notify_set_icon (notify, n, icon_name); if ( icon ) - notify_notification_attach_to_status_icon (notify->priv->notification, icon); + notify_notification_attach_to_status_icon (n, icon); - g_signal_connect (G_OBJECT(notify->priv->notification),"closed", - G_CALLBACK(xfpm_notify_closed_cb), notify); - - notify_notification_show (notify->priv->notification, NULL); + if ( !simple ) + { + g_signal_connect (G_OBJECT(n),"closed", + G_CALLBACK(xfpm_notify_closed_cb), notify); + notify->priv->notification = n; + } + notify_notification_show (n, NULL); } diff --git a/src/xfpm-notify.h b/src/xfpm-notify.h index e4e65ad2a001a7a9443b59592cfd5d5e94a9b8e3..ac0e9ff5f39c3412cd873098a4a88cc6154cf77e 100644 --- a/src/xfpm-notify.h +++ b/src/xfpm-notify.h @@ -61,6 +61,7 @@ void xfpm_notify_show_notification (XfpmNotify *notify, const gchar *text, const gchar *icon_name, gint timeout, + gboolean simple, XfpmNotifyUrgency urgency, GtkStatusIcon *icon); diff --git a/src/xfpm-supply.c b/src/xfpm-supply.c index bae23757834791014d1ad0bea968ff3e8ff5b01f..0ff55e4bcd075946b66678d23b3d7b30546c42ae 100644 --- a/src/xfpm-supply.c +++ b/src/xfpm-supply.c @@ -246,20 +246,8 @@ xfpm_supply_get_message_from_battery_state ( XfpmBatteryState state, HalDeviceTy } } -/* -static gboolean -xfpm_supply_get_running_on_battery (XfpmSupply *supply) -{ - if ( !supply->priv->adapter_found ) - return FALSE; - else if ( supply->priv->adapter_present ) - return FALSE; - - return TRUE; -} -*/ static void -xfpm_supply_battery_state_changed_cb (XfpmBattery *battery, XfpmBatteryState state, XfpmSupply *supply) +xfpm_supply_show_battery_notification (XfpmSupply *supply, XfpmBatteryState state, XfpmBattery *battery) { HalDeviceType type; const HalDevice *device = xfpm_battery_get_device (battery); @@ -268,10 +256,9 @@ xfpm_supply_battery_state_changed_cb (XfpmBattery *battery, XfpmBatteryState sta g_object_get (G_OBJECT(device), "type", &type, NULL); else { - g_critical ("Unable to get device object\n"); + g_critical ("Unable to get device type\n"); return; } - const gchar *message = xfpm_supply_get_message_from_battery_state (state, @@ -279,19 +266,25 @@ xfpm_supply_battery_state_changed_cb (XfpmBattery *battery, XfpmBatteryState sta supply->priv->adapter_found ? supply->priv->adapter_present : TRUE); - if ( !message ) return; - xfpm_notify_show_notification (xfpm_battery_get_notify_obj(battery), + xfpm_notify_show_notification (supply->priv->notify, _("Xfce power manager"), message, xfpm_battery_get_icon_name (battery), 10000, - XFPM_NOTIFY_NORMAL, + FALSE, + XFPM_NOTIFY_NORMAL, xfpm_battery_get_status_icon (battery)); } +static void +xfpm_supply_battery_state_changed_cb (XfpmBattery *battery, XfpmBatteryState state, XfpmSupply *supply) +{ + xfpm_supply_show_battery_notification (supply, state, battery); +} + static void xfpm_supply_show_battery_info (GtkWidget *w, XfpmBattery *battery) { @@ -390,6 +383,31 @@ xfpm_supply_popup_battery_menu_cb (XfpmBattery *battery, GtkStatusIcon *icon, icon, button, activate_time); } +//FIXME: Change the name of this function +static void +xfpm_supply_set_adapter_presence (XfpmSupply *supply) +{ + if ( g_hash_table_size (supply->priv->hash) == 0 ) + return; + + int i; + GList *list = g_hash_table_get_values (supply->priv->hash ); + + if (!list) + return; + + for ( i = 0;i <g_list_length(list); i++) + { + XfpmBattery *battery = NULL; + battery = (XfpmBattery *) g_list_nth_data (list, i); + if ( battery ) + xfpm_battery_set_adapter_presence (battery, supply->priv->adapter_present); + } + + g_list_free (list); + +} + static void xfpm_supply_adapter_changed_cb (XfpmAdapter *adapter, gboolean present, XfpmSupply *supply ) { @@ -397,7 +415,9 @@ xfpm_supply_adapter_changed_cb (XfpmAdapter *adapter, gboolean present, XfpmSupp g_critical ("Callback from the adapter object but no adapter found in the system\n"); supply->priv->adapter_present = present; + xfpm_supply_set_adapter_presence (supply); g_signal_emit (G_OBJECT(supply), signals[ON_BATTERY], 0, !supply->priv->adapter_present); + } static XfpmBattery * @@ -433,12 +453,14 @@ xfpm_supply_add_device (XfpmSupply *supply, const HalDevice *device) g_object_get (G_OBJECT(device), "is-present", &adapter_present, NULL); supply->priv->adapter_present = adapter_present; g_signal_emit (G_OBJECT(supply), signals[ON_BATTERY], 0, supply->priv->adapter_present); + xfpm_supply_set_adapter_presence (supply); } else { TRACE("New battery found %s", udi); XfpmBattery *battery = xfpm_battery_new (device); xfpm_battery_set_show_icon (battery, supply->priv->show_icon); + xfpm_battery_set_adapter_presence (battery, supply->priv->adapter_present); g_hash_table_insert (supply->priv->hash, g_strdup(udi), battery); g_signal_connect (G_OBJECT(battery), "battery-state-changed", @@ -540,7 +562,6 @@ xfpm_supply_set_battery_show_tray_icon (XfpmSupply *supply) } g_list_free (list); - } static void