[tracker/miner-web: 1/49] TrackerMiner: Take DBus initialization away to tracker-dbus.[ch]



commit b63986ae21635fc1730e9337243b162d0acbb54f
Author: Carlos Garnacho <carlos lanedo com>
Date:   Fri Nov 6 15:25:19 2009 +0100

    TrackerMiner: Take DBus initialization away to tracker-dbus.[ch]
    
    This way, other miner base object implementation will be able to define
    extra interfaces by doing initialization themselves, the Miner DBus interface
    will need to be there in order to have things working though.

 src/libtracker-miner/Makefile.am     |    2 +
 src/libtracker-miner/tracker-dbus.c  |  274 ++++++++++++++++++++++++++++++++++
 src/libtracker-miner/tracker-dbus.h  |   38 +++++
 src/libtracker-miner/tracker-miner.c |  262 +++------------------------------
 4 files changed, 335 insertions(+), 241 deletions(-)
---
diff --git a/src/libtracker-miner/Makefile.am b/src/libtracker-miner/Makefile.am
index 89502fa..38404c7 100644
--- a/src/libtracker-miner/Makefile.am
+++ b/src/libtracker-miner/Makefile.am
@@ -27,6 +27,8 @@ libtracker_minerincludedir=$(includedir)/tracker-$(TRACKER_API_VERSION)/libtrack
 libtracker_miner_ TRACKER_API_VERSION@_la_SOURCES = 	\
 	tracker-crawler.c				\
 	tracker-crawler.h				\
+	tracker-dbus.c					\
+	tracker-dbus.h					\
 	tracker-marshal.c				\
 	tracker-marshal.h				\
 	tracker-miner.c					\
