[tracker/writeback: 4/8] Changed from using a method to listening on the Writeback signal



commit e3be6938cad8762558ed52a3bbe3711e2319d5a5
Author: Philip Van Hoof <philip codeminded be>
Date:   Wed Nov 11 12:08:01 2009 +0100

    Changed from using a method to listening on the Writeback signal
    
    I also added a small sample that calls back in on_sparql_result_received that
    will simply print all the statements that have predicate tracker:writeback set

 data/dbus/tracker-writeback.xml                    |    4 -
 src/tracker-writeback/Makefile.am                  |   19 ++-
 src/tracker-writeback/tracker-marshal.list         |    1 +
 src/tracker-writeback/tracker-writeback-dbus.h     |    4 -
 .../tracker-writeback-dispatcher.c                 |  216 ++++++++++++++------
 5 files changed, 170 insertions(+), 74 deletions(-)
---
diff --git a/data/dbus/tracker-writeback.xml b/data/dbus/tracker-writeback.xml
index f1466f6..e67cf70 100644
--- a/data/dbus/tracker-writeback.xml
+++ b/data/dbus/tracker-writeback.xml
@@ -4,9 +4,5 @@
   <interface name="org.freedesktop.Tracker1.Writeback">
     <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="tracker_writeback_dbus"/>
 
-    <method name="UpdateMetadata">
-      <annotation name="org.freedesktop.DBus.GLib.Async" value="true"/>
-      <arg type="s" name="uri" direction="in" />
-    </method>
   </interface>
 </node>
diff --git a/src/tracker-writeback/Makefile.am b/src/tracker-writeback/Makefile.am
index 60988c9..3d13b85 100644
--- a/src/tracker-writeback/Makefile.am
+++ b/src/tracker-writeback/Makefile.am
@@ -30,6 +30,7 @@ INCLUDES = 								\
 libexec_PROGRAMS = tracker-writeback
 
 tracker_writeback_SOURCES = 						\
+	$(marshal_sources)						\
 	$(dbus_sources)							\
 	tracker-writeback-dispatcher.c					\
 	tracker-writeback-dispatcher.h					\
@@ -41,6 +42,7 @@ tracker_writeback_SOURCES = 						\
 
 tracker_writeback_LDADD = 						\
 	$(top_builddir)/src/libtracker-common/libtracker-common.la	\
+	$(top_builddir)/src/libtracker-client/libtracker-client- TRACKER_API_VERSION@.la \
 	$(DBUS_LIBS)							\
 	$(GMODULE_LIBS)							\
 	$(GTHREAD_LIBS)							\
@@ -50,10 +52,25 @@ tracker_writeback_LDADD = 						\
 dbus_sources = 								\
 	tracker-writeback-glue.h
 
+marshal_sources =							\
+	tracker-marshal.h						\
+	tracker-marshal.c
+
+tracker-marshal.h: tracker-marshal.list
+	$(AM_V_GEN)$(GLIB_GENMARSHAL) $< --prefix=tracker_marshal --header > $@
+
+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 = 							\
-	$(dbus_sources)
+	$(dbus_sources)							\
+	$(marshal_sources)
 
 CLEANFILES = $(BUILT_SOURCES)
+
+EXTRA_DIST = tracker-marshal.list
+
diff --git a/src/tracker-writeback/tracker-marshal.list b/src/tracker-writeback/tracker-marshal.list
new file mode 100644
index 0000000..374d4b7
--- /dev/null
+++ b/src/tracker-writeback/tracker-marshal.list
@@ -0,0 +1 @@
+VOID:BOXED
diff --git a/src/tracker-writeback/tracker-writeback-dbus.h b/src/tracker-writeback/tracker-writeback-dbus.h
index e22faff..734578b 100644
--- a/src/tracker-writeback/tracker-writeback-dbus.h
+++ b/src/tracker-writeback/tracker-writeback-dbus.h
@@ -29,10 +29,6 @@ G_BEGIN_DECLS
 #define TRACKER_WRITEBACK_DBUS_NAME      "org.freedesktop.Tracker1.Writeback"
 #define TRACKER_WRITEBACK_DBUS_PATH      "/org/freedesktop/Tracker1/Writeback"
 
