-/* Called with a list of directories just below some node */
-static void got_dirs(void *v, int nvec, char **vec) {
- struct callbackdata *cbd = v;
- struct choosenode *cn = cbd->u.choosenode;
- int n;
-
- D(("got_dirs %d dirs for %s", nvec, cn->path));
- for(n = 0; n < nvec; ++n)
- newnode(cn, vec[n],
- trackname_transform("dir", vec[n], "display"),
- trackname_transform("dir", vec[n], "sort"),
- CN_EXPANDABLE, fill_directory_node);
- updated_node(cn, 1);
-}
-
-/* Fill a child node */
-static void fill_directory_node(struct choosenode *cn) {
- struct callbackdata *cbd;
-
- D(("fill_directory_node %s", cn->path));
- /* TODO: caching */
- /* TODO: de-dupe against fill_letter_node */
- assert(report_label != 0);
- gtk_label_set_text(GTK_LABEL(report_label), "getting files");
- clear_children(cn);
- cbd = xmalloc(sizeof *cbd);
- cbd->u.choosenode = cn;
- disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
- cbd = xmalloc(sizeof *cbd);
- cbd->u.choosenode = cn;
- disorder_eclient_files(client, got_files, cn->path, 0, cbd);
-}
-
-/* Expand a node */
-static void expand_node(struct choosenode *cn) {
- D(("expand_node %s", cn->path));
- assert(cn->flags & CN_EXPANDABLE);
- /* If node is already expanded do nothing. */
- if(cn->flags & CN_EXPANDED) return;
- /* We mark the node as expanded and request that it fill itself. When it has
- * completed it will called updated_node() and we can redraw at that
- * point. */
- cn->flags |= CN_EXPANDED;
- /* TODO: visual feedback */
- cn->fill(cn);
-}
-
-/* Contract a node */
-static void contract_node(struct choosenode *cn) {
- D(("contract_node %s", cn->path));
- assert(cn->flags & CN_EXPANDABLE);
- /* If node is already contracted do nothing. */
- if(!(cn->flags & CN_EXPANDED)) return;
- cn->flags &= ~CN_EXPANDED;
- /* Clear selection below this node */
- clear_selection(cn);
- /* Zot children. We never used to do this but the result would be that over
- * time you'd end up with the entire tree pulled into memory. If the server
- * is over a slow network it will make interactivity slightly worse; if
- * anyone complains we can make it an option. */
- clear_children(cn);
- /* We can contract a node immediately. */
- redisplay_tree();
-}
-
-/* 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 Iterator, updated to point to next
+ * @return True if iterator remains valid
+ */
+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);