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