-/* qsort callback for ordering choosenodes */
-static int compare_choosenode(const void *av, const void *bv) {
- const struct choosenode *const *aa = av, *const *bb = bv;
- const struct choosenode *a = *aa, *b = *bb;
-
- return compare_tracks(a->sort, b->sort,
- a->display, b->display,
- a->path, b->path);
-}
-
-/* Called when an expandable node is updated. */
-static void updated_node(struct choosenode *cn, int redisplay) {
- D(("updated_node %s", cn->path));
- assert(cn->flags & CN_EXPANDABLE);
- /* It might be that the node has been de-expanded since we requested the
- * update. In that case we ignore this notification. */
- if(!(cn->flags & CN_EXPANDED)) return;
- /* Sort children */
- qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
- compare_choosenode);
- if(redisplay)
- redisplay_tree();
-}
-
-/* Searching --------------------------------------------------------------- */
-
-static int compare_track_for_qsort(const void *a, const void *b) {
- return compare_path(*(char **)a, *(char **)b);
-}
-
-/* Return true iff FILE is a child of DIR */
-static int is_child(const char *dir, const char *file) {
- const size_t dlen = strlen(dir);
-
- return (!strncmp(file, dir, dlen)
- && file[dlen] == '/'
- && strchr(file + dlen + 1, '/') == 0);
-}
-
-/* Return true iff FILE is a descendant of DIR */
-static int is_descendant(const char *dir, const char *file) {
- const size_t dlen = strlen(dir);
-
- return !strncmp(file, dir, dlen) && file[dlen] == '/';
-}
-
-/* Called to fill a node in the search results tree */
-static void fill_search_node(struct choosenode *cn) {
- int n;
- const size_t plen = strlen(cn->path);
- const char *s;
- char *dir, *last = 0;
-
- D(("fill_search_node %s", cn->path));
- /* We depend on the search results being sorted as by compare_path(). */
- clear_children(cn);
- for(n = 0; n < nsearchresults; ++n) {
- /* We only care about descendants of CN */
- if(!is_descendant(cn->path, searchresults[n]))
- continue;
- s = strchr(searchresults[n] + plen + 1, '/');
- if(s) {
- /* We've identified a subdirectory of CN. */
- dir = xstrndup(searchresults[n], s - searchresults[n]);
- if(!last || strcmp(dir, last)) {
- /* Not a duplicate */
- last = dir;
- newnode(cn, dir,
- trackname_transform("dir", dir, "display"),
- trackname_transform("dir", dir, "sort"),
- CN_EXPANDABLE, fill_search_node);
- }
- } else {
- /* We've identified a file in CN */
- newnode(cn, searchresults[n],
- trackname_transform("track", searchresults[n], "display"),
- trackname_transform("track", searchresults[n], "sort"),
- 0/*flags*/, 0/*fill*/);
- }
+/** @brief Remove node @p it and all its children
+ * @param it Iterator, updated to point to next
+ * @return True if iterator remains valid
+ *
+ * TODO is this necessary? gtk_tree_store_remove() does not document what
+ * happens to children.
+ */
+static gboolean choose_remove_node(GtkTreeIter *it) {
+ GtkTreeIter child[1];
+ gboolean childv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
+ child,
+ it);
+ while(childv)
+ childv = choose_remove_node(child);
+ return gtk_tree_store_remove(choose_store, it);
+}
+
+/** @brief Update length and state fields */
+static gboolean choose_set_state_callback(GtkTreeModel attribute((unused)) *model,
+ GtkTreePath attribute((unused)) *path,
+ GtkTreeIter *it,
+ gpointer attribute((unused)) data) {
+ if(choose_is_file(it)) {
+ const char *track = choose_get_track(it);
+ const long l = namepart_length(track);
+ char length[64];
+ if(l > 0)
+ byte_snprintf(length, sizeof length, "%ld:%02ld", l / 60, l % 60);
+ else
+ length[0] = 0;
+ gtk_tree_store_set(choose_store, it,
+ LENGTH_COLUMN, length,
+ STATE_COLUMN, queued(track),
+ -1);
+ if(choose_is_search_result(track))
+ gtk_tree_store_set(choose_store, it,
+ BG_COLUMN, SEARCH_RESULT_BG,
+ FG_COLUMN, SEARCH_RESULT_FG,
+ -1);
+ else
+ gtk_tree_store_set(choose_store, it,
+ BG_COLUMN, (char *)0,
+ FG_COLUMN, (char *)0,
+ -1);