[tracker/tracker-0.8] Fixes GB#619061, add history and auto-completion to t-s-t



commit 88ed614d33e2130a11a66ca4dec1b9c4bb19ff01
Author: Tshepang Lekhonkhobe <tshepang gmail com>
Date:   Thu May 20 00:50:03 2010 +0200

    Fixes GB#619061, add history and auto-completion to t-s-t

 src/tracker-search-tool/tracker-entry.gs         |  111 +++++++++++++++-------
 src/tracker-search-tool/tracker-metadata-tile.gs |    2 +-
 src/tracker-search-tool/tracker-query.gs         |    7 +-
 src/tracker-search-tool/tracker-search-tool.gs   |   21 ++--
 4 files changed, 90 insertions(+), 51 deletions(-)
---
diff --git a/src/tracker-search-tool/tracker-entry.gs b/src/tracker-search-tool/tracker-entry.gs
index 16d5fdd..a27a175 100644
--- a/src/tracker-search-tool/tracker-entry.gs
+++ b/src/tracker-search-tool/tracker-entry.gs
@@ -23,51 +23,92 @@
 uses
     Gtk
     TrackerUtils
-    
-    
+
+/* wait 0.5s after each keystroke before performing a search */
 const static RUN_DELAY : int = 500
-    
-class TrackerSearchEntry  : Gtk.Entry implements Gtk.Activatable
+
+class TrackerSearchEntry  : ComboBoxEntry implements Gtk.Activatable
     id_invoker : uint = 0
+    entry : Entry
+    history : list of string
 
     prop Query : TrackerQuery
 
     init
-        set_icon_from_stock (EntryIconPosition.SECONDARY, STOCK_CLEAR)
-        set_icon_sensitive (EntryIconPosition.PRIMARY, false)
-        set_icon_sensitive (EntryIconPosition.SECONDARY, false)
-        set_icon_tooltip_text (EntryIconPosition.SECONDARY, _("Clear the search text"))
-        activate += entry_activate
-        changed += entry_changed
-        icon_press += def (p0, p1)
+        entry = get_child() as Entry
+        entry.set_icon_from_stock (EntryIconPosition.SECONDARY, STOCK_CLEAR)
+        entry.set_icon_sensitive (EntryIconPosition.SECONDARY, true)
+        entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
+                                     _("Clear the search text"))
+
+        var model = new ListStore (1, typeof (string))
+        set_model(model)
+        set_text_column(0)
+
+        var completion = new EntryCompletion ()
+        completion.set_model(model)
+        completion.set_text_column(0)
+        entry.set_completion(completion)
+
+        entry.activate += entry_activate
+        entry.changed += entry_changed
+        entry.icon_press += def (p0)
             if p0 is EntryIconPosition.SECONDARY
-                text = "" 
-        
-        
-    def private entry_changed (editable : Editable) 
-        if Query is not null
-            if text is null
-                Query.SearchTerms = ""
-                if id_invoker != 0
-                    Source.remove (id_invoker)
-                    id_invoker = 0
-                set_icon_sensitive (EntryIconPosition.SECONDARY, false)
-            else
-                if id_invoker != 0
-                    Source.remove (id_invoker)
-                id_invoker = Timeout.add (RUN_DELAY, run_query)
-
-    def private entry_activate (entry : TrackerSearchEntry)
+                entry.text = ""
+
+        history = new list of string
+
+    def private entry_changed ()
+        if entry.text is null
+            Query.SearchTerms = ""
+            if id_invoker != 0
+                Source.remove (id_invoker)
+                id_invoker = 0
+        else
+            if id_invoker != 0
+                Source.remove (id_invoker)
+            id_invoker = Timeout.add (RUN_DELAY, run_query)
+
+    def private entry_activate ()
         entry.grab_focus ()
 
     def private run_query () : bool
-        if Query is not null
-            if (text is null) or (text is "")
-                set_icon_sensitive (EntryIconPosition.SECONDARY, false)
-                Query.SearchTerms = ""
-            else
-                set_icon_sensitive (EntryIconPosition.SECONDARY, true)
-                Query.SearchTerms = EscapeSparql (text, true)
+        var txt = entry.text
+        if (txt is null) or (txt is "")
+            Query.SearchTerms = ""
+            return false
+
+        /* remove leading whitespace */
+        txt = txt.chug()
+
+        var len = txt.len()
+        if len > 2
+
+            /* remove leading non-alphanumeric chars */
+            while(!txt[0].isalnum())
+                txt = txt.slice(1, len--)
+                if len < 3
+                    return false
+
+            /* remove trailing non-alphanumeric chars */
+            if(!txt[len - 1].isalnum())
+                while(!txt[len - 2].isalnum())
+                    txt = txt.slice(0, len - 2)
+                    len--
+                    if len < 3
+                        return false
+
+            /* hit tracker-store */
+            Query.SearchTerms = EscapeSparql (txt, true)
+
+            /* do not store duplicate strings in history */
+            for item in history
+                if txt == item
+                    return false
+
+            history.add(txt)
+            prepend_text(txt)
+
         return false
 
     def sync_action_properties (action : Action)
