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