chiark / gitweb /
More realistic default size for playlist window.
[disorder] / disobedience / playlists.c
CommitLineData
fc36ecb7
RK
1/*
2 * This file is part of DisOrder
121944d1 3 * Copyright (C) 2008, 2009 Richard Kettlewell
fc36ecb7
RK
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20/** @file disobedience/playlists.c
b5e60f0d 21 * @brief Playlist support for Disobedience
fc36ecb7 22 *
f0bd437a 23 * The playlists management window contains:
b5e60f0d 24 * - the playlist picker (a list of all playlists) TODO should be a tree!
f0bd437a
RK
25 * - an add button
26 * - a delete button
7fe2807d 27 * - the playlist editor (a d+d-capable view of the currently picked playlist)
b5e60f0d
RK
28 * - a close button TODO
29 *
30 * This file also maintains the playlist menu, allowing playlists to be
31 * activated from the main window's menu.
32 *
33 * Internally we maintain the playlist list, which is just the current list of
34 * playlists. Changes to this are reflected in the playlist menu and the
35 * playlist picker.
36 *
fc36ecb7
RK
37 */
38#include "disobedience.h"
6bfaa2a1
RK
39#include "queue-generic.h"
40#include "popup.h"
7f7c3819 41#include "validity.h"
fc36ecb7 42
0b0fb26b
RK
43#if PLAYLISTS
44
b5e60f0d
RK
45static void playlist_list_received_playlists(void *v,
46 const char *err,
47 int nvec, char **vec);
7fe2807d
RK
48static void playlist_editor_fill(const char *event,
49 void *eventdata,
50 void *callbackdata);
b35f7c0f
RK
51static int playlist_playall_sensitive(void *extra);
52static void playlist_playall_activate(GtkMenuItem *menuitem,
53 gpointer user_data);
dba3eb8e
RK
54static int playlist_remove_sensitive(void *extra) ;
55static void playlist_remove_activate(GtkMenuItem *menuitem,
56 gpointer user_data);
c41b2cac
RK
57static void playlist_new_locked(void *v, const char *err);
58static void playlist_new_retrieved(void *v, const char *err,
59 int nvec,
60 char **vec);
61static void playlist_new_created(void *v, const char *err);
62static void playlist_new_unlocked(void *v, const char *err);
63static void playlist_new_entry_edited(GtkEditable *editable,
64 gpointer user_data);
65static void playlist_new_button_toggled(GtkToggleButton *tb,
66 gpointer userdata);
67static void playlist_new_changed(const char *event,
68 void *eventdata,
69 void *callbackdata);
70static const char *playlist_new_valid(void);
71static void playlist_new_details(char **namep,
72 char **fullnamep,
73 gboolean *sharedp,
74 gboolean *publicp,
75 gboolean *privatep);
76static void playlist_new_ok(GtkButton *button,
77 gpointer userdata);
78static void playlist_new_cancel(GtkButton *button,
79 gpointer userdata);
80static void playlists_editor_received_tracks(void *v,
81 const char *err,
82 int nvec, char **vec);
83static void playlist_window_destroyed(GtkWidget *widget,
84 GtkWidget **widget_pointer);
85static gboolean playlist_window_keypress(GtkWidget *widget,
86 GdkEventKey *event,
87 gpointer user_data);
88static int playlistcmp(const void *ap, const void *bp);
7a3eb8eb
RK
89static void playlist_modify_locked(void *v, const char *err);
90void playlist_modify_retrieved(void *v, const char *err,
487eb32a
RK
91 int nvec,
92 char **vec);
7a3eb8eb
RK
93static void playlist_modify_updated(void *v, const char *err);
94static void playlist_modify_unlocked(void *v, const char *err);
95static void playlist_drop(struct queuelike *ql,
96 int ntracks,
97 char **tracks, char **ids,
98 struct queue_entry *after_me);
99struct playlist_modify_data;
100static void playlist_drop_modify(struct playlist_modify_data *mod,
101 int nvec, char **vec);
102static void playlist_remove_modify(struct playlist_modify_data *mod,
103 int nvec, char **vec);
fc36ecb7 104
f0bd437a 105/** @brief Playlist editing window */
7fe2807d 106static GtkWidget *playlist_window;
f0bd437a 107
6bfaa2a1
RK
108/** @brief Columns for the playlist editor */
109static const struct queue_column playlist_columns[] = {
110 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
111 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
112 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
6bfaa2a1
RK
113};
114
dba3eb8e
RK
115/** @brief Pop-up menu for playlist editor
116 *
117 * Status:
118 * - track properties works but, bizarrely, raises the main window
119 * - play track works
120 * - play playlist works
121 * - select/deselect all work
122 */
6bfaa2a1
RK
123static struct menuitem playlist_menuitems[] = {
124 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
125 { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 },
b35f7c0f 126 { "Play playlist", playlist_playall_activate, playlist_playall_sensitive, 0, 0 },
dba3eb8e 127 { "Remove track from queue", playlist_remove_activate, playlist_remove_sensitive, 0, 0 },
6bfaa2a1
RK
128 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
129 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
130};
131
a6712ea8
RK
132static const GtkTargetEntry playlist_targets[] = {
133 {
134 PLAYLIST_TRACKS, /* drag type */
135 GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */
136 PLAYLIST_TRACKS_ID /* ID value */
137 },
138 {
139 PLAYABLE_TRACKS, /* drag type */
140 GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */
141 PLAYABLE_TRACKS_ID, /* ID value */
142 },
143 {
144 .target = NULL
145 }
146};
147
6bfaa2a1
RK
148/** @brief Queuelike for editing a playlist */
149static struct queuelike ql_playlist = {
150 .name = "playlist",
151 .columns = playlist_columns,
152 .ncolumns = sizeof playlist_columns / sizeof *playlist_columns,
153 .menuitems = playlist_menuitems,
154 .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems,
a6712ea8
RK
155 .drop = playlist_drop,
156 .drag_source_targets = playlist_targets,
157 .drag_source_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY,
158 .drag_dest_targets = playlist_targets,
159 .drag_dest_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY,
6bfaa2a1
RK
160};
161
3c1a4e96 162/* Maintaining the list of playlists ---------------------------------------- */
121944d1 163
b5e60f0d
RK
164/** @brief Current list of playlists or NULL */
165char **playlists;
166
167/** @brief Count of playlists */
168int nplaylists;
169
170/** @brief Schedule an update to the list of playlists
171 *
172 * Called periodically and when a playlist is created or deleted.
173 */
174static void playlist_list_update(const char attribute((unused)) *event,
175 void attribute((unused)) *eventdata,
176 void attribute((unused)) *callbackdata) {
177 disorder_eclient_playlists(client, playlist_list_received_playlists, 0);
fc36ecb7
RK
178}
179
c41b2cac
RK
180/** @brief Called with a new list of playlists */
181static void playlist_list_received_playlists(void attribute((unused)) *v,
182 const char *err,
183 int nvec, char **vec) {
184 if(err) {
185 playlists = 0;
186 nplaylists = -1;
187 /* Probably means server does not support playlists */
188 } else {
189 playlists = vec;
190 nplaylists = nvec;
191 qsort(playlists, nplaylists, sizeof (char *), playlistcmp);
192 }
193 /* Tell our consumers */
194 event_raise("playlists-updated", 0);
195}
196
fc36ecb7
RK
197/** @brief qsort() callback for playlist name comparison */
198static int playlistcmp(const void *ap, const void *bp) {
199 const char *a = *(char **)ap, *b = *(char **)bp;
200 const char *ad = strchr(a, '.'), *bd = strchr(b, '.');
201 int c;
202
203 /* Group owned playlists by owner */
204 if(ad && bd) {
205 const int adn = ad - a, bdn = bd - b;
206 if((c = strncmp(a, b, adn < bdn ? adn : bdn)))
207 return c;
208 /* Lexical order within playlists of a single owner */
209 return strcmp(ad + 1, bd + 1);
210 }
211
212 /* Owned playlists after shared ones */
213 if(ad) {
214 return 1;
215 } else if(bd) {
216 return -1;
217 }
218
219 /* Lexical order of shared playlists */
220 return strcmp(a, b);
221}
222
121944d1
RK
223/* Playlists menu ----------------------------------------------------------- */
224
b35f7c0f
RK
225static void playlist_menu_playing(void attribute((unused)) *v,
226 const char *err) {
227 if(err)
228 popup_protocol_error(0, err);
229}
230
121944d1
RK
231/** @brief Play received playlist contents
232 *
233 * Passed as a completion callback by menu_activate_playlist().
234 */
b5e60f0d
RK
235static void playlist_menu_received_content(void attribute((unused)) *v,
236 const char *err,
237 int nvec, char **vec) {
121944d1
RK
238 if(err) {
239 popup_protocol_error(0, err);
240 return;
241 }
242 for(int n = 0; n < nvec; ++n)
b35f7c0f 243 disorder_eclient_play(client, vec[n], playlist_menu_playing, NULL);
121944d1
RK
244}
245
246/** @brief Called to activate a playlist
247 *
248 * Called when the menu item for a playlist is clicked.
249 */
b5e60f0d 250static void playlist_menu_activate(GtkMenuItem *menuitem,
f9b20469
RK
251 gpointer attribute((unused)) user_data) {
252 GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
253 const char *playlist = gtk_label_get_text(label);
254
b5e60f0d
RK
255 disorder_eclient_playlist_get(client, playlist_menu_received_content,
256 playlist, NULL);
f9b20469
RK
257}
258
b5e60f0d
RK
259/** @brief Called when the playlists change
260 *
261 * Naively refills the menu. The results might be unsettling if the menu is
262 * currently open, but this is hopefuly fairly rare.
263 */
264static void playlist_menu_changed(const char attribute((unused)) *event,
265 void attribute((unused)) *eventdata,
266 void attribute((unused)) *callbackdata) {
f9b20469
RK
267 if(!playlists_menu)
268 return; /* OMG too soon */
269 GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
f9b20469
RK
270 while(menu->children)
271 gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
272 /* NB nplaylists can be -1 as well as 0 */
273 for(int n = 0; n < nplaylists; ++n) {
274 GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]);
b5e60f0d 275 g_signal_connect(w, "activate", G_CALLBACK(playlist_menu_activate), 0);
f9b20469
RK
276 gtk_widget_show(w);
277 gtk_menu_shell_append(menu, w);
278 }
fdea9f40 279 gtk_widget_set_sensitive(menu_playlists_widget,
f9b20469 280 nplaylists > 0);
fdea9f40 281 gtk_widget_set_sensitive(menu_editplaylists_widget,
6acdbba4 282 nplaylists >= 0);
f9b20469
RK
283}
284
7f7c3819
RK
285/* Popup to create a new playlist ------------------------------------------- */
286
287/** @brief New-playlist popup */
288static GtkWidget *playlist_new_window;
289
290/** @brief Text entry in new-playlist popup */
291static GtkWidget *playlist_new_entry;
292
b5e60f0d 293/** @brief Label for displaying feedback on what's wrong */
7f7c3819
RK
294static GtkWidget *playlist_new_info;
295
b5e60f0d 296/** @brief "Shared" radio button */
7f7c3819 297static GtkWidget *playlist_new_shared;
b5e60f0d
RK
298
299/** @brief "Public" radio button */
7f7c3819 300static GtkWidget *playlist_new_public;
b5e60f0d
RK
301
302/** @brief "Private" radio button */
7f7c3819
RK
303static GtkWidget *playlist_new_private;
304
c41b2cac
RK
305/** @brief Buttons for new-playlist popup */
306static struct button playlist_new_buttons[] = {
307 {
308 .stock = GTK_STOCK_OK,
309 .clicked = playlist_new_ok,
310 .tip = "Create new playlist"
311 },
312 {
313 .stock = GTK_STOCK_CANCEL,
314 .clicked = playlist_new_cancel,
315 .tip = "Do not create new playlist"
7c12e4bd 316 }
c41b2cac
RK
317};
318#define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons)
319
320/** @brief Pop up a new window to enter the playlist name and details */
321static void playlist_new_playlist(void) {
322 assert(playlist_new_window == NULL);
323 playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
324 g_signal_connect(playlist_new_window, "destroy",
325 G_CALLBACK(gtk_widget_destroyed), &playlist_new_window);
326 gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist");
327 /* Window will be modal, suppressing access to other windows */
328 gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE);
329 gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window),
330 GTK_WINDOW(playlist_window));
331
332 /* Window contents will use a table (grid) layout */
333 GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/);
334
335 /* First row: playlist name */
336 gtk_table_attach_defaults(GTK_TABLE(table),
337 gtk_label_new("Playlist name"),
338 0, 1, 0, 1);
339 playlist_new_entry = gtk_entry_new();
340 g_signal_connect(playlist_new_entry, "changed",
341 G_CALLBACK(playlist_new_entry_edited), NULL);
342 gtk_table_attach_defaults(GTK_TABLE(table),
343 playlist_new_entry,
344 1, 3, 0, 1);
345
346 /* Second row: radio buttons to choose type */
347 playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared");
348 playlist_new_public
349 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared),
350 "public");
351 playlist_new_private
352 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared),
353 "private");
354 g_signal_connect(playlist_new_shared, "toggled",
355 G_CALLBACK(playlist_new_button_toggled), NULL);
356 g_signal_connect(playlist_new_public, "toggled",
357 G_CALLBACK(playlist_new_button_toggled), NULL);
358 g_signal_connect(playlist_new_private, "toggled",
359 G_CALLBACK(playlist_new_button_toggled), NULL);
360 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2);
361 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2);
362 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2);
363
364 /* Third row: info bar saying why not */
365 playlist_new_info = gtk_label_new("");
366 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info,
367 0, 3, 2, 3);
368
369 /* Fourth row: ok/cancel buttons */
370 GtkWidget *hbox = create_buttons_box(playlist_new_buttons,
371 NPLAYLIST_NEW_BUTTONS,
372 gtk_hbox_new(FALSE, 0));
373 gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4);
374
375 gtk_container_add(GTK_CONTAINER(playlist_new_window),
376 frame_widget(table, NULL));
377
378 /* Set initial state of OK button */
379 playlist_new_changed(0,0,0);
380
381 /* TODO: return should = OK, escape should = cancel */
382
383 /* Display the window */
384 gtk_widget_show_all(playlist_new_window);
7c12e4bd
RK
385}
386
c41b2cac
RK
387/** @brief Called when 'ok' is clicked in new-playlist popup */
388static void playlist_new_ok(GtkButton attribute((unused)) *button,
389 gpointer attribute((unused)) userdata) {
390 gboolean shared, public, private;
391 char *name, *fullname;
392 playlist_new_details(&name, &fullname, &shared, &public, &private);
393
394 /* We need to:
395 * - lock the playlist
396 * - check it doesn't exist
397 * - set sharing (which will create it empty
398 * - unlock it
399 *
400 * TODO we should freeze the window while this is going on to stop a second
401 * click.
402 */
403 disorder_eclient_playlist_lock(client, playlist_new_locked, fullname,
404 fullname);
7c12e4bd
RK
405}
406
c41b2cac
RK
407/** @brief Called when the proposed new playlist has been locked */
408static void playlist_new_locked(void *v, const char *err) {
409 char *fullname = v;
7c12e4bd
RK
410 if(err) {
411 popup_protocol_error(0, err);
412 return;
413 }
c41b2cac
RK
414 disorder_eclient_playlist_get(client, playlist_new_retrieved,
415 fullname, fullname);
7c12e4bd
RK
416}
417
418/** @brief Called when the proposed new playlist's contents have been retrieved
419 *
420 * ...or rather, normally, when it's been reported that it does not exist.
421 */
422static void playlist_new_retrieved(void *v, const char *err,
423 int nvec,
424 char attribute((unused)) **vec) {
425 char *fullname = v;
426 if(!err && nvec != -1)
427 /* A rare case but not in principle impossible */
428 err = "A playlist with that name already exists.";
429 if(err) {
430 popup_protocol_error(0, err);
431 disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname);
432 return;
433 }
434 gboolean shared, public, private;
435 playlist_new_details(0, 0, &shared, &public, &private);
436 disorder_eclient_playlist_set_share(client, playlist_new_created, fullname,
437 public ? "public"
438 : private ? "private"
439 : "shared",
440 fullname);
441}
442
c41b2cac
RK
443/** @brief Called when the new playlist has been created */
444static void playlist_new_created(void attribute((unused)) *v, const char *err) {
7c12e4bd
RK
445 if(err) {
446 popup_protocol_error(0, err);
447 return;
448 }
c41b2cac
RK
449 disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL);
450 // TODO arrange for the new playlist to be selected
7c12e4bd
RK
451}
452
c41b2cac
RK
453/** @brief Called when the newly created playlist has unlocked */
454static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) {
455 if(err)
456 popup_protocol_error(0, err);
457 /* Pop down the creation window */
458 gtk_widget_destroy(playlist_new_window);
7f7c3819
RK
459}
460
461/** @brief Called when 'cancel' is clicked in new-playlist popup */
462static void playlist_new_cancel(GtkButton attribute((unused)) *button,
463 gpointer attribute((unused)) userdata) {
464 gtk_widget_destroy(playlist_new_window);
465}
466
c41b2cac
RK
467/** @brief Called when some radio button in the new-playlist popup changes */
468static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) *tb,
469 gpointer attribute((unused)) userdata) {
470 playlist_new_changed(0,0,0);
471}
472
473/** @brief Called when the text entry field in the new-playlist popup changes */
474static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable,
475 gpointer attribute((unused)) user_data) {
476 playlist_new_changed(0,0,0);
477}
478
479/** @brief Called to update new playlist window state
480 *
481 * This is called whenever one the text entry or radio buttons changed, and
482 * also when the set of known playlists changes. It determines whether the new
483 * playlist would be creatable and sets the sensitivity of the OK button
484 * and info display accordingly.
485 */
486static void playlist_new_changed(const char attribute((unused)) *event,
487 void attribute((unused)) *eventdata,
488 void attribute((unused)) *callbackdata) {
489 if(!playlist_new_window)
490 return;
491 const char *reason = playlist_new_valid();
492 gtk_widget_set_sensitive(playlist_new_buttons[0].widget,
493 !reason);
494 gtk_label_set_text(GTK_LABEL(playlist_new_info), reason);
495}
7f7c3819 496
7c12e4bd
RK
497/** @brief Test whether the new-playlist window settings are valid
498 * @return NULL on success or an error string if not
499 */
7f7c3819
RK
500static const char *playlist_new_valid(void) {
501 gboolean shared, public, private;
7c12e4bd
RK
502 char *name, *fullname;
503 playlist_new_details(&name, &fullname, &shared, &public, &private);
7f7c3819
RK
504 if(!(shared || public || private))
505 return "No type set.";
7f7c3819
RK
506 if(!*name)
507 return "";
7f7c3819 508 /* See if the result is valid */
7c12e4bd
RK
509 if(!valid_username(name)
510 || playlist_parse_name(fullname, NULL, NULL))
7f7c3819 511 return "Not a valid playlist name.";
b5e60f0d
RK
512 /* See if the result clashes with an existing name. This is not a perfect
513 * check, the playlist might be created after this point but before we get a
514 * chance to disable the "OK" button. However when we try to create the
515 * playlist we will first try to retrieve it, with a lock held, so we
516 * shouldn't end up overwriting anything. */
7f7c3819
RK
517 for(int n = 0; n < nplaylists; ++n)
518 if(!strcmp(playlists[n], fullname)) {
519 if(shared)
520 return "A shared playlist with that name already exists.";
521 else
522 return "You already have a playlist with that name.";
523 }
524 /* As far as we can tell creation would work */
525 return NULL;
526}
527
c41b2cac
RK
528/** @brief Get entered new-playlist details
529 * @param namep Where to store entered name (or NULL)
530 * @param fullnamep Where to store computed full name (or NULL)
531 * @param sharep Where to store 'shared' flag (or NULL)
532 * @param publicp Where to store 'public' flag (or NULL)
533 * @param privatep Where to store 'private' flag (or NULL)
7f7c3819 534 */
c41b2cac
RK
535static void playlist_new_details(char **namep,
536 char **fullnamep,
537 gboolean *sharedp,
538 gboolean *publicp,
539 gboolean *privatep) {
540 gboolean shared, public, private;
541 g_object_get(playlist_new_shared, "active", &shared, (char *)NULL);
542 g_object_get(playlist_new_public, "active", &public, (char *)NULL);
543 g_object_get(playlist_new_private, "active", &private, (char *)NULL);
544 char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry),
545 0, -1); /* name owned by calle */
546 char *name = xstrdup(gname);
547 g_free(gname);
548 if(sharedp) *sharedp = shared;
549 if(publicp) *publicp = public;
550 if(privatep) *privatep = private;
551 if(namep) *namep = name;
552 if(fullnamep) {
553 if(*sharedp) *fullnamep = *namep;
554 else byte_xasprintf(fullnamep, "%s.%s", config->username, name);
555 }
7f7c3819
RK
556}
557
b5e60f0d 558/* Playlist picker ---------------------------------------------------------- */
121944d1 559
b5e60f0d
RK
560/** @brief Delete button */
561static GtkWidget *playlist_picker_delete_button;
562
563/** @brief Tree model for list of playlists */
564static GtkListStore *playlist_picker_list;
565
566/** @brief Selection for list of playlists */
567static GtkTreeSelection *playlist_picker_selection;
568
569/** @brief Currently selected playlist */
570static const char *playlist_picker_selected;
571
572/** @brief (Re-)populate the playlist picker tree model */
573static void playlist_picker_fill(const char attribute((unused)) *event,
574 void attribute((unused)) *eventdata,
575 void attribute((unused)) *callbackdata) {
f0bd437a
RK
576 GtkTreeIter iter[1];
577
7fe2807d 578 if(!playlist_window)
d571e07b 579 return;
b5e60f0d
RK
580 if(!playlist_picker_list)
581 playlist_picker_list = gtk_list_store_new(1, G_TYPE_STRING);
582 const char *was_selected = playlist_picker_selected;
583 gtk_list_store_clear(playlist_picker_list); /* clears playlists_selected */
d571e07b 584 for(int n = 0; n < nplaylists; ++n) {
b5e60f0d
RK
585 gtk_list_store_insert_with_values(playlist_picker_list, iter,
586 n /*position*/,
587 0, playlists[n], /* column 0 */
588 -1); /* no more cols */
d571e07b
RK
589 /* Reselect the selected playlist */
590 if(was_selected && !strcmp(was_selected, playlists[n]))
b5e60f0d 591 gtk_tree_selection_select_iter(playlist_picker_selection, iter);
d571e07b 592 }
9a6e6a46
RK
593 /* TODO deselecting then reselecting the current playlist resets the playlist
594 * editor, which trashes the user's selection. */
f0bd437a
RK
595}
596
597/** @brief Called when the selection might have changed */
b5e60f0d
RK
598static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
599 gpointer attribute((unused)) user_data) {
f0bd437a
RK
600 GtkTreeIter iter;
601 char *gselected, *selected;
602
603 /* Identify the current selection */
b5e60f0d
RK
604 if(gtk_tree_selection_get_selected(playlist_picker_selection, 0, &iter)) {
605 gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), &iter,
f0bd437a
RK
606 0, &gselected, -1);
607 selected = xstrdup(gselected);
608 g_free(gselected);
609 } else
610 selected = 0;
d9b141cc
RK
611 /* Set button sensitivity according to the new state */
612 if(selected)
b5e60f0d 613 gtk_widget_set_sensitive(playlist_picker_delete_button, 1);
7c12e4bd 614 else
b5e60f0d 615 gtk_widget_set_sensitive(playlist_picker_delete_button, 0);
f0bd437a 616 /* Eliminate no-change cases */
b5e60f0d 617 if(!selected && !playlist_picker_selected)
f0bd437a 618 return;
b5e60f0d
RK
619 if(selected
620 && playlist_picker_selected
621 && !strcmp(selected, playlist_picker_selected))
f0bd437a 622 return;
d9b141cc 623 /* Record the new state */
b5e60f0d 624 playlist_picker_selected = selected;
7e702780
RK
625 /* Re-initalize the queue */
626 ql_new_queue(&ql_playlist, NULL);
7fe2807d 627 playlist_editor_fill(NULL, (void *)playlist_picker_selected, NULL);
f0bd437a
RK
628}
629
630/** @brief Called when the 'add' button is pressed */
b5e60f0d
RK
631static void playlist_picker_add(GtkButton attribute((unused)) *button,
632 gpointer attribute((unused)) userdata) {
7c12e4bd 633 /* Unselect whatever is selected TODO why?? */
b5e60f0d
RK
634 gtk_tree_selection_unselect_all(playlist_picker_selection);
635 playlist_new_playlist();
f0bd437a
RK
636}
637
d9b141cc 638/** @brief Called when playlist deletion completes */
b5e60f0d
RK
639static void playlists_picker_delete_completed(void attribute((unused)) *v,
640 const char *err) {
d9b141cc
RK
641 if(err)
642 popup_protocol_error(0, err);
643}
644
f0bd437a 645/** @brief Called when the 'Delete' button is pressed */
b5e60f0d
RK
646static void playlist_picker_delete(GtkButton attribute((unused)) *button,
647 gpointer attribute((unused)) userdata) {
f0bd437a
RK
648 GtkWidget *yesno;
649 int res;
650
b5e60f0d 651 if(!playlist_picker_selected)
f0bd437a 652 return; /* shouldn't happen */
7fe2807d 653 yesno = gtk_message_dialog_new(GTK_WINDOW(playlist_window),
f0bd437a
RK
654 GTK_DIALOG_MODAL,
655 GTK_MESSAGE_QUESTION,
656 GTK_BUTTONS_YES_NO,
c5782050 657 "Do you really want to delete playlist %s?"
f0bd437a 658 " This action cannot be undone.",
b5e60f0d 659 playlist_picker_selected);
f0bd437a
RK
660 res = gtk_dialog_run(GTK_DIALOG(yesno));
661 gtk_widget_destroy(yesno);
662 if(res == GTK_RESPONSE_YES) {
663 disorder_eclient_playlist_delete(client,
b5e60f0d
RK
664 playlists_picker_delete_completed,
665 playlist_picker_selected,
f0bd437a
RK
666 NULL);
667 }
668}
669
670/** @brief Table of buttons below the playlist list */
b5e60f0d 671static struct button playlist_picker_buttons[] = {
f0bd437a
RK
672 {
673 GTK_STOCK_ADD,
b5e60f0d 674 playlist_picker_add,
f0bd437a
RK
675 "Create a new playlist",
676 0
677 },
678 {
679 GTK_STOCK_REMOVE,
b5e60f0d 680 playlist_picker_delete,
f0bd437a
RK
681 "Delete a playlist",
682 0
683 },
684};
b5e60f0d 685#define NPLAYLIST_PICKER_BUTTONS (sizeof playlist_picker_buttons / sizeof *playlist_picker_buttons)
f0bd437a 686
506e02d8 687/** @brief Create the list of playlists for the edit playlists window */
b5e60f0d 688static GtkWidget *playlist_picker_create(void) {
506e02d8 689 /* Create the list of playlist and populate it */
b5e60f0d 690 playlist_picker_fill(NULL, NULL, NULL);
506e02d8 691 /* Create the tree view */
b5e60f0d 692 GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlist_picker_list));
506e02d8
RK
693 /* ...and the renderers for it */
694 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
695 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist",
696 cr,
697 "text", 0,
698 NULL);
699 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
700 /* Get the selection for the view; set its mode; arrange for a callback when
701 * it changes */
b5e60f0d
RK
702 playlist_picker_selected = NULL;
703 playlist_picker_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
704 gtk_tree_selection_set_mode(playlist_picker_selection, GTK_SELECTION_BROWSE);
705 g_signal_connect(playlist_picker_selection, "changed",
706 G_CALLBACK(playlist_picker_selection_changed), NULL);
506e02d8
RK
707
708 /* Create the control buttons */
b5e60f0d
RK
709 GtkWidget *buttons = create_buttons_box(playlist_picker_buttons,
710 NPLAYLIST_PICKER_BUTTONS,
506e02d8 711 gtk_hbox_new(FALSE, 1));
b5e60f0d 712 playlist_picker_delete_button = playlist_picker_buttons[1].widget;
506e02d8 713
b5e60f0d 714 playlist_picker_selection_changed(NULL, NULL);
7c12e4bd 715
506e02d8
RK
716 /* Buttons live below the list */
717 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
718 gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
719 gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
720
721 return vbox;
722}
723
a544b1cc
RK
724static void playlist_picker_destroy(void) {
725 playlist_picker_delete_button = NULL;
726 g_object_unref(playlist_picker_list);
727 playlist_picker_list = NULL;
728 playlist_picker_selection = NULL;
729 playlist_picker_selected = NULL;
730}
731
7fe2807d 732/* Playlist editor ---------------------------------------------------------- */
506e02d8 733
c41b2cac
RK
734static GtkWidget *playlists_editor_create(void) {
735 assert(ql_playlist.view == NULL); /* better not be set up already */
736 GtkWidget *w = init_queuelike(&ql_playlist);
737 /* Initially empty */
738 return w;
739}
740
741/** @brief (Re-)populate the playlist tree model */
742static void playlist_editor_fill(const char attribute((unused)) *event,
743 void *eventdata,
744 void attribute((unused)) *callbackdata) {
745 const char *modified_playlist = eventdata;
746 if(!playlist_window)
747 return;
748 if(!playlist_picker_selected)
749 return;
750 if(!strcmp(playlist_picker_selected, modified_playlist))
751 disorder_eclient_playlist_get(client, playlists_editor_received_tracks,
752 playlist_picker_selected,
753 (void *)playlist_picker_selected);
754}
755
7e702780 756/** @brief Called with new tracks for the playlist */
f06a754c 757static void playlists_editor_received_tracks(void *v,
7fe2807d
RK
758 const char *err,
759 int nvec, char **vec) {
f06a754c 760 const char *playlist = v;
7e702780
RK
761 if(err) {
762 popup_protocol_error(0, err);
763 return;
764 }
f06a754c
RK
765 if(!playlist_picker_selected
766 || strcmp(playlist, playlist_picker_selected)) {
767 /* The tracks are for the wrong playlist - something must have changed
768 * while the fetch command was in flight. We just ignore this callback,
769 * the right answer will be requested and arrive in due course. */
770 return;
771 }
7e702780
RK
772 if(nvec == -1)
773 /* No such playlist, presumably we'll get a deleted event shortly */
774 return;
775 /* Translate the list of tracks into queue entries */
eaf4cd2e 776 struct queue_entry *newq, **qq = &newq, *qprev = NULL;
7e702780
RK
777 hash *h = hash_new(sizeof(int));
778 for(int n = 0; n < nvec; ++n) {
779 struct queue_entry *q = xmalloc(sizeof *q);
8bbeb28e 780 q->prev = qprev;
7e702780
RK
781 q->track = vec[n];
782 /* Synthesize a unique ID so that the selection survives updates. Tracks
783 * can appear more than once in the queue so we can't use raw track names,
784 * so we add a serial number to the start. */
7e702780
RK
785 int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0;
786 byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]);
f57ff3aa 787 hash_add(h, vec[n], &serial, HASH_INSERT_OR_REPLACE);
7e702780
RK
788 *qq = q;
789 qq = &q->next;
8bbeb28e 790 qprev = q;
7e702780
RK
791 }
792 *qq = NULL;
7e702780 793 ql_new_queue(&ql_playlist, newq);
7e702780
RK
794}
795
7a3eb8eb
RK
796/* Playlist mutation -------------------------------------------------------- */
797
798/** @brief State structure for guarded playlist modification
799 *
f57ff3aa
RK
800 * To safely move, insert or delete rows we must:
801 * - take a lock
802 * - fetch the playlist
803 * - verify it's not changed
804 * - update the playlist contents
805 * - store the playlist
806 * - release the lock
807 *
808 * The playlist_modify_ functions do just that.
809 *
7a3eb8eb
RK
810 * To kick things off create one of these and disorder_eclient_playlist_lock()
811 * with playlist_modify_locked() as its callback. @c modify will be called; it
812 * should disorder_eclient_playlist_set() to set the new state with
813 * playlist_modify_updated() as its callback.
814 */
815struct playlist_modify_data {
816 /** @brief Affected playlist */
817 const char *playlist;
818 /** @brief Modification function
819 * @param mod Pointer back to state structure
820 * @param ntracks Length of playlist
821 * @param tracks Tracks in playlist
822 */
823 void (*modify)(struct playlist_modify_data *mod,
824 int ntracks, char **tracks);
825
826 /** @brief Number of tracks dropped */
827 int ntracks;
828 /** @brief Track names dropped */
829 char **tracks;
830 /** @brief Track IDs dropped */
831 char **ids;
832 /** @brief Drop after this point */
833 struct queue_entry *after_me;
834};
835
836/** @brief Called with playlist locked
837 *
838 * This is the entry point for guarded modification ising @ref
839 * playlist_modify_data.
840 */
841static void playlist_modify_locked(void *v, const char *err) {
842 struct playlist_modify_data *mod = v;
843 if(err) {
844 popup_protocol_error(0, err);
845 return;
846 }
847 disorder_eclient_playlist_get(client, playlist_modify_retrieved,
848 mod->playlist, mod);
849}
850
851/** @brief Called with current playlist contents
852 * Checks that the playlist is still current and has not changed.
853 */
854void playlist_modify_retrieved(void *v, const char *err,
855 int nvec,
856 char **vec) {
857 struct playlist_modify_data *mod = v;
858 if(err) {
859 popup_protocol_error(0, err);
860 disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
861 return;
862 }
863 if(nvec < 0
864 || !playlist_picker_selected
865 || strcmp(mod->playlist, playlist_picker_selected)) {
866 disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
867 return;
868 }
869 /* We check that the contents haven't changed. If they have we just abandon
870 * the operation. The user will have to try again. */
871 struct queue_entry *q;
872 int n;
873 for(n = 0, q = ql_playlist.q; q && n < nvec; ++n, q = q->next)
874 if(strcmp(q->track, vec[n]))
875 break;
876 if(n != nvec || q != NULL) {
877 disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
878 return;
879 }
880 mod->modify(mod, nvec, vec);
881}
882
883/** @brief Called when the playlist has been updated */
884static void playlist_modify_updated(void attribute((unused)) *v,
885 const char *err) {
886 if(err)
887 popup_protocol_error(0, err);
888 disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
889}
890
891/** @brief Called when the playlist has been unlocked */
892static void playlist_modify_unlocked(void attribute((unused)) *v,
893 const char *err) {
894 if(err)
895 popup_protocol_error(0, err);
896}
897
898/* Drop tracks into a playlist ---------------------------------------------- */
899
900static void playlist_drop(struct queuelike attribute((unused)) *ql,
901 int ntracks,
902 char **tracks, char **ids,
903 struct queue_entry *after_me) {
904 struct playlist_modify_data *mod = xmalloc(sizeof *mod);
905
906 mod->playlist = playlist_picker_selected;
907 mod->modify = playlist_drop_modify;
908 mod->ntracks = ntracks;
909 mod->tracks = tracks;
910 mod->ids = ids;
911 mod->after_me = after_me;
7a3eb8eb
RK
912 disorder_eclient_playlist_lock(client, playlist_modify_locked,
913 mod->playlist, mod);
914}
915
a6712ea8
RK
916/** @brief Return true if track @p i is in the moved set */
917static int playlist_drop_is_moved(struct playlist_modify_data *mod,
918 int i) {
919 struct queue_entry *q;
920
a6712ea8
RK
921 /* Find the q corresponding to i, so we can get the ID */
922 for(q = ql_playlist.q; i; q = q->next, --i)
923 ;
a6712ea8
RK
924 /* See if track i matches any of the moved set by ID */
925 for(int n = 0; n < mod->ntracks; ++n)
8bbeb28e 926 if(!strcmp(q->id, mod->ids[n]))
a6712ea8 927 return 1;
a6712ea8
RK
928 return 0;
929}
930
7a3eb8eb
RK
931static void playlist_drop_modify(struct playlist_modify_data *mod,
932 int nvec, char **vec) {
933 char **newvec;
934 int nnewvec;
f57ff3aa 935
53ce677c 936 //fprintf(stderr, "\nplaylist_drop_modify\n");
a6712ea8
RK
937 /* after_me is the queue_entry to insert after, or NULL to insert at the
938 * beginning (including the case when the playlist is empty) */
53ce677c
RK
939 //fprintf(stderr, "after_me = %s\n",
940 // mod->after_me ? mod->after_me->track : "NULL");
a6712ea8
RK
941 struct queue_entry *q = ql_playlist.q;
942 int ins = 0;
943 if(mod->after_me) {
944 ++ins;
945 while(q && q != mod->after_me) {
946 q = q->next;
947 ++ins;
948 }
949 }
950 /* Now ins is the index to insert at; equivalently, the row to insert before,
951 * and so equal to nvec to append. */
8bbeb28e 952#if 0
a6712ea8
RK
953 fprintf(stderr, "ins = %d = %s\n",
954 ins, ins < nvec ? vec[ins] : "NULL");
d8efe0a3
RK
955 for(int n = 0; n < nvec; ++n)
956 fprintf(stderr, "%d: %s %s\n", n, n == ins ? "->" : " ", vec[n]);
a6712ea8 957 fprintf(stderr, "nvec = %d\n", nvec);
8bbeb28e 958#endif
7a3eb8eb
RK
959 if(mod->ids) {
960 /* This is a rearrangement */
d8efe0a3
RK
961 /* We have:
962 * - vec[], the current layout
963 * - ins, pointing into vec
964 * - mod->tracks[], a subset of vec[] which is to be moved
965 *
966 * ins is the insertion point BUT it is in terms of the whole
967 * array, i.e. before mod->tracks[] have been removed. The first
968 * step then is to remove everything in mod->tracks[] and adjust
969 * ins downwards as necessary.
970 */
971 /* First zero out anything that's moved */
972 int before_ins = 0;
973 for(int n = 0; n < nvec; ++n) {
974 if(playlist_drop_is_moved(mod, n)) {
975 vec[n] = NULL;
976 if(n < ins)
977 ++before_ins;
7a3eb8eb
RK
978 }
979 }
d8efe0a3
RK
980 /* Now collapse down the array */
981 int i = 0;
982 for(int n = 0; n < nvec; ++n) {
983 if(vec[n])
984 vec[i++] = vec[n];
985 }
986 assert(i + mod->ntracks == nvec);
987 nvec = i;
988 /* Adjust the insertion point to take account of things moved from before
989 * it */
990 ins -= before_ins;
991 /* The effect is now the same as an insertion */
7a3eb8eb 992 }
d8efe0a3
RK
993 /* This is (now) an insertion */
994 nnewvec = nvec + mod->ntracks;
995 newvec = xcalloc(nnewvec, sizeof (char *));
996 memcpy(newvec, vec,
997 ins * sizeof (char *));
998 memcpy(newvec + ins, mod->tracks,
999 mod->ntracks * sizeof (char *));
1000 memcpy(newvec + ins + mod->ntracks, vec + ins,
1001 (nvec - ins) * sizeof (char *));
7a3eb8eb
RK
1002 disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist,
1003 newvec, nnewvec, mod);
1004}
1005
b35f7c0f
RK
1006/* Playlist editor right-click menu ---------------------------------------- */
1007
1008/** @brief Called to determine whether the playlist is playable */
1009static int playlist_playall_sensitive(void attribute((unused)) *extra) {
1010 /* If there's no playlist obviously we can't play it */
1011 if(!playlist_picker_selected)
1012 return FALSE;
1013 /* If it's empty we can't play it */
1014 if(!ql_playlist.q)
1015 return FALSE;
1016 /* Otherwise we can */
1017 return TRUE;
1018}
1019
1020/** @brief Called to play the selected playlist */
1021static void playlist_playall_activate(GtkMenuItem attribute((unused)) *menuitem,
1022 gpointer attribute((unused)) user_data) {
1023 if(!playlist_picker_selected)
1024 return;
1025 /* Re-use the menu-based activation callback */
1026 disorder_eclient_playlist_get(client, playlist_menu_received_content,
1027 playlist_picker_selected, NULL);
1028}
1029
dba3eb8e
RK
1030/** @brief Called to determine whether the playlist is playable */
1031static int playlist_remove_sensitive(void attribute((unused)) *extra) {
1032 /* If there's no playlist obviously we can't remove from it */
1033 if(!playlist_picker_selected)
1034 return FALSE;
1035 /* If no tracks are selected we cannot remove them */
1036 if(!gtk_tree_selection_count_selected_rows(ql_playlist.selection))
1037 return FALSE;
1038 /* We're good to go */
1039 return TRUE;
1040}
1041
1042/** @brief Called to play the selected playlist */
1043static void playlist_remove_activate(GtkMenuItem attribute((unused)) *menuitem,
1044 gpointer attribute((unused)) user_data) {
f57ff3aa 1045 /* TODO backspace should work too */
dba3eb8e
RK
1046 if(!playlist_picker_selected)
1047 return;
7a3eb8eb 1048 struct playlist_modify_data *mod = xmalloc(sizeof *mod);
487eb32a 1049
7a3eb8eb
RK
1050 mod->playlist = playlist_picker_selected;
1051 mod->modify = playlist_remove_modify;
1052 disorder_eclient_playlist_lock(client, playlist_modify_locked,
1053 mod->playlist, mod);
487eb32a
RK
1054}
1055
7a3eb8eb
RK
1056static void playlist_remove_modify(struct playlist_modify_data *mod,
1057 int attribute((unused)) nvec, char **vec) {
487eb32a
RK
1058 GtkTreeIter iter[1];
1059 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql_playlist.store),
1060 iter);
7a3eb8eb 1061 int n = 0, m = 0;
487eb32a
RK
1062 while(it) {
1063 if(!gtk_tree_selection_iter_is_selected(ql_playlist.selection, iter))
1064 vec[m++] = vec[n++];
1065 else
1066 n++;
1067 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_playlist.store), iter);
1068 }
7a3eb8eb
RK
1069 disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist,
1070 vec, m, mod);
dba3eb8e
RK
1071}
1072
506e02d8
RK
1073/* Playlists window --------------------------------------------------------- */
1074
7de5fbb9 1075/** @brief Pop up the playlists window
121944d1
RK
1076 *
1077 * Called when the playlists menu item is selected
1078 */
f06a754c
RK
1079void playlist_window_create(gpointer attribute((unused)) callback_data,
1080 guint attribute((unused)) callback_action,
1081 GtkWidget attribute((unused)) *menu_item) {
f0bd437a 1082 /* If the window already exists, raise it */
7fe2807d
RK
1083 if(playlist_window) {
1084 gtk_window_present(GTK_WINDOW(playlist_window));
f0bd437a
RK
1085 return;
1086 }
1087 /* Create the window */
7fe2807d
RK
1088 playlist_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1089 gtk_widget_set_style(playlist_window, tool_style);
1090 g_signal_connect(playlist_window, "destroy",
1091 G_CALLBACK(playlist_window_destroyed), &playlist_window);
1092 gtk_window_set_title(GTK_WINDOW(playlist_window), "Playlists Management");
f0bd437a
RK
1093 /* TODO loads of this is very similar to (copied from!) users.c - can we
1094 * de-dupe? */
1095 /* Keyboard shortcuts */
7fe2807d
RK
1096 g_signal_connect(playlist_window, "key-press-event",
1097 G_CALLBACK(playlist_window_keypress), 0);
f0bd437a 1098 /* default size is too small */
9e239dff 1099 gtk_window_set_default_size(GTK_WINDOW(playlist_window), 640, 320);
f0bd437a 1100
506e02d8 1101 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
b5e60f0d 1102 gtk_box_pack_start(GTK_BOX(hbox), playlist_picker_create(),
506e02d8
RK
1103 FALSE/*expand*/, FALSE, 0);
1104 gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(),
1105 FALSE/*expand*/, FALSE, 2);
7fe2807d 1106 gtk_box_pack_start(GTK_BOX(hbox), playlists_editor_create(),
506e02d8 1107 TRUE/*expand*/, TRUE/*fill*/, 0);
f0bd437a 1108
7fe2807d
RK
1109 gtk_container_add(GTK_CONTAINER(playlist_window), frame_widget(hbox, NULL));
1110 gtk_widget_show_all(playlist_window);
f9b20469
RK
1111}
1112
c41b2cac
RK
1113/** @brief Keypress handler */
1114static gboolean playlist_window_keypress(GtkWidget attribute((unused)) *widget,
1115 GdkEventKey *event,
1116 gpointer attribute((unused)) user_data) {
1117 if(event->state)
1118 return FALSE;
1119 switch(event->keyval) {
1120 case GDK_Escape:
1121 gtk_widget_destroy(playlist_window);
1122 return TRUE;
1123 default:
1124 return FALSE;
1125 }
1126}
1127
1128/** @brief Called when the playlist window is destroyed */
1129static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget,
1130 GtkWidget **widget_pointer) {
1131 destroy_queuelike(&ql_playlist);
a544b1cc 1132 playlist_picker_destroy();
c41b2cac
RK
1133 *widget_pointer = NULL;
1134}
1135
fc36ecb7
RK
1136/** @brief Initialize playlist support */
1137void playlists_init(void) {
1138 /* We re-get all playlists upon any change... */
b5e60f0d 1139 event_register("playlist-created", playlist_list_update, 0);
b5e60f0d 1140 event_register("playlist-deleted", playlist_list_update, 0);
fc36ecb7 1141 /* ...and on reconnection */
b5e60f0d 1142 event_register("log-connected", playlist_list_update, 0);
fc36ecb7 1143 /* ...and from time to time */
b5e60f0d 1144 event_register("periodic-slow", playlist_list_update, 0);
fc36ecb7 1145 /* ...and at startup */
b5e60f0d 1146 playlist_list_update(0, 0, 0);
7c12e4bd
RK
1147
1148 /* Update the playlists menu when the set of playlists changes */
b5e60f0d 1149 event_register("playlists-updated", playlist_menu_changed, 0);
7c12e4bd
RK
1150 /* Update the new-playlist OK button when the set of playlists changes */
1151 event_register("playlists-updated", playlist_new_changed, 0);
d9b141cc 1152 /* Update the list of playlists in the edit window when the set changes */
b5e60f0d 1153 event_register("playlists-updated", playlist_picker_fill, 0);
7e702780 1154 /* Update the displayed playlist when it is modified */
7fe2807d 1155 event_register("playlist-modified", playlist_editor_fill, 0);
fc36ecb7
RK
1156}
1157
0b0fb26b
RK
1158#endif
1159
fc36ecb7
RK
1160/*
1161Local Variables:
1162c-basic-offset:2
1163comment-column:40
1164fill-column:79
1165indent-tabs-mode:nil
1166End:
1167*/