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