chiark / gitweb /
typo
[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
21 * @brief Playlist for Disobedience
22 *
f0bd437a
RK
23 * The playlists management window contains:
24 * - a list of all playlists
25 * - an add button
26 * - a delete button
27 * - a drag+drop capable view of the playlist
28 * - a close button
fc36ecb7
RK
29 */
30#include "disobedience.h"
6bfaa2a1
RK
31#include "queue-generic.h"
32#include "popup.h"
fc36ecb7 33
0b0fb26b
RK
34#if PLAYLISTS
35
fc36ecb7
RK
36static void playlists_updated(void *v,
37 const char *err,
38 int nvec, char **vec);
39
f0bd437a
RK
40/** @brief Playlist editing window */
41static GtkWidget *playlists_window;
42
43/** @brief Tree model for list of playlists */
44static GtkListStore *playlists_list;
45
46/** @brief Selection for list of playlists */
47static GtkTreeSelection *playlists_selection;
48
49/** @brief Currently selected playlist */
50static const char *playlists_selected;
51
52/** @brief Delete button */
53static GtkWidget *playlists_delete_button;
54
fc36ecb7
RK
55/** @brief Current list of playlists or NULL */
56char **playlists;
57
58/** @brief Count of playlists */
59int nplaylists;
60
6bfaa2a1
RK
61/** @brief Columns for the playlist editor */
62static const struct queue_column playlist_columns[] = {
63 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
64 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
65 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
66 { "Length", column_length, 0, COL_RIGHT }
67};
68
69/** @brief Pop-up menu for playlist editor */
70// TODO some of these may not be generic enough yet - check!
71static struct menuitem playlist_menuitems[] = {
72 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
73 { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 },
74 //{ "Play playlist", ql_playall_activate, ql_playall_sensitive, 0, 0 },
75 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
76 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
77 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
78};
79
80/** @brief Queuelike for editing a playlist */
81static struct queuelike ql_playlist = {
82 .name = "playlist",
83 .columns = playlist_columns,
84 .ncolumns = sizeof playlist_columns / sizeof *playlist_columns,
85 .menuitems = playlist_menuitems,
86 .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems,
87 //.drop = playlist_drop //TODO
88};
89
3c1a4e96 90/* Maintaining the list of playlists ---------------------------------------- */
121944d1 91
fc36ecb7
RK
92/** @brief Schedule an update to the list of playlists */
93static void playlists_update(const char attribute((unused)) *event,
94 void attribute((unused)) *eventdata,
95 void attribute((unused)) *callbackdata) {
96 disorder_eclient_playlists(client, playlists_updated, 0);
97}
98
99/** @brief qsort() callback for playlist name comparison */
100static int playlistcmp(const void *ap, const void *bp) {
101 const char *a = *(char **)ap, *b = *(char **)bp;
102 const char *ad = strchr(a, '.'), *bd = strchr(b, '.');
103 int c;
104
105 /* Group owned playlists by owner */
106 if(ad && bd) {
107 const int adn = ad - a, bdn = bd - b;
108 if((c = strncmp(a, b, adn < bdn ? adn : bdn)))
109 return c;
110 /* Lexical order within playlists of a single owner */
111 return strcmp(ad + 1, bd + 1);
112 }
113
114 /* Owned playlists after shared ones */
115 if(ad) {
116 return 1;
117 } else if(bd) {
118 return -1;
119 }
120
121 /* Lexical order of shared playlists */
122 return strcmp(a, b);
123}
124
125/** @brief Called with a new list of playlists */
126static void playlists_updated(void attribute((unused)) *v,
127 const char *err,
128 int nvec, char **vec) {
129 if(err) {
130 playlists = 0;
131 nplaylists = -1;
132 /* Probably means server does not support playlists */
133 } else {
134 playlists = vec;
135 nplaylists = nvec;
136 qsort(playlists, nplaylists, sizeof (char *), playlistcmp);
137 }
138 /* Tell our consumers */
139 event_raise("playlists-updated", 0);
140}
141
121944d1
RK
142/* Playlists menu ----------------------------------------------------------- */
143
144/** @brief Play received playlist contents
145 *
146 * Passed as a completion callback by menu_activate_playlist().
147 */
148static void playlist_play_content(void attribute((unused)) *v,
149 const char *err,
150 int nvec, char **vec) {
151 if(err) {
152 popup_protocol_error(0, err);
153 return;
154 }
155 for(int n = 0; n < nvec; ++n)
156 disorder_eclient_play(client, vec[n], NULL, NULL);
157}
158
159/** @brief Called to activate a playlist
160 *
161 * Called when the menu item for a playlist is clicked.
162 */
f9b20469
RK
163static void menu_activate_playlist(GtkMenuItem *menuitem,
164 gpointer attribute((unused)) user_data) {
165 GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
166 const char *playlist = gtk_label_get_text(label);
167
121944d1 168 disorder_eclient_playlist_get(client, playlist_play_content, playlist, NULL);
f9b20469
RK
169}
170
171/** @brief Called when the playlists change */
172static void menu_playlists_changed(const char attribute((unused)) *event,
173 void attribute((unused)) *eventdata,
174 void attribute((unused)) *callbackdata) {
175 if(!playlists_menu)
176 return; /* OMG too soon */
177 GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
178 /* TODO: we could be more sophisticated and only insert/remove widgets as
c5782050
RK
179 * needed. The current logic trashes the selection which is not acceptable
180 * and interacts badly with one playlist being currently locked and
181 * edited. */
f9b20469
RK
182 while(menu->children)
183 gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
184 /* NB nplaylists can be -1 as well as 0 */
185 for(int n = 0; n < nplaylists; ++n) {
186 GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]);
187 g_signal_connect(w, "activate", G_CALLBACK(menu_activate_playlist), 0);
188 gtk_widget_show(w);
189 gtk_menu_shell_append(menu, w);
190 }
fdea9f40 191 gtk_widget_set_sensitive(menu_playlists_widget,
f9b20469 192 nplaylists > 0);
fdea9f40 193 gtk_widget_set_sensitive(menu_editplaylists_widget,
6acdbba4 194 nplaylists >= 0);
f9b20469
RK
195}
196
121944d1
RK
197/* Playlists window (list of playlists) ------------------------------------- */
198
f0bd437a 199/** @brief (Re-)populate the playlist tree model */
506e02d8
RK
200static void playlists_fill(const char attribute((unused)) *event,
201 void attribute((unused)) *eventdata,
202 void attribute((unused)) *callbackdata) {
f0bd437a
RK
203 GtkTreeIter iter[1];
204
205 if(!playlists_list)
206 playlists_list = gtk_list_store_new(1, G_TYPE_STRING);
207 gtk_list_store_clear(playlists_list);
208 for(int n = 0; n < nplaylists; ++n)
209 gtk_list_store_insert_with_values(playlists_list, iter, n/*position*/,
210 0, playlists[n], /* column 0 */
211 -1); /* no more cols */
212 // TODO reselect whatever was formerly selected if possible, if not then
213 // zap the contents view
214}
215
216/** @brief Called when the selection might have changed */
217static void playlists_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
218 gpointer attribute((unused)) user_data) {
219 GtkTreeIter iter;
220 char *gselected, *selected;
221
222 /* Identify the current selection */
223 if(gtk_tree_selection_get_selected(playlists_selection, 0, &iter)) {
224 gtk_tree_model_get(GTK_TREE_MODEL(playlists_list), &iter,
225 0, &gselected, -1);
226 selected = xstrdup(gselected);
227 g_free(gselected);
228 } else
229 selected = 0;
230 /* Eliminate no-change cases */
231 if(!selected && !playlists_selected)
232 return;
233 if(selected && playlists_selected && !strcmp(selected, playlists_selected))
234 return;
235 /* There's been a change */
236 playlists_selected = selected;
237 if(playlists_selected) {
238 fprintf(stderr, "playlists selection changed\n'"); /* TODO */
239 gtk_widget_set_sensitive(playlists_delete_button, 1);
240 } else
241 gtk_widget_set_sensitive(playlists_delete_button, 0);
242}
243
244/** @brief Called when the 'add' button is pressed */
245static void playlists_add(GtkButton attribute((unused)) *button,
246 gpointer attribute((unused)) userdata) {
247 /* Unselect whatever is selected */
248 gtk_tree_selection_unselect_all(playlists_selection);
249 fprintf(stderr, "playlists_add\n");/* TODO */
c5782050
RK
250 /* We need to pop up a window asking for:
251 * - the name for the playlist
252 * - whether it is to be a public, private or shared playlist
253 * Moreover we should keep track of the known playlists and grey out OK
254 * if the name is a clash (as well as if it's actually invalid).
255 */
f0bd437a
RK
256}
257
258/** @brief Called when the 'Delete' button is pressed */
259static void playlists_delete(GtkButton attribute((unused)) *button,
c5782050 260 gpointer attribute((unused)) userdata) {
f0bd437a
RK
261 GtkWidget *yesno;
262 int res;
263
264 if(!playlists_selected)
265 return; /* shouldn't happen */
266 yesno = gtk_message_dialog_new(GTK_WINDOW(playlists_window),
267 GTK_DIALOG_MODAL,
268 GTK_MESSAGE_QUESTION,
269 GTK_BUTTONS_YES_NO,
c5782050 270 "Do you really want to delete playlist %s?"
f0bd437a
RK
271 " This action cannot be undone.",
272 playlists_selected);
273 res = gtk_dialog_run(GTK_DIALOG(yesno));
274 gtk_widget_destroy(yesno);
275 if(res == GTK_RESPONSE_YES) {
276 disorder_eclient_playlist_delete(client,
277 NULL/*playlists_delete_completed*/,
278 playlists_selected,
279 NULL);
280 }
281}
282
283/** @brief Table of buttons below the playlist list */
284static struct button playlists_buttons[] = {
285 {
286 GTK_STOCK_ADD,
287 playlists_add,
288 "Create a new playlist",
289 0
290 },
291 {
292 GTK_STOCK_REMOVE,
293 playlists_delete,
294 "Delete a playlist",
295 0
296 },
297};
298#define NPLAYLISTS_BUTTONS (sizeof playlists_buttons / sizeof *playlists_buttons)
299
506e02d8
RK
300/** @brief Create the list of playlists for the edit playlists window */
301static GtkWidget *playlists_window_list(void) {
302 /* Create the list of playlist and populate it */
303 playlists_fill(NULL, NULL, NULL);
304 /* Create the tree view */
305 GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlists_list));
306 /* ...and the renderers for it */
307 GtkCellRenderer *cr = gtk_cell_renderer_text_new();
308 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist",
309 cr,
310 "text", 0,
311 NULL);
312 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
313 /* Get the selection for the view; set its mode; arrange for a callback when
314 * it changes */
315 playlists_selected = NULL;
316 playlists_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
317 gtk_tree_selection_set_mode(playlists_selection, GTK_SELECTION_BROWSE);
318 g_signal_connect(playlists_selection, "changed",
319 G_CALLBACK(playlists_selection_changed), NULL);
320
321 /* Create the control buttons */
322 GtkWidget *buttons = create_buttons_box(playlists_buttons,
323 NPLAYLISTS_BUTTONS,
324 gtk_hbox_new(FALSE, 1));
325 playlists_delete_button = playlists_buttons[1].widget;
326
327 /* Buttons live below the list */
328 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
329 gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
330 gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
331
332 return vbox;
333}
334
335/* Playlists window (edit current playlist) --------------------------------- */
336
337static GtkWidget *playlists_window_edit(void) {
6bfaa2a1
RK
338 assert(ql_playlist.view == NULL); /* better not be set up already */
339 GtkWidget *w = init_queuelike(&ql_playlist);
340 return w;
506e02d8
RK
341}
342
343/* Playlists window --------------------------------------------------------- */
344
f0bd437a
RK
345/** @brief Keypress handler */
346static gboolean playlists_keypress(GtkWidget attribute((unused)) *widget,
347 GdkEventKey *event,
348 gpointer attribute((unused)) user_data) {
349 if(event->state)
350 return FALSE;
351 switch(event->keyval) {
352 case GDK_Escape:
353 gtk_widget_destroy(playlists_window);
354 return TRUE;
355 default:
356 return FALSE;
357 }
358}
359
6bfaa2a1
RK
360/** @brief Called when the playlist window is destroyed */
361static void playlists_window_destroyed(GtkWidget attribute((unused)) *widget,
362 GtkWidget **widget_pointer) {
363 fprintf(stderr, "playlists_window_destroy\n");
364 destroy_queuelike(&ql_playlist);
365 *widget_pointer = NULL;
366}
367
7de5fbb9 368/** @brief Pop up the playlists window
121944d1
RK
369 *
370 * Called when the playlists menu item is selected
371 */
f0bd437a 372void edit_playlists(gpointer attribute((unused)) callback_data,
f9b20469
RK
373 guint attribute((unused)) callback_action,
374 GtkWidget attribute((unused)) *menu_item) {
f0bd437a
RK
375 /* If the window already exists, raise it */
376 if(playlists_window) {
377 gtk_window_present(GTK_WINDOW(playlists_window));
378 return;
379 }
380 /* Create the window */
381 playlists_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
382 gtk_widget_set_style(playlists_window, tool_style);
383 g_signal_connect(playlists_window, "destroy",
6bfaa2a1 384 G_CALLBACK(playlists_window_destroyed), &playlists_window);
f0bd437a
RK
385 gtk_window_set_title(GTK_WINDOW(playlists_window), "Playlists Management");
386 /* TODO loads of this is very similar to (copied from!) users.c - can we
387 * de-dupe? */
388 /* Keyboard shortcuts */
389 g_signal_connect(playlists_window, "key-press-event",
390 G_CALLBACK(playlists_keypress), 0);
391 /* default size is too small */
392 gtk_window_set_default_size(GTK_WINDOW(playlists_window), 240, 240);
f0bd437a 393
506e02d8
RK
394 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
395 gtk_box_pack_start(GTK_BOX(hbox), playlists_window_list(),
396 FALSE/*expand*/, FALSE, 0);
397 gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(),
398 FALSE/*expand*/, FALSE, 2);
399 gtk_box_pack_start(GTK_BOX(hbox), playlists_window_edit(),
400 TRUE/*expand*/, TRUE/*fill*/, 0);
f0bd437a 401
f0bd437a
RK
402 gtk_container_add(GTK_CONTAINER(playlists_window), frame_widget(hbox, NULL));
403 gtk_widget_show_all(playlists_window);
f9b20469
RK
404}
405
fc36ecb7
RK
406/** @brief Initialize playlist support */
407void playlists_init(void) {
408 /* We re-get all playlists upon any change... */
409 event_register("playlist-created", playlists_update, 0);
410 event_register("playlist-modified", playlists_update, 0);
411 event_register("playlist-deleted", playlists_update, 0);
412 /* ...and on reconnection */
413 event_register("log-connected", playlists_update, 0);
414 /* ...and from time to time */
415 event_register("periodic-slow", playlists_update, 0);
416 /* ...and at startup */
f9b20469 417 event_register("playlists-updated", menu_playlists_changed, 0);
fc36ecb7
RK
418 playlists_update(0, 0, 0);
419}
420
0b0fb26b
RK
421#endif
422
fc36ecb7
RK
423/*
424Local Variables:
425c-basic-offset:2
426comment-column:40
427fill-column:79
428indent-tabs-mode:nil
429End:
430*/