-void tracker_writeback_dbus_update_metadata  (TrackerWritebackDispatcher  *dispatcher,
-                                              const gchar                 *uri,
-                                              DBusGMethodInvocation       *context,
-                                              GError                     **error);
 
 G_END_DECLS
 
diff --git a/src/tracker-writeback/tracker-writeback-dispatcher.c b/src/tracker-writeback/tracker-writeback-dispatcher.c
index 667fe19..d53fa26 100644
--- a/src/tracker-writeback/tracker-writeback-dispatcher.c
+++ b/src/tracker-writeback/tracker-writeback-dispatcher.c
@@ -18,26 +18,37 @@
  * Boston, MA  02110-1301, USA.
  */
 
+#include <libtracker-common/tracker-dbus.h>
+#include <libtracker-client/tracker.h>
+
 #include "tracker-writeback-dispatcher.h"
 #include "tracker-writeback-dbus.h"
 #include "tracker-writeback-glue.h"
 #include "tracker-writeback-module.h"
-#include <libtracker-common/tracker-dbus.h>
+#include "tracker-marshal.h"
 
 typedef struct {
 	DBusGConnection *connection;
-	DBusGProxy *gproxy;
+	DBusGProxy *gproxy, *tproxy;
 } DBusData;
 
 typedef struct {
-        GHashTable *modules;
-        DBusData *dbus_data;
+	GHashTable *modules;
+	DBusData *dbus_data;
+	TrackerClient *client;
 } TrackerWritebackDispatcherPrivate;
 
 #define TRACKER_WRITEBACK_DISPATCHER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRACKER_TYPE_WRITEBACK_DISPATCHER, TrackerWritebackDispatcherPrivate))
 
-static void tracker_writeback_dispatcher_finalize    (GObject *object);
-static void tracker_writeback_dispatcher_constructed (GObject *object);
+#define TRACKER_SERVICE			"org.freedesktop.Tracker1"
+#define TRACKER_RESOURCES_OBJECT	"/org/freedesktop/Tracker1/Resources"
+#define TRACKER_INTERFACE_RESOURCES	"org.freedesktop.Tracker1.Resources"
+
+static void tracker_writeback_dispatcher_finalize    (GObject                    *object);
+static void tracker_writeback_dispatcher_constructed (GObject                    *object);
+static void on_writeback_cb                          (DBusGProxy                 *proxy,
+                                                      const gchar *const         *subjects,
+                                                      TrackerWritebackDispatcher *object);
 
 
 G_DEFINE_TYPE (TrackerWritebackDispatcher, tracker_writeback_dispatcher, G_TYPE_OBJECT)
@@ -46,10 +57,10 @@ G_DEFINE_TYPE (TrackerWritebackDispatcher, tracker_writeback_dispatcher, G_TYPE_
 static void
 tracker_writeback_dispatcher_class_init (TrackerWritebackDispatcherClass *klass)
 {
-        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-        object_class->finalize = tracker_writeback_dispatcher_finalize;
-        object_class->constructed = tracker_writeback_dispatcher_constructed;
+	object_class->finalize = tracker_writeback_dispatcher_finalize;
+	object_class->constructed = tracker_writeback_dispatcher_constructed;
 
 	g_type_class_add_private (object_class, sizeof (TrackerWritebackDispatcherPrivate));
 }
@@ -64,12 +75,12 @@ dbus_register_service (DBusGProxy  *proxy,
 	g_message ("Registering D-Bus service '%s'...", name);
 
 	if (!org_freedesktop_DBus_request_name (proxy,
-						name,
-						DBUS_NAME_FLAG_DO_NOT_QUEUE,
-						&result, &error)) {
+	                                        name,
+	                                        DBUS_NAME_FLAG_DO_NOT_QUEUE,
+	                                        &result, &error)) {
 		g_critical ("Could not acquire name:'%s', %s",
-			    name,
-			    error ? error->message : "no error given");
+		            name,
+		            error ? error->message : "no error given");
 		g_error_free (error);
 
 		return FALSE;
@@ -77,8 +88,8 @@ dbus_register_service (DBusGProxy  *proxy,
 
 	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);
+		            "perhaps the application is already running?",
+		            name);
 		return FALSE;
 	}
 
