[PATCH] adding a fulltext toggle button to search bar



Hi,
Since tracker support has been added to nautilus, all searches are
fulltext by default.  But if the user is looking for a file name, the
fulltext search results can be distracting.  In the patch below I
added a toggle button with the text "Fulltext" to the right of the
search entry bar.  When it's pressed the search is a fulltext search
(using the fts:match query), and when it's not pressed a search for
the file name is used by adding ' ; nfo:fileName ?name .
FILTER(regex(?name,"search_string"))' to the sparql query.  What do
you think?  Do you want the gui to be implemented differently?  In
addition, I want to try to add more of tracker's filter functionality
(such as date filtering) to the search bar filter mechanism which
currently only supports location and mime type.  Is that a good idea?
Doing this would only mean adding items to the filter comboboxes when
tracker is enabled, so no gui change would be necessary.
Daniel Lowengrub

---
diff --git a/libnautilus-private/nautilus-query.c
b/libnautilus-private/nautilus-query.c
index 267620d..2fb22d0 100644
--- a/libnautilus-private/nautilus-query.c
+++ b/libnautilus-private/nautilus-query.c
@@ -34,6 +34,7 @@ struct NautilusQueryDetails {
 	char *text;
 	char *location_uri;
 	GList *mime_types;
+	gboolean fts_active;
 };

 static void  nautilus_query_class_init       (NautilusQueryClass *class);
@@ -88,6 +89,12 @@ nautilus_query_get_text (NautilusQuery *query)
 	return g_strdup (query->details->text);
 }

+gboolean
+nautilus_query_get_fts (NautilusQuery *query)
+{
+	return query->details->fts_active;
+}
+
 void
 nautilus_query_set_text (NautilusQuery *query, const char *text)
 {
@@ -95,6 +102,12 @@ nautilus_query_set_text (NautilusQuery *query,
const char *text)
 	query->details->text = g_strdup (text);
 }

+void
+nautilus_query_set_fts (NautilusQuery *query, gboolean fts_active)
+{
+	query->details->fts_active = fts_active;
+}
+
 char *
 nautilus_query_get_location (NautilusQuery *query)
 {
diff --git a/libnautilus-private/nautilus-query.h
b/libnautilus-private/nautilus-query.h
index 8d92c3d..d1039cc 100644
--- a/libnautilus-private/nautilus-query.h
+++ b/libnautilus-private/nautilus-query.h
@@ -52,6 +52,9 @@ NautilusQuery* nautilus_query_new      (void);
 char *         nautilus_query_get_text           (NautilusQuery *query);
 void           nautilus_query_set_text           (NautilusQuery
*query, const char *text);

+gboolean       nautilus_query_get_fts           (NautilusQuery *query);
+void           nautilus_query_set_fts           (NautilusQuery
*query, gboolean fts_active);
+
 char *         nautilus_query_get_location       (NautilusQuery *query);
 void           nautilus_query_set_location       (NautilusQuery
*query, const char *uri);

diff --git a/libnautilus-private/nautilus-search-engine-tracker.c
b/libnautilus-private/nautilus-search-engine-tracker.c
index 775de56..20c4c15 100644
--- a/libnautilus-private/nautilus-search-engine-tracker.c
+++ b/libnautilus-private/nautilus-search-engine-tracker.c
@@ -296,6 +296,7 @@ nautilus_search_engine_tracker_start
(NautilusSearchEngine *engine)
 	char 	**mimes;
 	int 	i, mime_count;
 	GString *sparql;
+	gboolean fts_active;

 	tracker = NAUTILUS_SEARCH_ENGINE_TRACKER (engine);

@@ -309,6 +310,7 @@ nautilus_search_engine_tracker_start
(NautilusSearchEngine *engine)
 	}

 	search_text = nautilus_query_get_text (tracker->details->query);
+	fts_active = nautilus_query_get_fts (tracker->details->query);

 	mimetypes = nautilus_query_get_mime_types (tracker->details->query);

