[totem] playlist: implement type-ahead search in the playlist



commit ad170b039a517d3dff1d0bfd3236445f66834d70
Author: Tim-Philipp Müller <tim centricular net>
Date:   Tue Oct 27 15:38:23 2009 +0000

    playlist: implement type-ahead search in the playlist
    
    Implement a basic form of type-ahead search for the playlist treeview
    widget. This makes it easy to jump to an item in the playlist by name
    if the title or filename is known.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=599795

 src/totem-playlist.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)
---
diff --git a/src/totem-playlist.c b/src/totem-playlist.c
index 78eb431..78045ac 100644
--- a/src/totem-playlist.c
+++ b/src/totem-playlist.c
@@ -1304,6 +1304,51 @@ treeview_row_changed (GtkTreeView *treeview, GtkTreePath *arg1,
 	}
 }
 
+static gboolean
+search_equal_is_match (const gchar * s, const gchar * lc_key)
+{
+	gboolean match = FALSE;
+
+	if (s != NULL) {
+		gchar *lc_s;
+
+		/* maybe also normalize both strings? */
+		lc_s = g_utf8_strdown (s, -1);
+		match = (lc_s != NULL && strstr (lc_s, lc_key) != NULL);
+		g_free (lc_s);
+	}
+
+	return match;
+}
+
+static gboolean
+search_equal_func (GtkTreeModel *model, gint col, const gchar *key,
+                   GtkTreeIter *iter, gpointer userdata)
+{
+	gboolean match;
+	gchar *lc_key, *fn = NULL;
+
+	lc_key = g_utf8_strdown (key, -1);
+
+        /* type-ahead search: first check display filename / title, then URI */
+	gtk_tree_model_get (model, iter, FILENAME_COL, &fn, -1);
+	match = search_equal_is_match (fn, lc_key);
+	g_free (fn);
+
+	if (!match) {
+		gchar *uri = NULL;
+
+		gtk_tree_model_get (model, iter, URI_COL, &uri, -1);
+		fn = g_filename_from_uri (uri, NULL, NULL);
+		match = search_equal_is_match (fn, lc_key);
+		g_free (fn);
+		g_free (uri);
+	}
+
+	g_free (lc_key);
+	return !match; /* needs to return FALSE if row matches */
+}
+
 static void
 init_treeview (GtkWidget *treeview, TotemPlaylist *playlist)
 {
@@ -1340,6 +1385,10 @@ init_treeview (GtkWidget *treeview, TotemPlaylist *playlist)
 
 	playlist->priv->selection = selection;
 
+	/* make type-ahead search work in the playlist */
+	gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (treeview),
+	                                     search_equal_func, NULL, NULL);
+
 	gtk_widget_show (treeview);
 }
 



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