[tracker/libtracker-sparql-porting: 5/34] tracker-info: Use new libtracker-sparql API



commit fb806c774016613d0e11d96e3dbc8e66b3a0c846
Author: Martyn Russell <martyn lanedo com>
Date:   Mon Jul 19 15:32:28 2010 +0100

    tracker-info: Use new libtracker-sparql API

 src/tracker-utils/tracker-info.c |  183 +++++++++++++++++++-------------------
 1 files changed, 90 insertions(+), 93 deletions(-)
---
diff --git a/src/tracker-utils/tracker-info.c b/src/tracker-utils/tracker-info.c
index 646d564..7779e50 100644
--- a/src/tracker-utils/tracker-info.c
+++ b/src/tracker-utils/tracker-info.c
@@ -28,13 +28,12 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 
-#include <libtracker-client/tracker-client.h>
-#include <libtracker-common/tracker-common.h>
+#include <libtracker-sparql/tracker-sparql.h>
 
-#define ABOUT	  \
+#define ABOUT \
 	"Tracker " PACKAGE_VERSION "\n"
 
-#define LICENSE	  \
+#define LICENSE \
 	"This program is free software and comes without any warranty.\n" \
 	"It is licensed under version 2 or later of the General Public " \
 	"License which can be viewed at:\n" \
@@ -62,7 +61,7 @@ static GOptionEntry entries[] = {
 
 static gchar *
 get_shorthand (GHashTable  *prefixes,
-	       const gchar *namespace)
+               const gchar *namespace)
 {
 	gchar *hash;
 
@@ -79,33 +78,8 @@ get_shorthand (GHashTable  *prefixes,
 
 		return g_strdup_printf ("%s:%s", prefix, property);
 	}
-	
-	return g_strdup (namespace);
-}
-
-static void
-print_property_value (gpointer data,
-		      gpointer user_data)
-{
-	GHashTable *prefixes;
-	gchar **pair;
-
-	prefixes = user_data;
-	pair = data;
-
-	if (!pair[0] || !pair[1]) {
-		return;
-	}
-
-	if (G_UNLIKELY (full_namespaces)) {
-		g_print ("  '%s' = '%s'\n", pair[0], pair[1]);
-	} else {
-		gchar *shorthand;
 
-		shorthand = get_shorthand (prefixes, pair[0]);
-		g_print ("  '%s' = '%s'\n", shorthand, pair[1]);
-		g_free (shorthand);
-	}
+	return g_strdup (namespace);
 }
 
 static gboolean
@@ -127,52 +101,48 @@ has_valid_uri_scheme (const gchar *uri)
 }
 
 static GHashTable *
-get_prefixes (TrackerClient *client)
+get_prefixes (TrackerSparqlConnection *connection)
 {
-	GPtrArray *results;
+	TrackerSparqlCursor *cursor;
+	GError *error = NULL;
 	GHashTable *retval;
 	const gchar *query;
 
 	retval = g_hash_table_new_full (g_str_hash,
-					g_str_equal,
-					NULL,
-					NULL);
+	                                g_str_equal,
+	                                NULL,
+	                                NULL);
 
 	/* FIXME: Would like to get this in the same SPARQL that we
 	 * use to get the info, but doesn't seem possible at the
 	 * moment with the limited string manipulation features we
-	 * support in SPARQL. 
+	 * support in SPARQL.
 	 */
-	query = "SELECT ?prefix ?ns "
-		"WHERE {"
-		"  ?ns a tracker:Namespace ;"
-		"  tracker:prefix ?prefix "
-		"}";
+	query = "SELECT ?ns ?prefix "
+	        "WHERE {"
+	        "  ?ns a tracker:Namespace ;"
+	        "  tracker:prefix ?prefix "
+	        "}";
 
