[tracker/gdbus-porting: 35/65] tracker-extract: GDBus porting



commit e2cd6cbd84430c31b32d3a0917e15fd66a24d25a
Author: Philip Van Hoof <philip codeminded be>
Date:   Fri Dec 31 15:45:18 2010 +0100

    tracker-extract: GDBus porting

 src/tracker-extract/Makefile.am                 |   11 +-
 src/tracker-extract/tracker-extract-gstreamer.c |    1 -
 src/tracker-extract/tracker-extract-mp3.c       |    1 -
 src/tracker-extract/tracker-extract.c           |  218 ++++++++++++++++++-----
 src/tracker-extract/tracker-extract.h           |   14 +--
 src/tracker-extract/tracker-main.c              |   26 +---
 6 files changed, 184 insertions(+), 87 deletions(-)
---
diff --git a/src/tracker-extract/Makefile.am b/src/tracker-extract/Makefile.am
index 37ed716..4c69748 100644
--- a/src/tracker-extract/Makefile.am
+++ b/src/tracker-extract/Makefile.am
@@ -335,13 +335,10 @@ libexec_PROGRAMS = tracker-extract
 
 tracker_extract_SOURCES = \
 	$(marshal_sources) \
-	$(dbus_sources) \
 	tracker-albumart.c \
 	tracker-albumart.h \
 	tracker-config.c \
 	tracker-config.h \
-	tracker-dbus.c \
-	tracker-dbus.h \
 	tracker-extract.c \
 	tracker-extract.h \
 	tracker-read.c \
@@ -384,8 +381,6 @@ marshal_sources = \
         tracker-marshal.h \
         tracker-marshal.c
 
-dbus_sources = tracker-extract-glue.h
-
 tracker-marshal.h: tracker-marshal.list
 	$(AM_V_GEN)$(GLIB_GENMARSHAL) $< --prefix=tracker_marshal --header > $@
 
@@ -393,12 +388,8 @@ tracker-marshal.c: tracker-marshal.list
 	$(AM_V_GEN)echo "#include \"tracker-marshal.h\"" > $@ && \
 		   $(GLIB_GENMARSHAL) $< --prefix=tracker_marshal --body >> $@
 
-%-glue.h: $(top_srcdir)/data/dbus/%.xml
-	$(AM_V_GEN)$(DBUSBINDINGTOOL) --mode=glib-server --output=$@ --prefix=$(subst -,_,$*) $^
-
 BUILT_SOURCES =                                        \
-	$(marshal_sources)                             \
-	$(dbus_sources)
+	$(marshal_sources)                             
 
 CLEANFILES = $(BUILT_SOURCES)
 
diff --git a/src/tracker-extract/tracker-extract-gstreamer.c b/src/tracker-extract/tracker-extract-gstreamer.c
index 29a0780..a7dd539 100644
--- a/src/tracker-extract/tracker-extract-gstreamer.c
+++ b/src/tracker-extract/tracker-extract-gstreamer.c
@@ -35,7 +35,6 @@
 #include <libtracker-extract/tracker-extract.h>
 
 #include "tracker-albumart.h"
-#include "tracker-dbus.h"
 
 /* We wait this long (seconds) for NULL state before freeing */
 #define TRACKER_EXTRACT_GUARD_TIMEOUT 3
diff --git a/src/tracker-extract/tracker-extract-mp3.c b/src/tracker-extract/tracker-extract-mp3.c
index d7dc2d7..14c3b6e 100644
--- a/src/tracker-extract/tracker-extract-mp3.c
+++ b/src/tracker-extract/tracker-extract-mp3.c
@@ -48,7 +48,6 @@
 #include <libtracker-extract/tracker-extract.h>
 
 #include "tracker-albumart.h"
