nemo r55 - in trunk: . broker common gtk uicommon



Author: arj
Date: Tue Jan  8 17:28:06 2008
New Revision: 55
URL: http://svn.gnome.org/viewvc/nemo?rev=55&view=rev

Log:
Re-add snippet support in search results



Modified:
   trunk/NEWS
   trunk/broker/Broker.cs
   trunk/broker/Tracker.cs
   trunk/broker/Xesam.cs
   trunk/common/Common.cs
   trunk/gtk/DisplayDocumentItem.cs
   trunk/gtk/MainWindow.cs
   trunk/gtk/SearchPopup.cs
   trunk/uicommon/DocumentItem.cs

Modified: trunk/NEWS
==============================================================================
--- trunk/NEWS	(original)
+++ trunk/NEWS	Tue Jan  8 17:28:06 2008
@@ -7,6 +7,7 @@
 - Add open directory to right click menu for file
 - Add copy to clipboard to right click menu for file
 - Add pagination support in search results window
+- Re-add snippet support in search results
 
 Bugfixes:
 - Fix indexing so it doesn't kill the machine

Modified: trunk/broker/Broker.cs
==============================================================================
--- trunk/broker/Broker.cs	(original)
+++ trunk/broker/Broker.cs	Tue Jan  8 17:28:06 2008
@@ -174,17 +174,9 @@
 		    meta.set_starred(file, starred, delegate {});	
 	    }	
 		
-	#if false
-	    public void search_snippet(string uri, string search_string, VoidFunction<string> callback)
-	    {
-			ThreadPool.QueueUserWorkItem(delegate {
-				string result = "";
-				if (search_string.Length != 0)
-					result = bus_search.GetSnippet("Files", uri, search_string);
-				callback(result);
-			});
-	    }
+	    public abstract void search_snippet(string uri, string search_string, VoidFunction<string> callback);
 
+#if false
 	    public void get_text(string uri, VoidFunction<string> callback)
 	    {
 	    	// FIXME: remember thread pool
@@ -192,6 +184,6 @@
 			//string result = bus_files.GetTextContents(uri, 0, 100);
 			callback(""); // result
 	    }
-	#endif
+#endif
 	}
 }
\ No newline at end of file

Modified: trunk/broker/Tracker.cs
==============================================================================
--- trunk/broker/Tracker.cs	(original)
+++ trunk/broker/Tracker.cs	Tue Jan  8 17:28:06 2008
@@ -145,5 +145,15 @@
 	            }
 	        });
 		}
+
+	    public override void search_snippet(string uri, string search_string, VoidFunction<string> callback)
+	    {
+			ThreadPool.QueueUserWorkItem(delegate {
+				string result = "";
+				if (search_string.Length != 0)
+					result = bus_search.GetSnippet("Files", uri, search_string);
+				callback(result);
+			});
+	    }
 	}
 }

Modified: trunk/broker/Xesam.cs
==============================================================================
--- trunk/broker/Xesam.cs	(original)
+++ trunk/broker/Xesam.cs	Tue Jan  8 17:28:06 2008
@@ -203,6 +203,42 @@
 			}
 	 	}
 
+	    public override void search_snippet(string uri, string search_string, VoidFunction<string> callback)
+		{
+       		string session_id = bus_search.NewSession();
+			bus_search.SetProperty(session_id, "hit.fields", new string[] { "snippet" });
+
+			string xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+"<request xmlns=\"http://freedesktop.org/standards/xesam/1.0/query\";>" +
+"  <query>" +
+"   <and>" +
+"    <inSet> " +
+"      <field name=\"xesam:url\"/> " +
+"      <string>" + "file://" + uri + "</string> " + 
+"    </inSet> " +
+"    <fullText> " +
+"      <string>" + search_string + "</string> " +
+"    </fullText> " +
+"   </and>" +
+"  </query> " +
+"</request> ";
+		
+			string search_id = bus_search.NewSearch(session_id, xml_string);
+
+			hits_added_callbacks.Add(search_id, new Callback(1, delegate(ref int count, UInt32 amount) {
+				object[][] result = bus_search.GetHits(search_id, amount);
+				if (result.Length > 0)
+					callback(result[0][0] as string);
+			}));
+
+			hits_done_callbacks.Add(search_id, delegate  { 
+				bus_search.CloseSession(session_id);
+				hits_added_callbacks.Remove(search_id);
+			});
+
+			bus_search.StartSearch(session_id);		
+		}
+
 		// delagates not mighty enough to support yield
 		public static IEnumerable<string> string_start()
 		{
@@ -237,6 +273,7 @@
             		session_id = bus_search.NewSession();
 					System.Console.WriteLine("just before hit fields are called");
 					bus_search.SetProperty(session_id, "hit.fields", registered_fields);
+					bus_search.SetProperty(session_id, "hit.fields.extended", new string[] { "snippet" });
 					System.Console.WriteLine("got session id {0}", session_id);
 				
 					System.Console.WriteLine("searching for text 3");

Modified: trunk/common/Common.cs
==============================================================================
--- trunk/common/Common.cs	(original)
+++ trunk/common/Common.cs	Tue Jan  8 17:28:06 2008
@@ -97,6 +97,22 @@
 			w.Close();
 		}
 
