chiark / gitweb /
Playing checkbox in Disobedience choose tab is now only visible for
[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
ad47bd4c
RK
53static void queue_lookups_completed(const char attribute((unused)) *event,
54 void attribute((unused)) *eventdata,
55 void *callbackdata) {
56 struct queuelike *ql = callbackdata;
57 ql_update_list_store(ql);
c133bd3c
RK
58}
59
60/* Column formatting -------------------------------------------------------- */
61
62/** @brief Format the 'when' column */
63const char *column_when(const struct queue_entry *q,
64 const char attribute((unused)) *data) {
65 char when[64];
66 struct tm tm;
67 time_t t;
68
69 D(("column_when"));
70 switch(q->state) {
71 case playing_isscratch:
72 case playing_unplayed:
73 case playing_random:
74 t = q->expected;
75 break;
76 case playing_failed:
77 case playing_no_player:
78 case playing_ok:
79 case playing_scratched:
80 case playing_started:
81 case playing_paused:
82 case playing_quitting:
83 t = q->played;
84 break;
85 default:
86 t = 0;
87 break;
88 }
89 if(t)
90 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
91 else
92 when[0] = 0;
93 return xstrdup(when);
94}
95
96/** @brief Format the 'who' column */
97const char *column_who(const struct queue_entry *q,
98 const char attribute((unused)) *data) {
99 D(("column_who"));
100 return q->submitter ? q->submitter : "";
101}
102
103/** @brief Format one of the track name columns */
104const char *column_namepart(const struct queue_entry *q,
22717074 105 const char *data) {
c133bd3c
RK
106 D(("column_namepart"));
107 return namepart(q->track, "display", data);
108}
109
110/** @brief Format the length column */
111const char *column_length(const struct queue_entry *q,
112 const char attribute((unused)) *data) {
113 D(("column_length"));
114 long l;
115 time_t now;
116 char *played = 0, *length = 0;
117
118 /* Work out what to say for the length */
ad47bd4c 119 l = namepart_length(q->track);
c133bd3c
RK
120 if(l > 0)
121 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
122 else
123 byte_xasprintf(&length, "?:??");
124 /* For the currently playing track we want to report how much of the track
125 * has been played */
126 if(q == playing_track) {
127 /* log_state() arranges that we re-get the playing data whenever the
128 * pause/resume state changes */
129 if(last_state & DISORDER_TRACK_PAUSED)
130 l = playing_track->sofar;
131 else {
132 time(&now);
133 l = playing_track->sofar + (now - last_playing);
134 }
135 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
136 return played;
137 } else
138 return length;
139}
140
c133bd3c
RK
141/* List store maintenance -------------------------------------------------- */
142
22717074 143/** @brief Return the @ref queue_entry corresponding to @p iter
83fb99f9 144 * @param model Model that owns @p iter
22717074
RK
145 * @param iter Tree iterator
146 * @return ID string
147 */
83fb99f9 148struct queue_entry *ql_iter_to_q(GtkTreeModel *model,
22717074 149 GtkTreeIter *iter) {
83fb99f9 150 struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql");
22717074
RK
151 GValue v[1];
152 memset(v, 0, sizeof v);
83fb99f9 153 gtk_tree_model_get_value(model, iter, ql->ncolumns, v);
22717074
RK
154 assert(G_VALUE_TYPE(v) == G_TYPE_POINTER);
155 struct queue_entry *const q = g_value_get_pointer(v);
156 g_value_unset(v);
157 return q;
158}
159
c133bd3c
RK
160/** @brief Update one row of a list store
161 * @param q Queue entry
162 * @param iter Iterator referring to row or NULL to work it out
163 */
164void ql_update_row(struct queue_entry *q,
165 GtkTreeIter *iter) {
166 const struct queuelike *const ql = q->ql;
167
168 D(("ql_update_row"));
169 /* If no iter was supplied, work it out */
170 GtkTreeIter my_iter[1];
171 if(!iter) {
172 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter);
173 struct queue_entry *qq;
174 for(qq = ql->q; qq && q != qq; qq = qq->next)
175 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter);
176 if(!qq)
177 return;
178 iter = my_iter;
179 }
180 /* Update all the columns */
181 for(int col = 0; col < ql->ncolumns; ++col)
182 gtk_list_store_set(ql->store, iter,
183 col, ql->columns[col].value(q,
184 ql->columns[col].data),
185 -1);
22717074
RK
186 /* The hidden extra column is the queue entry */
187 gtk_list_store_set(ql->store, iter, ql->ncolumns, q, -1);
c133bd3c
RK
188}
189
190/** @brief Update the list store
191 * @param ql Queuelike to update
192 *
193 * Called when new namepart data is available (and initially). Doesn't change
194 * the rows, just updates the cell values.
195 */
196void ql_update_list_store(struct queuelike *ql) {
197 D(("ql_update_list_store"));
198 GtkTreeIter iter[1];
199 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
200 for(struct queue_entry *q = ql->q; q; q = q->next) {
201 ql_update_row(q, iter);
202 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
203 }
204}
205
83fb99f9
RK
206struct newqueue_data {
207 struct queue_entry *old, *new;
208};
209
210static void record_queue_map(hash *h,
211 const char *id,
212 struct queue_entry *old,
213 struct queue_entry *new) {
214 struct newqueue_data *nqd;
215
216 if(!(nqd = hash_find(h, id))) {
217 static const struct newqueue_data empty[1];
218 hash_add(h, id, empty, HASH_INSERT);
219 nqd = hash_find(h, id);
220 }
221 if(old)
222 nqd->old = old;
223 if(new)
224 nqd->new = new;
225}
226
227#if 0
228static void dump_queue(struct queue_entry *head, struct queue_entry *mark) {
229 for(struct queue_entry *q = head; q; q = q->next) {
230 if(q == mark)
231 fprintf(stderr, "!");
232 fprintf(stderr, "%s", q->id);
233 if(q->next)
234 fprintf(stderr, " ");
235 }
236 fprintf(stderr, "\n");
237}
238
239static void dump_rows(struct queuelike *ql) {
240 GtkTreeIter iter[1];
241 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
242 iter);
243 while(it) {
244 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
245 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
246 fprintf(stderr, "%s", q->id);
247 if(it)
248 fprintf(stderr, " ");
249 }
250 fprintf(stderr, "\n");
251}
252#endif
253
c133bd3c
RK
254/** @brief Reset the list store
255 * @param ql Queuelike to reset
83fb99f9 256 * @param newq New queue contents/ordering
c133bd3c 257 *
83fb99f9 258 * Updates the queue to match @p newq
c133bd3c
RK
259 */
260void ql_new_queue(struct queuelike *ql,
261 struct queue_entry *newq) {
262 D(("ql_new_queue"));
83fb99f9
RK
263 ++suppress_actions;
264
265 /* Tell every queue entry which queue owns it */
266 //fprintf(stderr, "%s: filling in q->ql\n", ql->name);
267 for(struct queue_entry *q = newq; q; q = q->next)
c133bd3c 268 q->ql = ql;
83fb99f9
RK
269
270 //fprintf(stderr, "%s: constructing h\n", ql->name);
271 /* Construct map from id to new and old structures */
272 hash *h = hash_new(sizeof(struct newqueue_data));
273 for(struct queue_entry *q = ql->q; q; q = q->next)
274 record_queue_map(h, q->id, q, NULL);
275 for(struct queue_entry *q = newq; q; q = q->next)
276 record_queue_map(h, q->id, NULL, q);
277
278 /* The easy bit: delete rows not present any more. In the same pass we
279 * update the secret column containing the queue_entry pointer. */
280 //fprintf(stderr, "%s: deleting rows...\n", ql->name);
281 GtkTreeIter iter[1];
282 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
283 iter);
284 int inserted = 0, deleted = 0, kept = 0;
285 while(it) {
286 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
287 const struct newqueue_data *nqd = hash_find(h, q->id);
288 if(nqd->new) {
289 /* Tell this row that it belongs to the new version of the queue */
290 gtk_list_store_set(ql->store, iter, ql->ncolumns, nqd->new, -1);
291 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
292 ++kept;
293 } else {
294 /* Delete this row (and move iter to the next one) */
295 //fprintf(stderr, " delete %s", q->id);
296 it = gtk_list_store_remove(ql->store, iter);
297 ++deleted;
298 }
c133bd3c 299 }
83fb99f9
RK
300
301 /* Now every row's secret column is right, but we might be missing new rows
302 * and they might be in the wrong order */
303
304 /* We're going to have to support arbitrary rearrangements, so we might as
305 * well add new elements at the end. */
306 //fprintf(stderr, "%s: adding rows...\n", ql->name);
307 struct queue_entry *after = 0;
308 for(struct queue_entry *q = newq; q; q = q->next) {
309 const struct newqueue_data *nqd = hash_find(h, q->id);
310 if(!nqd->old) {
311 GtkTreeIter iter[1];
312 if(after) {
313 /* Try to insert at the right sort of place */
314 GtkTreeIter where[1];
315 gboolean wit = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
316 where);
317 while(wit && ql_iter_to_q(GTK_TREE_MODEL(ql->store), where) != after)
318 wit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), where);
319 if(wit)
320 gtk_list_store_insert_after(ql->store, iter, where);
321 else
322 gtk_list_store_append(ql->store, iter);
323 } else
324 gtk_list_store_prepend(ql->store, iter);
325 gtk_list_store_set(ql->store, iter, ql->ncolumns, q, -1);
326 //fprintf(stderr, " add %s", q->id);
327 ++inserted;
328 }
329 after = newq;
330 }
331
332 /* Now exactly the right set of rows are present, and they have the right
333 * queue_entry pointers in their secret column, but they may be in the wrong
334 * order.
335 *
336 * The current code is simple but amounts to a bubble-sort - we might easily
337 * called gtk_tree_model_iter_next a couple of thousand times.
338 */
339 //fprintf(stderr, "%s: rearranging rows\n", ql->name);
340 //fprintf(stderr, "%s: queue state: ", ql->name);
341 //dump_queue(newq, 0);
342 //fprintf(stderr, "%s: row state: ", ql->name);
343 //dump_rows(ql);
344 it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
345 iter);
346 struct queue_entry *rq = newq; /* r for 'right, correct' */
347 int swaps = 0, searches = 0;
348 while(it) {
349 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
350 //fprintf(stderr, " rq = %p, q = %p\n", rq, q);
351 //fprintf(stderr, " rq->id = %s, q->id = %s\n", rq->id, q->id);
352
353 if(q != rq) {
354 //fprintf(stderr, " mismatch\n");
355 GtkTreeIter next[1] = { *iter };
356 gboolean nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
357 while(nit) {
358 struct queue_entry *nq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), next);
359 //fprintf(stderr, " candidate: %s\n", nq->id);
360 if(nq == rq)
361 break;
362 nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
363 ++searches;
364 }
365 assert(nit);
366 //fprintf(stderr, " found it\n");
367 gtk_list_store_swap(ql->store, iter, next);
368 *iter = *next;
369 //fprintf(stderr, "%s: new row state: ", ql->name);
370 //dump_rows(ql);
371 ++swaps;
372 }
373 /* ...and onto the next one */
374 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
375 rq = rq->next;
376 }
377#if 0
378 fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name,
379 kept, inserted, deleted, swaps, searches);
380#endif
381 //fprintf(stderr, "done\n");
382 ql->q = newq;
383 /* Set the rest of the columns in new rows */
384 ql_update_list_store(ql);
83fb99f9 385 --suppress_actions;
c133bd3c
RK
386}
387
388/** @brief Initialize a @ref queuelike */
389GtkWidget *init_queuelike(struct queuelike *ql) {
390 D(("init_queuelike"));
22717074
RK
391 /* Create the list store. We add an extra column to hold the ID. */
392 GType *types = xcalloc(ql->ncolumns + 1, sizeof (GType));
c133bd3c
RK
393 for(int n = 0; n < ql->ncolumns; ++n)
394 types[n] = G_TYPE_STRING;
22717074
RK
395 types[ql->ncolumns] = G_TYPE_POINTER;
396 ql->store = gtk_list_store_newv(ql->ncolumns + 1, types);
397 g_object_set_data(G_OBJECT(ql->store), "ql", (void *)ql);
c133bd3c
RK
398
399 /* Create the view */
400 ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store));
1583d68f 401 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql->view), TRUE);
c133bd3c
RK
402
403 /* Create cell renderers and label columns */
404 for(int n = 0; n < ql->ncolumns; ++n) {
405 GtkCellRenderer *r = gtk_cell_renderer_text_new();
b0b15b7c
RK
406 if(ql->columns[n].flags & COL_ELLIPSIZE)
407 g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_END, (char *)0);
408 if(ql->columns[n].flags & COL_RIGHT)
409 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
c133bd3c
RK
410 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
411 (ql->columns[n].name,
412 r,
413 "text", n,
414 (char *)0);
3ffb8e5d
RK
415 gtk_tree_view_column_set_resizable(c, TRUE);
416 gtk_tree_view_column_set_reorderable(c, TRUE);
b0b15b7c
RK
417 if(ql->columns[n].flags & COL_EXPAND)
418 g_object_set(c, "expand", TRUE, (char *)0);
c133bd3c
RK
419 gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c);
420 }
421
422 /* The selection should support multiple things being selected */
423 ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view));
424 gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE);
425
c133bd3c 426 /* Catch button presses */
17c36802 427 g_signal_connect(ql->view, "button-press-event",
c133bd3c
RK
428 G_CALLBACK(ql_button_release), ql);
429
430 /* TODO style? */
c133bd3c
RK
431
432 ql->init();
433
ad47bd4c
RK
434 /* Update display text when lookups complete */
435 event_register("lookups-completed", queue_lookups_completed, ql);
436
ee7552f8
RK
437 GtkWidget *scrolled = scroll_widget(ql->view);
438 g_object_set_data(G_OBJECT(scrolled), "type", (void *)ql_tabtype(ql));
439 return scrolled;
c133bd3c
RK
440}
441
442/*
443Local Variables:
444c-basic-offset:2
445comment-column:40
446fill-column:79
447indent-tabs-mode:nil
448End:
449*/