chiark / gitweb /
Disobedience choose 'select all' now only selects tracks (and
[disorder] / disobedience / queue-generic.c
CommitLineData
c133bd3c
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2006-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/queue-generic.c
21 * @brief Queue widgets
22 *
23 * This file provides contains code shared between all the queue-like
24 * widgets - the queue, the recent list and the added tracks list.
25 *
26 * This code is in the process of being rewritten to use the native list
27 * widget.
28 *
29 * There are three @ref queuelike objects: @ref ql_queue, @ref
30 * ql_recent and @ref ql_added. Each has an associated queue linked
31 * list and a list store containing the contents.
32 *
33 * When new contents turn up we rearrange the list store accordingly.
34 *
35 * NB that while in the server the playing track is not in the queue, in
36 * Disobedience, the playing does live in @c ql_queue.q, despite its different
37 * status to everything else found in that list.
ee7552f8
RK
38 *
39 * To do:
ee7552f8 40 * - display playing row in a different color?
c133bd3c
RK
41 */
42#include "disobedience.h"
6982880f 43#include "popup.h"
c133bd3c
RK
44#include "queue-generic.h"
45
46static struct queuelike *const queuelikes[] = {
47 &ql_queue, &ql_recent, &ql_added
48};
49#define NQUEUELIKES (sizeof queuelikes / sizeof *queuelikes)
50
51/* Track detail lookup ----------------------------------------------------- */
52
53static int namepart_lookups_outstanding;
54static const struct cache_type cachetype_string = { 3600 };
55static const struct cache_type cachetype_integer = { 3600 };
56
57/** @brief Called when a namepart lookup has completed or failed
58 *
59 * When there are no lookups in flight a redraw is provoked. This might well
60 * provoke further lookups.
61 */
62static void namepart_completed_or_failed(void) {
63 --namepart_lookups_outstanding;
c133bd3c
RK
64 if(!namepart_lookups_outstanding) {
65 /* There are no more lookups outstanding, so we update the display */
66 for(unsigned n = 0; n < NQUEUELIKES; ++n)
67 ql_update_list_store(queuelikes[n]);
68 }
69}
70
71/** @brief Called when a namepart lookup has completed */
72static void namepart_completed(void *v, const char *error, const char *value) {
73 D(("namepart_completed"));
74 if(error) {
75 gtk_label_set_text(GTK_LABEL(report_label), error);
76 value = "?";
77 }
78 const char *key = v;
79
80 cache_put(&cachetype_string, key, value);
81 namepart_completed_or_failed();
82}
83
84/** @brief Called when a length lookup has completed */
85static void length_completed(void *v, const char *error, long l) {
86 D(("length_completed"));
87 if(error) {
88 gtk_label_set_text(GTK_LABEL(report_label), error);
89 l = -1;
90 }
91 const char *key = v;
92 long *value;
93
94 D(("namepart_completed"));
95 value = xmalloc(sizeof *value);
96 *value = l;
97 cache_put(&cachetype_integer, key, value);
98 namepart_completed_or_failed();
99}
100
101/** @brief Arrange to fill in a namepart cache entry */
102static void namepart_fill(const char *track,
103 const char *context,
104 const char *part,
105 const char *key) {
106 D(("namepart_fill %s %s %s %s", track, context, part, key));
107 /* We limit the total number of lookups in flight */
108 ++namepart_lookups_outstanding;
109 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
110 disorder_eclient_namepart(client, namepart_completed,
111 track, context, part, (void *)key);
112}
113
114/** @brief Look up a namepart
115 * @param track Track name
116 * @param context Context
117 * @param part Name part
118 * @param lookup If nonzero, will schedule a lookup for unknown values
119 *
120 * If it is in the cache then just return its value. If not then look it up
121 * and arrange for the queues to be updated when its value is available. */
122static const char *namepart(const char *track,
123 const char *context,
124 const char *part) {
125 char *key;
126 const char *value;
127
128 D(("namepart %s %s %s", track, context, part));
129 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
130 context, part, track);
131 value = cache_get(&cachetype_string, key);
132 if(!value) {
133 D(("deferring..."));
134 namepart_fill(track, context, part, key);
135 value = "?";
136 }
137 return value;
138}
139
140/** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
141void namepart_update(const char *track,
142 const char *context,
143 const char *part) {
144 char *key;
145
146 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
147 context, part, track);
148 /* Only refetch if it's actually in the cache. */
149 if(cache_get(&cachetype_string, key))
150 namepart_fill(track, context, part, key);
151}
152
153/** @brief Look up a track length
154 *
155 * If it is in the cache then just return its value. If not then look it up
156 * and arrange for the queues to be updated when its value is available. */
157static long getlength(const char *track) {
158 char *key;
159 const long *value;
160
161 D(("getlength %s", track));
162 byte_xasprintf(&key, "length track=%s", track);
163 value = cache_get(&cachetype_integer, key);
164 if(value)
165 return *value;
166 D(("deferring..."));;
167 ++namepart_lookups_outstanding;
168 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
169 disorder_eclient_length(client, length_completed, track, key);
170 return -1;
171}
172
173/* Column formatting -------------------------------------------------------- */
174
175/** @brief Format the 'when' column */
176const char *column_when(const struct queue_entry *q,
177 const char attribute((unused)) *data) {
178 char when[64];
179 struct tm tm;
180 time_t t;
181
182 D(("column_when"));
183 switch(q->state) {
184 case playing_isscratch:
185 case playing_unplayed:
186 case playing_random:
187 t = q->expected;
188 break;
189 case playing_failed:
190 case playing_no_player:
191 case playing_ok:
192 case playing_scratched:
193 case playing_started:
194 case playing_paused:
195 case playing_quitting:
196 t = q->played;
197 break;
198 default:
199 t = 0;
200 break;
201 }
202 if(t)
203 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
204 else
205 when[0] = 0;
206 return xstrdup(when);
207}
208
209/** @brief Format the 'who' column */
210const char *column_who(const struct queue_entry *q,
211 const char attribute((unused)) *data) {
212 D(("column_who"));
213 return q->submitter ? q->submitter : "";
214}
215
216/** @brief Format one of the track name columns */
217const char *column_namepart(const struct queue_entry *q,
22717074 218 const char *data) {
c133bd3c
RK
219 D(("column_namepart"));
220 return namepart(q->track, "display", data);
221}
222
223/** @brief Format the length column */
224const char *column_length(const struct queue_entry *q,
225 const char attribute((unused)) *data) {
226 D(("column_length"));
227 long l;
228 time_t now;
229 char *played = 0, *length = 0;
230
231 /* Work out what to say for the length */
232 l = getlength(q->track);
233 if(l > 0)
234 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
235 else
236 byte_xasprintf(&length, "?:??");
237 /* For the currently playing track we want to report how much of the track
238 * has been played */
239 if(q == playing_track) {
240 /* log_state() arranges that we re-get the playing data whenever the
241 * pause/resume state changes */
242 if(last_state & DISORDER_TRACK_PAUSED)
243 l = playing_track->sofar;
244 else {
245 time(&now);
246 l = playing_track->sofar + (now - last_playing);
247 }
248 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
249 return played;
250 } else
251 return length;
252}
253
c133bd3c
RK
254/* List store maintenance -------------------------------------------------- */
255
22717074 256/** @brief Return the @ref queue_entry corresponding to @p iter
83fb99f9 257 * @param model Model that owns @p iter
22717074
RK
258 * @param iter Tree iterator
259 * @return ID string
260 */
83fb99f9 261struct queue_entry *ql_iter_to_q(GtkTreeModel *model,
22717074 262 GtkTreeIter *iter) {
83fb99f9 263 struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql");
22717074
RK
264 GValue v[1];
265 memset(v, 0, sizeof v);
83fb99f9 266 gtk_tree_model_get_value(model, iter, ql->ncolumns, v);
22717074
RK
267 assert(G_VALUE_TYPE(v) == G_TYPE_POINTER);
268 struct queue_entry *const q = g_value_get_pointer(v);
269 g_value_unset(v);
270 return q;
271}
272
c133bd3c
RK
273/** @brief Update one row of a list store
274 * @param q Queue entry
275 * @param iter Iterator referring to row or NULL to work it out
276 */
277void ql_update_row(struct queue_entry *q,
278 GtkTreeIter *iter) {
279 const struct queuelike *const ql = q->ql;
280
281 D(("ql_update_row"));
282 /* If no iter was supplied, work it out */
283 GtkTreeIter my_iter[1];
284 if(!iter) {
285 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter);
286 struct queue_entry *qq;
287 for(qq = ql->q; qq && q != qq; qq = qq->next)
288 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter);
289 if(!qq)
290 return;
291 iter = my_iter;
292 }
293 /* Update all the columns */
294 for(int col = 0; col < ql->ncolumns; ++col)
295 gtk_list_store_set(ql->store, iter,
296 col, ql->columns[col].value(q,
297 ql->columns[col].data),
298 -1);
22717074
RK
299 /* The hidden extra column is the queue entry */
300 gtk_list_store_set(ql->store, iter, ql->ncolumns, q, -1);
c133bd3c
RK
301}
302
303/** @brief Update the list store
304 * @param ql Queuelike to update
305 *
306 * Called when new namepart data is available (and initially). Doesn't change
307 * the rows, just updates the cell values.
308 */
309void ql_update_list_store(struct queuelike *ql) {
310 D(("ql_update_list_store"));
311 GtkTreeIter iter[1];
312 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
313 for(struct queue_entry *q = ql->q; q; q = q->next) {
314 ql_update_row(q, iter);
315 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
316 }
317}
318
83fb99f9
RK
319struct newqueue_data {
320 struct queue_entry *old, *new;
321};
322
323static void record_queue_map(hash *h,
324 const char *id,
325 struct queue_entry *old,
326 struct queue_entry *new) {
327 struct newqueue_data *nqd;
328
329 if(!(nqd = hash_find(h, id))) {
330 static const struct newqueue_data empty[1];
331 hash_add(h, id, empty, HASH_INSERT);
332 nqd = hash_find(h, id);
333 }
334 if(old)
335 nqd->old = old;
336 if(new)
337 nqd->new = new;
338}
339
340#if 0
341static void dump_queue(struct queue_entry *head, struct queue_entry *mark) {
342 for(struct queue_entry *q = head; q; q = q->next) {
343 if(q == mark)
344 fprintf(stderr, "!");
345 fprintf(stderr, "%s", q->id);
346 if(q->next)
347 fprintf(stderr, " ");
348 }
349 fprintf(stderr, "\n");
350}
351
352static void dump_rows(struct queuelike *ql) {
353 GtkTreeIter iter[1];
354 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
355 iter);
356 while(it) {
357 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
358 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
359 fprintf(stderr, "%s", q->id);
360 if(it)
361 fprintf(stderr, " ");
362 }
363 fprintf(stderr, "\n");
364}
365#endif
366
c133bd3c
RK
367/** @brief Reset the list store
368 * @param ql Queuelike to reset
83fb99f9 369 * @param newq New queue contents/ordering
c133bd3c 370 *
83fb99f9 371 * Updates the queue to match @p newq
c133bd3c
RK
372 */
373void ql_new_queue(struct queuelike *ql,
374 struct queue_entry *newq) {
375 D(("ql_new_queue"));
83fb99f9
RK
376 ++suppress_actions;
377
378 /* Tell every queue entry which queue owns it */
379 //fprintf(stderr, "%s: filling in q->ql\n", ql->name);
380 for(struct queue_entry *q = newq; q; q = q->next)
c133bd3c 381 q->ql = ql;
83fb99f9
RK
382
383 //fprintf(stderr, "%s: constructing h\n", ql->name);
384 /* Construct map from id to new and old structures */
385 hash *h = hash_new(sizeof(struct newqueue_data));
386 for(struct queue_entry *q = ql->q; q; q = q->next)
387 record_queue_map(h, q->id, q, NULL);
388 for(struct queue_entry *q = newq; q; q = q->next)
389 record_queue_map(h, q->id, NULL, q);
390
391 /* The easy bit: delete rows not present any more. In the same pass we
392 * update the secret column containing the queue_entry pointer. */
393 //fprintf(stderr, "%s: deleting rows...\n", ql->name);
394 GtkTreeIter iter[1];
395 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
396 iter);
397 int inserted = 0, deleted = 0, kept = 0;
398 while(it) {
399 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
400 const struct newqueue_data *nqd = hash_find(h, q->id);
401 if(nqd->new) {
402 /* Tell this row that it belongs to the new version of the queue */
403 gtk_list_store_set(ql->store, iter, ql->ncolumns, nqd->new, -1);
404 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
405 ++kept;
406 } else {
407 /* Delete this row (and move iter to the next one) */
408 //fprintf(stderr, " delete %s", q->id);
409 it = gtk_list_store_remove(ql->store, iter);
410 ++deleted;
411 }
c133bd3c 412 }
83fb99f9
RK
413
414 /* Now every row's secret column is right, but we might be missing new rows
415 * and they might be in the wrong order */
416
417 /* We're going to have to support arbitrary rearrangements, so we might as
418 * well add new elements at the end. */
419 //fprintf(stderr, "%s: adding rows...\n", ql->name);
420 struct queue_entry *after = 0;
421 for(struct queue_entry *q = newq; q; q = q->next) {
422 const struct newqueue_data *nqd = hash_find(h, q->id);
423 if(!nqd->old) {
424 GtkTreeIter iter[1];
425 if(after) {
426 /* Try to insert at the right sort of place */
427 GtkTreeIter where[1];
428 gboolean wit = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
429 where);
430 while(wit && ql_iter_to_q(GTK_TREE_MODEL(ql->store), where) != after)
431 wit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), where);
432 if(wit)
433 gtk_list_store_insert_after(ql->store, iter, where);
434 else
435 gtk_list_store_append(ql->store, iter);
436 } else
437 gtk_list_store_prepend(ql->store, iter);
438 gtk_list_store_set(ql->store, iter, ql->ncolumns, q, -1);
439 //fprintf(stderr, " add %s", q->id);
440 ++inserted;
441 }
442 after = newq;
443 }
444
445 /* Now exactly the right set of rows are present, and they have the right
446 * queue_entry pointers in their secret column, but they may be in the wrong
447 * order.
448 *
449 * The current code is simple but amounts to a bubble-sort - we might easily
450 * called gtk_tree_model_iter_next a couple of thousand times.
451 */
452 //fprintf(stderr, "%s: rearranging rows\n", ql->name);
453 //fprintf(stderr, "%s: queue state: ", ql->name);
454 //dump_queue(newq, 0);
455 //fprintf(stderr, "%s: row state: ", ql->name);
456 //dump_rows(ql);
457 it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
458 iter);
459 struct queue_entry *rq = newq; /* r for 'right, correct' */
460 int swaps = 0, searches = 0;
461 while(it) {
462 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
463 //fprintf(stderr, " rq = %p, q = %p\n", rq, q);
464 //fprintf(stderr, " rq->id = %s, q->id = %s\n", rq->id, q->id);
465
466 if(q != rq) {
467 //fprintf(stderr, " mismatch\n");
468 GtkTreeIter next[1] = { *iter };
469 gboolean nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
470 while(nit) {
471 struct queue_entry *nq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), next);
472 //fprintf(stderr, " candidate: %s\n", nq->id);
473 if(nq == rq)
474 break;
475 nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
476 ++searches;
477 }
478 assert(nit);
479 //fprintf(stderr, " found it\n");
480 gtk_list_store_swap(ql->store, iter, next);
481 *iter = *next;
482 //fprintf(stderr, "%s: new row state: ", ql->name);
483 //dump_rows(ql);
484 ++swaps;
485 }
486 /* ...and onto the next one */
487 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
488 rq = rq->next;
489 }
490#if 0
491 fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name,
492 kept, inserted, deleted, swaps, searches);
493#endif
494 //fprintf(stderr, "done\n");
495 ql->q = newq;
496 /* Set the rest of the columns in new rows */
497 ql_update_list_store(ql);
83fb99f9 498 --suppress_actions;
c133bd3c
RK
499}
500
501/** @brief Initialize a @ref queuelike */
502GtkWidget *init_queuelike(struct queuelike *ql) {
503 D(("init_queuelike"));
22717074
RK
504 /* Create the list store. We add an extra column to hold the ID. */
505 GType *types = xcalloc(ql->ncolumns + 1, sizeof (GType));
c133bd3c
RK
506 for(int n = 0; n < ql->ncolumns; ++n)
507 types[n] = G_TYPE_STRING;
22717074
RK
508 types[ql->ncolumns] = G_TYPE_POINTER;
509 ql->store = gtk_list_store_newv(ql->ncolumns + 1, types);
510 g_object_set_data(G_OBJECT(ql->store), "ql", (void *)ql);
c133bd3c
RK
511
512 /* Create the view */
513 ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store));
1583d68f 514 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql->view), TRUE);
c133bd3c
RK
515
516 /* Create cell renderers and label columns */
517 for(int n = 0; n < ql->ncolumns; ++n) {
518 GtkCellRenderer *r = gtk_cell_renderer_text_new();
b0b15b7c
RK
519 if(ql->columns[n].flags & COL_ELLIPSIZE)
520 g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_END, (char *)0);
521 if(ql->columns[n].flags & COL_RIGHT)
522 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
c133bd3c
RK
523 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
524 (ql->columns[n].name,
525 r,
526 "text", n,
527 (char *)0);
3ffb8e5d
RK
528 gtk_tree_view_column_set_resizable(c, TRUE);
529 gtk_tree_view_column_set_reorderable(c, TRUE);
b0b15b7c
RK
530 if(ql->columns[n].flags & COL_EXPAND)
531 g_object_set(c, "expand", TRUE, (char *)0);
c133bd3c
RK
532 gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c);
533 }
534
535 /* The selection should support multiple things being selected */
536 ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view));
537 gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE);
538
c133bd3c 539 /* Catch button presses */
17c36802 540 g_signal_connect(ql->view, "button-press-event",
c133bd3c
RK
541 G_CALLBACK(ql_button_release), ql);
542
543 /* TODO style? */
c133bd3c
RK
544
545 ql->init();
546
ee7552f8
RK
547 GtkWidget *scrolled = scroll_widget(ql->view);
548 g_object_set_data(G_OBJECT(scrolled), "type", (void *)ql_tabtype(ql));
549 return scrolled;
c133bd3c
RK
550}
551
552/*
553Local Variables:
554c-basic-offset:2
555comment-column:40
556fill-column:79
557indent-tabs-mode:nil
558End:
559*/