2 * This file is part of DisOrder
3 * Copyright (C) 2006-2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/queue-generic.c
19 * @brief Queue widgets
21 * This file provides contains code shared between all the queue-like
22 * widgets - the queue, the recent list and the added tracks list.
24 * This code is in the process of being rewritten to use the native list
27 * There are three @ref queuelike objects: @ref ql_queue, @ref
28 * ql_recent and @ref ql_added. Each has an associated queue linked
29 * list and a list store containing the contents.
31 * When new contents turn up we rearrange the list store accordingly.
33 * NB that while in the server the playing track is not in the queue, in
34 * Disobedience, the playing does live in @c ql_queue.q, despite its different
35 * status to everything else found in that list.
38 * - display playing row in a different color?
40 #include "disobedience.h"
42 #include "queue-generic.h"
44 static struct queuelike *const queuelikes[] = {
45 &ql_queue, &ql_recent, &ql_added
47 #define NQUEUELIKES (sizeof queuelikes / sizeof *queuelikes)
49 /* Track detail lookup ----------------------------------------------------- */
51 static void queue_lookups_completed(const char attribute((unused)) *event,
52 void attribute((unused)) *eventdata,
54 struct queuelike *ql = callbackdata;
55 ql_update_list_store(ql);
58 /* Column formatting -------------------------------------------------------- */
60 /** @brief Format the 'when' column */
61 const char *column_when(const struct queue_entry *q,
62 const char attribute((unused)) *data) {
69 case playing_isscratch:
70 case playing_unplayed:
75 case playing_no_player:
77 case playing_scratched:
80 case playing_quitting:
88 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
94 /** @brief Format the 'who' column */
95 const char *column_who(const struct queue_entry *q,
96 const char attribute((unused)) *data) {
98 return q->submitter ? q->submitter : "";
101 /** @brief Format one of the track name columns */
102 const char *column_namepart(const struct queue_entry *q,
104 D(("column_namepart"));
105 return namepart(q->track, "display", data);
108 /** @brief Format the length column */
109 const char *column_length(const struct queue_entry *q,
110 const char attribute((unused)) *data) {
111 D(("column_length"));
114 char *played = 0, *length = 0;
116 /* Work out what to say for the length */
117 l = namepart_length(q->track);
119 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
121 byte_xasprintf(&length, "?:??");
122 /* For the currently playing track we want to report how much of the track
124 if(q == playing_track) {
125 /* log_state() arranges that we re-get the playing data whenever the
126 * pause/resume state changes */
127 if(last_state & DISORDER_TRACK_PAUSED)
128 l = playing_track->sofar;
131 l = playing_track->sofar + (now - last_playing);
133 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
139 /* List store maintenance -------------------------------------------------- */
141 /** @brief Return the @ref queue_entry corresponding to @p iter
142 * @param model Model that owns @p iter
143 * @param iter Tree iterator
146 struct queue_entry *ql_iter_to_q(GtkTreeModel *model,
148 struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql");
150 memset(v, 0, sizeof v);
151 gtk_tree_model_get_value(model, iter, ql->ncolumns + QUEUEPOINTER_COLUMN, v);
152 assert(G_VALUE_TYPE(v) == G_TYPE_POINTER);
153 struct queue_entry *const q = g_value_get_pointer(v);
158 /** @brief Update one row of a list store
159 * @param q Queue entry
160 * @param iter Iterator referring to row or NULL to work it out
162 void ql_update_row(struct queue_entry *q,
164 const struct queuelike *const ql = q->ql;
166 D(("ql_update_row"));
167 /* If no iter was supplied, work it out */
168 GtkTreeIter my_iter[1];
170 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter);
171 struct queue_entry *qq;
172 for(qq = ql->q; qq && q != qq; qq = qq->next)
173 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter);
178 /* Update all the columns */
179 for(int col = 0; col < ql->ncolumns; ++col)
180 gtk_list_store_set(ql->store, iter,
181 col, ql->columns[col].value(q,
182 ql->columns[col].data),
184 gtk_list_store_set(ql->store, iter,
185 ql->ncolumns + QUEUEPOINTER_COLUMN, q,
187 if(q == playing_track)
188 gtk_list_store_set(ql->store, iter,
189 ql->ncolumns + BACKGROUND_COLUMN, BG_PLAYING,
190 ql->ncolumns + FOREGROUND_COLUMN, FG_PLAYING,
193 gtk_list_store_set(ql->store, iter,
194 ql->ncolumns + BACKGROUND_COLUMN, (char *)0,
195 ql->ncolumns + FOREGROUND_COLUMN, (char *)0,
199 /** @brief Update the list store
200 * @param ql Queuelike to update
202 * Called when new namepart data is available (and initially). Doesn't change
203 * the rows, just updates the cell values.
205 void ql_update_list_store(struct queuelike *ql) {
206 D(("ql_update_list_store"));
208 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
209 for(struct queue_entry *q = ql->q; q; q = q->next) {
210 ql_update_row(q, iter);
211 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
215 struct newqueue_data {
216 struct queue_entry *old, *new;
219 static void record_queue_map(hash *h,
221 struct queue_entry *old,
222 struct queue_entry *new) {
223 struct newqueue_data *nqd;
225 if(!(nqd = hash_find(h, id))) {
226 static const struct newqueue_data empty[1];
227 hash_add(h, id, empty, HASH_INSERT);
228 nqd = hash_find(h, id);
237 static void dump_queue(struct queue_entry *head, struct queue_entry *mark) {
238 for(struct queue_entry *q = head; q; q = q->next) {
240 fprintf(stderr, "!");
241 fprintf(stderr, "%s", q->id);
243 fprintf(stderr, " ");
245 fprintf(stderr, "\n");
248 static void dump_rows(struct queuelike *ql) {
250 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
253 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
254 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
255 fprintf(stderr, "%s", q->id);
257 fprintf(stderr, " ");
259 fprintf(stderr, "\n");
263 /** @brief Reset the list store
264 * @param ql Queuelike to reset
265 * @param newq New queue contents/ordering
267 * Updates the queue to match @p newq
269 void ql_new_queue(struct queuelike *ql,
270 struct queue_entry *newq) {
274 /* Tell every queue entry which queue owns it */
275 //fprintf(stderr, "%s: filling in q->ql\n", ql->name);
276 for(struct queue_entry *q = newq; q; q = q->next)
279 //fprintf(stderr, "%s: constructing h\n", ql->name);
280 /* Construct map from id to new and old structures */
281 hash *h = hash_new(sizeof(struct newqueue_data));
282 for(struct queue_entry *q = ql->q; q; q = q->next)
283 record_queue_map(h, q->id, q, NULL);
284 for(struct queue_entry *q = newq; q; q = q->next)
285 record_queue_map(h, q->id, NULL, q);
287 /* The easy bit: delete rows not present any more. In the same pass we
288 * update the secret column containing the queue_entry pointer. */
289 //fprintf(stderr, "%s: deleting rows...\n", ql->name);
291 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
293 int inserted = 0, deleted = 0, kept = 0;
295 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
296 const struct newqueue_data *nqd = hash_find(h, q->id);
298 /* Tell this row that it belongs to the new version of the queue */
299 gtk_list_store_set(ql->store, iter,
300 ql->ncolumns + QUEUEPOINTER_COLUMN, nqd->new,
302 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
305 /* Delete this row (and move iter to the next one) */
306 //fprintf(stderr, " delete %s", q->id);
307 it = gtk_list_store_remove(ql->store, iter);
312 /* Now every row's secret column is right, but we might be missing new rows
313 * and they might be in the wrong order */
315 /* We're going to have to support arbitrary rearrangements, so we might as
316 * well add new elements at the end. */
317 //fprintf(stderr, "%s: adding rows...\n", ql->name);
318 struct queue_entry *after = 0;
319 for(struct queue_entry *q = newq; q; q = q->next) {
320 const struct newqueue_data *nqd = hash_find(h, q->id);
323 /* Try to insert at the right sort of place */
324 GtkTreeIter where[1];
325 gboolean wit = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
327 while(wit && ql_iter_to_q(GTK_TREE_MODEL(ql->store), where) != after)
328 wit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), where);
330 gtk_list_store_insert_after(ql->store, iter, where);
332 gtk_list_store_append(ql->store, iter);
334 gtk_list_store_prepend(ql->store, iter);
335 gtk_list_store_set(ql->store, iter,
336 ql->ncolumns + QUEUEPOINTER_COLUMN, q,
338 //fprintf(stderr, " add %s", q->id);
344 /* Now exactly the right set of rows are present, and they have the right
345 * queue_entry pointers in their secret column, but they may be in the wrong
348 * The current code is simple but amounts to a bubble-sort - we might easily
349 * called gtk_tree_model_iter_next a couple of thousand times.
351 //fprintf(stderr, "%s: rearranging rows\n", ql->name);
352 //fprintf(stderr, "%s: queue state: ", ql->name);
353 //dump_queue(newq, 0);
354 //fprintf(stderr, "%s: row state: ", ql->name);
356 it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
358 struct queue_entry *rq = newq; /* r for 'right, correct' */
359 int swaps = 0, searches = 0;
361 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
362 //fprintf(stderr, " rq = %p, q = %p\n", rq, q);
363 //fprintf(stderr, " rq->id = %s, q->id = %s\n", rq->id, q->id);
366 //fprintf(stderr, " mismatch\n");
367 GtkTreeIter next[1] = { *iter };
368 gboolean nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
370 struct queue_entry *nq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), next);
371 //fprintf(stderr, " candidate: %s\n", nq->id);
374 nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
378 //fprintf(stderr, " found it\n");
379 gtk_list_store_swap(ql->store, iter, next);
381 //fprintf(stderr, "%s: new row state: ", ql->name);
385 /* ...and onto the next one */
386 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
390 fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name,
391 kept, inserted, deleted, swaps, searches);
393 //fprintf(stderr, "done\n");
395 /* Set the rest of the columns in new rows */
396 ql_update_list_store(ql);
400 /** @brief Initialize a @ref queuelike */
401 GtkWidget *init_queuelike(struct queuelike *ql) {
402 D(("init_queuelike"));
403 /* Create the list store. We add an extra column to hold a pointer to the
405 GType *types = xcalloc(ql->ncolumns + EXTRA_COLUMNS, sizeof (GType));
406 for(int n = 0; n < ql->ncolumns + EXTRA_COLUMNS; ++n)
407 types[n] = G_TYPE_STRING;
408 types[ql->ncolumns + QUEUEPOINTER_COLUMN] = G_TYPE_POINTER;
409 ql->store = gtk_list_store_newv(ql->ncolumns + EXTRA_COLUMNS, types);
410 g_object_set_data(G_OBJECT(ql->store), "ql", (void *)ql);
412 /* Create the view */
413 ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store));
414 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql->view), TRUE);
416 /* Create cell renderers and label columns */
417 for(int n = 0; n < ql->ncolumns; ++n) {
418 GtkCellRenderer *r = gtk_cell_renderer_text_new();
419 if(ql->columns[n].flags & COL_ELLIPSIZE)
420 g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_END, (char *)0);
421 if(ql->columns[n].flags & COL_RIGHT)
422 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
423 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
424 (ql->columns[n].name,
427 "background", ql->ncolumns + BACKGROUND_COLUMN,
428 "foreground", ql->ncolumns + FOREGROUND_COLUMN,
430 gtk_tree_view_column_set_resizable(c, TRUE);
431 gtk_tree_view_column_set_reorderable(c, TRUE);
432 if(ql->columns[n].flags & COL_EXPAND)
433 g_object_set(c, "expand", TRUE, (char *)0);
434 gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c);
437 /* The selection should support multiple things being selected */
438 ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view));
439 gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE);
441 /* Catch button presses */
442 g_signal_connect(ql->view, "button-press-event",
443 G_CALLBACK(ql_button_release), ql);
449 /* Update display text when lookups complete */
450 event_register("lookups-completed", queue_lookups_completed, ql);
452 GtkWidget *scrolled = scroll_widget(ql->view);
453 g_object_set_data(G_OBJECT(scrolled), "type", (void *)ql_tabtype(ql));