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