chiark / gitweb /
leave a TODO
[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
0b0fb26b
RK
43#if PLAYLISTS
44
b5e60f0d
RK
45static void playlist_list_received_playlists(void *v,
46 const char *err,
47 int nvec, char **vec);
7fe2807d
RK
48static void playlist_editor_fill(const char *event,
49 void *eventdata,
50 void *callbackdata);
fc36ecb7 51
f0bd437a 52/** @brief Playlist editing window */
7fe2807d 53static GtkWidget *playlist_window;
f0bd437a 54
6bfaa2a1
RK
55/** @brief Columns for the playlist editor */
56static const struct queue_column playlist_columns[] = {
57 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
58 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
59 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
60 { "Length", column_length, 0, COL_RIGHT }
61};
62
63/** @brief Pop-up menu for playlist editor */
64// TODO some of these may not be generic enough yet - check!
65static struct menuitem playlist_menuitems[] = {
66 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
67 { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 },
68 //{ "Play playlist", ql_playall_activate, ql_playall_sensitive, 0, 0 },
69 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
70 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
71 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
72};
73
74/** @brief Queuelike for editing a playlist */
75static struct queuelike ql_playlist = {
76 .name = "playlist",
77 .columns = playlist_columns,
78 .ncolumns = sizeof playlist_columns / sizeof *playlist_columns,
79 .menuitems = playlist_menuitems,
80 .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems,
81 //.drop = playlist_drop //TODO
82};
83
3c1a4e96 84/* Maintaining the list of playlists ---------------------------------------- */
121944d1 85
b5e60f0d
RK
86/** @brief Current list of playlists or NULL */
87char **playlists;
88
89/** @brief Count of playlists */
90int nplaylists;
91
92/** @brief Schedule an update to the list of playlists
93 *
94 * Called periodically and when a playlist is created or deleted.
95 */
96static void playlist_list_update(const char attribute((unused)) *event,
97 void attribute((unused)) *eventdata,
98 void attribute((unused)) *callbackdata) {
99 disorder_eclient_playlists(client, playlist_list_received_playlists, 0);
fc36ecb7
RK
100}
101
102/** @brief qsort() callback for playlist name comparison */
103static int playlistcmp(const void *ap, const void *bp) {
104 const char *a = *(char **)ap, *b = *(char **)bp;
105 const char *ad = strchr(a, '.'), *bd = strchr(b, '.');
106 int c;
107
108 /* Group owned playlists by owner */
109 if(ad && bd) {
110 const int adn = ad - a, bdn = bd - b;
111 if((c = strncmp(a, b, adn < bdn ? adn : bdn)))
112 return c;
113 /* Lexical order within playlists of a single owner */
114 return strcmp(ad + 1, bd + 1);
115 }
116
117 /* Owned playlists after shared ones */
118 if(ad) {
119 return 1;
120 } else if(bd) {
121 return -1;
122 }
123
124 /* Lexical order of shared playlists */
125 return strcmp(a, b);
126}
127
128/** @brief Called with a new list of playlists */
b5e60f0d
RK
129static void playlist_list_received_playlists(void attribute((unused)) *v,
130 const char *err,
131 int nvec, char **vec) {
fc36ecb7
RK
132 if(err) {
133 playlists = 0;
134 nplaylists = -1;
135 /* Probably means server does not support playlists */
136 } else {
137 playlists = vec;
138 nplaylists = nvec;
139 qsort(playlists, nplaylists, sizeof (char *), playlistcmp);
140 }
141 /* Tell our consumers */
142 event_raise("playlists-updated", 0);
143}
144
121944d1
RK
145/* Playlists menu ----------------------------------------------------------- */
146
147/** @brief Play received playlist contents
148 *
149 * Passed as a completion callback by menu_activate_playlist().
150 */
b5e60f0d
RK
151static void playlist_menu_received_content(void attribute((unused)) *v,
152 const char *err,
153 int nvec, char **vec) {
121944d1
RK
154 if(err) {
155 popup_protocol_error(0, err);
156 return;
157 }
158 for(int n = 0; n < nvec; ++n)
159 disorder_eclient_play(client, vec[n], NULL, NULL);
160}
161
162/** @brief Called to activate a playlist
163 *
164 * Called when the menu item for a playlist is clicked.
165 */
b5e60f0d 166static void playlist_menu_activate(GtkMenuItem *menuitem,
f9b20469
RK
167 gpointer attribute((unused)) user_data) {
168 GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
169 const char *playlist = gtk_label_get_text(label);
170
b5e60f0d
RK
171 disorder_eclient_playlist_get(client, playlist_menu_received_content,
172 playlist, NULL);
f9b20469
RK
173}
174
b5e60f0d
RK
175/** @brief Called when the playlists change
176 *
177 * Naively refills the menu. The results might be unsettling if the menu is
178 * currently open, but this is hopefuly fairly rare.
179 */
180static void playlist_menu_changed(const char attribute((unused)) *event,
181 void attribute((unused)) *eventdata,
182 void attribute((unused)) *callbackdata) {
f9b20469
RK
183 if(!playlists_menu)
184 return; /* OMG too soon */
185 GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
f9b20469
RK
186 while(menu->children)
187 gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
188 /* NB nplaylists can be -1 as well as 0 */
189 for(int n = 0; n < nplaylists; ++n) {
190 GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]);
b5e60f0d 191 g_signal_connect(w, "activate", G_CALLBACK(playlist_menu_activate), 0);
f9b20469
RK
192 gtk_widget_show(w);
193 gtk_menu_shell_append(menu, w);
194 }
fdea9f40 195 gtk_widget_set_sensitive(menu_playlists_widget,
f9b20469 196 nplaylists > 0);
fdea9f40 197 gtk_widget_set_sensitive(menu_editplaylists_widget,
6acdbba4 198 nplaylists >= 0);
f9b20469
RK
199}
200
7f7c3819
RK
201/* Popup to create a new playlist ------------------------------------------- */
202
203/** @brief New-playlist popup */
204static GtkWidget *playlist_new_window;
205
206/** @brief Text entry in new-playlist popup */
207static GtkWidget *playlist_new_entry;
208
b5e60f0d 209/** @brief Label for displaying feedback on what's wrong */
7f7c3819
RK
210static GtkWidget *playlist_new_info;
211
b5e60f0d 212/** @brief "Shared" radio button */
7f7c3819 213static GtkWidget *playlist_new_shared;
b5e60f0d
RK
214
215/** @brief "Public" radio button */
7f7c3819 216static GtkWidget *playlist_new_public;
b5e60f0d
RK
217
218/** @brief "Private" radio button */
7f7c3819
RK
219static GtkWidget *playlist_new_private;
220
b5e60f0d
RK
221/** @brief Get entered new-playlist details
222 * @param namep Where to store entered name (or NULL)
223 * @param fullnamep Where to store computed full name (or NULL)
224 * @param sharep Where to store 'shared' flag (or NULL)
225 * @param publicp Where to store 'public' flag (or NULL)
226 * @param privatep Where to store 'private' flag (or NULL)
227 */
7c12e4bd
RK
228static void playlist_new_details(char **namep,
229 char **fullnamep,
230 gboolean *sharedp,
231 gboolean *publicp,
232 gboolean *privatep) {
233 gboolean shared, public, private;
234 g_object_get(playlist_new_shared, "active", &shared, (char *)NULL);
235 g_object_get(playlist_new_public, "active", &public, (char *)NULL);
236 g_object_get(playlist_new_private, "active", &private, (char *)NULL);
237 char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry),
238 0, -1); /* name owned by calle */
239 char *name = xstrdup(gname);
240 g_free(gname);
241 if(sharedp) *sharedp = shared;
242 if(publicp) *publicp = public;
243 if(privatep) *privatep = private;
244 if(namep) *namep = name;
245 if(fullnamep) {
246 if(*sharedp) *fullnamep = *namep;
247 else byte_xasprintf(fullnamep, "%s.%s", config->username, name);
248 }
249}
250
251/** @brief Called when the newly created playlist has unlocked */
252static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) {
253 if(err)
254 popup_protocol_error(0, err);
b5e60f0d 255 /* Pop down the creation window */
7c12e4bd
RK
256 gtk_widget_destroy(playlist_new_window);
257}
258
259/** @brief Called when the new playlist has been created */
260static void playlist_new_created(void attribute((unused)) *v, const char *err) {
261 if(err) {
262 popup_protocol_error(0, err);
263 return;
264 }
265 disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL);
266 // TODO arrange for the new playlist to be selected
267}
268
269/** @brief Called when the proposed new playlist's contents have been retrieved
270 *
271 * ...or rather, normally, when it's been reported that it does not exist.
272 */
273static void playlist_new_retrieved(void *v, const char *err,
274 int nvec,
275 char attribute((unused)) **vec) {
276 char *fullname = v;
277 if(!err && nvec != -1)
278 /* A rare case but not in principle impossible */
279 err = "A playlist with that name already exists.";
280 if(err) {
281 popup_protocol_error(0, err);
282 disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname);
283 return;
284 }
285 gboolean shared, public, private;
286 playlist_new_details(0, 0, &shared, &public, &private);
287 disorder_eclient_playlist_set_share(client, playlist_new_created, fullname,
288 public ? "public"
289 : private ? "private"
290 : "shared",
291 fullname);
292}
293
294/** @brief Called when the proposed new playlist has been locked */
295static void playlist_new_locked(void *v, const char *err) {
296 char *fullname = v;
297 if(err) {
298 popup_protocol_error(0, err);
299 return;
300 }
301 disorder_eclient_playlist_get(client, playlist_new_retrieved,
302 fullname, fullname);
303}
304
7f7c3819
RK
305/** @brief Called when 'ok' is clicked in new-playlist popup */
306static void playlist_new_ok(GtkButton attribute((unused)) *button,
307 gpointer attribute((unused)) userdata) {
7c12e4bd
RK
308 gboolean shared, public, private;
309 char *name, *fullname;
310 playlist_new_details(&name, &fullname, &shared, &public, &private);
311
312 /* We need to:
313 * - lock the playlist
314 * - check it doesn't exist
315 * - set sharing (which will create it empty
316 * - unlock it
317 *
b5e60f0d
RK
318 * TODO we should freeze the window while this is going on to stop a second
319 * click.
7c12e4bd
RK
320 */
321 disorder_eclient_playlist_lock(client, playlist_new_locked, fullname,
322 fullname);
7f7c3819
RK
323}
324
325/** @brief Called when 'cancel' is clicked in new-playlist popup */
326static void playlist_new_cancel(GtkButton attribute((unused)) *button,
327 gpointer attribute((unused)) userdata) {
328 gtk_widget_destroy(playlist_new_window);
329}
330
331/** @brief Buttons for new-playlist popup */
332static struct button playlist_new_buttons[] = {
333 {
334 .stock = GTK_STOCK_OK,
335 .clicked = playlist_new_ok,
336 .tip = "Create new playlist"
337 },
338 {
339 .stock = GTK_STOCK_CANCEL,
340 .clicked = playlist_new_cancel,
7c12e4bd 341 .tip = "Do not create new playlist"
7f7c3819
RK
342 }
343};
344#define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons)
345
7c12e4bd
RK
346/** @brief Test whether the new-playlist window settings are valid
347 * @return NULL on success or an error string if not
348 */
7f7c3819
RK
349static const char *playlist_new_valid(void) {
350 gboolean shared, public, private;
7c12e4bd
RK
351 char *name, *fullname;
352 playlist_new_details(&name, &fullname, &shared, &public, &private);
7f7c3819
RK
353 if(!(shared || public || private))
354 return "No type set.";
7f7c3819
RK
355 if(!*name)
356 return "";
7f7c3819 357 /* See if the result is valid */
7c12e4bd
RK
358 if(!valid_username(name)
359 || playlist_parse_name(fullname, NULL, NULL))
7f7c3819 360 return "Not a valid playlist name.";
b5e60f0d
RK
361 /* See if the result clashes with an existing name. This is not a perfect
362 * check, the playlist might be created after this point but before we get a
363 * chance to disable the "OK" button. However when we try to create the
364 * playlist we will first try to retrieve it, with a lock held, so we
365 * shouldn't end up overwriting anything. */
7f7c3819
RK
366 for(int n = 0; n < nplaylists; ++n)
367 if(!strcmp(playlists[n], fullname)) {
368 if(shared)
369 return "A shared playlist with that name already exists.";
370 else
371 return "You already have a playlist with that name.";
372 }
373 /* As far as we can tell creation would work */
374 return NULL;
375}
376
7c12e4bd 377/** @brief Called to update new playlist window state
7f7c3819 378 *
7c12e4bd
RK
379 * This is called whenever one the text entry or radio buttons changed, and
380 * also when the set of known playlists changes. It determines whether the new
381 * playlist would be creatable and sets the sensitivity of the OK button
382 * and info display accordingly.
7f7c3819 383 */
7c12e4bd
RK
384static void playlist_new_changed(const char attribute((unused)) *event,
385 void attribute((unused)) *eventdata,
386 void attribute((unused)) *callbackdata) {
387 if(!playlist_new_window)
388 return;
7f7c3819
RK
389 const char *reason = playlist_new_valid();
390 gtk_widget_set_sensitive(playlist_new_buttons[0].widget,
391 !reason);
392 gtk_label_set_text(GTK_LABEL(playlist_new_info), reason);
393}
394
395/** @brief Called when some radio button in the new-playlist popup changes */
396static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) tb,
397 gpointer attribute((unused)) userdata) {
7c12e4bd 398 playlist_new_changed(0,0,0);
7f7c3819
RK
399}
400
401/** @brief Called when the text entry field in the new-playlist popup changes */
402static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable,
403 gpointer attribute((unused)) user_data) {
7c12e4bd 404 playlist_new_changed(0,0,0);
7f7c3819
RK
405}
406
407/** @brief Pop up a new window to enter the playlist name and details */
b5e60f0d 408static void playlist_new_playlist(void) {
7f7c3819
RK
409 assert(playlist_new_window == NULL);
410 playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
411 g_signal_connect(playlist_new_window, "destroy",
412 G_CALLBACK(gtk_widget_destroyed), &playlist_new_window);
413 gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist");
414 /* Window will be modal, suppressing access to other windows */
415 gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE);
7c12e4bd 416 gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window),
7fe2807d 417 GTK_WINDOW(playlist_window));
7f7c3819
RK
418
419 /* Window contents will use a table (grid) layout */
420 GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/);
421
422 /* First row: playlist name */
423 gtk_table_attach_defaults(GTK_TABLE(table),
424 gtk_label_new("Playlist name"),
425 0, 1, 0, 1);
426 playlist_new_entry = gtk_entry_new();
427 g_signal_connect(playlist_new_entry, "changed",
428 G_CALLBACK(playlist_new_entry_edited), NULL);
429 gtk_table_attach_defaults(GTK_TABLE(table),
430 playlist_new_entry,
431 1, 3, 0, 1);
432
433 /* Second row: radio buttons to choose type */
434 playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared");
435 playlist_new_public
436 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared),
437 "public");
438 playlist_new_private
439 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared),
440 "private");
441 g_signal_connect(playlist_new_shared, "toggled",
442 G_CALLBACK(playlist_new_button_toggled), NULL);
443 g_signal_connect(playlist_new_public, "toggled",
444 G_CALLBACK(playlist_new_button_toggled), NULL);
445 g_signal_connect(playlist_new_private, "toggled",
446 G_CALLBACK(playlist_new_button_toggled), NULL);
447 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2);
448 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2);
449 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2);
450
451 /* Third row: info bar saying why not */
452 playlist_new_info = gtk_label_new("");
453 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info,
454 0, 3, 2, 3);
455
456 /* Fourth row: ok/cancel buttons */
457 GtkWidget *hbox = create_buttons_box(playlist_new_buttons,
458 NPLAYLIST_NEW_BUTTONS,
459 gtk_hbox_new(FALSE, 0));
460 gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4);
461
462 gtk_container_add(GTK_CONTAINER(playlist_new_window),
463 frame_widget(table, NULL));
464
465 /* Set initial state of OK button */
7c12e4bd 466 playlist_new_changed(0,0,0);
7f7c3819 467
7e702780
RK
468 /* TODO: return should = OK, escape should = cancel */
469
7f7c3819
RK
470 /* Display the window */
471 gtk_widget_show_all(playlist_new_window);
472}
473
b5e60f0d 474/* Playlist picker ---------------------------------------------------------- */
121944d1 475
b5e60f0d
RK
476/** @brief Delete button */
477static GtkWidget *playlist_picker_delete_button;
478
479/** @brief Tree model for list of playlists */
480static GtkListStore *playlist_picker_list;
481
482/** @brief Selection for list of playlists */
483static GtkTreeSelection *playlist_picker_selection;
484
485/** @brief Currently selected playlist */
486static const char *playlist_picker_selected;
487
488/** @brief (Re-)populate the playlist picker tree model */
489static void playlist_picker_fill(const char attribute((unused)) *event,
490 void attribute((unused)) *eventdata,
491 void attribute((unused)) *callbackdata) {
f0bd437a
RK
492 GtkTreeIter iter[1];
493
7fe2807d 494 if(!playlist_window)
d571e07b 495 return;
b5e60f0d
RK
496 if(!playlist_picker_list)
497 playlist_picker_list = gtk_list_store_new(1, G_TYPE_STRING);
498 const char *was_selected = playlist_picker_selected;
499 gtk_list_store_clear(playlist_picker_list); /* clears playlists_selected */
d571e07b 500 for(int n = 0; n < nplaylists; ++n) {
b5e60f0d
RK
501 gtk_list_store_insert_with_values(playlist_picker_list, iter,
502 n /*position*/,
503 0, playlists[n], /* column 0 */
504 -1); /* no more cols */
d571e07b
RK
505 /* Reselect the selected playlist */
506 if(was_selected && !strcmp(was_selected, playlists[n]))
b5e60f0d 507 gtk_tree_selection_select_iter(playlist_picker_selection, iter);
d571e07b 508 }
9a6e6a46
RK
509 /* TODO deselecting then reselecting the current playlist resets the playlist
510 * editor, which trashes the user's selection. */
f0bd437a
RK
511}
512
513/** @brief Called when the selection might have changed */
b5e60f0d
RK
514static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
515 gpointer attribute((unused)) user_data) {
f0bd437a
RK
516 GtkTreeIter iter;
517 char *gselected, *selected;
518
519 /* Identify the current selection */
b5e60f0d
RK
520 if(gtk_tree_selection_get_selected(playlist_picker_selection, 0, &iter)) {
521 gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), &iter,
f0bd437a
RK
522 0, &gselected, -1);
523 selected = xstrdup(gselected);
524 g_free(gselected);
525 } else
526 selected = 0;
d9b141cc
RK
527 /* Set button sensitivity according to the new state */
528 if(selected)
b5e60f0d 529 gtk_widget_set_sensitive(playlist_picker_delete_button, 1);
7c12e4bd 530 else
b5e60f0d 531 gtk_widget_set_sensitive(playlist_picker_delete_button, 0);
f0bd437a 532 /* Eliminate no-change cases */
b5e60f0d 533 if(!selected && !playlist_picker_selected)
f0bd437a 534 return;
b5e60f0d
RK
535 if(selected
536 && playlist_picker_selected
537 && !strcmp(selected, playlist_picker_selected))
f0bd437a 538 return;
d9b141cc 539 /* Record the new state */
b5e60f0d 540 playlist_picker_selected = selected;
7e702780
RK
541 /* Re-initalize the queue */
542 ql_new_queue(&ql_playlist, NULL);
7fe2807d 543 playlist_editor_fill(NULL, (void *)playlist_picker_selected, NULL);
f0bd437a
RK
544}
545
546/** @brief Called when the 'add' button is pressed */
b5e60f0d
RK
547static void playlist_picker_add(GtkButton attribute((unused)) *button,
548 gpointer attribute((unused)) userdata) {
7c12e4bd 549 /* Unselect whatever is selected TODO why?? */
b5e60f0d
RK
550 gtk_tree_selection_unselect_all(playlist_picker_selection);
551 playlist_new_playlist();
f0bd437a
RK
552}
553
d9b141cc 554/** @brief Called when playlist deletion completes */
b5e60f0d
RK
555static void playlists_picker_delete_completed(void attribute((unused)) *v,
556 const char *err) {
d9b141cc
RK
557 if(err)
558 popup_protocol_error(0, err);
559}
560
f0bd437a 561/** @brief Called when the 'Delete' button is pressed */
b5e60f0d
RK
562static void playlist_picker_delete(GtkButton attribute((unused)) *button,
563 gpointer attribute((unused)) userdata) {
f0bd437a
RK
564 GtkWidget *yesno;
565 int res;
566
b5e60f0d 567 if(!playlist_picker_selected)
f0bd437a 568 return; /* shouldn't happen */
7fe2807d 569 yesno = gtk_message_dialog_new(GTK_WINDOW(playlist_window),
f0bd437a
RK
570 GTK_DIALOG_MODAL,
571 GTK_MESSAGE_QUESTION,
572 GTK_BUTTONS_YES_NO,
c5782050 573 "Do you really want to delete playlist %s?"
f0bd437a 574 " This action cannot be undone.",
b5e60f0d 575 playlist_picker_selected);
f0bd437a
RK
576 res = gtk_dialog_run(GTK_DIALOG(yesno));
577 gtk_widget_destroy(yesno);
578 if(res == GTK_RESPONSE_YES) {
579 disorder_eclient_playlist_delete(client,
b5e60f0d
RK
580 playlists_picker_delete_completed,
581 playlist_picker_selected,
f0bd437a
RK
582 NULL);
583 }
584}
585
586/** @brief Table of buttons below the playlist list */
b5e60f0d 587static struct button playlist_picker_buttons[] = {
f0bd437a
RK
588 {
589 GTK_STOCK_ADD,
b5e60f0d 590 playlist_picker_add,
f0bd437a
RK
591 "Create a new playlist",
592 0
593 },
594 {
595 GTK_STOCK_REMOVE,
b5e60f0d 596 playlist_picker_delete,
f0bd437a
RK
597 "Delete a playlist",
598 0
599 },
600};
b5e60f0d 601#define NPLAYLIST_PICKER_BUTTONS (sizeof playlist_picker_buttons / sizeof *playlist_picker_buttons)
f0bd437a 602
506e02d8 603/** @brief Create the list of playlists for the edit playlists window */
b5e60f0d 604static GtkWidget *playlist_picker_create(void) {
506e02d8 605 /* Create the list of playlist and populate it */
b5e60f0d 606 playlist_picker_fill(NULL, NULL, NULL);
506e02d8 607 /* Create the tree view */
b5e60f0d 608 GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlist_picker_list));
506e02d8
RK
609 /* ...and the renderers for it */
610 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
611 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist",
612 cr,
613 "text", 0,
614 NULL);
615 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
616 /* Get the selection for the view; set its mode; arrange for a callback when
617 * it changes */
b5e60f0d
RK
618 playlist_picker_selected = NULL;
619 playlist_picker_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
620 gtk_tree_selection_set_mode(playlist_picker_selection, GTK_SELECTION_BROWSE);
621 g_signal_connect(playlist_picker_selection, "changed",
622 G_CALLBACK(playlist_picker_selection_changed), NULL);
506e02d8
RK
623
624 /* Create the control buttons */
b5e60f0d
RK
625 GtkWidget *buttons = create_buttons_box(playlist_picker_buttons,
626 NPLAYLIST_PICKER_BUTTONS,
506e02d8 627 gtk_hbox_new(FALSE, 1));
b5e60f0d 628 playlist_picker_delete_button = playlist_picker_buttons[1].widget;
506e02d8 629
b5e60f0d 630 playlist_picker_selection_changed(NULL, NULL);
7c12e4bd 631
506e02d8
RK
632 /* Buttons live below the list */
633 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
634 gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
635 gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
636
637 return vbox;
638}
639
7fe2807d 640/* Playlist editor ---------------------------------------------------------- */
506e02d8 641
7e702780 642/** @brief Called with new tracks for the playlist */
f06a754c 643static void playlists_editor_received_tracks(void *v,
7fe2807d
RK
644 const char *err,
645 int nvec, char **vec) {
f06a754c 646 const char *playlist = v;
7e702780
RK
647 if(err) {
648 popup_protocol_error(0, err);
649 return;
650 }
f06a754c
RK
651 if(!playlist_picker_selected
652 || strcmp(playlist, playlist_picker_selected)) {
653 /* The tracks are for the wrong playlist - something must have changed
654 * while the fetch command was in flight. We just ignore this callback,
655 * the right answer will be requested and arrive in due course. */
656 return;
657 }
7e702780
RK
658 if(nvec == -1)
659 /* No such playlist, presumably we'll get a deleted event shortly */
660 return;
661 /* Translate the list of tracks into queue entries */
662 struct queue_entry *newq, **qq = &newq;
663 hash *h = hash_new(sizeof(int));
664 for(int n = 0; n < nvec; ++n) {
665 struct queue_entry *q = xmalloc(sizeof *q);
666 q->track = vec[n];
667 /* Synthesize a unique ID so that the selection survives updates. Tracks
668 * can appear more than once in the queue so we can't use raw track names,
669 * so we add a serial number to the start. */
670 /* TODO but this doesn't work for some reason */
671 int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0;
672 byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]);
7e702780
RK
673 hash_add(h, vec[0], &serial, HASH_INSERT_OR_REPLACE);
674 *qq = q;
675 qq = &q->next;
676 }
677 *qq = NULL;
7e702780 678 ql_new_queue(&ql_playlist, newq);
7e702780
RK
679}
680
681/** @brief (Re-)populate the playlist tree model */
7fe2807d
RK
682static void playlist_editor_fill(const char attribute((unused)) *event,
683 void *eventdata,
684 void attribute((unused)) *callbackdata) {
7e702780 685 const char *modified_playlist = eventdata;
7fe2807d 686 if(!playlist_window)
7e702780 687 return;
b5e60f0d 688 if(!playlist_picker_selected)
7e702780 689 return;
b5e60f0d 690 if(!strcmp(playlist_picker_selected, modified_playlist))
7fe2807d 691 disorder_eclient_playlist_get(client, playlists_editor_received_tracks,
f06a754c
RK
692 playlist_picker_selected,
693 (void *)playlist_picker_selected);
7e702780
RK
694}
695
7fe2807d 696static GtkWidget *playlists_editor_create(void) {
6bfaa2a1
RK
697 assert(ql_playlist.view == NULL); /* better not be set up already */
698 GtkWidget *w = init_queuelike(&ql_playlist);
7e702780 699 /* Initially empty */
6bfaa2a1 700 return w;
506e02d8
RK
701}
702
703/* Playlists window --------------------------------------------------------- */
704
f0bd437a 705/** @brief Keypress handler */
7fe2807d
RK
706static gboolean playlist_window_keypress(GtkWidget attribute((unused)) *widget,
707 GdkEventKey *event,
708 gpointer attribute((unused)) user_data) {
f0bd437a
RK
709 if(event->state)
710 return FALSE;
711 switch(event->keyval) {
712 case GDK_Escape:
7fe2807d 713 gtk_widget_destroy(playlist_window);
f0bd437a
RK
714 return TRUE;
715 default:
716 return FALSE;
717 }
718}
719
6bfaa2a1 720/** @brief Called when the playlist window is destroyed */
7fe2807d
RK
721static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget,
722 GtkWidget **widget_pointer) {
6bfaa2a1
RK
723 destroy_queuelike(&ql_playlist);
724 *widget_pointer = NULL;
725}
726
7de5fbb9 727/** @brief Pop up the playlists window
121944d1
RK
728 *
729 * Called when the playlists menu item is selected
730 */
f06a754c
RK
731void playlist_window_create(gpointer attribute((unused)) callback_data,
732 guint attribute((unused)) callback_action,
733 GtkWidget attribute((unused)) *menu_item) {
f0bd437a 734 /* If the window already exists, raise it */
7fe2807d
RK
735 if(playlist_window) {
736 gtk_window_present(GTK_WINDOW(playlist_window));
f0bd437a
RK
737 return;
738 }
739 /* Create the window */
7fe2807d
RK
740 playlist_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
741 gtk_widget_set_style(playlist_window, tool_style);
742 g_signal_connect(playlist_window, "destroy",
743 G_CALLBACK(playlist_window_destroyed), &playlist_window);
744 gtk_window_set_title(GTK_WINDOW(playlist_window), "Playlists Management");
f0bd437a
RK
745 /* TODO loads of this is very similar to (copied from!) users.c - can we
746 * de-dupe? */
747 /* Keyboard shortcuts */
7fe2807d
RK
748 g_signal_connect(playlist_window, "key-press-event",
749 G_CALLBACK(playlist_window_keypress), 0);
f0bd437a 750 /* default size is too small */
7fe2807d 751 gtk_window_set_default_size(GTK_WINDOW(playlist_window), 512, 240);
f0bd437a 752
506e02d8 753 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
b5e60f0d 754 gtk_box_pack_start(GTK_BOX(hbox), playlist_picker_create(),
506e02d8
RK
755 FALSE/*expand*/, FALSE, 0);
756 gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(),
757 FALSE/*expand*/, FALSE, 2);
7fe2807d 758 gtk_box_pack_start(GTK_BOX(hbox), playlists_editor_create(),
506e02d8 759 TRUE/*expand*/, TRUE/*fill*/, 0);
f0bd437a 760
7fe2807d
RK
761 gtk_container_add(GTK_CONTAINER(playlist_window), frame_widget(hbox, NULL));
762 gtk_widget_show_all(playlist_window);
f9b20469
RK
763}
764
fc36ecb7
RK
765/** @brief Initialize playlist support */
766void playlists_init(void) {
767 /* We re-get all playlists upon any change... */
b5e60f0d 768 event_register("playlist-created", playlist_list_update, 0);
b5e60f0d 769 event_register("playlist-deleted", playlist_list_update, 0);
fc36ecb7 770 /* ...and on reconnection */
b5e60f0d 771 event_register("log-connected", playlist_list_update, 0);
fc36ecb7 772 /* ...and from time to time */
b5e60f0d 773 event_register("periodic-slow", playlist_list_update, 0);
fc36ecb7 774 /* ...and at startup */
b5e60f0d 775 playlist_list_update(0, 0, 0);
7c12e4bd
RK
776
777 /* Update the playlists menu when the set of playlists changes */
b5e60f0d 778 event_register("playlists-updated", playlist_menu_changed, 0);
7c12e4bd
RK
779 /* Update the new-playlist OK button when the set of playlists changes */
780 event_register("playlists-updated", playlist_new_changed, 0);
d9b141cc 781 /* Update the list of playlists in the edit window when the set changes */
b5e60f0d 782 event_register("playlists-updated", playlist_picker_fill, 0);
7e702780 783 /* Update the displayed playlist when it is modified */
7fe2807d 784 event_register("playlist-modified", playlist_editor_fill, 0);
fc36ecb7
RK
785}
786
0b0fb26b
RK
787#endif
788
fc36ecb7
RK
789/*
790Local Variables:
791c-basic-offset:2
792comment-column:40
793fill-column:79
794indent-tabs-mode:nil
795End:
796*/