-	results = tracker_resources_sparql_query (client, query, NULL);
+	cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 
-	if (results) {
-		gint i;
+	while (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
+		const gchar *key, *value;
 
-		/* First remove all parts after and including '#' */
-		for (i = 0; i < results->len; i++) {
-			gchar **data;
-			gchar *key, *value;
-			
-			data = g_ptr_array_index (results, i);
-
-			if (!data || !data[1]) {
-				continue;
-			}
+		key = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+		value = tracker_sparql_cursor_get_string (cursor, 1, NULL);
 
-			key = g_strndup (data[1], strlen (data[1]) - 1);
-			value = g_strdup (data[0]);
-
-			g_hash_table_insert (retval, key, value);
+		if (!key || !value) {
+			continue;
 		}
 
-		g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-		g_ptr_array_free (results, TRUE);
+		g_hash_table_insert (retval,
+		                     g_strndup (key, strlen (key) - 1),
+		                     g_strdup (value));
+	}
+
+	if (cursor) {
+		g_object_unref (cursor);
 	}
 
 	return retval;
@@ -181,8 +151,9 @@ get_prefixes (TrackerClient *client)
 int
 main (int argc, char **argv)
 {
-	TrackerClient *client;
+	TrackerSparqlConnection *connection;
 	GOptionContext *context;
+	GError *error = NULL;
 	GHashTable *prefixes;
 	gchar **p;
 
@@ -224,26 +195,32 @@ main (int argc, char **argv)
 
 	g_option_context_free (context);
 
-	client = tracker_client_new (0, G_MAXINT);
+	g_type_init ();
+
+	if (!g_thread_supported ()) {
+		g_thread_init (NULL);
+	}
+
+	connection = tracker_sparql_connection_get (&error);
 
-	if (!client) {
-		g_printerr ("%s\n",
-		            _("Could not establish a D-Bus connection to Tracker"));
+	if (!connection) {
+		g_printerr ("%s: %s\n",
+		            _("Could not establish a connection to Tracker"),
+		            error ? error->message : _("No error given"));
+		g_clear_error (&error);
 		return EXIT_FAILURE;
 	}
 
-	prefixes = get_prefixes (client);
+	prefixes = get_prefixes (connection);
 
 	for (p = filenames; *p; p++) {
-		GPtrArray *results;
+		TrackerSparqlCursor *cursor;
 		GError *error = NULL;
 		gchar *uri;
 		gchar *query;
 		gchar *urn;
 
-		g_print ("%s:'%s'\n",
-		         _("Querying information for entity"),
-		         *p);
+		g_print ("%s:'%s'\n", _("Querying information for entity"), *p);
 
 		/* support both, URIs and local file paths */
 		if (has_valid_uri_scheme (*p)) {
@@ -258,7 +235,7 @@ main (int argc, char **argv)
 
 		/* First check whether there's some entity with nie:url like this */
 		query = g_strdup_printf ("SELECT ?urn WHERE { ?urn nie:url \"%s\" }", uri);
-		results = tracker_resources_sparql_query (client, query, &error);
+		cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 		g_free (query);
 
 		if (error) {
@@ -270,24 +247,29 @@ main (int argc, char **argv)
 			continue;
 		}
 
-		if (!results || results->len == 0) {
+		if (!cursor) {
 			/* No URN matches, use uri as URN */
 			urn = g_strdup (uri);
 		} else {
-			gchar **args;
+			if (!tracker_sparql_cursor_next (cursor, NULL, NULL)) {
+				g_printerr ("  %s, %s\n",
+				            _("Unable to retrieve data for URI"),
+				            _("No error given"));
 
-			args = g_ptr_array_index (results, 0);
-			urn = g_strdup (args[0]);
-
-			g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-			g_ptr_array_free (results, TRUE);
+				g_free (uri);
+				g_object_unref (cursor);
+				continue;
+			}
 
+			urn = g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL));
 			g_print ("  '%s'\n", urn);
+
+			g_object_unref (cursor);
 		}
 
 		query = g_strdup_printf ("SELECT ?predicate ?object WHERE { <%s> ?predicate ?object }", urn);
 
-		results = tracker_resources_sparql_query (client, query, &error);
+		cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
 
 		g_free (uri);
 		g_free (query);
@@ -301,31 +283,46 @@ main (int argc, char **argv)
 			continue;
 		}
 
-		if (!results) {
+		if (!cursor) {
 			g_print ("  %s\n",
 			         _("No metadata available for that URI"));
 		} else {
-			gint length;
-
-			length = results->len;
+			g_print ("%s:\n", _("Results"));
+
+			while (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
+				const gchar *key = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+				const gchar *value = tracker_sparql_cursor_get_string (cursor, 1, NULL);
+
+				if (!key || !value) {
+					continue;
+				}
+
+				/* Don't display nie:plainTextContent */
+				if (strcmp (key, "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#plainTextContent";) == 0) {
+					continue;
+				}
+
+				if (G_UNLIKELY (full_namespaces)) {
+					g_print ("  '%s' = '%s'\n", key, value);
+				} else {
+					gchar *shorthand;
+
+					shorthand = get_shorthand (prefixes, key);
+					g_print ("  '%s' = '%s'\n", shorthand, value);
+					g_free (shorthand);
+				}
+			}
 
-			g_print (g_dngettext (NULL,
-			                      "Result: %d",
-			                      "Results: %d",
-			                      length),
-			         length);
 			g_print ("\n");
 
-			g_ptr_array_foreach (results, (GFunc) print_property_value, prefixes);
-			g_ptr_array_foreach (results, (GFunc) g_strfreev, NULL);
-			g_ptr_array_free (results, TRUE);
+			g_object_unref (cursor);
 		}
 
 		g_print ("\n");
 	}
 
 	g_hash_table_unref (prefixes);
-	g_object_unref (client);
+	g_object_unref (connection);
 
 	return EXIT_SUCCESS;
 }



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