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