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