@@ -87,10 +98,10 @@ dbus_register_service (DBusGProxy  *proxy,
 
 static gboolean
 dbus_register_object (GObject		    *object,
-		      DBusGConnection	    *connection,
-		      DBusGProxy	    *proxy,
-		      const DBusGObjectInfo *info,
-		      const gchar	    *path)
+                      DBusGConnection	    *connection,
+                      DBusGProxy	    *proxy,
+                      const DBusGObjectInfo *info,
+                      const gchar	    *path)
 {
 	g_message ("Registering D-Bus object...");
 	g_message ("  Path:'%s'", path);
@@ -120,98 +131,173 @@ dbus_data_create (GObject *object)
 	}
 
 	gproxy = dbus_g_proxy_new_for_name (connection,
-					    DBUS_SERVICE_DBUS,
-					    DBUS_PATH_DBUS,
-					    DBUS_INTERFACE_DBUS);
+	                                    DBUS_SERVICE_DBUS,
+	                                    DBUS_PATH_DBUS,
+	                                    DBUS_INTERFACE_DBUS);
 
 	if (!dbus_register_service (gproxy,
-                                    TRACKER_WRITEBACK_DBUS_NAME)) {
+	                            TRACKER_WRITEBACK_DBUS_NAME)) {
 		g_object_unref (gproxy);
 		return NULL;
 	}
 
 	if (!dbus_register_object (object,
-				   connection, gproxy,
-				   &dbus_glib_tracker_writeback_object_info,
-				   TRACKER_WRITEBACK_DBUS_PATH)) {
+	                           connection, gproxy,
+	                           &dbus_glib_tracker_writeback_object_info,
+	                           TRACKER_WRITEBACK_DBUS_PATH)) {
 		g_object_unref (gproxy);
 		return NULL;
 	}
 
+	dbus_g_object_register_marshaller (tracker_marshal_VOID__BOXED,
+	                                   G_TYPE_NONE,
+	                                   G_TYPE_STRV,
+	                                   G_TYPE_INVALID);
+
 	/* Now we're successfully connected and registered, create the data */
-	data = g_slice_new0 (DBusData);
+	data = g_new0 (DBusData, 1);
 	data->connection = dbus_g_connection_ref (connection);
-	data->gproxy = g_object_ref (gproxy);
+	data->gproxy = gproxy;
 
 	return data;
 }
 
 static void
