+
+ //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, 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) {
+ GtkTreeIter iter[1];
+ 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, q, -1);
+ //fprintf(stderr, " add %s", q->id);
+ ++inserted;
+ }
+ after = newq;