@@ -330,11 +332,17 @@ nautilus_search_engine_tracker_start
(NautilusSearchEngine *engine)

 	if (tracker->details->version == TRACKER_0_8) {
 		/* new tracker 0.8 API */
-		sparql = g_string_new ("SELECT ?url WHERE { ?file a
nfo:FileDataObject ; nie:url ?url; ");
+		sparql = g_string_new ("SELECT ?url WHERE { ?file a
nfo:FileDataObject ; nie:url ?url ");
 		if (mime_count > 0)
-			g_string_append (sparql, "nie:mimeType ?mime ; ");
-		g_string_append (sparql, "fts:match ");
-		sparql_append_string_literal (sparql, search_text);
+			g_string_append (sparql, "; nie:mimeType ?mime ");
+		if (fts_active) {
+			g_string_append (sparql, "; fts:match ");
+			sparql_append_string_literal (sparql, search_text);
+		} else {
+			g_string_append (sparql, "; nfo:fileName ?name . FILTER (regex(?name,");
+			sparql_append_string_literal (sparql, search_text);
+			g_string_append (sparql, "))");
+		}

 		if (location || mime_count > 0) {
 			g_string_append (sparql, " . FILTER (");
@@ -361,7 +369,6 @@ nautilus_search_engine_tracker_start
(NautilusSearchEngine *engine)
 			g_string_append (sparql, ")");
 		}
 		g_string_append (sparql, " }");
-
 		tracker_resources_sparql_query_async (tracker->details->client,
 						      sparql->str,
 						      (TrackerReplyGPtrArray) search_callback,
diff --git a/src/nautilus-navigation-window-pane.c
b/src/nautilus-navigation-window-pane.c
index 65359cf..e7f781f 100644
--- a/src/nautilus-navigation-window-pane.c
+++ b/src/nautilus-navigation-window-pane.c
@@ -125,7 +125,6 @@ search_bar_activate_callback (NautilusSearchBar *bar,
 	g_assert (NAUTILUS_IS_SEARCH_DIRECTORY (directory));

 	search_directory = NAUTILUS_SEARCH_DIRECTORY (directory);
-
 	query = nautilus_search_bar_get_query (NAUTILUS_SEARCH_BAR
(pane->search_bar));
 	if (query != NULL) {
 		NautilusWindowSlot *slot = NAUTILUS_WINDOW_PANE (pane)->active_slot;
diff --git a/src/nautilus-navigation-window-slot.c
b/src/nautilus-navigation-window-slot.c
index 2d0e907..74cde52 100644
--- a/src/nautilus-navigation-window-slot.c
+++ b/src/nautilus-navigation-window-slot.c
@@ -107,7 +107,6 @@ query_editor_changed_callback (NautilusSearchBar *bar,
 	NautilusDirectory *directory;

 	g_assert (NAUTILUS_IS_FILE (slot->viewed_file));
-
 	directory = nautilus_directory_get_for_file (slot->viewed_file);
 	g_assert (NAUTILUS_IS_SEARCH_DIRECTORY (directory));

diff --git a/src/nautilus-query-editor.c b/src/nautilus-query-editor.c
index 3ab8697..24d0f02 100644
--- a/src/nautilus-query-editor.c
+++ b/src/nautilus-query-editor.c
@@ -1156,12 +1156,14 @@ nautilus_query_editor_get_query
(NautilusQueryEditor *editor)
 	NautilusQuery *query;
 	GList *l;
 	NautilusQueryEditorRow *row;
+	gboolean fts_active;

 	if (editor == NULL || editor->details == NULL ||
editor->details->entry == NULL) {
 		return NULL;
 	}

 	query_text = gtk_entry_get_text (GTK_ENTRY (editor->details->entry));
+	fts_active = nautilus_search_bar_get_fts (editor->details->bar);

 	/* Empty string is a NULL query */
 	if (query_text && query_text[0] == '\0') {
@@ -1170,6 +1172,7 @@ nautilus_query_editor_get_query
(NautilusQueryEditor *editor)
 	
 	query = nautilus_query_new ();
 	nautilus_query_set_text (query, query_text);
+	nautilus_query_set_fts (query, fts_active);

 	for (l = editor->details->rows; l != NULL; l = l->next) {
 		row = l->data;
diff --git a/src/nautilus-search-bar.c b/src/nautilus-search-bar.c
index b8d5240..eea9d2b 100644
--- a/src/nautilus-search-bar.c
+++ b/src/nautilus-search-bar.c
@@ -32,6 +32,7 @@
 struct NautilusSearchBarDetails {
 	GtkWidget *entry;
 	gboolean entry_borrowed;
+	gboolean fts_active;
 };

 enum {
@@ -144,12 +145,22 @@ focus_in_event_callback (GtkWidget *widget,
 	return FALSE;
 }

+static void fts_toggle_cb (GtkWidget *fts_button, NautilusSearchBar *bar)
+{
+	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fts_button))) {
+		bar->details->fts_active = TRUE;
+	} else {
+		bar->details->fts_active = FALSE;
+	}
+}
+		
 static void
 nautilus_search_bar_init (NautilusSearchBar *bar)
 {
 	GtkWidget *alignment;
 	GtkWidget *hbox;
 	GtkWidget *label;
+	GtkWidget *fts_button;

 	bar->details = g_new0 (NautilusSearchBarDetails, 1);

@@ -177,14 +188,20 @@ nautilus_search_bar_init (NautilusSearchBar *bar)
 				       GTK_STOCK_FIND);
 	gtk_box_pack_start (GTK_BOX (hbox), bar->details->entry, TRUE, TRUE, 0);

+	fts_button = gtk_toggle_button_new_with_label ("Fulltext");
+	gtk_box_pack_start (GTK_BOX (hbox), fts_button, FALSE, TRUE, 0);
+
 	g_signal_connect (bar->details->entry, "activate",
 			  G_CALLBACK (entry_activate_cb), bar);
 	g_signal_connect (bar->details->entry, "icon-release",
 			  G_CALLBACK (entry_icon_release_cb), bar);
 	g_signal_connect (bar->details->entry, "focus-in-event",
 			  G_CALLBACK (focus_in_event_callback), bar);
+	g_signal_connect (G_OBJECT (fts_button), "toggled",
+			  G_CALLBACK (fts_toggle_cb), bar);

 	gtk_widget_show (bar->details->entry);
+	gtk_widget_show (fts_button);
 }

 GtkWidget *
@@ -235,10 +252,15 @@ nautilus_search_bar_get_query (NautilusSearchBar *bar)
 	
 	query = nautilus_query_new ();
 	nautilus_query_set_text (query, query_text);
-
+	nautilus_query_set_fts (query, bar->details->fts_active);
 	return query;
 }

+gboolean nautilus_search_bar_get_fts (NautilusSearchBar *bar)
+{
+	return bar->details->fts_active;
+}
+
 void
 nautilus_search_bar_grab_focus (NautilusSearchBar *bar)
 {
diff --git a/src/nautilus-search-bar.h b/src/nautilus-search-bar.h
index 0bc7c9a..1c6aac6 100644
--- a/src/nautilus-search-bar.h
+++ b/src/nautilus-search-bar.h
@@ -61,6 +61,7 @@ GtkWidget *    nautilus_search_bar_borrow_entry
(NautilusSearchBar *bar);
 void           nautilus_search_bar_return_entry  (NautilusSearchBar *bar);
 void           nautilus_search_bar_grab_focus    (NautilusSearchBar *bar);
 NautilusQuery *nautilus_search_bar_get_query     (NautilusSearchBar *bar);
+gboolean       nautilus_search_bar_get_fts       (NautilusSearchBar *bar);
 void           nautilus_search_bar_clear         (NautilusSearchBar *bar);

 #endif /* NAUTILUS_SEARCH_BAR_H */


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