[tracker/tracker-needle-improved-tagging] tracker-needle: Initial useful appliance of the tags list



commit 468437da62a95164a01397269969aad866b61ca1
Author: Martyn Russell <martyn lanedo com>
Date:   Fri Jun 24 13:12:29 2011 +0100

    tracker-needle: Initial useful appliance of the tags list
    
    This really allows the user to filter the current results futher by clikcing on a tag.
    This is not what I want to end up with, but it's a start. What we want is:
    
    - Search bar = fts:match || nao:prefLabel
    - Tags list = nao:prefLabel
    
    Also, we want a way to add/delete tags and items from those, which can come later.

 src/tracker-needle/tracker-needle.vala       |   20 +++++++++
 src/tracker-needle/tracker-query.vala        |   56 +++++++++++++++++++++----
 src/tracker-needle/tracker-result-store.vala |    4 ++
 src/tracker-needle/tracker-taglist.vala      |   38 ++++++++++++++----
 4 files changed, 101 insertions(+), 17 deletions(-)
---
diff --git a/src/tracker-needle/tracker-needle.vala b/src/tracker-needle/tracker-needle.vala
index 1eeb88e..f15c0de 100644
--- a/src/tracker-needle/tracker-needle.vala
+++ b/src/tracker-needle/tracker-needle.vala
@@ -315,6 +315,7 @@ public class Tracker.Needle {
 		taglist = new Tracker.TagList ();
 		taglist.hide ();
 		view.pack_end (taglist, false, true, 0);
+		taglist.selection_changed.connect (taglist_selection_changed);
 
 		view_categories.set_active (true);
 	}
@@ -329,6 +330,15 @@ public class Tracker.Needle {
 		return false;
 	}
 
+	private void taglist_selection_changed (GenericArray<string> new_tags) {
+		if (new_tags != null && new_tags.length > 0)
+			debug ("Tags selected changed, first:'%s', ...", new_tags[0]);
+		else
+			debug ("Tags selected changed, none selected");
+
+		search_run ();
+	}
+
 	private void search_changed (Editable editable) {
 		if (last_search_id != 0) {
 			Source.remove (last_search_id);
@@ -457,6 +467,13 @@ public class Tracker.Needle {
 		}
 
 		if (store != null) {
+			// Set tags first
+			if (show_tags.active) {
+				store.search_tags = taglist.tags;
+			} else {
+				store.search_tags = null;
+			}
+
 			store.search_term = search.get_text ();
 		}
 
@@ -600,6 +617,9 @@ public class Tracker.Needle {
 			debug ("Hiding tags");
 			taglist.hide ();
 		}
+
+		// Re-run search to filter with or without tags
+		search_run ();
 	}
 
 	private void show_stats_clicked () {
diff --git a/src/tracker-needle/tracker-query.vala b/src/tracker-needle/tracker-query.vala
index 892303d..a09cea4 100644
--- a/src/tracker-needle/tracker-query.vala
+++ b/src/tracker-needle/tracker-query.vala
@@ -73,6 +73,7 @@ public class Tracker.Query {
 		  %s .
 		  ?urn nfo:belongsToContainer ?parent ;
 		  tracker:available true .
+                  %s
 		}",
 
 		// CONTACTS
@@ -81,7 +82,8 @@ public class Tracker.Query {
 		// APPLICATIONS
 		"WHERE {
 		  ?urn a nfo:Software .
-		  %s
+		  %s .
+                  %s
 		}",
 
 		// MUSIC
@@ -98,21 +100,24 @@ public class Tracker.Query {
 		  %s .
 		  ?urn nmm:performer [ nmm:artistName ?performer ] ;
 		       nmm:musicAlbum [ nie:title ?album ] ;
-		       nie:url ?tooltip
+		       nie:url ?tooltip .
+                  %s
 		}",
 
 		// IMAGES
 		"WHERE {
 		  ?urn a nfo:Image ;
 		         nie:url ?tooltip .
-		  %s
+		  %s .
+                  %s
 		}",
 
 		// VIDEOS
 		"WHERE {
 		  ?urn a nfo:Video ;
 		         nie:url ?tooltip .
-		  %s
+		  %s .
+                  %s
 		}",
 
 		// DOCUMENTS
