[tracker/gdbus-porting: 16/65] libtracker-miner: GDBus porting of tracker-miner-web.c



commit 27a9cc6219f1565186f421f1bf28f4a14c7ea024
Author: Philip Van Hoof <philip codeminded be>
Date:   Thu Dec 30 13:36:38 2010 +0100

    libtracker-miner: GDBus porting of tracker-miner-web.c

 src/libtracker-miner/tracker-miner-web.c |  340 +++++++++++++++++++++++++-----
 1 files changed, 287 insertions(+), 53 deletions(-)
---
diff --git a/src/libtracker-miner/tracker-miner-web.c b/src/libtracker-miner/tracker-miner-web.c
index 9ca7e72..e2c51c7 100644
--- a/src/libtracker-miner/tracker-miner-web.c
+++ b/src/libtracker-miner/tracker-miner-web.c
@@ -23,9 +23,21 @@
 #include "tracker-miner-web.h"
 #include "tracker-miner-web-dbus.h"
 
-#if 0
-#include "tracker-miner-web-glue.h"
-#endif
+/* Introspection data for the service we are exporting */
+static const gchar introspection_xml[] =
+  "<node>"
+  "  <interface name='org.freedesktop.Tracker1.Miner.Web'>"
+  "    <method name='Authenticate' />"
+  "    <method name='GetAssociationData'>"
+  "      <arg name='result' type='a{ss}' direction='out' />"
+  "    </method>"
+  "    <method name='Associate'>"
+  "      <arg name='data' type='a{ss}' direction='in' />"
+  "    </method>"
+  "    <method name='Dissociate' />"
+  "    <property name='Associated' type='b' access='read' />"
+  "  </interface>"
+  "</node>";
 
 /**
  * SECTION:tracker-miner-web
@@ -42,6 +54,12 @@
 
 struct TrackerMinerWebPrivate {
 	gboolean associated;
+	GDBusConnection *d_connection;
+	GDBusNodeInfo *introspection_data;
+	guint registration_id;
+	guint own_id;
+	gchar *full_name;
+	gchar *full_path;
 };
 
 enum {
@@ -58,6 +76,7 @@ static void miner_web_get_property (GObject      *object,
                                     GValue       *value,
                                     GParamSpec   *pspec);
 static void miner_web_constructed  (GObject      *object);
+static void miner_web_finalize     (GObject       *object);
 
 G_DEFINE_ABSTRACT_TYPE (TrackerMinerWeb, tracker_miner_web, TRACKER_TYPE_MINER)
 
@@ -66,6 +85,7 @@ tracker_miner_web_class_init (TrackerMinerWebClass *klass)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+	object_class->finalize     = miner_web_finalize;
 	object_class->set_property = miner_web_set_property;
 	object_class->get_property = miner_web_get_property;
 	object_class->constructed  = miner_web_constructed;
@@ -128,108 +148,323 @@ miner_web_get_property (GObject    *object,
 }
 
 static void
-miner_web_constructed (GObject *object)
+miner_web_finalize (GObject *object)
 {
-#if 0
-	_tracker_miner_dbus_init (TRACKER_MINER (object),
-	                          &dbus_glib__tracker_miner_web_dbus_object_info);
-#endif
+	TrackerMinerWebPrivate *priv;
 
-	G_OBJECT_CLASS (tracker_miner_web_parent_class)->constructed (object);
-}
+	priv = TRACKER_MINER_WEB_GET_PRIVATE (object);
 
-/**
- * tracker_miner_web_error_quark:
- *
- * Returns: the #GQuark used to identify miner web errors in GError
- * structures.
- **/
-GQuark
-tracker_miner_web_error_quark (void)
-{
-	return g_quark_from_static_string (TRACKER_MINER_WEB_ERROR_DOMAIN);
+	if (priv->own_id != 0) {
+		g_bus_unown_name (priv->own_id);
+	}
+
+	if (priv->registration_id != 0) {
+		g_dbus_connection_unregister_object (priv->d_connection,
+		                                     priv->registration_id);
+	}
+
+	if (priv->introspection_data) {
+		g_dbus_node_info_unref (priv->introspection_data);
+	}
+
+	if (priv->d_connection) {
+		g_object_unref (priv->d_connection);
+	}
+
+	g_free (priv->full_name);
+	g_free (priv->full_path);
+
+	G_OBJECT_CLASS (tracker_miner_web_parent_class)->finalize (object);
 }
 
