chiark / gitweb /
Move track sorting to its own function. Only choose_populate() uses
[disorder] / disobedience / choose.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
ebcfbfef 3 * Copyright (C) 2008 Richard Kettlewell
460b9539 4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
b9bdafc7
RK
20/** @file disobedience/choose.c
21 * @brief Hierarchical track selection and search
22 *
ebcfbfef
RK
23 * We now use an ordinary GtkTreeStore/GtkTreeView.
24 *
ebcfbfef
RK
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
a98fd571
RK
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,
29 * lengths, etc).
ebcfbfef
RK
30 *
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?)
34 *
35 * TODO:
36 * - sweep up contracted nodes
37 * - update when content may have changed (e.g. after a rescan)
a98fd571 38 * - proper sorting
b9bdafc7 39 */
460b9539 40
41#include "disobedience.h"
6982880f 42#include "choose.h"
16e145a5 43
ebcfbfef 44/** @brief The current selection tree */
6982880f 45GtkTreeStore *choose_store;
16e145a5 46
ebcfbfef 47/** @brief The view onto the selection tree */
6982880f 48GtkWidget *choose_view;
16e145a5 49
ebcfbfef 50/** @brief The selection tree's selection */
6982880f 51GtkTreeSelection *choose_selection;
460b9539 52
2a9a65e4
RK
53/** @brief Count of file listing operations in flight */
54static int choose_list_in_flight;
55
56/** @brief Count of files inserted in current batch of listing operations */
57static int choose_inserted;
58
a98fd571
RK
59static char *choose_get_string(GtkTreeIter *iter, int column) {
60 gchar *gs;
61 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
62 column, &gs,
63 -1);
64 char *s = xstrdup(gs);
65 g_free(gs);
66 return s;
6a06a735
RK
67}
68
a98fd571
RK
69char *choose_get_track(GtkTreeIter *iter) {
70 char *s = choose_get_string(iter, TRACK_COLUMN);
71 return *s ? s : 0; /* Placeholder -> NULL */
72}
73
74char *choose_get_sort(GtkTreeIter *iter) {
75 return choose_get_string(iter, SORT_COLUMN);
76}
77
78int choose_is_file(GtkTreeIter *iter) {
79 gboolean isfile;
80 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
81 ISFILE_COLUMN, &isfile,
82 -1);
83 return isfile;
84}
85
86int choose_is_dir(GtkTreeIter *iter) {
87 gboolean isfile;
88 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
89 ISFILE_COLUMN, &isfile,
90 -1);
91 if(isfile)
92 return FALSE;
93 return !choose_is_placeholder(iter);
94}
95
96int choose_is_placeholder(GtkTreeIter *iter) {
97 return choose_get_string(iter, TRACK_COLUMN)[0] == 0;
e8a26cdd
RK
98}
99
ebcfbfef
RK
100/** @brief Remove node @p it and all its children
101 * @param Iterator, updated to point to next
102 * @return True if iterator remains valid
16e145a5 103 */
ebcfbfef
RK
104static gboolean choose_remove_node(GtkTreeIter *it) {
105 GtkTreeIter child[1];
106 gboolean childv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
107 child,
108 it);
109 while(childv)
110 childv = choose_remove_node(child);
ebcfbfef
RK
111 return gtk_tree_store_remove(choose_store, it);
112}
113
ad47bd4c
RK
114/** @brief Update length and state fields */
115static gboolean choose_set_state_callback(GtkTreeModel attribute((unused)) *model,
116 GtkTreePath attribute((unused)) *path,
117 GtkTreeIter *it,
118 gpointer attribute((unused)) data) {
a98fd571
RK
119 if(choose_is_file(it)) {
120 const char *track = choose_get_track(it);
121 const long l = namepart_length(track);
ad47bd4c
RK
122 char length[64];
123 if(l > 0)
124 byte_snprintf(length, sizeof length, "%ld:%02ld", l / 60, l % 60);
125 else
126 length[0] = 0;
127 gtk_tree_store_set(choose_store, it,
128 LENGTH_COLUMN, length,
a98fd571 129 STATE_COLUMN, queued(track),
ad47bd4c 130 -1);
cfa78eaa
RK
131 if(choose_is_search_result(track))
132 gtk_tree_store_set(choose_store, it,
b96f65cf
RK
133 BG_COLUMN, SEARCH_RESULT_BG,
134 FG_COLUMN, SEARCH_RESULT_FG,
cfa78eaa
RK
135 -1);
136 else
137 gtk_tree_store_set(choose_store, it,
138 BG_COLUMN, (char *)0,
139 FG_COLUMN, (char *)0,
140 -1);
ad47bd4c
RK
141 }
142 return FALSE; /* continue walking */
143}
144
145/** @brief Called when the queue or playing track change */
146static void choose_set_state(const char attribute((unused)) *event,
147 void attribute((unused)) *eventdata,
148 void attribute((unused)) *callbackdata) {
149 gtk_tree_model_foreach(GTK_TREE_MODEL(choose_store),
150 choose_set_state_callback,
151 NULL);
152}
153
ebcfbfef
RK
154/** @brief (Re-)populate a node
155 * @param parent_ref Node to populate or NULL to fill root
156 * @param nvec Number of children to add
157 * @param vec Children
a98fd571 158 * @param files 1 if children are files, 0 if directories
b9bdafc7 159 *
ebcfbfef
RK
160 * Adjusts the set of files (or directories) below @p parent_ref to match those
161 * listed in @p nvec and @p vec.
fcc8b9f7 162 *
ebcfbfef 163 * @parent_ref will be destroyed.
fcc8b9f7 164 */
ebcfbfef
RK
165static void choose_populate(GtkTreeRowReference *parent_ref,
166 int nvec, char **vec,
a98fd571 167 int isfile) {
2fadbbc6 168 const char *type = isfile ? "track" : "dir";
ebcfbfef
RK
169 /* Compute parent_* */
170 GtkTreeIter pit[1], *parent_it;
171 GtkTreePath *parent_path;
172 if(parent_ref) {
173 parent_path = gtk_tree_row_reference_get_path(parent_ref);
174 parent_it = pit;
175 gboolean pitv = gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store),
176 pit, parent_path);
177 assert(pitv);
178 /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n",
2fadbbc6 179 type,
ebcfbfef 180 gtk_tree_path_to_string(parent_path));*/
460b9539 181 } else {
ebcfbfef
RK
182 parent_path = 0;
183 parent_it = 0;
184 /*fprintf(stderr, "choose_populate %s: populating the root\n",
2fadbbc6 185 type);*/
ebcfbfef
RK
186 }
187 /* Remove unwanted nodes and find out which we must add */
2fadbbc6
RK
188 //fprintf(stderr, " trimming unwanted %s nodes\n", type);
189 struct tracksort_data *td = tracksort_init(nvec, vec, type);
ebcfbfef
RK
190 GtkTreeIter it[1];
191 gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
192 it,
193 parent_it);
194 while(itv) {
a98fd571 195 const char *track = choose_get_track(it);
ebcfbfef
RK
196 int keep;
197
a98fd571 198 if(!track) {
ebcfbfef
RK
199 /* Always kill placeholders */
200 //fprintf(stderr, " kill a placeholder\n");
201 keep = 0;
a98fd571 202 } else if(choose_is_file(it) == isfile) {
ebcfbfef 203 /* This is the type we care about */
2fadbbc6 204 //fprintf(stderr, " %s is a %s\n", track, type);
ebcfbfef 205 int n;
2fadbbc6 206 for(n = 0; n < nvec && strcmp(td[n].track, track); ++n)
ebcfbfef
RK
207 ;
208 if(n < nvec) {
209 //fprintf(stderr, " ... and survives\n");
2fadbbc6 210 td[n].extra = td;
ebcfbfef
RK
211 keep = 1;
212 } else {
213 //fprintf(stderr, " ... and is to be removed\n");
214 keep = 0;
c3df9503 215 }
6a06a735 216 } else {
ebcfbfef 217 /* Keep wrong-type entries */
a98fd571 218 //fprintf(stderr, " %s has wrong type\n", track);
ebcfbfef 219 keep = 1;
460b9539 220 }
ebcfbfef
RK
221 if(keep)
222 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
223 else
224 itv = choose_remove_node(it);
225 }
226 /* Add nodes we don't have */
227 int inserted = 0;
2fadbbc6 228 //fprintf(stderr, " inserting new %s nodes\n", type);
ebcfbfef 229 for(int n = 0; n < nvec; ++n) {
2fadbbc6
RK
230 if(!td[n].extra) {
231 //fprintf(stderr, " %s was not found\n", td[n].track);
ebcfbfef
RK
232 gtk_tree_store_append(choose_store, it, parent_it);
233 gtk_tree_store_set(choose_store, it,
2fadbbc6 234 NAME_COLUMN, td[n].display,
a98fd571 235 ISFILE_COLUMN, isfile,
2fadbbc6
RK
236 TRACK_COLUMN, td[n].track,
237 SORT_COLUMN, td[n].sort,
ebcfbfef 238 -1);
ad47bd4c
RK
239 /* Update length and state; we expect this to kick off length lookups
240 * rather than necessarily get the right value the first time round. */
241 choose_set_state_callback(0, 0, it, 0);
ebcfbfef
RK
242 ++inserted;
243 /* If we inserted a directory, insert a placeholder too, so it appears to
244 * have children; it will be deleted when we expand the directory. */
a98fd571 245 if(!isfile) {
ebcfbfef
RK
246 //fprintf(stderr, " inserting a placeholder\n");
247 GtkTreeIter placeholder[1];
248
249 gtk_tree_store_append(choose_store, placeholder, it);
250 gtk_tree_store_set(choose_store, placeholder,
251 NAME_COLUMN, "Waddling...",
a98fd571
RK
252 TRACK_COLUMN, "",
253 ISFILE_COLUMN, FALSE,
ebcfbfef 254 -1);
e9b70a84 255 }
460b9539 256 }
460b9539 257 }
ebcfbfef
RK
258 //fprintf(stderr, " %d nodes inserted\n", inserted);
259 if(inserted) {
260 /* TODO sort the children */
33288048 261 }
ebcfbfef
RK
262 if(parent_ref) {
263 /* If we deleted a placeholder then we must re-expand the row */
264 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), parent_path, FALSE);
265 gtk_tree_row_reference_free(parent_ref);
266 gtk_tree_path_free(parent_path);
460b9539 267 }
2a9a65e4
RK
268 /* We only notify others that we've inserted tracks when there are no more
269 * insertions pending, so that they don't have to keep track of how many
270 * requests they've made. */
271 choose_inserted += inserted;
272 if(--choose_list_in_flight == 0) {
273 /* Notify interested parties that we inserted some tracks, AFTER making
274 * sure that the row is properly expanded */
275 if(choose_inserted) {
276 event_raise("choose-inserted-tracks", parent_it);
277 choose_inserted = 0;
278 }
279 }
460b9539 280}
281
ebcfbfef
RK
282static void choose_dirs_completed(void *v,
283 const char *error,
284 int nvec, char **vec) {
285 if(error) {
286 popup_protocol_error(0, error);
287 return;
460b9539 288 }
a98fd571 289 choose_populate(v, nvec, vec, 0/*!isfile*/);
460b9539 290}
291
ebcfbfef
RK
292static void choose_files_completed(void *v,
293 const char *error,
294 int nvec, char **vec) {
295 if(error) {
53fa91bb 296 popup_protocol_error(0, error);
ebcfbfef 297 return;
460b9539 298 }
a98fd571 299 choose_populate(v, nvec, vec, 1/*isfile*/);
460b9539 300}
301
a3602333
RK
302void choose_play_completed(void attribute((unused)) *v,
303 const char *error) {
304 if(error)
305 popup_protocol_error(0, error);
306}
307
308static void choose_state_toggled
309 (GtkCellRendererToggle attribute((unused)) *cell_renderer,
310 gchar *path_str,
311 gpointer attribute((unused)) user_data) {
312 GtkTreeIter it[1];
313 /* Identify the track */
314 gboolean itv =
315 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store),
316 it,
317 path_str);
318 if(!itv)
319 return;
a98fd571 320 if(!choose_is_file(it))
a3602333 321 return;
a98fd571
RK
322 const char *track = choose_get_track(it);
323 if(queued(track))
a3602333 324 return;
a98fd571 325 disorder_eclient_play(client, track, choose_play_completed, 0);
a3602333
RK
326
327}
328
ebcfbfef
RK
329static void choose_row_expanded(GtkTreeView attribute((unused)) *treeview,
330 GtkTreeIter *iter,
331 GtkTreePath *path,
460b9539 332 gpointer attribute((unused)) user_data) {
ebcfbfef
RK
333 /*fprintf(stderr, "row-expanded path=[%s]\n",
334 gtk_tree_path_to_string(path));*/
335 /* We update a node's contents whenever it is expanded, even if it was
336 * already populated; the effect is that contracting and expanding a node
337 * suffices to update it to the latest state on the server. */
a98fd571 338 const char *track = choose_get_track(iter);
ebcfbfef 339 disorder_eclient_files(client, choose_files_completed,
a98fd571 340 track,
ebcfbfef
RK
341 NULL,
342 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
343 path));
344 disorder_eclient_dirs(client, choose_dirs_completed,
a98fd571 345 track,
ebcfbfef
RK
346 NULL,
347 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
348 path));
349 /* The row references are destroyed in the _completed handlers. */
2a9a65e4 350 choose_list_in_flight += 2;
ebcfbfef
RK
351}
352
ebcfbfef 353/** @brief Create the choose tab */
460b9539 354GtkWidget *choose_widget(void) {
ebcfbfef 355 /* Create the tree store. */
a98fd571 356 choose_store = gtk_tree_store_new(CHOOSE_COLUMNS,
ad47bd4c
RK
357 G_TYPE_BOOLEAN,
358 G_TYPE_STRING,
ebcfbfef 359 G_TYPE_STRING,
a3602333 360 G_TYPE_BOOLEAN,
a98fd571 361 G_TYPE_STRING,
cfa78eaa
RK
362 G_TYPE_STRING,
363 G_TYPE_STRING,
a98fd571 364 G_TYPE_STRING);
ebcfbfef
RK
365
366 /* Create the view */
367 choose_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store));
ad47bd4c 368 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view), TRUE);
ebcfbfef
RK
369
370 /* Create cell renderers and columns */
ad47bd4c
RK
371 /* TODO use a table */
372 {
e9a72659 373 GtkCellRenderer *r = gtk_cell_renderer_toggle_new();
ad47bd4c 374 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
e9a72659 375 ("Queued",
ad47bd4c 376 r,
e9a72659
RK
377 "active", STATE_COLUMN,
378 "visible", ISFILE_COLUMN,
ad47bd4c
RK
379 (char *)0);
380 gtk_tree_view_column_set_resizable(c, TRUE);
381 gtk_tree_view_column_set_reorderable(c, TRUE);
ad47bd4c 382 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
e9a72659
RK
383 g_signal_connect(r, "toggled",
384 G_CALLBACK(choose_state_toggled), 0);
ad47bd4c
RK
385 }
386 {
387 GtkCellRenderer *r = gtk_cell_renderer_text_new();
388 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
389 ("Length",
390 r,
391 "text", LENGTH_COLUMN,
392 (char *)0);
393 gtk_tree_view_column_set_resizable(c, TRUE);
394 gtk_tree_view_column_set_reorderable(c, TRUE);
a3602333 395 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
ad47bd4c
RK
396 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
397 }
398 {
e9a72659 399 GtkCellRenderer *r = gtk_cell_renderer_text_new();
ad47bd4c 400 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
e9a72659 401 ("Track",
ad47bd4c 402 r,
e9a72659
RK
403 "text", NAME_COLUMN,
404 "background", BG_COLUMN,
405 "foreground", FG_COLUMN,
ad47bd4c
RK
406 (char *)0);
407 gtk_tree_view_column_set_resizable(c, TRUE);
408 gtk_tree_view_column_set_reorderable(c, TRUE);
e9a72659 409 g_object_set(c, "expand", TRUE, (char *)0);
ad47bd4c 410 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
e9a72659 411 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view), c);
ad47bd4c 412 }
460b9539 413
ebcfbfef
RK
414 /* The selection should support multiple things being selected */
415 choose_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view));
416 gtk_tree_selection_set_mode(choose_selection, GTK_SELECTION_MULTIPLE);
417
418 /* Catch button presses */
6982880f
RK
419 g_signal_connect(choose_view, "button-press-event",
420 G_CALLBACK(choose_button_event), 0);
421 g_signal_connect(choose_view, "button-release-event",
422 G_CALLBACK(choose_button_event), 0);
ebcfbfef
RK
423 /* Catch row expansions so we can fill in placeholders */
424 g_signal_connect(choose_view, "row-expanded",
425 G_CALLBACK(choose_row_expanded), 0);
ad47bd4c
RK
426
427 event_register("queue-list-changed", choose_set_state, 0);
428 event_register("playing-track-changed", choose_set_state, 0);
cfa78eaa 429 event_register("search-results-changed", choose_set_state, 0);
3efa1d16
RK
430 event_register("lookups-completed", choose_set_state, 0);
431
ebcfbfef
RK
432 /* Fill the root */
433 disorder_eclient_files(client, choose_files_completed, "", NULL, NULL);
434 disorder_eclient_dirs(client, choose_dirs_completed, "", NULL, NULL);
cfa78eaa 435
ebcfbfef
RK
436 /* Make the widget scrollable */
437 GtkWidget *scrolled = scroll_widget(choose_view);
cfa78eaa
RK
438
439 /* Pack vertically with the search widget */
440 GtkWidget *vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
441 gtk_box_pack_start(GTK_BOX(vbox), scrolled,
442 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
443 gtk_box_pack_end(GTK_BOX(vbox), choose_search_widget(),
444 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
445
446 g_object_set_data(G_OBJECT(vbox), "type", (void *)&choose_tabtype);
447 return vbox;
460b9539 448}
449
450/*
451Local Variables:
452c-basic-offset:2
453comment-column:40
454fill-column:79
455indent-tabs-mode:nil
456End:
457*/