[tracker] Add DeviceKit-power backend



commit 7e8fb0b23e88789f563c4c039d667d76acf93ac2
Author: Richard Hughes <hughsient gmail com>
Date:   Mon Jun 22 11:45:54 2009 +0200

    Add DeviceKit-power backend
    
    Add DeviceKit-power backend and prefer it by default over HAL if
    it's available.

 configure.ac                                    |   34 ++++
 src/libtracker-common/Makefile.am               |   35 +++-
 src/libtracker-common/tracker-power-devicekit.c |  217 +++++++++++++++++++++++
 src/tracker-indexer/tracker-indexer.c           |   41 +++--
 4 files changed, 301 insertions(+), 26 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 5f4901c..e8a904a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -136,6 +136,7 @@ GMIME_REQUIRED=2.1.0
 LIBXML2_REQUIRED=0.6
 LIBNOTIFY_REQUIRED=0.4.3
 HAL_REQUIRED=0.5
+DEVKIT_POWER_REQUIRED=007
 GDKPIXBUF_REQUIRED=2.12.0
 UNAC_REQUIRED=1.0.0
 POPPLER_GLIB_REQUIRED=0.4.5
@@ -388,6 +389,38 @@ fi
 AM_CONDITIONAL(HAVE_HAL, test "x$have_hal" = "xyes")
 
 ##################################################################