-#if 0
-/* DBus methods */
-void
-_tracker_miner_web_dbus_authenticate (TrackerMinerWeb        *miner,
-                                      DBusGMethodInvocation  *context,
-                                      GError                **error)
+static void
+handle_method_call_authenticate (TrackerMinerWeb       *miner,
+                                 GDBusMethodInvocation *invocation,
+                                 GVariant              *parameters)
 {
 	GError *local_error = NULL;
+	TrackerDBusRequest *request;
 
-	g_return_if_fail (TRACKER_IS_MINER_WEB (miner));
+	request = tracker_g_dbus_request_begin (invocation, "%s()", __PRETTY_FUNCTION__);
 
 	TRACKER_MINER_WEB_GET_CLASS (miner)->authenticate (miner, &local_error);
 
 	if (local_error != NULL) {
-		dbus_g_method_return_error (context, local_error);
+		tracker_dbus_request_end (request, local_error);
+
+		g_dbus_method_invocation_return_gerror (invocation, local_error);
+
 		g_error_free (local_error);
 	} else {
-		dbus_g_method_return (context);
+		tracker_dbus_request_end (request, NULL);
+		g_dbus_method_invocation_return_value (invocation, NULL);
 	}
 }
 
-void
-_tracker_miner_web_dbus_get_association_data (TrackerMinerWeb        *miner,
-                                              DBusGMethodInvocation  *context,
-                                              GError                **error)
+static GVariant*
+variant_from_hashtable (GHashTable *table)
+{
+	GVariantBuilder builder;
+	GHashTableIter iter;
+	gpointer key;
+	gpointer value;
+
+	g_hash_table_iter_init (&iter, table);
+	g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
+
+	while (g_hash_table_iter_next (&iter, &key, &value)) {
+		g_variant_builder_add (&builder, "{?*}",
+		                       g_variant_new_string ((const gchar*) key),
+		                       g_variant_new_string ((const gchar *) value));
+	}
+
+	return g_variant_ref_sink (g_variant_builder_end (&builder));
+}
+
+static GHashTable*
+hashtable_from_variant (GVariant *variant)
+{
+	GHashTable* table;
+	GVariantIter iter;
+	GVariant* variant1;
+	GVariant* variant2;
+
+	table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+	g_variant_iter_init (&iter, variant);
+
+	while (g_variant_iter_loop (&iter, "{?*}", &variant1, &variant2)) {
+		g_hash_table_insert (table,
+		                     g_variant_dup_string (variant1, NULL),
+		                     g_variant_dup_string (variant2, NULL));
+	}
+
+	return table;
+}
+
+static void
+handle_method_call_get_association_data (TrackerMinerWeb       *miner,
+                                         GDBusMethodInvocation *invocation,
+                                         GVariant              *parameters)
 {
 	GHashTable *association_data;
 	GError *local_error = NULL;
+	TrackerDBusRequest *request;
 
 	g_return_if_fail (TRACKER_IS_MINER_WEB (miner));
 
+	request = tracker_g_dbus_request_begin (invocation, "%s()", __PRETTY_FUNCTION__);
+
 	association_data = TRACKER_MINER_WEB_GET_CLASS (miner)->get_association_data (miner, &local_error);
 
 	if (local_error != NULL) {
-		dbus_g_method_return_error (context, local_error);
+		tracker_dbus_request_end (request, local_error);
+
+		g_dbus_method_invocation_return_gerror (invocation, local_error);
+
 		g_error_free (local_error);
 	} else {
-		dbus_g_method_return (context, association_data);
-		/* g_hash_table_unref (association_data); */
+		tracker_dbus_request_end (request, NULL);
+		g_dbus_method_invocation_return_value (invocation, variant_from_hashtable (association_data));
+
+		/* This was commented out before GDBus port too
+		 * g_hash_table_unref (association_data); */
 	}
 }
 
