[tracker/needle: 27/34] tracker-needle: migrate to libtracker-sparql



commit dfdda79907c25318fdbc4fb82079b9509750e1e4
Author: Martyn Russell <martyn lanedo com>
Date:   Sun Aug 15 18:11:32 2010 +0100

    tracker-needle: migrate to libtracker-sparql

 src/tracker-needle/Makefile.am         |    8 ++-
 src/tracker-needle/tracker-needle.vala |  130 ++++++++++++++------------------
 src/tracker-needle/tracker-query.vala  |   88 +++++++++++++++++++++
 3 files changed, 150 insertions(+), 76 deletions(-)
---
diff --git a/src/tracker-needle/Makefile.am b/src/tracker-needle/Makefile.am
index 7be13a2..2fbb8a8 100644
--- a/src/tracker-needle/Makefile.am
+++ b/src/tracker-needle/Makefile.am
@@ -5,6 +5,7 @@ bin_PROGRAMS = tracker-needle
 tracker_needle_VALASOURCES = 					\
 	tracker-cell-renderer-text.vala				\
 	tracker-needle.vala \
+	tracker-query.vala \
 	tracker-stats.vala \
 	tracker-utils.vala
 
@@ -23,13 +24,16 @@ tracker_needle_CFLAGS = 					\
 	-DTRACKER_UI_DIR=\"$(datadir)/tracker/\"		\
 	-DLOCALEDIR=\""$(localedir)"\"				\
 	-DSRCDIR=\"$(abs_srcdir)/\"				\
-	$(TRACKER_APPS_CFLAGS)					\
-	$(TRACKER_VALA_CFLAGS)					\
+	-I$(top_srcdir)/src/libtracker-sparql \
+	-I$(top_builddir)/src/libtracker-sparql \
 	$(WARN_CFLAGS)						\
 	$(GCOV_CFLAGS)						\
+	$(TRACKER_APPS_CFLAGS)					\
+	$(TRACKER_VALA_CFLAGS)					\
 	-include config.h
 
 tracker_needle_LDADD = 						\
+	$(top_builddir)/src/libtracker-sparql/libtracker-sparql-$(TRACKER_API_VERSION).la \
 	$(TRACKER_APPS_LIBS)					\
 	$(TRACKER_VALA_LIBS)					\
 	$(GCOV_LIBS)
diff --git a/src/tracker-needle/tracker-needle.vala b/src/tracker-needle/tracker-needle.vala
index 91cfb77..f7ae539 100644
--- a/src/tracker-needle/tracker-needle.vala
+++ b/src/tracker-needle/tracker-needle.vala
@@ -18,6 +18,7 @@
 //
 
 using Gtk;
+using Tracker.Sparql;
 
 [CCode (cname = "TRACKER_UI_DIR")]
 extern static const string UIDIR;
@@ -25,14 +26,8 @@ extern static const string UIDIR;
 [CCode (cname = "SRCDIR")]
 extern static const string SRCDIR;
 