diff --git a/src/tracker-search-tool/tracker-metadata-tile.gs b/src/tracker-search-tool/tracker-metadata-tile.gs
index fdae19f..838e2a3 100644
--- a/src/tracker-search-tool/tracker-metadata-tile.gs
+++ b/src/tracker-search-tool/tracker-metadata-tile.gs
@@ -250,7 +250,7 @@ class TrackerMetadataTile : EventBox
 
 
     def private GetCategory (uri : string) : Categories
-        var query = "SELECT rdf:type(?s) where { ?s nie:url \"%s\" }".printf(uri)
+        var query = "SELECT rdf:type(?s) WHERE { ?s nie:url \"%s\" }".printf(uri)
         var results = Query.Query (query)
         var res = ""
 
diff --git a/src/tracker-search-tool/tracker-query.gs b/src/tracker-search-tool/tracker-query.gs
index 1c60d0c..38bb73d 100644
--- a/src/tracker-search-tool/tracker-query.gs
+++ b/src/tracker-search-tool/tracker-query.gs
@@ -28,7 +28,7 @@ interface Resources : GLib.Object
 
 class TrackerQuery : Object
 
-    tracker : Resources;
+    tracker : Resources
     _SearchTerms : string
 
     event SearchSettingsChanged ()
@@ -49,7 +49,7 @@ class TrackerQuery : Object
     init
         Category = "All"
 
-         self.notify += def (t, propety)
+        notify += def (t, propety)
             if propety.name is "Category" or  propety.name is "SortField" or propety.name is "Fields"
                 SearchSettingsChanged ()
             else
@@ -59,7 +59,6 @@ class TrackerQuery : Object
                     else
                         SearchSettingsChanged ()
 
-
     def Connect () : bool
 
         try
@@ -81,7 +80,7 @@ class TrackerQuery : Object
         else
             cat = Category
 
-        query = "SELECT ?s nie:url(?s) nie:mimeType(?s) WHERE { ?s fts:match \"%s\". ?s a %s } limit 100 ".printf (SearchTerms, cat)
+        query = "SELECT ?s nie:url(?s) nie:mimeType(?s) WHERE { ?s fts:match \"%s\". ?s a %s } LIMIT 100 ".printf (SearchTerms, cat)
 
         // to do : add Fields, Category and SortField
         try
diff --git a/src/tracker-search-tool/tracker-search-tool.gs b/src/tracker-search-tool/tracker-search-tool.gs
index 2a81e3d..eb6e545 100644
--- a/src/tracker-search-tool/tracker-search-tool.gs
+++ b/src/tracker-search-tool/tracker-search-tool.gs
@@ -104,26 +104,23 @@ init
 
         try
             builder.add_from_file (UIDIR + "tst.ui")
-
         except e : GLib.Error
-
             var msg = new MessageDialog (null, DialogFlags.MODAL, \
                                          MessageType.ERROR, ButtonsType.OK, \
                                         N_("Failed to load UI\n%s"), e.message)
             msg.run ()
             Gtk.main_quit()
 
-
     window = builder.get_object ("window") as Window
     window.destroy += Gtk.main_quit
 
-    /* create tracker widgets */
-
+    /* create widgets */
     var
-        accel_group = new AccelGroup
+        accel_group = new AccelGroup ()
+        entry = new Entry ()
 
         query = new TrackerQuery
-        entry = new TrackerSearchEntry ()
+        search_entry = new TrackerSearchEntry ()
         grid = new TrackerResultGrid ()
         categories = new TrackerCategoryView ()
         tile = new TrackerMetadataTile ()
@@ -135,16 +132,18 @@ init
         search_label = builder.get_object ("SearchLabel") as Label
 
     window.add_accel_group (accel_group)
-    search_label.set_mnemonic_widget (entry)
+    search_label.set_mnemonic_widget (search_entry)
 
     query.Connect ()
-    entry.Query = query
-    entry_box.add (entry)
+    search_entry.Query = query
+    entry_box.add (search_entry)
 
     keyval : uint
     mods : Gdk.ModifierType
     accelerator_parse ("<Ctrl>s", out keyval, out mods)
-    entry.add_accelerator ("activate", accel_group, keyval, mods, AccelFlags.VISIBLE | AccelFlags.LOCKED)
+    entry = search_entry.get_child () as Entry
+    entry.add_accelerator ("activate", accel_group, keyval, mods,
+                            AccelFlags.VISIBLE | AccelFlags.LOCKED)
 
     grid.Query = query
     grid_box.add (grid)



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