-tracker_writeback_dispatcher_init (TrackerWritebackDispatcher *dispatcher)
+dbus_data_free (DBusData *data)
 {
-        TrackerWritebackDispatcherPrivate *priv;
+	dbus_g_connection_unref (data->connection);
+
+	if (data->gproxy)
+		g_object_unref (data->gproxy);
 
-        priv = TRACKER_WRITEBACK_DISPATCHER_GET_PRIVATE (dispatcher);
+	if (data->tproxy)
+		g_object_unref (data->tproxy);
 
-        priv->dbus_data = dbus_data_create (G_OBJECT (dispatcher));
-        priv->modules = g_hash_table_new_full (g_str_hash,
-                                               g_str_equal,
-                                               (GDestroyNotify) g_free,
-                                               NULL);
+	g_free (data);
+}
+
+static void
+tracker_writeback_dispatcher_init (TrackerWritebackDispatcher *dispatcher)
+{
+	TrackerWritebackDispatcherPrivate *priv;
+
+	priv = TRACKER_WRITEBACK_DISPATCHER_GET_PRIVATE (dispatcher);
+
+	priv->client = tracker_connect (TRUE, 0);
+	priv->dbus_data = dbus_data_create (G_OBJECT (dispatcher));
+	priv->modules = g_hash_table_new_full (g_str_hash,
+	                                       g_str_equal,
+	                                       (GDestroyNotify) g_free,
+	                                       NULL);
+
+	priv->dbus_data->tproxy = dbus_g_proxy_new_for_name (priv->dbus_data->connection,
+	                                                     TRACKER_SERVICE,
+	                                                     TRACKER_RESOURCES_OBJECT,
+	                                                     TRACKER_INTERFACE_RESOURCES);
+
+	dbus_g_proxy_add_signal (priv->dbus_data->tproxy, "Writeback", 
+	                         G_TYPE_STRV, 
+	                         G_TYPE_INVALID);
+
+	dbus_g_proxy_connect_signal (priv->dbus_data->tproxy, "Writeback",
+	                             G_CALLBACK (on_writeback_cb),
+	                             dispatcher,
+	                             NULL);
 }
 
 static void
 tracker_writeback_dispatcher_finalize (GObject *object)
 {
-        G_OBJECT_CLASS (tracker_writeback_dispatcher_parent_class)->finalize (object);
+	TrackerWritebackDispatcherPrivate *priv;
+
+	priv = TRACKER_WRITEBACK_DISPATCHER_GET_PRIVATE (object);
+
+	if (priv->client) {
+		tracker_disconnect (priv->client);
+	}
+
+	dbus_data_free (priv->dbus_data);
+
+	G_OBJECT_CLASS (tracker_writeback_dispatcher_parent_class)->finalize (object);
 }
 
 static void
 tracker_writeback_dispatcher_constructed (GObject *object)
 {
-        GList *modules;
-        TrackerWritebackDispatcherPrivate *priv;
+	GList *modules;
+	TrackerWritebackDispatcherPrivate *priv;
 
-        priv = TRACKER_WRITEBACK_DISPATCHER_GET_PRIVATE (object);
-        modules = tracker_writeback_modules_list ();
+	priv = TRACKER_WRITEBACK_DISPATCHER_GET_PRIVATE (object);
+	modules = tracker_writeback_modules_list ();
 
-        while (modules) {
-                TrackerWritebackModule *module;
-                const gchar *path;
+	while (modules) {
+		TrackerWritebackModule *module;
+		const gchar *path;
 
-                path = modules->data;
-                module = tracker_writeback_module_get (path);
+		path = modules->data;
+		module = tracker_writeback_module_get (path);
 
-                g_hash_table_insert (priv->modules, g_strdup (path), module);
+		g_hash_table_insert (priv->modules, g_strdup (path), module);
 
-                modules = modules->next;
-        }
+		modules = modules->next;
+	}
 }
 
 TrackerWritebackDispatcher *
 tracker_writeback_dispatcher_new ()
 {
-        return g_object_new (TRACKER_TYPE_WRITEBACK_DISPATCHER, NULL);
+	return g_object_new (TRACKER_TYPE_WRITEBACK_DISPATCHER, NULL);
+}
+
+static void
+on_sparql_result_received (GPtrArray *result, 
+                           GError    *error, 
+                           gpointer   user_data)
+{
+	gchar *subject = user_data;
+	guint n;
+
+	g_print ("<%s> ", subject);
+
+	for (n = 0; n < result->len; n++) {
+		const GStrv row = g_ptr_array_index (result, n);
+
+		if (n != 0)
+			g_print (";\n\t<%s> \"%s\"", row[0], row[1]);
+		else
+			g_print ("<%s> \"%s\"", row[0], row[1]);
+	}
+
+	g_print (" .\n");
+
+	g_free (subject);
 }
 
-void
-tracker_writeback_dbus_update_metadata (TrackerWritebackDispatcher  *dispatcher,
-                                        const gchar                 *uri,
-                                        DBusGMethodInvocation       *context,
-                                        GError                     **error)
+static void 
+on_writeback_cb (DBusGProxy                *proxy,
+                const gchar *const         *subjects,
+                TrackerWritebackDispatcher *object)
 {
-	guint request_id;
+	guint n;
+	TrackerWritebackDispatcherPrivate *priv;
+
+	priv = TRACKER_WRITEBACK_DISPATCHER_GET_PRIVATE (object);
 
-	request_id = tracker_dbus_get_next_request_id ();
+	for (n = 0; subjects[n] != NULL; n++) {
+		gchar *query;
 
-	tracker_dbus_async_return_if_fail (dispatcher != NULL, context);
-	tracker_dbus_async_return_if_fail (uri != NULL, context);
 
-	tracker_dbus_request_new (request_id, "%s for '%s'",
-				  __PRETTY_FUNCTION__,
-                                  uri);
+		query = g_strdup_printf ("SELECT ?predicate ?object { "
+		                                "<%s> ?predicate ?object . "
+		                                "?predicate tracker:writeback true "
+		                         "}", subjects[n]);
 
-	dbus_g_method_return (context);
+		tracker_resources_sparql_query_async (priv->client,
+		                                      query,
+		                                      on_sparql_result_received,
+		                                      g_strdup (subjects[n]));
 
-	tracker_dbus_request_success (request_id);
+		g_free (query);
+	}
 }
+



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