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