| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2008, 2009 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | /** @file disobedience/playlists.c |
| 21 | * @brief Playlist support for Disobedience |
| 22 | * |
| 23 | * The playlists management window contains: |
| 24 | * - the playlist picker (a list of all playlists) TODO should be a tree! |
| 25 | * - an add button |
| 26 | * - a delete button |
| 27 | * - the playlist editor (a d+d-capable view of the currently picked playlist) |
| 28 | * - a close button TODO |
| 29 | * |
| 30 | * This file also maintains the playlist menu, allowing playlists to be |
| 31 | * activated from the main window's menu. |
| 32 | * |
| 33 | * Internally we maintain the playlist list, which is just the current list of |
| 34 | * playlists. Changes to this are reflected in the playlist menu and the |
| 35 | * playlist picker. |
| 36 | * |
| 37 | */ |
| 38 | #include "disobedience.h" |
| 39 | #include "queue-generic.h" |
| 40 | #include "popup.h" |
| 41 | #include "validity.h" |
| 42 | |
| 43 | static void playlist_list_received_playlists(void *v, |
| 44 | const char *err, |
| 45 | int nvec, char **vec); |
| 46 | static void playlist_editor_fill(const char *event, |
| 47 | void *eventdata, |
| 48 | void *callbackdata); |
| 49 | static int playlist_playall_sensitive(void *extra); |
| 50 | static void playlist_playall_activate(GtkMenuItem *menuitem, |
| 51 | gpointer user_data); |
| 52 | static int playlist_remove_sensitive(void *extra) ; |
| 53 | static void playlist_remove_activate(GtkMenuItem *menuitem, |
| 54 | gpointer user_data); |
| 55 | static void playlist_new_locked(void *v, const char *err); |
| 56 | static void playlist_new_retrieved(void *v, const char *err, |
| 57 | int nvec, |
| 58 | char **vec); |
| 59 | static void playlist_new_created(void *v, const char *err); |
| 60 | static void playlist_new_unlocked(void *v, const char *err); |
| 61 | static void playlist_new_entry_edited(GtkEditable *editable, |
| 62 | gpointer user_data); |
| 63 | static void playlist_new_button_toggled(GtkToggleButton *tb, |
| 64 | gpointer userdata); |
| 65 | static void playlist_new_changed(const char *event, |
| 66 | void *eventdata, |
| 67 | void *callbackdata); |
| 68 | static const char *playlist_new_valid(void); |
| 69 | static void playlist_new_details(char **namep, |
| 70 | char **fullnamep, |
| 71 | gboolean *sharedp, |
| 72 | gboolean *publicp, |
| 73 | gboolean *privatep); |
| 74 | static void playlist_new_ok(GtkButton *button, |
| 75 | gpointer userdata); |
| 76 | static void playlist_new_cancel(GtkButton *button, |
| 77 | gpointer userdata); |
| 78 | static void playlists_editor_received_tracks(void *v, |
| 79 | const char *err, |
| 80 | int nvec, char **vec); |
| 81 | static void playlist_window_destroyed(GtkWidget *widget, |
| 82 | GtkWidget **widget_pointer); |
| 83 | static gboolean playlist_window_keypress(GtkWidget *widget, |
| 84 | GdkEventKey *event, |
| 85 | gpointer user_data); |
| 86 | static int playlistcmp(const void *ap, const void *bp); |
| 87 | static void playlist_modify_locked(void *v, const char *err); |
| 88 | void playlist_modify_retrieved(void *v, const char *err, |
| 89 | int nvec, |
| 90 | char **vec); |
| 91 | static void playlist_modify_updated(void *v, const char *err); |
| 92 | static void playlist_modify_unlocked(void *v, const char *err); |
| 93 | static void playlist_drop(struct queuelike *ql, |
| 94 | int ntracks, |
| 95 | char **tracks, char **ids, |
| 96 | struct queue_entry *after_me); |
| 97 | struct playlist_modify_data; |
| 98 | static void playlist_drop_modify(struct playlist_modify_data *mod, |
| 99 | int nvec, char **vec); |
| 100 | static void playlist_remove_modify(struct playlist_modify_data *mod, |
| 101 | int nvec, char **vec); |
| 102 | static gboolean playlist_new_keypress(GtkWidget *widget, |
| 103 | GdkEventKey *event, |
| 104 | gpointer user_data); |
| 105 | static gboolean playlist_picker_keypress(GtkWidget *widget, |
| 106 | GdkEventKey *event, |
| 107 | gpointer user_data); |
| 108 | static void playlist_editor_button_toggled(GtkToggleButton *tb, |
| 109 | gpointer userdata); |
| 110 | static void playlist_editor_set_buttons(const char *event, |
| 111 | void *eventdata, |
| 112 | void *callbackdata); |
| 113 | static void playlist_editor_got_share(void *v, |
| 114 | const char *err, |
| 115 | const char *value); |
| 116 | static void playlist_editor_share_set(void *v, const char *err); |
| 117 | |
| 118 | /** @brief Playlist editing window */ |
| 119 | static GtkWidget *playlist_window; |
| 120 | |
| 121 | /** @brief Columns for the playlist editor */ |
| 122 | static const struct queue_column playlist_columns[] = { |
| 123 | { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE }, |
| 124 | { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE }, |
| 125 | { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE }, |
| 126 | }; |
| 127 | |
| 128 | /** @brief Pop-up menu for playlist editor |
| 129 | * |
| 130 | * Status: |
| 131 | * - track properties works but, bizarrely, raises the main window |
| 132 | * - play track works |
| 133 | * - play playlist works |
| 134 | * - select/deselect all work |
| 135 | */ |
| 136 | static struct menuitem playlist_menuitems[] = { |
| 137 | { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 }, |
| 138 | { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 }, |
| 139 | { "Play playlist", playlist_playall_activate, playlist_playall_sensitive, 0, 0 }, |
| 140 | { "Remove track from queue", playlist_remove_activate, playlist_remove_sensitive, 0, 0 }, |
| 141 | { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 }, |
| 142 | { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 }, |
| 143 | }; |
| 144 | |
| 145 | static const GtkTargetEntry playlist_targets[] = { |
| 146 | { |
| 147 | PLAYLIST_TRACKS, /* drag type */ |
| 148 | GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */ |
| 149 | PLAYLIST_TRACKS_ID /* ID value */ |
| 150 | }, |
| 151 | { |
| 152 | PLAYABLE_TRACKS, /* drag type */ |
| 153 | GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ |
| 154 | PLAYABLE_TRACKS_ID, /* ID value */ |
| 155 | }, |
| 156 | { |
| 157 | .target = NULL |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | /** @brief Queuelike for editing a playlist */ |
| 162 | static struct queuelike ql_playlist = { |
| 163 | .name = "playlist", |
| 164 | .columns = playlist_columns, |
| 165 | .ncolumns = sizeof playlist_columns / sizeof *playlist_columns, |
| 166 | .menuitems = playlist_menuitems, |
| 167 | .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems, |
| 168 | .drop = playlist_drop, |
| 169 | .drag_source_targets = playlist_targets, |
| 170 | .drag_source_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, |
| 171 | .drag_dest_targets = playlist_targets, |
| 172 | .drag_dest_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, |
| 173 | }; |
| 174 | |
| 175 | /* Maintaining the list of playlists ---------------------------------------- */ |
| 176 | |
| 177 | /** @brief Current list of playlists or NULL */ |
| 178 | char **playlists; |
| 179 | |
| 180 | /** @brief Count of playlists */ |
| 181 | int nplaylists; |
| 182 | |
| 183 | /** @brief Schedule an update to the list of playlists |
| 184 | * |
| 185 | * Called periodically and when a playlist is created or deleted. |
| 186 | */ |
| 187 | static void playlist_list_update(const char attribute((unused)) *event, |
| 188 | void attribute((unused)) *eventdata, |
| 189 | void attribute((unused)) *callbackdata) { |
| 190 | disorder_eclient_playlists(client, playlist_list_received_playlists, 0); |
| 191 | } |
| 192 | |
| 193 | /** @brief Called with a new list of playlists */ |
| 194 | static void playlist_list_received_playlists(void attribute((unused)) *v, |
| 195 | const char *err, |
| 196 | int nvec, char **vec) { |
| 197 | if(err) { |
| 198 | playlists = 0; |
| 199 | nplaylists = -1; |
| 200 | /* Probably means server does not support playlists */ |
| 201 | } else { |
| 202 | playlists = vec; |
| 203 | nplaylists = nvec; |
| 204 | qsort(playlists, nplaylists, sizeof (char *), playlistcmp); |
| 205 | } |
| 206 | /* Tell our consumers */ |
| 207 | event_raise("playlists-updated", 0); |
| 208 | } |
| 209 | |
| 210 | /** @brief qsort() callback for playlist name comparison */ |
| 211 | static int playlistcmp(const void *ap, const void *bp) { |
| 212 | const char *a = *(char **)ap, *b = *(char **)bp; |
| 213 | const char *ad = strchr(a, '.'), *bd = strchr(b, '.'); |
| 214 | int c; |
| 215 | |
| 216 | /* Group owned playlists by owner */ |
| 217 | if(ad && bd) { |
| 218 | const int adn = ad - a, bdn = bd - b; |
| 219 | if((c = strncmp(a, b, adn < bdn ? adn : bdn))) |
| 220 | return c; |
| 221 | /* Lexical order within playlists of a single owner */ |
| 222 | return strcmp(ad + 1, bd + 1); |
| 223 | } |
| 224 | |
| 225 | /* Owned playlists after shared ones */ |
| 226 | if(ad) { |
| 227 | return 1; |
| 228 | } else if(bd) { |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | /* Lexical order of shared playlists */ |
| 233 | return strcmp(a, b); |
| 234 | } |
| 235 | |
| 236 | /* Playlists menu ----------------------------------------------------------- */ |
| 237 | |
| 238 | static void playlist_menu_playing(void attribute((unused)) *v, |
| 239 | const char *err) { |
| 240 | if(err) |
| 241 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 242 | } |
| 243 | |
| 244 | /** @brief Play received playlist contents |
| 245 | * |
| 246 | * Passed as a completion callback by menu_activate_playlist(). |
| 247 | */ |
| 248 | static void playlist_menu_received_content(void attribute((unused)) *v, |
| 249 | const char *err, |
| 250 | int nvec, char **vec) { |
| 251 | if(err) { |
| 252 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 253 | return; |
| 254 | } |
| 255 | for(int n = 0; n < nvec; ++n) |
| 256 | disorder_eclient_play(client, vec[n], playlist_menu_playing, NULL); |
| 257 | } |
| 258 | |
| 259 | /** @brief Called to activate a playlist |
| 260 | * |
| 261 | * Called when the menu item for a playlist is clicked. |
| 262 | */ |
| 263 | static void playlist_menu_activate(GtkMenuItem *menuitem, |
| 264 | gpointer attribute((unused)) user_data) { |
| 265 | GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child); |
| 266 | const char *playlist = gtk_label_get_text(label); |
| 267 | |
| 268 | disorder_eclient_playlist_get(client, playlist_menu_received_content, |
| 269 | playlist, NULL); |
| 270 | } |
| 271 | |
| 272 | /** @brief Called when the playlists change |
| 273 | * |
| 274 | * Naively refills the menu. The results might be unsettling if the menu is |
| 275 | * currently open, but this is hopefuly fairly rare. |
| 276 | */ |
| 277 | static void playlist_menu_changed(const char attribute((unused)) *event, |
| 278 | void attribute((unused)) *eventdata, |
| 279 | void attribute((unused)) *callbackdata) { |
| 280 | if(!playlists_menu) |
| 281 | return; /* OMG too soon */ |
| 282 | GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu); |
| 283 | while(menu->children) |
| 284 | gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data)); |
| 285 | /* NB nplaylists can be -1 as well as 0 */ |
| 286 | for(int n = 0; n < nplaylists; ++n) { |
| 287 | GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]); |
| 288 | g_signal_connect(w, "activate", G_CALLBACK(playlist_menu_activate), 0); |
| 289 | gtk_widget_show(w); |
| 290 | gtk_menu_shell_append(menu, w); |
| 291 | } |
| 292 | gtk_widget_set_sensitive(menu_playlists_widget, |
| 293 | nplaylists > 0); |
| 294 | gtk_widget_set_sensitive(menu_editplaylists_widget, |
| 295 | nplaylists >= 0); |
| 296 | } |
| 297 | |
| 298 | /* Popup to create a new playlist ------------------------------------------- */ |
| 299 | |
| 300 | /** @brief New-playlist popup */ |
| 301 | static GtkWidget *playlist_new_window; |
| 302 | |
| 303 | /** @brief Text entry in new-playlist popup */ |
| 304 | static GtkWidget *playlist_new_entry; |
| 305 | |
| 306 | /** @brief Label for displaying feedback on what's wrong */ |
| 307 | static GtkWidget *playlist_new_info; |
| 308 | |
| 309 | /** @brief "Shared" radio button */ |
| 310 | static GtkWidget *playlist_new_shared; |
| 311 | |
| 312 | /** @brief "Public" radio button */ |
| 313 | static GtkWidget *playlist_new_public; |
| 314 | |
| 315 | /** @brief "Private" radio button */ |
| 316 | static GtkWidget *playlist_new_private; |
| 317 | |
| 318 | /** @brief Buttons for new-playlist popup */ |
| 319 | static struct button playlist_new_buttons[] = { |
| 320 | { |
| 321 | .stock = GTK_STOCK_OK, |
| 322 | .clicked = playlist_new_ok, |
| 323 | .tip = "Create new playlist" |
| 324 | }, |
| 325 | { |
| 326 | .stock = GTK_STOCK_CANCEL, |
| 327 | .clicked = playlist_new_cancel, |
| 328 | .tip = "Do not create new playlist" |
| 329 | } |
| 330 | }; |
| 331 | #define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons) |
| 332 | |
| 333 | /** @brief Pop up a new window to enter the playlist name and details */ |
| 334 | static void playlist_new_playlist(void) { |
| 335 | assert(playlist_new_window == NULL); |
| 336 | playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 337 | g_signal_connect(playlist_new_window, "destroy", |
| 338 | G_CALLBACK(gtk_widget_destroyed), &playlist_new_window); |
| 339 | gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist"); |
| 340 | /* Window will be modal, suppressing access to other windows */ |
| 341 | gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE); |
| 342 | gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window), |
| 343 | GTK_WINDOW(playlist_window)); |
| 344 | |
| 345 | /* Window contents will use a table (grid) layout */ |
| 346 | GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/); |
| 347 | |
| 348 | /* First row: playlist name */ |
| 349 | gtk_table_attach_defaults(GTK_TABLE(table), |
| 350 | gtk_label_new("Playlist name"), |
| 351 | 0, 1, 0, 1); |
| 352 | playlist_new_entry = gtk_entry_new(); |
| 353 | g_signal_connect(playlist_new_entry, "changed", |
| 354 | G_CALLBACK(playlist_new_entry_edited), NULL); |
| 355 | gtk_table_attach_defaults(GTK_TABLE(table), |
| 356 | playlist_new_entry, |
| 357 | 1, 3, 0, 1); |
| 358 | |
| 359 | /* Second row: radio buttons to choose type */ |
| 360 | playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared"); |
| 361 | playlist_new_public |
| 362 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), |
| 363 | "public"); |
| 364 | playlist_new_private |
| 365 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), |
| 366 | "private"); |
| 367 | g_signal_connect(playlist_new_shared, "toggled", |
| 368 | G_CALLBACK(playlist_new_button_toggled), NULL); |
| 369 | g_signal_connect(playlist_new_public, "toggled", |
| 370 | G_CALLBACK(playlist_new_button_toggled), NULL); |
| 371 | g_signal_connect(playlist_new_private, "toggled", |
| 372 | G_CALLBACK(playlist_new_button_toggled), NULL); |
| 373 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2); |
| 374 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2); |
| 375 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2); |
| 376 | |
| 377 | /* Third row: info bar saying why not */ |
| 378 | playlist_new_info = gtk_label_new(""); |
| 379 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info, |
| 380 | 0, 3, 2, 3); |
| 381 | |
| 382 | /* Fourth row: ok/cancel buttons */ |
| 383 | GtkWidget *hbox = create_buttons_box(playlist_new_buttons, |
| 384 | NPLAYLIST_NEW_BUTTONS, |
| 385 | gtk_hbox_new(FALSE, 0)); |
| 386 | gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4); |
| 387 | |
| 388 | gtk_container_add(GTK_CONTAINER(playlist_new_window), |
| 389 | frame_widget(table, NULL)); |
| 390 | |
| 391 | /* Set initial state of OK button */ |
| 392 | playlist_new_changed(0,0,0); |
| 393 | |
| 394 | g_signal_connect(playlist_new_window, "key-press-event", |
| 395 | G_CALLBACK(playlist_new_keypress), 0); |
| 396 | |
| 397 | /* Display the window */ |
| 398 | gtk_widget_show_all(playlist_new_window); |
| 399 | } |
| 400 | |
| 401 | /** @brief Keypress handler */ |
| 402 | static gboolean playlist_new_keypress(GtkWidget attribute((unused)) *widget, |
| 403 | GdkEventKey *event, |
| 404 | gpointer attribute((unused)) user_data) { |
| 405 | if(event->state) |
| 406 | return FALSE; |
| 407 | switch(event->keyval) { |
| 408 | case GDK_Return: |
| 409 | playlist_new_ok(NULL, NULL); |
| 410 | return TRUE; |
| 411 | case GDK_Escape: |
| 412 | gtk_widget_destroy(playlist_new_window); |
| 413 | return TRUE; |
| 414 | default: |
| 415 | return FALSE; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /** @brief Called when 'ok' is clicked in new-playlist popup */ |
| 420 | static void playlist_new_ok(GtkButton attribute((unused)) *button, |
| 421 | gpointer attribute((unused)) userdata) { |
| 422 | if(playlist_new_valid()) |
| 423 | return; |
| 424 | gboolean shared, public, private; |
| 425 | char *name, *fullname; |
| 426 | playlist_new_details(&name, &fullname, &shared, &public, &private); |
| 427 | |
| 428 | /* We need to: |
| 429 | * - lock the playlist |
| 430 | * - check it doesn't exist |
| 431 | * - set sharing (which will create it empty |
| 432 | * - unlock it |
| 433 | * |
| 434 | * TODO we should freeze the window while this is going on to stop a second |
| 435 | * click. |
| 436 | */ |
| 437 | disorder_eclient_playlist_lock(client, playlist_new_locked, fullname, |
| 438 | fullname); |
| 439 | } |
| 440 | |
| 441 | /** @brief Called when the proposed new playlist has been locked */ |
| 442 | static void playlist_new_locked(void *v, const char *err) { |
| 443 | char *fullname = v; |
| 444 | if(err) { |
| 445 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 446 | return; |
| 447 | } |
| 448 | disorder_eclient_playlist_get(client, playlist_new_retrieved, |
| 449 | fullname, fullname); |
| 450 | } |
| 451 | |
| 452 | /** @brief Called when the proposed new playlist's contents have been retrieved |
| 453 | * |
| 454 | * ...or rather, normally, when it's been reported that it does not exist. |
| 455 | */ |
| 456 | static void playlist_new_retrieved(void *v, const char *err, |
| 457 | int nvec, |
| 458 | char attribute((unused)) **vec) { |
| 459 | char *fullname = v; |
| 460 | if(!err && nvec != -1) |
| 461 | /* A rare case but not in principle impossible */ |
| 462 | err = "A playlist with that name already exists."; |
| 463 | if(err) { |
| 464 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 465 | disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname); |
| 466 | return; |
| 467 | } |
| 468 | gboolean shared, public, private; |
| 469 | playlist_new_details(0, 0, &shared, &public, &private); |
| 470 | disorder_eclient_playlist_set_share(client, playlist_new_created, fullname, |
| 471 | public ? "public" |
| 472 | : private ? "private" |
| 473 | : "shared", |
| 474 | fullname); |
| 475 | } |
| 476 | |
| 477 | /** @brief Called when the new playlist has been created */ |
| 478 | static void playlist_new_created(void attribute((unused)) *v, const char *err) { |
| 479 | if(err) { |
| 480 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 481 | return; |
| 482 | } |
| 483 | disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL); |
| 484 | // TODO arrange for the new playlist to be selected |
| 485 | } |
| 486 | |
| 487 | /** @brief Called when the newly created playlist has unlocked */ |
| 488 | static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) { |
| 489 | if(err) |
| 490 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 491 | /* Pop down the creation window */ |
| 492 | gtk_widget_destroy(playlist_new_window); |
| 493 | } |
| 494 | |
| 495 | /** @brief Called when 'cancel' is clicked in new-playlist popup */ |
| 496 | static void playlist_new_cancel(GtkButton attribute((unused)) *button, |
| 497 | gpointer attribute((unused)) userdata) { |
| 498 | gtk_widget_destroy(playlist_new_window); |
| 499 | } |
| 500 | |
| 501 | /** @brief Called when some radio button in the new-playlist popup changes */ |
| 502 | static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) *tb, |
| 503 | gpointer attribute((unused)) userdata) { |
| 504 | playlist_new_changed(0,0,0); |
| 505 | } |
| 506 | |
| 507 | /** @brief Called when the text entry field in the new-playlist popup changes */ |
| 508 | static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable, |
| 509 | gpointer attribute((unused)) user_data) { |
| 510 | playlist_new_changed(0,0,0); |
| 511 | } |
| 512 | |
| 513 | /** @brief Called to update new playlist window state |
| 514 | * |
| 515 | * This is called whenever one the text entry or radio buttons changed, and |
| 516 | * also when the set of known playlists changes. It determines whether the new |
| 517 | * playlist would be creatable and sets the sensitivity of the OK button |
| 518 | * and info display accordingly. |
| 519 | */ |
| 520 | static void playlist_new_changed(const char attribute((unused)) *event, |
| 521 | void attribute((unused)) *eventdata, |
| 522 | void attribute((unused)) *callbackdata) { |
| 523 | if(!playlist_new_window) |
| 524 | return; |
| 525 | const char *reason = playlist_new_valid(); |
| 526 | gtk_widget_set_sensitive(playlist_new_buttons[0].widget, |
| 527 | !reason); |
| 528 | gtk_label_set_text(GTK_LABEL(playlist_new_info), reason); |
| 529 | } |
| 530 | |
| 531 | /** @brief Test whether the new-playlist window settings are valid |
| 532 | * @return NULL on success or an error string if not |
| 533 | */ |
| 534 | static const char *playlist_new_valid(void) { |
| 535 | gboolean shared, public, private; |
| 536 | char *name, *fullname; |
| 537 | playlist_new_details(&name, &fullname, &shared, &public, &private); |
| 538 | if(!(shared || public || private)) |
| 539 | return "No type set."; |
| 540 | if(!*name) |
| 541 | return ""; |
| 542 | /* See if the result is valid */ |
| 543 | if(!valid_username(name) |
| 544 | || playlist_parse_name(fullname, NULL, NULL)) |
| 545 | return "Not a valid playlist name."; |
| 546 | /* See if the result clashes with an existing name. This is not a perfect |
| 547 | * check, the playlist might be created after this point but before we get a |
| 548 | * chance to disable the "OK" button. However when we try to create the |
| 549 | * playlist we will first try to retrieve it, with a lock held, so we |
| 550 | * shouldn't end up overwriting anything. */ |
| 551 | for(int n = 0; n < nplaylists; ++n) |
| 552 | if(!strcmp(playlists[n], fullname)) { |
| 553 | if(shared) |
| 554 | return "A shared playlist with that name already exists."; |
| 555 | else |
| 556 | return "You already have a playlist with that name."; |
| 557 | } |
| 558 | /* As far as we can tell creation would work */ |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | /** @brief Get entered new-playlist details |
| 563 | * @param namep Where to store entered name (or NULL) |
| 564 | * @param fullnamep Where to store computed full name (or NULL) |
| 565 | * @param sharep Where to store 'shared' flag (or NULL) |
| 566 | * @param publicp Where to store 'public' flag (or NULL) |
| 567 | * @param privatep Where to store 'private' flag (or NULL) |
| 568 | */ |
| 569 | static void playlist_new_details(char **namep, |
| 570 | char **fullnamep, |
| 571 | gboolean *sharedp, |
| 572 | gboolean *publicp, |
| 573 | gboolean *privatep) { |
| 574 | gboolean shared, public, private; |
| 575 | g_object_get(playlist_new_shared, "active", &shared, (char *)NULL); |
| 576 | g_object_get(playlist_new_public, "active", &public, (char *)NULL); |
| 577 | g_object_get(playlist_new_private, "active", &private, (char *)NULL); |
| 578 | char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry), |
| 579 | 0, -1); /* name owned by calle */ |
| 580 | char *name = xstrdup(gname); |
| 581 | g_free(gname); |
| 582 | if(sharedp) *sharedp = shared; |
| 583 | if(publicp) *publicp = public; |
| 584 | if(privatep) *privatep = private; |
| 585 | if(namep) *namep = name; |
| 586 | if(fullnamep) { |
| 587 | if(*sharedp) *fullnamep = *namep; |
| 588 | else byte_xasprintf(fullnamep, "%s.%s", config->username, name); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /* Playlist picker ---------------------------------------------------------- */ |
| 593 | |
| 594 | /** @brief Delete button */ |
| 595 | static GtkWidget *playlist_picker_delete_button; |
| 596 | |
| 597 | /** @brief Tree model for list of playlists */ |
| 598 | static GtkListStore *playlist_picker_list; |
| 599 | |
| 600 | /** @brief Selection for list of playlists */ |
| 601 | static GtkTreeSelection *playlist_picker_selection; |
| 602 | |
| 603 | /** @brief Currently selected playlist */ |
| 604 | static const char *playlist_picker_selected; |
| 605 | |
| 606 | /** @brief (Re-)populate the playlist picker tree model */ |
| 607 | static void playlist_picker_fill(const char attribute((unused)) *event, |
| 608 | void attribute((unused)) *eventdata, |
| 609 | void attribute((unused)) *callbackdata) { |
| 610 | GtkTreeIter iter[1]; |
| 611 | |
| 612 | if(!playlist_window) |
| 613 | return; |
| 614 | if(!playlist_picker_list) |
| 615 | playlist_picker_list = gtk_list_store_new(1, G_TYPE_STRING); |
| 616 | const char *was_selected = playlist_picker_selected; |
| 617 | gtk_list_store_clear(playlist_picker_list); /* clears playlists_selected */ |
| 618 | for(int n = 0; n < nplaylists; ++n) { |
| 619 | gtk_list_store_insert_with_values(playlist_picker_list, iter, |
| 620 | n /*position*/, |
| 621 | 0, playlists[n], /* column 0 */ |
| 622 | -1); /* no more cols */ |
| 623 | /* Reselect the selected playlist */ |
| 624 | if(was_selected && !strcmp(was_selected, playlists[n])) |
| 625 | gtk_tree_selection_select_iter(playlist_picker_selection, iter); |
| 626 | } |
| 627 | /* TODO deselecting then reselecting the current playlist resets the playlist |
| 628 | * editor, which trashes the user's selection. */ |
| 629 | } |
| 630 | |
| 631 | /** @brief Called when the selection might have changed */ |
| 632 | static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused)) *treeselection, |
| 633 | gpointer attribute((unused)) user_data) { |
| 634 | GtkTreeIter iter; |
| 635 | char *gselected, *selected; |
| 636 | |
| 637 | /* Identify the current selection */ |
| 638 | if(gtk_tree_selection_get_selected(playlist_picker_selection, 0, &iter)) { |
| 639 | gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), &iter, |
| 640 | 0, &gselected, -1); |
| 641 | selected = xstrdup(gselected); |
| 642 | g_free(gselected); |
| 643 | } else |
| 644 | selected = 0; |
| 645 | /* Set button sensitivity according to the new state */ |
| 646 | int deletable = FALSE; |
| 647 | if(selected) { |
| 648 | if(strchr(selected, '.')) { |
| 649 | if(!strncmp(selected, config->username, strlen(config->username))) |
| 650 | deletable = TRUE; |
| 651 | } else |
| 652 | deletable = TRUE; |
| 653 | } |
| 654 | gtk_widget_set_sensitive(playlist_picker_delete_button, deletable); |
| 655 | /* TODO delete should not be sensitive for public playlists owned by other |
| 656 | * users */ |
| 657 | /* Eliminate no-change cases */ |
| 658 | if(!selected && !playlist_picker_selected) |
| 659 | return; |
| 660 | if(selected |
| 661 | && playlist_picker_selected |
| 662 | && !strcmp(selected, playlist_picker_selected)) |
| 663 | return; |
| 664 | /* Record the new state */ |
| 665 | playlist_picker_selected = selected; |
| 666 | /* Re-initalize the queue */ |
| 667 | ql_new_queue(&ql_playlist, NULL); |
| 668 | /* Synthesize a playlist-modified to re-initialize the editor etc */ |
| 669 | event_raise("playlist-modified", (void *)playlist_picker_selected); |
| 670 | } |
| 671 | |
| 672 | /** @brief Called when the 'add' button is pressed */ |
| 673 | static void playlist_picker_add(GtkButton attribute((unused)) *button, |
| 674 | gpointer attribute((unused)) userdata) { |
| 675 | /* Unselect whatever is selected TODO why?? */ |
| 676 | gtk_tree_selection_unselect_all(playlist_picker_selection); |
| 677 | playlist_new_playlist(); |
| 678 | } |
| 679 | |
| 680 | /** @brief Called when playlist deletion completes */ |
| 681 | static void playlists_picker_delete_completed(void attribute((unused)) *v, |
| 682 | const char *err) { |
| 683 | if(err) |
| 684 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 685 | } |
| 686 | |
| 687 | /** @brief Called when the 'Delete' button is pressed */ |
| 688 | static void playlist_picker_delete(GtkButton attribute((unused)) *button, |
| 689 | gpointer attribute((unused)) userdata) { |
| 690 | GtkWidget *yesno; |
| 691 | int res; |
| 692 | |
| 693 | if(!playlist_picker_selected) |
| 694 | return; |
| 695 | yesno = gtk_message_dialog_new(GTK_WINDOW(playlist_window), |
| 696 | GTK_DIALOG_MODAL, |
| 697 | GTK_MESSAGE_QUESTION, |
| 698 | GTK_BUTTONS_YES_NO, |
| 699 | "Do you really want to delete playlist %s?" |
| 700 | " This action cannot be undone.", |
| 701 | playlist_picker_selected); |
| 702 | res = gtk_dialog_run(GTK_DIALOG(yesno)); |
| 703 | gtk_widget_destroy(yesno); |
| 704 | if(res == GTK_RESPONSE_YES) { |
| 705 | disorder_eclient_playlist_delete(client, |
| 706 | playlists_picker_delete_completed, |
| 707 | playlist_picker_selected, |
| 708 | NULL); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /** @brief Table of buttons below the playlist list */ |
| 713 | static struct button playlist_picker_buttons[] = { |
| 714 | { |
| 715 | GTK_STOCK_ADD, |
| 716 | playlist_picker_add, |
| 717 | "Create a new playlist", |
| 718 | 0 |
| 719 | }, |
| 720 | { |
| 721 | GTK_STOCK_REMOVE, |
| 722 | playlist_picker_delete, |
| 723 | "Delete a playlist", |
| 724 | 0 |
| 725 | }, |
| 726 | }; |
| 727 | #define NPLAYLIST_PICKER_BUTTONS (sizeof playlist_picker_buttons / sizeof *playlist_picker_buttons) |
| 728 | |
| 729 | /** @brief Create the list of playlists for the edit playlists window */ |
| 730 | static GtkWidget *playlist_picker_create(void) { |
| 731 | /* Create the list of playlist and populate it */ |
| 732 | playlist_picker_fill(NULL, NULL, NULL); |
| 733 | /* Create the tree view */ |
| 734 | GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlist_picker_list)); |
| 735 | /* ...and the renderers for it */ |
| 736 | GtkCellRenderer *cr = gtk_cell_renderer_text_new(); |
| 737 | GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist", |
| 738 | cr, |
| 739 | "text", 0, |
| 740 | NULL); |
| 741 | gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col); |
| 742 | /* Get the selection for the view; set its mode; arrange for a callback when |
| 743 | * it changes */ |
| 744 | playlist_picker_selected = NULL; |
| 745 | playlist_picker_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)); |
| 746 | gtk_tree_selection_set_mode(playlist_picker_selection, GTK_SELECTION_BROWSE); |
| 747 | g_signal_connect(playlist_picker_selection, "changed", |
| 748 | G_CALLBACK(playlist_picker_selection_changed), NULL); |
| 749 | |
| 750 | /* Create the control buttons */ |
| 751 | GtkWidget *buttons = create_buttons_box(playlist_picker_buttons, |
| 752 | NPLAYLIST_PICKER_BUTTONS, |
| 753 | gtk_hbox_new(FALSE, 1)); |
| 754 | playlist_picker_delete_button = playlist_picker_buttons[1].widget; |
| 755 | |
| 756 | playlist_picker_selection_changed(NULL, NULL); |
| 757 | |
| 758 | /* Buttons live below the list */ |
| 759 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); |
| 760 | gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0); |
| 761 | gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0); |
| 762 | |
| 763 | g_signal_connect(tree, "key-press-event", |
| 764 | G_CALLBACK(playlist_picker_keypress), 0); |
| 765 | |
| 766 | return vbox; |
| 767 | } |
| 768 | |
| 769 | static gboolean playlist_picker_keypress(GtkWidget attribute((unused)) *widget, |
| 770 | GdkEventKey *event, |
| 771 | gpointer attribute((unused)) user_data) { |
| 772 | if(event->state) |
| 773 | return FALSE; |
| 774 | switch(event->keyval) { |
| 775 | case GDK_BackSpace: |
| 776 | case GDK_Delete: |
| 777 | playlist_picker_delete(NULL, NULL); |
| 778 | return TRUE; |
| 779 | default: |
| 780 | return FALSE; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | static void playlist_picker_destroy(void) { |
| 785 | playlist_picker_delete_button = NULL; |
| 786 | g_object_unref(playlist_picker_list); |
| 787 | playlist_picker_list = NULL; |
| 788 | playlist_picker_selection = NULL; |
| 789 | playlist_picker_selected = NULL; |
| 790 | } |
| 791 | |
| 792 | /* Playlist editor ---------------------------------------------------------- */ |
| 793 | |
| 794 | static GtkWidget *playlist_editor_shared; |
| 795 | static GtkWidget *playlist_editor_public; |
| 796 | static GtkWidget *playlist_editor_private; |
| 797 | static int playlist_editor_setting_buttons; |
| 798 | |
| 799 | static GtkWidget *playlists_editor_create(void) { |
| 800 | assert(ql_playlist.view == NULL); /* better not be set up already */ |
| 801 | |
| 802 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
| 803 | playlist_editor_shared = gtk_radio_button_new_with_label(NULL, "shared"); |
| 804 | playlist_editor_public |
| 805 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared), |
| 806 | "public"); |
| 807 | playlist_editor_private |
| 808 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared), |
| 809 | "private"); |
| 810 | g_signal_connect(playlist_editor_public, "toggled", |
| 811 | G_CALLBACK(playlist_editor_button_toggled), |
| 812 | (void *)"public"); |
| 813 | g_signal_connect(playlist_editor_private, "toggled", |
| 814 | G_CALLBACK(playlist_editor_button_toggled), |
| 815 | (void *)"private"); |
| 816 | gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_shared, |
| 817 | FALSE/*expand*/, FALSE/*fill*/, 0); |
| 818 | gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_public, |
| 819 | FALSE/*expand*/, FALSE/*fill*/, 0); |
| 820 | gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_private, |
| 821 | FALSE/*expand*/, FALSE/*fill*/, 0); |
| 822 | playlist_editor_set_buttons(0,0,0); |
| 823 | |
| 824 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); |
| 825 | gtk_box_pack_start(GTK_BOX(vbox), init_queuelike(&ql_playlist), |
| 826 | TRUE/*expand*/, TRUE/*fill*/, 0); |
| 827 | gtk_box_pack_start(GTK_BOX(vbox), hbox, |
| 828 | FALSE/*expand*/, FALSE/*fill*/, 0); |
| 829 | return vbox; |
| 830 | } |
| 831 | |
| 832 | /** @brief Called when the public/private buttons are set */ |
| 833 | static void playlist_editor_button_toggled(GtkToggleButton *tb, |
| 834 | gpointer userdata) { |
| 835 | const char *state = userdata; |
| 836 | if(!gtk_toggle_button_get_active(tb) |
| 837 | || !playlist_picker_selected |
| 838 | || playlist_editor_setting_buttons) |
| 839 | return; |
| 840 | disorder_eclient_playlist_set_share(client, playlist_editor_share_set, |
| 841 | playlist_picker_selected, state, NULL); |
| 842 | } |
| 843 | |
| 844 | static void playlist_editor_share_set(void attribute((unused)) *v, |
| 845 | const attribute((unused)) char *err) { |
| 846 | if(err) |
| 847 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 848 | } |
| 849 | |
| 850 | /** @brief Set the editor button state and sensitivity */ |
| 851 | static void playlist_editor_set_buttons(const char attribute((unused)) *event, |
| 852 | void *eventdata, |
| 853 | void attribute((unused)) *callbackdata) { |
| 854 | /* If this event is for a non-selected playlist do nothing */ |
| 855 | if(eventdata |
| 856 | && playlist_picker_selected |
| 857 | && strcmp(eventdata, playlist_picker_selected)) |
| 858 | return; |
| 859 | if(playlist_picker_selected) { |
| 860 | if(strchr(playlist_picker_selected, '.')) |
| 861 | disorder_eclient_playlist_get_share(client, |
| 862 | playlist_editor_got_share, |
| 863 | playlist_picker_selected, |
| 864 | (void *)playlist_picker_selected); |
| 865 | else |
| 866 | playlist_editor_got_share((void *)playlist_picker_selected, NULL, |
| 867 | "shared"); |
| 868 | } else |
| 869 | playlist_editor_got_share(NULL, NULL, NULL); |
| 870 | } |
| 871 | |
| 872 | /** @brief Called with playlist sharing details */ |
| 873 | static void playlist_editor_got_share(void *v, |
| 874 | const char *err, |
| 875 | const char *value) { |
| 876 | const char *playlist = v; |
| 877 | if(err) { |
| 878 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 879 | value = NULL; |
| 880 | } |
| 881 | /* Set the currently active button */ |
| 882 | ++playlist_editor_setting_buttons; |
| 883 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_shared), |
| 884 | value && !strcmp(value, "shared")); |
| 885 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_public), |
| 886 | value && !strcmp(value, "public")); |
| 887 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_private), |
| 888 | value && !strcmp(value, "private")); |
| 889 | /* Set button sensitivity */ |
| 890 | gtk_widget_set_sensitive(playlist_editor_shared, FALSE); |
| 891 | int sensitive = (playlist |
| 892 | && strchr(playlist, '.') |
| 893 | && !strncmp(playlist, config->username, |
| 894 | strlen(config->username))); |
| 895 | gtk_widget_set_sensitive(playlist_editor_public, sensitive); |
| 896 | gtk_widget_set_sensitive(playlist_editor_private, sensitive); |
| 897 | --playlist_editor_setting_buttons; |
| 898 | } |
| 899 | |
| 900 | /** @brief (Re-)populate the playlist tree model */ |
| 901 | static void playlist_editor_fill(const char attribute((unused)) *event, |
| 902 | void *eventdata, |
| 903 | void attribute((unused)) *callbackdata) { |
| 904 | const char *modified_playlist = eventdata; |
| 905 | if(!playlist_window) |
| 906 | return; |
| 907 | if(!playlist_picker_selected) |
| 908 | return; |
| 909 | if(!strcmp(playlist_picker_selected, modified_playlist)) |
| 910 | disorder_eclient_playlist_get(client, playlists_editor_received_tracks, |
| 911 | playlist_picker_selected, |
| 912 | (void *)playlist_picker_selected); |
| 913 | } |
| 914 | |
| 915 | /** @brief Called with new tracks for the playlist */ |
| 916 | static void playlists_editor_received_tracks(void *v, |
| 917 | const char *err, |
| 918 | int nvec, char **vec) { |
| 919 | const char *playlist = v; |
| 920 | if(err) { |
| 921 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 922 | return; |
| 923 | } |
| 924 | if(!playlist_picker_selected |
| 925 | || strcmp(playlist, playlist_picker_selected)) { |
| 926 | /* The tracks are for the wrong playlist - something must have changed |
| 927 | * while the fetch command was in flight. We just ignore this callback, |
| 928 | * the right answer will be requested and arrive in due course. */ |
| 929 | return; |
| 930 | } |
| 931 | if(nvec == -1) |
| 932 | /* No such playlist, presumably we'll get a deleted event shortly */ |
| 933 | return; |
| 934 | /* Translate the list of tracks into queue entries */ |
| 935 | struct queue_entry *newq, **qq = &newq, *qprev = NULL; |
| 936 | hash *h = hash_new(sizeof(int)); |
| 937 | for(int n = 0; n < nvec; ++n) { |
| 938 | struct queue_entry *q = xmalloc(sizeof *q); |
| 939 | q->prev = qprev; |
| 940 | q->track = vec[n]; |
| 941 | /* Synthesize a unique ID so that the selection survives updates. Tracks |
| 942 | * can appear more than once in the queue so we can't use raw track names, |
| 943 | * so we add a serial number to the start. */ |
| 944 | int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0; |
| 945 | byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]); |
| 946 | hash_add(h, vec[n], &serial, HASH_INSERT_OR_REPLACE); |
| 947 | *qq = q; |
| 948 | qq = &q->next; |
| 949 | qprev = q; |
| 950 | } |
| 951 | *qq = NULL; |
| 952 | ql_new_queue(&ql_playlist, newq); |
| 953 | } |
| 954 | |
| 955 | /* Playlist mutation -------------------------------------------------------- */ |
| 956 | |
| 957 | /** @brief State structure for guarded playlist modification |
| 958 | * |
| 959 | * To safely move, insert or delete rows we must: |
| 960 | * - take a lock |
| 961 | * - fetch the playlist |
| 962 | * - verify it's not changed |
| 963 | * - update the playlist contents |
| 964 | * - store the playlist |
| 965 | * - release the lock |
| 966 | * |
| 967 | * The playlist_modify_ functions do just that. |
| 968 | * |
| 969 | * To kick things off create one of these and disorder_eclient_playlist_lock() |
| 970 | * with playlist_modify_locked() as its callback. @c modify will be called; it |
| 971 | * should disorder_eclient_playlist_set() to set the new state with |
| 972 | * playlist_modify_updated() as its callback. |
| 973 | */ |
| 974 | struct playlist_modify_data { |
| 975 | /** @brief Affected playlist */ |
| 976 | const char *playlist; |
| 977 | /** @brief Modification function |
| 978 | * @param mod Pointer back to state structure |
| 979 | * @param ntracks Length of playlist |
| 980 | * @param tracks Tracks in playlist |
| 981 | */ |
| 982 | void (*modify)(struct playlist_modify_data *mod, |
| 983 | int ntracks, char **tracks); |
| 984 | |
| 985 | /** @brief Number of tracks dropped */ |
| 986 | int ntracks; |
| 987 | /** @brief Track names dropped */ |
| 988 | char **tracks; |
| 989 | /** @brief Track IDs dropped */ |
| 990 | char **ids; |
| 991 | /** @brief Drop after this point */ |
| 992 | struct queue_entry *after_me; |
| 993 | }; |
| 994 | |
| 995 | /** @brief Called with playlist locked |
| 996 | * |
| 997 | * This is the entry point for guarded modification ising @ref |
| 998 | * playlist_modify_data. |
| 999 | */ |
| 1000 | static void playlist_modify_locked(void *v, const char *err) { |
| 1001 | struct playlist_modify_data *mod = v; |
| 1002 | if(err) { |
| 1003 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 1004 | return; |
| 1005 | } |
| 1006 | disorder_eclient_playlist_get(client, playlist_modify_retrieved, |
| 1007 | mod->playlist, mod); |
| 1008 | } |
| 1009 | |
| 1010 | /** @brief Called with current playlist contents |
| 1011 | * Checks that the playlist is still current and has not changed. |
| 1012 | */ |
| 1013 | void playlist_modify_retrieved(void *v, const char *err, |
| 1014 | int nvec, |
| 1015 | char **vec) { |
| 1016 | struct playlist_modify_data *mod = v; |
| 1017 | if(err) { |
| 1018 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 1019 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); |
| 1020 | return; |
| 1021 | } |
| 1022 | if(nvec < 0 |
| 1023 | || !playlist_picker_selected |
| 1024 | || strcmp(mod->playlist, playlist_picker_selected)) { |
| 1025 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); |
| 1026 | return; |
| 1027 | } |
| 1028 | /* We check that the contents haven't changed. If they have we just abandon |
| 1029 | * the operation. The user will have to try again. */ |
| 1030 | struct queue_entry *q; |
| 1031 | int n; |
| 1032 | for(n = 0, q = ql_playlist.q; q && n < nvec; ++n, q = q->next) |
| 1033 | if(strcmp(q->track, vec[n])) |
| 1034 | break; |
| 1035 | if(n != nvec || q != NULL) { |
| 1036 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); |
| 1037 | return; |
| 1038 | } |
| 1039 | mod->modify(mod, nvec, vec); |
| 1040 | } |
| 1041 | |
| 1042 | /** @brief Called when the playlist has been updated */ |
| 1043 | static void playlist_modify_updated(void attribute((unused)) *v, |
| 1044 | const char *err) { |
| 1045 | if(err) |
| 1046 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 1047 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); |
| 1048 | } |
| 1049 | |
| 1050 | /** @brief Called when the playlist has been unlocked */ |
| 1051 | static void playlist_modify_unlocked(void attribute((unused)) *v, |
| 1052 | const char *err) { |
| 1053 | if(err) |
| 1054 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
| 1055 | } |
| 1056 | |
| 1057 | /* Drop tracks into a playlist ---------------------------------------------- */ |
| 1058 | |
| 1059 | static void playlist_drop(struct queuelike attribute((unused)) *ql, |
| 1060 | int ntracks, |
| 1061 | char **tracks, char **ids, |
| 1062 | struct queue_entry *after_me) { |
| 1063 | struct playlist_modify_data *mod = xmalloc(sizeof *mod); |
| 1064 | |
| 1065 | mod->playlist = playlist_picker_selected; |
| 1066 | mod->modify = playlist_drop_modify; |
| 1067 | mod->ntracks = ntracks; |
| 1068 | mod->tracks = tracks; |
| 1069 | mod->ids = ids; |
| 1070 | mod->after_me = after_me; |
| 1071 | disorder_eclient_playlist_lock(client, playlist_modify_locked, |
| 1072 | mod->playlist, mod); |
| 1073 | } |
| 1074 | |
| 1075 | /** @brief Return true if track @p i is in the moved set */ |
| 1076 | static int playlist_drop_is_moved(struct playlist_modify_data *mod, |
| 1077 | int i) { |
| 1078 | struct queue_entry *q; |
| 1079 | |
| 1080 | /* Find the q corresponding to i, so we can get the ID */ |
| 1081 | for(q = ql_playlist.q; i; q = q->next, --i) |
| 1082 | ; |
| 1083 | /* See if track i matches any of the moved set by ID */ |
| 1084 | for(int n = 0; n < mod->ntracks; ++n) |
| 1085 | if(!strcmp(q->id, mod->ids[n])) |
| 1086 | return 1; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | static void playlist_drop_modify(struct playlist_modify_data *mod, |
| 1091 | int nvec, char **vec) { |
| 1092 | char **newvec; |
| 1093 | int nnewvec; |
| 1094 | |
| 1095 | //fprintf(stderr, "\nplaylist_drop_modify\n"); |
| 1096 | /* after_me is the queue_entry to insert after, or NULL to insert at the |
| 1097 | * beginning (including the case when the playlist is empty) */ |
| 1098 | //fprintf(stderr, "after_me = %s\n", |
| 1099 | // mod->after_me ? mod->after_me->track : "NULL"); |
| 1100 | struct queue_entry *q = ql_playlist.q; |
| 1101 | int ins = 0; |
| 1102 | if(mod->after_me) { |
| 1103 | ++ins; |
| 1104 | while(q && q != mod->after_me) { |
| 1105 | q = q->next; |
| 1106 | ++ins; |
| 1107 | } |
| 1108 | } |
| 1109 | /* Now ins is the index to insert at; equivalently, the row to insert before, |
| 1110 | * and so equal to nvec to append. */ |
| 1111 | #if 0 |
| 1112 | fprintf(stderr, "ins = %d = %s\n", |
| 1113 | ins, ins < nvec ? vec[ins] : "NULL"); |
| 1114 | for(int n = 0; n < nvec; ++n) |
| 1115 | fprintf(stderr, "%d: %s %s\n", n, n == ins ? "->" : " ", vec[n]); |
| 1116 | fprintf(stderr, "nvec = %d\n", nvec); |
| 1117 | #endif |
| 1118 | if(mod->ids) { |
| 1119 | /* This is a rearrangement */ |
| 1120 | /* We have: |
| 1121 | * - vec[], the current layout |
| 1122 | * - ins, pointing into vec |
| 1123 | * - mod->tracks[], a subset of vec[] which is to be moved |
| 1124 | * |
| 1125 | * ins is the insertion point BUT it is in terms of the whole |
| 1126 | * array, i.e. before mod->tracks[] have been removed. The first |
| 1127 | * step then is to remove everything in mod->tracks[] and adjust |
| 1128 | * ins downwards as necessary. |
| 1129 | */ |
| 1130 | /* First zero out anything that's moved */ |
| 1131 | int before_ins = 0; |
| 1132 | for(int n = 0; n < nvec; ++n) { |
| 1133 | if(playlist_drop_is_moved(mod, n)) { |
| 1134 | vec[n] = NULL; |
| 1135 | if(n < ins) |
| 1136 | ++before_ins; |
| 1137 | } |
| 1138 | } |
| 1139 | /* Now collapse down the array */ |
| 1140 | int i = 0; |
| 1141 | for(int n = 0; n < nvec; ++n) { |
| 1142 | if(vec[n]) |
| 1143 | vec[i++] = vec[n]; |
| 1144 | } |
| 1145 | assert(i + mod->ntracks == nvec); |
| 1146 | nvec = i; |
| 1147 | /* Adjust the insertion point to take account of things moved from before |
| 1148 | * it */ |
| 1149 | ins -= before_ins; |
| 1150 | /* The effect is now the same as an insertion */ |
| 1151 | } |
| 1152 | /* This is (now) an insertion */ |
| 1153 | nnewvec = nvec + mod->ntracks; |
| 1154 | newvec = xcalloc(nnewvec, sizeof (char *)); |
| 1155 | memcpy(newvec, vec, |
| 1156 | ins * sizeof (char *)); |
| 1157 | memcpy(newvec + ins, mod->tracks, |
| 1158 | mod->ntracks * sizeof (char *)); |
| 1159 | memcpy(newvec + ins + mod->ntracks, vec + ins, |
| 1160 | (nvec - ins) * sizeof (char *)); |
| 1161 | disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist, |
| 1162 | newvec, nnewvec, mod); |
| 1163 | } |
| 1164 | |
| 1165 | /* Playlist editor right-click menu ---------------------------------------- */ |
| 1166 | |
| 1167 | /** @brief Called to determine whether the playlist is playable */ |
| 1168 | static int playlist_playall_sensitive(void attribute((unused)) *extra) { |
| 1169 | /* If there's no playlist obviously we can't play it */ |
| 1170 | if(!playlist_picker_selected) |
| 1171 | return FALSE; |
| 1172 | /* If it's empty we can't play it */ |
| 1173 | if(!ql_playlist.q) |
| 1174 | return FALSE; |
| 1175 | /* Otherwise we can */ |
| 1176 | return TRUE; |
| 1177 | } |
| 1178 | |
| 1179 | /** @brief Called to play the selected playlist */ |
| 1180 | static void playlist_playall_activate(GtkMenuItem attribute((unused)) *menuitem, |
| 1181 | gpointer attribute((unused)) user_data) { |
| 1182 | if(!playlist_picker_selected) |
| 1183 | return; |
| 1184 | /* Re-use the menu-based activation callback */ |
| 1185 | disorder_eclient_playlist_get(client, playlist_menu_received_content, |
| 1186 | playlist_picker_selected, NULL); |
| 1187 | } |
| 1188 | |
| 1189 | /** @brief Called to determine whether the playlist is playable */ |
| 1190 | static int playlist_remove_sensitive(void attribute((unused)) *extra) { |
| 1191 | /* If there's no playlist obviously we can't remove from it */ |
| 1192 | if(!playlist_picker_selected) |
| 1193 | return FALSE; |
| 1194 | /* If no tracks are selected we cannot remove them */ |
| 1195 | if(!gtk_tree_selection_count_selected_rows(ql_playlist.selection)) |
| 1196 | return FALSE; |
| 1197 | /* We're good to go */ |
| 1198 | return TRUE; |
| 1199 | } |
| 1200 | |
| 1201 | /** @brief Called to remove the selected playlist */ |
| 1202 | static void playlist_remove_activate(GtkMenuItem attribute((unused)) *menuitem, |
| 1203 | gpointer attribute((unused)) user_data) { |
| 1204 | if(!playlist_picker_selected) |
| 1205 | return; |
| 1206 | struct playlist_modify_data *mod = xmalloc(sizeof *mod); |
| 1207 | |
| 1208 | mod->playlist = playlist_picker_selected; |
| 1209 | mod->modify = playlist_remove_modify; |
| 1210 | disorder_eclient_playlist_lock(client, playlist_modify_locked, |
| 1211 | mod->playlist, mod); |
| 1212 | } |
| 1213 | |
| 1214 | static void playlist_remove_modify(struct playlist_modify_data *mod, |
| 1215 | int attribute((unused)) nvec, char **vec) { |
| 1216 | GtkTreeIter iter[1]; |
| 1217 | gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql_playlist.store), |
| 1218 | iter); |
| 1219 | int n = 0, m = 0; |
| 1220 | while(it) { |
| 1221 | if(!gtk_tree_selection_iter_is_selected(ql_playlist.selection, iter)) |
| 1222 | vec[m++] = vec[n++]; |
| 1223 | else |
| 1224 | n++; |
| 1225 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_playlist.store), iter); |
| 1226 | } |
| 1227 | disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist, |
| 1228 | vec, m, mod); |
| 1229 | } |
| 1230 | |
| 1231 | /* Playlists window --------------------------------------------------------- */ |
| 1232 | |
| 1233 | /** @brief Pop up the playlists window |
| 1234 | * |
| 1235 | * Called when the playlists menu item is selected |
| 1236 | */ |
| 1237 | void playlist_window_create(gpointer attribute((unused)) callback_data, |
| 1238 | guint attribute((unused)) callback_action, |
| 1239 | GtkWidget attribute((unused)) *menu_item) { |
| 1240 | /* If the window already exists, raise it */ |
| 1241 | if(playlist_window) { |
| 1242 | gtk_window_present(GTK_WINDOW(playlist_window)); |
| 1243 | return; |
| 1244 | } |
| 1245 | /* Create the window */ |
| 1246 | playlist_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 1247 | gtk_widget_set_style(playlist_window, tool_style); |
| 1248 | g_signal_connect(playlist_window, "destroy", |
| 1249 | G_CALLBACK(playlist_window_destroyed), &playlist_window); |
| 1250 | gtk_window_set_title(GTK_WINDOW(playlist_window), "Playlists Management"); |
| 1251 | /* TODO loads of this is very similar to (copied from!) users.c - can we |
| 1252 | * de-dupe? */ |
| 1253 | /* Keyboard shortcuts */ |
| 1254 | g_signal_connect(playlist_window, "key-press-event", |
| 1255 | G_CALLBACK(playlist_window_keypress), 0); |
| 1256 | /* default size is too small */ |
| 1257 | gtk_window_set_default_size(GTK_WINDOW(playlist_window), 640, 320); |
| 1258 | |
| 1259 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
| 1260 | gtk_box_pack_start(GTK_BOX(hbox), playlist_picker_create(), |
| 1261 | FALSE/*expand*/, FALSE, 0); |
| 1262 | gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), |
| 1263 | FALSE/*expand*/, FALSE, 2); |
| 1264 | gtk_box_pack_start(GTK_BOX(hbox), playlists_editor_create(), |
| 1265 | TRUE/*expand*/, TRUE/*fill*/, 0); |
| 1266 | |
| 1267 | gtk_container_add(GTK_CONTAINER(playlist_window), frame_widget(hbox, NULL)); |
| 1268 | gtk_widget_show_all(playlist_window); |
| 1269 | } |
| 1270 | |
| 1271 | /** @brief Keypress handler */ |
| 1272 | static gboolean playlist_window_keypress(GtkWidget attribute((unused)) *widget, |
| 1273 | GdkEventKey *event, |
| 1274 | gpointer attribute((unused)) user_data) { |
| 1275 | if(event->state) |
| 1276 | return FALSE; |
| 1277 | switch(event->keyval) { |
| 1278 | case GDK_Escape: |
| 1279 | gtk_widget_destroy(playlist_window); |
| 1280 | return TRUE; |
| 1281 | default: |
| 1282 | return FALSE; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | /** @brief Called when the playlist window is destroyed */ |
| 1287 | static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget, |
| 1288 | GtkWidget **widget_pointer) { |
| 1289 | destroy_queuelike(&ql_playlist); |
| 1290 | playlist_picker_destroy(); |
| 1291 | *widget_pointer = NULL; |
| 1292 | } |
| 1293 | |
| 1294 | /** @brief Initialize playlist support */ |
| 1295 | void playlists_init(void) { |
| 1296 | /* We re-get all playlists upon any change... */ |
| 1297 | event_register("playlist-created", playlist_list_update, 0); |
| 1298 | event_register("playlist-deleted", playlist_list_update, 0); |
| 1299 | /* ...and on reconnection */ |
| 1300 | event_register("log-connected", playlist_list_update, 0); |
| 1301 | /* ...and from time to time */ |
| 1302 | event_register("periodic-slow", playlist_list_update, 0); |
| 1303 | /* ...and at startup */ |
| 1304 | playlist_list_update(0, 0, 0); |
| 1305 | |
| 1306 | /* Update the playlists menu when the set of playlists changes */ |
| 1307 | event_register("playlists-updated", playlist_menu_changed, 0); |
| 1308 | /* Update the new-playlist OK button when the set of playlists changes */ |
| 1309 | event_register("playlists-updated", playlist_new_changed, 0); |
| 1310 | /* Update the list of playlists in the edit window when the set changes */ |
| 1311 | event_register("playlists-updated", playlist_picker_fill, 0); |
| 1312 | /* Update the displayed playlist when it is modified */ |
| 1313 | event_register("playlist-modified", playlist_editor_fill, 0); |
| 1314 | /* Update the shared/public/etc buttons when a playlist is modified */ |
| 1315 | event_register("playlist-modified", playlist_editor_set_buttons, 0); |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | Local Variables: |
| 1320 | c-basic-offset:2 |
| 1321 | comment-column:40 |
| 1322 | fill-column:79 |
| 1323 | indent-tabs-mode:nil |
| 1324 | End: |
| 1325 | */ |