-void
-_tracker_miner_web_dbus_associate (TrackerMinerWeb        *miner,
-                                   GHashTable             *association_data,
-                                   DBusGMethodInvocation  *context,
-                                   GError                **error)
+static void
+handle_method_call_associate (TrackerMinerWeb       *miner,
+                              GDBusMethodInvocation *invocation,
+                              GVariant              *parameters)
 {
+	GHashTable *association_data;
 	GError *local_error = NULL;
+	TrackerDBusRequest *request;
 
-	g_return_if_fail (TRACKER_IS_MINER_WEB (miner));
-	g_return_if_fail (association_data != NULL);
+	association_data = hashtable_from_variant (parameters);
+
+	request = tracker_g_dbus_request_begin (invocation, "%s()", __PRETTY_FUNCTION__);
 
 	TRACKER_MINER_WEB_GET_CLASS (miner)->associate (miner, association_data, &local_error);
 
+	g_hash_table_unref (association_data);
+
 	if (local_error != NULL) {
-		dbus_g_method_return_error (context, local_error);
+		tracker_dbus_request_end (request, local_error);
+
+		g_dbus_method_invocation_return_gerror (invocation, local_error);
+
 		g_error_free (local_error);
 	} else {
-		dbus_g_method_return (context);
+		tracker_dbus_request_end (request, NULL);
+		g_dbus_method_invocation_return_value (invocation, NULL);
 	}
 }
 
