2 * This file is part of DisOrder
3 * Copyright (C) 2008, 2009 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/playlists.c
21 * @brief Playlist for Disobedience
23 * The playlists management window contains:
24 * - a list of all playlists
27 * - a drag+drop capable view of the playlist
30 #include "disobedience.h"
31 #include "queue-generic.h"
37 static void playlists_updated(void *v,
39 int nvec, char **vec);
41 /** @brief Playlist editing window */
42 static GtkWidget *playlists_window;
44 /** @brief Tree model for list of playlists */
45 static GtkListStore *playlists_list;
47 /** @brief Selection for list of playlists */
48 static GtkTreeSelection *playlists_selection;
50 /** @brief Currently selected playlist */
51 static const char *playlists_selected;
53 /** @brief Delete button */
54 static GtkWidget *playlists_delete_button;
56 /** @brief Current list of playlists or NULL */
59 /** @brief Count of playlists */
62 /** @brief Columns for the playlist editor */
63 static const struct queue_column playlist_columns[] = {
64 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
65 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
66 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
67 { "Length", column_length, 0, COL_RIGHT }
70 /** @brief Pop-up menu for playlist editor */
71 // TODO some of these may not be generic enough yet - check!
72 static struct menuitem playlist_menuitems[] = {
73 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
74 { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 },
75 //{ "Play playlist", ql_playall_activate, ql_playall_sensitive, 0, 0 },
76 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
77 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
78 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
81 /** @brief Queuelike for editing a playlist */
82 static struct queuelike ql_playlist = {
84 .columns = playlist_columns,
85 .ncolumns = sizeof playlist_columns / sizeof *playlist_columns,
86 .menuitems = playlist_menuitems,
87 .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems,
88 //.drop = playlist_drop //TODO
91 /* Maintaining the list of playlists ---------------------------------------- */
93 /** @brief Schedule an update to the list of playlists */
94 static void playlists_update(const char attribute((unused)) *event,
95 void attribute((unused)) *eventdata,
96 void attribute((unused)) *callbackdata) {
97 disorder_eclient_playlists(client, playlists_updated, 0);
100 /** @brief qsort() callback for playlist name comparison */
101 static int playlistcmp(const void *ap, const void *bp) {
102 const char *a = *(char **)ap, *b = *(char **)bp;
103 const char *ad = strchr(a, '.'), *bd = strchr(b, '.');
106 /* Group owned playlists by owner */
108 const int adn = ad - a, bdn = bd - b;
109 if((c = strncmp(a, b, adn < bdn ? adn : bdn)))
111 /* Lexical order within playlists of a single owner */
112 return strcmp(ad + 1, bd + 1);
115 /* Owned playlists after shared ones */
122 /* Lexical order of shared playlists */
126 /** @brief Called with a new list of playlists */
127 static void playlists_updated(void attribute((unused)) *v,
129 int nvec, char **vec) {
133 /* Probably means server does not support playlists */
137 qsort(playlists, nplaylists, sizeof (char *), playlistcmp);
139 /* Tell our consumers */
140 event_raise("playlists-updated", 0);
143 /* Playlists menu ----------------------------------------------------------- */
145 /** @brief Play received playlist contents
147 * Passed as a completion callback by menu_activate_playlist().
149 static void playlist_play_content(void attribute((unused)) *v,
151 int nvec, char **vec) {
153 popup_protocol_error(0, err);
156 for(int n = 0; n < nvec; ++n)
157 disorder_eclient_play(client, vec[n], NULL, NULL);
160 /** @brief Called to activate a playlist
162 * Called when the menu item for a playlist is clicked.
164 static void menu_activate_playlist(GtkMenuItem *menuitem,
165 gpointer attribute((unused)) user_data) {
166 GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
167 const char *playlist = gtk_label_get_text(label);
169 disorder_eclient_playlist_get(client, playlist_play_content, playlist, NULL);
172 /** @brief Called when the playlists change */
173 static void menu_playlists_changed(const char attribute((unused)) *event,
174 void attribute((unused)) *eventdata,
175 void attribute((unused)) *callbackdata) {
177 return; /* OMG too soon */
178 GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
179 /* TODO: we could be more sophisticated and only insert/remove widgets as
180 * needed. The current logic trashes the selection which is not acceptable
181 * and interacts badly with one playlist being currently locked and
183 while(menu->children)
184 gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
185 /* NB nplaylists can be -1 as well as 0 */
186 for(int n = 0; n < nplaylists; ++n) {
187 GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]);
188 g_signal_connect(w, "activate", G_CALLBACK(menu_activate_playlist), 0);
190 gtk_menu_shell_append(menu, w);
192 gtk_widget_set_sensitive(menu_playlists_widget,
194 gtk_widget_set_sensitive(menu_editplaylists_widget,
198 /* Popup to create a new playlist ------------------------------------------- */
200 /** @brief New-playlist popup */
201 static GtkWidget *playlist_new_window;
203 /** @brief Text entry in new-playlist popup */
204 static GtkWidget *playlist_new_entry;
206 static GtkWidget *playlist_new_info;
208 static GtkWidget *playlist_new_shared;
209 static GtkWidget *playlist_new_public;
210 static GtkWidget *playlist_new_private;
212 /** @brief Called when 'ok' is clicked in new-playlist popup */
213 static void playlist_new_ok(GtkButton attribute((unused)) *button,
214 gpointer attribute((unused)) userdata) {
217 /** @brief Called when 'cancel' is clicked in new-playlist popup */
218 static void playlist_new_cancel(GtkButton attribute((unused)) *button,
219 gpointer attribute((unused)) userdata) {
220 gtk_widget_destroy(playlist_new_window);
223 /** @brief Buttons for new-playlist popup */
224 static struct button playlist_new_buttons[] = {
226 .stock = GTK_STOCK_OK,
227 .clicked = playlist_new_ok,
228 .tip = "Create new playlist"
231 .stock = GTK_STOCK_CANCEL,
232 .clicked = playlist_new_cancel,
233 .tip = "Do not create new plalist"
236 #define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons)
238 /** @brief Test whether the new-playlist window settings are valid */
239 static const char *playlist_new_valid(void) {
240 gboolean shared, public, private;
241 g_object_get(playlist_new_shared, "active", &shared, (char *)NULL);
242 g_object_get(playlist_new_public, "active", &public, (char *)NULL);
243 g_object_get(playlist_new_private, "active", &private, (char *)NULL);
244 if(!(shared || public || private))
245 return "No type set.";
246 char *name = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry),
250 if(!valid_username(name))
251 return "Not a valid playlist name.";
252 /* Construct the full form of the name */
257 byte_xasprintf(&fullname, "%s.%s", config->username, name);
258 /* See if the result is valid */
259 if(playlist_parse_name(fullname, NULL, NULL))
260 return "Not a valid playlist name.";
261 /* See if the result clashes with an existing name */
262 for(int n = 0; n < nplaylists; ++n)
263 if(!strcmp(playlists[n], fullname)) {
265 return "A shared playlist with that name already exists.";
267 return "You already have a playlist with that name.";
269 /* As far as we can tell creation would work */
273 /** @brief Called when anything in the new-playlist popup changes
275 * We use this to set the activation state of the OK button.
277 static void playlist_new_changed(void) {
278 const char *reason = playlist_new_valid();
279 gtk_widget_set_sensitive(playlist_new_buttons[0].widget,
281 gtk_label_set_text(GTK_LABEL(playlist_new_info), reason);
284 /** @brief Called when some radio button in the new-playlist popup changes */
285 static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) tb,
286 gpointer attribute((unused)) userdata) {
287 playlist_new_changed();
290 /** @brief Called when the text entry field in the new-playlist popup changes */
291 static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable,
292 gpointer attribute((unused)) user_data) {
293 playlist_new_changed();
296 /** @brief Pop up a new window to enter the playlist name and details */
297 static void playlist_new(void) {
298 assert(playlist_new_window == NULL);
299 playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
300 g_signal_connect(playlist_new_window, "destroy",
301 G_CALLBACK(gtk_widget_destroyed), &playlist_new_window);
302 gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist");
303 /* Window will be modal, suppressing access to other windows */
304 gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE);
306 /* Window contents will use a table (grid) layout */
307 GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/);
309 /* First row: playlist name */
310 gtk_table_attach_defaults(GTK_TABLE(table),
311 gtk_label_new("Playlist name"),
313 playlist_new_entry = gtk_entry_new();
314 g_signal_connect(playlist_new_entry, "changed",
315 G_CALLBACK(playlist_new_entry_edited), NULL);
316 gtk_table_attach_defaults(GTK_TABLE(table),
320 /* Second row: radio buttons to choose type */
321 playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared");
323 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared),
326 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared),
328 g_signal_connect(playlist_new_shared, "toggled",
329 G_CALLBACK(playlist_new_button_toggled), NULL);
330 g_signal_connect(playlist_new_public, "toggled",
331 G_CALLBACK(playlist_new_button_toggled), NULL);
332 g_signal_connect(playlist_new_private, "toggled",
333 G_CALLBACK(playlist_new_button_toggled), NULL);
334 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2);
335 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2);
336 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2);
338 /* Third row: info bar saying why not */
339 playlist_new_info = gtk_label_new("");
340 gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info,
343 /* Fourth row: ok/cancel buttons */
344 GtkWidget *hbox = create_buttons_box(playlist_new_buttons,
345 NPLAYLIST_NEW_BUTTONS,
346 gtk_hbox_new(FALSE, 0));
347 gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4);
349 gtk_container_add(GTK_CONTAINER(playlist_new_window),
350 frame_widget(table, NULL));
352 /* Set initial state of OK button */
353 playlist_new_changed();
355 /* Display the window */
356 gtk_widget_show_all(playlist_new_window);
359 /* Playlists window (list of playlists) ------------------------------------- */
361 /** @brief (Re-)populate the playlist tree model */
362 static void playlists_fill(const char attribute((unused)) *event,
363 void attribute((unused)) *eventdata,
364 void attribute((unused)) *callbackdata) {
368 playlists_list = gtk_list_store_new(1, G_TYPE_STRING);
369 gtk_list_store_clear(playlists_list);
370 for(int n = 0; n < nplaylists; ++n)
371 gtk_list_store_insert_with_values(playlists_list, iter, n/*position*/,
372 0, playlists[n], /* column 0 */
373 -1); /* no more cols */
374 // TODO reselect whatever was formerly selected if possible, if not then
375 // zap the contents view
378 /** @brief Called when the selection might have changed */
379 static void playlists_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
380 gpointer attribute((unused)) user_data) {
382 char *gselected, *selected;
384 /* Identify the current selection */
385 if(gtk_tree_selection_get_selected(playlists_selection, 0, &iter)) {
386 gtk_tree_model_get(GTK_TREE_MODEL(playlists_list), &iter,
388 selected = xstrdup(gselected);
392 /* Eliminate no-change cases */
393 if(!selected && !playlists_selected)
395 if(selected && playlists_selected && !strcmp(selected, playlists_selected))
397 /* There's been a change */
398 playlists_selected = selected;
399 if(playlists_selected) {
400 fprintf(stderr, "playlists selection changed\n'"); /* TODO */
401 gtk_widget_set_sensitive(playlists_delete_button, 1);
403 gtk_widget_set_sensitive(playlists_delete_button, 0);
406 /** @brief Called when the 'add' button is pressed */
407 static void playlists_add(GtkButton attribute((unused)) *button,
408 gpointer attribute((unused)) userdata) {
409 /* Unselect whatever is selected */
410 gtk_tree_selection_unselect_all(playlists_selection);
411 fprintf(stderr, "playlists_add\n");/* TODO */
415 /** @brief Called when the 'Delete' button is pressed */
416 static void playlists_delete(GtkButton attribute((unused)) *button,
417 gpointer attribute((unused)) userdata) {
421 if(!playlists_selected)
422 return; /* shouldn't happen */
423 yesno = gtk_message_dialog_new(GTK_WINDOW(playlists_window),
425 GTK_MESSAGE_QUESTION,
427 "Do you really want to delete playlist %s?"
428 " This action cannot be undone.",
430 res = gtk_dialog_run(GTK_DIALOG(yesno));
431 gtk_widget_destroy(yesno);
432 if(res == GTK_RESPONSE_YES) {
433 disorder_eclient_playlist_delete(client,
434 NULL/*playlists_delete_completed*/,
440 /** @brief Table of buttons below the playlist list */
441 static struct button playlists_buttons[] = {
445 "Create a new playlist",
455 #define NPLAYLISTS_BUTTONS (sizeof playlists_buttons / sizeof *playlists_buttons)
457 /** @brief Create the list of playlists for the edit playlists window */
458 static GtkWidget *playlists_window_list(void) {
459 /* Create the list of playlist and populate it */
460 playlists_fill(NULL, NULL, NULL);
461 /* Create the tree view */
462 GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlists_list));
463 /* ...and the renderers for it */
464 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
465 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist",
469 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
470 /* Get the selection for the view; set its mode; arrange for a callback when
472 playlists_selected = NULL;
473 playlists_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
474 gtk_tree_selection_set_mode(playlists_selection, GTK_SELECTION_BROWSE);
475 g_signal_connect(playlists_selection, "changed",
476 G_CALLBACK(playlists_selection_changed), NULL);
478 /* Create the control buttons */
479 GtkWidget *buttons = create_buttons_box(playlists_buttons,
481 gtk_hbox_new(FALSE, 1));
482 playlists_delete_button = playlists_buttons[1].widget;
484 /* Buttons live below the list */
485 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
486 gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
487 gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
492 /* Playlists window (edit current playlist) --------------------------------- */
494 static GtkWidget *playlists_window_edit(void) {
495 assert(ql_playlist.view == NULL); /* better not be set up already */
496 GtkWidget *w = init_queuelike(&ql_playlist);
500 /* Playlists window --------------------------------------------------------- */
502 /** @brief Keypress handler */
503 static gboolean playlists_keypress(GtkWidget attribute((unused)) *widget,
505 gpointer attribute((unused)) user_data) {
508 switch(event->keyval) {
510 gtk_widget_destroy(playlists_window);
517 /** @brief Called when the playlist window is destroyed */
518 static void playlists_window_destroyed(GtkWidget attribute((unused)) *widget,
519 GtkWidget **widget_pointer) {
520 destroy_queuelike(&ql_playlist);
521 *widget_pointer = NULL;
524 /** @brief Pop up the playlists window
526 * Called when the playlists menu item is selected
528 void edit_playlists(gpointer attribute((unused)) callback_data,
529 guint attribute((unused)) callback_action,
530 GtkWidget attribute((unused)) *menu_item) {
531 /* If the window already exists, raise it */
532 if(playlists_window) {
533 gtk_window_present(GTK_WINDOW(playlists_window));
536 /* Create the window */
537 playlists_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
538 gtk_widget_set_style(playlists_window, tool_style);
539 g_signal_connect(playlists_window, "destroy",
540 G_CALLBACK(playlists_window_destroyed), &playlists_window);
541 gtk_window_set_title(GTK_WINDOW(playlists_window), "Playlists Management");
542 /* TODO loads of this is very similar to (copied from!) users.c - can we
544 /* Keyboard shortcuts */
545 g_signal_connect(playlists_window, "key-press-event",
546 G_CALLBACK(playlists_keypress), 0);
547 /* default size is too small */
548 gtk_window_set_default_size(GTK_WINDOW(playlists_window), 512, 240);
550 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
551 gtk_box_pack_start(GTK_BOX(hbox), playlists_window_list(),
552 FALSE/*expand*/, FALSE, 0);
553 gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(),
554 FALSE/*expand*/, FALSE, 2);
555 gtk_box_pack_start(GTK_BOX(hbox), playlists_window_edit(),
556 TRUE/*expand*/, TRUE/*fill*/, 0);
558 gtk_container_add(GTK_CONTAINER(playlists_window), frame_widget(hbox, NULL));
559 gtk_widget_show_all(playlists_window);
562 /** @brief Initialize playlist support */
563 void playlists_init(void) {
564 /* We re-get all playlists upon any change... */
565 event_register("playlist-created", playlists_update, 0);
566 event_register("playlist-modified", playlists_update, 0);
567 event_register("playlist-deleted", playlists_update, 0);
568 /* ...and on reconnection */
569 event_register("log-connected", playlists_update, 0);
570 /* ...and from time to time */
571 event_register("periodic-slow", playlists_update, 0);
572 /* ...and at startup */
573 event_register("playlists-updated", menu_playlists_changed, 0);
574 playlists_update(0, 0, 0);