+/** @brief Called with new tracks for the playlist */
+static void playlists_editor_received_tracks(void *v,
+ const char *err,
+ int nvec, char **vec) {
+ const char *playlist = v;
+ if(err) {
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+ return;
+ }
+ if(!playlist_picker_selected
+ || strcmp(playlist, playlist_picker_selected)) {
+ /* The tracks are for the wrong playlist - something must have changed
+ * while the fetch command was in flight. We just ignore this callback,
+ * the right answer will be requested and arrive in due course. */
+ return;
+ }
+ if(nvec == -1)
+ /* No such playlist, presumably we'll get a deleted event shortly */
+ return;
+ /* Translate the list of tracks into queue entries */
+ struct queue_entry *newq, **qq = &newq, *qprev = NULL;
+ hash *h = hash_new(sizeof(int));
+ for(int n = 0; n < nvec; ++n) {
+ struct queue_entry *q = xmalloc(sizeof *q);
+ q->prev = qprev;
+ q->track = vec[n];
+ /* Synthesize a unique ID so that the selection survives updates. Tracks
+ * can appear more than once in the queue so we can't use raw track names,
+ * so we add a serial number to the start. */
+ int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0;
+ byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]);
+ hash_add(h, vec[n], &serial, HASH_INSERT_OR_REPLACE);
+ *qq = q;
+ qq = &q->next;
+ qprev = q;
+ }
+ *qq = NULL;
+ ql_new_queue(&ql_playlist, newq);
+}
+
+static void playlist_editor_ok(GtkButton attribute((unused)) *button,
+ gpointer attribute((unused)) userdata) {
+ gtk_widget_destroy(playlist_window);
+}
+
+static void playlist_editor_help(GtkButton attribute((unused)) *button,
+ gpointer attribute((unused)) userdata) {
+ popup_help("playlists.html");
+}
+
+/* Playlist mutation -------------------------------------------------------- */
+
+/** @brief State structure for guarded playlist modification
+ *
+ * To safely move, insert or delete rows we must:
+ * - take a lock
+ * - fetch the playlist
+ * - verify it's not changed
+ * - update the playlist contents
+ * - store the playlist
+ * - release the lock
+ *
+ * The playlist_modify_ functions do just that.
+ *
+ * To kick things off create one of these and disorder_eclient_playlist_lock()
+ * with playlist_modify_locked() as its callback. @c modify will be called; it
+ * should disorder_eclient_playlist_set() to set the new state with
+ * playlist_modify_updated() as its callback.
+ */
+struct playlist_modify_data {
+ /** @brief Affected playlist */
+ const char *playlist;
+ /** @brief Modification function
+ * @param mod Pointer back to state structure
+ * @param ntracks Length of playlist
+ * @param tracks Tracks in playlist
+ */
+ void (*modify)(struct playlist_modify_data *mod,
+ int ntracks, char **tracks);
+
+ /** @brief Number of tracks dropped */
+ int ntracks;
+ /** @brief Track names dropped */
+ char **tracks;
+ /** @brief Track IDs dropped */
+ char **ids;
+ /** @brief Drop after this point */
+ struct queue_entry *after_me;
+};
+
+/** @brief Called with playlist locked
+ *
+ * This is the entry point for guarded modification ising @ref
+ * playlist_modify_data.
+ */
+static void playlist_modify_locked(void *v, const char *err) {
+ struct playlist_modify_data *mod = v;
+ if(err) {
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+ return;
+ }
+ disorder_eclient_playlist_get(client, playlist_modify_retrieved,
+ mod->playlist, mod);
+}
+
+/** @brief Called with current playlist contents
+ * Checks that the playlist is still current and has not changed.
+ */
+void playlist_modify_retrieved(void *v, const char *err,
+ int nvec,
+ char **vec) {
+ struct playlist_modify_data *mod = v;
+ if(err) {
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+ disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
+ return;
+ }
+ if(nvec < 0
+ || !playlist_picker_selected
+ || strcmp(mod->playlist, playlist_picker_selected)) {
+ disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
+ return;
+ }
+ /* We check that the contents haven't changed. If they have we just abandon
+ * the operation. The user will have to try again. */
+ struct queue_entry *q;
+ int n;
+ for(n = 0, q = ql_playlist.q; q && n < nvec; ++n, q = q->next)
+ if(strcmp(q->track, vec[n]))
+ break;
+ if(n != nvec || q != NULL) {
+ disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
+ return;
+ }
+ mod->modify(mod, nvec, vec);
+}
+
+/** @brief Called when the playlist has been updated */
+static void playlist_modify_updated(void attribute((unused)) *v,
+ const char *err) {
+ if(err)
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+ disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
+}
+
+/** @brief Called when the playlist has been unlocked */
+static void playlist_modify_unlocked(void attribute((unused)) *v,
+ const char *err) {
+ if(err)
+ popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
+}
+
+/* Drop tracks into a playlist ---------------------------------------------- */
+
+static void playlist_drop(struct queuelike attribute((unused)) *ql,
+ int ntracks,
+ char **tracks, char **ids,
+ struct queue_entry *after_me) {
+ struct playlist_modify_data *mod = xmalloc(sizeof *mod);
+
+ mod->playlist = playlist_picker_selected;
+ mod->modify = playlist_drop_modify;
+ mod->ntracks = ntracks;
+ mod->tracks = tracks;
+ mod->ids = ids;
+ mod->after_me = after_me;
+ disorder_eclient_playlist_lock(client, playlist_modify_locked,
+ mod->playlist, mod);
+}
+
+/** @brief Return true if track @p i is in the moved set */
+static int playlist_drop_is_moved(struct playlist_modify_data *mod,
+ int i) {
+ struct queue_entry *q;
+
+ /* Find the q corresponding to i, so we can get the ID */
+ for(q = ql_playlist.q; i; q = q->next, --i)
+ ;
+ /* See if track i matches any of the moved set by ID */
+ for(int n = 0; n < mod->ntracks; ++n)
+ if(!strcmp(q->id, mod->ids[n]))
+ return 1;
+ return 0;
+}
+
+static void playlist_drop_modify(struct playlist_modify_data *mod,
+ int nvec, char **vec) {
+ char **newvec;
+ int nnewvec;
+
+ //fprintf(stderr, "\nplaylist_drop_modify\n");
+ /* after_me is the queue_entry to insert after, or NULL to insert at the
+ * beginning (including the case when the playlist is empty) */
+ //fprintf(stderr, "after_me = %s\n",
+ // mod->after_me ? mod->after_me->track : "NULL");
+ struct queue_entry *q = ql_playlist.q;
+ int ins = 0;
+ if(mod->after_me) {
+ ++ins;
+ while(q && q != mod->after_me) {
+ q = q->next;
+ ++ins;
+ }
+ }
+ /* Now ins is the index to insert at; equivalently, the row to insert before,
+ * and so equal to nvec to append. */
+#if 0
+ fprintf(stderr, "ins = %d = %s\n",
+ ins, ins < nvec ? vec[ins] : "NULL");
+ for(int n = 0; n < nvec; ++n)
+ fprintf(stderr, "%d: %s %s\n", n, n == ins ? "->" : " ", vec[n]);
+ fprintf(stderr, "nvec = %d\n", nvec);
+#endif
+ if(mod->ids) {
+ /* This is a rearrangement */
+ /* We have:
+ * - vec[], the current layout
+ * - ins, pointing into vec
+ * - mod->tracks[], a subset of vec[] which is to be moved
+ *
+ * ins is the insertion point BUT it is in terms of the whole
+ * array, i.e. before mod->tracks[] have been removed. The first
+ * step then is to remove everything in mod->tracks[] and adjust
+ * ins downwards as necessary.
+ */
+ /* First zero out anything that's moved */
+ int before_ins = 0;
+ for(int n = 0; n < nvec; ++n) {
+ if(playlist_drop_is_moved(mod, n)) {
+ vec[n] = NULL;
+ if(n < ins)
+ ++before_ins;
+ }
+ }
+ /* Now collapse down the array */
+ int i = 0;
+ for(int n = 0; n < nvec; ++n) {
+ if(vec[n])
+ vec[i++] = vec[n];
+ }
+ assert(i + mod->ntracks == nvec);
+ nvec = i;
+ /* Adjust the insertion point to take account of things moved from before
+ * it */
+ ins -= before_ins;
+ /* The effect is now the same as an insertion */
+ }
+ /* This is (now) an insertion */
+ nnewvec = nvec + mod->ntracks;
+ newvec = xcalloc(nnewvec, sizeof (char *));
+ memcpy(newvec, vec,
+ ins * sizeof (char *));
+ memcpy(newvec + ins, mod->tracks,
+ mod->ntracks * sizeof (char *));
+ memcpy(newvec + ins + mod->ntracks, vec + ins,
+ (nvec - ins) * sizeof (char *));
+ disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist,
+ newvec, nnewvec, mod);
+}
+
+/* Playlist editor right-click menu ---------------------------------------- */
+
+/** @brief Called to determine whether the playlist is playable */
+static int playlist_playall_sensitive(void attribute((unused)) *extra) {
+ /* If there's no playlist obviously we can't play it */
+ if(!playlist_picker_selected)
+ return FALSE;
+ /* If it's empty we can't play it */
+ if(!ql_playlist.q)
+ return FALSE;
+ /* Otherwise we can */
+ return TRUE;
+}
+
+/** @brief Called to play the selected playlist */
+static void playlist_playall_activate(GtkMenuItem attribute((unused)) *menuitem,
+ gpointer attribute((unused)) user_data) {
+ if(!playlist_picker_selected)
+ return;
+ /* Re-use the menu-based activation callback */
+ disorder_eclient_playlist_get(client, playlist_menu_received_content,
+ playlist_picker_selected, NULL);
+}
+
+/** @brief Called to determine whether the playlist is playable */
+static int playlist_remove_sensitive(void attribute((unused)) *extra) {
+ /* If there's no playlist obviously we can't remove from it */
+ if(!playlist_picker_selected)
+ return FALSE;
+ /* If no tracks are selected we cannot remove them */
+ if(!gtk_tree_selection_count_selected_rows(ql_playlist.selection))
+ return FALSE;
+ /* We're good to go */
+ return TRUE;
+}
+
+/** @brief Called to remove the selected playlist */
+static void playlist_remove_activate(GtkMenuItem attribute((unused)) *menuitem,
+ gpointer attribute((unused)) user_data) {
+ if(!playlist_picker_selected)
+ return;
+ struct playlist_modify_data *mod = xmalloc(sizeof *mod);
+
+ mod->playlist = playlist_picker_selected;
+ mod->modify = playlist_remove_modify;
+ disorder_eclient_playlist_lock(client, playlist_modify_locked,
+ mod->playlist, mod);
+}
+
+static void playlist_remove_modify(struct playlist_modify_data *mod,
+ int attribute((unused)) nvec, char **vec) {
+ GtkTreeIter iter[1];
+ gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql_playlist.store),
+ iter);
+ int n = 0, m = 0;
+ while(it) {
+ if(!gtk_tree_selection_iter_is_selected(ql_playlist.selection, iter))
+ vec[m++] = vec[n++];
+ else
+ n++;
+ it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_playlist.store), iter);
+ }
+ disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist,
+ vec, m, mod);
+}
+
+/* Playlists window --------------------------------------------------------- */
+
+/** @brief Pop up the playlists window
+ *
+ * Called when the playlists menu item is selected
+ */
+void playlist_window_create(gpointer attribute((unused)) callback_data,
+ guint attribute((unused)) callback_action,
+ GtkWidget attribute((unused)) *menu_item) {