From: Richard Kettlewell Date: Thu, 26 Nov 2009 10:59:02 +0000 (+0000) Subject: Merge Disobedience playlist support. X-Git-Tag: 5.0.3~4^2~45 X-Git-Url: https://git.distorted.org.uk/~mdw/disorder/commitdiff_plain/d64e1d92acaf5a00908f630848f1f308574847ea?hp=012aaa91b77cc74ee7bc4f56d1aaefc99eae5d81 Merge Disobedience playlist support. --- diff --git a/CHANGES.html b/CHANGES.html index 90101b0..dbe660e 100644 --- a/CHANGES.html +++ b/CHANGES.html @@ -85,6 +85,9 @@ span.command { SoX. SoX support will be removed in a future version.

+

Playlists are now supported. These allow a collection of tracks to be + prepared offline and played as a unit.

+

Disobedience

@@ -96,6 +99,8 @@ span.command { “Recent”, “Added” and “Choose” tabs to the queue.

+

Disobedience now supports playlist editing.

+

Web Interface

diff --git a/README b/README index 57435a8..4c567ba 100644 --- a/README +++ b/README @@ -89,9 +89,6 @@ platform, please get in touch. --without-gtk Don't build GTK+ client (Disobedience) --without-python Don't build Python support - On a Mac you can use --with-bits=64 to request a 64-bit build. The default - is 32 bits. You will need suitable versions of all the libraries used. - If configure cannot guess where your web server keeps its HTML documents and CGI programs, you may have to tell it, for instance: diff --git a/README.developers b/README.developers index 9e84520..3f2cf2a 100644 --- a/README.developers +++ b/README.developers @@ -18,22 +18,13 @@ Dependencies: refuse to use it). * On FreeBSD you'll need at least these packages: - autotools - bash - flac - mad - boehm-gc - db43 - gmake - gsed - libao - libgcrypt - wget - vorbis-tools + autotools bash flac mad boehm-gc db43 gmake gsed libao libgcrypt wget + vorbis-tools * On OS X with Fink: - fink install gtk+2-dev gc libgrypt pcre flac vorbis-tools libmad wget sed + fink install gtk+2-dev gc libgrypt pcre flac vorbis-tools libmad wget \ + sed libsamplerate0-dev * Please report unstated dependencies (here, README or debian/control). diff --git a/disobedience/added.c b/disobedience/added.c index 7f654ee..aa15f48 100644 --- a/disobedience/added.c +++ b/disobedience/added.c @@ -93,6 +93,8 @@ struct queuelike ql_added = { .ncolumns = sizeof added_columns / sizeof *added_columns, .menuitems = added_menuitems, .nmenuitems = sizeof added_menuitems / sizeof *added_menuitems, + .drag_source_targets = choose_targets, + .drag_source_actions = GDK_ACTION_COPY, }; GtkWidget *added_widget(void) { diff --git a/disobedience/autoscroll.c b/disobedience/autoscroll.c index bfe71c1..2b24699 100644 --- a/disobedience/autoscroll.c +++ b/disobedience/autoscroll.c @@ -68,12 +68,11 @@ static gboolean autoscroll_timeout(gpointer data) { /* see if we are near the edge. */ offset = ty - (visible_rect.y + 2 * SCROLL_EDGE_SIZE); - if (offset > 0) - { - offset = ty - (visible_rect.y + visible_rect.height - 2 * SCROLL_EDGE_SIZE); - if (offset < 0) - return TRUE; - } + if (offset > 0) { + offset = ty - (visible_rect.y + visible_rect.height - 2 * SCROLL_EDGE_SIZE); + if (offset < 0) + return TRUE; + } GtkAdjustment *vadjustment = gtk_tree_view_get_vadjustment(tree_view); diff --git a/disobedience/choose-menu.c b/disobedience/choose-menu.c index f1aa3b0..44ad04f 100644 --- a/disobedience/choose-menu.c +++ b/disobedience/choose-menu.c @@ -121,7 +121,7 @@ static void choose_properties_activate(GtkMenuItem attribute((unused)) *item, gtk_tree_selection_selected_foreach(choose_selection, choose_gather_selected_files_callback, v); - properties(v->nvec, (const char **)v->vec); + properties(v->nvec, (const char **)v->vec, toplevel); } /** @brief Set sensitivity for select children diff --git a/disobedience/choose.c b/disobedience/choose.c index 9829f19..a1d50c1 100644 --- a/disobedience/choose.c +++ b/disobedience/choose.c @@ -33,15 +33,19 @@ #include "disobedience.h" #include "choose.h" #include "multidrag.h" +#include "queue-generic.h" #include /** @brief Drag types */ -static const GtkTargetEntry choose_targets[] = { +const GtkTargetEntry choose_targets[] = { { - (char *)"text/x-disorder-playable-tracks", /* drag type */ + PLAYABLE_TRACKS, /* drag type */ GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ - 1 /* ID value */ + PLAYABLE_TRACKS_ID /* ID value */ }, + { + .target = NULL + } }; /** @brief The current selection tree */ @@ -711,7 +715,7 @@ GtkWidget *choose_widget(void) { gtk_drag_source_set(choose_view, GDK_BUTTON1_MASK, choose_targets, - sizeof choose_targets / sizeof *choose_targets, + 1, GDK_ACTION_COPY); g_signal_connect(choose_view, "drag-data-get", G_CALLBACK(choose_drag_data_get), NULL); diff --git a/disobedience/disobedience.c b/disobedience/disobedience.c index 9b050f4..96bf5c6 100644 --- a/disobedience/disobedience.c +++ b/disobedience/disobedience.c @@ -490,9 +490,7 @@ int main(int argc, char **argv) { disorder_eclient_version(client, version_completed, 0); event_register("log-connected", check_rtp_address, 0); suppress_actions = 0; -#if PLAYLISTS playlists_init(); -#endif /* If no password is set yet pop up a login box */ if(!config->password) login_box(); diff --git a/disobedience/disobedience.h b/disobedience/disobedience.h index cafa48d..11da2c3 100644 --- a/disobedience/disobedience.h +++ b/disobedience/disobedience.h @@ -117,7 +117,8 @@ void popup_protocol_error(int code, const char *msg); /* Report an error */ -void properties(int ntracks, const char **tracks); +void properties(int ntracks, const char **tracks, + GtkWidget *parent); /* Pop up a properties window for a list of tracks */ GtkWidget *scroll_widget(GtkWidget *child); @@ -134,7 +135,8 @@ void popup_submsg(GtkWidget *parent, GtkMessageType mt, const char *msg); void fpopup_msg(GtkMessageType mt, const char *fmt, ...); -struct progress_window *progress_window_new(const char *title); +struct progress_window *progress_window_new(const char *title, + GtkWidget *parent); /* Pop up a progress window */ void progress_window_progress(struct progress_window *pw, @@ -212,6 +214,8 @@ void choose_update(void); void play_completed(void *v, const char *err); +extern const GtkTargetEntry choose_targets[]; + /* Login details */ void login_box(void); @@ -253,17 +257,15 @@ void popup_settings(void); /* Playlists */ -#if PLAYLISTS void playlists_init(void); -void edit_playlists(gpointer callback_data, - guint callback_action, - GtkWidget *menu_item); +void playlist_window_create(gpointer callback_data, + guint callback_action, + GtkWidget *menu_item); extern char **playlists; extern int nplaylists; -extern GtkWidget *playlists_widget; +extern GtkWidget *menu_playlists_widget; extern GtkWidget *playlists_menu; -extern GtkWidget *editplaylists_widget; -#endif +extern GtkWidget *menu_editplaylists_widget; #endif /* DISOBEDIENCE_H */ diff --git a/disobedience/menu.c b/disobedience/menu.c index 0243139..3566745 100644 --- a/disobedience/menu.c +++ b/disobedience/menu.c @@ -24,11 +24,9 @@ static GtkWidget *selectall_widget; static GtkWidget *selectnone_widget; static GtkWidget *properties_widget; -#if PLAYLISTS -GtkWidget *playlists_widget; +GtkWidget *menu_playlists_widget; GtkWidget *playlists_menu; -GtkWidget *editplaylists_widget; -#endif +GtkWidget *menu_editplaylists_widget; /** @brief Main menu widgets */ GtkItemFactory *mainmenufactory; @@ -298,16 +296,14 @@ GtkWidget *menubar(GtkWidget *w) { 0, /* item_type */ 0 /* extra_data */ }, -#if PLAYLISTS { (char *)"/Edit/Edit playlists", /* path */ 0, /* accelerator */ - edit_playlists, /* callback */ + playlist_window_create, /* callback */ 0, /* callback_action */ 0, /* item_type */ 0 /* extra_data */ }, -#endif { @@ -350,7 +346,6 @@ GtkWidget *menubar(GtkWidget *w) { (char *)"", /* item_type */ 0 /* extra_data */ }, -#if PLAYLISTS { (char *)"/Control/Activate playlist", /* path */ 0, /* accelerator */ @@ -359,8 +354,7 @@ GtkWidget *menubar(GtkWidget *w) { (char *)"", /* item_type */ 0 /* extra_data */ }, -#endif - + { (char *)"/Help", /* path */ 0, /* accelerator */ @@ -404,22 +398,18 @@ GtkWidget *menubar(GtkWidget *w) { "/Edit/Deselect all tracks"); properties_widget = gtk_item_factory_get_widget(mainmenufactory, "/Edit/Track properties"); -#if PLAYLISTS - playlists_widget = gtk_item_factory_get_item(mainmenufactory, + menu_playlists_widget = gtk_item_factory_get_item(mainmenufactory, "/Control/Activate playlist"); playlists_menu = gtk_item_factory_get_widget(mainmenufactory, "/Control/Activate playlist"); - editplaylists_widget = gtk_item_factory_get_widget(mainmenufactory, + menu_editplaylists_widget = gtk_item_factory_get_widget(mainmenufactory, "/Edit/Edit playlists"); -#endif assert(selectall_widget != 0); assert(selectnone_widget != 0); assert(properties_widget != 0); -#if PLAYLISTS - assert(playlists_widget != 0); + assert(menu_playlists_widget != 0); assert(playlists_menu != 0); - assert(editplaylists_widget != 0); -#endif + assert(menu_editplaylists_widget != 0); GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory, "/Edit"); diff --git a/disobedience/playlists.c b/disobedience/playlists.c index cd8979d..c10ef6f 100644 --- a/disobedience/playlists.c +++ b/disobedience/playlists.c @@ -1,6 +1,6 @@ /* * This file is part of DisOrder - * Copyright (C) 2008 Richard Kettlewell + * Copyright (C) 2008, 2009 Richard Kettlewell * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,37 +18,161 @@ * USA */ /** @file disobedience/playlists.c - * @brief Playlist for Disobedience + * @brief Playlist support for Disobedience * * The playlists management window contains: - * - a list of all playlists + * - the playlist picker (a list of all playlists) TODO should be a tree! * - an add button * - a delete button - * - a drag+drop capable view of the playlist - * - a close button + * - the playlist editor (a d+d-capable view of the currently picked playlist) + * - a close button TODO + * + * This file also maintains the playlist menu, allowing playlists to be + * activated from the main window's menu. + * + * Internally we maintain the playlist list, which is just the current list of + * playlists. Changes to this are reflected in the playlist menu and the + * playlist picker. + * */ #include "disobedience.h" +#include "queue-generic.h" +#include "popup.h" +#include "validity.h" -#if PLAYLISTS - -static void playlists_updated(void *v, - const char *err, - int nvec, char **vec); +static void playlist_list_received_playlists(void *v, + const char *err, + int nvec, char **vec); +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); +static gboolean playlist_new_keypress(GtkWidget *widget, + GdkEventKey *event, + gpointer user_data); +static gboolean playlist_picker_keypress(GtkWidget *widget, + GdkEventKey *event, + gpointer user_data); +static void playlist_editor_button_toggled(GtkToggleButton *tb, + gpointer userdata); +static void playlist_editor_set_buttons(const char *event, + void *eventdata, + void *callbackdata); +static void playlist_editor_got_share(void *v, + const char *err, + const char *value); +static void playlist_editor_share_set(void *v, const char *err); /** @brief Playlist editing window */ -static GtkWidget *playlists_window; +static GtkWidget *playlist_window; -/** @brief Tree model for list of playlists */ -static GtkListStore *playlists_list; +/** @brief Columns for the playlist editor */ +static const struct queue_column playlist_columns[] = { + { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE }, + { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE }, + { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE }, +}; -/** @brief Selection for list of playlists */ -static GtkTreeSelection *playlists_selection; +/** @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", 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 }, +}; -/** @brief Currently selected playlist */ -static const char *playlists_selected; +static const GtkTargetEntry playlist_targets[] = { + { + PLAYLIST_TRACKS, /* drag type */ + GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */ + PLAYLIST_TRACKS_ID /* ID value */ + }, + { + PLAYABLE_TRACKS, /* drag type */ + GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ + PLAYABLE_TRACKS_ID, /* ID value */ + }, + { + .target = NULL + } +}; -/** @brief Delete button */ -static GtkWidget *playlists_delete_button; +/** @brief Queuelike for editing a playlist */ +static struct queuelike ql_playlist = { + .name = "playlist", + .columns = playlist_columns, + .ncolumns = sizeof playlist_columns / sizeof *playlist_columns, + .menuitems = playlist_menuitems, + .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems, + .drop = playlist_drop, + .drag_source_targets = playlist_targets, + .drag_source_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, + .drag_dest_targets = playlist_targets, + .drag_dest_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, +}; + +/* Maintaining the list of playlists ---------------------------------------- */ /** @brief Current list of playlists or NULL */ char **playlists; @@ -56,11 +180,31 @@ char **playlists; /** @brief Count of playlists */ int nplaylists; -/** @brief Schedule an update to the list of playlists */ -static void playlists_update(const char attribute((unused)) *event, - void attribute((unused)) *eventdata, - void attribute((unused)) *callbackdata) { - disorder_eclient_playlists(client, playlists_updated, 0); +/** @brief Schedule an update to the list of playlists + * + * Called periodically and when a playlist is created or deleted. + */ +static void playlist_list_update(const char attribute((unused)) *event, + void attribute((unused)) *eventdata, + void attribute((unused)) *callbackdata) { + 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 */ @@ -89,244 +233,1082 @@ static int playlistcmp(const void *ap, const void *bp) { return strcmp(a, b); } -/** @brief Called with a new list of playlists */ -static void playlists_updated(void attribute((unused)) *v, - const char *err, - int nvec, char **vec) { +/* 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(). + */ +static void playlist_menu_received_content(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); + popup_protocol_error(0, err); + return; } - /* Tell our consumers */ - event_raise("playlists-updated", 0); + for(int n = 0; n < nvec; ++n) + disorder_eclient_play(client, vec[n], playlist_menu_playing, NULL); } -/** @brief Called to activate a playlist */ -static void menu_activate_playlist(GtkMenuItem *menuitem, +/** @brief Called to activate a playlist + * + * Called when the menu item for a playlist is clicked. + */ +static void playlist_menu_activate(GtkMenuItem *menuitem, gpointer attribute((unused)) user_data) { GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child); const char *playlist = gtk_label_get_text(label); - fprintf(stderr, "activate playlist %s\n", playlist); /* TODO */ + disorder_eclient_playlist_get(client, playlist_menu_received_content, + playlist, NULL); } -/** @brief Called when the playlists change */ -static void menu_playlists_changed(const char attribute((unused)) *event, - void attribute((unused)) *eventdata, - void attribute((unused)) *callbackdata) { +/** @brief Called when the playlists change + * + * Naively refills the menu. The results might be unsettling if the menu is + * currently open, but this is hopefuly fairly rare. + */ +static void playlist_menu_changed(const char attribute((unused)) *event, + void attribute((unused)) *eventdata, + void attribute((unused)) *callbackdata) { if(!playlists_menu) return; /* OMG too soon */ GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu); - /* TODO: we could be more sophisticated and only insert/remove widgets as - * needed. For now that's too much effort. */ while(menu->children) gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data)); /* NB nplaylists can be -1 as well as 0 */ for(int n = 0; n < nplaylists; ++n) { GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]); - g_signal_connect(w, "activate", G_CALLBACK(menu_activate_playlist), 0); + g_signal_connect(w, "activate", G_CALLBACK(playlist_menu_activate), 0); gtk_widget_show(w); gtk_menu_shell_append(menu, w); } - gtk_widget_set_sensitive(playlists_widget, + gtk_widget_set_sensitive(menu_playlists_widget, nplaylists > 0); - gtk_widget_set_sensitive(editplaylists_widget, + gtk_widget_set_sensitive(menu_editplaylists_widget, nplaylists >= 0); } -/** @brief (Re-)populate the playlist tree model */ -static void playlists_fill(void) { - GtkTreeIter iter[1]; +/* Popup to create a new playlist ------------------------------------------- */ + +/** @brief New-playlist popup */ +static GtkWidget *playlist_new_window; + +/** @brief Text entry in new-playlist popup */ +static GtkWidget *playlist_new_entry; + +/** @brief Label for displaying feedback on what's wrong */ +static GtkWidget *playlist_new_info; + +/** @brief "Shared" radio button */ +static GtkWidget *playlist_new_shared; + +/** @brief "Public" radio button */ +static GtkWidget *playlist_new_public; + +/** @brief "Private" radio button */ +static GtkWidget *playlist_new_private; + +/** @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); - if(!playlists_list) - playlists_list = gtk_list_store_new(1, G_TYPE_STRING); - gtk_list_store_clear(playlists_list); + /* 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); + + g_signal_connect(playlist_new_window, "key-press-event", + G_CALLBACK(playlist_new_keypress), 0); + + /* Display the window */ + gtk_widget_show_all(playlist_new_window); +} + +/** @brief Keypress handler */ +static gboolean playlist_new_keypress(GtkWidget attribute((unused)) *widget, + GdkEventKey *event, + gpointer attribute((unused)) user_data) { + if(event->state) + return FALSE; + switch(event->keyval) { + case GDK_Return: + playlist_new_ok(NULL, NULL); + return TRUE; + case GDK_Escape: + gtk_widget_destroy(playlist_new_window); + return TRUE; + default: + return FALSE; + } +} + +/** @brief Called when 'ok' is clicked in new-playlist popup */ +static void playlist_new_ok(GtkButton attribute((unused)) *button, + gpointer attribute((unused)) userdata) { + if(playlist_new_valid()) + return; + 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 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_get(client, playlist_new_retrieved, + fullname, fullname); +} + +/** @brief Called when the proposed new playlist's contents have been retrieved + * + * ...or rather, normally, when it's been reported that it does not exist. + */ +static void playlist_new_retrieved(void *v, const char *err, + int nvec, + char attribute((unused)) **vec) { + char *fullname = v; + if(!err && nvec != -1) + /* A rare case but not in principle impossible */ + err = "A playlist with that name already exists."; + if(err) { + popup_protocol_error(0, err); + disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname); + return; + } + gboolean shared, public, private; + playlist_new_details(0, 0, &shared, &public, &private); + disorder_eclient_playlist_set_share(client, playlist_new_created, fullname, + public ? "public" + : private ? "private" + : "shared", + fullname); +} + +/** @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_unlock(client, playlist_new_unlocked, NULL); + // TODO arrange for the new playlist to be selected +} + +/** @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 */ +static void playlist_new_cancel(GtkButton attribute((unused)) *button, + gpointer attribute((unused)) userdata) { + gtk_widget_destroy(playlist_new_window); +} + +/** @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 + */ +static const char *playlist_new_valid(void) { + gboolean shared, public, private; + char *name, *fullname; + playlist_new_details(&name, &fullname, &shared, &public, &private); + if(!(shared || public || private)) + return "No type set."; + if(!*name) + return ""; + /* See if the result is valid */ + if(!valid_username(name) + || playlist_parse_name(fullname, NULL, NULL)) + return "Not a valid playlist name."; + /* See if the result clashes with an existing name. This is not a perfect + * check, the playlist might be created after this point but before we get a + * chance to disable the "OK" button. However when we try to create the + * playlist we will first try to retrieve it, with a lock held, so we + * shouldn't end up overwriting anything. */ for(int n = 0; n < nplaylists; ++n) - gtk_list_store_insert_with_values(playlists_list, iter, n/*position*/, - 0, playlists[n], /* column 0 */ - -1); /* no more cols */ - // TODO reselect whatever was formerly selected if possible, if not then - // zap the contents view + if(!strcmp(playlists[n], fullname)) { + if(shared) + return "A shared playlist with that name already exists."; + else + return "You already have a playlist with that name."; + } + /* As far as we can tell creation would work */ + return NULL; +} + +/** @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); + } +} + +/* Playlist picker ---------------------------------------------------------- */ + +/** @brief Delete button */ +static GtkWidget *playlist_picker_delete_button; + +/** @brief Tree model for list of playlists */ +static GtkListStore *playlist_picker_list; + +/** @brief Selection for list of playlists */ +static GtkTreeSelection *playlist_picker_selection; + +/** @brief Currently selected playlist */ +static const char *playlist_picker_selected; + +/** @brief (Re-)populate the playlist picker tree model */ +static void playlist_picker_fill(const char attribute((unused)) *event, + void attribute((unused)) *eventdata, + void attribute((unused)) *callbackdata) { + GtkTreeIter iter[1]; + + if(!playlist_window) + return; + if(!playlist_picker_list) + playlist_picker_list = gtk_list_store_new(1, G_TYPE_STRING); + const char *was_selected = playlist_picker_selected; + gtk_list_store_clear(playlist_picker_list); /* clears playlists_selected */ + for(int n = 0; n < nplaylists; ++n) { + gtk_list_store_insert_with_values(playlist_picker_list, iter, + n /*position*/, + 0, playlists[n], /* column 0 */ + -1); /* no more cols */ + /* Reselect the selected playlist */ + 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 */ -static void playlists_selection_changed(GtkTreeSelection attribute((unused)) *treeselection, - gpointer attribute((unused)) user_data) { +static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused)) *treeselection, + gpointer attribute((unused)) user_data) { GtkTreeIter iter; char *gselected, *selected; /* Identify the current selection */ - if(gtk_tree_selection_get_selected(playlists_selection, 0, &iter)) { - gtk_tree_model_get(GTK_TREE_MODEL(playlists_list), &iter, + if(gtk_tree_selection_get_selected(playlist_picker_selection, 0, &iter)) { + gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), &iter, 0, &gselected, -1); selected = xstrdup(gselected); g_free(gselected); } else selected = 0; + /* Set button sensitivity according to the new state */ + if(selected) + gtk_widget_set_sensitive(playlist_picker_delete_button, 1); + else + gtk_widget_set_sensitive(playlist_picker_delete_button, 0); + /* TODO delete should not be sensitive for public playlists owned by other + * users */ /* Eliminate no-change cases */ - if(!selected && !playlists_selected) + if(!selected && !playlist_picker_selected) return; - if(selected && playlists_selected && !strcmp(selected, playlists_selected)) + if(selected + && playlist_picker_selected + && !strcmp(selected, playlist_picker_selected)) return; - /* There's been a change */ - playlists_selected = selected; - if(playlists_selected) { - fprintf(stderr, "playlists selection changed\n'"); /* TODO */ - gtk_widget_set_sensitive(playlists_delete_button, 1); - } else - gtk_widget_set_sensitive(playlists_delete_button, 0); + /* Record the new state */ + playlist_picker_selected = selected; + /* Re-initalize the queue */ + ql_new_queue(&ql_playlist, NULL); + /* Synthesize a playlist-modified to re-initialize the editor etc */ + event_raise("playlist-modified", (void *)playlist_picker_selected); } /** @brief Called when the 'add' button is pressed */ -static void playlists_add(GtkButton attribute((unused)) *button, - gpointer attribute((unused)) userdata) { - /* Unselect whatever is selected */ - gtk_tree_selection_unselect_all(playlists_selection); - fprintf(stderr, "playlists_add\n");/* TODO */ +static void playlist_picker_add(GtkButton attribute((unused)) *button, + gpointer attribute((unused)) userdata) { + /* Unselect whatever is selected TODO why?? */ + gtk_tree_selection_unselect_all(playlist_picker_selection); + playlist_new_playlist(); +} + +/** @brief Called when playlist deletion completes */ +static void playlists_picker_delete_completed(void attribute((unused)) *v, + const char *err) { + if(err) + popup_protocol_error(0, err); } /** @brief Called when the 'Delete' button is pressed */ -static void playlists_delete(GtkButton attribute((unused)) *button, - gpointer attribute((unused)) userdata) { +static void playlist_picker_delete(GtkButton attribute((unused)) *button, + gpointer attribute((unused)) userdata) { GtkWidget *yesno; int res; - if(!playlists_selected) - return; /* shouldn't happen */ - yesno = gtk_message_dialog_new(GTK_WINDOW(playlists_window), + if(!playlist_picker_selected) + return; + yesno = gtk_message_dialog_new(GTK_WINDOW(playlist_window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, - "Do you really want to delete user %s?" + "Do you really want to delete playlist %s?" " This action cannot be undone.", - playlists_selected); + playlist_picker_selected); res = gtk_dialog_run(GTK_DIALOG(yesno)); gtk_widget_destroy(yesno); if(res == GTK_RESPONSE_YES) { disorder_eclient_playlist_delete(client, - NULL/*playlists_delete_completed*/, - playlists_selected, + playlists_picker_delete_completed, + playlist_picker_selected, NULL); } } /** @brief Table of buttons below the playlist list */ -static struct button playlists_buttons[] = { +static struct button playlist_picker_buttons[] = { { GTK_STOCK_ADD, - playlists_add, + playlist_picker_add, "Create a new playlist", 0 }, { GTK_STOCK_REMOVE, - playlists_delete, + playlist_picker_delete, "Delete a playlist", 0 }, }; -#define NPLAYLISTS_BUTTONS (sizeof playlists_buttons / sizeof *playlists_buttons) +#define NPLAYLIST_PICKER_BUTTONS (sizeof playlist_picker_buttons / sizeof *playlist_picker_buttons) -/** @brief Keypress handler */ -static gboolean playlists_keypress(GtkWidget attribute((unused)) *widget, - GdkEventKey *event, - gpointer attribute((unused)) user_data) { +/** @brief Create the list of playlists for the edit playlists window */ +static GtkWidget *playlist_picker_create(void) { + /* Create the list of playlist and populate it */ + playlist_picker_fill(NULL, NULL, NULL); + /* Create the tree view */ + GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlist_picker_list)); + /* ...and the renderers for it */ + GtkCellRenderer *cr = gtk_cell_renderer_text_new(); + GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist", + cr, + "text", 0, + NULL); + gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col); + /* Get the selection for the view; set its mode; arrange for a callback when + * it changes */ + playlist_picker_selected = NULL; + playlist_picker_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)); + gtk_tree_selection_set_mode(playlist_picker_selection, GTK_SELECTION_BROWSE); + g_signal_connect(playlist_picker_selection, "changed", + G_CALLBACK(playlist_picker_selection_changed), NULL); + + /* Create the control buttons */ + GtkWidget *buttons = create_buttons_box(playlist_picker_buttons, + NPLAYLIST_PICKER_BUTTONS, + gtk_hbox_new(FALSE, 1)); + playlist_picker_delete_button = playlist_picker_buttons[1].widget; + + playlist_picker_selection_changed(NULL, NULL); + + /* Buttons live below the list */ + GtkWidget *vbox = gtk_vbox_new(FALSE, 0); + gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0); + gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0); + + g_signal_connect(tree, "key-press-event", + G_CALLBACK(playlist_picker_keypress), 0); + + return vbox; +} + +static gboolean playlist_picker_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(playlists_window); + case GDK_BackSpace: + case GDK_Delete: + playlist_picker_delete(NULL, NULL); return TRUE; default: return FALSE; } } -void edit_playlists(gpointer attribute((unused)) callback_data, - guint attribute((unused)) callback_action, - GtkWidget attribute((unused)) *menu_item) { - GtkWidget *tree, *hbox, *vbox, *buttons; - GtkCellRenderer *cr; - GtkTreeViewColumn *col; +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); +} + +/** @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(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_protocol_error(0, 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_protocol_error(0, 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) { /* If the window already exists, raise it */ - if(playlists_window) { - gtk_window_present(GTK_WINDOW(playlists_window)); + if(playlist_window) { + gtk_window_present(GTK_WINDOW(playlist_window)); return; } /* Create the window */ - playlists_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_widget_set_style(playlists_window, tool_style); - g_signal_connect(playlists_window, "destroy", - G_CALLBACK(gtk_widget_destroyed), &playlists_window); - gtk_window_set_title(GTK_WINDOW(playlists_window), "Playlists Management"); + playlist_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_widget_set_style(playlist_window, tool_style); + g_signal_connect(playlist_window, "destroy", + G_CALLBACK(playlist_window_destroyed), &playlist_window); + gtk_window_set_title(GTK_WINDOW(playlist_window), "Playlists Management"); /* TODO loads of this is very similar to (copied from!) users.c - can we * de-dupe? */ /* Keyboard shortcuts */ - g_signal_connect(playlists_window, "key-press-event", - G_CALLBACK(playlists_keypress), 0); + g_signal_connect(playlist_window, "key-press-event", + G_CALLBACK(playlist_window_keypress), 0); /* default size is too small */ - gtk_window_set_default_size(GTK_WINDOW(playlists_window), 240, 240); - /* Create the list of playlist and populate it */ - playlists_fill(); - /* Create the tree view */ - tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlists_list)); - /* ...and the renderers for it */ - cr = gtk_cell_renderer_text_new(); - col = gtk_tree_view_column_new_with_attributes("Playlist", - cr, - "text", 0, - NULL); - gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col); - /* Get the selection for the view; set its mode; arrange for a callback when - * it changes */ - playlists_selected = NULL; - playlists_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)); - gtk_tree_selection_set_mode(playlists_selection, GTK_SELECTION_BROWSE); - g_signal_connect(playlists_selection, "changed", - G_CALLBACK(playlists_selection_changed), NULL); + gtk_window_set_default_size(GTK_WINDOW(playlist_window), 640, 320); - /* Create the control buttons */ - buttons = create_buttons_box(playlists_buttons, - NPLAYLISTS_BUTTONS, - gtk_hbox_new(FALSE, 1)); - playlists_delete_button = playlists_buttons[1].widget; + GtkWidget *hbox = gtk_hbox_new(FALSE, 0); + gtk_box_pack_start(GTK_BOX(hbox), playlist_picker_create(), + FALSE/*expand*/, FALSE, 0); + gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), + FALSE/*expand*/, FALSE, 2); + gtk_box_pack_start(GTK_BOX(hbox), playlists_editor_create(), + TRUE/*expand*/, TRUE/*fill*/, 0); - /* Buttons live below the list */ - vbox = gtk_vbox_new(FALSE, 0); - gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0); - gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0); + gtk_container_add(GTK_CONTAINER(playlist_window), frame_widget(hbox, NULL)); + 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; + } +} - hbox = gtk_hbox_new(FALSE, 0); - gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0); - gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), FALSE/*expand*/, FALSE, 2); - // TODO something to edit the playlist in - //gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0); - gtk_container_add(GTK_CONTAINER(playlists_window), frame_widget(hbox, NULL)); - gtk_widget_show_all(playlists_window); +/** @brief Called when the playlist window is destroyed */ +static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget, + GtkWidget **widget_pointer) { + destroy_queuelike(&ql_playlist); + playlist_picker_destroy(); + *widget_pointer = NULL; } /** @brief Initialize playlist support */ void playlists_init(void) { /* We re-get all playlists upon any change... */ - event_register("playlist-created", playlists_update, 0); - event_register("playlist-modified", playlists_update, 0); - event_register("playlist-deleted", playlists_update, 0); + event_register("playlist-created", playlist_list_update, 0); + event_register("playlist-deleted", playlist_list_update, 0); /* ...and on reconnection */ - event_register("log-connected", playlists_update, 0); + event_register("log-connected", playlist_list_update, 0); /* ...and from time to time */ - event_register("periodic-slow", playlists_update, 0); + event_register("periodic-slow", playlist_list_update, 0); /* ...and at startup */ - event_register("playlists-updated", menu_playlists_changed, 0); - playlists_update(0, 0, 0); -} + playlist_list_update(0, 0, 0); -#endif + /* Update the playlists menu when the set of playlists changes */ + event_register("playlists-updated", playlist_menu_changed, 0); + /* Update the new-playlist OK button when the set of playlists changes */ + event_register("playlists-updated", playlist_new_changed, 0); + /* Update the list of playlists in the edit window when the set changes */ + event_register("playlists-updated", playlist_picker_fill, 0); + /* Update the displayed playlist when it is modified */ + event_register("playlist-modified", playlist_editor_fill, 0); + /* Update the shared/public/etc buttons when a playlist is modified */ + event_register("playlist-modified", playlist_editor_set_buttons, 0); +} /* Local Variables: diff --git a/disobedience/progress.c b/disobedience/progress.c index 2e31603..33dbfc9 100644 --- a/disobedience/progress.c +++ b/disobedience/progress.c @@ -30,12 +30,14 @@ struct progress_window { }; /** @brief Create a progress window */ -struct progress_window *progress_window_new(const char *title) { +struct progress_window *progress_window_new(const char *title, + GtkWidget *parent) { struct progress_window *pw = xmalloc(sizeof *pw); pw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_window_set_transient_for(GTK_WINDOW(pw->window), - GTK_WINDOW(toplevel)); + if(parent) + gtk_window_set_transient_for(GTK_WINDOW(pw->window), + GTK_WINDOW(parent)); g_signal_connect(pw->window, "destroy", G_CALLBACK(gtk_widget_destroyed), &pw->window); gtk_window_set_default_size(GTK_WINDOW(pw->window), 360, -1); diff --git a/disobedience/properties.c b/disobedience/properties.c index 62ea22f..092e8db 100644 --- a/disobedience/properties.c +++ b/disobedience/properties.c @@ -186,7 +186,8 @@ static gboolean properties_keypress(GtkWidget attribute((unused)) *widget, } } -void properties(int ntracks, const char **tracks) { +void properties(int ntracks, const char **tracks, + GtkWidget *parent) { int n, m; struct prefdata *f; GtkWidget *buttonbox, *vbox, *label, *entry, *propagate; @@ -299,7 +300,9 @@ void properties(int ntracks, const char **tracks) { if(pw) progress_window_progress(pw, 0, 0); /* Pop up a progress bar while we're waiting */ - pw = progress_window_new("Fetching Track Properties"); + while(parent->parent) + parent = parent->parent; + pw = progress_window_new("Fetching Track Properties", parent); } /* Everything is filled in now */ diff --git a/disobedience/queue-generic.c b/disobedience/queue-generic.c index 82bd939..de86f29 100644 --- a/disobedience/queue-generic.c +++ b/disobedience/queue-generic.c @@ -43,19 +43,6 @@ #include "multidrag.h" #include "autoscroll.h" -static const GtkTargetEntry queuelike_targets[] = { - { - (char *)"text/x-disorder-queued-tracks", /* drag type */ - GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */ - 0 /* ID value */ - }, - { - (char *)"text/x-disorder-playable-tracks", /* drag type */ - GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ - 1 /* ID value */ - }, -}; - /* Track detail lookup ----------------------------------------------------- */ static void queue_lookups_completed(const char attribute((unused)) *event, @@ -255,22 +242,27 @@ static void record_queue_map(hash *h, hash_add(h, id, empty, HASH_INSERT); nqd = hash_find(h, id); } - if(old) + if(old) { +#if DEBUG_QUEUE + fprintf(stderr, " old: %s\n", id); +#endif nqd->old = old; - if(new) + } + if(new) { +#if DEBUG_QUEUE + fprintf(stderr, " new: %s\n", id); +#endif nqd->new = new; + } } -#if 0 +#if DEBUG_QUEUE static void dump_queue(struct queue_entry *head, struct queue_entry *mark) { for(struct queue_entry *q = head; q; q = q->next) { if(q == mark) - fprintf(stderr, "!"); - fprintf(stderr, "%s", q->id); - if(q->next) - fprintf(stderr, " "); + fprintf(stderr, " !"); + fprintf(stderr, " %s\n", q->id); } - fprintf(stderr, "\n"); } static void dump_rows(struct queuelike *ql) { @@ -280,11 +272,8 @@ static void dump_rows(struct queuelike *ql) { while(it) { struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter); it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); - fprintf(stderr, "%s", q->id); - if(it) - fprintf(stderr, " "); + fprintf(stderr, " %s\n", q->id); } - fprintf(stderr, "\n"); } #endif @@ -300,11 +289,15 @@ void ql_new_queue(struct queuelike *ql, ++suppress_actions; /* Tell every queue entry which queue owns it */ - //fprintf(stderr, "%s: filling in q->ql\n", ql->name); +#if DEBUG_QUEUE + fprintf(stderr, "%s: filling in q->ql\n", ql->name); +#endif for(struct queue_entry *q = newq; q; q = q->next) q->ql = ql; - //fprintf(stderr, "%s: constructing h\n", ql->name); +#if DEBUG_QUEUE + fprintf(stderr, "%s: constructing h\n", ql->name); +#endif /* 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) @@ -314,7 +307,9 @@ void ql_new_queue(struct queuelike *ql, /* 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); +#if DEBUG_QUEUE + fprintf(stderr, "%s: deleting rows...\n", ql->name); +#endif GtkTreeIter iter[1]; gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter); @@ -331,7 +326,9 @@ void ql_new_queue(struct queuelike *ql, ++kept; } else { /* Delete this row (and move iter to the next one) */ - //fprintf(stderr, " delete %s", q->id); +#if DEBUG_QUEUE + fprintf(stderr, " delete %s\n", q->id); +#endif it = gtk_list_store_remove(ql->store, iter); ++deleted; } @@ -342,7 +339,9 @@ void ql_new_queue(struct queuelike *ql, /* 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); +#if DEBUG_QUEUE + fprintf(stderr, "%s: adding rows...\n", ql->name); +#endif 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); @@ -363,7 +362,9 @@ void ql_new_queue(struct queuelike *ql, gtk_list_store_set(ql->store, iter, ql->ncolumns + QUEUEPOINTER_COLUMN, q, -1); - //fprintf(stderr, " add %s", q->id); +#if DEBUG_QUEUE + fprintf(stderr, " add %s\n", q->id); +#endif ++inserted; } after = newq; @@ -376,49 +377,63 @@ void ql_new_queue(struct queuelike *ql, * 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' */ +#if DEBUG_QUEUE + fprintf(stderr, "%s: rearranging rows\n", ql->name); + fprintf(stderr, "%s: target state:\n", ql->name); + dump_queue(newq, 0); + fprintf(stderr, "%s: current state:\n", ql->name); + dump_rows(ql); +#endif + it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter); + struct queue_entry *tq = newq; /* t-for-target */ int swaps = 0, searches = 0; + int row = 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"); + struct queue_entry *cq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter); + /* c-for-current */ + + /* Everything has the right queue pointer (see above) so it's sufficient to + * compare pointers to detect mismatches */ + if(cq != tq) { +#if DEBUG_QUEUE + fprintf(stderr, " pointer mismatch at row %d\n", row); + fprintf(stderr, " target id %s\n", tq->id); + fprintf(stderr, " actual id %s\n", cq->id); +#endif + /* Start looking for the target row fromn the next row */ 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) +#if DEBUG_QUEUE + fprintf(stderr, " candidate: %s\n", nq->id); +#endif + if(nq == tq) break; nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next); ++searches; } + /* Note that this assertion will fail in the face of duplicate IDs. + * q->id really does need to be unique. */ 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); +#if DEBUG_QUEUE + fprintf(stderr, "%s: found it. new row state:\n", ql->name); + dump_rows(ql); +#endif ++swaps; } /* ...and onto the next one */ it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); - rq = rq->next; + tq = tq->next; + ++row; } -#if 0 +#if DEBUG_QUEUE fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name, kept, inserted, deleted, swaps, searches); + fprintf(stderr, "done\n"); #endif - //fprintf(stderr, "done\n"); ql->q = newq; /* Set the rest of the columns in new rows */ ql_update_list_store(ql); @@ -490,6 +505,28 @@ static GtkTreePath *ql_drop_path(GtkWidget *w, return path; } +#if 0 +static const char *act(GdkDragAction action) { + struct dynstr d[1]; + + dynstr_init(d); + if(action & GDK_ACTION_DEFAULT) + dynstr_append_string(d, "|DEFAULT"); + if(action & GDK_ACTION_COPY) + dynstr_append_string(d, "|COPY"); + if(action & GDK_ACTION_MOVE) + dynstr_append_string(d, "|MOVE"); + if(action & GDK_ACTION_LINK) + dynstr_append_string(d, "|LINK"); + if(action & GDK_ACTION_PRIVATE) + dynstr_append_string(d, "|PRIVATE"); + if(action & GDK_ACTION_ASK) + dynstr_append_string(d, "|ASK"); + dynstr_terminate(d); + return d->nvec ? d->vec + 1 : ""; +} +#endif + /** @brief Called when a drag moves within a candidate destination * @param w Destination widget * @param dc Drag context @@ -524,8 +561,13 @@ static gboolean ql_drag_motion(GtkWidget *w, action = GDK_ACTION_MOVE; else if(dc->actions & GDK_ACTION_COPY) action = GDK_ACTION_COPY; - /*fprintf(stderr, "suggested %#x actions %#x result %#x\n", - dc->suggested_action, dc->actions, action);*/ + /* TODO this comes up with the wrong answer sometimes. If we are in the + * middle of a rearrange then the suggested action will be COPY, which we'll + * take, even though MOVE would actually be appropriate. The drag still + * seems to work, but it _is_ wrong. The answer is to take the target into + * account. */ + /*fprintf(stderr, "suggested %s actions %s result %s\n", + act(dc->suggested_action), act(dc->actions), act(action));*/ if(action) { // If the action is acceptable then we see if this widget is acceptable if(gtk_drag_dest_find_target(w, dc, NULL) == GDK_NONE) @@ -608,12 +650,13 @@ static void ql_drag_data_get_collect(GtkTreeModel *model, static void ql_drag_data_get(GtkWidget attribute((unused)) *w, GdkDragContext attribute((unused)) *dc, GtkSelectionData *data, - guint attribute((unused)) info_, + guint attribute((unused)) info, guint attribute((unused)) time_, gpointer user_data) { struct queuelike *const ql = user_data; struct dynstr result[1]; + //fprintf(stderr, "ql_drag_data_get %s info=%d\n", ql->name, info); dynstr_init(result); gtk_tree_selection_selected_foreach(ql->selection, ql_drag_data_get_collect, @@ -646,7 +689,7 @@ static void ql_drag_data_received(GtkWidget attribute((unused)) *w, gint x, gint y, GtkSelectionData *data, - guint attribute((unused)) info_, + guint info_, guint attribute((unused)) time_, gpointer user_data) { struct queuelike *const ql = user_data; @@ -654,7 +697,7 @@ static void ql_drag_data_received(GtkWidget attribute((unused)) *w, struct vector ids[1], tracks[1]; int parity = 0; - //fprintf(stderr, "drag-data-received: %d,%d info_=%u\n", x, y, info_); + //fprintf(stderr, "drag-data-received: %d,%d info=%u\n", x, y, info_); /* Get the selection string */ p = result = (char *)gtk_selection_data_get_text(data); if(!result) { @@ -687,18 +730,21 @@ static void ql_drag_data_received(GtkWidget attribute((unused)) *w, GtkTreePath *path = ql_drop_path(w, GTK_TREE_MODEL(ql->store), x, y, &pos); if(path) { q = ql_path_to_q(GTK_TREE_MODEL(ql->store), path); + //fprintf(stderr, " drop path: %s q=%p pos=%d\n", + // gtk_tree_path_to_string(path), q, pos); } else { /* This generally means a drop past the end of the queue. We find the last * element in the queue and ask to move after that. */ for(q = ql->q; q && q->next; q = q->next) ; + //fprintf(stderr, " after end. q=%p. pos=%d\n", q, pos); } switch(pos) { case GTK_TREE_VIEW_DROP_BEFORE: case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE: if(q) { q = q->prev; - //fprintf(stderr, " ...but we like to drop near %s\n", + //fprintf(stderr, " but we like to drop near %s\n", // q ? q->id : "NULL"); } break; @@ -711,11 +757,12 @@ static void ql_drag_data_received(GtkWidget attribute((unused)) *w, /* Note that q->id can match one of ids[]. This doesn't matter for * moveafter but TODO may matter for playlist support. */ switch(info_) { - case 0: - /* Rearrangement. Send ID and track data. */ + case QUEUED_TRACKS_ID: + case PLAYLIST_TRACKS_ID: + /* Rearrangement within some widget. Send ID and track data. */ ql->drop(ql, tracks->nvec, tracks->vec, ids->vec, q); break; - case 1: + case PLAYABLE_TRACKS_ID: /* Copying between widgets. IDs mean nothing so don't send them. */ ql->drop(ql, tracks->nvec, tracks->vec, NULL, q); break; @@ -724,6 +771,14 @@ static void ql_drag_data_received(GtkWidget attribute((unused)) *w, gtk_tree_path_free(path); } +static int count_drag_targets(const GtkTargetEntry *targets) { + const GtkTargetEntry *t = targets; + + while(t->target) + ++t; + return t - targets; +} + /** @brief Initialize a @ref queuelike */ GtkWidget *init_queuelike(struct queuelike *ql) { D(("init_queuelike")); @@ -763,6 +818,7 @@ GtkWidget *init_queuelike(struct queuelike *ql) { /* The selection should support multiple things being selected */ ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view)); + g_object_ref(ql->selection); gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE); /* Catch button presses */ @@ -791,15 +847,15 @@ GtkWidget *init_queuelike(struct queuelike *ql) { /* This view will act as a drag source */ gtk_drag_source_set(ql->view, GDK_BUTTON1_MASK, - queuelike_targets, - sizeof queuelike_targets / sizeof *queuelike_targets, - GDK_ACTION_MOVE); + ql->drag_source_targets, + count_drag_targets(ql->drag_source_targets), + ql->drag_dest_actions); /* This view will act as a drag destination */ gtk_drag_dest_set(ql->view, GTK_DEST_DEFAULT_HIGHLIGHT|GTK_DEST_DEFAULT_DROP, - queuelike_targets, - sizeof queuelike_targets / sizeof *queuelike_targets, - GDK_ACTION_MOVE|GDK_ACTION_COPY); + ql->drag_dest_targets, + count_drag_targets(ql->drag_dest_targets), + ql->drag_dest_actions); g_signal_connect(ql->view, "drag-motion", G_CALLBACK(ql_drag_motion), ql); g_signal_connect(ql->view, "drag-leave", @@ -814,9 +870,9 @@ GtkWidget *init_queuelike(struct queuelike *ql) { /* For queues that cannot accept a drop we still accept a copy out */ gtk_drag_source_set(ql->view, GDK_BUTTON1_MASK, - queuelike_targets, - sizeof queuelike_targets / sizeof *queuelike_targets, - GDK_ACTION_COPY); + ql->drag_source_targets, + count_drag_targets(ql->drag_source_targets), + ql->drag_source_actions); g_signal_connect(ql->view, "drag-data-get", G_CALLBACK(ql_drag_data_get), ql); make_treeview_multidrag(ql->view, NULL); @@ -824,7 +880,8 @@ GtkWidget *init_queuelike(struct queuelike *ql) { /* TODO style? */ - ql->init(ql); + if(ql->init) + ql->init(ql); /* Update display text when lookups complete */ event_register("lookups-completed", queue_lookups_completed, ql); @@ -834,6 +891,31 @@ GtkWidget *init_queuelike(struct queuelike *ql) { return scrolled; } +/** @brief Destroy a queuelike + * @param ql Queuelike to destroy + * + * Returns @p ql to its initial state. + */ +void destroy_queuelike(struct queuelike *ql) { + if(ql->store) { + g_object_unref(ql->store); + ql->store = NULL; + } + if(ql->view) { + gtk_object_destroy(GTK_OBJECT(ql->view)); + ql->view = NULL; + } + if(ql->menu) { + gtk_object_destroy(GTK_OBJECT(ql->menu)); + ql->menu = NULL; + } + if(ql->selection) { + g_object_unref(ql->selection); + ql->selection = NULL; + } + ql->q = NULL; +} + /* Local Variables: c-basic-offset:2 diff --git a/disobedience/queue-generic.h b/disobedience/queue-generic.h index c15b383..efd6400 100644 --- a/disobedience/queue-generic.h +++ b/disobedience/queue-generic.h @@ -1,6 +1,6 @@ /* * This file is part of DisOrder - * Copyright (C) 2006-2008 Richard Kettlewell + * Copyright (C) 2006-2009 Richard Kettlewell * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -104,11 +104,31 @@ struct queuelike { void (*drop)(struct queuelike *ql, int ntracks, char **tracks, char **ids, struct queue_entry *after_me); - /** @brief Stashed drag target row */ - GtkTreePath *drag_target; + /** @brief Source target list */ + const GtkTargetEntry *drag_source_targets; + + /** @brief Drag source actions */ + GdkDragAction drag_source_actions; + + /** @brief Destination target list */ + const GtkTargetEntry *drag_dest_targets; + + /** @brief Drag destination actions */ + GdkDragAction drag_dest_actions; + }; enum { + PLAYABLE_TRACKS_ID, + QUEUED_TRACKS_ID, + PLAYLIST_TRACKS_ID +}; + +#define PLAYABLE_TRACKS (char *)"text/x-disorder-playable-tracks" +#define QUEUED_TRACKS (char *)"text/x-disorder-queued-tracks" +#define PLAYLIST_TRACKS (char *)"text/x-disorder-playlist-tracks" + +enum { QUEUEPOINTER_COLUMN, FOREGROUND_COLUMN, BACKGROUND_COLUMN, @@ -157,6 +177,7 @@ gboolean ql_button_release(GtkWidget *widget, GdkEventButton *event, gpointer user_data); GtkWidget *init_queuelike(struct queuelike *ql); +void destroy_queuelike(struct queuelike *ql); void ql_update_list_store(struct queuelike *ql) ; void ql_update_row(struct queue_entry *q, GtkTreeIter *iter); diff --git a/disobedience/queue-menu.c b/disobedience/queue-menu.c index beb40f5..ebf8555 100644 --- a/disobedience/queue-menu.c +++ b/disobedience/queue-menu.c @@ -71,7 +71,7 @@ void ql_properties_activate(GtkMenuItem attribute((unused)) *menuitem, gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); } if(v->nvec) - properties(v->nvec, (const char **)v->vec); + properties(v->nvec, (const char **)v->vec, ql->view); } /* Scratch */ diff --git a/disobedience/queue.c b/disobedience/queue.c index 80b163a..0db90d7 100644 --- a/disobedience/queue.c +++ b/disobedience/queue.c @@ -225,6 +225,22 @@ static struct menuitem queue_menuitems[] = { { "Adopt track", ql_adopt_activate, ql_adopt_sensitive, 0, 0 }, }; +static const GtkTargetEntry queue_targets[] = { + { + QUEUED_TRACKS, /* drag type */ + GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */ + QUEUED_TRACKS_ID /* ID value */ + }, + { + PLAYABLE_TRACKS, /* drag type */ + GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ + PLAYABLE_TRACKS_ID, /* ID value */ + }, + { + .target = NULL + } +}; + struct queuelike ql_queue = { .name = "queue", .init = queue_init, @@ -232,7 +248,11 @@ struct queuelike ql_queue = { .ncolumns = sizeof queue_columns / sizeof *queue_columns, .menuitems = queue_menuitems, .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems, - .drop = queue_drop + .drop = queue_drop, + .drag_source_targets = queue_targets, + .drag_source_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, + .drag_dest_targets = queue_targets, + .drag_dest_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, }; /** @brief Called when a key is pressed in the queue tree view */ diff --git a/disobedience/recent.c b/disobedience/recent.c index f53e631..0e74feb 100644 --- a/disobedience/recent.c +++ b/disobedience/recent.c @@ -91,6 +91,8 @@ struct queuelike ql_recent = { .ncolumns = sizeof recent_columns / sizeof *recent_columns, .menuitems = recent_menuitems, .nmenuitems = sizeof recent_menuitems / sizeof *recent_menuitems, + .drag_source_targets = choose_targets, + .drag_source_actions = GDK_ACTION_COPY, }; GtkWidget *recent_widget(void) { diff --git a/doc/disobedience.1.in b/doc/disobedience.1.in index f7fb5c2..f6ce906 100644 --- a/doc/disobedience.1.in +++ b/doc/disobedience.1.in @@ -49,6 +49,12 @@ Edit the details of the selected tracks. See .B "Properties Window" below. +.TP +.B "Edit Playlists" +Edit playlists. +See +.B "Playlists Window" +below. .SS "Control Menu" This has the following options: .TP @@ -67,6 +73,10 @@ Enables or disables network play. See .B "NETWORK PLAY" below. +.TP +.B "Activate Playlist" +This submenu has the selection of playlists which you are able to play. +Picking one will add its contents to the queue. .SS "Help Menu" This has only one option, "About DisOrder", which pops up a box giving the name, author and version number of the software. @@ -331,6 +341,39 @@ configuration (but this may be changed in the future). As above, click on .PP The "Delete" button deletes the selected user. This operation cannot be undone. +.SS "Playlists Window" +A playlist is a collection of tracks that can be prepared in advance and then +played as a unit. +Playlists come in three kinds: +.TP +.B shared +A playlist that anyone can edit. +These are not owned by any user. +.TP +.B public +A playlist that only one user (its owner) can edit, +but that anyone can see the contents of. +.TP +.B private +A playlist that is entirely private to one user. +.PP +Public and private playlists start with the owner's username. +.PP +The left side of the playlist window is a list of all playlists that you can +see: all shared and public playlists plus your private playlists. +Selecting one will bring it up in the right hand side of the window, +allowing it to be reviewed or edited. +You can drag tracks from the choose tab in the main window into it and +rearrange tracks already in the playlist by selecting and dragging them. +.PP +The add button creates a new playlist. +You must selected whether it will be a shared, public or private playlist. +.PP +At the bottom of the window are radio buttons showing which kind of playlist it +is. +If you own the playlist you can use these to switch it between public and +private, but if it is shared or owned by someone else these buttons will be +greyed out. .SH "KEYBOARD SHORTCUTS" .TP .B CTRL+A diff --git a/doc/disorder_protocol.5.in b/doc/disorder_protocol.5.in index 6bc9c47..6a25e1a 100644 --- a/doc/disorder_protocol.5.in +++ b/doc/disorder_protocol.5.in @@ -232,6 +232,7 @@ Requires permission to modify that playlist and the \fBplay\fR right. .B playlist-get \fIPLAYLIST\fR Get the contents of a playlist, in a response body. Requires permission to read that playlist and the \fBread\fR right. +If the playlist does not exist the response is 555. .TP .B playlist-get-share \fIPLAYLIST\fR Get the sharing status of a playlist. diff --git a/lib/Makefile.am b/lib/Makefile.am index 3c1e2f1..3505eb9 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -91,6 +91,7 @@ libdisorder_a_SOURCES=charset.c charset.h \ unicode.h unicode.c \ unidata.h unidata.c \ vacopy.h \ + validity.c validity.h \ vector.c vector.h \ version.c version.h \ wav.h wav.c \ diff --git a/lib/trackdb-int.h b/lib/trackdb-int.h index b21b5d9..3be725e 100644 --- a/lib/trackdb-int.h +++ b/lib/trackdb-int.h @@ -153,7 +153,6 @@ int trackdb_get_global_tid(const char *name, char **parsetags(const char *s); int tag_intersection(char **a, char **b); -int valid_username(const char *user); #endif /* TRACKDB_INT_H */ diff --git a/lib/trackdb-playlists.c b/lib/trackdb-playlists.c index fd33393..27d3319 100644 --- a/lib/trackdb-playlists.c +++ b/lib/trackdb-playlists.c @@ -33,6 +33,7 @@ #include "configuration.h" #include "vector.h" #include "eventlog.h" +#include "validity.h" static int trackdb_playlist_get_tid(const char *name, const char *who, @@ -54,43 +55,6 @@ static int trackdb_playlist_delete_tid(const char *name, const char *who, DB_TXN *tid); -/** @brief Parse a playlist name - * @param name Playlist name - * @param ownerp Where to put owner, or NULL - * @param sharep Where to put default sharing, or NULL - * @return 0 on success, -1 on error - * - * Playlists take the form USER.PLAYLIST or just PLAYLIST. The PLAYLIST part - * is alphanumeric and nonempty. USER is a username (see valid_username()). - */ -int playlist_parse_name(const char *name, - char **ownerp, - char **sharep) { - const char *dot = strchr(name, '.'), *share; - char *owner; - - if(dot) { - /* Owned playlist */ - owner = xstrndup(name, dot - name); - if(!valid_username(owner)) - return -1; - if(!valid_username(dot + 1)) - return -1; - share = "private"; - } else { - /* Shared playlist */ - if(!valid_username(name)) - return -1; - owner = 0; - share = "shared"; - } - if(ownerp) - *ownerp = owner; - if(sharep) - *sharep = xstrdup(share); - return 0; -} - /** @brief Check read access rights * @param name Playlist name * @param who Who wants to read diff --git a/lib/trackdb.c b/lib/trackdb.c index d2ebe07..628498a 100644 --- a/lib/trackdb.c +++ b/lib/trackdb.c @@ -59,6 +59,7 @@ #include "unidata.h" #include "base64.h" #include "sendmail.h" +#include "validity.h" #define RESCAN "disorder-rescan" #define DEADLOCK "disorder-deadlock" @@ -3013,32 +3014,6 @@ static int trusted(const char *user) { return n < config->trust.n; } -/** @brief Return non-zero for a valid username - * @param user Candidate username - * @return Nonzero if it's valid - * - * Currently we only allow the letters and digits in ASCII. We could be more - * liberal than this but it is a nice simple test. It is critical that - * semicolons are never allowed. - * - * NB also used by playlist_parse_name() to validate playlist names! - */ -int valid_username(const char *user) { - if(!*user) - return 0; - while(*user) { - const uint8_t c = *user++; - /* For now we are very strict */ - if((c >= 'a' && c <= 'z') - || (c >= 'A' && c <= 'Z') - || (c >= '0' && c <= '9')) - /* ok */; - else - return 0; - } - return 1; -} - /** @brief Add a user * @param user Username * @param password Initial password or NULL diff --git a/lib/trackdb.h b/lib/trackdb.h index 901a74a..1d7c8e9 100644 --- a/lib/trackdb.h +++ b/lib/trackdb.h @@ -184,9 +184,6 @@ void trackdb_add_rescanned(void (*rescanned)(void *ru), void *ru); int trackdb_rescan_underway(void); -int playlist_parse_name(const char *name, - char **ownerp, - char **sharep); int trackdb_playlist_get(const char *name, const char *who, char ***tracksp, diff --git a/lib/validity.c b/lib/validity.c new file mode 100644 index 0000000..408c4d5 --- /dev/null +++ b/lib/validity.c @@ -0,0 +1,98 @@ +/* + * This file is part of DisOrder + * Copyright (C) 2009 Richard Kettlewell + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ +/** @file lib/validity.c + * @brief Various validity checks + */ +#include "common.h" +#include "validity.h" + +#include "mem.h" + +/** @brief Parse a playlist name + * @param name Playlist name + * @param ownerp Where to put owner, or NULL + * @param sharep Where to put default sharing, or NULL + * @return 0 on success, -1 on error + * + * Playlists take the form USER.PLAYLIST or just PLAYLIST. The PLAYLIST part + * is alphanumeric and nonempty. USER is a username (see valid_username()). + */ +int playlist_parse_name(const char *name, + char **ownerp, + char **sharep) { + const char *dot = strchr(name, '.'), *share; + char *owner; + + if(dot) { + /* Owned playlist */ + owner = xstrndup(name, dot - name); + if(!valid_username(owner)) + return -1; + if(!valid_username(dot + 1)) + return -1; + share = "private"; + } else { + /* Shared playlist */ + if(!valid_username(name)) + return -1; + owner = 0; + share = "shared"; + } + if(ownerp) + *ownerp = owner; + if(sharep) + *sharep = xstrdup(share); + return 0; +} + +/** @brief Return non-zero for a valid username + * @param user Candidate username + * @return Nonzero if it's valid + * + * Currently we only allow the letters and digits in ASCII. We could be more + * liberal than this but it is a nice simple test. It is critical that + * semicolons are never allowed. + * + * NB also used by playlist_parse_name() to validate playlist names! + */ +int valid_username(const char *user) { + if(!*user) + return 0; + while(*user) { + const uint8_t c = *user++; + /* For now we are very strict */ + if((c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9')) + /* ok */; + else + return 0; + } + return 1; +} + +/* +Local Variables: +c-basic-offset:2 +comment-column:40 +fill-column:79 +indent-tabs-mode:nil +End: +*/ diff --git a/lib/validity.h b/lib/validity.h new file mode 100644 index 0000000..8ce2505 --- /dev/null +++ b/lib/validity.h @@ -0,0 +1,43 @@ +/* + * This file is part of DisOrder + * Copyright (C) 2009 Richard Kettlewell + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ +/** @file lib/validity.c + * @brief Various validity checks + */ +#ifndef VALIDITY_H +#define VALIDITY_H + +#include "common.h" +#include "validity.h" + +int playlist_parse_name(const char *name, + char **ownerp, + char **sharep); +int valid_username(const char *user); + +#endif /* VALIDITY_H */ + +/* +Local Variables: +c-basic-offset:2 +comment-column:40 +fill-column:79 +indent-tabs-mode:nil +End: +*/