+
+ //fprintf(stderr, "%s: constructing h\n", ql->name);
+ /* Construct map from id to new and old structures */
+ hash *h = hash_new(sizeof(struct newqueue_data));
+ for(struct queue_entry *q = ql->q; q; q = q->next)
+ record_queue_map(h, q->id, q, NULL);
+ for(struct queue_entry *q = newq; q; q = q->next)
+ record_queue_map(h, q->id, NULL, q);
+
+ /* The easy bit: delete rows not present any more. In the same pass we
+ * update the secret column containing the queue_entry pointer. */
+ //fprintf(stderr, "%s: deleting rows...\n", ql->name);
+ GtkTreeIter iter[1];
+ gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
+ iter);
+ int inserted = 0, deleted = 0, kept = 0;
+ while(it) {
+ struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
+ const struct newqueue_data *nqd = hash_find(h, q->id);
+ if(nqd->new) {
+ /* Tell this row that it belongs to the new version of the queue */
+ gtk_list_store_set(ql->store, iter,
+ ql->ncolumns + QUEUEPOINTER_COLUMN, nqd->new,
+ -1);
+ it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
+ ++kept;
+ } else {
+ /* Delete this row (and move iter to the next one) */
+ //fprintf(stderr, " delete %s", q->id);
+ it = gtk_list_store_remove(ql->store, iter);
+ ++deleted;
+ }
+ }
+
+ /* Now every row's secret column is right, but we might be missing new rows
+ * and they might be in the wrong order */
+
+ /* We're going to have to support arbitrary rearrangements, so we might as
+ * well add new elements at the end. */
+ //fprintf(stderr, "%s: adding rows...\n", ql->name);
+ struct queue_entry *after = 0;
+ for(struct queue_entry *q = newq; q; q = q->next) {
+ const struct newqueue_data *nqd = hash_find(h, q->id);
+ if(!nqd->old) {
+ if(after) {
+ /* Try to insert at the right sort of place */
+ GtkTreeIter where[1];
+ gboolean wit = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
+ where);
+ while(wit && ql_iter_to_q(GTK_TREE_MODEL(ql->store), where) != after)
+ wit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), where);
+ if(wit)
+ gtk_list_store_insert_after(ql->store, iter, where);
+ else
+ gtk_list_store_append(ql->store, iter);
+ } else
+ gtk_list_store_prepend(ql->store, iter);
+ gtk_list_store_set(ql->store, iter,
+ ql->ncolumns + QUEUEPOINTER_COLUMN, q,
+ -1);
+ //fprintf(stderr, " add %s", q->id);
+ ++inserted;
+ }
+ after = newq;
+ }
+
+ /* Now exactly the right set of rows are present, and they have the right
+ * queue_entry pointers in their secret column, but they may be in the wrong
+ * order.
+ *
+ * The current code is simple but amounts to a bubble-sort - we might easily
+ * called gtk_tree_model_iter_next a couple of thousand times.
+ */
+ //fprintf(stderr, "%s: rearranging rows\n", ql->name);
+ //fprintf(stderr, "%s: queue state: ", ql->name);
+ //dump_queue(newq, 0);
+ //fprintf(stderr, "%s: row state: ", ql->name);
+ //dump_rows(ql);
+ it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store),
+ iter);
+ struct queue_entry *rq = newq; /* r for 'right, correct' */
+ int swaps = 0, searches = 0;
+ while(it) {
+ struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter);
+ //fprintf(stderr, " rq = %p, q = %p\n", rq, q);
+ //fprintf(stderr, " rq->id = %s, q->id = %s\n", rq->id, q->id);
+
+ if(q != rq) {
+ //fprintf(stderr, " mismatch\n");
+ GtkTreeIter next[1] = { *iter };
+ gboolean nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
+ while(nit) {
+ struct queue_entry *nq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), next);
+ //fprintf(stderr, " candidate: %s\n", nq->id);
+ if(nq == rq)
+ break;
+ nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next);
+ ++searches;
+ }
+ assert(nit);
+ //fprintf(stderr, " found it\n");
+ gtk_list_store_swap(ql->store, iter, next);
+ *iter = *next;
+ //fprintf(stderr, "%s: new row state: ", ql->name);
+ //dump_rows(ql);
+ ++swaps;
+ }
+ /* ...and onto the next one */
+ it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
+ rq = rq->next;
+ }
+#if 0
+ fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name,
+ kept, inserted, deleted, swaps, searches);
+#endif
+ //fprintf(stderr, "done\n");
+ ql->q = newq;
+ /* Set the rest of the columns in new rows */
+ ql_update_list_store(ql);
+ --suppress_actions;
+}
+
+/* Drag and drop has to be figured out experimentally, because it is not well
+ * documented.
+ *
+ * First you get a row-inserted. The path argument points to the destination
+ * row but this will not yet have had its values set. The source row is still
+ * present. AFAICT the iter argument points to the same place.
+ *
+ * Then you get a row-deleted. The path argument identifies the row that was
+ * deleted. By this stage the row inserted above has acquired its values.
+ *
+ * A complication is that the deletion will move the inserted row. For
+ * instance, if you do a drag that moves row 1 down to after the track that was
+ * formerly on row 9, in the row-inserted call it will show up as row 10, but
+ * in the row-deleted call, row 1 will have been deleted thus making the
+ * inserted row be row 9.
+ *
+ * So when we see the row-inserted we have no idea what track to move.
+ * Therefore we stash it until we see a row-deleted.
+ */
+
+/** @brief row-inserted callback */
+static void ql_row_inserted(GtkTreeModel attribute((unused)) *treemodel,
+ GtkTreePath *path,
+ GtkTreeIter attribute((unused)) *iter,
+ gpointer user_data) {
+ struct queuelike *const ql = user_data;
+ if(!suppress_actions) {
+#if 0
+ char *ps = gtk_tree_path_to_string(path);
+ GtkTreeIter piter[1];
+ gboolean pi = gtk_tree_model_get_iter(treemodel, piter, path);
+ struct queue_entry *pq = pi ? ql_iter_to_q(treemodel, piter) : 0;
+ struct queue_entry *iq = ql_iter_to_q(treemodel, iter);
+
+ fprintf(stderr, "row-inserted %s path=%s pi=%d pq=%p path=%s iq=%p iter=%s\n",
+ ql->name,
+ ps,
+ pi,
+ pq,
+ (pi
+ ? (pq ? pq->track : "(pq=0)")
+ : "(pi=FALSE)"),
+ iq,
+ iq ? iq->track : "(iq=0)");
+
+ GtkTreeIter j[1];
+ gboolean jt = gtk_tree_model_get_iter_first(treemodel, j);
+ int row = 0;
+ while(jt) {
+ struct queue_entry *q = ql_iter_to_q(treemodel, j);
+ fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)");
+ jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), j);
+ }
+ g_free(ps);
+#endif
+ /* Remember an iterator pointing at the insertion target */
+ if(ql->drag_target)
+ gtk_tree_path_free(ql->drag_target);
+ ql->drag_target = gtk_tree_path_copy(path);
+ }
+}
+
+/** @brief row-deleted callback */
+static void ql_row_deleted(GtkTreeModel attribute((unused)) *treemodel,
+ GtkTreePath *path,
+ gpointer user_data) {
+ struct queuelike *const ql = user_data;
+
+ if(!suppress_actions) {
+#if 0
+ char *ps = gtk_tree_path_to_string(path);
+ fprintf(stderr, "row-deleted %s path=%s ql->drag_target=%s\n",
+ ql->name, ps, gtk_tree_path_to_string(ql->drag_target));
+ GtkTreeIter j[1];
+ gboolean jt = gtk_tree_model_get_iter_first(treemodel, j);
+ int row = 0;
+ while(jt) {
+ struct queue_entry *q = ql_iter_to_q(treemodel, j);
+ fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)");
+ jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), j);
+ }
+ g_free(ps);
+#endif
+ if(!ql->drag_target) {
+ error(0, "%s: unsuppressed row-deleted with no row-inserted",
+ ql->name);
+ return;
+ }
+
+ /* Get the source and destination row numbers. */
+ int srcrow = gtk_tree_path_get_indices(path)[0];
+ int dstrow = gtk_tree_path_get_indices(ql->drag_target)[0];
+ //fprintf(stderr, "srcrow=%d dstrow=%d\n", srcrow, dstrow);
+
+ /* Note that the source row is computed AFTER the destination has been
+ * inserted, since GTK+ does the insert before the delete. Therefore if
+ * the source row is south (higher row number) of the destination, it will
+ * be one higher than expected.
+ *
+ * For instance if we drag row 1 to before row 0 we will see row-inserted
+ * for row 0 but then a row-deleted for row 2.
+ */
+ if(srcrow > dstrow)
+ --srcrow;
+
+ /* Tell the queue implementation */
+ ql->drop(srcrow, dstrow);
+
+ /* Dispose of stashed data */
+ gtk_tree_path_free(ql->drag_target);
+ ql->drag_target = 0;