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