+/** @brief Called to extract the dragged data from the source queuelike
+ * @param w Source widget (the tree view)
+ * @param dc Drag context
+ * @param data Where to put the answer
+ * @param info_ Target @c info parameter
+ * @param time_ Time data requested (for some reason not a @c time_t)
+ * @param user_data The queuelike
+ *
+ * The list of tracks is converted into a single string, consisting of IDs
+ * and track names. Each is terminated by a newline. Including both ID and
+ * track name means that the receiver can use whichever happens to be more
+ * convenient.
+ *
+ * If there are no IDs for rows in this widget then the ID half is undefined.
+ *
+ * This is the handler for the "drag-data-get" signal.
+ */
+static void ql_drag_data_get(GtkWidget attribute((unused)) *w,
+ GdkDragContext attribute((unused)) *dc,
+ GtkSelectionData *data,
+ guint attribute((unused)) info,
+ guint attribute((unused)) time_,
+ gpointer user_data) {
+ struct queuelike *const ql = user_data;
+ struct dynstr result[1];
+
+ //fprintf(stderr, "ql_drag_data_get %s info=%d\n", ql->name, info);
+ dynstr_init(result);
+ gtk_tree_selection_selected_foreach(ql->selection,
+ ql_drag_data_get_collect,
+ result);
+ // TODO must not be able to drag playing track!
+ //fprintf(stderr, "drag-data-get: %.*s\n",
+ // result->nvec, result->vec);
+ /* gtk_selection_data_set_text() insists that data->target is one of a
+ * variety of stringy atoms. TODO: where does this value actually come
+ * from? */
+ gtk_selection_data_set(data,
+ GDK_TARGET_STRING,
+ 8, (guchar *)result->vec, result->nvec);
+}
+
+/** @brief Called when drag data is received
+ * @param w Target widget (the tree view)
+ * @param dc Drag context
+ * @param x The drop location
+ * @param y The drop location
+ * @param data The selection data
+ * @param info_ The target type that was chosen
+ * @param time_ Time data received (for some reason not a @c time_t)
+ * @param user_data The queuelike
+ *
+ * This is the handler for the "drag-data-received" signal.
+ */
+static void ql_drag_data_received(GtkWidget attribute((unused)) *w,
+ GdkDragContext attribute((unused)) *dc,
+ gint x,
+ gint y,
+ GtkSelectionData *data,
+ guint info_,
+ guint attribute((unused)) time_,
+ gpointer user_data) {
+ struct queuelike *const ql = user_data;
+ char *result, *p;
+ struct vector ids[1], tracks[1];
+ int parity = 0;
+
+ //fprintf(stderr, "drag-data-received: %d,%d info=%u\n", x, y, info_);
+ /* Get the selection string */
+ p = result = (char *)gtk_selection_data_get_text(data);
+ if(!result) {
+ //fprintf(stderr, "gtk_selection_data_get_text() returned NULL\n");
+ return;
+ }
+ //fprintf(stderr, "%s--\n", result);
+ /* Parse it back into IDs and track names */
+ vector_init(ids);
+ vector_init(tracks);
+ while(*p) {
+ char *nl = strchr(p, '\n');
+ if(!nl)
+ break;
+ *nl = 0;
+ //fprintf(stderr, " %s\n", p);
+ vector_append(parity++ & 1 ? tracks : ids, xstrdup(p));
+ p = nl + 1;
+ }
+ g_free(result);
+ if(ids->nvec != tracks->nvec) {
+ //fprintf(stderr, " inconsistent drag data!\n");
+ return;
+ }
+ vector_terminate(ids);
+ vector_terminate(tracks);
+ /* Figure out which row the drop precedes (if any) */
+ GtkTreeViewDropPosition pos;
+ struct queue_entry *q;
+ GtkTreePath *path = ql_drop_path(w, GTK_TREE_MODEL(ql->store), x, y, &pos);
+ if(path) {
+ q = ql_path_to_q(GTK_TREE_MODEL(ql->store), path);
+ //fprintf(stderr, " drop path: %s q=%p pos=%d\n",
+ // gtk_tree_path_to_string(path), q, pos);
+ } else {
+ /* This generally means a drop past the end of the queue. We find the last
+ * element in the queue and ask to move after that. */
+ for(q = ql->q; q && q->next; q = q->next)
+ ;
+ //fprintf(stderr, " after end. q=%p. pos=%d\n", q, pos);
+ }
+ switch(pos) {
+ case GTK_TREE_VIEW_DROP_BEFORE:
+ case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
+ if(q) {
+ q = q->prev;
+ //fprintf(stderr, " but we like to drop near %s\n",
+ // q ? q->id : "NULL");
+ }
+ break;
+ default:
+ break;
+ }
+ /* Guarantee we never drop an empty list */
+ if(!tracks->nvec)
+ return;
+ /* Note that q->id can match one of ids[]. This doesn't matter for
+ * moveafter but TODO may matter for playlist support. */
+ switch(info_) {
+ case QUEUED_TRACKS_ID:
+ case PLAYLIST_TRACKS_ID:
+ /* Rearrangement within some widget. Send ID and track data. */
+ ql->drop(ql, tracks->nvec, tracks->vec, ids->vec, q);
+ break;
+ case PLAYABLE_TRACKS_ID:
+ /* Copying between widgets. IDs mean nothing so don't send them. */
+ ql->drop(ql, tracks->nvec, tracks->vec, NULL, q);
+ break;