@@ -126,6 +131,7 @@ public class Tracker.Query {
 		  OPTIONAL {
 		    ?urn nco:publisher ?publisher .
 		  }
+                  %s
 		}",
 
 		// MAIL
@@ -133,7 +139,8 @@ public class Tracker.Query {
 		  ?urn a nmo:Email ;
 		         nmo:from ?sender ;
 		         nmo:to ?to .
-		  %s
+		  %s .
+                  %s
 		}",
 
 		// CALENDAR
@@ -147,13 +154,15 @@ public class Tracker.Query {
 		  OPTIONAL {
 		    ?urn nfo:belongsToContainer ?parent .
 		  }
+                  %s
 		}",
 
 		// BOOKMARKS
 		"WHERE {
 		  ?urn a nfo:Bookmark ;
-               nfo:bookmarks ?bookmark .
-		  %s
+                       nfo:bookmarks ?bookmark .
+		  %s .
+                  %s
 		}"
 	};
 
@@ -162,6 +171,8 @@ public class Tracker.Query {
 	public uint limit { get; set; }
 	public string query { get; private set; }
 
+	public GenericArray<string> tags { get; set; }
+
 	private static Sparql.Connection connection;
 
 	public Query () {
@@ -170,6 +181,31 @@ public class Tracker.Query {
 		} catch (GLib.Error e) {
 			warning ("Could not get Sparql connection: %s", e.message);
 		}
+
+		tags = null;
+	}
+
+	private string get_match_tags () {
+		string match_tags = "";
+
+		if (tags != null && tags.length > 0) {
+			string filter = "";
+
+			for (int i = 0; i < tags.length; i++) {
+				string escaped = Tracker.Sparql.escape_string (tags[i]);
+
+				if (filter.length > 1)
+					filter += ", ";
+
+				filter += "\"%s\"".printf (escaped);
+			}
+
+			match_tags += "?urn nao:hasTag ?tag .
+                                       ?tag nao:prefLabel ?label .
+                                       FILTER (?label IN (%s))".printf (filter);
+		}
+
+		return match_tags;
 	}
 
 	private bool check_query_and_match_type (Type query_type, Match match_type) {
@@ -209,8 +245,9 @@ public class Tracker.Query {
 
 		string criteria_escaped = Tracker.Sparql.escape_string (criteria);
 		string match = match_clauses[match_type].printf (criteria_escaped);
+		string match_tags = get_match_tags ();
 
-		query = "SELECT count(?urn) " + where_clauses[query_type].printf (match);
+		query = "SELECT count(?urn) " + where_clauses[query_type].printf (match, match_tags);
 
 		try {
 			cursor = yield connection.query_async (query, null);
@@ -242,8 +279,9 @@ public class Tracker.Query {
 
 		string criteria_escaped = Tracker.Sparql.escape_string (criteria);
 		string match = match_clauses[match_type].printf (criteria_escaped);
+		string match_tags = get_match_tags ();
 
-		query = "SELECT " + string.joinv (" ", args) + " " + where_clauses[query_type].printf (match);
+		query = "SELECT " + string.joinv (" ", args) + " " + where_clauses[query_type].printf (match, match_tags);
 		query += @" OFFSET $offset LIMIT $limit";
 
 		debug ("Running query: '%s'", query);
diff --git a/src/tracker-needle/tracker-result-store.vala b/src/tracker-needle/tracker-result-store.vala
index 4c1f15a..b677832 100644
--- a/src/tracker-needle/tracker-result-store.vala
+++ b/src/tracker-needle/tracker-result-store.vala
@@ -87,6 +87,7 @@ public class Tracker.ResultStore : Gtk.TreeModel, GLib.Object {
 
 			query = new Tracker.Query ();
 			query.criteria = _search_term;
+			query.tags = search_tags;
 			query.limit = 100;
 			query.offset = op.offset;
 
@@ -202,6 +203,7 @@ public class Tracker.ResultStore : Gtk.TreeModel, GLib.Object {
 
 			Tracker.Query query = new Tracker.Query ();
 			query.criteria = _search_term;
+			query.tags = search_tags;
 
 			count = yield query.get_count_async (query_data.type, query_data.match, cancellable);
 			cancellable.set_error_if_cancelled ();
@@ -324,6 +326,8 @@ public class Tracker.ResultStore : Gtk.TreeModel, GLib.Object {
 		}
 	}
 
+	public GenericArray<string> search_tags { get; set; }
+
 	public bool active {
 		get;
 		private set;
diff --git a/src/tracker-needle/tracker-taglist.vala b/src/tracker-needle/tracker-taglist.vala
index 818c2a5..3b5b5b9 100644
--- a/src/tracker-needle/tracker-taglist.vala
+++ b/src/tracker-needle/tracker-taglist.vala
@@ -26,9 +26,15 @@ public class Tracker.TagList : ScrolledWindow {
 	private int offset;
 	private int limit;
 
+	public GenericArray<string> tags { get; private set; }
+
+	public signal void selection_changed (GenericArray<string> new_tags); 
+
 	public TagList () {
 		limit = 100;
 
+		tags = new GenericArray<string> ();
+
 		// Set scrolling
 		set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
 		set_size_request (175, -1);
@@ -47,7 +53,7 @@ public class Tracker.TagList : ScrolledWindow {
 		Gtk.CellRenderer renderer;
 
 		col = new Gtk.TreeViewColumn ();
-		col.set_title (_("Tags"));
+		col.set_title (_("Filter by tags"));
 		col.set_resizable (true);
 		col.set_expand (true);
 		col.set_sizing (Gtk.TreeViewColumnSizing.AUTOSIZE);
@@ -61,7 +67,7 @@ public class Tracker.TagList : ScrolledWindow {
 
 		renderer = new CellRendererText ();
 		col.pack_start (renderer, true);
-		col.set_cell_data_func (renderer, text_renderer_func);
+		col.set_cell_data_func (renderer, model_text_renderer_func);
 
 		renderer = new CellRendererText ();
 		renderer.xpad = 5;
@@ -70,6 +76,9 @@ public class Tracker.TagList : ScrolledWindow {
 		col.add_attribute (renderer, "text", 3);
 		treeview.append_column (col);
 
+		var selection = treeview.get_selection ();
+		selection.changed.connect (model_selection_changed);
+
 		add (treeview);
 		base.show_all ();
 
@@ -81,13 +90,13 @@ public class Tracker.TagList : ScrolledWindow {
 			return;
 		}
 
-		get_tags.begin ();
+		query_get_tags.begin ();
 	}
 
-	private void text_renderer_func (CellLayout   cell_layout,
-	                                 CellRenderer cell,
-	                                 TreeModel    tree_model,
-	                                 TreeIter     iter) {
+	private void model_text_renderer_func (CellLayout   cell_layout,
+	                                       CellRenderer cell,
+	                                       TreeModel    tree_model,
+	                                       TreeIter     iter) {
 		string text, subtext;
 		string markup = null;
 
@@ -104,7 +113,20 @@ public class Tracker.TagList : ScrolledWindow {
 		cell.set ("markup", markup);
 	}
 
-	private async void get_tags () {
+	private void model_selection_foreach (TreeModel model, TreePath path, TreeIter iter) {
+		weak string tag;
+		model.get (iter, 1, out tag);
+		tags.add (tag);
+	}
+
+	private void model_selection_changed (TreeSelection selection) {
+		tags = new GenericArray<string> ();
+
+		selection.selected_foreach (model_selection_foreach);
+		selection_changed (tags);
+	}
+
+	private async void query_get_tags () {
 		string query = @"
 		               SELECT 
 		                 ?tag 



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