+# Check for DeviceKit-power
+##################################################################
+
+AC_ARG_ENABLE(devkit_power,
+	      AS_HELP_STRING([--disable-devkit-power],
+			     [disable DeviceKit-power support for AC power detection [[default=yes]]]),,
+	      [enable_devkit_power=yes])
+
+if test "x$enable_devkit_power" != "xno"; then
+   PKG_CHECK_MODULES(DEVKIT_POWER,
+		     [devkit-power-gobject >= $DEVKIT_POWER_REQUIRED],
+		     [have_devkit_power=yes] ,
+		     [have_devkit_power=no])
+   AC_SUBST(DEVKIT_POWER_CFLAGS)
+   AC_SUBST(DEVKIT_POWER_LIBS)
+
+   if test "x$have_devkit_power" = "xyes"; then
+      AC_DEFINE(HAVE_DEVKIT_POWER, [], [Define if we have DEVKIT_POWER])
+   fi
+else
+   have_devkit_power="no  (disabled)"
+fi
+
+if test "x$enable_devkit_power" = "xyes"; then
+   if test "x$have_devkit_power" != "xyes"; then
+      AC_MSG_ERROR([Couldn't find devkit_power >= $DEVKIT_POWER_REQUIRED and devkit_power-storage.])
+   fi
+fi
+
+AM_CONDITIONAL(HAVE_DEVKIT_POWER, test "x$have_devkit_power" = "xyes")
+
+##################################################################
 # Check for libxml2
 ##################################################################
 
@@ -1429,6 +1462,7 @@ Build Configuration:
 	Support for file monitoring:           	gio (w/ libinotify: $have_inotify)
 	Support for ioprio:			$have_ioprio
 	Support for HAL:                    	$have_hal
+	Support for DeviceKit-power:           	$have_devkit_power
 
 Applications:
 
diff --git a/src/libtracker-common/Makefile.am b/src/libtracker-common/Makefile.am
index caf9c7e..8cac867 100644
--- a/src/libtracker-common/Makefile.am
+++ b/src/libtracker-common/Makefile.am
@@ -4,6 +4,7 @@ INCLUDES =						\
 	-DSHAREDIR=\""$(datadir)"\"			\
 	-DG_LOG_DOMAIN=\"Tracker\"			\
 	-DTRACKER_COMPILATION				\
+	-DI_KNOW_THE_DEVICEKIT_POWER_API_IS_SUBJECT_TO_CHANGE	\
 	-I$(top_srcdir)/src				\
 	$(WARN_CFLAGS)					\
 	$(GLIB2_CFLAGS)					\
@@ -12,6 +13,7 @@ INCLUDES =						\
 	$(GIO_CFLAGS)					\
 	$(PANGO_CFLAGS)					\
 	$(HAL_CFLAGS)					\
+	$(DEVKIT_POWER_CFLAGS)				\
 	$(DBUS_CFLAGS)					\
 	$(UNAC_CFLAGS)
 
@@ -25,18 +27,33 @@ else
 os_sources = tracker-os-dependant-unix.c
 endif
 
+# For storage, we need HAL, tracker isn't yet ported to DeviceKit-disks
 if HAVE_HAL
-hal_sources = 						\
-	tracker-power-hal.c				\
-	tracker-storage-hal.c
-hal_headers = 						\
-	tracker-power.h					\
+storage_headers =					\
 	tracker-storage.h
+storage_sources =					\
+	tracker-storage-hal.c
+endif
+
+# Prefer DeviceKit-power for the power data if it's available, else fall back to HAL
+if HAVE_DEVKIT_POWER
+power_headers =						\
+	tracker-power.h
+power_sources =						\
+	tracker-power-devicekit.c
+else
+if HAVE_HAL
+power_headers =						\
+	tracker-power.h
+power_sources =						\
+	tracker-power-hal.c
+endif
 endif
 
-libtracker_common_la_SOURCES =	 			\
+libtracker_common_la_SOURCES =				\
 	$(os_sources)					\
-	$(hal_sources)					\
+	$(power_sources)				\
+	$(storage_sources)				\
 	tracker-class.c					\
 	tracker-config.c 				\
 	tracker-dbus.c	 				\
@@ -58,7 +75,8 @@ libtracker_common_la_SOURCES =	 			\
 	tracker-albumart.c
 
 noinst_HEADERS =					\
-	$(hal_headers)					\
+	$(power_headers)				\
+	$(storage_headers)				\
 	tracker-dbus.h					\
 	tracker-ioprio.h				\
 	tracker-log.h					\
@@ -88,6 +106,7 @@ libtracker_common_la_LDFLAGS = 				\
 libtracker_common_la_LIBADD = 				\
 	$(top_builddir)/src/libstemmer/libstemmer.la	\
 	$(HAL_LIBS)					\
+	$(DEVKIT_POWER_LIBS)				\
 	$(DBUS_LIBS)					\
 	$(UNAC_LIBS)					\
 	$(PANGO_LIBS)					\
diff --git a/src/libtracker-common/tracker-power-devicekit.c b/src/libtracker-common/tracker-power-devicekit.c
new file mode 100644
index 0000000..1c3ea6c
--- /dev/null
+++ b/src/libtracker-common/tracker-power-devicekit.c
@@ -0,0 +1,217 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc gnome org)
+ * Copyright (C) 2008, Nokia (urho konttori nokia com)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "config.h"
+
+#ifdef HAVE_DEVKIT_POWER
+
+#include <devkit-power-gobject/devicekit-power.h>
+
+#include "tracker-power.h"
+
+#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_POWER, TrackerPowerPriv))
+
+typedef struct {
+	DkpClient *client;
+	gboolean   on_battery;
+	gboolean   on_low_battery;
+} TrackerPowerPriv;
+
+static void	tracker_power_finalize		(GObject	 *object);
+static void	tracker_power_get_property	(GObject	 *object,
+						 guint		  param_id,
+						 GValue		 *value,
+						 GParamSpec	 *pspec);
+static void	tracker_power_client_changed_cb (DkpClient	 *client,
+						 TrackerPower	 *power);
+
+enum {
+	PROP_0,
+	PROP_ON_BATTERY,
+	PROP_ON_LOW_BATTERY
+};
+
+G_DEFINE_TYPE (TrackerPower, tracker_power, G_TYPE_OBJECT);
+
+static void
+tracker_power_class_init (TrackerPowerClass *klass)
+{
+	GObjectClass *object_class;
+
+	object_class = G_OBJECT_CLASS (klass);
+
+	object_class->finalize	   = tracker_power_finalize;
+	object_class->get_property = tracker_power_get_property;
+
+	g_object_class_install_property (object_class,
+					 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_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));
+}
+
+static void
+tracker_power_init (TrackerPower *power)
+{
+	TrackerPowerPriv *priv;
+
+	g_message ("Initializing DeviceKit-power...");
+
+	priv = GET_PRIV (power);
+
+	/* connect to a DeviceKit-power instance */
+	priv->client = dkp_client_new ();
+	g_signal_connect (priv->client, "changed",
+			  G_CALLBACK (tracker_power_client_changed_cb), power);
+
+	/* coldplug */
+	priv->on_battery = dkp_client_on_battery (priv->client);
+	priv->on_low_battery = dkp_client_on_low_battery (priv->client);
+}
+
+static void
+tracker_power_finalize (GObject *object)
+{
+	TrackerPowerPriv *priv;
+
+	priv = GET_PRIV (object);
+
+	g_object_unref (priv->client);
+
+	(G_OBJECT_CLASS (tracker_power_parent_class)->finalize) (object);
+}
+
+static void
+tracker_power_get_property (GObject    *object,
+			    guint       param_id,
+			    GValue     *value,
+			    GParamSpec *pspec)
+{
+	TrackerPowerPriv *priv;
+
+	priv = GET_PRIV (object);
+
+	switch (param_id) {
+	case PROP_ON_BATTERY:
+		g_value_set_boolean (value, priv->on_battery);
+		break;
+	case PROP_ON_LOW_BATTERY:
+		g_value_set_boolean (value, priv->on_low_battery);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+		break;
+	};
+}
+
+/**
+ * tracker_power_client_changed_cb:
+ **/
+static void
+tracker_power_client_changed_cb (DkpClient *client, TrackerPower *power)
+{
+	gboolean on_battery;
+	gboolean on_low_battery;
+	TrackerPowerPriv *priv;
+
+	priv = GET_PRIV (power);
+
+	/* get the on-battery state */
+	on_battery = dkp_client_on_battery (priv->client);
+	if (on_battery != priv->on_battery) {
+		priv->on_battery = on_battery;
+		g_object_notify (G_OBJECT (power), "on-battery");
+	}
+
+	/* get the on-low-battery state */
+	on_low_battery = dkp_client_on_battery (priv->client);
+	if (on_low_battery != priv->on_low_battery) {
+		priv->on_low_battery = on_low_battery;
+		g_object_notify (G_OBJECT (power), "on-low-battery");
+	}
+}
+
+/**
+ * tracker_power_new:
+ *
+ * Creates a new instance of #TrackerPower.
+ *
+ * Returns: The newly created #TrackerPower.
+ **/
+TrackerPower *
+tracker_power_new ()
+{
+	return g_object_new (TRACKER_TYPE_POWER, NULL);
+}
+
+/**
+ * tracker_hal_get_on_battery:
+ * @power: A #TrackerPower.
+ *
+ * Returns whether the computer battery (if any) is currently in use.
+ *
+ * Returns: #TRUE if the computer is running on battery power.
+ **/
+gboolean
+tracker_power_get_on_battery (TrackerPower *power)
+{
+	TrackerPowerPriv *priv;
+
+	g_return_val_if_fail (TRACKER_IS_POWER (power), TRUE);
+
+	priv = GET_PRIV (power);
+
+	return priv->on_battery;
+}
+
+/**
+ * tracker_power_get_on_low_battery:
+ * @power: A #TrackerPower
+ *
+ * Returns whether the computer has batteries.
+ *
+ * Returns: #TRUE if the computer has batteries available.
+ **/
+gboolean
+tracker_power_get_on_low_battery (TrackerPower *power)
+{
+	TrackerPowerPriv *priv;
+
+	g_return_val_if_fail (TRACKER_IS_POWER (power), TRUE);
+
+	priv = GET_PRIV (power);
+
+	return priv->on_low_battery;
+}
+
+#endif /* HAVE_DEVKIT_POWER */
diff --git a/src/tracker-indexer/tracker-indexer.c b/src/tracker-indexer/tracker-indexer.c
index 2bab6d0..ef505ad 100644
--- a/src/tracker-indexer/tracker-indexer.c
+++ b/src/tracker-indexer/tracker-indexer.c
@@ -137,8 +137,8 @@ struct TrackerIndexerPrivate {
 	TrackerConfig *config;
 	TrackerLanguage *language;
 
-	TrackerPower   *hal_power;
-	TrackerStorage *hal_storage;
+	TrackerPower   *power;
+	TrackerStorage *storage;
 
 	GTimer *timer;
 
@@ -472,7 +472,7 @@ tracker_indexer_transaction_open (TrackerIndexer *indexer)
 	start_transaction (indexer);
 }
 