-[DBus (name = "org.freedesktop.Tracker1.Resources")]
-interface Resources : GLib.Object {
-	public abstract string[,] SparqlQuery (string query) throws DBus.Error;
-}
-
 public class TrackerNeedle {
 	private const string UI_FILE = "tracker-needle.ui";
-	private Resources tracker;
 	private Window window;
 	private ToggleToolButton view_list;
 	private ToggleToolButton view_icons;
@@ -47,37 +42,19 @@ public class TrackerNeedle {
 	private IconView iconview;
 	private uint last_search_id = 0;
 	private ListStore store;
+	private int size_small = 0;
+	private int size_big = 0;
 	static bool current_view = true;
 	static bool current_find_in = true;
 
-
 	public void show () {
-		setup_dbus ();
 		setup_ui ();
 
 		window.show ();
 	}
 
-	private void setup_dbus () {
-		try {
-			var conn = DBus.Bus.get (DBus.BusType.SESSION);
-			tracker = (Resources) conn.get_object ("org.freedesktop.Tracker1",
-			                                       "/org/freedesktop/Tracker1/Resources",
-			                                       "org.freedesktop.Tracker1.Resources");
-		} catch (DBus.Error e) {
-			var msg = new MessageDialog (null,
-			                             DialogFlags.MODAL,
-			                             MessageType.ERROR,
-			                             ButtonsType.CANCEL,
-			                             "Error connecting to D-Bus session bus, %s", 
-			                             e.message);
-			msg.run ();
-			Gtk.main_quit ();
-		}
-	}
-
 	private void setup_ui () {
-		var builder = new Builder ();
+		var builder = new Gtk.Builder ();
 
 		try {
 			//try load from source tree first.
@@ -98,6 +75,9 @@ public class TrackerNeedle {
 			}
 		}
 
+		Gtk.icon_size_lookup (Gtk.IconSize.MENU, out size_small, null);
+		Gtk.icon_size_lookup (Gtk.IconSize.DIALOG, out size_big, null);
+
 		window = builder.get_object ("window_needle") as Window;
 		window.destroy.connect (Gtk.main_quit);
 
@@ -183,52 +163,54 @@ public class TrackerNeedle {
 	}
 
 	private bool search_run () {
-		// Need to escape this string
-		string query;
-		string criteria;
+		last_search_id = 0;
 
-		criteria = search.get_text ();
+		Tracker.Query query = new Tracker.Query ();
+		Tracker.Sparql.Cursor cursor = null;
 
-		if (criteria.length < 1) {
-			last_search_id = 0;
-			return false;
-		}
+		query.limit = 100;
+		query.criteria = search.get_text ();
 
 		if (find_in_contents.active) {
-			query = @"SELECT ?u nie:url(?u) tracker:coalesce(nie:title(?u), nfo:fileName(?u), \"Unknown\") nfo:fileLastModified(?u) nfo:fileSize(?u) nie:url(?c) WHERE { ?u fts:match \"$criteria\" . ?u nfo:belongsToContainer ?c ; tracker:available true . } ORDER BY DESC(fts:rank(?u)) OFFSET 0 LIMIT 100";
+			cursor = query.perform (query.Type.ALL);
 		} else {
-			query = @"SELECT ?u nie:url(?u) tracker:coalesce(nfo:fileName(?u), \"Unknown\") nfo:fileLastModified(?u) nfo:fileSize(?u) nie:url(?c) WHERE { ?u a nfo:FileDataObject ; nfo:belongsToContainer ?c ; tracker:available true . FILTER(fn:contains(nfo:fileName(?u), \"$criteria\")) } ORDER BY DESC(nfo:fileName(?u)) OFFSET 0 LIMIT 100";
+			cursor = query.perform (query.Type.ALL_ONLY_IN_TITLES);
 		}
 
-		debug ("Query:'%s'", query);
-
-		try {
-			var result = tracker.SparqlQuery (query);
-
-			store.clear ();
-
-			var screen = window.get_screen ();
-			var theme = IconTheme.get_for_screen (screen);
+		if (cursor == null) {
+			// FIXME: Print "no results" some where
+			return false;
+		}
 
-			var size_small = 0;
-			Gtk.icon_size_lookup (Gtk.IconSize.MENU, out size_small, null);
+		store.clear ();
 
-			var size_big = 0;
-			Gtk.icon_size_lookup (Gtk.IconSize.DIALOG, out size_big, null);
+		var screen = window.get_screen ();
+		var theme = IconTheme.get_for_screen (screen);
 
-			for (int i = 0; i < result.length[0]; i++) {
-				debug ("--> %s", result[i,0]);
-				debug ("  --> %s", result[i,1]);
-				debug ("  --> %s", result[i,2]);
-				debug ("  --> %s", result[i,3]);
-				debug ("  --> %s", result[i,4]);
-				debug ("  --> %s", result[i,5]);
+		try {
+			while (cursor.next()) {
+				int i;
+
+				for (i = 0; i < cursor.n_columns; i++) {
+					if (i == 0) {
+						debug ("--> %s", cursor.get_string (i));
+					} else {
+						debug ("  --> %s", cursor.get_string (i));
+					}
+				}
+				debug ("\n");
 
 				// Get icon
-				Gdk.Pixbuf pixbuf_small = tracker_pixbuf_new_from_file (theme, result[i,1], size_small);
-				Gdk.Pixbuf pixbuf_big = tracker_pixbuf_new_from_file (theme, result[i,1], size_big);
-				string file_size = GLib.format_size_for_display (result[i,4].to_int());
-				string file_time = tracker_time_format_from_iso8601 (result[i,3]);
+				string urn = cursor.get_string (0);
+				string _file = cursor.get_string (1);
+				string title = cursor.get_string (2);
+				string _file_time = cursor.get_string (3);
+				string _file_size = cursor.get_string (4);
+				string tooltip = cursor.get_string (7);
+				Gdk.Pixbuf pixbuf_small = tracker_pixbuf_new_from_file (theme, _file, size_small);
+				Gdk.Pixbuf pixbuf_big = tracker_pixbuf_new_from_file (theme, _file, size_big);
+				string file_size = GLib.format_size_for_display (_file_size.to_int());
+				string file_time = tracker_time_format_from_iso8601 (_file_time);
 
 				// Insert into model
 				TreeIter iter;
@@ -236,22 +218,22 @@ public class TrackerNeedle {
 
 				// FIXME: should optimise this a bit more, inserting 2 images into a list eek
 				store.set (iter,
-				           0, pixbuf_small,
-				           1, pixbuf_big,
-				           2, result[i,0],
-				           3, result[i,1],
-				           4, result[i,2],
-				           5, file_time,
-				           6, file_size,
-				           7, result[i,5],
-				           -1);
+					       0, pixbuf_small,
+					       1, pixbuf_big,
+					       2, urn,
+					       3, _file,
+					       4, title,
+					       5, file_time,
+					       6, file_size,
+					       7, tooltip,
+					       -1);
 			}
-		} catch (DBus.Error e) {
-			warning ("Could not run SPARQL query: " + e.message);
+		} catch (GLib.Error e) {
+			warning ("Could not iterate query results: %s", e.message);
 			return false;
 		}
-		
-		return true;
+
+		return false;
 	}
 
 	private void view_toggled () {
diff --git a/src/tracker-needle/tracker-query.vala b/src/tracker-needle/tracker-query.vala
new file mode 100644
index 0000000..ed76b42
--- /dev/null
+++ b/src/tracker-needle/tracker-query.vala
@@ -0,0 +1,88 @@
+//
+// Copyright 2010, Martyn Russell <martyn lanedo com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+// 02110-1301, USA.
+//
+
+using Tracker.Sparql;
+
+public class Tracker.Query {
+	public enum Type {
+		ALL,
+		ALL_ONLY_IN_TITLES,
+		MUSIC
+	}
+
+	public string criteria { get; set; }
+	public uint offset { get; set; }
+	public uint limit { get; set; }
+	public string query { get; private set; }
+
+	private static Sparql.Connection connection;
+
+	public Query () {
+		try {
+			connection = Sparql.Connection.get ();
+		} catch (Sparql.Error e) {
+			warning ("Could not get Sparql connection: %s", e.message);
+		}
+	}
+
+	public Sparql.Cursor? perform (Type query_type)
+	requires (connection != null) {
+		Sparql.Cursor cursor = null;
+
+		if (criteria == null || criteria.length < 1) {
+			warning ("Criteria was NULL or an empty string, no query performed");
+			return null;
+		}
+
+		if (limit < 1) {
+			warning ("Limit was < 1, no query performed");
+			return null;
+		}
+
+		string criteria_escaped = tracker_sparql_escape_string (criteria);
+
+		switch (query_type) {
+		case Type.ALL:
+			query = @"SELECT ?u nie:url(?u) tracker:coalesce(nie:title(?u), nfo:fileName(?u), \"Unknown\") nfo:fileLastModified(?u) nfo:fileSize(?u) nie:url(?c) WHERE { ?u fts:match \"$criteria_escaped\" . ?u nfo:belongsToContainer ?c ; tracker:available true . } ORDER BY DESC(fts:rank(?u)) OFFSET 0 LIMIT 100";
+			break;
+			
+		case Type.ALL_ONLY_IN_TITLES:
+			query = @"SELECT ?u nie:url(?u) tracker:coalesce(nfo:fileName(?u), \"Unknown\") nfo:fileLastModified(?u) nfo:fileSize(?u) nie:url(?c) WHERE { ?u a nfo:FileDataObject ; nfo:belongsToContainer ?c ; tracker:available true . FILTER(fn:contains(nfo:fileName(?u), \"$criteria_escaped\")) } ORDER BY DESC(nfo:fileName(?u)) OFFSET 0 LIMIT 100";
+			break;
+
+		case Type.MUSIC:
+			break;
+
+		default:
+			assert_not_reached ();
+		}
+
+		debug ("Running query: '%s'", query);
+
+		try {
+			cursor = connection.query (query, null);
+		} catch (Sparql.Error ea) {
+			warning ("Could not run Sparql query: %s", ea.message);
+		} catch (GLib.IOError eb) {
+			warning ("Could not run Sparql query: %s", eb.message);
+		}
+
+		return cursor;
+	}
+}



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