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