[Goodies-commits] r6876 - in xfce4-power-manager/trunk: . libxfpm src

Ali Abdallah aliov at xfce.org
Mon Mar 9 23:09:41 CET 2009


Author: aliov
Date: 2009-03-09 22:09:41 +0000 (Mon, 09 Mar 2009)
New Revision: 6876

Modified:
   xfce4-power-manager/trunk/ChangeLog
   xfce4-power-manager/trunk/libxfpm/hal-device.c
   xfce4-power-manager/trunk/libxfpm/hal-power.c
   xfce4-power-manager/trunk/src/xfpm-battery.c
   xfce4-power-manager/trunk/src/xfpm-battery.h
   xfce4-power-manager/trunk/src/xfpm-notify.c
   xfce4-power-manager/trunk/src/xfpm-notify.h
   xfce4-power-manager/trunk/src/xfpm-supply.c
Log:
Changes in the battery tooltips/notifications

Modified: xfce4-power-manager/trunk/ChangeLog
===================================================================
--- xfce4-power-manager/trunk/ChangeLog	2009-03-09 19:29:31 UTC (rev 6875)
+++ xfce4-power-manager/trunk/ChangeLog	2009-03-09 22:09:41 UTC (rev 6876)
@@ -1,4 +1,7 @@
 
+2009-03-09 23:09 Ali aliov at xfce.org 
+	 * : Changes in the battery tooltips/notifications
+
 2009-03-09 13:22 Ali aliov at xfce.org 
 	 * : Bump xfce dependencies version to 4.6.0
 

Modified: xfce4-power-manager/trunk/libxfpm/hal-device.c
===================================================================
--- xfce4-power-manager/trunk/libxfpm/hal-device.c	2009-03-09 19:29:31 UTC (rev 6875)
+++ xfce4-power-manager/trunk/libxfpm/hal-device.c	2009-03-09 22:09:41 UTC (rev 6876)
@@ -81,7 +81,7 @@
     guint32   reporting_design;
     guint32   reporting_last_full;
     
-    gint      time;
+    guint      time;
     
 };
 
@@ -325,7 +325,7 @@
 		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);

Modified: xfce4-power-manager/trunk/libxfpm/hal-power.c
===================================================================
--- xfce4-power-manager/trunk/libxfpm/hal-power.c	2009-03-09 19:29:31 UTC (rev 6875)
+++ xfce4-power-manager/trunk/libxfpm/hal-power.c	2009-03-09 22:09:41 UTC (rev 6876)
@@ -241,8 +241,9 @@
     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_adapter (power);
     hal_power_get_batteries (power);
-    hal_power_get_adapter (power);
     
 out:
 	;

Modified: xfce4-power-manager/trunk/src/xfpm-battery.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-battery.c	2009-03-09 19:29:31 UTC (rev 6875)
+++ xfce4-power-manager/trunk/src/xfpm-battery.c	2009-03-09 22:09:41 UTC (rev 6876)
@@ -55,8 +55,7 @@
 {
     XfpmTrayIcon  *icon;
     HalDevice     *device;
-    XfpmNotify    *notify;
-    
+
     HalDeviceType    type;
     gchar 	    *icon_prefix;
     
@@ -117,7 +116,6 @@
     
     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 @@
 	g_free(icon);
 	return;
     }
-    
-    
 }
 
 static void
@@ -250,78 +246,163 @@
     }
 }
 
-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);
+}
+
+static void
 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 @@
 		  "percentage", &percentage,
 		  "last-full", &last_full,
 		  "current-charge", &current_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 @@
     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 @@
     
 }
 
-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);

Modified: xfce4-power-manager/trunk/src/xfpm-battery.h
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-battery.h	2009-03-09 19:29:31 UTC (rev 6875)
+++ xfce4-power-manager/trunk/src/xfpm-battery.h	2009-03-09 22:09:41 UTC (rev 6876)
@@ -64,7 +64,6 @@
 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);
 

Modified: xfce4-power-manager/trunk/src/xfpm-notify.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-notify.c	2009-03-09 19:29:31 UTC (rev 6875)
+++ xfce4-power-manager/trunk/src/xfpm-notify.c	2009-03-09 22:09:41 UTC (rev 6876)
@@ -144,23 +144,26 @@
 
 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);
 }

Modified: xfce4-power-manager/trunk/src/xfpm-notify.h
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-notify.h	2009-03-09 19:29:31 UTC (rev 6875)
+++ xfce4-power-manager/trunk/src/xfpm-notify.h	2009-03-09 22:09:41 UTC (rev 6876)
@@ -61,6 +61,7 @@
 						const gchar *text,
 						const gchar *icon_name,
 						gint timeout,
+						gboolean simple,
 						XfpmNotifyUrgency urgency,
 						GtkStatusIcon *icon);
 

Modified: xfce4-power-manager/trunk/src/xfpm-supply.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-supply.c	2009-03-09 19:29:31 UTC (rev 6875)
+++ xfce4-power-manager/trunk/src/xfpm-supply.c	2009-03-09 22:09:41 UTC (rev 6876)
@@ -246,20 +246,8 @@
     }
 }
 
-/*
-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 @@
 	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,20 +266,26 @@
 						      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)
 {
     xfpm_battery_show_info (battery);
@@ -390,14 +383,41 @@
 		   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 )
 {
     if ( !supply->priv->adapter_found )
     	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 @@
 	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 @@
     }
     
     g_list_free (list);
-    
 }
 
 static void




More information about the Goodies-commits mailing list