[tracker] libtracker-common: Refactor properties used by TrackerPower
- From: Jürg Billeter <juergbi src gnome org>
- To: svn-commits-list gnome org
- Subject: [tracker] libtracker-common: Refactor properties used by TrackerPower
- Date: Fri, 15 May 2009 10:13:01 -0400 (EDT)
commit 2e5104fae0444cbe4c0322b7e2a9312743cc70b8
Author: Richard Hughes <hughsient gmail com>
Date: Fri May 15 16:09:43 2009 +0200
libtracker-common: Refactor properties used by TrackerPower
Switch from three overlapping properties in the power backend to
on-battery and on-low-battery.
---
src/libtracker-common/tracker-power-hal.c | 106 +++++++++++-----------------
src/libtracker-common/tracker-power.h | 5 +-
src/tracker-indexer/tracker-indexer.c | 14 ++--
src/trackerd/tracker-main.c | 3 +-
src/trackerd/tracker-status.c | 47 ++++++-------
5 files changed, 74 insertions(+), 101 deletions(-)
diff --git a/src/libtracker-common/tracker-power-hal.c b/src/libtracker-common/tracker-power-hal.c
index 42b9caf..80a89ed 100644
--- a/src/libtracker-common/tracker-power-hal.c
+++ b/src/libtracker-common/tracker-power-hal.c
@@ -40,6 +40,8 @@
#define PROP_AC_ADAPTER_ON "ac_adapter.present"
#define PROP_BATT_PERCENTAGE "battery.charge_level.percentage"
+#define BATTERY_LOW_THRESHOLD 0.05f
+
#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_POWER, TrackerPowerPriv))
typedef struct {
@@ -49,7 +51,7 @@ typedef struct {
GHashTable *batteries;
gchar *ac_adapter_udi;
- gboolean battery_in_use;
+ gboolean on_battery;
gdouble battery_percentage;
} TrackerPowerPriv;
@@ -78,9 +80,8 @@ static void hal_device_property_modified_cb (LibHalContext *context,
enum {
PROP_0,
- PROP_BATTERY_IN_USE,
- PROP_BATTERY_EXISTS,
- PROP_BATTERY_PERCENTAGE
+ PROP_ON_BATTERY,
+ PROP_ON_LOW_BATTERY
};
G_DEFINE_TYPE (TrackerPower, tracker_power, G_TYPE_OBJECT);
@@ -96,26 +97,19 @@ tracker_power_class_init (TrackerPowerClass *klass)
object_class->get_property = hal_get_property;
g_object_class_install_property (object_class,
- PROP_BATTERY_IN_USE,
- g_param_spec_boolean ("battery-in-use",
+ PROP_ON_BATTERY,
+ g_param_spec_boolean ("on-battery",
"Battery in use",
"Whether the battery is being used",
FALSE,
G_PARAM_READABLE));
g_object_class_install_property (object_class,
- PROP_BATTERY_EXISTS,
- g_param_spec_boolean ("battery-exists",
- "Battery exists",
- "There is a battery on this machine",
- FALSE,
- G_PARAM_READABLE));
- g_object_class_install_property (object_class,
- PROP_BATTERY_PERCENTAGE,
- g_param_spec_double ("battery-percentage",
- "Battery percentage",
- "Battery percentage",
- 0.0, 1.0, 0.0,
+ PROP_ON_LOW_BATTERY,
+ g_param_spec_boolean ("on-low-battery",
+ "Battery low",
+ "Whether the battery is low",
+ FALSE,
G_PARAM_READABLE));
g_type_class_add_private (object_class, sizeof (TrackerPowerPriv));
@@ -229,14 +223,12 @@ hal_get_property (GObject *object,
priv = GET_PRIV (object);
switch (param_id) {
- case PROP_BATTERY_IN_USE:
- g_value_set_boolean (value, priv->battery_in_use);
- break;
- case PROP_BATTERY_EXISTS:
- g_value_set_boolean (value, priv->ac_adapter_udi != NULL);
+ case PROP_ON_BATTERY:
+ g_value_set_boolean (value, priv->on_battery);
break;
- case PROP_BATTERY_PERCENTAGE:
- g_value_set_double (value, priv->battery_percentage);
+ case PROP_ON_LOW_BATTERY:
+ /* hardcoded to 5% */
+ g_value_set_boolean (value, priv->battery_percentage < BATTERY_LOW_THRESHOLD);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -273,11 +265,10 @@ hal_setup_ac_adapters (TrackerPower *power)
if (!devices || !devices[0]) {
libhal_free_string_array (devices);
- priv->battery_in_use = FALSE;
- g_object_notify (G_OBJECT (power), "battery-in-use");
+ priv->on_battery = FALSE;
+ g_object_notify (G_OBJECT (power), "on-battery");
priv->ac_adapter_udi = NULL;
- g_object_notify (G_OBJECT (power), "battery-exists");
return TRUE;
}
@@ -286,7 +277,6 @@ hal_setup_ac_adapters (TrackerPower *power)
if (!priv->ac_adapter_udi) {
/* For now just use the first one we find */
priv->ac_adapter_udi = g_strdup (*p);
- g_object_notify (G_OBJECT (power), "battery-exists");
g_message (" Device '%s' (default)", *p);
} else {
@@ -309,15 +299,15 @@ hal_setup_ac_adapters (TrackerPower *power)
}
/* Get current state, are we using the battery now? */
- priv->battery_in_use = !libhal_device_get_property_bool (priv->context,
+ priv->on_battery = !libhal_device_get_property_bool (priv->context,
priv->ac_adapter_udi,
PROP_AC_ADAPTER_ON,
NULL);
g_message ("HAL reports system is currently powered by %s",
- priv->battery_in_use ? "battery" : "AC adapter");
+ priv->on_battery ? "battery" : "AC adapter");
- g_object_notify (G_OBJECT (power), "battery-in-use");
+ g_object_notify (G_OBJECT (power), "on-battery");
return TRUE;
}
@@ -377,6 +367,7 @@ hal_battery_notify (TrackerPower *power)
TrackerPowerPriv *priv;
GList *values, *v;
gint percentage, n_values;
+ gdouble old_percentage;
priv = GET_PRIV (power);
percentage = n_values = 0;
@@ -388,6 +379,8 @@ hal_battery_notify (TrackerPower *power)
n_values++;
}
+ old_percentage = priv->battery_percentage;
+
if (n_values > 0) {
priv->battery_percentage = (gdouble) percentage / n_values;
priv->battery_percentage /= 100;
@@ -395,9 +388,15 @@ hal_battery_notify (TrackerPower *power)
priv->battery_percentage = 0;
}
- g_list_free (values);
+ /* only notify when we cross the threshold up or down */
+ if ((priv->battery_percentage > BATTERY_LOW_THRESHOLD &&
+ old_percentage <= BATTERY_LOW_THRESHOLD) ||
+ (priv->battery_percentage <= BATTERY_LOW_THRESHOLD &&
+ old_percentage > BATTERY_LOW_THRESHOLD)) {
+ g_object_notify (G_OBJECT (power), "on-low-battery");
+ }
- g_object_notify (G_OBJECT (power), "battery-percentage");
+ g_list_free (values);
}
static void
@@ -477,14 +476,14 @@ hal_device_property_modified_cb (LibHalContext *context,
if (priv->ac_adapter_udi && strcmp (priv->ac_adapter_udi, udi) == 0) {
/* Property change is on the AC adapter */
- priv->battery_in_use = !libhal_device_get_property_bool (priv->context,
+ priv->on_battery = !libhal_device_get_property_bool (priv->context,
priv->ac_adapter_udi,
PROP_AC_ADAPTER_ON,
&error);
g_message ("HAL reports system is now powered by %s",
- priv->battery_in_use ? "battery" : "AC adapter");
+ priv->on_battery ? "battery" : "AC adapter");
- g_object_notify (G_OBJECT (power), "battery-in-use");
+ g_object_notify (G_OBJECT (power), "on-battery");
if (dbus_error_is_set (&error)) {
g_critical ("Could not get device property:'%s' for udi:'%s', %s",
@@ -514,7 +513,7 @@ tracker_power_new ()
}
/**
- * tracker_power_get_battery_in_use:
+ * tracker_hal_get_on_battery:
* @power: A #TrackerPower.
*
* Returns whether the computer battery (if any) is currently in use.
@@ -522,7 +521,7 @@ tracker_power_new ()
* Returns: #TRUE if the computer is running on battery power.
**/
gboolean
-tracker_power_get_battery_in_use (TrackerPower *power)
+tracker_power_get_on_battery (TrackerPower *power)
{
TrackerPowerPriv *priv;
@@ -530,11 +529,11 @@ tracker_power_get_battery_in_use (TrackerPower *power)
priv = GET_PRIV (power);
- return priv->battery_in_use;
+ return priv->on_battery;
}
/**
- * tracker_power_get_battery_exists:
+ * tracker_power_get_on_low_battery:
* @power: A #TrackerPower
*
* Returns whether the computer has batteries.
@@ -542,7 +541,7 @@ tracker_power_get_battery_in_use (TrackerPower *power)
* Returns: #TRUE if the computer has batteries available.
**/
gboolean
-tracker_power_get_battery_exists (TrackerPower *power)
+tracker_power_get_on_low_battery (TrackerPower *power)
{
TrackerPowerPriv *priv;
@@ -550,28 +549,7 @@ tracker_power_get_battery_exists (TrackerPower *power)
priv = GET_PRIV (power);
- return priv->ac_adapter_udi != NULL;
-}
-
-/**
- * tracker_power_get_battery_percentage:
- * @power: A #TrackerPower
- *
- * Returns the battery percentage left on the
- * computer, or #0.0 if no batteries are present.
- *
- * Returns: The battery percentage left.
- **/
-gdouble
-tracker_power_get_battery_percentage (TrackerPower *power)
-{
- TrackerPowerPriv *priv;
-
- g_return_val_if_fail (TRACKER_IS_POWER (power), 0.0);
-
- priv = GET_PRIV (power);
-
- return priv->battery_percentage;
+ return (priv->battery_percentage < BATTERY_LOW_THRESHOLD);
}
#endif /* HAVE_HAL */
diff --git a/src/libtracker-common/tracker-power.h b/src/libtracker-common/tracker-power.h
index daa85aa..7bbb044 100644
--- a/src/libtracker-common/tracker-power.h
+++ b/src/libtracker-common/tracker-power.h
@@ -54,9 +54,8 @@ GType tracker_power_get_type (void) G_GNUC_CONST;
TrackerPower * tracker_power_new (void);
-gboolean tracker_power_get_battery_in_use (TrackerPower *power);
-gboolean tracker_power_get_battery_exists (TrackerPower *power);
-gdouble tracker_power_get_battery_percentage (TrackerPower *power);
+gboolean tracker_power_get_on_battery (TrackerPower *power);
+gboolean tracker_power_get_on_low_battery (TrackerPower *power);
#endif /* HAVE_HAL */
diff --git a/src/tracker-indexer/tracker-indexer.c b/src/tracker-indexer/tracker-indexer.c
index b0937a9..2bab6d0 100644
--- a/src/tracker-indexer/tracker-indexer.c
+++ b/src/tracker-indexer/tracker-indexer.c
@@ -485,7 +485,7 @@ set_up_throttle (TrackerIndexer *indexer)
*/
throttle = tracker_config_get_throttle (indexer->private->config);
- if (tracker_power_get_battery_in_use (indexer->private->hal_power)) {
+ if (tracker_power_get_on_battery (indexer->private->hal_power)) {
g_message ("We are running on battery");
if (throttle == THROTTLE_DEFAULT) {
@@ -515,9 +515,9 @@ set_up_throttle (TrackerIndexer *indexer)
}
static void
-notify_battery_in_use_cb (GObject *gobject,
- GParamSpec *arg1,
- gpointer user_data)
+notify_on_battery_cb (GObject *gobject,
+ GParamSpec *arg1,
+ gpointer user_data)
{
set_up_throttle (TRACKER_INDEXER (user_data));
}
@@ -615,7 +615,7 @@ tracker_indexer_finalize (GObject *object)
#ifdef HAVE_HAL
g_signal_handlers_disconnect_by_func (priv->hal_power,
- notify_battery_in_use_cb,
+ notify_on_battery_cb,
TRACKER_INDEXER (object));
g_object_unref (priv->hal_power);
@@ -970,8 +970,8 @@ tracker_indexer_init (TrackerIndexer *indexer)
priv->hal_power = tracker_power_new ();
priv->hal_storage = tracker_storage_new ();
- g_signal_connect (priv->hal_power, "notify::battery-in-use",
- G_CALLBACK (notify_battery_in_use_cb),
+ g_signal_connect (priv->hal_power, "notify::on-battery",
+ G_CALLBACK (notify_on_battery_cb),
indexer);
set_up_throttle (indexer);
diff --git a/src/trackerd/tracker-main.c b/src/trackerd/tracker-main.c
index ea6d20f..52a41a8 100644
--- a/src/trackerd/tracker-main.c
+++ b/src/trackerd/tracker-main.c
@@ -276,8 +276,7 @@ check_runtime_level (TrackerConfig *config,
runlevel = TRACKER_RUNNING_MAIN_INSTANCE;
#ifdef HAVE_HAL
- if (!tracker_power_get_battery_exists (hal) ||
- !tracker_power_get_battery_in_use (hal)) {
+ if (!tracker_power_get_on_battery (hal)) {
return TRACKER_RUNNING_MAIN_INSTANCE;
}
diff --git a/src/trackerd/tracker-status.c b/src/trackerd/tracker-status.c
index 5eaf4f9..704c6bd 100644
--- a/src/trackerd/tracker-status.c
+++ b/src/trackerd/tracker-status.c
@@ -78,12 +78,12 @@ static void indexer_pause (gboolean should_block);
static void low_disk_space_limit_cb (GObject *gobject,
GParamSpec *arg1,
gpointer user_data);
-static void battery_in_use_cb (GObject *gobject,
- GParamSpec *arg1,
- gpointer user_data);
-static void battery_percentage_cb (GObject *object,
- GParamSpec *pspec,
- gpointer user_data);
+static void on_battery_cb (GObject *gobject,
+ GParamSpec *arg1,
+ gpointer user_data);
+static void on_low_battery_cb (GObject *object,
+ GParamSpec *pspec,
+ gpointer user_data);
static void disk_space_check_stop (void);
static GStaticPrivate private_key = G_STATIC_PRIVATE_INIT;
@@ -107,10 +107,10 @@ private_free (gpointer data)
#ifdef HAVE_HAL
g_signal_handlers_disconnect_by_func (private->hal,
- battery_in_use_cb,
+ on_battery_cb,
NULL);
g_signal_handlers_disconnect_by_func (private->hal,
- battery_percentage_cb,
+ on_low_battery_cb,
NULL);
g_object_unref (private->hal);
@@ -395,7 +395,7 @@ set_up_throttle (gboolean debugging)
*/
throttle = tracker_config_get_throttle (private->config);
- if (tracker_power_get_battery_in_use (private->hal)) {
+ if (tracker_power_get_on_battery (private->hal)) {
if (debugging) {
g_message ("We are running on battery");
}
@@ -439,34 +439,31 @@ set_up_throttle (gboolean debugging)
}
static void
-battery_in_use_cb (GObject *gobject,
+on_battery_cb (GObject *gobject,
GParamSpec *arg1,
- gpointer user_data)
+ gpointer user_data)
{
set_up_throttle (TRUE);
}
static void
-battery_percentage_cb (GObject *object,
+on_low_battery_cb (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
TrackerStatusPrivate *private;
- gdouble percentage;
- gboolean battery_in_use;
+ gboolean on_low_battery;
+ gboolean on_battery;
private = g_static_private_get (&private_key);
g_return_if_fail (private != NULL);
- percentage = tracker_power_get_battery_percentage (private->hal);
- battery_in_use = tracker_power_get_battery_in_use (private->hal);
-
- g_message ("Battery percentage is now %.0f%%",
- percentage * 100);
+ on_low_battery = tracker_power_get_on_low_battery (private->hal);
+ on_battery = tracker_power_get_on_battery (private->hal);
/* FIXME: This could be a configuration option */
- if (battery_in_use) {
- if (percentage <= 0.05) {
+ if (on_battery) {
+ if (on_low_battery) {
/* Running on low batteries, stop indexing for now */
tracker_status_set_is_paused_for_batt (TRUE);
} else {
@@ -526,11 +523,11 @@ tracker_status_init (TrackerConfig *config,
private->hal = g_object_ref (hal);
g_message ("Setting battery percentage checking");
- g_signal_connect (private->hal, "notify::battery-percentage",
- G_CALLBACK (battery_percentage_cb),
+ g_signal_connect (private->hal, "notify::on-low-battery",
+ G_CALLBACK (on_low_battery_cb),
NULL);
- g_signal_connect (private->hal, "notify::battery-in-use",
- G_CALLBACK (battery_in_use_cb),
+ g_signal_connect (private->hal, "notify::on-battery",
+ G_CALLBACK (on_battery_cb),
NULL);
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]