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"
43 /** @brief The current selection tree */
44 GtkTreeStore
*choose_store
;
46 /** @brief The view onto the selection tree */
47 GtkWidget
*choose_view
;
49 /** @brief The selection tree's selection */
50 GtkTreeSelection
*choose_selection
;
52 /** @brief Count of file listing operations in flight */
53 static int choose_list_in_flight
;
55 /** @brief If nonzero autocollapse column won't be set */
56 static int choose_suppress_set_autocollapse
;
58 static char *choose_get_string(GtkTreeIter
*iter
, int column
) {
60 gtk_tree_model_get(GTK_TREE_MODEL(choose_store
), iter
,
63 char *s
= xstrdup(gs
);
68 char *choose_get_track(GtkTreeIter
*iter
) {
69 char *s
= choose_get_string(iter
, TRACK_COLUMN
);
70 return *s ? s
: 0; /* Placeholder -> NULL */
73 char *choose_get_sort(GtkTreeIter
*iter
) {
74 return choose_get_string(iter
, SORT_COLUMN
);
77 char *choose_get_display(GtkTreeIter
*iter
) {
78 return choose_get_string(iter
, NAME_COLUMN
);
81 int choose_is_file(GtkTreeIter
*iter
) {
83 gtk_tree_model_get(GTK_TREE_MODEL(choose_store
), iter
,
84 ISFILE_COLUMN
, &isfile
,
89 int choose_is_dir(GtkTreeIter
*iter
) {
91 gtk_tree_model_get(GTK_TREE_MODEL(choose_store
), iter
,
92 ISFILE_COLUMN
, &isfile
,
96 return !choose_is_placeholder(iter
);
99 int choose_is_placeholder(GtkTreeIter
*iter
) {
100 return choose_get_string(iter
, TRACK_COLUMN
)[0] == 0;
103 int choose_can_autocollapse(GtkTreeIter
*iter
) {
104 gboolean autocollapse
;
105 gtk_tree_model_get(GTK_TREE_MODEL(choose_store
), iter
,
106 AUTOCOLLAPSE_COLUMN
, &autocollapse
,
111 /** @brief Remove node @p it and all its children
112 * @param Iterator, updated to point to next
113 * @return True if iterator remains valid
115 * TODO is this necessary? gtk_tree_store_remove() does not document what
116 * happens to children.
118 static gboolean
choose_remove_node(GtkTreeIter
*it
) {
119 GtkTreeIter child
[1];
120 gboolean childv
= gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store
),
124 childv
= choose_remove_node(child
);
125 return gtk_tree_store_remove(choose_store
, it
);
128 /** @brief Update length and state fields */
129 static gboolean
choose_set_state_callback(GtkTreeModel
attribute((unused
)) *model
,
130 GtkTreePath
attribute((unused
)) *path
,
132 gpointer
attribute((unused
)) data
) {
133 if(choose_is_file(it
)) {
134 const char *track
= choose_get_track(it
);
135 const long l
= namepart_length(track
);
138 byte_snprintf(length
, sizeof length
, "%ld:%02ld", l
/ 60, l
% 60);
141 gtk_tree_store_set(choose_store
, it
,
142 LENGTH_COLUMN
, length
,
143 STATE_COLUMN
, queued(track
),
145 if(choose_is_search_result(track
))
146 gtk_tree_store_set(choose_store
, it
,
147 BG_COLUMN
, SEARCH_RESULT_BG
,
148 FG_COLUMN
, SEARCH_RESULT_FG
,
151 gtk_tree_store_set(choose_store
, it
,
152 BG_COLUMN
, (char *)0,
153 FG_COLUMN
, (char *)0,
156 return FALSE
; /* continue walking */
159 /** @brief Called when the queue or playing track change */
160 static void choose_set_state(const char attribute((unused
)) *event
,
161 void attribute((unused
)) *eventdata
,
162 void attribute((unused
)) *callbackdata
) {
163 gtk_tree_model_foreach(GTK_TREE_MODEL(choose_store
),
164 choose_set_state_callback
,
168 /** @brief (Re-)populate a node
169 * @param parent_ref Node to populate or NULL to fill root
170 * @param nvec Number of children to add
171 * @param vec Children
172 * @param files 1 if children are files, 0 if directories
174 * Adjusts the set of files (or directories) below @p parent_ref to match those
175 * listed in @p nvec and @p vec.
177 * @parent_ref will be destroyed.
179 static void choose_populate(GtkTreeRowReference
*parent_ref
,
180 int nvec
, char **vec
,
182 const char *type
= isfile ?
"track" : "dir";
183 //fprintf(stderr, "%d new children of type %s\n", nvec, type);
186 /* Compute parent_* */
187 GtkTreeIter pit
[1], *parent_it
;
188 GtkTreePath
*parent_path
;
190 parent_path
= gtk_tree_row_reference_get_path(parent_ref
);
192 gboolean pitv
= gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store
),
195 /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n",
197 gtk_tree_path_to_string(parent_path));*/
201 /*fprintf(stderr, "choose_populate %s: populating the root\n",
204 /* Both td[] and the current node set are sorted so we can do a single linear
205 * pass to insert new nodes and remove unwanted ones. The total performance
206 * may be worse than linear depending on the performance of GTK+'s insert and
207 * delete operations. */
208 //fprintf(stderr, "sorting tracks\n");
209 struct tracksort_data
*td
= tracksort_init(nvec
, vec
, type
);
211 gboolean itv
= gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store
),
214 int inserted
= 0, deleted_placeholder
= 0;
215 //fprintf(stderr, "inserting tracks type=%s\n", type);
216 while(nvec
> 0 || itv
) {
217 /*fprintf(stderr, "td[] = %s, it=%s [%s]\n",
218 nvec > 0 ? td->track : "(none)",
219 itv ? choose_get_track(it) : "(!itv)",
220 itv ? (choose_is_file(it) ? "file" : "dir") : "");*/
221 enum { INSERT
, DELETE
, SKIP_TREE
, SKIP_BOTH
} action
;
222 const char *track
= itv ?
choose_get_track(it
) : 0;
224 //fprintf(stderr, " placeholder\n");
226 ++deleted_placeholder
;
227 } else if(nvec
> 0 && itv
) {
228 /* There's both a tree row and a td[] entry */
229 const int cmp
= compare_tracks(td
->sort
, choose_get_sort(it
),
230 td
->display
, choose_get_display(it
),
232 //fprintf(stderr, " cmp=%d\n", cmp);
234 /* td < it, so we insert td before it */
237 /* td > it, so we must either delete it (if the same type) or skip it */
238 if(choose_is_file(it
) == isfile
)
243 /* td = it, so we step past both */
245 } else if(nvec
> 0) {
246 /* We've reached the end of the tree rows, but new have tracks left in
248 //fprintf(stderr, " inserting\n");
251 /* We've reached the end of the new tracks from td[], but there are
252 * further tracks in the tree */
253 //fprintf(stderr, " deleting\n");
254 if(choose_is_file(it
) == isfile
)
262 //fprintf(stderr, " INSERT %s\n", td->track);
263 /* Insert a new row from td[] before it, or at the end if it is no longer
265 GtkTreeIter child
[1];
266 gtk_tree_store_insert_before(choose_store
,
268 parent_it
, /* parent */
269 itv ? it
: NULL
); /* successor */
270 gtk_tree_store_set(choose_store
, child
,
271 NAME_COLUMN
, td
->display
,
272 ISFILE_COLUMN
, isfile
,
273 TRACK_COLUMN
, td
->track
,
274 SORT_COLUMN
, td
->sort
,
275 AUTOCOLLAPSE_COLUMN
, FALSE
,
277 /* Update length and state; we expect this to kick off length lookups
278 * rather than necessarily get the right value the first time round. */
279 choose_set_state_callback(0, 0, child
, 0);
280 /* If we inserted a directory, insert a placeholder too, so it appears to
281 * have children; it will be deleted when we expand the directory. */
283 //fprintf(stderr, " inserting a placeholder\n");
284 GtkTreeIter placeholder
[1];
286 gtk_tree_store_append(choose_store
, placeholder
, child
);
287 gtk_tree_store_set(choose_store
, placeholder
,
288 NAME_COLUMN
, "Waddling...",
290 ISFILE_COLUMN
, FALSE
,
299 //fprintf(stderr, " SKIP_BOTH\n");
304 //fprintf(stderr, " SKIP_TREE\n");
305 itv
= gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store
), it
);
308 //fprintf(stderr, " DELETE\n");
309 itv
= choose_remove_node(it
);
313 /*fprintf(stderr, "inserted=%d deleted_placeholder=%d\n\n",
314 inserted, deleted_placeholder);*/
316 /* If we deleted a placeholder then we must re-expand the row */
317 if(deleted_placeholder
) {
318 ++choose_suppress_set_autocollapse
;
319 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view
), parent_path
, FALSE
);
320 --choose_suppress_set_autocollapse
;
322 gtk_tree_row_reference_free(parent_ref
);
323 gtk_tree_path_free(parent_path
);
326 /* We only notify others that we've inserted tracks when there are no more
327 * insertions pending, so that they don't have to keep track of how many
328 * requests they've made. */
329 if(--choose_list_in_flight
== 0) {
330 /* Notify interested parties that we inserted some tracks, AFTER making
331 * sure that the row is properly expanded */
332 //fprintf(stderr, "raising choose-more-tracks\n");
333 event_raise("choose-more-tracks", 0);
335 //fprintf(stderr, "choose_list_in_flight -> %d-\n", choose_list_in_flight);
338 static void choose_dirs_completed(void *v
,
340 int nvec
, char **vec
) {
342 popup_protocol_error(0, error
);
345 choose_populate(v
, nvec
, vec
, 0/*!isfile*/);
348 static void choose_files_completed(void *v
,
350 int nvec
, char **vec
) {
352 popup_protocol_error(0, error
);
355 choose_populate(v
, nvec
, vec
, 1/*isfile*/);
358 void choose_play_completed(void attribute((unused
)) *v
,
361 popup_protocol_error(0, error
);
364 static void choose_state_toggled
365 (GtkCellRendererToggle
attribute((unused
)) *cell_renderer
,
367 gpointer
attribute((unused
)) user_data
) {
369 /* Identify the track */
371 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store
),
376 if(!choose_is_file(it
))
378 const char *track
= choose_get_track(it
);
381 disorder_eclient_play(client
, track
, choose_play_completed
, 0);
385 static void choose_row_expanded(GtkTreeView
attribute((unused
)) *treeview
,
388 gpointer
attribute((unused
)) user_data
) {
389 /*fprintf(stderr, "row-expanded path=[%s]\n\n",
390 gtk_tree_path_to_string(path));*/
391 /* We update a node's contents whenever it is expanded, even if it was
392 * already populated; the effect is that contracting and expanding a node
393 * suffices to update it to the latest state on the server. */
394 const char *track
= choose_get_track(iter
);
395 disorder_eclient_files(client
, choose_files_completed
,
398 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store
),
400 disorder_eclient_dirs(client
, choose_dirs_completed
,
403 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store
),
405 /* The row references are destroyed in the _completed handlers. */
406 choose_list_in_flight
+= 2;
407 //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight);
408 if(!choose_suppress_set_autocollapse
) {
409 if(choose_auto_expanding
) {
410 /* This was an automatic expansion; mark it the row for auto-collapse. */
411 gtk_tree_store_set(choose_store
, iter
,
412 AUTOCOLLAPSE_COLUMN
, TRUE
,
414 /*fprintf(stderr, "enable auto-collapse for %s\n",
415 gtk_tree_path_to_string(path));*/
417 /* This was a manual expansion. Inhibit automatic collapse on this row
418 * and all its ancestors. */
421 gtk_tree_store_set(choose_store
, iter
,
422 AUTOCOLLAPSE_COLUMN
, FALSE
,
424 /*fprintf(stderr, "suppress auto-collapse for %s\n",
425 gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(choose_store),
427 GtkTreeIter child
= *iter
;
428 itv
= gtk_tree_model_iter_parent(GTK_TREE_MODEL(choose_store
),
432 /* The effect of this is that if you expand a row that's actually a
433 * sibling of the real target of the auto-expansion, it stays expanded
434 * when you clear a search. That's find and good, but it _still_ stays
435 * expanded if you expand it and then collapse it.
437 * An alternative policy would be to only auto-collapse rows that don't
438 * have any expanded children (apart from ones also subject to
439 * auto-collapse). I'm not sure what the most usable policy is.
445 static void choose_auto_collapse_callback(GtkTreeView
*tree_view
,
447 gpointer
attribute((unused
)) user_data
) {
450 gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store
), it
, path
);
451 if(choose_can_autocollapse(it
)) {
452 /*fprintf(stderr, "collapse %s\n",
453 gtk_tree_path_to_string(path));*/
454 gtk_tree_store_set(choose_store
, it
,
455 AUTOCOLLAPSE_COLUMN
, FALSE
,
457 gtk_tree_view_collapse_row(tree_view
, path
);
461 /** @brief Perform automatic collapse after a search is cleared */
462 void choose_auto_collapse(void) {
463 gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view
),
464 choose_auto_collapse_callback
,
468 /** @brief Create the choose tab */
469 GtkWidget
*choose_widget(void) {
470 /* Create the tree store. */
471 choose_store
= gtk_tree_store_new(CHOOSE_COLUMNS
,
482 /* Create the view */
483 choose_view
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store
));
484 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view
), TRUE
);
485 /* Suppress built-in typeahead find, we do our own search support.
486 * TODO: ^F still brings up the native search box
488 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(choose_view
), FALSE
);
490 /* Create cell renderers and columns */
491 /* TODO use a table */
493 GtkCellRenderer
*r
= gtk_cell_renderer_toggle_new();
494 GtkTreeViewColumn
*c
= gtk_tree_view_column_new_with_attributes
497 "active", STATE_COLUMN
,
498 "visible", ISFILE_COLUMN
,
500 gtk_tree_view_column_set_resizable(c
, TRUE
);
501 gtk_tree_view_column_set_reorderable(c
, TRUE
);
502 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view
), c
);
503 g_signal_connect(r
, "toggled",
504 G_CALLBACK(choose_state_toggled
), 0);
507 GtkCellRenderer
*r
= gtk_cell_renderer_text_new();
508 GtkTreeViewColumn
*c
= gtk_tree_view_column_new_with_attributes
511 "text", LENGTH_COLUMN
,
513 gtk_tree_view_column_set_resizable(c
, TRUE
);
514 gtk_tree_view_column_set_reorderable(c
, TRUE
);
515 g_object_set(r
, "xalign", (gfloat
)1.0, (char *)0);
516 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view
), c
);
519 GtkCellRenderer
*r
= gtk_cell_renderer_text_new();
520 GtkTreeViewColumn
*c
= gtk_tree_view_column_new_with_attributes
524 "background", BG_COLUMN
,
525 "foreground", FG_COLUMN
,
527 gtk_tree_view_column_set_resizable(c
, TRUE
);
528 gtk_tree_view_column_set_reorderable(c
, TRUE
);
529 g_object_set(c
, "expand", TRUE
, (char *)0);
530 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view
), c
);
531 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view
), c
);
534 /* The selection should support multiple things being selected */
535 choose_selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view
));
536 gtk_tree_selection_set_mode(choose_selection
, GTK_SELECTION_MULTIPLE
);
538 /* Catch button presses */
539 g_signal_connect(choose_view
, "button-press-event",
540 G_CALLBACK(choose_button_event
), 0);
541 g_signal_connect(choose_view
, "button-release-event",
542 G_CALLBACK(choose_button_event
), 0);
543 /* Catch row expansions so we can fill in placeholders */
544 g_signal_connect(choose_view
, "row-expanded",
545 G_CALLBACK(choose_row_expanded
), 0);
547 event_register("queue-list-changed", choose_set_state
, 0);
548 event_register("playing-track-changed", choose_set_state
, 0);
549 event_register("search-results-changed", choose_set_state
, 0);
550 event_register("lookups-completed", choose_set_state
, 0);
553 disorder_eclient_files(client
, choose_files_completed
, "", NULL
, NULL
);
554 disorder_eclient_dirs(client
, choose_dirs_completed
, "", NULL
, NULL
);
555 choose_list_in_flight
+= 2;
556 //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight);
558 /* Make the widget scrollable */
559 GtkWidget
*scrolled
= scroll_widget(choose_view
);
561 /* Pack vertically with the search widget */
562 GtkWidget
*vbox
= gtk_vbox_new(FALSE
/*homogenous*/, 1/*spacing*/);
563 gtk_box_pack_start(GTK_BOX(vbox
), scrolled
,
564 TRUE
/*expand*/, TRUE
/*fill*/, 0/*padding*/);
565 gtk_box_pack_end(GTK_BOX(vbox
), choose_search_widget(),
566 FALSE
/*expand*/, FALSE
/*fill*/, 0/*padding*/);
568 g_object_set_data(G_OBJECT(vbox
), "type", (void *)&choose_tabtype
);