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,
31 * TODO We do a period sweep which kills contracted nodes, putting back
32 * placeholders, and updating expanded nodes to keep up with server-side
33 * changes. (We could trigger the latter off rescan complete notifications?)
36 * - sweep up contracted nodes
37 * - update when content may have changed (e.g. after a rescan)
40 #include "disobedience.h"
42 #include <gdk/gdkkeysyms.h>
44 /** @brief The current selection tree */
45 GtkTreeStore
*choose_store
;
47 /** @brief The view onto the selection tree */
48 GtkWidget
*choose_view
;
50 /** @brief The selection tree's selection */
51 GtkTreeSelection
*choose_selection
;
53 /** @brief Count of file listing operations in flight */
54 static int choose_list_in_flight
;
56 /** @brief If nonzero autocollapse column won't be set */
57 static int choose_suppress_set_autocollapse
;
59 static char *choose_get_string(GtkTreeIter
*iter
, int column
) {
61 gtk_tree_model_get(GTK_TREE_MODEL(choose_store
), iter
,
64 char *s
= xstrdup(gs
);
69 char *choose_get_track(GtkTreeIter
*iter
) {
70 char *s
= choose_get_string(iter
, TRACK_COLUMN
);
71 return *s ? s
: 0; /* Placeholder -> NULL */
74 char *choose_get_sort(GtkTreeIter
*iter
) {
75 return choose_get_string(iter
, SORT_COLUMN
);
78 char *choose_get_display(GtkTreeIter
*iter
) {
79 return choose_get_string(iter
, NAME_COLUMN
);
82 int choose_is_file(GtkTreeIter
*iter
) {
84 gtk_tree_model_get(GTK_TREE_MODEL(choose_store
), iter
,
85 ISFILE_COLUMN
, &isfile
,
90 int choose_is_dir(GtkTreeIter
*iter
) {
92 gtk_tree_model_get(GTK_TREE_MODEL(choose_store
), iter
,
93 ISFILE_COLUMN
, &isfile
,
97 return !choose_is_placeholder(iter
);
100 int choose_is_placeholder(GtkTreeIter
*iter
) {
101 return choose_get_string(iter
, TRACK_COLUMN
)[0] == 0;
104 int choose_can_autocollapse(GtkTreeIter
*iter
) {
105 gboolean autocollapse
;
106 gtk_tree_model_get(GTK_TREE_MODEL(choose_store
), iter
,
107 AUTOCOLLAPSE_COLUMN
, &autocollapse
,
112 /** @brief Remove node @p it and all its children
113 * @param Iterator, updated to point to next
114 * @return True if iterator remains valid
116 * TODO is this necessary? gtk_tree_store_remove() does not document what
117 * happens to children.
119 static gboolean
choose_remove_node(GtkTreeIter
*it
) {
120 GtkTreeIter child
[1];
121 gboolean childv
= gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store
),
125 childv
= choose_remove_node(child
);
126 return gtk_tree_store_remove(choose_store
, it
);
129 /** @brief Update length and state fields */
130 static gboolean
choose_set_state_callback(GtkTreeModel
attribute((unused
)) *model
,
131 GtkTreePath
attribute((unused
)) *path
,
133 gpointer
attribute((unused
)) data
) {
134 if(choose_is_file(it
)) {
135 const char *track
= choose_get_track(it
);
136 const long l
= namepart_length(track
);
139 byte_snprintf(length
, sizeof length
, "%ld:%02ld", l
/ 60, l
% 60);
142 gtk_tree_store_set(choose_store
, it
,
143 LENGTH_COLUMN
, length
,
144 STATE_COLUMN
, queued(track
),
146 if(choose_is_search_result(track
))
147 gtk_tree_store_set(choose_store
, it
,
148 BG_COLUMN
, SEARCH_RESULT_BG
,
149 FG_COLUMN
, SEARCH_RESULT_FG
,
152 gtk_tree_store_set(choose_store
, it
,
153 BG_COLUMN
, (char *)0,
154 FG_COLUMN
, (char *)0,
157 return FALSE
; /* continue walking */
160 /** @brief Called when the queue or playing track change */
161 static void choose_set_state(const char attribute((unused
)) *event
,
162 void attribute((unused
)) *eventdata
,
163 void attribute((unused
)) *callbackdata
) {
164 gtk_tree_model_foreach(GTK_TREE_MODEL(choose_store
),
165 choose_set_state_callback
,
169 /** @brief (Re-)populate a node
170 * @param parent_ref Node to populate or NULL to fill root
171 * @param nvec Number of children to add
172 * @param vec Children
173 * @param files 1 if children are files, 0 if directories
175 * Adjusts the set of files (or directories) below @p parent_ref to match those
176 * listed in @p nvec and @p vec.
178 * @parent_ref will be destroyed.
180 static void choose_populate(GtkTreeRowReference
*parent_ref
,
181 int nvec
, char **vec
,
183 const char *type
= isfile ?
"track" : "dir";
184 //fprintf(stderr, "%d new children of type %s\n", nvec, type);
187 /* Compute parent_* */
188 GtkTreeIter pit
[1], *parent_it
;
189 GtkTreePath
*parent_path
;
191 parent_path
= gtk_tree_row_reference_get_path(parent_ref
);
193 gboolean pitv
= gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store
),
196 /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n",
198 gtk_tree_path_to_string(parent_path));*/
202 /*fprintf(stderr, "choose_populate %s: populating the root\n",
205 /* Both td[] and the current node set are sorted so we can do a single linear
206 * pass to insert new nodes and remove unwanted ones. The total performance
207 * may be worse than linear depending on the performance of GTK+'s insert and
208 * delete operations. */
209 //fprintf(stderr, "sorting tracks\n");
210 struct tracksort_data
*td
= tracksort_init(nvec
, vec
, type
);
212 gboolean itv
= gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store
),
215 int inserted
= 0, deleted_placeholder
= 0;
216 //fprintf(stderr, "inserting tracks type=%s\n", type);
217 while(nvec
> 0 || itv
) {
218 /*fprintf(stderr, "td[] = %s, it=%s [%s]\n",
219 nvec > 0 ? td->track : "(none)",
220 itv ? choose_get_track(it) : "(!itv)",
221 itv ? (choose_is_file(it) ? "file" : "dir") : "");*/
222 enum { INSERT
, DELETE
, SKIP_TREE
, SKIP_BOTH
} action
;
223 const char *track
= itv ?
choose_get_track(it
) : 0;
225 //fprintf(stderr, " placeholder\n");
227 ++deleted_placeholder
;
228 } else if(nvec
> 0 && itv
) {
229 /* There's both a tree row and a td[] entry */
230 const int cmp
= compare_tracks(td
->sort
, choose_get_sort(it
),
231 td
->display
, choose_get_display(it
),
233 //fprintf(stderr, " cmp=%d\n", cmp);
235 /* td < it, so we insert td before it */
238 /* td > it, so we must either delete it (if the same type) or skip it */
239 if(choose_is_file(it
) == isfile
)
244 /* td = it, so we step past both */
246 } else if(nvec
> 0) {
247 /* We've reached the end of the tree rows, but new have tracks left in
249 //fprintf(stderr, " inserting\n");
252 /* We've reached the end of the new tracks from td[], but there are
253 * further tracks in the tree */
254 //fprintf(stderr, " deleting\n");
255 if(choose_is_file(it
) == isfile
)
263 //fprintf(stderr, " INSERT %s\n", td->track);
264 /* Insert a new row from td[] before it, or at the end if it is no longer
266 GtkTreeIter child
[1];
267 gtk_tree_store_insert_before(choose_store
,
269 parent_it
, /* parent */
270 itv ? it
: NULL
); /* successor */
271 gtk_tree_store_set(choose_store
, child
,
272 NAME_COLUMN
, td
->display
,
273 ISFILE_COLUMN
, isfile
,
274 TRACK_COLUMN
, td
->track
,
275 SORT_COLUMN
, td
->sort
,
276 AUTOCOLLAPSE_COLUMN
, FALSE
,
278 /* Update length and state; we expect this to kick off length lookups
279 * rather than necessarily get the right value the first time round. */
280 choose_set_state_callback(0, 0, child
, 0);
281 /* If we inserted a directory, insert a placeholder too, so it appears to
282 * have children; it will be deleted when we expand the directory. */
284 //fprintf(stderr, " inserting a placeholder\n");
285 GtkTreeIter placeholder
[1];
287 gtk_tree_store_append(choose_store
, placeholder
, child
);
288 gtk_tree_store_set(choose_store
, placeholder
,
289 NAME_COLUMN
, "Waddling...",
291 ISFILE_COLUMN
, FALSE
,
300 //fprintf(stderr, " SKIP_BOTH\n");
305 //fprintf(stderr, " SKIP_TREE\n");
306 itv
= gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store
), it
);
309 //fprintf(stderr, " DELETE\n");
310 itv
= choose_remove_node(it
);
314 /*fprintf(stderr, "inserted=%d deleted_placeholder=%d\n\n",
315 inserted, deleted_placeholder);*/
317 /* If we deleted a placeholder then we must re-expand the row */
318 if(deleted_placeholder
) {
319 ++choose_suppress_set_autocollapse
;
320 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view
), parent_path
, FALSE
);
321 --choose_suppress_set_autocollapse
;
323 gtk_tree_row_reference_free(parent_ref
);
324 gtk_tree_path_free(parent_path
);
327 /* We only notify others that we've inserted tracks when there are no more
328 * insertions pending, so that they don't have to keep track of how many
329 * requests they've made. */
330 if(--choose_list_in_flight
== 0) {
331 /* Notify interested parties that we inserted some tracks, AFTER making
332 * sure that the row is properly expanded */
333 //fprintf(stderr, "raising choose-more-tracks\n");
334 event_raise("choose-more-tracks", 0);
336 //fprintf(stderr, "choose_list_in_flight -> %d-\n", choose_list_in_flight);
339 static void choose_dirs_completed(void *v
,
341 int nvec
, char **vec
) {
343 popup_protocol_error(0, error
);
346 choose_populate(v
, nvec
, vec
, 0/*!isfile*/);
349 static void choose_files_completed(void *v
,
351 int nvec
, char **vec
) {
353 popup_protocol_error(0, error
);
356 choose_populate(v
, nvec
, vec
, 1/*isfile*/);
359 void choose_play_completed(void attribute((unused
)) *v
,
362 popup_protocol_error(0, error
);
365 static void choose_state_toggled
366 (GtkCellRendererToggle
attribute((unused
)) *cell_renderer
,
368 gpointer
attribute((unused
)) user_data
) {
370 /* Identify the track */
372 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store
),
377 if(!choose_is_file(it
))
379 const char *track
= choose_get_track(it
);
382 disorder_eclient_play(client
, track
, choose_play_completed
, 0);
386 /** @brief (Re-)get the children of @p path
387 * @param path Path to target row
388 * @param iter Iterator pointing at target row
390 * Called from choose_row_expanded() to make sure that the contents are present
391 * and from choose_refill_callback() to (re-)synchronize.
393 static void choose_refill_row(GtkTreePath
*path
,
395 const char *track
= choose_get_track(iter
);
396 disorder_eclient_files(client
, choose_files_completed
,
399 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store
),
401 disorder_eclient_dirs(client
, choose_dirs_completed
,
404 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store
),
406 /* The row references are destroyed in the _completed handlers. */
407 choose_list_in_flight
+= 2;
410 static void choose_row_expanded(GtkTreeView
attribute((unused
)) *treeview
,
413 gpointer
attribute((unused
)) user_data
) {
414 /*fprintf(stderr, "row-expanded path=[%s]\n\n",
415 gtk_tree_path_to_string(path));*/
416 /* We update a node's contents whenever it is expanded, even if it was
417 * already populated; the effect is that contracting and expanding a node
418 * suffices to update it to the latest state on the server. */
419 choose_refill_row(path
, iter
);
420 if(!choose_suppress_set_autocollapse
) {
421 if(choose_auto_expanding
) {
422 /* This was an automatic expansion; mark it the row for auto-collapse. */
423 gtk_tree_store_set(choose_store
, iter
,
424 AUTOCOLLAPSE_COLUMN
, TRUE
,
426 /*fprintf(stderr, "enable auto-collapse for %s\n",
427 gtk_tree_path_to_string(path));*/
429 /* This was a manual expansion. Inhibit automatic collapse on this row
430 * and all its ancestors. */
433 gtk_tree_store_set(choose_store
, iter
,
434 AUTOCOLLAPSE_COLUMN
, FALSE
,
436 /*fprintf(stderr, "suppress auto-collapse for %s\n",
437 gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(choose_store),
439 GtkTreeIter child
= *iter
;
440 itv
= gtk_tree_model_iter_parent(GTK_TREE_MODEL(choose_store
),
444 /* The effect of this is that if you expand a row that's actually a
445 * sibling of the real target of the auto-expansion, it stays expanded
446 * when you clear a search. That's find and good, but it _still_ stays
447 * expanded if you expand it and then collapse it.
449 * An alternative policy would be to only auto-collapse rows that don't
450 * have any expanded children (apart from ones also subject to
451 * auto-collapse). I'm not sure what the most usable policy is.
457 static void choose_auto_collapse_callback(GtkTreeView
*tree_view
,
459 gpointer
attribute((unused
)) user_data
) {
462 gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store
), it
, path
);
463 if(choose_can_autocollapse(it
)) {
464 /*fprintf(stderr, "collapse %s\n",
465 gtk_tree_path_to_string(path));*/
466 gtk_tree_store_set(choose_store
, it
,
467 AUTOCOLLAPSE_COLUMN
, FALSE
,
469 gtk_tree_view_collapse_row(tree_view
, path
);
473 /** @brief Perform automatic collapse after a search is cleared */
474 void choose_auto_collapse(void) {
475 gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view
),
476 choose_auto_collapse_callback
,
480 /** @brief Called from choose_refill() with each expanded row */
481 static void choose_refill_callback(GtkTreeView
attribute((unused
)) *tree_view
,
483 gpointer
attribute((unused
)) user_data
) {
486 gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store
), it
, path
);
487 choose_refill_row(path
, it
);
490 /** @brief Synchronize all visible data with the server
492 * Called at startup, when a rescan completes, and via periodic_slow().
494 static void choose_refill(const char attribute((unused
)) *event
,
495 void attribute((unused
)) *eventdata
,
496 void attribute((unused
)) *callbackdata
) {
497 //fprintf(stderr, "choose_refill\n");
498 /* Update the root */
499 disorder_eclient_files(client
, choose_files_completed
, "", NULL
, NULL
);
500 disorder_eclient_dirs(client
, choose_dirs_completed
, "", NULL
, NULL
);
501 choose_list_in_flight
+= 2;
502 /* Update all expanded rows */
503 gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view
),
504 choose_refill_callback
,
506 //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight);
509 /** @brief Called for key-*-event on the main view
511 * Switches focus to the
513 static gboolean
choose_key_event(GtkWidget
attribute((unused
)) *widget
,
515 gpointer
attribute((unused
)) user_data
) {
516 /*fprintf(stderr, "choose_key_event type=%d state=%#x keyval=%#x\n",
517 event->type, event->state, event->keyval);*/
518 switch(event
->keyval
) {
525 return FALSE
; /* We'll take these */
527 /* ^F is expected to start a search. We implement this by focusing the
528 * search entry box. */
529 if((event
->state
& ~(GDK_LOCK_MASK
|GDK_SHIFT_MASK
)) == GDK_CONTROL_MASK
530 && event
->type
== GDK_KEY_PRESS
) {
531 gtk_widget_grab_focus(user_data
);
536 /* ^G is expected to go the next match. We simulate a click on the 'next'
538 if((event
->state
& ~(GDK_LOCK_MASK
|GDK_SHIFT_MASK
)) == GDK_CONTROL_MASK
539 && event
->type
== GDK_KEY_PRESS
) {
540 choose_next_clicked(0, 0);
545 gtk_widget_event(user_data
, (GdkEvent
*)event
);
549 /** @brief Create the choose tab */
550 GtkWidget
*choose_widget(void) {
551 /* Create the tree store. */
552 choose_store
= gtk_tree_store_new(CHOOSE_COLUMNS
,
563 /* Create the view */
564 choose_view
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store
));
565 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view
), TRUE
);
566 /* Suppress built-in typeahead find, we do our own search support.
567 * TODO: ^F still brings up the native search box
569 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(choose_view
), FALSE
);
571 /* Create cell renderers and columns */
572 /* TODO use a table */
574 GtkCellRenderer
*r
= gtk_cell_renderer_toggle_new();
575 GtkTreeViewColumn
*c
= gtk_tree_view_column_new_with_attributes
578 "active", STATE_COLUMN
,
579 "visible", ISFILE_COLUMN
,
581 gtk_tree_view_column_set_resizable(c
, TRUE
);
582 gtk_tree_view_column_set_reorderable(c
, TRUE
);
583 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view
), c
);
584 g_signal_connect(r
, "toggled",
585 G_CALLBACK(choose_state_toggled
), 0);
588 GtkCellRenderer
*r
= gtk_cell_renderer_text_new();
589 GtkTreeViewColumn
*c
= gtk_tree_view_column_new_with_attributes
592 "text", LENGTH_COLUMN
,
594 gtk_tree_view_column_set_resizable(c
, TRUE
);
595 gtk_tree_view_column_set_reorderable(c
, TRUE
);
596 g_object_set(r
, "xalign", (gfloat
)1.0, (char *)0);
597 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view
), c
);
600 GtkCellRenderer
*r
= gtk_cell_renderer_text_new();
601 GtkTreeViewColumn
*c
= gtk_tree_view_column_new_with_attributes
605 "background", BG_COLUMN
,
606 "foreground", FG_COLUMN
,
608 gtk_tree_view_column_set_resizable(c
, TRUE
);
609 gtk_tree_view_column_set_reorderable(c
, TRUE
);
610 g_object_set(c
, "expand", TRUE
, (char *)0);
611 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view
), c
);
612 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view
), c
);
615 /* The selection should support multiple things being selected */
616 choose_selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view
));
617 gtk_tree_selection_set_mode(choose_selection
, GTK_SELECTION_MULTIPLE
);
619 /* Catch button presses */
620 g_signal_connect(choose_view
, "button-press-event",
621 G_CALLBACK(choose_button_event
), 0);
622 g_signal_connect(choose_view
, "button-release-event",
623 G_CALLBACK(choose_button_event
), 0);
624 /* Catch row expansions so we can fill in placeholders */
625 g_signal_connect(choose_view
, "row-expanded",
626 G_CALLBACK(choose_row_expanded
), 0);
628 event_register("queue-list-changed", choose_set_state
, 0);
629 event_register("playing-track-changed", choose_set_state
, 0);
630 event_register("search-results-changed", choose_set_state
, 0);
631 event_register("lookups-completed", choose_set_state
, 0);
633 /* After a rescan we update the choose tree. We get a rescan-complete
634 * automatically at startup and upon connection too. */
635 event_register("rescan-complete", choose_refill
, 0);
637 /* Make the widget scrollable */
638 GtkWidget
*scrolled
= scroll_widget(choose_view
);
640 /* Pack vertically with the search widget */
641 GtkWidget
*vbox
= gtk_vbox_new(FALSE
/*homogenous*/, 1/*spacing*/);
642 gtk_box_pack_start(GTK_BOX(vbox
), scrolled
,
643 TRUE
/*expand*/, TRUE
/*fill*/, 0/*padding*/);
644 gtk_box_pack_end(GTK_BOX(vbox
), choose_search_widget(),
645 FALSE
/*expand*/, FALSE
/*fill*/, 0/*padding*/);
647 g_object_set_data(G_OBJECT(vbox
), "type", (void *)&choose_tabtype
);
649 /* Redirect keyboard activity to the search widget */
650 g_signal_connect(choose_view
, "key-press-event",
651 G_CALLBACK(choose_key_event
), choose_search_entry
);
652 g_signal_connect(choose_view
, "key-release-event",
653 G_CALLBACK(choose_key_event
), choose_search_entry
);