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"
34 static void playlists_updated(void *v,
36 int nvec, char **vec);
38 /** @brief Playlist editing window */
39 static GtkWidget *playlists_window;
41 /** @brief Tree model for list of playlists */
42 static GtkListStore *playlists_list;
44 /** @brief Selection for list of playlists */
45 static GtkTreeSelection *playlists_selection;
47 /** @brief Currently selected playlist */
48 static const char *playlists_selected;
50 /** @brief Delete button */
51 static GtkWidget *playlists_delete_button;
53 /** @brief Current list of playlists or NULL */
56 /** @brief Count of playlists */
59 /* Maintianng the list of playlists ----------------------------------------- */
61 /** @brief Schedule an update to the list of playlists */
62 static void playlists_update(const char attribute((unused)) *event,
63 void attribute((unused)) *eventdata,
64 void attribute((unused)) *callbackdata) {
65 disorder_eclient_playlists(client, playlists_updated, 0);
68 /** @brief qsort() callback for playlist name comparison */
69 static int playlistcmp(const void *ap, const void *bp) {
70 const char *a = *(char **)ap, *b = *(char **)bp;
71 const char *ad = strchr(a, '.'), *bd = strchr(b, '.');
74 /* Group owned playlists by owner */
76 const int adn = ad - a, bdn = bd - b;
77 if((c = strncmp(a, b, adn < bdn ? adn : bdn)))
79 /* Lexical order within playlists of a single owner */
80 return strcmp(ad + 1, bd + 1);
83 /* Owned playlists after shared ones */
90 /* Lexical order of shared playlists */
94 /** @brief Called with a new list of playlists */
95 static void playlists_updated(void attribute((unused)) *v,
97 int nvec, char **vec) {
101 /* Probably means server does not support playlists */
105 qsort(playlists, nplaylists, sizeof (char *), playlistcmp);
107 /* Tell our consumers */
108 event_raise("playlists-updated", 0);
111 /* Playlists menu ----------------------------------------------------------- */
113 /** @brief Play received playlist contents
115 * Passed as a completion callback by menu_activate_playlist().
117 static void playlist_play_content(void attribute((unused)) *v,
119 int nvec, char **vec) {
121 popup_protocol_error(0, err);
124 for(int n = 0; n < nvec; ++n)
125 disorder_eclient_play(client, vec[n], NULL, NULL);
128 /** @brief Called to activate a playlist
130 * Called when the menu item for a playlist is clicked.
132 static void menu_activate_playlist(GtkMenuItem *menuitem,
133 gpointer attribute((unused)) user_data) {
134 GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
135 const char *playlist = gtk_label_get_text(label);
137 disorder_eclient_playlist_get(client, playlist_play_content, playlist, NULL);
140 /** @brief Called when the playlists change */
141 static void menu_playlists_changed(const char attribute((unused)) *event,
142 void attribute((unused)) *eventdata,
143 void attribute((unused)) *callbackdata) {
145 return; /* OMG too soon */
146 GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
147 /* TODO: we could be more sophisticated and only insert/remove widgets as
148 * needed. The current logic trashes the selection which is not acceptable
149 * and interacts badly with one playlist being currently locked and
151 while(menu->children)
152 gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
153 /* NB nplaylists can be -1 as well as 0 */
154 for(int n = 0; n < nplaylists; ++n) {
155 GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]);
156 g_signal_connect(w, "activate", G_CALLBACK(menu_activate_playlist), 0);
158 gtk_menu_shell_append(menu, w);
160 gtk_widget_set_sensitive(menu_playlists_widget,
162 gtk_widget_set_sensitive(menu_editplaylists_widget,
166 /* Playlists window (list of playlists) ------------------------------------- */
168 /** @brief (Re-)populate the playlist tree model */
169 static void playlists_fill(const char attribute((unused)) *event,
170 void attribute((unused)) *eventdata,
171 void attribute((unused)) *callbackdata) {
175 playlists_list = gtk_list_store_new(1, G_TYPE_STRING);
176 gtk_list_store_clear(playlists_list);
177 for(int n = 0; n < nplaylists; ++n)
178 gtk_list_store_insert_with_values(playlists_list, iter, n/*position*/,
179 0, playlists[n], /* column 0 */
180 -1); /* no more cols */
181 // TODO reselect whatever was formerly selected if possible, if not then
182 // zap the contents view
185 /** @brief Called when the selection might have changed */
186 static void playlists_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
187 gpointer attribute((unused)) user_data) {
189 char *gselected, *selected;
191 /* Identify the current selection */
192 if(gtk_tree_selection_get_selected(playlists_selection, 0, &iter)) {
193 gtk_tree_model_get(GTK_TREE_MODEL(playlists_list), &iter,
195 selected = xstrdup(gselected);
199 /* Eliminate no-change cases */
200 if(!selected && !playlists_selected)
202 if(selected && playlists_selected && !strcmp(selected, playlists_selected))
204 /* There's been a change */
205 playlists_selected = selected;
206 if(playlists_selected) {
207 fprintf(stderr, "playlists selection changed\n'"); /* TODO */
208 gtk_widget_set_sensitive(playlists_delete_button, 1);
210 gtk_widget_set_sensitive(playlists_delete_button, 0);
213 /** @brief Called when the 'add' button is pressed */
214 static void playlists_add(GtkButton attribute((unused)) *button,
215 gpointer attribute((unused)) userdata) {
216 /* Unselect whatever is selected */
217 gtk_tree_selection_unselect_all(playlists_selection);
218 fprintf(stderr, "playlists_add\n");/* TODO */
219 /* We need to pop up a window asking for:
220 * - the name for the playlist
221 * - whether it is to be a public, private or shared playlist
222 * Moreover we should keep track of the known playlists and grey out OK
223 * if the name is a clash (as well as if it's actually invalid).
227 /** @brief Called when the 'Delete' button is pressed */
228 static void playlists_delete(GtkButton attribute((unused)) *button,
229 gpointer attribute((unused)) userdata) {
233 if(!playlists_selected)
234 return; /* shouldn't happen */
235 yesno = gtk_message_dialog_new(GTK_WINDOW(playlists_window),
237 GTK_MESSAGE_QUESTION,
239 "Do you really want to delete playlist %s?"
240 " This action cannot be undone.",
242 res = gtk_dialog_run(GTK_DIALOG(yesno));
243 gtk_widget_destroy(yesno);
244 if(res == GTK_RESPONSE_YES) {
245 disorder_eclient_playlist_delete(client,
246 NULL/*playlists_delete_completed*/,
252 /** @brief Table of buttons below the playlist list */
253 static struct button playlists_buttons[] = {
257 "Create a new playlist",
267 #define NPLAYLISTS_BUTTONS (sizeof playlists_buttons / sizeof *playlists_buttons)
269 /** @brief Create the list of playlists for the edit playlists window */
270 static GtkWidget *playlists_window_list(void) {
271 /* Create the list of playlist and populate it */
272 playlists_fill(NULL, NULL, NULL);
273 /* Create the tree view */
274 GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlists_list));
275 /* ...and the renderers for it */
276 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
277 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist",
281 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
282 /* Get the selection for the view; set its mode; arrange for a callback when
284 playlists_selected = NULL;
285 playlists_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
286 gtk_tree_selection_set_mode(playlists_selection, GTK_SELECTION_BROWSE);
287 g_signal_connect(playlists_selection, "changed",
288 G_CALLBACK(playlists_selection_changed), NULL);
290 /* Create the control buttons */
291 GtkWidget *buttons = create_buttons_box(playlists_buttons,
293 gtk_hbox_new(FALSE, 1));
294 playlists_delete_button = playlists_buttons[1].widget;
296 /* Buttons live below the list */
297 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
298 gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
299 gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
304 /* Playlists window (edit current playlist) --------------------------------- */
306 static GtkWidget *playlists_window_edit(void) {
310 /* Playlists window --------------------------------------------------------- */
312 /** @brief Keypress handler */
313 static gboolean playlists_keypress(GtkWidget attribute((unused)) *widget,
315 gpointer attribute((unused)) user_data) {
318 switch(event->keyval) {
320 gtk_widget_destroy(playlists_window);
327 /** @brief Pop up the playlists window
329 * Called when the playlists menu item is selected
331 void edit_playlists(gpointer attribute((unused)) callback_data,
332 guint attribute((unused)) callback_action,
333 GtkWidget attribute((unused)) *menu_item) {
334 /* If the window already exists, raise it */
335 if(playlists_window) {
336 gtk_window_present(GTK_WINDOW(playlists_window));
339 /* Create the window */
340 playlists_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
341 gtk_widget_set_style(playlists_window, tool_style);
342 g_signal_connect(playlists_window, "destroy",
343 G_CALLBACK(gtk_widget_destroyed), &playlists_window);
344 gtk_window_set_title(GTK_WINDOW(playlists_window), "Playlists Management");
345 /* TODO loads of this is very similar to (copied from!) users.c - can we
347 /* Keyboard shortcuts */
348 g_signal_connect(playlists_window, "key-press-event",
349 G_CALLBACK(playlists_keypress), 0);
350 /* default size is too small */
351 gtk_window_set_default_size(GTK_WINDOW(playlists_window), 240, 240);
353 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
354 gtk_box_pack_start(GTK_BOX(hbox), playlists_window_list(),
355 FALSE/*expand*/, FALSE, 0);
356 gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(),
357 FALSE/*expand*/, FALSE, 2);
358 gtk_box_pack_start(GTK_BOX(hbox), playlists_window_edit(),
359 TRUE/*expand*/, TRUE/*fill*/, 0);
361 gtk_container_add(GTK_CONTAINER(playlists_window), frame_widget(hbox, NULL));
362 gtk_widget_show_all(playlists_window);
365 /** @brief Initialize playlist support */
366 void playlists_init(void) {
367 /* We re-get all playlists upon any change... */
368 event_register("playlist-created", playlists_update, 0);
369 event_register("playlist-modified", playlists_update, 0);
370 event_register("playlist-deleted", playlists_update, 0);
371 /* ...and on reconnection */
372 event_register("log-connected", playlists_update, 0);
373 /* ...and from time to time */
374 event_register("periodic-slow", playlists_update, 0);
375 /* ...and at startup */
376 event_register("playlists-updated", menu_playlists_changed, 0);
377 playlists_update(0, 0, 0);