+		public static string strip_html_tags(string text)
+		{
+			string val = text;
+			int index = 0;
+			while ((index = val.IndexOf("<")) != -1)
+			{
+				int index2 = val.IndexOf(">");
+				if (index2 != -1 && index2 < index + 25 && index2 > index)
+					val = val.Remove(index, index2 - index + 1);
+				else
+					break;
+			}
+
+			return val;
+		}
+
 		public static List<List<T>> split_list<T>(List<T> l, int split_size)
 		{
 			List<List<T>> lists = new List<List<T>>();

Modified: trunk/gtk/DisplayDocumentItem.cs
==============================================================================
--- trunk/gtk/DisplayDocumentItem.cs	(original)
+++ trunk/gtk/DisplayDocumentItem.cs	Tue Jan  8 17:28:06 2008
@@ -32,7 +32,10 @@
 		
 		public void set_snippet(string snippet)
 		{
-			// FIXME: DO IT
+			snippet_wrapper = new Gtk.EventBox();
+			Gtk.Label tmp = new Gtk.Label();
+			tmp.Markup = snippet;
+			snippet_wrapper.Add(tmp);
 		}
 
 		public void set_snippet_tip(string text_snippet)

Modified: trunk/gtk/MainWindow.cs
==============================================================================
--- trunk/gtk/MainWindow.cs	(original)
+++ trunk/gtk/MainWindow.cs	Tue Jan  8 17:28:06 2008
@@ -332,7 +332,6 @@
 			items.Add(item);
 		}
 
-
 //		System.Console.WriteLine("{0} - {1} - {2} - {3}", root_x, root_y, search_input.Allocation.Width, search_input.Allocation.Height);
 
 		SearchPopup popup = new SearchPopup(calendar_driver.update_view_set_next);

Modified: trunk/gtk/SearchPopup.cs
==============================================================================
--- trunk/gtk/SearchPopup.cs	(original)
+++ trunk/gtk/SearchPopup.cs	Tue Jan  8 17:28:06 2008
@@ -260,7 +260,8 @@
 				if (doc_item != null) {
 					doc_item.on_snippet_callback = delegate (string snippet) {
 						if (snippet.Length != 0 && snippet != "") {
-							System.Console.WriteLine("got text: {0}", snippet);
+
+							System.Console.WriteLine("got snippet: {0}", snippet);
 
 							Gtk.Label snippet_label = new Gtk.Label();
 							snippet_label.SetAlignment(0, 0); // left aligned
@@ -270,7 +271,9 @@
 
 							update();
 						}
-					}; 
+					};
+					if (doc_item.snippet != "")
+						doc_item.on_snippet_callback(doc_item.snippet);
 				}
 			} catch (Exception) { // not document item
 			}

Modified: trunk/uicommon/DocumentItem.cs
==============================================================================
--- trunk/uicommon/DocumentItem.cs	(original)
+++ trunk/uicommon/DocumentItem.cs	Tue Jan  8 17:28:06 2008
@@ -61,12 +61,16 @@
 
         // set data
         
+        string search_text;
+        
 		protected void populate(string search_text)
 		{
-#if false
 			if (search_text != "") {
+				this.search_text = search_text;
 				Singleton<SingletonWrapper<Broker>>.Instance.wrapped.search_snippet(path, search_text, 
-														  Helpers.RunInMainThread<string>(on_snippet_result));
+														  							Helpers.RunInMainThread<string>(on_snippet_result));
+			}
+#if false
 			} else
 				Singleton<SingletonWrapper<Broker>>.Instance.wrapped.get_text(path, Helpers.RunInMainThread<string>(on_snippet_result));
 #endif
@@ -107,18 +111,16 @@
 		
 		protected void on_snippet_result(string text_snippet)
         {
-        	snippet = text_snippet;
-        
         	DisplayDocumentItem display_doc_item = (DisplayDocumentItem) display_item;
         
-			string formatted_text = format_snippet_text(text_snippet, 150);
-			display_doc_item.set_snippet(formatted_text);
-			
-			if (formatted_text.Length != text_snippet.Length)
+			snippet = format_snippet_text(text_snippet, 60);
+			display_doc_item.set_snippet(snippet);
+
+			if (snippet.Length != text_snippet.Length)
 				display_doc_item.set_snippet_tip(text_snippet);
 				
 			if (on_snippet_callback != null) {
-				string small_formatted_text = format_snippet_text(text_snippet, 50);
+				string small_formatted_text = format_snippet_text(text_snippet, 60);
 				if (small_formatted_text != null && small_formatted_text != "")
 					on_snippet_callback(small_formatted_text);
 			}
@@ -127,6 +129,8 @@
         // helper functions
         public string format_snippet_text(string text, int max_length)
         {
+        	text = Helpers.strip_html_tags(text);
+        
             if (text.Length < max_length)
             {
             	return text;
@@ -139,8 +143,26 @@
 
                 if (word_begin == -1 || word_end == -1 || word_end <= word_begin || word_end - word_begin > max_length)
                 {
-//                    System.Console.WriteLine("word searched for not found in snippet {0}", text);
-                    return "";
+                	int search_word_pos = text.IndexOf(search_text);
+                	
+                	int half_distance = max_length/2 - search_text.Length / 2;
+                	
+                	if (search_word_pos == -1)
+                		half_distance = max_length/2;
+                		
+                	if (search_word_pos > half_distance) {
+                		int rest_of_string = Math.Min(text.Length - search_word_pos, 2 * half_distance + search_text.Length);
+                		text = text.Substring(search_word_pos - half_distance, rest_of_string);
+                	} else {
+                		int rest_of_string = Math.Min(text.Length, 2 * half_distance + search_text.Length);
+						text = text.Substring(0, rest_of_string);
+					}
+
+                	text = text.Replace(search_text, "<b>" + search_text + "</b>");
+                	string capitalized = search_text.Substring(0,1).ToUpper() + search_text.Substring(1);
+                	text = text.Replace(capitalized, "<b>" + capitalized + "</b>");
+					
+                    return text;
                 }
 
                 word_end += "</b>".Length;



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