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