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