diff --git a/src/libtracker-miner/tracker-dbus.c b/src/libtracker-miner/tracker-dbus.c
new file mode 100644
index 0000000..2099e7c
--- /dev/null
+++ b/src/libtracker-miner/tracker-dbus.c
@@ -0,0 +1,274 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009, Nokia (urho konttori nokia com)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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 <stdlib.h>
+
+#include "tracker-dbus.h"
+#include "tracker-miner-dbus.h"
+
+typedef struct {
+        DBusGConnection *connection;
+        DBusGProxy *gproxy;
+        GHashTable *name_monitors;
+} DBusData;
+
+static GQuark dbus_data = 0;
+
+static gboolean
+dbus_register_service (DBusGProxy  *proxy,
+		       const gchar *name)
+{
+	GError *error = NULL;
+	guint	result;
+
+	g_message ("Registering D-Bus service...\n"
+		   "  Name:'%s'",
+		   name);
+
+	if (!org_freedesktop_DBus_request_name (proxy,
+						name,
+						DBUS_NAME_FLAG_DO_NOT_QUEUE,
+						&result, &error)) {
+		g_critical ("Could not acquire name:'%s', %s",
+			    name,
+			    error ? error->message : "no error given");
+		g_error_free (error);
+
+		return FALSE;
+	}
+
+	if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
+		g_critical ("D-Bus service name:'%s' is already taken, "
+			    "perhaps the application is already running?",
+			    name);
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+static gboolean
+dbus_register_object (GObject		    *object,
+		      DBusGConnection	    *connection,
+		      DBusGProxy	    *proxy,
+		      const DBusGObjectInfo *info,
+		      const gchar	    *path)
+{
+	g_message ("Registering D-Bus object...");
+	g_message ("  Path:'%s'", path);
+	g_message ("  Object Type:'%s'", G_OBJECT_TYPE_NAME (object));
+
+	dbus_g_object_type_install_info (G_OBJECT_TYPE (object), info);
+	dbus_g_connection_register_g_object (connection, path, object);
+
+	return TRUE;
+}
+
+static void
+name_owner_changed_cb (DBusGProxy *proxy,
+		       gchar	  *name,
+		       gchar	  *old_owner,
+		       gchar	  *new_owner,
+		       gpointer    user_data)
+{
+	TrackerMinerDBusNameFunc func;
+        TrackerMiner *miner;
+        gboolean available;
+        DBusData *data;
+
+        miner = user_data;
+
+        if (!name || !*name) {
+                return;
+        }
+
+	data = g_object_get_qdata (G_OBJECT (miner), dbus_data);
+
+        if (!data) {
+                return;
+        }
+
+        func = g_hash_table_lookup (data->name_monitors, name);
+
+        if (!func) {
+                return;
+        }
+
+	available = (new_owner && *new_owner);
+        (func) (miner, name, available);
+}
+
+static void
+dbus_set_name_monitor (TrackerMiner *miner,
+		       DBusGProxy   *proxy)
+{
+	dbus_g_proxy_add_signal (proxy, "NameOwnerChanged",
+				 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
+				 G_TYPE_INVALID);
+
+	dbus_g_proxy_connect_signal (proxy, "NameOwnerChanged",
+				     G_CALLBACK (name_owner_changed_cb),
+				     miner, NULL);
+}
+
+static void
+dbus_data_destroy (gpointer data)
+{
+	DBusData *dd;
+
+	dd = data;
+
+	if (dd->gproxy) {
+		g_object_unref (dd->gproxy);
+	}
+
+	if (dd->connection) {
+		dbus_g_connection_unref (dd->connection);
+	}
+
+	if (dd->name_monitors) {
+		g_hash_table_unref (dd->name_monitors);
+	}
+
+	g_slice_free (DBusData, dd);
+}
+
+static DBusData *
+dbus_data_create (TrackerMiner          *miner,
+		  const gchar           *name,
+                  const DBusGObjectInfo *info)
+{
+	DBusData *data;
+	DBusGConnection *connection;
+	DBusGProxy *gproxy;
+	GError *error = NULL;
+	gchar *full_name, *full_path;
+
+	connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+
+	if (!connection) {
+		g_critical ("Could not connect to the D-Bus session bus, %s",
+			    error ? error->message : "no error given.");
+		g_error_free (error);
+		return NULL;
+	}
+
+	gproxy = dbus_g_proxy_new_for_name (connection,
+					    DBUS_SERVICE_DBUS,
+					    DBUS_PATH_DBUS,
+					    DBUS_INTERFACE_DBUS);
+
+	/* Register the service name for the miner */
+	full_name = g_strconcat (TRACKER_MINER_DBUS_NAME_PREFIX, name, NULL);
+
+	if (!dbus_register_service (gproxy, full_name)) {
+		g_object_unref (gproxy);
+		g_free (full_name);
+		return NULL;
+	}
+
+	g_free (full_name);
+
+	full_path = g_strconcat (TRACKER_MINER_DBUS_PATH_PREFIX, name, NULL);
+
+	if (!dbus_register_object (G_OBJECT (miner),
+				   connection, gproxy,
+                                   info,
+				   full_path)) {
+		g_object_unref (gproxy);
+		g_free (full_path);
+		return NULL;
+	}
+
+        dbus_set_name_monitor (miner, gproxy);
+
+	g_free (full_path);
+
+	/* Now we're successfully connected and registered, create the data */
+	data = g_slice_new0 (DBusData);
+	data->connection = dbus_g_connection_ref (connection);
+	data->gproxy = g_object_ref (gproxy);
+        data->name_monitors = g_hash_table_new_full (g_str_hash,
+                                                     g_str_equal,
+                                                     (GDestroyNotify) g_free,
+                                                     NULL);
+
+	return data;
+}
+
+void
+tracker_miner_dbus_init (TrackerMiner          *miner,
+                         const DBusGObjectInfo *info)
+{
+	DBusData *data;
+        gchar *name;
+
+	if (G_UNLIKELY (dbus_data == 0)) {
+		dbus_data = g_quark_from_static_string ("tracker-miner-dbus-data");
+	}
+
+	data = g_object_get_qdata (G_OBJECT (miner), dbus_data);
+
+        if (data) {
+                return;
+        }
+
+        g_object_get (miner, "name", &name, NULL);
+
+	if (!name) {
+		g_critical ("Miner '%s' should have been given a name, bailing out",
+                            G_OBJECT_TYPE_NAME (miner));
+		g_assert_not_reached ();
+	}
+
+        data = dbus_data_create (miner, name, info);
+
+	if (G_UNLIKELY (!data)) {
+		g_critical ("Miner could not register object on D-Bus session");
+		exit (EXIT_FAILURE);
+		return;
+	}
+
+	g_object_set_qdata_full (G_OBJECT (miner),
+				 dbus_data,
+				 data,
+				 dbus_data_destroy);
+}
+
+void
+tracker_miner_dbus_add_name_watch (TrackerMiner             *miner,
+                                   const gchar              *name,
+                                   TrackerMinerDBusNameFunc  func)
+{
+        DBusData *data;
+
+	data = g_object_get_qdata (G_OBJECT (miner), dbus_data);
+
+        if (!data) {
+                g_critical ("Miner '%s' was not registered on "
+                            "DBus, can watch for owner changes",
+                            G_OBJECT_TYPE_NAME (miner));
+                return;
+        }
+
+        g_hash_table_insert (data->name_monitors,
+                             g_strdup (name),
+                             func);
+}
diff --git a/src/libtracker-miner/tracker-dbus.h b/src/libtracker-miner/tracker-dbus.h
new file mode 100644
index 0000000..d163aaf
--- /dev/null
+++ b/src/libtracker-miner/tracker-dbus.h
@@ -0,0 +1,38 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009, Nokia (urho konttori nokia com)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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.
+ */
+
+#ifndef __TRACKER_MINER_DBUS_HELPER_H__
+#define __TRACKER_MINER_DBUS_HELPER_H__
+
+#include <libtracker-common/tracker-dbus.h>
+#include "tracker-miner.h"
+
+typedef void (* TrackerMinerDBusNameFunc) (TrackerMiner *miner,
+                                           const gchar  *name,
+                                           gboolean      available);
+
+void  tracker_miner_dbus_init (TrackerMiner          *miner,
+                               const DBusGObjectInfo *info);
+
+void  tracker_miner_dbus_add_name_watch (TrackerMiner             *miner,
+                                         const gchar              *name,
+                                         TrackerMinerDBusNameFunc  func);
+
+#endif /* __TRACKER_MINER_DBUS_HELPER_H__ */
diff --git a/src/libtracker-miner/tracker-miner.c b/src/libtracker-miner/tracker-miner.c
index 457a270..2b24491 100644
--- a/src/libtracker-miner/tracker-miner.c
+++ b/src/libtracker-miner/tracker-miner.c
@@ -19,8 +19,6 @@
 
 #include "config.h"
 
-#include <stdlib.h>
-
 #include <libtracker-common/tracker-dbus.h>
 #include <libtracker-common/tracker-type-utils.h>
 
@@ -28,6 +26,7 @@
 #include "tracker-miner.h"
 #include "tracker-miner-dbus.h"
 #include "tracker-miner-glue.h"
+#include "tracker-dbus.h"
 
 /**
  * SECTION:tracker-miner
@@ -62,13 +61,6 @@ struct TrackerMinerPrivate {
 };
 
 typedef struct {
-	gchar *registered_name;
-	DBusGConnection *connection;
-	DBusGProxy *proxy;
-	GHashTable *name_monitors;
-} DBusData;
-
-typedef struct {
 	gint cookie;
 	gchar *application;
 	gchar *reason;
@@ -103,8 +95,6 @@ enum {
 	LAST_SIGNAL
 };
 
-static GQuark dbus_data = 0;
-
 static guint signals[LAST_SIGNAL] = { 0 };
 
 static void       miner_set_property           (GObject       *object,
@@ -130,6 +120,9 @@ static void       async_call_data_destroy      (AsyncCallData *data,
                                                 gboolean       remove);
 static void       sparql_cancelled_cb          (GCancellable  *cancellable,
                                                 AsyncCallData *data);
+static void       store_name_monitor_cb (TrackerMiner *miner,
+                                         const gchar  *name,
+                                         gboolean      available);
 
 G_DEFINE_ABSTRACT_TYPE (TrackerMiner, tracker_miner, G_TYPE_OBJECT)
 
@@ -413,10 +406,6 @@ miner_finalize (GObject *object)
 		g_object_unref (miner->private->client);
 	}
 
-	if (dbus_data != 0) {
-		g_object_set_qdata (G_OBJECT (miner), dbus_data, NULL);
-	}
-
 	g_hash_table_unref (miner->private->pauses);
 
 	g_ptr_array_foreach (miner->private->async_calls,
@@ -430,146 +419,33 @@ miner_finalize (GObject *object)
 static void
 miner_constructed (GObject *object)
 {
-	TrackerMiner *miner;
-	DBusData *data;
-
-	miner = TRACKER_MINER (object);
-
-	if (!miner->private->name) {
-		g_critical ("Miner should have been given a name, bailing out");
-		g_assert_not_reached ();
-	}
-
-	if (G_UNLIKELY (dbus_data == 0)) {
-		dbus_data = g_quark_from_static_string ("tracker-miner-dbus-data");
-	}
-
-	data = g_object_get_qdata (G_OBJECT (miner), dbus_data);
-
-	if (G_LIKELY (!data)) {
-		data = dbus_data_new (miner, miner->private->name);
-	}
-
-	if (G_UNLIKELY (!data)) {
-		g_critical ("Miner could not register object on D-Bus session");
-		exit (EXIT_FAILURE);
-		return;
-	}
-
-	g_object_set_qdata_full (G_OBJECT (miner),
-	                         dbus_data,
-	                         data,
-	                         dbus_data_destroy);
-}
-
-static gboolean
-dbus_register_service (DBusGProxy  *proxy,
-                       const gchar *name)
-{
-	GError *error = NULL;
-	guint result;
-
-	g_message ("Registering D-Bus service...\n"
-	           "  Name:'%s'",
-	           name);
-
-	if (!org_freedesktop_DBus_request_name (proxy,
-	                                        name,
-	                                        DBUS_NAME_FLAG_DO_NOT_QUEUE,
-	                                        &result, &error)) {
-		g_critical ("Could not register name:'%s', %s",
-		            name,
-		            error ? error->message : "no error given");
-		g_error_free (error);
-
-		return FALSE;
-	}
-
-	if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
-		g_critical ("D-Bus service name:'%s' is already taken, "
-		            "perhaps the application is already running?",
-		            name);
-		return FALSE;
-	}
-
-	return TRUE;
-}
-
-static gboolean
-dbus_release_service (DBusGProxy  *proxy,
-                      const gchar *name)
-{
-	GError *error = NULL;
-	guint result;
-
-	g_message ("Releasing D-Bus service...\n"
-	           "  Name:'%s'",
-	           name);
-
-	if (!org_freedesktop_DBus_release_name (proxy, name, &result, &error)) {
-		g_critical ("Could not release name:'%s', %s",
-		            name,
-		            error ? error->message : "no error given");
-		g_error_free (error);
-
-		return FALSE;
-	}
-
-	switch (result) {
-	case DBUS_RELEASE_NAME_REPLY_RELEASED:
-		break;
-
-	case DBUS_RELEASE_NAME_REPLY_NON_EXISTENT:
-		g_critical ("D-Bus service name:'%s' does not exist?",
-		            name);
-		return FALSE;
-
-	case DBUS_RELEASE_NAME_REPLY_NOT_OWNER:
-		g_critical ("D-Bus service name:'%s' was not started by us, "
-		            "we can not release the name!",
-		            name);
-		return FALSE;
-	}
+	TrackerMiner *miner = TRACKER_MINER (object);
 
-	return TRUE;
+	tracker_miner_dbus_init (miner, &dbus_glib_tracker_miner_object_info);
+	tracker_miner_dbus_add_name_watch (miner, "org.freedesktop.Tracker1",
+                                       store_name_monitor_cb);
 }
 
-static gboolean
-dbus_register_object (GObject               *object,
-                      DBusGConnection       *connection,
-                      DBusGProxy            *proxy,
-                      const DBusGObjectInfo *info,
-                      const gchar           *path)
+/**
+ * tracker_miner_error_quark:
+ *
+ * Returns the #GQuark used to identify miner errors in GError structures.
+ *
+ * Returns: the error #GQuark
+ **/
+GQuark
+tracker_miner_error_quark (void)
 {
-	g_message ("Registering D-Bus object...");
-	g_message ("  Path:'%s'", path);
-	g_message ("  Object Type:'%s'", G_OBJECT_TYPE_NAME (object));
-
-	dbus_g_object_type_install_info (G_OBJECT_TYPE (object), info);
-	dbus_g_connection_register_g_object (connection, path, object);
-
-	return TRUE;
+	return g_quark_from_static_string (TRACKER_MINER_ERROR_DOMAIN);
 }
 
 static void
-name_owner_changed_cb (DBusGProxy *proxy,
-                       gchar      *name,
-                       gchar      *old_owner,
-                       gchar      *new_owner,
-                       gpointer    user_data)
+store_name_monitor_cb (TrackerMiner *miner,
+		       const gchar  *name,
+		       gboolean      available)
 {
-	TrackerMiner *miner;
-	gboolean available;
 	GError *error = NULL;
 
-	if (!name || !*name ||
-	    strcmp (name, "org.freedesktop.Tracker1") != 0) {
-		return;
-	}
-
-	miner = user_data;
-	available = (new_owner && *new_owner);
-
 	g_debug ("Tracker-store availability has changed to %d", available);
 
 	if (available && miner->private->availability_cookie != 0) {
@@ -599,102 +475,6 @@ name_owner_changed_cb (DBusGProxy *proxy,
 	}
 }
 
-static void
-dbus_set_name_monitor (TrackerMiner *miner,
-                       DBusGProxy   *proxy)
-{
-	dbus_g_proxy_add_signal (proxy, "NameOwnerChanged",
-	                         G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
-	                         G_TYPE_INVALID);
-
-	dbus_g_proxy_connect_signal (proxy, "NameOwnerChanged",
-	                             G_CALLBACK (name_owner_changed_cb),
-	                             miner, NULL);
-}
-
-static void
-dbus_data_destroy (gpointer data)
-{
-	DBusData *dd;
-
-	dd = data;
-
-	/* Release the service name for the miner */
-	dbus_release_service (dd->proxy, dd->registered_name);
-
-	if (dd->proxy) {
-		g_object_unref (dd->proxy);
-	}
-
-	if (dd->connection) {
-		dbus_g_connection_unref (dd->connection);
-	}
-
-	if (dd->name_monitors) {
-		g_hash_table_unref (dd->name_monitors);
-	}
-
-	g_slice_free (DBusData, dd);
-}
-
-static DBusData *
-dbus_data_new (TrackerMiner *miner,
-               const gchar  *name)
-{
-	DBusData *data;
-	DBusGConnection *connection;
-	DBusGProxy *proxy;
-	GError *error = NULL;
-	gchar *full_name, *full_path;
-
-	connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
-
-	if (!connection) {
-		g_critical ("Could not connect to the D-Bus session bus, %s",
-		            error ? error->message : "no error given.");
-		g_error_free (error);
-		return NULL;
-	}
-
-	proxy = dbus_g_proxy_new_for_name (connection,
-	                                    DBUS_SERVICE_DBUS,
-	                                    DBUS_PATH_DBUS,
-	                                    DBUS_INTERFACE_DBUS);
-
-	/* Register the service name for the miner */
-	full_name = g_strconcat (TRACKER_MINER_DBUS_NAME_PREFIX, name, NULL);
-
-	if (!dbus_register_service (proxy, full_name)) {
-		g_object_unref (proxy);
-		g_free (full_name);
-		return NULL;
-	}
-
-	full_path = g_strconcat (TRACKER_MINER_DBUS_PATH_PREFIX, name, NULL);
-
-	if (!dbus_register_object (G_OBJECT (miner),
-	                           connection, proxy,
-	                           &dbus_glib_tracker_miner_object_info,
-	                           full_path)) {
-		g_object_unref (proxy);
-		g_free (full_name);
-		g_free (full_path);
-		return NULL;
-	}
-
-	dbus_set_name_monitor (miner, proxy);
-
-	g_free (full_path);
-
-	/* Now we're successfully connected and registered, create the data */
-	data = g_slice_new0 (DBusData);
-	data->registered_name = full_name;
-	data->connection = dbus_g_connection_ref (connection);
-	data->proxy = g_object_ref (proxy);
-
-	return data;
-}
-
 static PauseData *
 pause_data_new (const gchar *application,
                 const gchar *reason)



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