+static void playlist_picker_destroy(void) {
+ playlist_picker_delete_button = NULL;
+ g_object_unref(playlist_picker_list);
+ playlist_picker_list = NULL;
+ playlist_picker_selection = NULL;
+ playlist_picker_selected = NULL;
+}
+
+/* Playlist editor ---------------------------------------------------------- */
+
+static GtkWidget *playlist_editor_shared;
+static GtkWidget *playlist_editor_public;
+static GtkWidget *playlist_editor_private;
+static int playlist_editor_setting_buttons;
+
+static GtkWidget *playlists_editor_create(void) {
+ assert(ql_playlist.view == NULL); /* better not be set up already */
+
+ GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
+ playlist_editor_shared = gtk_radio_button_new_with_label(NULL, "shared");
+ playlist_editor_public
+ = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared),
+ "public");
+ playlist_editor_private
+ = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared),
+ "private");
+ g_signal_connect(playlist_editor_public, "toggled",
+ G_CALLBACK(playlist_editor_button_toggled),
+ (void *)"public");
+ g_signal_connect(playlist_editor_private, "toggled",
+ G_CALLBACK(playlist_editor_button_toggled),
+ (void *)"private");
+ gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_shared,
+ FALSE/*expand*/, FALSE/*fill*/, 0);
+ gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_public,
+ FALSE/*expand*/, FALSE/*fill*/, 0);
+ gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_private,
+ FALSE/*expand*/, FALSE/*fill*/, 0);
+ playlist_editor_set_buttons(0,0,0);
+
+ GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(vbox), init_queuelike(&ql_playlist),
+ TRUE/*expand*/, TRUE/*fill*/, 0);
+ gtk_box_pack_start(GTK_BOX(vbox), hbox,
+ FALSE/*expand*/, FALSE/*fill*/, 0);
+ return vbox;
+}
+
+/** @brief Called when the public/private buttons are set */
+static void playlist_editor_button_toggled(GtkToggleButton *tb,
+ gpointer userdata) {
+ const char *state = userdata;
+ if(!gtk_toggle_button_get_active(tb)
+ || !playlist_picker_selected
+ || playlist_editor_setting_buttons)
+ return;
+ disorder_eclient_playlist_set_share(client, playlist_editor_share_set,
+ playlist_picker_selected, state, NULL);
+}
+
+static void playlist_editor_share_set(void attribute((unused)) *v,
+ const attribute((unused)) char *err) {
+ if(err)
+ popup_protocol_error(0, err);
+}
+
+/** @brief Set the editor button state and sensitivity */
+static void playlist_editor_set_buttons(const char attribute((unused)) *event,
+ void *eventdata,
+ void attribute((unused)) *callbackdata) {
+ /* If this event is for a non-selected playlist do nothing */
+ if(eventdata
+ && playlist_picker_selected
+ && strcmp(eventdata, playlist_picker_selected))
+ return;
+ if(playlist_picker_selected) {
+ if(strchr(playlist_picker_selected, '.'))
+ disorder_eclient_playlist_get_share(client,
+ playlist_editor_got_share,
+ playlist_picker_selected,
+ (void *)playlist_picker_selected);
+ else
+ playlist_editor_got_share((void *)playlist_picker_selected, NULL,
+ "shared");
+ } else
+ playlist_editor_got_share(NULL, NULL, NULL);
+}
+
+/** @brief Called with playlist sharing details */
+static void playlist_editor_got_share(void *v,
+ const char *err,
+ const char *value) {
+ const char *playlist = v;
+ if(err) {
+ popup_protocol_error(0, err);
+ value = NULL;
+ }
+ /* Set the currently active button */
+ ++playlist_editor_setting_buttons;
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_shared),
+ value && !strcmp(value, "shared"));
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_public),
+ value && !strcmp(value, "public"));
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_private),
+ value && !strcmp(value, "private"));
+ /* Set button sensitivity */
+ gtk_widget_set_sensitive(playlist_editor_shared, FALSE);
+ int sensitive = (playlist
+ && strchr(playlist, '.')
+ && !strncmp(playlist, config->username,
+ strlen(config->username)));
+ gtk_widget_set_sensitive(playlist_editor_public, sensitive);
+ gtk_widget_set_sensitive(playlist_editor_private, sensitive);
+ --playlist_editor_setting_buttons;
+}
+
+/** @brief (Re-)populate the playlist tree model */
+static void playlist_editor_fill(const char attribute((unused)) *event,
+ void *eventdata,
+ void attribute((unused)) *callbackdata) {
+ const char *modified_playlist = eventdata;
+ if(!playlist_window)
+ return;
+ if(!playlist_picker_selected)
+ return;
+ if(!strcmp(playlist_picker_selected, modified_playlist))
+ disorder_eclient_playlist_get(client, playlists_editor_received_tracks,
+ playlist_picker_selected,
+ (void *)playlist_picker_selected);
+}
+
+/** @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_protocol_error(0, 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);
+}
+
+/* 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_protocol_error(0, err);
+ return;
+ }
+ disorder_eclient_playlist_get(client, playlist_modify_retrieved,
+ mod->playlist, mod);
+}