- * All the widgets at or below @p cn are deleted. All choosenodes below
- * it are emptied. i.e. we prune the tree at @p cn.
- */
-static void clear_children(struct choosenode *cn) {
- int n;
-
- D(("clear_children %s", cn->path));
- /* Recursively clear subtrees */
- for(n = 0; n < cn->children.nvec; ++n) {
- clear_children(cn->children.vec[n]);
- delete_cn_widgets(cn->children.vec[n]);
- }
- cn->children.nvec = 0;
-}
-
-/** @brief Called with a list of files just below some node */
-static void got_files(void *v, const char *error, int nvec, char **vec) {
- struct choosenode *cn = v;
- int n;
-
- if(error) {
- popup_protocol_error(0, error);
- return;
- }
-
- D(("got_files %d files for %s %s", nvec, cn->path, cnflags(cn)));
- /* Complicated by the need to resolve aliases. */
- cn->flags &= ~CN_GETTING_FILES;
- --gets_in_flight;
- if((cn->pending = nvec)) {
- cn->flags |= CN_RESOLVING_FILES;
- for(n = 0; n < nvec; ++n) {
- disorder_eclient_resolve(client, got_resolved_file, vec[n], cn);
- ++gets_in_flight;
- }
- }
- /* If there are no files and the directories are all read by now, we're
- * done */
- if(!(cn->flags & CN_GETTING_ANY))
- filled(cn);
- if(!gets_in_flight)
- redisplay_tree("got_files");
-}
-
-/** @brief Called with an alias resolved filename */
-static void got_resolved_file(void *v, const char *error, const char *track) {
- struct choosenode *const cn = v, *file_cn;
-
- if(error) {
- popup_protocol_error(0, error);
- } else {
- D(("resolved %s %s %d left", cn->path, cnflags(cn), cn->pending - 1));
- /* TODO as below */
- file_cn = newnode(cn, track,
- trackname_transform("track", track, "display"),
- trackname_transform("track", track, "sort"),
- 0/*flags*/, 0/*fill*/);
- --gets_in_flight;
- /* Only bother updating when we've got the lot */
- if(--cn->pending == 0) {
- cn->flags &= ~CN_RESOLVING_FILES;
- updated_node(cn, gets_in_flight == 0, "got_resolved_file");
- if(!(cn->flags & CN_GETTING_ANY))
- filled(cn);
- }
- }
-}
-
-/** @brief Called with a list of directories just below some node */
-static void got_dirs(void *v, const char *error, int nvec, char **vec) {
- struct choosenode *cn = v;
- int n;
-
- if(error) {
- popup_protocol_error(0, error);
- return;
- }
-
- D(("got_dirs %d dirs for %s %s", nvec, cn->path, cnflags(cn)));
- /* TODO this depends on local configuration for trackname_transform().
- * This will work, since the defaults are now built-in, but it'll be
- * (potentially) different to the server's configured settings.
- *
- * Really we want a variant of files/dirs that produces both the
- * raw filename and the transformed name for a chosen context.
- */
- --gets_in_flight;
- 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, gets_in_flight == 0, "got_dirs");
- cn->flags &= ~CN_GETTING_DIRS;
- if(!(cn->flags & CN_GETTING_ANY))
- filled(cn);
-}
-
-/** @brief Fill a child node */
-static void fill_directory_node(struct choosenode *cn) {
- D(("fill_directory_node %s", cn->path));
- /* TODO: caching */
- if(cn->flags & CN_GETTING_ANY)
- return;
- assert(report_label != 0);
- gtk_label_set_text(GTK_LABEL(report_label), "getting files");
- clear_children(cn);
- disorder_eclient_dirs(client, got_dirs, cn->path, 0, cn);
- disorder_eclient_files(client, got_files, cn->path, 0, cn);
- cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
- gets_in_flight += 2;
-}
-
-/** @brief Expand a node */
-static void expand_node(struct choosenode *cn, int contingent) {
- D(("expand_node %s %d %s", cn->path, contingent, cnflags(cn)));
- 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;
- if(contingent)
- cn->flags |= CN_CONTINGENT;
- else
- cn->flags &= ~CN_CONTINGENT;
- /* If this node is not contingently expanded, mark all its parents back to
- * the root as not contingent either, so they won't be contracted when the
- * search results change */
- if(!contingent) {
- struct choosenode *cnp;
-
- for(cnp = cn->parent; cnp; cnp = cnp->parent)
- cnp->flags &= ~CN_CONTINGENT;
- }
- /* TODO: visual feedback */
- cn->fill(cn);
-}
-
-/** @brief Make sure all the search results below @p cn are expanded
- * @param cn Node to start at
- */
-static void expand_from(struct choosenode *cn) {
- int n;
-
- if(nsearchvisible == nsearchresults)
- /* We're done */
- return;
- /* Are any of the search tracks at/below this point? */
- if(!(cn == root || hash_find(searchhash, cn->path)))
- return;
- D(("expand_from %d/%d visible %s",
- nsearchvisible, nsearchresults, cn->path));
- if(cn->flags & CN_EXPANDABLE) {
- if(cn->flags & CN_EXPANDED)
- /* This node is marked as expanded already. children.nvec might be 0,
- * indicating that expansion is still underway. We should get another
- * callback when it is expanded. */
- for(n = 0; n < cn->children.nvec && gets_in_flight < 10; ++n)
- expand_from(cn->children.vec[n]);
- else {
- /* This node is not expanded yet */
- expand_node(cn, 1);
- }
- } else {
- /* This is an actual search result */
- ++nsearchvisible;
- progress_window_progress(spw, nsearchvisible, nsearchresults);
- if(nsearchvisible == nsearchresults) {
- if(suppress_redisplay) {
- suppress_redisplay = 0;
- redisplay_tree("all search results visible");
- }
- /* We've got the lot. We make sure the first result is visible. */
- cn = first_search_result(root);
- gtk_adjustment_clamp_page(vadjust, cn->ymin, cn->ymax);
- }
- }
-}
-
-/** @brief Contract all contingently expanded nodes below @p cn */
-static void contract_contingent(struct choosenode *cn) {
- int n;
-
- if(cn->flags & CN_CONTINGENT)
- contract_node(cn);
- else
- for(n = 0; n < cn->children.nvec; ++n)
- contract_contingent(cn->children.vec[n]);
-}
-
-/** @brief 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|CN_CONTINGENT);
- /* 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("contract_node");
-}
-
-/** @brief 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);
-}
-
-/** @brief Called when an expandable node is updated. */
-static void updated_node(struct choosenode *cn, int redisplay,
- const char *why) {
- 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) {
- char whywhy[1024];
-
- snprintf(whywhy, sizeof whywhy, "updated_node %s", why);
- redisplay_tree(whywhy);
- }
-}
-
-/* Searching --------------------------------------------------------------- */
-
-/** @brief Return true if @p track is a search result