2 * This file is part of DisOrder
3 * Copyright (C) 2006-2009 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 Disobedience 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 static const GtkTargetEntry queuelike_targets[] = {
51 (char *)"text/x-disorder-track-data", /* drag type */
52 GTK_TARGET_SAME_WIDGET, /* rearrangement only for now */
57 /* Track detail lookup ----------------------------------------------------- */
59 static void queue_lookups_completed(const char attribute((unused)) *event,
60 void attribute((unused)) *eventdata,
62 struct queuelike *ql = callbackdata;
63 ql_update_list_store(ql);
66 /* Column formatting -------------------------------------------------------- */
68 /** @brief Format the 'when' column */
69 const char *column_when(const struct queue_entry *q,
70 const char attribute((unused)) *data) {
77 case playing_isscratch:
78 case playing_unplayed:
83 case playing_no_player:
85 case playing_scratched:
88 case playing_quitting:
96 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
102 /** @brief Format the 'who' column */
103 const char *column_who(const struct queue_entry *q,
104 const char attribute((unused)) *data) {
106 return q->submitter ? q->submitter : "";
109 /** @brief Format one of the track name columns */
110 const char *column_namepart(const struct queue_entry *q,
112 D(("column_namepart"));
113 return namepart(q->track, "display", data);
116 /** @brief Format the length column */
117 const char *column_length(const struct queue_entry *q,
118 const char attribute((unused)) *data) {
119 D(("column_length"));
122 char *played = 0, *length = 0;
124 /* Work out what to say for the length */
125 l = namepart_length(q->track);
127 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
129 byte_xasprintf(&length, "?:??");
130 /* For the currently playing track we want to report how much of the track
132 if(q == playing_track) {
133 /* log_state() arranges that we re-get the playing data whenever the
134 * pause/resume state changes */
135 if(last_state & DISORDER_TRACK_PAUSED)
136 l = playing_track->sofar;
141 l = playing_track->sofar + (now - last_playing);
143 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
149 /* List store maintenance -------------------------------------------------- */
151 /** @brief Return the @ref queue_entry corresponding to @p iter
152 * @param model Model that owns @p iter
153 * @param iter Tree iterator
154 * @return Pointer to queue entry
156 struct queue_entry *ql_iter_to_q(GtkTreeModel *model,
158 struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql");
160 memset(v, 0, sizeof v);
161 gtk_tree_model_get_value(model, iter, ql->ncolumns + QUEUEPOINTER_COLUMN, v);
162 assert(G_VALUE_TYPE(v) == G_TYPE_POINTER);
163 struct queue_entry *const q = g_value_get_pointer(v);
168 /** @brief Return the @ref queue_entry corresponding to @p path
169 * @param model Model to query
170 * @param path Path into tree
171 * @return Pointer to queue entry or NULL
173 struct queue_entry *ql_path_to_q(GtkTreeModel *model,
176 if(!gtk_tree_model_get_iter(model, iter, path))
178 return ql_iter_to_q(model, iter);
181 /** @brief Update one row of a list store
182 * @param q Queue entry
183 * @param iter Iterator referring to row or NULL to work it out
185 void ql_update_row(struct queue_entry *q,
187 const struct queuelike *const ql = q->ql;
189 D(("ql_update_row"));
190 /* If no iter was supplied, work it out */
191 GtkTreeIter my_iter[1];
193 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter);
194 struct queue_entry *qq;
195 for(qq = ql->q; qq && q != qq; qq = qq->next)
196 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter);
201 /* Update all the columns */
202 for(int col = 0; col < ql->ncolumns; ++col) {
203 const char *const v = ql->columns[col].value(q,
204 ql->columns[col].data);
206 gtk_list_store_set(ql->store, iter,
210 gtk_list_store_set(ql->store, iter,
211 ql->ncolumns + QUEUEPOINTER_COLUMN, q,
213 if(q == playing_track)
214 gtk_list_store_set(ql->store, iter,
215 ql->ncolumns + BACKGROUND_COLUMN, BG_PLAYING,
216 ql->ncolumns + FOREGROUND_COLUMN, FG_PLAYING,
219 gtk_list_store_set(ql->store, iter,
220 ql->ncolumns + BACKGROUND_COLUMN, (char *)0,
221 ql->ncolumns + FOREGROUND_COLUMN, (char *)0,
225 /** @brief Update the list store
226 * @param ql Queuelike to update
228 * Called when new namepart data is available (and initially). Doesn't change
229 * the rows, just updates the cell values.
231 void ql_update_list_store(struct queuelike *ql) {
232 D(("ql_update_list_store"));
234 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
235 for(struct queue_entry *q = ql->q; q; q = q->next) {
236 ql_update_row(q, iter);
237 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
241 struct newqueue_data {
242 struct queue_entry *old, *new;
245 static void record_queue_map(hash *h,
247 struct queue_entry *old,
248 struct queue_entry *new) {
249 struct newqueue_data *nqd;
251 if(!(nqd = hash_find(h, id))) {
252 static const struct newqueue_data empty[1];
253 hash_add(h, id, empty, HASH_INSERT);
254 nqd = hash_find(h, id);
263 static void dump_queue(struct queue_entry *head, struct queue_entry *mark) {
264 for(struct queue_entry *q = head; q; q = q->next) {
266 fprintf(stderr, "!");
267 fprintf(stderr, "%s", q->id);
269 fprintf(stderr, " ");
271 fprintf(stderr, "\n");
274 static void dump_rows(struct queuelike *ql) {
276 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
279 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
280 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
281 fprintf(stderr, "%s", q->id);
283 fprintf(stderr, " ");
285 fprintf(stderr, "\n");
289 /** @brief Reset the list store
290 * @param ql Queuelike to reset
291 * @param newq New queue contents/ordering
293 * Updates the queue to match @p newq
295 void ql_new_queue(struct queuelike *ql,
296 struct queue_entry *newq) {
300 /* Tell every queue entry which queue owns it */
301 //fprintf(stderr, "%s: filling in q->ql\n", ql->name);
302 for(struct queue_entry *q = newq; q; q = q->next)
305 //fprintf(stderr, "%s: constructing h\n", ql->name);
306 /* Construct map from id to new and old structures */
307 hash *h = hash_new(sizeof(struct newqueue_data));
308 for(struct queue_entry *q = ql->q; q; q = q->next)
309 record_queue_map(h, q->id, q, NULL);
310 for(struct queue_entry *q = newq; q; q = q->next)
311 record_queue_map(h, q->id, NULL, q);
313 /* The easy bit: delete rows not present any more. In the same pass we
314 * update the secret column containing the queue_entry pointer. */
315 //fprintf(stderr, "%s: deleting rows...\n", ql->name);
317 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
319 int inserted = 0, deleted = 0, kept = 0;
321 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
322 const struct newqueue_data *nqd = hash_find(h, q->id);
324 /* Tell this row that it belongs to the new version of the queue */
325 gtk_list_store_set(ql->store, iter,
326 ql->ncolumns + QUEUEPOINTER_COLUMN, nqd->new,
328 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
331 /* Delete this row (and move iter to the next one) */
332 //fprintf(stderr, " delete %s", q->id);
333 it = gtk_list_store_remove(ql->store, iter);
338 /* Now every row's secret column is right, but we might be missing new rows
339 * and they might be in the wrong order */
341 /* We're going to have to support arbitrary rearrangements, so we might as
342 * well add new elements at the end. */
343 //fprintf(stderr, "%s: adding rows...\n", ql->name);
344 struct queue_entry *after = 0;
345 for(struct queue_entry *q = newq; q; q = q->next) {
346 const struct newqueue_data *nqd = hash_find(h, q->id);
349 /* Try to insert at the right sort of place */
350 GtkTreeIter where[1];
351 gboolean wit = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
353 while(wit && ql_iter_to_q(GTK_TREE_MODEL(ql->store), where) != after)
354 wit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), where);
356 gtk_list_store_insert_after(ql->store, iter, where);
358 gtk_list_store_append(ql->store, iter);
360 gtk_list_store_prepend(ql->store, iter);
361 gtk_list_store_set(ql->store, iter,
362 ql->ncolumns + QUEUEPOINTER_COLUMN, q,
364 //fprintf(stderr, " add %s", q->id);
370 /* Now exactly the right set of rows are present, and they have the right
371 * queue_entry pointers in their secret column, but they may be in the wrong
374 * The current code is simple but amounts to a bubble-sort - we might easily
375 * called gtk_tree_model_iter_next a couple of thousand times.
377 //fprintf(stderr, "%s: rearranging rows\n", ql->name);
378 //fprintf(stderr, "%s: queue state: ", ql->name);
379 //dump_queue(newq, 0);
380 //fprintf(stderr, "%s: row state: ", ql->name);
382 it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
384 struct queue_entry *rq = newq; /* r for 'right, correct' */
385 int swaps = 0, searches = 0;
387 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
388 //fprintf(stderr, " rq = %p, q = %p\n", rq, q);
389 //fprintf(stderr, " rq->id = %s, q->id = %s\n", rq->id, q->id);
392 //fprintf(stderr, " mismatch\n");
393 GtkTreeIter next[1] = { *iter };
394 gboolean nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
396 struct queue_entry *nq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), next);
397 //fprintf(stderr, " candidate: %s\n", nq->id);
400 nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
404 //fprintf(stderr, " found it\n");
405 gtk_list_store_swap(ql->store, iter, next);
407 //fprintf(stderr, "%s: new row state: ", ql->name);
411 /* ...and onto the next one */
412 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
416 fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name,
417 kept, inserted, deleted, swaps, searches);
419 //fprintf(stderr, "done\n");
421 /* Set the rest of the columns in new rows */
422 ql_update_list_store(ql);
426 /** @brief Called when a drag operation from this queuelike begins
427 * @param w Source widget (the tree view)
428 * @param dc Drag context
429 * @param user_data The queuelike
431 static void ql_drag_begin(GtkWidget attribute((unused)) *w,
432 GdkDragContext attribute((unused)) *dc,
433 gpointer user_data) {
434 struct queuelike *const attribute((unused)) ql = user_data;
436 fprintf(stderr, "drag-begin\n");
437 // TODO call gtk_drag_source_set_icon() to set a suitably informative
441 /** @brief Callback to add selected tracks to the selection data
443 * Called from ql_drag_data_get().
445 static void ql_drag_data_get_collect(GtkTreeModel *model,
446 GtkTreePath attribute((unused)) *path,
449 struct dynstr *const result = data;
450 struct queue_entry *const q = ql_iter_to_q(model, iter);
452 dynstr_append_string(result, q->id);
453 dynstr_append(result, '\n');
454 dynstr_append_string(result, q->track);
455 dynstr_append(result, '\n');
458 /** @brief Called to extract the dragged data from the source queuelike
459 * @param w Source widget (the tree view)
460 * @param dc Drag context
461 * @param data Where to put the answer
462 * @param info_ Target @c info parameter
463 * @param time_ Time data requested (for some reason not a @c time_t)
464 * @param user_data The queuelike
466 static void ql_drag_data_get(GtkWidget attribute((unused)) *w,
467 GdkDragContext attribute((unused)) *dc,
468 GtkSelectionData *data,
469 guint attribute((unused)) info_,
470 guint attribute((unused)) time_,
471 gpointer user_data) {
472 struct queuelike *const ql = user_data;
473 struct dynstr result[1];
475 /* The list of tracks is converted into a single string, consisting of IDs
476 * and track names. Each is terminated by a newline. Including both ID and
477 * track name means that the receiver can use whichever happens to be more
480 gtk_tree_selection_selected_foreach(ql->selection,
481 ql_drag_data_get_collect,
483 //fprintf(stderr, "drag-data-get: %.*s\n",
484 // result->nvec, result->vec);
485 /* gtk_selection_data_set_text() insists that data->target is one of a
486 * variety of stringy atoms. TODO: where does this value actually come
488 gtk_selection_data_set(data,
490 8, (guchar *)result->vec, result->nvec);
493 /** @brief Called when drag data is received
494 * @param w Target widget (the tree view)
495 * @param dc Drag context
496 * @param x The drop location
497 * @param y The drop location
498 * @param data The selection data
499 * @param info_ The target type that was chosen
500 * @param time_ Time data received (for some reason not a @c time_t)
501 * @param user_data The queuelike
503 static void ql_drag_data_received(GtkWidget attribute((unused)) *w,
504 GdkDragContext attribute((unused)) *dc,
507 GtkSelectionData *data,
508 guint attribute((unused)) info_,
509 guint attribute((unused)) time_,
510 gpointer user_data) {
511 struct queuelike *const ql = user_data;
513 struct vector ids[1], tracks[1];
516 //fprintf(stderr, "drag-data-received: %d,%d\n", x, y);
517 /* Get the selection string */
518 p = result = (char *)gtk_selection_data_get_text(data);
520 //fprintf(stderr, "gtk_selection_data_get_text() returned NULL\n");
523 //fprintf(stderr, "%s--\n", result);
524 /* Parse it back into IDs and track names */
528 char *nl = strchr(p, '\n');
532 //fprintf(stderr, " %s\n", p);
533 vector_append(parity++ & 1 ? tracks : ids, xstrdup(p));
537 if(ids->nvec != tracks->nvec) {
538 //fprintf(stderr, " inconsistent drag data!\n");
541 vector_terminate(ids);
542 vector_terminate(tracks);
543 /* Figure out which row the drop precedes (if any) */
545 GtkTreeViewDropPosition pos;
546 struct queue_entry *q;
547 if(!gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(ql->view), x, y,
549 //fprintf(stderr, "gtk_tree_view_get_dest_row_at_pos returned FALSE\n");
550 /* This generally means a drop past the end of the queue. We find the last
551 * element in the queue and ask to move after that. */
552 for(q = ql->q; q && q->next; q = q->next)
555 /* Convert the path to a queue entry pointer. */
556 q = ql_path_to_q(GTK_TREE_MODEL(ql->store), path);
557 //fprintf(stderr, " tree view likes to drop near %s\n",
558 // q->id ? q->id : "NULL");
559 /* TODO interpretation of q=NULL */
560 /* Q should point to the entry just before the insertion point, so that
561 * moveafter works, or NULL to insert right at the start. We don't support
562 * dropping into a row, since that doesn't make sense for us. */
564 case GTK_TREE_VIEW_DROP_BEFORE:
565 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
568 //fprintf(stderr, " ...but we like to drop near %s\n",
569 // q ? q->id : "NULL");
576 /* Note that q->id can match one of ids[]. This doesn't matter for
577 * moveafter but TODO may matter for playlist support. */
578 ql->drop(ql, tracks->nvec, tracks->vec, ids->vec, q);
581 /** @brief Initialize a @ref queuelike */
582 GtkWidget *init_queuelike(struct queuelike *ql) {
583 D(("init_queuelike"));
584 /* Create the list store. We add an extra column to hold a pointer to the
586 GType *types = xcalloc(ql->ncolumns + EXTRA_COLUMNS, sizeof (GType));
587 for(int n = 0; n < ql->ncolumns + EXTRA_COLUMNS; ++n)
588 types[n] = G_TYPE_STRING;
589 types[ql->ncolumns + QUEUEPOINTER_COLUMN] = G_TYPE_POINTER;
590 ql->store = gtk_list_store_newv(ql->ncolumns + EXTRA_COLUMNS, types);
591 g_object_set_data(G_OBJECT(ql->store), "ql", (void *)ql);
593 /* Create the view */
594 ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store));
595 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql->view), TRUE);
597 /* Create cell renderers and label columns */
598 for(int n = 0; n < ql->ncolumns; ++n) {
599 GtkCellRenderer *r = gtk_cell_renderer_text_new();
600 if(ql->columns[n].flags & COL_ELLIPSIZE)
601 g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_END, (char *)0);
602 if(ql->columns[n].flags & COL_RIGHT)
603 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
604 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
605 (ql->columns[n].name,
608 "background", ql->ncolumns + BACKGROUND_COLUMN,
609 "foreground", ql->ncolumns + FOREGROUND_COLUMN,
611 gtk_tree_view_column_set_resizable(c, TRUE);
612 gtk_tree_view_column_set_reorderable(c, TRUE);
613 if(ql->columns[n].flags & COL_EXPAND)
614 g_object_set(c, "expand", TRUE, (char *)0);
615 gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c);
618 /* The selection should support multiple things being selected */
619 ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view));
620 gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE);
622 /* Catch button presses */
623 g_signal_connect(ql->view, "button-press-event",
624 G_CALLBACK(ql_button_release), ql);
628 /* Originally this was:
630 * gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql->view), TRUE);
632 * However this has a two deficiencies:
634 * 1) Only one row can be dragged at once. It would be nice
635 * to be able to do bulk rearrangements since the server
636 * can cope with that well.
637 * 2) Dragging between windows is not possible. When playlist
638 * support appears, it should be possible to drag tracks
639 * from the choose tag into the playlist.
641 * At the time of writing neither of these problems are fully solved, the
642 * code as it stands is just a stepping stone in that direction.
645 /* This view will act as a drag source */
646 gtk_drag_source_set(ql->view,
649 sizeof queuelike_targets / sizeof *queuelike_targets,
651 /* This view will act as a drag destination
653 * GTK_DEST_DEFAULT_ALL is going to have to change; GTK_DEST_DEFAULT_DROP
654 * does not allow us to detect illegal drops up front. However it will do
657 gtk_drag_dest_set(ql->view,
658 GTK_DEST_DEFAULT_ALL,
660 sizeof queuelike_targets / sizeof *queuelike_targets,
662 g_signal_connect(ql->view, "drag-begin",
663 G_CALLBACK(ql_drag_begin), ql);
664 g_signal_connect(ql->view, "drag-data-get",
665 G_CALLBACK(ql_drag_data_get), ql);
666 g_signal_connect(ql->view, "drag-data-received",
667 G_CALLBACK(ql_drag_data_received), ql);
669 /* TODO: support copy-dragging out of non-rearrangeable queues. Will need
670 * to support copy dropping into the rearrangeable ones. */
677 /* Update display text when lookups complete */
678 event_register("lookups-completed", queue_lookups_completed, ql);
680 GtkWidget *scrolled = scroll_widget(ql->view);
681 g_object_set_data(G_OBJECT(scrolled), "type", (void *)ql_tabtype(ql));