-#include "tracker-dbus.h"
 
 /* We mmap the beginning of the file and read separately the last 128
  * bytes for id3v1 tags. While these are probably cornercases the
diff --git a/src/tracker-extract/tracker-extract.c b/src/tracker-extract/tracker-extract.c
index 48827cc..76a30ed 100644
--- a/src/tracker-extract/tracker-extract.c
+++ b/src/tracker-extract/tracker-extract.c
@@ -32,7 +32,6 @@
 
 #include <libtracker-extract/tracker-extract.h>
 
-#include "tracker-dbus.h"
 #include "tracker-extract.h"
 #include "tracker-main.h"
 #include "tracker-marshal.h"
@@ -48,6 +47,21 @@
 #define UNKNOWN_METHOD_MESSAGE "Method \"%s\" with signature \"%s\" on " \
                                "interface \"%s\" doesn't exist, expected \"%s\""
 
+static const gchar introspection_xml[] =
+  "<node>"
+  "  <interface name='org.freedesktop.Tracker1.Extract'>"
+  "    <method name='GetPid'>"
+  "      <arg type='i' name='value' direction='out' />"
+  "    </method>"
+  "    <method name='GetMetadata'>"
+  "      <arg type='s' name='uri' direction='in' />"
+  "      <arg type='s' name='mime' direction='in' />"
+  "      <arg type='s' name='preupdate' direction='out' />"
+  "      <arg type='s' name='embedded' direction='out' />"
+  "    </method>"
+  "  </interface>"
+  "</node>";
+
 #define TRACKER_EXTRACT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_EXTRACT, TrackerExtractPrivate))
 
 extern gboolean debug;
@@ -58,6 +72,10 @@ typedef struct {
 	gboolean disable_shutdown;
 	gboolean force_internal_extractors;
 	gboolean disable_summary_on_finalize;
+	GDBusConnection *d_connection;
+	GDBusNodeInfo *introspection_data;
+	guint registration_id;
+	guint own_id;
 } TrackerExtractPrivate;
 
 typedef struct {
@@ -369,11 +387,11 @@ tracker_extract_new (gboolean     disable_shutdown,
 static gboolean
 get_file_metadata (TrackerExtract         *extract,
                    TrackerDBusRequest     *request,
-                   DBusGMethodInvocation  *context,
+                   GDBusMethodInvocation  *invocation,
                    const gchar            *uri,
                    const gchar            *mime,
-		   TrackerSparqlBuilder  **preupdate_out,
-		   TrackerSparqlBuilder  **statements_out)
+                   TrackerSparqlBuilder  **preupdate_out,
+                   TrackerSparqlBuilder  **statements_out)
 {
 	TrackerExtractPrivate *priv;
 	TrackerSparqlBuilder *statements, *preupdate;
@@ -640,15 +658,15 @@ tracker_extract_get_metadata_by_cmdline (TrackerExtract *object,
 	tracker_dbus_request_end (request, NULL);
 }
 
-void
-tracker_extract_get_pid (TrackerExtract         *object,
-                         DBusGMethodInvocation  *context,
-                         GError                **error)
+static void
+handle_method_call_get_pid (TrackerExtract        *object,
+                            GDBusMethodInvocation *invocation,
+                            GVariant              *parameters)
 {
 	TrackerDBusRequest *request;
 	pid_t value;
 
-	request = tracker_dbus_g_request_begin (context,
+	request = tracker_g_dbus_request_begin (invocation,
 	                                        "%s()",
 	                                        __FUNCTION__);
 
@@ -658,25 +676,27 @@ tracker_extract_get_pid (TrackerExtract         *object,
 	                            value);
 
 	tracker_dbus_request_end (request, NULL);
-	dbus_g_method_return (context, value);
+
+	g_dbus_method_invocation_return_value (invocation,
+	                                       g_variant_new ("(i)", (gint) value));
 }
 
-void
-tracker_extract_get_metadata (TrackerExtract         *object,
-                              const gchar            *uri,
-                              const gchar            *mime,
-                              DBusGMethodInvocation  *context,
-                              GError                **error)
+static void
+handle_method_call_get_metadata (TrackerExtract        *object,
+                                 GDBusMethodInvocation *invocation,
+                                 GVariant              *parameters)
 {
 	TrackerDBusRequest *request;
 	TrackerExtractPrivate *priv;
 	TrackerSparqlBuilder *sparql, *preupdate;
 	gboolean extracted = FALSE;
+	const gchar *uri = NULL, *mime = NULL;
 
+	g_variant_get (parameters, "(&s&s)", &uri, &mime);
 
-	tracker_dbus_async_return_if_fail (uri != NULL, context);
+	tracker_gdbus_async_return_if_fail (uri != NULL, invocation);
 
-	request = tracker_dbus_g_request_begin (context,
+	request = tracker_g_dbus_request_begin (invocation,
 	                                        "%s(uri:'%s', mime:%s)",
 	                                        __FUNCTION__,
 	                                        uri,
@@ -692,7 +712,13 @@ tracker_extract_get_metadata (TrackerExtract         *object,
 		alarm (MAX_EXTRACT_TIME);
 	}
 
-	extracted = get_file_metadata (object, request, context, uri, mime, &preupdate, &sparql);
+	extracted = get_file_metadata (object,
+	                               request,
+	                               invocation,
+	                               uri,
+	                               mime,
+	                               &preupdate,
+	                               &sparql);
 
 	if (extracted) {
 		tracker_dbus_request_end (request, NULL);
@@ -704,11 +730,13 @@ tracker_extract_get_metadata (TrackerExtract         *object,
 				preupdate_str = tracker_sparql_builder_get_result (preupdate);
 			}
 
-			dbus_g_method_return (context,
-			                      preupdate_str ? preupdate_str : "",
-			                      tracker_sparql_builder_get_result (sparql));
+			g_dbus_method_invocation_return_value (invocation,
+			                                       g_variant_new ("(ss)",
+			                                                      preupdate_str ? preupdate_str : "",
+			                                                      tracker_sparql_builder_get_result (sparql)));
 		} else {
-			dbus_g_method_return (context, "", "");
+			g_dbus_method_invocation_return_value (invocation,
+			                                       g_variant_new ("(ss)", "", ""));
 		}
 
 		g_object_unref (sparql);
@@ -721,7 +749,7 @@ tracker_extract_get_metadata (TrackerExtract         *object,
 		                            uri,
 		                            mime);
 		tracker_dbus_request_end (request, actual_error);
-		dbus_g_method_return_error (context, actual_error);
+		g_dbus_method_invocation_return_gerror (invocation, actual_error);
 		g_error_free (actual_error);
 	}
 
@@ -731,11 +759,14 @@ tracker_extract_get_metadata (TrackerExtract         *object,
 	}
 }
 
+#if 0
 static void
 get_metadata_fast (TrackerExtract *object,
                    DBusConnection *connection,
                    DBusMessage    *message)
 {
+	// todo: port to GDBus
+
 	TrackerDBusRequest *request;
 	const gchar *expected_signature;
 	TrackerExtractPrivate *priv;
@@ -887,34 +918,135 @@ get_metadata_fast (TrackerExtract *object,
 		alarm (0);
 	}
 }
+#endif
+
+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)
+{
+	TrackerExtract *extract = user_data;
+
+	if (g_strcmp0 (method_name, "GetPid") == 0) {
+		handle_method_call_get_pid (extract, invocation, parameters);
+	} else
+	if (g_strcmp0 (method_name, "GetMetadataFast") == 0) {
+#if 0
+		// todo
+		get_metadata_fast (extract, connection, message);
+#endif
+	} else
+	if (g_strcmp0 (method_name, "GetMetadata") == 0) {
+		handle_method_call_get_metadata (extract, 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
+};
 
-DBusHandlerResult
-tracker_extract_connection_filter (DBusConnection *connection,
-                                   DBusMessage    *message,
-                                   void           *user_data)
+void
+tracker_extract_dbus_start (TrackerExtract *extract)
 {
-	TrackerExtract *extract;
+	TrackerExtractPrivate *priv;
+	GError *error = NULL;
 
-	g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
-	g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+	priv = TRACKER_EXTRACT_GET_PRIVATE (extract);
+
+	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;
+	}
 
-	if (g_strcmp0 (TRACKER_EXTRACT_PATH, dbus_message_get_path (message))) {
-		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	priv->introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
+
+	priv->own_id = g_bus_own_name_on_connection (priv->d_connection,
+	                                             TRACKER_EXTRACT_SERVICE,
+	                                             G_BUS_NAME_OWNER_FLAGS_NONE,
+	                                             NULL, NULL, NULL, NULL);
+
+	g_message ("Registering D-Bus object...");
+	g_message ("  Path:'" TRACKER_EXTRACT_PATH "'");
+	g_message ("  Object Type:'%s'", G_OBJECT_TYPE_NAME (extract));
+
+	priv->registration_id =
+		g_dbus_connection_register_object (priv->d_connection,
+	                                       TRACKER_EXTRACT_PATH,
+	                                       priv->introspection_data->interfaces[0],
+	                                       &interface_vtable,
+	                                       extract,
+	                                       NULL,
+	                                       &error);
+
+	if (error) {
+		g_critical ("Could not register the D-Bus object "TRACKER_EXTRACT_PATH", %s",
+		            error ? error->message : "no error given.");
+		g_clear_error (&error);
+		return;
 	}
+}
+
+void
+tracker_extract_dbus_stop (TrackerExtract *extract)
+{
+	TrackerExtractPrivate *priv;
 
-	if (g_strcmp0 (TRACKER_EXTRACT_INTERFACE, dbus_message_get_interface (message))) {
-		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	priv = TRACKER_EXTRACT_GET_PRIVATE (extract);
+
+	if (priv->own_id != 0) {
+		g_bus_unown_name (priv->own_id);
 	}
 
-	/* Only check if the user_data is our TrackerExtract AFTER having checked that
-	 * the message matches expected path and interface. */
-	extract = user_data;
-	g_return_val_if_fail (TRACKER_IS_EXTRACT (extract), DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+	if (priv->registration_id != 0) {
+		g_dbus_connection_unregister_object (priv->d_connection,
+		                                     priv->registration_id);
+	}
 
-	if (!g_strcmp0 ("GetMetadataFast", dbus_message_get_member (message))) {
-		get_metadata_fast (extract, connection, message);
-		return DBUS_HANDLER_RESULT_HANDLED;
+	if (priv->introspection_data) {
+		g_dbus_node_info_unref (priv->introspection_data);
 	}
 
-	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	if (priv->d_connection) {
+		g_object_unref (priv->d_connection);
+	}
 }
diff --git a/src/tracker-extract/tracker-extract.h b/src/tracker-extract/tracker-extract.h
index 3d185df..dbe7256 100644
--- a/src/tracker-extract/tracker-extract.h
+++ b/src/tracker-extract/tracker-extract.h
@@ -53,17 +53,9 @@ GType           tracker_extract_get_type                (void);
 TrackerExtract *tracker_extract_new                     (gboolean                disable_shutdown,
                                                          gboolean                force_internal_extractors,
                                                          const gchar            *force_module);
-void            tracker_extract_get_pid                 (TrackerExtract         *object,
-                                                         DBusGMethodInvocation  *context,
-                                                         GError                **error);
-void            tracker_extract_get_metadata            (TrackerExtract         *object,
-                                                         const gchar            *uri,
-                                                         const gchar            *mime,
-                                                         DBusGMethodInvocation  *context,
-                                                         GError                **error);
-DBusHandlerResult tracker_extract_connection_filter     (DBusConnection         *connection,
-                                                         DBusMessage            *message,
-                                                         void                   *user_data);
+
+void            tracker_extract_dbus_start              (TrackerExtract         *extract);
+void            tracker_extract_dbus_stop               (TrackerExtract         *extract);
 
 /* Not DBus API */
 void            tracker_extract_get_metadata_by_cmdline (TrackerExtract         *object,
diff --git a/src/tracker-extract/tracker-main.c b/src/tracker-extract/tracker-main.c
index c29981b..0f9bcaa 100644
--- a/src/tracker-extract/tracker-main.c
+++ b/src/tracker-extract/tracker-main.c
@@ -50,7 +50,6 @@
 #include "tracker-albumart.h"
 #include "tracker-config.h"
 #include "tracker-main.h"
-#include "tracker-dbus.h"
 #include "tracker-extract.h"
 
 #define ABOUT	  \
@@ -411,13 +410,6 @@ main (int argc, char *argv[])
 	/* This makes sure we don't steal all the system's resources */
 	initialize_priority ();
 
-	if (!tracker_dbus_init ()) {
-		g_object_unref (config);
-		tracker_log_shutdown ();
-
-		return EXIT_FAILURE;
-	}
-
 	object = tracker_extract_new (disable_shutdown,
 	                              force_internal_extractors,
 	                              force_module);
@@ -425,23 +417,12 @@ main (int argc, char *argv[])
 	if (!object) {
 		g_object_unref (config);
 		tracker_log_shutdown ();
-
 		return EXIT_FAILURE;
 	}
 
 	tracker_memory_setrlimits ();
 
-	/* Make Tracker available for introspection */
-	if (!tracker_dbus_register_objects (object)) {
-		g_object_unref (object);
-		g_object_unref (config);
-		tracker_log_shutdown ();
-
-		return EXIT_FAILURE;
-	}
-
-	tracker_dbus_connection_add_filter (tracker_extract_connection_filter,
-	                                    object);
+	tracker_extract_dbus_start (object);
 
 	g_message ("Waiting for D-Bus requests...");
 
@@ -460,7 +441,10 @@ main (int argc, char *argv[])
 
 	/* Shutdown subsystems */
 	tracker_albumart_shutdown ();
-	tracker_dbus_shutdown ();
+
+	tracker_extract_dbus_stop (object);
+	g_object_unref (object);
+
 	tracker_log_shutdown ();
 
 	g_object_unref (config);



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