X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/7fe2807d48006b2191dfe8b1985176cd378e80c3..7a3eb8eb516c8cdb35eaf2e41030e802b623e713:/disobedience/playlists.c diff --git a/disobedience/playlists.c b/disobedience/playlists.c index 05de973..37bb0ea 100644 --- a/disobedience/playlists.c +++ b/disobedience/playlists.c @@ -48,6 +48,59 @@ static void playlist_list_received_playlists(void *v, static void playlist_editor_fill(const char *event, void *eventdata, void *callbackdata); +static int playlist_playall_sensitive(void *extra); +static void playlist_playall_activate(GtkMenuItem *menuitem, + gpointer user_data); +static int playlist_remove_sensitive(void *extra) ; +static void playlist_remove_activate(GtkMenuItem *menuitem, + gpointer user_data); +static void playlist_new_locked(void *v, const char *err); +static void playlist_new_retrieved(void *v, const char *err, + int nvec, + char **vec); +static void playlist_new_created(void *v, const char *err); +static void playlist_new_unlocked(void *v, const char *err); +static void playlist_new_entry_edited(GtkEditable *editable, + gpointer user_data); +static void playlist_new_button_toggled(GtkToggleButton *tb, + gpointer userdata); +static void playlist_new_changed(const char *event, + void *eventdata, + void *callbackdata); +static const char *playlist_new_valid(void); +static void playlist_new_details(char **namep, + char **fullnamep, + gboolean *sharedp, + gboolean *publicp, + gboolean *privatep); +static void playlist_new_ok(GtkButton *button, + gpointer userdata); +static void playlist_new_cancel(GtkButton *button, + gpointer userdata); +static void playlists_editor_received_tracks(void *v, + const char *err, + int nvec, char **vec); +static void playlist_window_destroyed(GtkWidget *widget, + GtkWidget **widget_pointer); +static gboolean playlist_window_keypress(GtkWidget *widget, + GdkEventKey *event, + gpointer user_data); +static int playlistcmp(const void *ap, const void *bp); +static void playlist_modify_locked(void *v, const char *err); +void playlist_modify_retrieved(void *v, const char *err, + int nvec, + char **vec); +static void playlist_modify_updated(void *v, const char *err); +static void playlist_modify_unlocked(void *v, const char *err); +static void playlist_drop(struct queuelike *ql, + int ntracks, + char **tracks, char **ids, + struct queue_entry *after_me); +struct playlist_modify_data; +static void playlist_drop_modify(struct playlist_modify_data *mod, + int nvec, char **vec); +static void playlist_remove_modify(struct playlist_modify_data *mod, + int nvec, char **vec); /** @brief Playlist editing window */ static GtkWidget *playlist_window; @@ -60,13 +113,19 @@ static const struct queue_column playlist_columns[] = { { "Length", column_length, 0, COL_RIGHT } }; -/** @brief Pop-up menu for playlist editor */ -// TODO some of these may not be generic enough yet - check! +/** @brief Pop-up menu for playlist editor + * + * Status: + * - track properties works but, bizarrely, raises the main window + * - play track works + * - play playlist works + * - select/deselect all work + */ static struct menuitem playlist_menuitems[] = { { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 }, { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 }, - //{ "Play playlist", ql_playall_activate, ql_playall_sensitive, 0, 0 }, - { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 }, + { "Play playlist", playlist_playall_activate, playlist_playall_sensitive, 0, 0 }, + { "Remove track from queue", playlist_remove_activate, playlist_remove_sensitive, 0, 0 }, { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 }, { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 }, }; @@ -78,7 +137,7 @@ static struct queuelike ql_playlist = { .ncolumns = sizeof playlist_columns / sizeof *playlist_columns, .menuitems = playlist_menuitems, .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems, - //.drop = playlist_drop //TODO + .drop = playlist_drop }; /* Maintaining the list of playlists ---------------------------------------- */ @@ -99,6 +158,23 @@ static void playlist_list_update(const char attribute((unused)) *event, disorder_eclient_playlists(client, playlist_list_received_playlists, 0); } +/** @brief Called with a new list of playlists */ +static void playlist_list_received_playlists(void attribute((unused)) *v, + const char *err, + int nvec, char **vec) { + if(err) { + playlists = 0; + nplaylists = -1; + /* Probably means server does not support playlists */ + } else { + playlists = vec; + nplaylists = nvec; + qsort(playlists, nplaylists, sizeof (char *), playlistcmp); + } + /* Tell our consumers */ + event_raise("playlists-updated", 0); +} + /** @brief qsort() callback for playlist name comparison */ static int playlistcmp(const void *ap, const void *bp) { const char *a = *(char **)ap, *b = *(char **)bp; @@ -125,25 +201,14 @@ static int playlistcmp(const void *ap, const void *bp) { return strcmp(a, b); } -/** @brief Called with a new list of playlists */ -static void playlist_list_received_playlists(void attribute((unused)) *v, - const char *err, - int nvec, char **vec) { - if(err) { - playlists = 0; - nplaylists = -1; - /* Probably means server does not support playlists */ - } else { - playlists = vec; - nplaylists = nvec; - qsort(playlists, nplaylists, sizeof (char *), playlistcmp); - } - /* Tell our consumers */ - event_raise("playlists-updated", 0); -} - /* Playlists menu ----------------------------------------------------------- */ +static void playlist_menu_playing(void attribute((unused)) *v, + const char *err) { + if(err) + popup_protocol_error(0, err); +} + /** @brief Play received playlist contents * * Passed as a completion callback by menu_activate_playlist(). @@ -156,7 +221,7 @@ static void playlist_menu_received_content(void attribute((unused)) *v, return; } for(int n = 0; n < nvec; ++n) - disorder_eclient_play(client, vec[n], NULL, NULL); + disorder_eclient_play(client, vec[n], playlist_menu_playing, NULL); } /** @brief Called to activate a playlist @@ -218,52 +283,117 @@ static GtkWidget *playlist_new_public; /** @brief "Private" radio button */ static GtkWidget *playlist_new_private; -/** @brief Get entered new-playlist details - * @param namep Where to store entered name (or NULL) - * @param fullnamep Where to store computed full name (or NULL) - * @param sharep Where to store 'shared' flag (or NULL) - * @param publicp Where to store 'public' flag (or NULL) - * @param privatep Where to store 'private' flag (or NULL) - */ -static void playlist_new_details(char **namep, - char **fullnamep, - gboolean *sharedp, - gboolean *publicp, - gboolean *privatep) { - gboolean shared, public, private; - g_object_get(playlist_new_shared, "active", &shared, (char *)NULL); - g_object_get(playlist_new_public, "active", &public, (char *)NULL); - g_object_get(playlist_new_private, "active", &private, (char *)NULL); - char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry), - 0, -1); /* name owned by calle */ - char *name = xstrdup(gname); - g_free(gname); - if(sharedp) *sharedp = shared; - if(publicp) *publicp = public; - if(privatep) *privatep = private; - if(namep) *namep = name; - if(fullnamep) { - if(*sharedp) *fullnamep = *namep; - else byte_xasprintf(fullnamep, "%s.%s", config->username, name); +/** @brief Buttons for new-playlist popup */ +static struct button playlist_new_buttons[] = { + { + .stock = GTK_STOCK_OK, + .clicked = playlist_new_ok, + .tip = "Create new playlist" + }, + { + .stock = GTK_STOCK_CANCEL, + .clicked = playlist_new_cancel, + .tip = "Do not create new playlist" } +}; +#define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons) + +/** @brief Pop up a new window to enter the playlist name and details */ +static void playlist_new_playlist(void) { + assert(playlist_new_window == NULL); + playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + g_signal_connect(playlist_new_window, "destroy", + G_CALLBACK(gtk_widget_destroyed), &playlist_new_window); + gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist"); + /* Window will be modal, suppressing access to other windows */ + gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE); + gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window), + GTK_WINDOW(playlist_window)); + + /* Window contents will use a table (grid) layout */ + GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/); + + /* First row: playlist name */ + gtk_table_attach_defaults(GTK_TABLE(table), + gtk_label_new("Playlist name"), + 0, 1, 0, 1); + playlist_new_entry = gtk_entry_new(); + g_signal_connect(playlist_new_entry, "changed", + G_CALLBACK(playlist_new_entry_edited), NULL); + gtk_table_attach_defaults(GTK_TABLE(table), + playlist_new_entry, + 1, 3, 0, 1); + + /* Second row: radio buttons to choose type */ + playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared"); + playlist_new_public + = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), + "public"); + playlist_new_private + = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), + "private"); + g_signal_connect(playlist_new_shared, "toggled", + G_CALLBACK(playlist_new_button_toggled), NULL); + g_signal_connect(playlist_new_public, "toggled", + G_CALLBACK(playlist_new_button_toggled), NULL); + g_signal_connect(playlist_new_private, "toggled", + G_CALLBACK(playlist_new_button_toggled), NULL); + gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2); + gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2); + gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2); + + /* Third row: info bar saying why not */ + playlist_new_info = gtk_label_new(""); + gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info, + 0, 3, 2, 3); + + /* Fourth row: ok/cancel buttons */ + GtkWidget *hbox = create_buttons_box(playlist_new_buttons, + NPLAYLIST_NEW_BUTTONS, + gtk_hbox_new(FALSE, 0)); + gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4); + + gtk_container_add(GTK_CONTAINER(playlist_new_window), + frame_widget(table, NULL)); + + /* Set initial state of OK button */ + playlist_new_changed(0,0,0); + + /* TODO: return should = OK, escape should = cancel */ + + /* Display the window */ + gtk_widget_show_all(playlist_new_window); } -/** @brief Called when the newly created playlist has unlocked */ -static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) { - if(err) - popup_protocol_error(0, err); - /* Pop down the creation window */ - gtk_widget_destroy(playlist_new_window); +/** @brief Called when 'ok' is clicked in new-playlist popup */ +static void playlist_new_ok(GtkButton attribute((unused)) *button, + gpointer attribute((unused)) userdata) { + gboolean shared, public, private; + char *name, *fullname; + playlist_new_details(&name, &fullname, &shared, &public, &private); + + /* We need to: + * - lock the playlist + * - check it doesn't exist + * - set sharing (which will create it empty + * - unlock it + * + * TODO we should freeze the window while this is going on to stop a second + * click. + */ + disorder_eclient_playlist_lock(client, playlist_new_locked, fullname, + fullname); } -/** @brief Called when the new playlist has been created */ -static void playlist_new_created(void attribute((unused)) *v, const char *err) { +/** @brief Called when the proposed new playlist has been locked */ +static void playlist_new_locked(void *v, const char *err) { + char *fullname = v; if(err) { popup_protocol_error(0, err); return; } - disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL); - // TODO arrange for the new playlist to be selected + disorder_eclient_playlist_get(client, playlist_new_retrieved, + fullname, fullname); } /** @brief Called when the proposed new playlist's contents have been retrieved @@ -291,35 +421,22 @@ static void playlist_new_retrieved(void *v, const char *err, fullname); } -/** @brief Called when the proposed new playlist has been locked */ -static void playlist_new_locked(void *v, const char *err) { - char *fullname = v; +/** @brief Called when the new playlist has been created */ +static void playlist_new_created(void attribute((unused)) *v, const char *err) { if(err) { popup_protocol_error(0, err); return; } - disorder_eclient_playlist_get(client, playlist_new_retrieved, - fullname, fullname); + disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL); + // TODO arrange for the new playlist to be selected } -/** @brief Called when 'ok' is clicked in new-playlist popup */ -static void playlist_new_ok(GtkButton attribute((unused)) *button, - gpointer attribute((unused)) userdata) { - gboolean shared, public, private; - char *name, *fullname; - playlist_new_details(&name, &fullname, &shared, &public, &private); - - /* We need to: - * - lock the playlist - * - check it doesn't exist - * - set sharing (which will create it empty - * - unlock it - * - * TODO we should freeze the window while this is going on to stop a second - * click. - */ - disorder_eclient_playlist_lock(client, playlist_new_locked, fullname, - fullname); +/** @brief Called when the newly created playlist has unlocked */ +static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) { + if(err) + popup_protocol_error(0, err); + /* Pop down the creation window */ + gtk_widget_destroy(playlist_new_window); } /** @brief Called when 'cancel' is clicked in new-playlist popup */ @@ -328,20 +445,35 @@ static void playlist_new_cancel(GtkButton attribute((unused)) *button, gtk_widget_destroy(playlist_new_window); } -/** @brief Buttons for new-playlist popup */ -static struct button playlist_new_buttons[] = { - { - .stock = GTK_STOCK_OK, - .clicked = playlist_new_ok, - .tip = "Create new playlist" - }, - { - .stock = GTK_STOCK_CANCEL, - .clicked = playlist_new_cancel, - .tip = "Do not create new playlist" - } -}; -#define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons) +/** @brief Called when some radio button in the new-playlist popup changes */ +static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) *tb, + gpointer attribute((unused)) userdata) { + playlist_new_changed(0,0,0); +} + +/** @brief Called when the text entry field in the new-playlist popup changes */ +static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable, + gpointer attribute((unused)) user_data) { + playlist_new_changed(0,0,0); +} + +/** @brief Called to update new playlist window state + * + * This is called whenever one the text entry or radio buttons changed, and + * also when the set of known playlists changes. It determines whether the new + * playlist would be creatable and sets the sensitivity of the OK button + * and info display accordingly. + */ +static void playlist_new_changed(const char attribute((unused)) *event, + void attribute((unused)) *eventdata, + void attribute((unused)) *callbackdata) { + if(!playlist_new_window) + return; + const char *reason = playlist_new_valid(); + gtk_widget_set_sensitive(playlist_new_buttons[0].widget, + !reason); + gtk_label_set_text(GTK_LABEL(playlist_new_info), reason); +} /** @brief Test whether the new-playlist window settings are valid * @return NULL on success or an error string if not @@ -374,101 +506,34 @@ static const char *playlist_new_valid(void) { return NULL; } -/** @brief Called to update new playlist window state - * - * This is called whenever one the text entry or radio buttons changed, and - * also when the set of known playlists changes. It determines whether the new - * playlist would be creatable and sets the sensitivity of the OK button - * and info display accordingly. +/** @brief Get entered new-playlist details + * @param namep Where to store entered name (or NULL) + * @param fullnamep Where to store computed full name (or NULL) + * @param sharep Where to store 'shared' flag (or NULL) + * @param publicp Where to store 'public' flag (or NULL) + * @param privatep Where to store 'private' flag (or NULL) */ -static void playlist_new_changed(const char attribute((unused)) *event, - void attribute((unused)) *eventdata, - void attribute((unused)) *callbackdata) { - if(!playlist_new_window) - return; - const char *reason = playlist_new_valid(); - gtk_widget_set_sensitive(playlist_new_buttons[0].widget, - !reason); - gtk_label_set_text(GTK_LABEL(playlist_new_info), reason); -} - -/** @brief Called when some radio button in the new-playlist popup changes */ -static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) tb, - gpointer attribute((unused)) userdata) { - playlist_new_changed(0,0,0); -} - -/** @brief Called when the text entry field in the new-playlist popup changes */ -static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable, - gpointer attribute((unused)) user_data) { - playlist_new_changed(0,0,0); -} - -/** @brief Pop up a new window to enter the playlist name and details */ -static void playlist_new_playlist(void) { - assert(playlist_new_window == NULL); - playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); - g_signal_connect(playlist_new_window, "destroy", - G_CALLBACK(gtk_widget_destroyed), &playlist_new_window); - gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist"); - /* Window will be modal, suppressing access to other windows */ - gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE); - gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window), - GTK_WINDOW(playlist_window)); - - /* Window contents will use a table (grid) layout */ - GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/); - - /* First row: playlist name */ - gtk_table_attach_defaults(GTK_TABLE(table), - gtk_label_new("Playlist name"), - 0, 1, 0, 1); - playlist_new_entry = gtk_entry_new(); - g_signal_connect(playlist_new_entry, "changed", - G_CALLBACK(playlist_new_entry_edited), NULL); - gtk_table_attach_defaults(GTK_TABLE(table), - playlist_new_entry, - 1, 3, 0, 1); - - /* Second row: radio buttons to choose type */ - playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared"); - playlist_new_public - = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), - "public"); - playlist_new_private - = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), - "private"); - g_signal_connect(playlist_new_shared, "toggled", - G_CALLBACK(playlist_new_button_toggled), NULL); - g_signal_connect(playlist_new_public, "toggled", - G_CALLBACK(playlist_new_button_toggled), NULL); - g_signal_connect(playlist_new_private, "toggled", - G_CALLBACK(playlist_new_button_toggled), NULL); - gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2); - gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2); - gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2); - - /* Third row: info bar saying why not */ - playlist_new_info = gtk_label_new(""); - gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info, - 0, 3, 2, 3); - - /* Fourth row: ok/cancel buttons */ - GtkWidget *hbox = create_buttons_box(playlist_new_buttons, - NPLAYLIST_NEW_BUTTONS, - gtk_hbox_new(FALSE, 0)); - gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4); - - gtk_container_add(GTK_CONTAINER(playlist_new_window), - frame_widget(table, NULL)); - - /* Set initial state of OK button */ - playlist_new_changed(0,0,0); - - /* TODO: return should = OK, escape should = cancel */ - - /* Display the window */ - gtk_widget_show_all(playlist_new_window); +static void playlist_new_details(char **namep, + char **fullnamep, + gboolean *sharedp, + gboolean *publicp, + gboolean *privatep) { + gboolean shared, public, private; + g_object_get(playlist_new_shared, "active", &shared, (char *)NULL); + g_object_get(playlist_new_public, "active", &public, (char *)NULL); + g_object_get(playlist_new_private, "active", &private, (char *)NULL); + char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry), + 0, -1); /* name owned by calle */ + char *name = xstrdup(gname); + g_free(gname); + if(sharedp) *sharedp = shared; + if(publicp) *publicp = public; + if(privatep) *privatep = private; + if(namep) *namep = name; + if(fullnamep) { + if(*sharedp) *fullnamep = *namep; + else byte_xasprintf(fullnamep, "%s.%s", config->username, name); + } } /* Playlist picker ---------------------------------------------------------- */ @@ -506,6 +571,8 @@ static void playlist_picker_fill(const char attribute((unused)) *event, if(was_selected && !strcmp(was_selected, playlists[n])) gtk_tree_selection_select_iter(playlist_picker_selection, iter); } + /* TODO deselecting then reselecting the current playlist resets the playlist + * editor, which trashes the user's selection. */ } /** @brief Called when the selection might have changed */ @@ -637,14 +704,44 @@ static GtkWidget *playlist_picker_create(void) { /* Playlist editor ---------------------------------------------------------- */ +static GtkWidget *playlists_editor_create(void) { + assert(ql_playlist.view == NULL); /* better not be set up already */ + GtkWidget *w = init_queuelike(&ql_playlist); + /* Initially empty */ + return w; +} + +/** @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 attribute((unused)) *v, +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; @@ -657,10 +754,8 @@ static void playlists_editor_received_tracks(void attribute((unused)) *v, /* 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. */ - /* TODO but this doesn't work for some reason */ int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0; byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]); - fprintf(stderr, "%s\n", q->id); hash_add(h, vec[0], &serial, HASH_INSERT_OR_REPLACE); *qq = q; qq = &q->next; @@ -669,58 +764,244 @@ static void playlists_editor_received_tracks(void attribute((unused)) *v, ql_new_queue(&ql_playlist, newq); } -/** @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) +/* Playlist mutation -------------------------------------------------------- */ + +/** @brief State structure for guarded playlist modification + * + * 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; - if(!playlist_picker_selected) + } + 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_protocol_error(0, err); + disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); return; - if(!strcmp(playlist_picker_selected, modified_playlist)) - disorder_eclient_playlist_get(client, playlists_editor_received_tracks, - playlist_picker_selected, NULL); + } + 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); } -static GtkWidget *playlists_editor_create(void) { - assert(ql_playlist.view == NULL); /* better not be set up already */ - GtkWidget *w = init_queuelike(&ql_playlist); - /* Initially empty */ - return w; +/** @brief Called when the playlist has been updated */ +static void playlist_modify_updated(void attribute((unused)) *v, + const char *err) { + if(err) + popup_protocol_error(0, err); + disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); } -/* Playlists window --------------------------------------------------------- */ +/** @brief Called when the playlist has been unlocked */ +static void playlist_modify_unlocked(void attribute((unused)) *v, + const char *err) { + if(err) + popup_protocol_error(0, err); +} -/** @brief Keypress handler */ -static gboolean playlist_window_keypress(GtkWidget attribute((unused)) *widget, - GdkEventKey *event, - gpointer attribute((unused)) user_data) { - if(event->state) +/* 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; + /* To safely move or insert rows we must: + * - take a lock + * - fetch the playlist + * - verify it's not changed + * - update the playlist contents + * - store the playlist + * - release the lock + * + */ + disorder_eclient_playlist_lock(client, playlist_modify_locked, + mod->playlist, mod); +} + +static void playlist_drop_modify(struct playlist_modify_data *mod, + int nvec, char **vec) { + char **newvec; + int nnewvec; + if(mod->ids) { + /* This is a rearrangement */ + /* TODO what if it's a drag from the queue? */ + abort(); + } else { + /* This is an insertion */ + 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; + } + } + 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; - switch(event->keyval) { - case GDK_Escape: - gtk_widget_destroy(playlist_window); - return TRUE; - default: + /* If it's empty we can't play it */ + if(!ql_playlist.q) return FALSE; - } + /* Otherwise we can */ + return TRUE; } -/** @brief Called when the playlist window is destroyed */ -static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget, - GtkWidget **widget_pointer) { - destroy_queuelike(&ql_playlist); - *widget_pointer = NULL; +/** @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 play the selected playlist */ +static void playlist_remove_activate(GtkMenuItem attribute((unused)) *menuitem, + gpointer attribute((unused)) user_data) { + if(!playlist_picker_selected) + return; + /* To safely remove rows we must: + * - take a lock + * - fetch the playlist + * - verify it's not changed + * - delete the selected rows from the retrieved version + * - store the playlist + * - release the lock + * + * In addition we careful check that the selected playlist hasn't changed + * underfoot, and avoid leaving the playlist locked if we bail out at any + * point. + */ + 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 edit_playlists(gpointer attribute((unused)) callback_data, - guint attribute((unused)) callback_action, - GtkWidget attribute((unused)) *menu_item) { +void playlist_window_create(gpointer attribute((unused)) callback_data, + guint attribute((unused)) callback_action, + GtkWidget attribute((unused)) *menu_item) { /* If the window already exists, raise it */ if(playlist_window) { gtk_window_present(GTK_WINDOW(playlist_window)); @@ -752,11 +1033,32 @@ void edit_playlists(gpointer attribute((unused)) callback_data, gtk_widget_show_all(playlist_window); } +/** @brief Keypress handler */ +static gboolean playlist_window_keypress(GtkWidget attribute((unused)) *widget, + GdkEventKey *event, + gpointer attribute((unused)) user_data) { + if(event->state) + return FALSE; + switch(event->keyval) { + case GDK_Escape: + gtk_widget_destroy(playlist_window); + return TRUE; + default: + return FALSE; + } +} + +/** @brief Called when the playlist window is destroyed */ +static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget, + GtkWidget **widget_pointer) { + destroy_queuelike(&ql_playlist); + *widget_pointer = NULL; +} + /** @brief Initialize playlist support */ void playlists_init(void) { /* We re-get all playlists upon any change... */ event_register("playlist-created", playlist_list_update, 0); - event_register("playlist-modified", playlist_list_update, 0); /* TODO why? */ event_register("playlist-deleted", playlist_list_update, 0); /* ...and on reconnection */ event_register("log-connected", playlist_list_update, 0);