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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file disobedience/choose.c
21 * @brief Hierarchical track selection and search
23 * We now use an ordinary GtkTreeStore/GtkTreeView.
25 * We don't want to pull the entire tree in memory, but we want directories to
26 * show up as having children. Therefore we give directories a placeholder
27 * child and replace their children when they are opened. Placeholders have
28 * TRACK_COLUMN="" and ISFILE_COLUMN=FALSE (so that they don't get check boxes,
32 * - sweep up contracted nodes, replacing their content with a placeholder
35 #include "disobedience.h"
37 #include <gdk/gdkkeysyms.h>
39 /** @brief The current selection tree */
40 GtkTreeStore *choose_store;
42 /** @brief The view onto the selection tree */
43 GtkWidget *choose_view;
45 /** @brief The selection tree's selection */
46 GtkTreeSelection *choose_selection;
48 /** @brief Count of file listing operations in flight */
49 static int choose_list_in_flight;
51 /** @brief If nonzero autocollapse column won't be set */
52 static int choose_suppress_set_autocollapse;
54 static char *choose_get_string(GtkTreeIter *iter, int column) {
56 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
59 char *s = xstrdup(gs);
64 char *choose_get_track(GtkTreeIter *iter) {
65 char *s = choose_get_string(iter, TRACK_COLUMN);
66 return *s ? s : 0; /* Placeholder -> NULL */
69 char *choose_get_sort(GtkTreeIter *iter) {
70 return choose_get_string(iter, SORT_COLUMN);
73 char *choose_get_display(GtkTreeIter *iter) {
74 return choose_get_string(iter, NAME_COLUMN);
77 int choose_is_file(GtkTreeIter *iter) {
79 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
80 ISFILE_COLUMN, &isfile,
85 int choose_is_dir(GtkTreeIter *iter) {
87 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
88 ISFILE_COLUMN, &isfile,
92 return !choose_is_placeholder(iter);
95 int choose_is_placeholder(GtkTreeIter *iter) {
96 return choose_get_string(iter, TRACK_COLUMN)[0] == 0;
99 int choose_can_autocollapse(GtkTreeIter *iter) {
100 gboolean autocollapse;
101 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
102 AUTOCOLLAPSE_COLUMN, &autocollapse,
107 /** @brief Remove node @p it and all its children
108 * @param Iterator, updated to point to next
109 * @return True if iterator remains valid
111 * TODO is this necessary? gtk_tree_store_remove() does not document what
112 * happens to children.
114 static gboolean choose_remove_node(GtkTreeIter *it) {
115 GtkTreeIter child[1];
116 gboolean childv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
120 childv = choose_remove_node(child);
121 return gtk_tree_store_remove(choose_store, it);
124 /** @brief Update length and state fields */
125 static gboolean choose_set_state_callback(GtkTreeModel attribute((unused)) *model,
126 GtkTreePath attribute((unused)) *path,
128 gpointer attribute((unused)) data) {
129 if(choose_is_file(it)) {
130 const char *track = choose_get_track(it);
131 const long l = namepart_length(track);
134 byte_snprintf(length, sizeof length, "%ld:%02ld", l / 60, l % 60);
137 gtk_tree_store_set(choose_store, it,
138 LENGTH_COLUMN, length,
139 STATE_COLUMN, queued(track),
141 if(choose_is_search_result(track))
142 gtk_tree_store_set(choose_store, it,
143 BG_COLUMN, SEARCH_RESULT_BG,
144 FG_COLUMN, SEARCH_RESULT_FG,
147 gtk_tree_store_set(choose_store, it,
148 BG_COLUMN, (char *)0,
149 FG_COLUMN, (char *)0,
152 return FALSE; /* continue walking */
155 /** @brief Called when the queue or playing track change */
156 static void choose_set_state(const char attribute((unused)) *event,
157 void attribute((unused)) *eventdata,
158 void attribute((unused)) *callbackdata) {
159 gtk_tree_model_foreach(GTK_TREE_MODEL(choose_store),
160 choose_set_state_callback,
164 /** @brief (Re-)populate a node
165 * @param parent_ref Node to populate or NULL to fill root
166 * @param nvec Number of children to add
167 * @param vec Children
168 * @param files 1 if children are files, 0 if directories
170 * Adjusts the set of files (or directories) below @p parent_ref to match those
171 * listed in @p nvec and @p vec.
173 * @parent_ref will be destroyed.
175 static void choose_populate(GtkTreeRowReference *parent_ref,
176 int nvec, char **vec,
178 const char *type = isfile ? "track" : "dir";
179 //fprintf(stderr, "%d new children of type %s\n", nvec, type);
182 /* Compute parent_* */
183 GtkTreeIter pit[1], *parent_it;
184 GtkTreePath *parent_path;
186 parent_path = gtk_tree_row_reference_get_path(parent_ref);
188 gboolean pitv = gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store),
191 /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n",
193 gtk_tree_path_to_string(parent_path));*/
197 /*fprintf(stderr, "choose_populate %s: populating the root\n",
200 /* Both td[] and the current node set are sorted so we can do a single linear
201 * pass to insert new nodes and remove unwanted ones. The total performance
202 * may be worse than linear depending on the performance of GTK+'s insert and
203 * delete operations. */
204 //fprintf(stderr, "sorting tracks\n");
205 struct tracksort_data *td = tracksort_init(nvec, vec, type);
207 gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
210 int inserted = 0, deleted_placeholder = 0;
211 //fprintf(stderr, "inserting tracks type=%s\n", type);
212 while(nvec > 0 || itv) {
213 /*fprintf(stderr, "td[] = %s, it=%s [%s]\n",
214 nvec > 0 ? td->track : "(none)",
215 itv ? choose_get_track(it) : "(!itv)",
216 itv ? (choose_is_file(it) ? "file" : "dir") : "");*/
217 enum { INSERT, DELETE, SKIP_TREE, SKIP_BOTH } action;
218 const char *track = itv ? choose_get_track(it) : 0;
220 //fprintf(stderr, " placeholder\n");
222 ++deleted_placeholder;
223 } else if(nvec > 0 && itv) {
224 /* There's both a tree row and a td[] entry */
225 const int cmp = compare_tracks(td->sort, choose_get_sort(it),
226 td->display, choose_get_display(it),
228 //fprintf(stderr, " cmp=%d\n", cmp);
230 /* td < it, so we insert td before it */
233 /* td > it, so we must either delete it (if the same type) or skip it */
234 if(choose_is_file(it) == isfile)
239 /* td = it, so we step past both */
241 } else if(nvec > 0) {
242 /* We've reached the end of the tree rows, but new have tracks left in
244 //fprintf(stderr, " inserting\n");
247 /* We've reached the end of the new tracks from td[], but there are
248 * further tracks in the tree */
249 //fprintf(stderr, " deleting\n");
250 if(choose_is_file(it) == isfile)
258 //fprintf(stderr, " INSERT %s\n", td->track);
259 /* Insert a new row from td[] before it, or at the end if it is no longer
261 GtkTreeIter child[1];
262 gtk_tree_store_insert_before(choose_store,
264 parent_it, /* parent */
265 itv ? it : NULL); /* successor */
266 gtk_tree_store_set(choose_store, child,
267 NAME_COLUMN, td->display,
268 ISFILE_COLUMN, isfile,
269 TRACK_COLUMN, td->track,
270 SORT_COLUMN, td->sort,
271 AUTOCOLLAPSE_COLUMN, FALSE,
273 /* Update length and state; we expect this to kick off length lookups
274 * rather than necessarily get the right value the first time round. */
275 choose_set_state_callback(0, 0, child, 0);
276 /* If we inserted a directory, insert a placeholder too, so it appears to
277 * have children; it will be deleted when we expand the directory. */
279 //fprintf(stderr, " inserting a placeholder\n");
280 GtkTreeIter placeholder[1];
282 gtk_tree_store_append(choose_store, placeholder, child);
283 gtk_tree_store_set(choose_store, placeholder,
284 NAME_COLUMN, "Waddling...",
286 ISFILE_COLUMN, FALSE,
295 //fprintf(stderr, " SKIP_BOTH\n");
300 //fprintf(stderr, " SKIP_TREE\n");
301 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
304 //fprintf(stderr, " DELETE\n");
305 itv = choose_remove_node(it);
309 /*fprintf(stderr, "inserted=%d deleted_placeholder=%d\n\n",
310 inserted, deleted_placeholder);*/
312 /* If we deleted a placeholder then we must re-expand the row */
313 if(deleted_placeholder) {
314 ++choose_suppress_set_autocollapse;
315 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), parent_path, FALSE);
316 --choose_suppress_set_autocollapse;
318 gtk_tree_row_reference_free(parent_ref);
319 gtk_tree_path_free(parent_path);
322 /* We only notify others that we've inserted tracks when there are no more
323 * insertions pending, so that they don't have to keep track of how many
324 * requests they've made. */
325 if(--choose_list_in_flight == 0) {
326 /* Notify interested parties that we inserted some tracks, AFTER making
327 * sure that the row is properly expanded */
328 //fprintf(stderr, "raising choose-more-tracks\n");
329 event_raise("choose-more-tracks", 0);
331 //fprintf(stderr, "choose_list_in_flight -> %d-\n", choose_list_in_flight);
334 static void choose_dirs_completed(void *v,
336 int nvec, char **vec) {
338 popup_protocol_error(0, err);
341 choose_populate(v, nvec, vec, 0/*!isfile*/);
344 static void choose_files_completed(void *v,
346 int nvec, char **vec) {
348 popup_protocol_error(0, err);
351 choose_populate(v, nvec, vec, 1/*isfile*/);
354 void choose_play_completed(void attribute((unused)) *v,
357 popup_protocol_error(0, err);
360 static void choose_state_toggled
361 (GtkCellRendererToggle attribute((unused)) *cell_renderer,
363 gpointer attribute((unused)) user_data) {
365 /* Identify the track */
367 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store),
372 if(!choose_is_file(it))
374 const char *track = choose_get_track(it);
377 disorder_eclient_play(client, track, choose_play_completed, 0);
381 /** @brief (Re-)get the children of @p path
382 * @param path Path to target row
383 * @param iter Iterator pointing at target row
385 * Called from choose_row_expanded() to make sure that the contents are present
386 * and from choose_refill_callback() to (re-)synchronize.
388 static void choose_refill_row(GtkTreePath *path,
390 const char *track = choose_get_track(iter);
391 disorder_eclient_files(client, choose_files_completed,
394 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
396 disorder_eclient_dirs(client, choose_dirs_completed,
399 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
401 /* The row references are destroyed in the _completed handlers. */
402 choose_list_in_flight += 2;
405 static void choose_row_expanded(GtkTreeView attribute((unused)) *treeview,
408 gpointer attribute((unused)) user_data) {
409 /*fprintf(stderr, "row-expanded path=[%s]\n\n",
410 gtk_tree_path_to_string(path));*/
411 /* We update a node's contents whenever it is expanded, even if it was
412 * already populated; the effect is that contracting and expanding a node
413 * suffices to update it to the latest state on the server. */
414 choose_refill_row(path, iter);
415 if(!choose_suppress_set_autocollapse) {
416 if(choose_auto_expanding) {
417 /* This was an automatic expansion; mark it the row for auto-collapse. */
418 gtk_tree_store_set(choose_store, iter,
419 AUTOCOLLAPSE_COLUMN, TRUE,
421 /*fprintf(stderr, "enable auto-collapse for %s\n",
422 gtk_tree_path_to_string(path));*/
424 /* This was a manual expansion. Inhibit automatic collapse on this row
425 * and all its ancestors. */
428 gtk_tree_store_set(choose_store, iter,
429 AUTOCOLLAPSE_COLUMN, FALSE,
431 /*fprintf(stderr, "suppress auto-collapse for %s\n",
432 gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(choose_store),
434 GtkTreeIter child = *iter;
435 itv = gtk_tree_model_iter_parent(GTK_TREE_MODEL(choose_store),
439 /* The effect of this is that if you expand a row that's actually a
440 * sibling of the real target of the auto-expansion, it stays expanded
441 * when you clear a search. That's find and good, but it _still_ stays
442 * expanded if you expand it and then collapse it.
444 * An alternative policy would be to only auto-collapse rows that don't
445 * have any expanded children (apart from ones also subject to
446 * auto-collapse). I'm not sure what the most usable policy is.
452 static void choose_auto_collapse_callback(GtkTreeView *tree_view,
454 gpointer attribute((unused)) user_data) {
457 gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path);
458 if(choose_can_autocollapse(it)) {
459 /*fprintf(stderr, "collapse %s\n",
460 gtk_tree_path_to_string(path));*/
461 gtk_tree_store_set(choose_store, it,
462 AUTOCOLLAPSE_COLUMN, FALSE,
464 gtk_tree_view_collapse_row(tree_view, path);
468 /** @brief Perform automatic collapse after a search is cleared */
469 void choose_auto_collapse(void) {
470 gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view),
471 choose_auto_collapse_callback,
475 /** @brief Called from choose_refill() with each expanded row */
476 static void choose_refill_callback(GtkTreeView attribute((unused)) *tree_view,
478 gpointer attribute((unused)) user_data) {
481 gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path);
482 choose_refill_row(path, it);
485 /** @brief Synchronize all visible data with the server
487 * Called at startup, when a rescan completes, and via periodic_slow().
489 static void choose_refill(const char attribute((unused)) *event,
490 void attribute((unused)) *eventdata,
491 void attribute((unused)) *callbackdata) {
492 //fprintf(stderr, "choose_refill\n");
493 /* Update the root */
494 disorder_eclient_files(client, choose_files_completed, "", NULL, NULL);
495 disorder_eclient_dirs(client, choose_dirs_completed, "", NULL, NULL);
496 choose_list_in_flight += 2;
497 /* Update all expanded rows */
498 gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view),
499 choose_refill_callback,
501 //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight);
504 /** @brief Called for key-*-event on the main view
506 static gboolean choose_key_event(GtkWidget attribute((unused)) *widget,
508 gpointer attribute((unused)) user_data) {
509 /*fprintf(stderr, "choose_key_event type=%d state=%#x keyval=%#x\n",
510 event->type, event->state, event->keyval);*/
511 switch(event->keyval) {
518 return FALSE; /* We'll take these */
520 /* ^F is expected to start a search. We implement this by focusing the
521 * search entry box. */
522 if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK
523 && event->type == GDK_KEY_PRESS) {
525 return TRUE; /* Handled it */
529 /* ^G is expected to go the next match. We simulate a click on the 'next'
531 if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK
532 && event->type == GDK_KEY_PRESS) {
533 choose_next_clicked(0, 0);
534 return TRUE; /* Handled it */
538 gtk_widget_event(user_data, (GdkEvent *)event);
539 return TRUE; /* Handled it */
542 /** @brief Create the choose tab */
543 GtkWidget *choose_widget(void) {
544 /* Create the tree store. */
545 choose_store = gtk_tree_store_new(CHOOSE_COLUMNS,
556 /* Create the view */
557 choose_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store));
558 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view), TRUE);
559 /* Suppress built-in typeahead find, we do our own search support. */
560 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(choose_view), FALSE);
562 /* Create cell renderers and columns */
563 /* TODO use a table */
565 GtkCellRenderer *r = gtk_cell_renderer_toggle_new();
566 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
569 "active", STATE_COLUMN,
570 "visible", ISFILE_COLUMN,
572 gtk_tree_view_column_set_resizable(c, TRUE);
573 gtk_tree_view_column_set_reorderable(c, TRUE);
574 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
575 g_signal_connect(r, "toggled",
576 G_CALLBACK(choose_state_toggled), 0);
579 GtkCellRenderer *r = gtk_cell_renderer_text_new();
580 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
583 "text", LENGTH_COLUMN,
585 gtk_tree_view_column_set_resizable(c, TRUE);
586 gtk_tree_view_column_set_reorderable(c, TRUE);
587 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
588 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
591 GtkCellRenderer *r = gtk_cell_renderer_text_new();
592 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
596 "background", BG_COLUMN,
597 "foreground", FG_COLUMN,
599 gtk_tree_view_column_set_resizable(c, TRUE);
600 gtk_tree_view_column_set_reorderable(c, TRUE);
601 g_object_set(c, "expand", TRUE, (char *)0);
602 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
603 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view), c);
606 /* The selection should support multiple things being selected */
607 choose_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view));
608 gtk_tree_selection_set_mode(choose_selection, GTK_SELECTION_MULTIPLE);
610 /* Catch button presses */
611 g_signal_connect(choose_view, "button-press-event",
612 G_CALLBACK(choose_button_event), 0);
613 g_signal_connect(choose_view, "button-release-event",
614 G_CALLBACK(choose_button_event), 0);
615 /* Catch row expansions so we can fill in placeholders */
616 g_signal_connect(choose_view, "row-expanded",
617 G_CALLBACK(choose_row_expanded), 0);
619 event_register("queue-list-changed", choose_set_state, 0);
620 event_register("playing-track-changed", choose_set_state, 0);
621 event_register("search-results-changed", choose_set_state, 0);
622 event_register("lookups-completed", choose_set_state, 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);