-#ifdef HAVE_HAL
+#ifdef HAVE_HAL || HAVE_DEVKIT_POWER
 
 static void
 set_up_throttle (TrackerIndexer *indexer)
@@ -485,7 +485,7 @@ set_up_throttle (TrackerIndexer *indexer)
 	 */
 	throttle = tracker_config_get_throttle (indexer->private->config);
 
-	if (tracker_power_get_on_battery (indexer->private->hal_power)) {
+	if (tracker_power_get_on_battery (indexer->private->power)) {
 		g_message ("We are running on battery");
 
 		if (throttle == THROTTLE_DEFAULT) {
@@ -522,7 +522,7 @@ notify_on_battery_cb (GObject *gobject,
 	set_up_throttle (TRACKER_INDEXER (user_data));
 }
 
-#endif /* HAVE_HAL */
+#endif /* HAVE_HAL || HAVE_DEVKIT_POWER */
 
 static void
 check_mount_removal (GQueue   *queue,
@@ -613,13 +613,16 @@ tracker_indexer_finalize (GObject *object)
 		g_timer_destroy (priv->timer);
 	}
 
-#ifdef HAVE_HAL
-	g_signal_handlers_disconnect_by_func (priv->hal_power,
+#ifdef HAVE_HAL || HAVE_DEVKIT_POWER
+	g_signal_handlers_disconnect_by_func (priv->power,
 					      notify_on_battery_cb,
 					      TRACKER_INDEXER (object));
 
-	g_object_unref (priv->hal_power);
-	g_object_unref (priv->hal_storage);
+	g_object_unref (priv->power);
+#endif /* HAVE_HAL || HAVE_DEVKIT_POWER */
+
+#ifdef HAVE_HAL
+	g_object_unref (priv->storage);
 #endif /* HAVE_HAL */
 
 	g_object_unref (priv->language);
@@ -967,15 +970,17 @@ tracker_indexer_init (TrackerIndexer *indexer)
 	priv->config = tracker_config_new ();
 
 #ifdef HAVE_HAL
-	priv->hal_power = tracker_power_new ();
-	priv->hal_storage = tracker_storage_new ();
+	priv->storage = tracker_storage_new ();
+#endif /* HAVE_HAL */
 
-	g_signal_connect (priv->hal_power, "notify::on-battery",
+#ifdef HAVE_HAL || HAVE_DEVKIT_POWER
+	priv->power = tracker_power_new ();
+	g_signal_connect (priv->power, "notify::on-battery",
 			  G_CALLBACK (notify_on_battery_cb),
 			  indexer);
 
 	set_up_throttle (indexer);
-#endif /* HAVE_HAL */
+#endif /* HAVE_HAL || HAVE_DEVKIT_POWER */
 
 	priv->language = tracker_language_new (priv->config);
 
@@ -1120,7 +1125,7 @@ item_add_to_datasource (TrackerIndexer *indexer,
 	file = tracker_module_file_get_file (module_file);
 
 #ifdef HAVE_HAL
-	removable_device_udi = tracker_storage_get_volume_udi_for_file (indexer->private->hal_storage,
+	removable_device_udi = tracker_storage_get_volume_udi_for_file (indexer->private->storage,
 									file);
 #else
 	removable_device_udi = NULL;
@@ -1220,7 +1225,7 @@ item_add_or_update (TrackerIndexer        *indexer,
 	generate_item_thumbnail (indexer, uri);
 
 #ifdef HAVE_HAL
-	if (tracker_storage_uri_is_on_removable_device (indexer->private->hal_storage,
+	if (tracker_storage_uri_is_on_removable_device (indexer->private->storage,
 							uri, 
 							&mount_point,
 							NULL)) {
@@ -1330,11 +1335,11 @@ item_move (TrackerIndexer  *indexer,
 	update_file_uri_recursively (source_uri, uri);
 
 #ifdef HAVE_HAL
-	if (tracker_storage_uri_is_on_removable_device (indexer->private->hal_storage,
+	if (tracker_storage_uri_is_on_removable_device (indexer->private->storage,
 							source_uri, 
 							&mount_point,
 							NULL) ) {
-		if (tracker_storage_uri_is_on_removable_device (indexer->private->hal_storage,
+		if (tracker_storage_uri_is_on_removable_device (indexer->private->storage,
 								uri, 
 								NULL,
 								NULL) ) {
@@ -1436,7 +1441,7 @@ item_remove (TrackerIndexer *indexer,
 	}*/
 
 #ifdef HAVE_HAL
-	if (tracker_storage_uri_is_on_removable_device (indexer->private->hal_storage,
+	if (tracker_storage_uri_is_on_removable_device (indexer->private->storage,
 							uri, 
 							&mount_point,
 							NULL)) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]