2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/choose.c
19 * @brief Hierarchical track selection and search
21 * We now use an ordinary GtkTreeStore/GtkTreeView.
23 * We don't want to pull the entire tree in memory, but we want directories to
24 * show up as having children. Therefore we give directories a placeholder
25 * child and replace their children when they are opened. Placeholders have
26 * TRACK_COLUMN="" and ISFILE_COLUMN=FALSE (so that they don't get check boxes,
30 * - sweep up contracted nodes, replacing their content with a placeholder
33 #include "disobedience.h"
35 #include <gdk/gdkkeysyms.h>
37 /** @brief The current selection tree */
38 GtkTreeStore *choose_store;
40 /** @brief The view onto the selection tree */
41 GtkWidget *choose_view;
43 /** @brief The selection tree's selection */
44 GtkTreeSelection *choose_selection;
46 /** @brief Count of file listing operations in flight */
47 static int choose_list_in_flight;
49 /** @brief If nonzero autocollapse column won't be set */
50 static int choose_suppress_set_autocollapse;
52 static char *choose_get_string(GtkTreeIter *iter, int column) {
54 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
57 char *s = xstrdup(gs);
62 char *choose_get_track(GtkTreeIter *iter) {
63 char *s = choose_get_string(iter, TRACK_COLUMN);
64 return *s ? s : 0; /* Placeholder -> NULL */
67 char *choose_get_sort(GtkTreeIter *iter) {
68 return choose_get_string(iter, SORT_COLUMN);
71 char *choose_get_display(GtkTreeIter *iter) {
72 return choose_get_string(iter, NAME_COLUMN);
75 int choose_is_file(GtkTreeIter *iter) {
77 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
78 ISFILE_COLUMN, &isfile,
83 int choose_is_dir(GtkTreeIter *iter) {
85 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
86 ISFILE_COLUMN, &isfile,
90 return !choose_is_placeholder(iter);
93 int choose_is_placeholder(GtkTreeIter *iter) {
94 return choose_get_string(iter, TRACK_COLUMN)[0] == 0;
97 int choose_can_autocollapse(GtkTreeIter *iter) {
98 gboolean autocollapse;
99 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
100 AUTOCOLLAPSE_COLUMN, &autocollapse,
105 /** @brief Remove node @p it and all its children
106 * @param it Iterator, updated to point to next
107 * @return True if iterator remains valid
109 * TODO is this necessary? gtk_tree_store_remove() does not document what
110 * happens to children.
112 static gboolean choose_remove_node(GtkTreeIter *it) {
113 GtkTreeIter child[1];
114 gboolean childv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
118 childv = choose_remove_node(child);
119 return gtk_tree_store_remove(choose_store, it);
122 /** @brief Update length and state fields */
123 static gboolean choose_set_state_callback(GtkTreeModel attribute((unused)) *model,
124 GtkTreePath attribute((unused)) *path,
126 gpointer attribute((unused)) data) {
127 if(choose_is_file(it)) {
128 const char *track = choose_get_track(it);
129 const long l = namepart_length(track);
132 byte_snprintf(length, sizeof length, "%ld:%02ld", l / 60, l % 60);
135 gtk_tree_store_set(choose_store, it,
136 LENGTH_COLUMN, length,
137 STATE_COLUMN, queued(track),
139 if(choose_is_search_result(track))
140 gtk_tree_store_set(choose_store, it,
141 BG_COLUMN, SEARCH_RESULT_BG,
142 FG_COLUMN, SEARCH_RESULT_FG,
145 gtk_tree_store_set(choose_store, it,
146 BG_COLUMN, (char *)0,
147 FG_COLUMN, (char *)0,
150 return FALSE; /* continue walking */
153 /** @brief Called when the queue or playing track change */
154 static void choose_set_state(const char attribute((unused)) *event,
155 void attribute((unused)) *eventdata,
156 void attribute((unused)) *callbackdata) {
157 gtk_tree_model_foreach(GTK_TREE_MODEL(choose_store),
158 choose_set_state_callback,
162 /** @brief (Re-)populate a node
163 * @param parent_ref Node to populate or NULL to fill root
164 * @param nvec Number of children to add
165 * @param vec Children
166 * @param isfile 1 if children are files, 0 if directories
168 * Adjusts the set of files (or directories) below @p parent_ref to match those
169 * listed in @p nvec and @p vec.
171 * @p parent_ref will be destroyed.
173 static void choose_populate(GtkTreeRowReference *parent_ref,
174 int nvec, char **vec,
176 const char *type = isfile ? "track" : "dir";
177 //fprintf(stderr, "%d new children of type %s\n", nvec, type);
180 /* Compute parent_* */
181 GtkTreeIter pit[1], *parent_it;
182 GtkTreePath *parent_path;
184 parent_path = gtk_tree_row_reference_get_path(parent_ref);
186 gboolean pitv = gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store),
189 /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n",
191 gtk_tree_path_to_string(parent_path));*/
195 /*fprintf(stderr, "choose_populate %s: populating the root\n",
198 /* Both td[] and the current node set are sorted so we can do a single linear
199 * pass to insert new nodes and remove unwanted ones. The total performance
200 * may be worse than linear depending on the performance of GTK+'s insert and
201 * delete operations. */
202 //fprintf(stderr, "sorting tracks\n");
203 struct tracksort_data *td = tracksort_init(nvec, vec, type);
205 gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
208 int inserted = 0, deleted_placeholder = 0;
209 //fprintf(stderr, "inserting tracks type=%s\n", type);
210 while(nvec > 0 || itv) {
211 /*fprintf(stderr, "td[] = %s, it=%s [%s]\n",
212 nvec > 0 ? td->track : "(none)",
213 itv ? choose_get_track(it) : "(!itv)",
214 itv ? (choose_is_file(it) ? "file" : "dir") : "");*/
215 enum { INSERT, DELETE, SKIP_TREE, SKIP_BOTH } action;
216 const char *track = itv ? choose_get_track(it) : 0;
218 //fprintf(stderr, " placeholder\n");
220 ++deleted_placeholder;
221 } else if(nvec > 0 && itv) {
222 /* There's both a tree row and a td[] entry */
223 const int cmp = compare_tracks(td->sort, choose_get_sort(it),
224 td->display, choose_get_display(it),
226 //fprintf(stderr, " cmp=%d\n", cmp);
228 /* td < it, so we insert td before it */
231 /* td > it, so we must either delete it (if the same type) or skip it */
232 if(choose_is_file(it) == isfile)
237 /* td = it, so we step past both */
239 } else if(nvec > 0) {
240 /* We've reached the end of the tree rows, but new have tracks left in
242 //fprintf(stderr, " inserting\n");
245 /* We've reached the end of the new tracks from td[], but there are
246 * further tracks in the tree */
247 //fprintf(stderr, " deleting\n");
248 if(choose_is_file(it) == isfile)
256 //fprintf(stderr, " INSERT %s\n", td->track);
257 /* Insert a new row from td[] before it, or at the end if it is no longer
259 GtkTreeIter child[1];
260 gtk_tree_store_insert_before(choose_store,
262 parent_it, /* parent */
263 itv ? it : NULL); /* successor */
264 gtk_tree_store_set(choose_store, child,
265 NAME_COLUMN, td->display,
266 ISFILE_COLUMN, isfile,
267 TRACK_COLUMN, td->track,
268 SORT_COLUMN, td->sort,
269 AUTOCOLLAPSE_COLUMN, FALSE,
271 /* Update length and state; we expect this to kick off length lookups
272 * rather than necessarily get the right value the first time round. */
273 choose_set_state_callback(0, 0, child, 0);
274 /* If we inserted a directory, insert a placeholder too, so it appears to
275 * have children; it will be deleted when we expand the directory. */
277 //fprintf(stderr, " inserting a placeholder\n");
278 GtkTreeIter placeholder[1];
280 gtk_tree_store_append(choose_store, placeholder, child);
281 gtk_tree_store_set(choose_store, placeholder,
282 NAME_COLUMN, "Waddling...",
284 ISFILE_COLUMN, FALSE,
293 //fprintf(stderr, " SKIP_BOTH\n");
298 //fprintf(stderr, " SKIP_TREE\n");
299 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
302 //fprintf(stderr, " DELETE\n");
303 itv = choose_remove_node(it);
307 /*fprintf(stderr, "inserted=%d deleted_placeholder=%d\n\n",
308 inserted, deleted_placeholder);*/
310 /* If we deleted a placeholder then we must re-expand the row */
311 if(deleted_placeholder) {
312 ++choose_suppress_set_autocollapse;
313 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), parent_path, FALSE);
314 --choose_suppress_set_autocollapse;
316 gtk_tree_row_reference_free(parent_ref);
317 gtk_tree_path_free(parent_path);
320 /* We only notify others that we've inserted tracks when there are no more
321 * insertions pending, so that they don't have to keep track of how many
322 * requests they've made. */
323 if(--choose_list_in_flight == 0) {
324 /* Notify interested parties that we inserted some tracks, AFTER making
325 * sure that the row is properly expanded */
326 //fprintf(stderr, "raising choose-more-tracks\n");
327 event_raise("choose-more-tracks", 0);
329 //fprintf(stderr, "choose_list_in_flight -> %d-\n", choose_list_in_flight);
332 static void choose_dirs_completed(void *v,
334 int nvec, char **vec) {
336 popup_protocol_error(0, err);
339 choose_populate(v, nvec, vec, 0/*!isfile*/);
342 static void choose_files_completed(void *v,
344 int nvec, char **vec) {
346 popup_protocol_error(0, err);
349 choose_populate(v, nvec, vec, 1/*isfile*/);
352 void choose_play_completed(void attribute((unused)) *v,
355 popup_protocol_error(0, err);
358 static void choose_state_toggled
359 (GtkCellRendererToggle attribute((unused)) *cell_renderer,
361 gpointer attribute((unused)) user_data) {
363 /* Identify the track */
365 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store),
370 if(!choose_is_file(it))
372 const char *track = choose_get_track(it);
375 disorder_eclient_play(client, track, choose_play_completed, 0);
379 /** @brief (Re-)get the children of @p path
380 * @param path Path to target row
381 * @param iter Iterator pointing at target row
383 * Called from choose_row_expanded() to make sure that the contents are present
384 * and from choose_refill_callback() to (re-)synchronize.
386 static void choose_refill_row(GtkTreePath *path,
388 const char *track = choose_get_track(iter);
389 disorder_eclient_files(client, choose_files_completed,
392 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
394 disorder_eclient_dirs(client, choose_dirs_completed,
397 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
399 /* The row references are destroyed in the _completed handlers. */
400 choose_list_in_flight += 2;
403 static void choose_row_expanded(GtkTreeView attribute((unused)) *treeview,
406 gpointer attribute((unused)) user_data) {
407 /*fprintf(stderr, "row-expanded path=[%s]\n\n",
408 gtk_tree_path_to_string(path));*/
409 /* We update a node's contents whenever it is expanded, even if it was
410 * already populated; the effect is that contracting and expanding a node
411 * suffices to update it to the latest state on the server. */
412 choose_refill_row(path, iter);
413 if(!choose_suppress_set_autocollapse) {
414 if(choose_auto_expanding) {
415 /* This was an automatic expansion; mark it the row for auto-collapse. */
416 gtk_tree_store_set(choose_store, iter,
417 AUTOCOLLAPSE_COLUMN, TRUE,
419 /*fprintf(stderr, "enable auto-collapse for %s\n",
420 gtk_tree_path_to_string(path));*/
422 /* This was a manual expansion. Inhibit automatic collapse on this row
423 * and all its ancestors. */
426 gtk_tree_store_set(choose_store, iter,
427 AUTOCOLLAPSE_COLUMN, FALSE,
429 /*fprintf(stderr, "suppress auto-collapse for %s\n",
430 gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(choose_store),
432 GtkTreeIter child = *iter;
433 itv = gtk_tree_model_iter_parent(GTK_TREE_MODEL(choose_store),
437 /* The effect of this is that if you expand a row that's actually a
438 * sibling of the real target of the auto-expansion, it stays expanded
439 * when you clear a search. That's find and good, but it _still_ stays
440 * expanded if you expand it and then collapse it.
442 * An alternative policy would be to only auto-collapse rows that don't
443 * have any expanded children (apart from ones also subject to
444 * auto-collapse). I'm not sure what the most usable policy is.
450 static void choose_auto_collapse_callback(GtkTreeView *tree_view,
452 gpointer attribute((unused)) user_data) {
455 gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path);
456 if(choose_can_autocollapse(it)) {
457 /*fprintf(stderr, "collapse %s\n",
458 gtk_tree_path_to_string(path));*/
459 gtk_tree_store_set(choose_store, it,
460 AUTOCOLLAPSE_COLUMN, FALSE,
462 gtk_tree_view_collapse_row(tree_view, path);
466 /** @brief Perform automatic collapse after a search is cleared */
467 void choose_auto_collapse(void) {
468 gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view),
469 choose_auto_collapse_callback,
473 /** @brief Called from choose_refill() with each expanded row */
474 static void choose_refill_callback(GtkTreeView attribute((unused)) *tree_view,
476 gpointer attribute((unused)) user_data) {
479 gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path);
480 choose_refill_row(path, it);
483 /** @brief Synchronize all visible data with the server
485 * Called at startup, when a rescan completes, and via periodic_slow().
487 static void choose_refill(const char attribute((unused)) *event,
488 void attribute((unused)) *eventdata,
489 void attribute((unused)) *callbackdata) {
490 //fprintf(stderr, "choose_refill\n");
491 /* Update the root */
492 disorder_eclient_files(client, choose_files_completed, "", NULL, NULL);
493 disorder_eclient_dirs(client, choose_dirs_completed, "", NULL, NULL);
494 choose_list_in_flight += 2;
495 /* Update all expanded rows */
496 gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view),
497 choose_refill_callback,
499 //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight);
502 /** @brief Called for key-*-event on the main view
504 static gboolean choose_key_event(GtkWidget attribute((unused)) *widget,
506 gpointer user_data) {
507 /*fprintf(stderr, "choose_key_event type=%d state=%#x keyval=%#x\n",
508 event->type, event->state, event->keyval);*/
509 switch(event->keyval) {
516 return FALSE; /* We'll take these */
518 /* ^F is expected to start a search. We implement this by focusing the
519 * search entry box. */
520 if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK
521 && event->type == GDK_KEY_PRESS) {
523 return TRUE; /* Handled it */
527 /* ^G is expected to go the next match. We simulate a click on the 'next'
529 if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK
530 && event->type == GDK_KEY_PRESS) {
531 choose_next_clicked(0, 0);
532 return TRUE; /* Handled it */
536 /* Anything not handled we redirected to the search entry field */
537 gtk_widget_event(user_data, (GdkEvent *)event);
538 return TRUE; /* Handled it */
541 /** @brief Create the choose tab */
542 GtkWidget *choose_widget(void) {
543 /* Create the tree store. */
544 choose_store = gtk_tree_store_new(CHOOSE_COLUMNS,
555 /* Create the view */
556 choose_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store));
557 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view), TRUE);
558 /* Suppress built-in typeahead find, we do our own search support. */
559 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(choose_view), FALSE);
561 /* Create cell renderers and columns */
562 /* TODO use a table */
564 GtkCellRenderer *r = gtk_cell_renderer_toggle_new();
565 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
568 "active", STATE_COLUMN,
569 "visible", ISFILE_COLUMN,
571 gtk_tree_view_column_set_resizable(c, TRUE);
572 gtk_tree_view_column_set_reorderable(c, TRUE);
573 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
574 g_signal_connect(r, "toggled",
575 G_CALLBACK(choose_state_toggled), 0);
578 GtkCellRenderer *r = gtk_cell_renderer_text_new();
579 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
582 "text", LENGTH_COLUMN,
584 gtk_tree_view_column_set_resizable(c, TRUE);
585 gtk_tree_view_column_set_reorderable(c, TRUE);
586 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
587 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
590 GtkCellRenderer *r = gtk_cell_renderer_text_new();
591 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
595 "background", BG_COLUMN,
596 "foreground", FG_COLUMN,
598 gtk_tree_view_column_set_resizable(c, TRUE);
599 gtk_tree_view_column_set_reorderable(c, TRUE);
600 g_object_set(c, "expand", TRUE, (char *)0);
601 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
602 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view), c);
605 /* The selection should support multiple things being selected */
606 choose_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view));
607 gtk_tree_selection_set_mode(choose_selection, GTK_SELECTION_MULTIPLE);
609 /* Catch button presses */
610 g_signal_connect(choose_view, "button-press-event",
611 G_CALLBACK(choose_button_event), 0);
612 g_signal_connect(choose_view, "button-release-event",
613 G_CALLBACK(choose_button_event), 0);
614 /* Catch row expansions so we can fill in placeholders */
615 g_signal_connect(choose_view, "row-expanded",
616 G_CALLBACK(choose_row_expanded), 0);
618 event_register("queue-list-changed", choose_set_state, 0);
619 event_register("playing-track-changed", choose_set_state, 0);
620 event_register("search-results-changed", choose_set_state, 0);
621 event_register("lookups-completed", choose_set_state, 0);
622 event_register("choose-more-tracks", choose_menu_moretracks, 0);
624 /* After a rescan we update the choose tree. We get a rescan-complete
625 * automatically at startup and upon connection too. */
626 event_register("rescan-complete", choose_refill, 0);
628 /* Make the widget scrollable */
629 GtkWidget *scrolled = scroll_widget(choose_view);
631 /* Pack vertically with the search widget */
632 GtkWidget *vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
633 gtk_box_pack_start(GTK_BOX(vbox), scrolled,
634 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
635 gtk_box_pack_end(GTK_BOX(vbox), choose_search_widget(),
636 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
638 g_object_set_data(G_OBJECT(vbox), "type", (void *)&choose_tabtype);
640 /* Redirect keyboard activity to the search widget */
641 g_signal_connect(choose_view, "key-press-event",
642 G_CALLBACK(choose_key_event), choose_search_entry);
643 g_signal_connect(choose_view, "key-release-event",
644 G_CALLBACK(choose_key_event), choose_search_entry);