chiark / gitweb /
Correct playlist unique ID construction
[disorder] / disobedience / queue-generic.c
CommitLineData
c133bd3c
RK
1/*
2 * This file is part of DisOrder
0beb320e 3 * Copyright (C) 2006-2009 Richard Kettlewell
c133bd3c 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
c133bd3c 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
c133bd3c
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
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.
14 *
c133bd3c 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
c133bd3c
RK
17 */
18/** @file disobedience/queue-generic.c
132a5a4a 19 * @brief Disobedience queue widgets
c133bd3c
RK
20 *
21 * This file provides contains code shared between all the queue-like
22 * widgets - the queue, the recent list and the added tracks list.
23 *
24 * This code is in the process of being rewritten to use the native list
25 * widget.
26 *
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.
30 *
31 * When new contents turn up we rearrange the list store accordingly.
32 *
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.
ee7552f8
RK
36 *
37 * To do:
ee7552f8 38 * - display playing row in a different color?
c133bd3c
RK
39 */
40#include "disobedience.h"
6982880f 41#include "popup.h"
c133bd3c 42#include "queue-generic.h"
3ae36d41 43#include "multidrag.h"
df73cc67 44#include "autoscroll.h"
c133bd3c 45
0beb320e
RK
46static const GtkTargetEntry queuelike_targets[] = {
47 {
4b51f265
RK
48 (char *)"text/x-disorder-queued-tracks", /* drag type */
49 GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */
0beb320e
RK
50 0 /* ID value */
51 },
4b51f265
RK
52 {
53 (char *)"text/x-disorder-playable-tracks", /* drag type */
54 GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */
55 1 /* ID value */
56 },
0beb320e
RK
57};
58
c133bd3c
RK
59/* Track detail lookup ----------------------------------------------------- */
60
ad47bd4c
RK
61static void queue_lookups_completed(const char attribute((unused)) *event,
62 void attribute((unused)) *eventdata,
63 void *callbackdata) {
64 struct queuelike *ql = callbackdata;
65 ql_update_list_store(ql);
c133bd3c
RK
66}
67
68/* Column formatting -------------------------------------------------------- */
69
70/** @brief Format the 'when' column */
71const char *column_when(const struct queue_entry *q,
72 const char attribute((unused)) *data) {
73 char when[64];
74 struct tm tm;
75 time_t t;
76
77 D(("column_when"));
78 switch(q->state) {
79 case playing_isscratch:
80 case playing_unplayed:
81 case playing_random:
82 t = q->expected;
83 break;
84 case playing_failed:
85 case playing_no_player:
86 case playing_ok:
87 case playing_scratched:
88 case playing_started:
89 case playing_paused:
90 case playing_quitting:
91 t = q->played;
92 break;
93 default:
94 t = 0;
95 break;
96 }
97 if(t)
98 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
99 else
100 when[0] = 0;
101 return xstrdup(when);
102}
103
104/** @brief Format the 'who' column */
105const char *column_who(const struct queue_entry *q,
106 const char attribute((unused)) *data) {
107 D(("column_who"));
108 return q->submitter ? q->submitter : "";
109}
110
111/** @brief Format one of the track name columns */
112const char *column_namepart(const struct queue_entry *q,
22717074 113 const char *data) {
c133bd3c
RK
114 D(("column_namepart"));
115 return namepart(q->track, "display", data);
116}
117
118/** @brief Format the length column */
119const char *column_length(const struct queue_entry *q,
120 const char attribute((unused)) *data) {
121 D(("column_length"));
122 long l;
123 time_t now;
124 char *played = 0, *length = 0;
125
126 /* Work out what to say for the length */
ad47bd4c 127 l = namepart_length(q->track);
c133bd3c
RK
128 if(l > 0)
129 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
130 else
131 byte_xasprintf(&length, "?:??");
132 /* For the currently playing track we want to report how much of the track
133 * has been played */
134 if(q == playing_track) {
135 /* log_state() arranges that we re-get the playing data whenever the
136 * pause/resume state changes */
137 if(last_state & DISORDER_TRACK_PAUSED)
138 l = playing_track->sofar;
139 else {
3900d6d6
RK
140 if(!last_playing)
141 return NULL;
4265e5d3 142 xtime(&now);
c133bd3c
RK
143 l = playing_track->sofar + (now - last_playing);
144 }
145 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
146 return played;
147 } else
148 return length;
149}
150
c133bd3c
RK
151/* List store maintenance -------------------------------------------------- */
152
22717074 153/** @brief Return the @ref queue_entry corresponding to @p iter
83fb99f9 154 * @param model Model that owns @p iter
22717074 155 * @param iter Tree iterator
82db9336 156 * @return Pointer to queue entry
22717074 157 */
83fb99f9 158struct queue_entry *ql_iter_to_q(GtkTreeModel *model,
22717074 159 GtkTreeIter *iter) {
83fb99f9 160 struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql");
22717074
RK
161 GValue v[1];
162 memset(v, 0, sizeof v);
0fb5e8f3 163 gtk_tree_model_get_value(model, iter, ql->ncolumns + QUEUEPOINTER_COLUMN, v);
22717074
RK
164 assert(G_VALUE_TYPE(v) == G_TYPE_POINTER);
165 struct queue_entry *const q = g_value_get_pointer(v);
166 g_value_unset(v);
167 return q;
168}
169
0beb320e
RK
170/** @brief Return the @ref queue_entry corresponding to @p path
171 * @param model Model to query
172 * @param path Path into tree
173 * @return Pointer to queue entry or NULL
174 */
175struct queue_entry *ql_path_to_q(GtkTreeModel *model,
176 GtkTreePath *path) {
177 GtkTreeIter iter[1];
178 if(!gtk_tree_model_get_iter(model, iter, path))
179 return NULL;
180 return ql_iter_to_q(model, iter);
181}
182
c133bd3c
RK
183/** @brief Update one row of a list store
184 * @param q Queue entry
185 * @param iter Iterator referring to row or NULL to work it out
186 */
187void ql_update_row(struct queue_entry *q,
188 GtkTreeIter *iter) {
189 const struct queuelike *const ql = q->ql;
190
191 D(("ql_update_row"));
192 /* If no iter was supplied, work it out */
193 GtkTreeIter my_iter[1];
194 if(!iter) {
195 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter);
196 struct queue_entry *qq;
197 for(qq = ql->q; qq && q != qq; qq = qq->next)
198 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter);
199 if(!qq)
200 return;
201 iter = my_iter;
202 }
203 /* Update all the columns */
3900d6d6
RK
204 for(int col = 0; col < ql->ncolumns; ++col) {
205 const char *const v = ql->columns[col].value(q,
206 ql->columns[col].data);
207 if(v)
208 gtk_list_store_set(ql->store, iter,
209 col, v,
210 -1);
211 }
0fb5e8f3
RK
212 gtk_list_store_set(ql->store, iter,
213 ql->ncolumns + QUEUEPOINTER_COLUMN, q,
214 -1);
215 if(q == playing_track)
216 gtk_list_store_set(ql->store, iter,
217 ql->ncolumns + BACKGROUND_COLUMN, BG_PLAYING,
218 ql->ncolumns + FOREGROUND_COLUMN, FG_PLAYING,
219 -1);
220 else
221 gtk_list_store_set(ql->store, iter,
222 ql->ncolumns + BACKGROUND_COLUMN, (char *)0,
223 ql->ncolumns + FOREGROUND_COLUMN, (char *)0,
224 -1);
c133bd3c
RK
225}
226
227/** @brief Update the list store
228 * @param ql Queuelike to update
229 *
230 * Called when new namepart data is available (and initially). Doesn't change
231 * the rows, just updates the cell values.
232 */
233void ql_update_list_store(struct queuelike *ql) {
234 D(("ql_update_list_store"));
235 GtkTreeIter iter[1];
236 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
237 for(struct queue_entry *q = ql->q; q; q = q->next) {
238 ql_update_row(q, iter);
239 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
240 }
241}
242
83fb99f9
RK
243struct newqueue_data {
244 struct queue_entry *old, *new;
245};
246
247static void record_queue_map(hash *h,
248 const char *id,
249 struct queue_entry *old,
250 struct queue_entry *new) {
251 struct newqueue_data *nqd;
252
253 if(!(nqd = hash_find(h, id))) {
254 static const struct newqueue_data empty[1];
255 hash_add(h, id, empty, HASH_INSERT);
256 nqd = hash_find(h, id);
257 }
a59bf4da
RK
258 if(old) {
259#if DEBUG_QUEUE
260 fprintf(stderr, " old: %s\n", id);
261#endif
83fb99f9 262 nqd->old = old;
a59bf4da
RK
263 }
264 if(new) {
265#if DEBUG_QUEUE
266 fprintf(stderr, " new: %s\n", id);
267#endif
83fb99f9 268 nqd->new = new;
a59bf4da 269 }
83fb99f9
RK
270}
271
a59bf4da 272#if DEBUG_QUEUE
83fb99f9
RK
273static void dump_queue(struct queue_entry *head, struct queue_entry *mark) {
274 for(struct queue_entry *q = head; q; q = q->next) {
275 if(q == mark)
a59bf4da
RK
276 fprintf(stderr, " !");
277 fprintf(stderr, " %s\n", q->id);
83fb99f9 278 }
83fb99f9
RK
279}
280
281static void dump_rows(struct queuelike *ql) {
282 GtkTreeIter iter[1];
283 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
284 iter);
285 while(it) {
286 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
287 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
a59bf4da 288 fprintf(stderr, " %s\n", q->id);
83fb99f9 289 }
83fb99f9
RK
290}
291#endif
292
c133bd3c
RK
293/** @brief Reset the list store
294 * @param ql Queuelike to reset
83fb99f9 295 * @param newq New queue contents/ordering
c133bd3c 296 *
83fb99f9 297 * Updates the queue to match @p newq
c133bd3c
RK
298 */
299void ql_new_queue(struct queuelike *ql,
300 struct queue_entry *newq) {
301 D(("ql_new_queue"));
83fb99f9
RK
302 ++suppress_actions;
303
304 /* Tell every queue entry which queue owns it */
a59bf4da
RK
305#if DEBUG_QUEUE
306 fprintf(stderr, "%s: filling in q->ql\n", ql->name);
307#endif
83fb99f9 308 for(struct queue_entry *q = newq; q; q = q->next)
c133bd3c 309 q->ql = ql;
83fb99f9 310
a59bf4da
RK
311#if DEBUG_QUEUE
312 fprintf(stderr, "%s: constructing h\n", ql->name);
313#endif
83fb99f9
RK
314 /* Construct map from id to new and old structures */
315 hash *h = hash_new(sizeof(struct newqueue_data));
316 for(struct queue_entry *q = ql->q; q; q = q->next)
317 record_queue_map(h, q->id, q, NULL);
318 for(struct queue_entry *q = newq; q; q = q->next)
319 record_queue_map(h, q->id, NULL, q);
320
321 /* The easy bit: delete rows not present any more. In the same pass we
322 * update the secret column containing the queue_entry pointer. */
a59bf4da
RK
323#if DEBUG_QUEUE
324 fprintf(stderr, "%s: deleting rows...\n", ql->name);
325#endif
83fb99f9
RK
326 GtkTreeIter iter[1];
327 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
328 iter);
329 int inserted = 0, deleted = 0, kept = 0;
330 while(it) {
331 struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
332 const struct newqueue_data *nqd = hash_find(h, q->id);
333 if(nqd->new) {
334 /* Tell this row that it belongs to the new version of the queue */
0fb5e8f3
RK
335 gtk_list_store_set(ql->store, iter,
336 ql->ncolumns + QUEUEPOINTER_COLUMN, nqd->new,
337 -1);
83fb99f9
RK
338 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
339 ++kept;
340 } else {
341 /* Delete this row (and move iter to the next one) */
a59bf4da
RK
342#if DEBUG_QUEUE
343 fprintf(stderr, " delete %s\n", q->id);
344#endif
83fb99f9
RK
345 it = gtk_list_store_remove(ql->store, iter);
346 ++deleted;
347 }
c133bd3c 348 }
83fb99f9
RK
349
350 /* Now every row's secret column is right, but we might be missing new rows
351 * and they might be in the wrong order */
352
353 /* We're going to have to support arbitrary rearrangements, so we might as
354 * well add new elements at the end. */
a59bf4da
RK
355#if DEBUG_QUEUE
356 fprintf(stderr, "%s: adding rows...\n", ql->name);
357#endif
83fb99f9
RK
358 struct queue_entry *after = 0;
359 for(struct queue_entry *q = newq; q; q = q->next) {
360 const struct newqueue_data *nqd = hash_find(h, q->id);
361 if(!nqd->old) {
83fb99f9
RK
362 if(after) {
363 /* Try to insert at the right sort of place */
364 GtkTreeIter where[1];
365 gboolean wit = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
366 where);
367 while(wit && ql_iter_to_q(GTK_TREE_MODEL(ql->store), where) != after)
368 wit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), where);
369 if(wit)
370 gtk_list_store_insert_after(ql->store, iter, where);
371 else
372 gtk_list_store_append(ql->store, iter);
373 } else
374 gtk_list_store_prepend(ql->store, iter);
0fb5e8f3
RK
375 gtk_list_store_set(ql->store, iter,
376 ql->ncolumns + QUEUEPOINTER_COLUMN, q,
377 -1);
a59bf4da
RK
378#if DEBUG_QUEUE
379 fprintf(stderr, " add %s\n", q->id);
380#endif
83fb99f9
RK
381 ++inserted;
382 }
383 after = newq;
384 }
385
386 /* Now exactly the right set of rows are present, and they have the right
387 * queue_entry pointers in their secret column, but they may be in the wrong
388 * order.
389 *
390 * The current code is simple but amounts to a bubble-sort - we might easily
391 * called gtk_tree_model_iter_next a couple of thousand times.
392 */
a59bf4da
RK
393#if DEBUG_QUEUE
394 fprintf(stderr, "%s: rearranging rows\n", ql->name);
395 fprintf(stderr, "%s: target state:\n", ql->name);
396 dump_queue(newq, 0);
397 fprintf(stderr, "%s: current state:\n", ql->name);
398 dump_rows(ql);
399#endif
400 it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
401 struct queue_entry *tq = newq; /* t-for-target */
83fb99f9 402 int swaps = 0, searches = 0;
a59bf4da 403 int row = 0;
83fb99f9 404 while(it) {
a59bf4da
RK
405 struct queue_entry *cq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
406 /* c-for-current */
83fb99f9 407
a59bf4da
RK
408 /* Everything has the right queue pointer (see above) so it's sufficient to
409 * compare pointers to detect mismatches */
410 if(cq != tq) {
411#if DEBUG_QUEUE
412 fprintf(stderr, " pointer mismatch at row %d\n", row);
413 fprintf(stderr, " target id %s\n", tq->id);
414 fprintf(stderr, " actual id %s\n", cq->id);
415#endif
416 /* Start looking for the target row fromn the next row */
83fb99f9
RK
417 GtkTreeIter next[1] = { *iter };
418 gboolean nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
419 while(nit) {
420 struct queue_entry *nq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), next);
a59bf4da
RK
421#if DEBUG_QUEUE
422 fprintf(stderr, " candidate: %s\n", nq->id);
423#endif
424 if(nq == tq)
83fb99f9
RK
425 break;
426 nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
427 ++searches;
428 }
f57ff3aa
RK
429 /* Note that this assertion will fail in the face of duplicate IDs.
430 * q->id really does need to be unique. */
83fb99f9 431 assert(nit);
83fb99f9
RK
432 gtk_list_store_swap(ql->store, iter, next);
433 *iter = *next;
a59bf4da
RK
434#if DEBUG_QUEUE
435 fprintf(stderr, "%s: found it. new row state:\n", ql->name);
436 dump_rows(ql);
437#endif
83fb99f9
RK
438 ++swaps;
439 }
440 /* ...and onto the next one */
441 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
a59bf4da
RK
442 tq = tq->next;
443 ++row;
83fb99f9 444 }
a59bf4da 445#if DEBUG_QUEUE
83fb99f9
RK
446 fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name,
447 kept, inserted, deleted, swaps, searches);
a59bf4da 448 fprintf(stderr, "done\n");
83fb99f9 449#endif
83fb99f9
RK
450 ql->q = newq;
451 /* Set the rest of the columns in new rows */
452 ql_update_list_store(ql);
83fb99f9 453 --suppress_actions;
c133bd3c
RK
454}
455
78d746a3
RK
456/* Drag and drop ------------------------------------------------------------ */
457
458/** @brief Identify the drop path
459 * @param w Destination tree view widget
460 * @param model Underlying tree model
461 * @param wx X coordinate
462 * @param wy Y coordinate
463 * @param posp Where to store relative position
464 * @return Target path or NULL
465 *
466 * This is used by ql_drag_motion() and ql_drag_data_received() to identify a
467 * drop would or does land. It's important that they use the same code since
468 * otherwise the visual feedback can be inconsistent with the actual effect!
42eabfd7
RK
469 *
470 * Remember to free the returned path.
78d746a3
RK
471 */
472static GtkTreePath *ql_drop_path(GtkWidget *w,
473 GtkTreeModel *model,
474 int wx, int wy,
475 GtkTreeViewDropPosition *posp) {
476 GtkTreePath *path = NULL;
477 GtkTreeViewDropPosition pos;
478 GtkTreeIter iter[1], last[1];
479 int tx, ty;
480
481 gtk_tree_view_convert_widget_to_tree_coords(GTK_TREE_VIEW(w),
482 wx, wy, &tx, &ty);
483 if(gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(w),
484 wx, wy,
485 &path,
486 &pos)) {
487 //fprintf(stderr, "gtk_tree_view_get_dest_row_at_pos() -> TRUE\n");
488 // Normalize drop position
489 switch(pos) {
490 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
491 pos = GTK_TREE_VIEW_DROP_BEFORE;
492 break;
493 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
494 pos = GTK_TREE_VIEW_DROP_AFTER;
495 break;
496 default: break;
497 }
498 } else if(gtk_tree_model_get_iter_first(model, iter)) {
499 /* If the pointer isn't over any particular row then either it's below
500 * the last row, in which case we want the dropzone to be below that row;
501 * or it's above the first row (in the column headings) in which case we
502 * want the dropzone to be above that row. */
503 if(ty >= 0) {
504 /* Find the last row */
505 do {
506 *last = *iter;
507 } while(gtk_tree_model_iter_next(model, iter));
508 /* The drop target is just after it */
509 pos = GTK_TREE_VIEW_DROP_AFTER;
510 *iter = *last;
511 } else {
512 /* The drop target will be just before the first row */
513 pos = GTK_TREE_VIEW_DROP_BEFORE;
514 }
515 path = gtk_tree_model_get_path(model, iter);
516 }
517 *posp = pos;
518 return path;
519}
520
30b358a3
RK
521/** @brief Called when a drag moves within a candidate destination
522 * @param w Destination widget
523 * @param dc Drag context
524 * @param x Current pointer location
525 * @param y Current pointer location
526 * @param time_ Current time
527 * @param user_data Pointer to queuelike
528 * @return TRUE in a dropzone, otherwise FALSE
df73cc67
RK
529 *
530 * This is the handler for the "drag-motion" signal.
30b358a3 531 */
a6e6b359
RK
532static gboolean ql_drag_motion(GtkWidget *w,
533 GdkDragContext *dc,
534 gint x,
535 gint y,
536 guint time_,
78d746a3
RK
537 gpointer user_data) {
538 struct queuelike *const ql = user_data;
a6e6b359 539 GdkDragAction action = 0;
78d746a3 540
a6e6b359
RK
541 // GTK_DEST_DEFAULT_MOTION vets actions as follows:
542 // 1) if dc->suggested_action is in the gtk_drag_dest_set actions
543 // then dc->suggested_action is taken as the action.
544 // 2) otherwise if dc->actions intersects the gtk_drag_dest_set actions
545 // then the lowest-numbered member of the intersection is chosen.
546 // 3) otherwise no member is chosen and gdk_drag_status() is called
547 // with action=0 to refuse the drop.
a6e6b359 548 if(dc->suggested_action) {
4b51f265 549 if(dc->suggested_action & (GDK_ACTION_MOVE|GDK_ACTION_COPY))
a6e6b359
RK
550 action = dc->suggested_action;
551 } else if(dc->actions & GDK_ACTION_MOVE)
4b51f265
RK
552 action = GDK_ACTION_MOVE;
553 else if(dc->actions & GDK_ACTION_COPY)
554 action = GDK_ACTION_COPY;
a6e6b359
RK
555 /*fprintf(stderr, "suggested %#x actions %#x result %#x\n",
556 dc->suggested_action, dc->actions, action);*/
557 if(action) {
558 // If the action is acceptable then we see if this widget is acceptable
4b51f265 559 if(gtk_drag_dest_find_target(w, dc, NULL) == GDK_NONE)
a6e6b359
RK
560 action = 0;
561 }
562 // Report the status
78d746a3 563 //fprintf(stderr, "drag action: %u\n", action);
a6e6b359
RK
564 gdk_drag_status(dc, action, time_);
565 if(action) {
a6e6b359
RK
566 GtkTreeViewDropPosition pos;
567
78d746a3
RK
568 // Find the drop target
569 GtkTreePath *path = ql_drop_path(w, GTK_TREE_MODEL(ql->store), x, y, &pos);
570 // Highlight drop target
571 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w), path, pos);
572 if(path)
573 gtk_tree_path_free(path);
a6e6b359 574 }
df73cc67 575 autoscroll_add(GTK_TREE_VIEW(w));
98c3d1f1 576 return TRUE; /* We are (always) in a drop zone */
a6e6b359 577}
82db9336 578
30b358a3
RK
579/** @brief Called when a drag moves leaves a candidate destination
580 * @param w Destination widget
581 * @param dc Drag context
582 * @param time_ Current time
583 * @param user_data Pointer to queuelike
df73cc67
RK
584 *
585 * This is the handler for the "drag-leave" signal.
586 *
587 * It turns out that we get a drag-leave event when the data is dropped, too
588 * (See _gtk_drag_dest_handle_event). This seems logically consistent and is
589 * convenient too - for instance it's why autoscroll_remove() gets called at
590 * the end of a drag+drop sequence.
30b358a3 591 */
a6e6b359
RK
592static void ql_drag_leave(GtkWidget *w,
593 GdkDragContext attribute((unused)) *dc,
594 guint attribute((unused)) time_,
595 gpointer attribute((unused)) user_data) {
596 //struct queuelike *const ql = user_data;
82db9336 597
a6e6b359 598 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w), NULL, 0);
df73cc67 599 autoscroll_remove(GTK_TREE_VIEW(w));
a6e6b359 600}
82db9336 601
0beb320e 602/** @brief Callback to add selected tracks to the selection data
82db9336 603 *
0beb320e 604 * Called from ql_drag_data_get().
82db9336 605 */
0beb320e
RK
606static void ql_drag_data_get_collect(GtkTreeModel *model,
607 GtkTreePath attribute((unused)) *path,
608 GtkTreeIter *iter,
609 gpointer data) {
610 struct dynstr *const result = data;
611 struct queue_entry *const q = ql_iter_to_q(model, iter);
612
613 dynstr_append_string(result, q->id);
614 dynstr_append(result, '\n');
615 dynstr_append_string(result, q->track);
616 dynstr_append(result, '\n');
617}
82db9336 618
0beb320e
RK
619/** @brief Called to extract the dragged data from the source queuelike
620 * @param w Source widget (the tree view)
621 * @param dc Drag context
622 * @param data Where to put the answer
623 * @param info_ Target @c info parameter
624 * @param time_ Time data requested (for some reason not a @c time_t)
625 * @param user_data The queuelike
ff18efce
RK
626 *
627 * The list of tracks is converted into a single string, consisting of IDs
628 * and track names. Each is terminated by a newline. Including both ID and
629 * track name means that the receiver can use whichever happens to be more
630 * convenient.
631 *
632 * If there are no IDs for rows in this widget then the ID half is undefined.
df73cc67
RK
633 *
634 * This is the handler for the "drag-data-get" signal.
0beb320e
RK
635 */
636static void ql_drag_data_get(GtkWidget attribute((unused)) *w,
637 GdkDragContext attribute((unused)) *dc,
638 GtkSelectionData *data,
639 guint attribute((unused)) info_,
640 guint attribute((unused)) time_,
641 gpointer user_data) {
82db9336 642 struct queuelike *const ql = user_data;
0beb320e
RK
643 struct dynstr result[1];
644
0beb320e
RK
645 dynstr_init(result);
646 gtk_tree_selection_selected_foreach(ql->selection,
647 ql_drag_data_get_collect,
648 result);
4b51f265 649 // TODO must not be able to drag playing track!
0beb320e
RK
650 //fprintf(stderr, "drag-data-get: %.*s\n",
651 // result->nvec, result->vec);
652 /* gtk_selection_data_set_text() insists that data->target is one of a
653 * variety of stringy atoms. TODO: where does this value actually come
654 * from? */
655 gtk_selection_data_set(data,
656 GDK_TARGET_STRING,
657 8, (guchar *)result->vec, result->nvec);
82db9336
RK
658}
659
0beb320e
RK
660/** @brief Called when drag data is received
661 * @param w Target widget (the tree view)
662 * @param dc Drag context
663 * @param x The drop location
664 * @param y The drop location
665 * @param data The selection data
666 * @param info_ The target type that was chosen
667 * @param time_ Time data received (for some reason not a @c time_t)
668 * @param user_data The queuelike
df73cc67
RK
669 *
670 * This is the handler for the "drag-data-received" signal.
0beb320e
RK
671 */
672static void ql_drag_data_received(GtkWidget attribute((unused)) *w,
673 GdkDragContext attribute((unused)) *dc,
674 gint x,
675 gint y,
676 GtkSelectionData *data,
677 guint attribute((unused)) info_,
678 guint attribute((unused)) time_,
679 gpointer user_data) {
82db9336 680 struct queuelike *const ql = user_data;
0beb320e
RK
681 char *result, *p;
682 struct vector ids[1], tracks[1];
683 int parity = 0;
684
4b51f265 685 //fprintf(stderr, "drag-data-received: %d,%d info_=%u\n", x, y, info_);
0beb320e
RK
686 /* Get the selection string */
687 p = result = (char *)gtk_selection_data_get_text(data);
688 if(!result) {
689 //fprintf(stderr, "gtk_selection_data_get_text() returned NULL\n");
690 return;
691 }
692 //fprintf(stderr, "%s--\n", result);
693 /* Parse it back into IDs and track names */
694 vector_init(ids);
695 vector_init(tracks);
696 while(*p) {
697 char *nl = strchr(p, '\n');
698 if(!nl)
699 break;
700 *nl = 0;
701 //fprintf(stderr, " %s\n", p);
702 vector_append(parity++ & 1 ? tracks : ids, xstrdup(p));
703 p = nl + 1;
704 }
705 g_free(result);
706 if(ids->nvec != tracks->nvec) {
707 //fprintf(stderr, " inconsistent drag data!\n");
708 return;
709 }
710 vector_terminate(ids);
711 vector_terminate(tracks);
712 /* Figure out which row the drop precedes (if any) */
0beb320e
RK
713 GtkTreeViewDropPosition pos;
714 struct queue_entry *q;
78d746a3
RK
715 GtkTreePath *path = ql_drop_path(w, GTK_TREE_MODEL(ql->store), x, y, &pos);
716 if(path) {
717 q = ql_path_to_q(GTK_TREE_MODEL(ql->store), path);
718 } else {
0beb320e
RK
719 /* This generally means a drop past the end of the queue. We find the last
720 * element in the queue and ask to move after that. */
721 for(q = ql->q; q && q->next; q = q->next)
722 ;
78d746a3
RK
723 }
724 switch(pos) {
725 case GTK_TREE_VIEW_DROP_BEFORE:
726 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
727 if(q) {
728 q = q->prev;
729 //fprintf(stderr, " ...but we like to drop near %s\n",
730 // q ? q->id : "NULL");
82db9336 731 }
78d746a3
RK
732 break;
733 default:
734 break;
82db9336 735 }
4b51f265
RK
736 /* Guarantee we never drop an empty list */
737 if(!tracks->nvec)
738 return;
0beb320e
RK
739 /* Note that q->id can match one of ids[]. This doesn't matter for
740 * moveafter but TODO may matter for playlist support. */
4b51f265
RK
741 switch(info_) {
742 case 0:
743 /* Rearrangement. Send ID and track data. */
744 ql->drop(ql, tracks->nvec, tracks->vec, ids->vec, q);
745 break;
746 case 1:
747 /* Copying between widgets. IDs mean nothing so don't send them. */
748 ql->drop(ql, tracks->nvec, tracks->vec, NULL, q);
749 break;
750 }
42eabfd7
RK
751 if(path)
752 gtk_tree_path_free(path);
82db9336
RK
753}
754
c133bd3c
RK
755/** @brief Initialize a @ref queuelike */
756GtkWidget *init_queuelike(struct queuelike *ql) {
757 D(("init_queuelike"));
0fb5e8f3
RK
758 /* Create the list store. We add an extra column to hold a pointer to the
759 * queue_entry. */
760 GType *types = xcalloc(ql->ncolumns + EXTRA_COLUMNS, sizeof (GType));
761 for(int n = 0; n < ql->ncolumns + EXTRA_COLUMNS; ++n)
c133bd3c 762 types[n] = G_TYPE_STRING;
0fb5e8f3
RK
763 types[ql->ncolumns + QUEUEPOINTER_COLUMN] = G_TYPE_POINTER;
764 ql->store = gtk_list_store_newv(ql->ncolumns + EXTRA_COLUMNS, types);
22717074 765 g_object_set_data(G_OBJECT(ql->store), "ql", (void *)ql);
c133bd3c
RK
766
767 /* Create the view */
768 ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store));
1583d68f 769 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql->view), TRUE);
c133bd3c
RK
770
771 /* Create cell renderers and label columns */
772 for(int n = 0; n < ql->ncolumns; ++n) {
773 GtkCellRenderer *r = gtk_cell_renderer_text_new();
b0b15b7c
RK
774 if(ql->columns[n].flags & COL_ELLIPSIZE)
775 g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_END, (char *)0);
776 if(ql->columns[n].flags & COL_RIGHT)
777 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
c133bd3c
RK
778 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
779 (ql->columns[n].name,
780 r,
781 "text", n,
0fb5e8f3
RK
782 "background", ql->ncolumns + BACKGROUND_COLUMN,
783 "foreground", ql->ncolumns + FOREGROUND_COLUMN,
c133bd3c 784 (char *)0);
3ffb8e5d
RK
785 gtk_tree_view_column_set_resizable(c, TRUE);
786 gtk_tree_view_column_set_reorderable(c, TRUE);
b0b15b7c
RK
787 if(ql->columns[n].flags & COL_EXPAND)
788 g_object_set(c, "expand", TRUE, (char *)0);
c133bd3c
RK
789 gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c);
790 }
791
792 /* The selection should support multiple things being selected */
793 ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view));
6bfaa2a1 794 g_object_ref(ql->selection);
c133bd3c
RK
795 gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE);
796
c133bd3c 797 /* Catch button presses */
17c36802 798 g_signal_connect(ql->view, "button-press-event",
c133bd3c
RK
799 G_CALLBACK(ql_button_release), ql);
800
82db9336
RK
801 /* Drag+drop*/
802 if(ql->drop) {
0beb320e
RK
803 /* Originally this was:
804 *
805 * gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql->view), TRUE);
806 *
807 * However this has a two deficiencies:
808 *
809 * 1) Only one row can be dragged at once. It would be nice
810 * to be able to do bulk rearrangements since the server
811 * can cope with that well.
812 * 2) Dragging between windows is not possible. When playlist
813 * support appears, it should be possible to drag tracks
814 * from the choose tag into the playlist.
815 *
816 * At the time of writing neither of these problems are fully solved, the
817 * code as it stands is just a stepping stone in that direction.
818 */
819
820 /* This view will act as a drag source */
821 gtk_drag_source_set(ql->view,
822 GDK_BUTTON1_MASK,
823 queuelike_targets,
824 sizeof queuelike_targets / sizeof *queuelike_targets,
825 GDK_ACTION_MOVE);
a6e6b359 826 /* This view will act as a drag destination */
0beb320e 827 gtk_drag_dest_set(ql->view,
a6e6b359 828 GTK_DEST_DEFAULT_HIGHLIGHT|GTK_DEST_DEFAULT_DROP,
0beb320e
RK
829 queuelike_targets,
830 sizeof queuelike_targets / sizeof *queuelike_targets,
4b51f265 831 GDK_ACTION_MOVE|GDK_ACTION_COPY);
a6e6b359
RK
832 g_signal_connect(ql->view, "drag-motion",
833 G_CALLBACK(ql_drag_motion), ql);
834 g_signal_connect(ql->view, "drag-leave",
835 G_CALLBACK(ql_drag_leave), ql);
0beb320e
RK
836 g_signal_connect(ql->view, "drag-data-get",
837 G_CALLBACK(ql_drag_data_get), ql);
838 g_signal_connect(ql->view, "drag-data-received",
839 G_CALLBACK(ql_drag_data_received), ql);
ff18efce
RK
840 make_treeview_multidrag(ql->view, NULL);
841 // TODO playing track should be refused by predicate arg
0beb320e 842 } else {
4b51f265
RK
843 /* For queues that cannot accept a drop we still accept a copy out */
844 gtk_drag_source_set(ql->view,
845 GDK_BUTTON1_MASK,
846 queuelike_targets,
847 sizeof queuelike_targets / sizeof *queuelike_targets,
848 GDK_ACTION_COPY);
4b51f265
RK
849 g_signal_connect(ql->view, "drag-data-get",
850 G_CALLBACK(ql_drag_data_get), ql);
ff18efce 851 make_treeview_multidrag(ql->view, NULL);
82db9336
RK
852 }
853
c133bd3c 854 /* TODO style? */
c133bd3c 855
6bfaa2a1
RK
856 if(ql->init)
857 ql->init(ql);
c133bd3c 858
ad47bd4c
RK
859 /* Update display text when lookups complete */
860 event_register("lookups-completed", queue_lookups_completed, ql);
861
ee7552f8
RK
862 GtkWidget *scrolled = scroll_widget(ql->view);
863 g_object_set_data(G_OBJECT(scrolled), "type", (void *)ql_tabtype(ql));
864 return scrolled;
c133bd3c
RK
865}
866
6bfaa2a1
RK
867/** @brief Destroy a queuelike
868 * @param ql Queuelike to destroy
869 *
870 * Returns @p ql to its initial state.
871 */
872void destroy_queuelike(struct queuelike *ql) {
873 if(ql->store) {
874 g_object_unref(ql->store);
875 ql->store = NULL;
876 }
877 if(ql->view) {
878 gtk_object_destroy(GTK_OBJECT(ql->view));
879 ql->view = NULL;
880 }
881 if(ql->menu) {
882 gtk_object_destroy(GTK_OBJECT(ql->menu));
883 ql->menu = NULL;
884 }
885 if(ql->selection) {
886 g_object_unref(ql->selection);
887 ql->selection = NULL;
888 }
889 ql->q = NULL;
890}
891
c133bd3c
RK
892/*
893Local Variables:
894c-basic-offset:2
895comment-column:40
896fill-column:79
897indent-tabs-mode:nil
898End:
899*/