-void
-_tracker_miner_web_dbus_dissociate (TrackerMinerWeb        *miner,
-                                    DBusGMethodInvocation  *context,
-                                    GError                **error)
+static void
+handle_method_call_dissociate (TrackerMinerWeb       *miner,
+                               GDBusMethodInvocation *invocation,
+                               GVariant              *parameters)
 {
 	GError *local_error = NULL;
+	TrackerDBusRequest *request;
 
-	g_return_if_fail (TRACKER_IS_MINER_WEB (miner));
+	request = tracker_g_dbus_request_begin (invocation, "%s()", __PRETTY_FUNCTION__);
 
 	TRACKER_MINER_WEB_GET_CLASS (miner)->dissociate (miner, &local_error);
 
 	if (local_error != NULL) {
-		dbus_g_method_return_error (context, local_error);
+		tracker_dbus_request_end (request, local_error);
+
+		g_dbus_method_invocation_return_gerror (invocation, local_error);
+
 		g_error_free (local_error);
 	} else {
-		dbus_g_method_return (context);
+		tracker_dbus_request_end (request, NULL);
+		g_dbus_method_invocation_return_value (invocation, NULL);
+	}
+}
+
+static void
+handle_method_call (GDBusConnection       *connection,
+                    const gchar           *sender,
+                    const gchar           *object_path,
+                    const gchar           *interface_name,
+                    const gchar           *method_name,
+                    GVariant              *parameters,
+                    GDBusMethodInvocation *invocation,
+                    gpointer               user_data)
+{
+	TrackerMinerWeb *miner = user_data;
+
+	tracker_gdbus_async_return_if_fail (miner != NULL, invocation);
+	tracker_gdbus_async_return_if_fail (TRACKER_IS_MINER_WEB (miner), invocation);
+
+	if (g_strcmp0 (method_name, "Authenticate") == 0) {
+		handle_method_call_authenticate (miner, invocation, parameters);
+	} else
+	if (g_strcmp0 (method_name, "GetAssociationData") == 0) {
+		handle_method_call_get_association_data (miner, invocation, parameters);
+	} else
+	if (g_strcmp0 (method_name, "Associate") == 0) {
+		handle_method_call_associate (miner, invocation, parameters);
+	} else
+	if (g_strcmp0 (method_name, "Dissociate") == 0) {
+		handle_method_call_dissociate (miner, invocation, parameters);
+	} else {
+		g_assert_not_reached ();
+	}
+}
+
+static GVariant *
+handle_get_property (GDBusConnection  *connection,
+                     const gchar      *sender,
+                     const gchar      *object_path,
+                     const gchar      *interface_name,
+                     const gchar      *property_name,
+                     GError          **error,
+                     gpointer          user_data)
+{
+	g_assert_not_reached ();
+	return NULL;
+}
+
+static gboolean
+handle_set_property (GDBusConnection  *connection,
+                     const gchar      *sender,
+                     const gchar      *object_path,
+                     const gchar      *interface_name,
+                     const gchar      *property_name,
+                     GVariant         *value,
+                     GError          **error,
+                     gpointer          user_data)
+{
+	g_assert_not_reached ();
+	return TRUE;
+}
+
+static const GDBusInterfaceVTable interface_vtable = {
+	handle_method_call,
+	handle_get_property,
+	handle_set_property
+};
+
+static void
+miner_web_constructed (GObject *miner)
+{
+	TrackerMinerWebPrivate *priv;
+	gchar *name, *full_path, *full_name;
+	GError *error = NULL;
+
+	priv = TRACKER_MINER_WEB_GET_PRIVATE (miner);
+
+	priv->d_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+
+	if (!priv->d_connection) {
+		g_critical ("Could not connect to the D-Bus session bus, %s",
+		            error ? error->message : "no error given.");
+		g_clear_error (&error);
+		return;
 	}
+
+	priv->introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
+
+	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 ();
+	}
+
+	full_name = g_strconcat (TRACKER_MINER_DBUS_NAME_PREFIX, name, NULL);
+
+	priv->own_id = g_bus_own_name_on_connection (priv->d_connection,
+	                                             full_name,
+	                                             G_BUS_NAME_OWNER_FLAGS_NONE,
+	                                             NULL, NULL, NULL, NULL);
+	priv->full_name = full_name;
+
+	/* Register the service name for the miner */
+	full_path = g_strconcat (TRACKER_MINER_DBUS_PATH_PREFIX, name, NULL);
+
+	g_message ("Registering D-Bus object...");
+	g_message ("  Path:'%s'", full_path);
+	g_message ("  Object Type:'%s'", G_OBJECT_TYPE_NAME (miner));
+
+	priv->registration_id =
+		g_dbus_connection_register_object (priv->d_connection,
+	                                       full_path,
+	                                       priv->introspection_data->interfaces[0],
+	                                       &interface_vtable,
+	                                       miner,
+	                                       NULL,
+	                                       &error);
+
+	if (error) {
+		g_critical ("Could not register the D-Bus object %s, %s",
+		            full_path,
+		            error ? error->message : "no error given.");
+		g_clear_error (&error);
+		return;
+	}
+
+	g_free (name);
+
+	priv->full_path = full_path;
+
+	G_OBJECT_CLASS (tracker_miner_web_parent_class)->constructed (miner);
+}
+
+/**
+ * tracker_miner_web_error_quark:
+ *
+ * Returns: the #GQuark used to identify miner web errors in GError
+ * structures.
+ **/
+GQuark
+tracker_miner_web_error_quark (void)
+{
+	return g_quark_from_static_string (TRACKER_MINER_WEB_ERROR_DOMAIN);
 }
 
 /**
@@ -315,4 +550,3 @@ tracker_miner_web_dissociate (TrackerMinerWeb   *miner,
 
 	TRACKER_MINER_WEB_GET_CLASS (miner)->dissociate (miner, error);
 }
-#endif



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