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); | |
7a3eb8eb RK |
89 | static void playlist_modify_locked(void *v, const char *err); |
90 | void playlist_modify_retrieved(void *v, const char *err, | |
487eb32a RK |
91 | int nvec, |
92 | char **vec); | |
7a3eb8eb RK |
93 | static void playlist_modify_updated(void *v, const char *err); |
94 | static void playlist_modify_unlocked(void *v, const char *err); | |
95 | static void playlist_drop(struct queuelike *ql, | |
96 | int ntracks, | |
97 | char **tracks, char **ids, | |
98 | struct queue_entry *after_me); | |
99 | struct playlist_modify_data; | |
100 | static void playlist_drop_modify(struct playlist_modify_data *mod, | |
101 | int nvec, char **vec); | |
102 | static void playlist_remove_modify(struct playlist_modify_data *mod, | |
103 | int nvec, char **vec); | |
fc36ecb7 | 104 | |
f0bd437a | 105 | /** @brief Playlist editing window */ |
7fe2807d | 106 | static GtkWidget *playlist_window; |
f0bd437a | 107 | |
6bfaa2a1 RK |
108 | /** @brief Columns for the playlist editor */ |
109 | static const struct queue_column playlist_columns[] = { | |
110 | { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE }, | |
111 | { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE }, | |
112 | { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE }, | |
113 | { "Length", column_length, 0, COL_RIGHT } | |
114 | }; | |
115 | ||
dba3eb8e RK |
116 | /** @brief Pop-up menu for playlist editor |
117 | * | |
118 | * Status: | |
119 | * - track properties works but, bizarrely, raises the main window | |
120 | * - play track works | |
121 | * - play playlist works | |
122 | * - select/deselect all work | |
123 | */ | |
6bfaa2a1 RK |
124 | static struct menuitem playlist_menuitems[] = { |
125 | { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 }, | |
126 | { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 }, | |
b35f7c0f | 127 | { "Play playlist", playlist_playall_activate, playlist_playall_sensitive, 0, 0 }, |
dba3eb8e | 128 | { "Remove track from queue", playlist_remove_activate, playlist_remove_sensitive, 0, 0 }, |
6bfaa2a1 RK |
129 | { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 }, |
130 | { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 }, | |
131 | }; | |
132 | ||
133 | /** @brief Queuelike for editing a playlist */ | |
134 | static struct queuelike ql_playlist = { | |
135 | .name = "playlist", | |
136 | .columns = playlist_columns, | |
137 | .ncolumns = sizeof playlist_columns / sizeof *playlist_columns, | |
138 | .menuitems = playlist_menuitems, | |
139 | .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems, | |
7a3eb8eb | 140 | .drop = playlist_drop |
6bfaa2a1 RK |
141 | }; |
142 | ||
3c1a4e96 | 143 | /* Maintaining the list of playlists ---------------------------------------- */ |
121944d1 | 144 | |
b5e60f0d RK |
145 | /** @brief Current list of playlists or NULL */ |
146 | char **playlists; | |
147 | ||
148 | /** @brief Count of playlists */ | |
149 | int nplaylists; | |
150 | ||
151 | /** @brief Schedule an update to the list of playlists | |
152 | * | |
153 | * Called periodically and when a playlist is created or deleted. | |
154 | */ | |
155 | static void playlist_list_update(const char attribute((unused)) *event, | |
156 | void attribute((unused)) *eventdata, | |
157 | void attribute((unused)) *callbackdata) { | |
158 | disorder_eclient_playlists(client, playlist_list_received_playlists, 0); | |
fc36ecb7 RK |
159 | } |
160 | ||
c41b2cac RK |
161 | /** @brief Called with a new list of playlists */ |
162 | static void playlist_list_received_playlists(void attribute((unused)) *v, | |
163 | const char *err, | |
164 | int nvec, char **vec) { | |
165 | if(err) { | |
166 | playlists = 0; | |
167 | nplaylists = -1; | |
168 | /* Probably means server does not support playlists */ | |
169 | } else { | |
170 | playlists = vec; | |
171 | nplaylists = nvec; | |
172 | qsort(playlists, nplaylists, sizeof (char *), playlistcmp); | |
173 | } | |
174 | /* Tell our consumers */ | |
175 | event_raise("playlists-updated", 0); | |
176 | } | |
177 | ||
fc36ecb7 RK |
178 | /** @brief qsort() callback for playlist name comparison */ |
179 | static int playlistcmp(const void *ap, const void *bp) { | |
180 | const char *a = *(char **)ap, *b = *(char **)bp; | |
181 | const char *ad = strchr(a, '.'), *bd = strchr(b, '.'); | |
182 | int c; | |
183 | ||
184 | /* Group owned playlists by owner */ | |
185 | if(ad && bd) { | |
186 | const int adn = ad - a, bdn = bd - b; | |
187 | if((c = strncmp(a, b, adn < bdn ? adn : bdn))) | |
188 | return c; | |
189 | /* Lexical order within playlists of a single owner */ | |
190 | return strcmp(ad + 1, bd + 1); | |
191 | } | |
192 | ||
193 | /* Owned playlists after shared ones */ | |
194 | if(ad) { | |
195 | return 1; | |
196 | } else if(bd) { | |
197 | return -1; | |
198 | } | |
199 | ||
200 | /* Lexical order of shared playlists */ | |
201 | return strcmp(a, b); | |
202 | } | |
203 | ||
121944d1 RK |
204 | /* Playlists menu ----------------------------------------------------------- */ |
205 | ||
b35f7c0f RK |
206 | static void playlist_menu_playing(void attribute((unused)) *v, |
207 | const char *err) { | |
208 | if(err) | |
209 | popup_protocol_error(0, err); | |
210 | } | |
211 | ||
121944d1 RK |
212 | /** @brief Play received playlist contents |
213 | * | |
214 | * Passed as a completion callback by menu_activate_playlist(). | |
215 | */ | |
b5e60f0d RK |
216 | static void playlist_menu_received_content(void attribute((unused)) *v, |
217 | const char *err, | |
218 | int nvec, char **vec) { | |
121944d1 RK |
219 | if(err) { |
220 | popup_protocol_error(0, err); | |
221 | return; | |
222 | } | |
223 | for(int n = 0; n < nvec; ++n) | |
b35f7c0f | 224 | disorder_eclient_play(client, vec[n], playlist_menu_playing, NULL); |
121944d1 RK |
225 | } |
226 | ||
227 | /** @brief Called to activate a playlist | |
228 | * | |
229 | * Called when the menu item for a playlist is clicked. | |
230 | */ | |
b5e60f0d | 231 | static void playlist_menu_activate(GtkMenuItem *menuitem, |
f9b20469 RK |
232 | gpointer attribute((unused)) user_data) { |
233 | GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child); | |
234 | const char *playlist = gtk_label_get_text(label); | |
235 | ||
b5e60f0d RK |
236 | disorder_eclient_playlist_get(client, playlist_menu_received_content, |
237 | playlist, NULL); | |
f9b20469 RK |
238 | } |
239 | ||
b5e60f0d RK |
240 | /** @brief Called when the playlists change |
241 | * | |
242 | * Naively refills the menu. The results might be unsettling if the menu is | |
243 | * currently open, but this is hopefuly fairly rare. | |
244 | */ | |
245 | static void playlist_menu_changed(const char attribute((unused)) *event, | |
246 | void attribute((unused)) *eventdata, | |
247 | void attribute((unused)) *callbackdata) { | |
f9b20469 RK |
248 | if(!playlists_menu) |
249 | return; /* OMG too soon */ | |
250 | GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu); | |
f9b20469 RK |
251 | while(menu->children) |
252 | gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data)); | |
253 | /* NB nplaylists can be -1 as well as 0 */ | |
254 | for(int n = 0; n < nplaylists; ++n) { | |
255 | GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]); | |
b5e60f0d | 256 | g_signal_connect(w, "activate", G_CALLBACK(playlist_menu_activate), 0); |
f9b20469 RK |
257 | gtk_widget_show(w); |
258 | gtk_menu_shell_append(menu, w); | |
259 | } | |
fdea9f40 | 260 | gtk_widget_set_sensitive(menu_playlists_widget, |
f9b20469 | 261 | nplaylists > 0); |
fdea9f40 | 262 | gtk_widget_set_sensitive(menu_editplaylists_widget, |
6acdbba4 | 263 | nplaylists >= 0); |
f9b20469 RK |
264 | } |
265 | ||
7f7c3819 RK |
266 | /* Popup to create a new playlist ------------------------------------------- */ |
267 | ||
268 | /** @brief New-playlist popup */ | |
269 | static GtkWidget *playlist_new_window; | |
270 | ||
271 | /** @brief Text entry in new-playlist popup */ | |
272 | static GtkWidget *playlist_new_entry; | |
273 | ||
b5e60f0d | 274 | /** @brief Label for displaying feedback on what's wrong */ |
7f7c3819 RK |
275 | static GtkWidget *playlist_new_info; |
276 | ||
b5e60f0d | 277 | /** @brief "Shared" radio button */ |
7f7c3819 | 278 | static GtkWidget *playlist_new_shared; |
b5e60f0d RK |
279 | |
280 | /** @brief "Public" radio button */ | |
7f7c3819 | 281 | static GtkWidget *playlist_new_public; |
b5e60f0d RK |
282 | |
283 | /** @brief "Private" radio button */ | |
7f7c3819 RK |
284 | static GtkWidget *playlist_new_private; |
285 | ||
c41b2cac RK |
286 | /** @brief Buttons for new-playlist popup */ |
287 | static struct button playlist_new_buttons[] = { | |
288 | { | |
289 | .stock = GTK_STOCK_OK, | |
290 | .clicked = playlist_new_ok, | |
291 | .tip = "Create new playlist" | |
292 | }, | |
293 | { | |
294 | .stock = GTK_STOCK_CANCEL, | |
295 | .clicked = playlist_new_cancel, | |
296 | .tip = "Do not create new playlist" | |
7c12e4bd | 297 | } |
c41b2cac RK |
298 | }; |
299 | #define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons) | |
300 | ||
301 | /** @brief Pop up a new window to enter the playlist name and details */ | |
302 | static void playlist_new_playlist(void) { | |
303 | assert(playlist_new_window == NULL); | |
304 | playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
305 | g_signal_connect(playlist_new_window, "destroy", | |
306 | G_CALLBACK(gtk_widget_destroyed), &playlist_new_window); | |
307 | gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist"); | |
308 | /* Window will be modal, suppressing access to other windows */ | |
309 | gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE); | |
310 | gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window), | |
311 | GTK_WINDOW(playlist_window)); | |
312 | ||
313 | /* Window contents will use a table (grid) layout */ | |
314 | GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/); | |
315 | ||
316 | /* First row: playlist name */ | |
317 | gtk_table_attach_defaults(GTK_TABLE(table), | |
318 | gtk_label_new("Playlist name"), | |
319 | 0, 1, 0, 1); | |
320 | playlist_new_entry = gtk_entry_new(); | |
321 | g_signal_connect(playlist_new_entry, "changed", | |
322 | G_CALLBACK(playlist_new_entry_edited), NULL); | |
323 | gtk_table_attach_defaults(GTK_TABLE(table), | |
324 | playlist_new_entry, | |
325 | 1, 3, 0, 1); | |
326 | ||
327 | /* Second row: radio buttons to choose type */ | |
328 | playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared"); | |
329 | playlist_new_public | |
330 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), | |
331 | "public"); | |
332 | playlist_new_private | |
333 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), | |
334 | "private"); | |
335 | g_signal_connect(playlist_new_shared, "toggled", | |
336 | G_CALLBACK(playlist_new_button_toggled), NULL); | |
337 | g_signal_connect(playlist_new_public, "toggled", | |
338 | G_CALLBACK(playlist_new_button_toggled), NULL); | |
339 | g_signal_connect(playlist_new_private, "toggled", | |
340 | G_CALLBACK(playlist_new_button_toggled), NULL); | |
341 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2); | |
342 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2); | |
343 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2); | |
344 | ||
345 | /* Third row: info bar saying why not */ | |
346 | playlist_new_info = gtk_label_new(""); | |
347 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info, | |
348 | 0, 3, 2, 3); | |
349 | ||
350 | /* Fourth row: ok/cancel buttons */ | |
351 | GtkWidget *hbox = create_buttons_box(playlist_new_buttons, | |
352 | NPLAYLIST_NEW_BUTTONS, | |
353 | gtk_hbox_new(FALSE, 0)); | |
354 | gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4); | |
355 | ||
356 | gtk_container_add(GTK_CONTAINER(playlist_new_window), | |
357 | frame_widget(table, NULL)); | |
358 | ||
359 | /* Set initial state of OK button */ | |
360 | playlist_new_changed(0,0,0); | |
361 | ||
362 | /* TODO: return should = OK, escape should = cancel */ | |
363 | ||
364 | /* Display the window */ | |
365 | gtk_widget_show_all(playlist_new_window); | |
7c12e4bd RK |
366 | } |
367 | ||
c41b2cac RK |
368 | /** @brief Called when 'ok' is clicked in new-playlist popup */ |
369 | static void playlist_new_ok(GtkButton attribute((unused)) *button, | |
370 | gpointer attribute((unused)) userdata) { | |
371 | gboolean shared, public, private; | |
372 | char *name, *fullname; | |
373 | playlist_new_details(&name, &fullname, &shared, &public, &private); | |
374 | ||
375 | /* We need to: | |
376 | * - lock the playlist | |
377 | * - check it doesn't exist | |
378 | * - set sharing (which will create it empty | |
379 | * - unlock it | |
380 | * | |
381 | * TODO we should freeze the window while this is going on to stop a second | |
382 | * click. | |
383 | */ | |
384 | disorder_eclient_playlist_lock(client, playlist_new_locked, fullname, | |
385 | fullname); | |
7c12e4bd RK |
386 | } |
387 | ||
c41b2cac RK |
388 | /** @brief Called when the proposed new playlist has been locked */ |
389 | static void playlist_new_locked(void *v, const char *err) { | |
390 | char *fullname = v; | |
7c12e4bd RK |
391 | if(err) { |
392 | popup_protocol_error(0, err); | |
393 | return; | |
394 | } | |
c41b2cac RK |
395 | disorder_eclient_playlist_get(client, playlist_new_retrieved, |
396 | fullname, fullname); | |
7c12e4bd RK |
397 | } |
398 | ||
399 | /** @brief Called when the proposed new playlist's contents have been retrieved | |
400 | * | |
401 | * ...or rather, normally, when it's been reported that it does not exist. | |
402 | */ | |
403 | static void playlist_new_retrieved(void *v, const char *err, | |
404 | int nvec, | |
405 | char attribute((unused)) **vec) { | |
406 | char *fullname = v; | |
407 | if(!err && nvec != -1) | |
408 | /* A rare case but not in principle impossible */ | |
409 | err = "A playlist with that name already exists."; | |
410 | if(err) { | |
411 | popup_protocol_error(0, err); | |
412 | disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname); | |
413 | return; | |
414 | } | |
415 | gboolean shared, public, private; | |
416 | playlist_new_details(0, 0, &shared, &public, &private); | |
417 | disorder_eclient_playlist_set_share(client, playlist_new_created, fullname, | |
418 | public ? "public" | |
419 | : private ? "private" | |
420 | : "shared", | |
421 | fullname); | |
422 | } | |
423 | ||
c41b2cac RK |
424 | /** @brief Called when the new playlist has been created */ |
425 | static void playlist_new_created(void attribute((unused)) *v, const char *err) { | |
7c12e4bd RK |
426 | if(err) { |
427 | popup_protocol_error(0, err); | |
428 | return; | |
429 | } | |
c41b2cac RK |
430 | disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL); |
431 | // TODO arrange for the new playlist to be selected | |
7c12e4bd RK |
432 | } |
433 | ||
c41b2cac RK |
434 | /** @brief Called when the newly created playlist has unlocked */ |
435 | static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) { | |
436 | if(err) | |
437 | popup_protocol_error(0, err); | |
438 | /* Pop down the creation window */ | |
439 | gtk_widget_destroy(playlist_new_window); | |
7f7c3819 RK |
440 | } |
441 | ||
442 | /** @brief Called when 'cancel' is clicked in new-playlist popup */ | |
443 | static void playlist_new_cancel(GtkButton attribute((unused)) *button, | |
444 | gpointer attribute((unused)) userdata) { | |
445 | gtk_widget_destroy(playlist_new_window); | |
446 | } | |
447 | ||
c41b2cac RK |
448 | /** @brief Called when some radio button in the new-playlist popup changes */ |
449 | static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) *tb, | |
450 | gpointer attribute((unused)) userdata) { | |
451 | playlist_new_changed(0,0,0); | |
452 | } | |
453 | ||
454 | /** @brief Called when the text entry field in the new-playlist popup changes */ | |
455 | static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable, | |
456 | gpointer attribute((unused)) user_data) { | |
457 | playlist_new_changed(0,0,0); | |
458 | } | |
459 | ||
460 | /** @brief Called to update new playlist window state | |
461 | * | |
462 | * This is called whenever one the text entry or radio buttons changed, and | |
463 | * also when the set of known playlists changes. It determines whether the new | |
464 | * playlist would be creatable and sets the sensitivity of the OK button | |
465 | * and info display accordingly. | |
466 | */ | |
467 | static void playlist_new_changed(const char attribute((unused)) *event, | |
468 | void attribute((unused)) *eventdata, | |
469 | void attribute((unused)) *callbackdata) { | |
470 | if(!playlist_new_window) | |
471 | return; | |
472 | const char *reason = playlist_new_valid(); | |
473 | gtk_widget_set_sensitive(playlist_new_buttons[0].widget, | |
474 | !reason); | |
475 | gtk_label_set_text(GTK_LABEL(playlist_new_info), reason); | |
476 | } | |
7f7c3819 | 477 | |
7c12e4bd RK |
478 | /** @brief Test whether the new-playlist window settings are valid |
479 | * @return NULL on success or an error string if not | |
480 | */ | |
7f7c3819 RK |
481 | static const char *playlist_new_valid(void) { |
482 | gboolean shared, public, private; | |
7c12e4bd RK |
483 | char *name, *fullname; |
484 | playlist_new_details(&name, &fullname, &shared, &public, &private); | |
7f7c3819 RK |
485 | if(!(shared || public || private)) |
486 | return "No type set."; | |
7f7c3819 RK |
487 | if(!*name) |
488 | return ""; | |
7f7c3819 | 489 | /* See if the result is valid */ |
7c12e4bd RK |
490 | if(!valid_username(name) |
491 | || playlist_parse_name(fullname, NULL, NULL)) | |
7f7c3819 | 492 | return "Not a valid playlist name."; |
b5e60f0d RK |
493 | /* See if the result clashes with an existing name. This is not a perfect |
494 | * check, the playlist might be created after this point but before we get a | |
495 | * chance to disable the "OK" button. However when we try to create the | |
496 | * playlist we will first try to retrieve it, with a lock held, so we | |
497 | * shouldn't end up overwriting anything. */ | |
7f7c3819 RK |
498 | for(int n = 0; n < nplaylists; ++n) |
499 | if(!strcmp(playlists[n], fullname)) { | |
500 | if(shared) | |
501 | return "A shared playlist with that name already exists."; | |
502 | else | |
503 | return "You already have a playlist with that name."; | |
504 | } | |
505 | /* As far as we can tell creation would work */ | |
506 | return NULL; | |
507 | } | |
508 | ||
c41b2cac RK |
509 | /** @brief Get entered new-playlist details |
510 | * @param namep Where to store entered name (or NULL) | |
511 | * @param fullnamep Where to store computed full name (or NULL) | |
512 | * @param sharep Where to store 'shared' flag (or NULL) | |
513 | * @param publicp Where to store 'public' flag (or NULL) | |
514 | * @param privatep Where to store 'private' flag (or NULL) | |
7f7c3819 | 515 | */ |
c41b2cac RK |
516 | static void playlist_new_details(char **namep, |
517 | char **fullnamep, | |
518 | gboolean *sharedp, | |
519 | gboolean *publicp, | |
520 | gboolean *privatep) { | |
521 | gboolean shared, public, private; | |
522 | g_object_get(playlist_new_shared, "active", &shared, (char *)NULL); | |
523 | g_object_get(playlist_new_public, "active", &public, (char *)NULL); | |
524 | g_object_get(playlist_new_private, "active", &private, (char *)NULL); | |
525 | char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry), | |
526 | 0, -1); /* name owned by calle */ | |
527 | char *name = xstrdup(gname); | |
528 | g_free(gname); | |
529 | if(sharedp) *sharedp = shared; | |
530 | if(publicp) *publicp = public; | |
531 | if(privatep) *privatep = private; | |
532 | if(namep) *namep = name; | |
533 | if(fullnamep) { | |
534 | if(*sharedp) *fullnamep = *namep; | |
535 | else byte_xasprintf(fullnamep, "%s.%s", config->username, name); | |
536 | } | |
7f7c3819 RK |
537 | } |
538 | ||
b5e60f0d | 539 | /* Playlist picker ---------------------------------------------------------- */ |
121944d1 | 540 | |
b5e60f0d RK |
541 | /** @brief Delete button */ |
542 | static GtkWidget *playlist_picker_delete_button; | |
543 | ||
544 | /** @brief Tree model for list of playlists */ | |
545 | static GtkListStore *playlist_picker_list; | |
546 | ||
547 | /** @brief Selection for list of playlists */ | |
548 | static GtkTreeSelection *playlist_picker_selection; | |
549 | ||
550 | /** @brief Currently selected playlist */ | |
551 | static const char *playlist_picker_selected; | |
552 | ||
553 | /** @brief (Re-)populate the playlist picker tree model */ | |
554 | static void playlist_picker_fill(const char attribute((unused)) *event, | |
555 | void attribute((unused)) *eventdata, | |
556 | void attribute((unused)) *callbackdata) { | |
f0bd437a RK |
557 | GtkTreeIter iter[1]; |
558 | ||
7fe2807d | 559 | if(!playlist_window) |
d571e07b | 560 | return; |
b5e60f0d RK |
561 | if(!playlist_picker_list) |
562 | playlist_picker_list = gtk_list_store_new(1, G_TYPE_STRING); | |
563 | const char *was_selected = playlist_picker_selected; | |
564 | gtk_list_store_clear(playlist_picker_list); /* clears playlists_selected */ | |
d571e07b | 565 | for(int n = 0; n < nplaylists; ++n) { |
b5e60f0d RK |
566 | gtk_list_store_insert_with_values(playlist_picker_list, iter, |
567 | n /*position*/, | |
568 | 0, playlists[n], /* column 0 */ | |
569 | -1); /* no more cols */ | |
d571e07b RK |
570 | /* Reselect the selected playlist */ |
571 | if(was_selected && !strcmp(was_selected, playlists[n])) | |
b5e60f0d | 572 | gtk_tree_selection_select_iter(playlist_picker_selection, iter); |
d571e07b | 573 | } |
9a6e6a46 RK |
574 | /* TODO deselecting then reselecting the current playlist resets the playlist |
575 | * editor, which trashes the user's selection. */ | |
f0bd437a RK |
576 | } |
577 | ||
578 | /** @brief Called when the selection might have changed */ | |
b5e60f0d RK |
579 | static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused)) *treeselection, |
580 | gpointer attribute((unused)) user_data) { | |
f0bd437a RK |
581 | GtkTreeIter iter; |
582 | char *gselected, *selected; | |
583 | ||
584 | /* Identify the current selection */ | |
b5e60f0d RK |
585 | if(gtk_tree_selection_get_selected(playlist_picker_selection, 0, &iter)) { |
586 | gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), &iter, | |
f0bd437a RK |
587 | 0, &gselected, -1); |
588 | selected = xstrdup(gselected); | |
589 | g_free(gselected); | |
590 | } else | |
591 | selected = 0; | |
d9b141cc RK |
592 | /* Set button sensitivity according to the new state */ |
593 | if(selected) | |
b5e60f0d | 594 | gtk_widget_set_sensitive(playlist_picker_delete_button, 1); |
7c12e4bd | 595 | else |
b5e60f0d | 596 | gtk_widget_set_sensitive(playlist_picker_delete_button, 0); |
f0bd437a | 597 | /* Eliminate no-change cases */ |
b5e60f0d | 598 | if(!selected && !playlist_picker_selected) |
f0bd437a | 599 | return; |
b5e60f0d RK |
600 | if(selected |
601 | && playlist_picker_selected | |
602 | && !strcmp(selected, playlist_picker_selected)) | |
f0bd437a | 603 | return; |
d9b141cc | 604 | /* Record the new state */ |
b5e60f0d | 605 | playlist_picker_selected = selected; |
7e702780 RK |
606 | /* Re-initalize the queue */ |
607 | ql_new_queue(&ql_playlist, NULL); | |
7fe2807d | 608 | playlist_editor_fill(NULL, (void *)playlist_picker_selected, NULL); |
f0bd437a RK |
609 | } |
610 | ||
611 | /** @brief Called when the 'add' button is pressed */ | |
b5e60f0d RK |
612 | static void playlist_picker_add(GtkButton attribute((unused)) *button, |
613 | gpointer attribute((unused)) userdata) { | |
7c12e4bd | 614 | /* Unselect whatever is selected TODO why?? */ |
b5e60f0d RK |
615 | gtk_tree_selection_unselect_all(playlist_picker_selection); |
616 | playlist_new_playlist(); | |
f0bd437a RK |
617 | } |
618 | ||
d9b141cc | 619 | /** @brief Called when playlist deletion completes */ |
b5e60f0d RK |
620 | static void playlists_picker_delete_completed(void attribute((unused)) *v, |
621 | const char *err) { | |
d9b141cc RK |
622 | if(err) |
623 | popup_protocol_error(0, err); | |
624 | } | |
625 | ||
f0bd437a | 626 | /** @brief Called when the 'Delete' button is pressed */ |
b5e60f0d RK |
627 | static void playlist_picker_delete(GtkButton attribute((unused)) *button, |
628 | gpointer attribute((unused)) userdata) { | |
f0bd437a RK |
629 | GtkWidget *yesno; |
630 | int res; | |
631 | ||
b5e60f0d | 632 | if(!playlist_picker_selected) |
f0bd437a | 633 | return; /* shouldn't happen */ |
7fe2807d | 634 | yesno = gtk_message_dialog_new(GTK_WINDOW(playlist_window), |
f0bd437a RK |
635 | GTK_DIALOG_MODAL, |
636 | GTK_MESSAGE_QUESTION, | |
637 | GTK_BUTTONS_YES_NO, | |
c5782050 | 638 | "Do you really want to delete playlist %s?" |
f0bd437a | 639 | " This action cannot be undone.", |
b5e60f0d | 640 | playlist_picker_selected); |
f0bd437a RK |
641 | res = gtk_dialog_run(GTK_DIALOG(yesno)); |
642 | gtk_widget_destroy(yesno); | |
643 | if(res == GTK_RESPONSE_YES) { | |
644 | disorder_eclient_playlist_delete(client, | |
b5e60f0d RK |
645 | playlists_picker_delete_completed, |
646 | playlist_picker_selected, | |
f0bd437a RK |
647 | NULL); |
648 | } | |
649 | } | |
650 | ||
651 | /** @brief Table of buttons below the playlist list */ | |
b5e60f0d | 652 | static struct button playlist_picker_buttons[] = { |
f0bd437a RK |
653 | { |
654 | GTK_STOCK_ADD, | |
b5e60f0d | 655 | playlist_picker_add, |
f0bd437a RK |
656 | "Create a new playlist", |
657 | 0 | |
658 | }, | |
659 | { | |
660 | GTK_STOCK_REMOVE, | |
b5e60f0d | 661 | playlist_picker_delete, |
f0bd437a RK |
662 | "Delete a playlist", |
663 | 0 | |
664 | }, | |
665 | }; | |
b5e60f0d | 666 | #define NPLAYLIST_PICKER_BUTTONS (sizeof playlist_picker_buttons / sizeof *playlist_picker_buttons) |
f0bd437a | 667 | |
506e02d8 | 668 | /** @brief Create the list of playlists for the edit playlists window */ |
b5e60f0d | 669 | static GtkWidget *playlist_picker_create(void) { |
506e02d8 | 670 | /* Create the list of playlist and populate it */ |
b5e60f0d | 671 | playlist_picker_fill(NULL, NULL, NULL); |
506e02d8 | 672 | /* Create the tree view */ |
b5e60f0d | 673 | GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlist_picker_list)); |
506e02d8 RK |
674 | /* ...and the renderers for it */ |
675 | GtkCellRenderer *cr = gtk_cell_renderer_text_new(); | |
676 | GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist", | |
677 | cr, | |
678 | "text", 0, | |
679 | NULL); | |
680 | gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col); | |
681 | /* Get the selection for the view; set its mode; arrange for a callback when | |
682 | * it changes */ | |
b5e60f0d RK |
683 | playlist_picker_selected = NULL; |
684 | playlist_picker_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)); | |
685 | gtk_tree_selection_set_mode(playlist_picker_selection, GTK_SELECTION_BROWSE); | |
686 | g_signal_connect(playlist_picker_selection, "changed", | |
687 | G_CALLBACK(playlist_picker_selection_changed), NULL); | |
506e02d8 RK |
688 | |
689 | /* Create the control buttons */ | |
b5e60f0d RK |
690 | GtkWidget *buttons = create_buttons_box(playlist_picker_buttons, |
691 | NPLAYLIST_PICKER_BUTTONS, | |
506e02d8 | 692 | gtk_hbox_new(FALSE, 1)); |
b5e60f0d | 693 | playlist_picker_delete_button = playlist_picker_buttons[1].widget; |
506e02d8 | 694 | |
b5e60f0d | 695 | playlist_picker_selection_changed(NULL, NULL); |
7c12e4bd | 696 | |
506e02d8 RK |
697 | /* Buttons live below the list */ |
698 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); | |
699 | gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0); | |
700 | gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0); | |
701 | ||
702 | return vbox; | |
703 | } | |
704 | ||
7fe2807d | 705 | /* Playlist editor ---------------------------------------------------------- */ |
506e02d8 | 706 | |
c41b2cac RK |
707 | static GtkWidget *playlists_editor_create(void) { |
708 | assert(ql_playlist.view == NULL); /* better not be set up already */ | |
709 | GtkWidget *w = init_queuelike(&ql_playlist); | |
710 | /* Initially empty */ | |
711 | return w; | |
712 | } | |
713 | ||
714 | /** @brief (Re-)populate the playlist tree model */ | |
715 | static void playlist_editor_fill(const char attribute((unused)) *event, | |
716 | void *eventdata, | |
717 | void attribute((unused)) *callbackdata) { | |
718 | const char *modified_playlist = eventdata; | |
719 | if(!playlist_window) | |
720 | return; | |
721 | if(!playlist_picker_selected) | |
722 | return; | |
723 | if(!strcmp(playlist_picker_selected, modified_playlist)) | |
724 | disorder_eclient_playlist_get(client, playlists_editor_received_tracks, | |
725 | playlist_picker_selected, | |
726 | (void *)playlist_picker_selected); | |
727 | } | |
728 | ||
7e702780 | 729 | /** @brief Called with new tracks for the playlist */ |
f06a754c | 730 | static void playlists_editor_received_tracks(void *v, |
7fe2807d RK |
731 | const char *err, |
732 | int nvec, char **vec) { | |
f06a754c | 733 | const char *playlist = v; |
7e702780 RK |
734 | if(err) { |
735 | popup_protocol_error(0, err); | |
736 | return; | |
737 | } | |
f06a754c RK |
738 | if(!playlist_picker_selected |
739 | || strcmp(playlist, playlist_picker_selected)) { | |
740 | /* The tracks are for the wrong playlist - something must have changed | |
741 | * while the fetch command was in flight. We just ignore this callback, | |
742 | * the right answer will be requested and arrive in due course. */ | |
743 | return; | |
744 | } | |
7e702780 RK |
745 | if(nvec == -1) |
746 | /* No such playlist, presumably we'll get a deleted event shortly */ | |
747 | return; | |
748 | /* Translate the list of tracks into queue entries */ | |
749 | struct queue_entry *newq, **qq = &newq; | |
750 | hash *h = hash_new(sizeof(int)); | |
751 | for(int n = 0; n < nvec; ++n) { | |
752 | struct queue_entry *q = xmalloc(sizeof *q); | |
753 | q->track = vec[n]; | |
754 | /* Synthesize a unique ID so that the selection survives updates. Tracks | |
755 | * can appear more than once in the queue so we can't use raw track names, | |
756 | * so we add a serial number to the start. */ | |
7e702780 RK |
757 | int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0; |
758 | byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]); | |
7e702780 RK |
759 | hash_add(h, vec[0], &serial, HASH_INSERT_OR_REPLACE); |
760 | *qq = q; | |
761 | qq = &q->next; | |
762 | } | |
763 | *qq = NULL; | |
7e702780 | 764 | ql_new_queue(&ql_playlist, newq); |
7e702780 RK |
765 | } |
766 | ||
7a3eb8eb RK |
767 | /* Playlist mutation -------------------------------------------------------- */ |
768 | ||
769 | /** @brief State structure for guarded playlist modification | |
770 | * | |
771 | * To kick things off create one of these and disorder_eclient_playlist_lock() | |
772 | * with playlist_modify_locked() as its callback. @c modify will be called; it | |
773 | * should disorder_eclient_playlist_set() to set the new state with | |
774 | * playlist_modify_updated() as its callback. | |
775 | */ | |
776 | struct playlist_modify_data { | |
777 | /** @brief Affected playlist */ | |
778 | const char *playlist; | |
779 | /** @brief Modification function | |
780 | * @param mod Pointer back to state structure | |
781 | * @param ntracks Length of playlist | |
782 | * @param tracks Tracks in playlist | |
783 | */ | |
784 | void (*modify)(struct playlist_modify_data *mod, | |
785 | int ntracks, char **tracks); | |
786 | ||
787 | /** @brief Number of tracks dropped */ | |
788 | int ntracks; | |
789 | /** @brief Track names dropped */ | |
790 | char **tracks; | |
791 | /** @brief Track IDs dropped */ | |
792 | char **ids; | |
793 | /** @brief Drop after this point */ | |
794 | struct queue_entry *after_me; | |
795 | }; | |
796 | ||
797 | /** @brief Called with playlist locked | |
798 | * | |
799 | * This is the entry point for guarded modification ising @ref | |
800 | * playlist_modify_data. | |
801 | */ | |
802 | static void playlist_modify_locked(void *v, const char *err) { | |
803 | struct playlist_modify_data *mod = v; | |
804 | if(err) { | |
805 | popup_protocol_error(0, err); | |
806 | return; | |
807 | } | |
808 | disorder_eclient_playlist_get(client, playlist_modify_retrieved, | |
809 | mod->playlist, mod); | |
810 | } | |
811 | ||
812 | /** @brief Called with current playlist contents | |
813 | * Checks that the playlist is still current and has not changed. | |
814 | */ | |
815 | void playlist_modify_retrieved(void *v, const char *err, | |
816 | int nvec, | |
817 | char **vec) { | |
818 | struct playlist_modify_data *mod = v; | |
819 | if(err) { | |
820 | popup_protocol_error(0, err); | |
821 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); | |
822 | return; | |
823 | } | |
824 | if(nvec < 0 | |
825 | || !playlist_picker_selected | |
826 | || strcmp(mod->playlist, playlist_picker_selected)) { | |
827 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); | |
828 | return; | |
829 | } | |
830 | /* We check that the contents haven't changed. If they have we just abandon | |
831 | * the operation. The user will have to try again. */ | |
832 | struct queue_entry *q; | |
833 | int n; | |
834 | for(n = 0, q = ql_playlist.q; q && n < nvec; ++n, q = q->next) | |
835 | if(strcmp(q->track, vec[n])) | |
836 | break; | |
837 | if(n != nvec || q != NULL) { | |
838 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); | |
839 | return; | |
840 | } | |
841 | mod->modify(mod, nvec, vec); | |
842 | } | |
843 | ||
844 | /** @brief Called when the playlist has been updated */ | |
845 | static void playlist_modify_updated(void attribute((unused)) *v, | |
846 | const char *err) { | |
847 | if(err) | |
848 | popup_protocol_error(0, err); | |
849 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); | |
850 | } | |
851 | ||
852 | /** @brief Called when the playlist has been unlocked */ | |
853 | static void playlist_modify_unlocked(void attribute((unused)) *v, | |
854 | const char *err) { | |
855 | if(err) | |
856 | popup_protocol_error(0, err); | |
857 | } | |
858 | ||
859 | /* Drop tracks into a playlist ---------------------------------------------- */ | |
860 | ||
861 | static void playlist_drop(struct queuelike attribute((unused)) *ql, | |
862 | int ntracks, | |
863 | char **tracks, char **ids, | |
864 | struct queue_entry *after_me) { | |
865 | struct playlist_modify_data *mod = xmalloc(sizeof *mod); | |
866 | ||
867 | mod->playlist = playlist_picker_selected; | |
868 | mod->modify = playlist_drop_modify; | |
869 | mod->ntracks = ntracks; | |
870 | mod->tracks = tracks; | |
871 | mod->ids = ids; | |
872 | mod->after_me = after_me; | |
873 | /* To safely move or insert rows we must: | |
874 | * - take a lock | |
875 | * - fetch the playlist | |
876 | * - verify it's not changed | |
877 | * - update the playlist contents | |
878 | * - store the playlist | |
879 | * - release the lock | |
880 | * | |
881 | */ | |
882 | disorder_eclient_playlist_lock(client, playlist_modify_locked, | |
883 | mod->playlist, mod); | |
884 | } | |
885 | ||
886 | static void playlist_drop_modify(struct playlist_modify_data *mod, | |
887 | int nvec, char **vec) { | |
888 | char **newvec; | |
889 | int nnewvec; | |
890 | if(mod->ids) { | |
891 | /* This is a rearrangement */ | |
892 | /* TODO what if it's a drag from the queue? */ | |
893 | abort(); | |
894 | } else { | |
895 | /* This is an insertion */ | |
896 | struct queue_entry *q = ql_playlist.q; | |
897 | int ins = 0; | |
898 | if(mod->after_me) { | |
899 | ++ins; | |
900 | while(q && q != mod->after_me) { | |
901 | q = q->next; | |
902 | ++ins; | |
903 | } | |
904 | } | |
905 | nnewvec = nvec + mod->ntracks; | |
906 | newvec = xcalloc(nnewvec, sizeof (char *)); | |
907 | memcpy(newvec, vec, | |
908 | ins * sizeof (char *)); | |
909 | memcpy(newvec + ins, mod->tracks, | |
910 | mod->ntracks * sizeof (char *)); | |
911 | memcpy(newvec + ins + mod->ntracks, vec + ins, | |
912 | (nvec - ins) * sizeof (char *)); | |
913 | } | |
914 | disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist, | |
915 | newvec, nnewvec, mod); | |
916 | } | |
917 | ||
b35f7c0f RK |
918 | /* Playlist editor right-click menu ---------------------------------------- */ |
919 | ||
920 | /** @brief Called to determine whether the playlist is playable */ | |
921 | static int playlist_playall_sensitive(void attribute((unused)) *extra) { | |
922 | /* If there's no playlist obviously we can't play it */ | |
923 | if(!playlist_picker_selected) | |
924 | return FALSE; | |
925 | /* If it's empty we can't play it */ | |
926 | if(!ql_playlist.q) | |
927 | return FALSE; | |
928 | /* Otherwise we can */ | |
929 | return TRUE; | |
930 | } | |
931 | ||
932 | /** @brief Called to play the selected playlist */ | |
933 | static void playlist_playall_activate(GtkMenuItem attribute((unused)) *menuitem, | |
934 | gpointer attribute((unused)) user_data) { | |
935 | if(!playlist_picker_selected) | |
936 | return; | |
937 | /* Re-use the menu-based activation callback */ | |
938 | disorder_eclient_playlist_get(client, playlist_menu_received_content, | |
939 | playlist_picker_selected, NULL); | |
940 | } | |
941 | ||
dba3eb8e RK |
942 | /** @brief Called to determine whether the playlist is playable */ |
943 | static int playlist_remove_sensitive(void attribute((unused)) *extra) { | |
944 | /* If there's no playlist obviously we can't remove from it */ | |
945 | if(!playlist_picker_selected) | |
946 | return FALSE; | |
947 | /* If no tracks are selected we cannot remove them */ | |
948 | if(!gtk_tree_selection_count_selected_rows(ql_playlist.selection)) | |
949 | return FALSE; | |
950 | /* We're good to go */ | |
951 | return TRUE; | |
952 | } | |
953 | ||
954 | /** @brief Called to play the selected playlist */ | |
955 | static void playlist_remove_activate(GtkMenuItem attribute((unused)) *menuitem, | |
956 | gpointer attribute((unused)) user_data) { | |
957 | if(!playlist_picker_selected) | |
958 | return; | |
959 | /* To safely remove rows we must: | |
960 | * - take a lock | |
961 | * - fetch the playlist | |
487eb32a RK |
962 | * - verify it's not changed |
963 | * - delete the selected rows from the retrieved version | |
dba3eb8e RK |
964 | * - store the playlist |
965 | * - release the lock | |
487eb32a RK |
966 | * |
967 | * In addition we careful check that the selected playlist hasn't changed | |
968 | * underfoot, and avoid leaving the playlist locked if we bail out at any | |
969 | * point. | |
dba3eb8e | 970 | */ |
7a3eb8eb | 971 | struct playlist_modify_data *mod = xmalloc(sizeof *mod); |
487eb32a | 972 | |
7a3eb8eb RK |
973 | mod->playlist = playlist_picker_selected; |
974 | mod->modify = playlist_remove_modify; | |
975 | disorder_eclient_playlist_lock(client, playlist_modify_locked, | |
976 | mod->playlist, mod); | |
487eb32a RK |
977 | } |
978 | ||
7a3eb8eb RK |
979 | static void playlist_remove_modify(struct playlist_modify_data *mod, |
980 | int attribute((unused)) nvec, char **vec) { | |
487eb32a RK |
981 | GtkTreeIter iter[1]; |
982 | gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql_playlist.store), | |
983 | iter); | |
7a3eb8eb | 984 | int n = 0, m = 0; |
487eb32a RK |
985 | while(it) { |
986 | if(!gtk_tree_selection_iter_is_selected(ql_playlist.selection, iter)) | |
987 | vec[m++] = vec[n++]; | |
988 | else | |
989 | n++; | |
990 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_playlist.store), iter); | |
991 | } | |
7a3eb8eb RK |
992 | disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist, |
993 | vec, m, mod); | |
dba3eb8e RK |
994 | } |
995 | ||
506e02d8 RK |
996 | /* Playlists window --------------------------------------------------------- */ |
997 | ||
7de5fbb9 | 998 | /** @brief Pop up the playlists window |
121944d1 RK |
999 | * |
1000 | * Called when the playlists menu item is selected | |
1001 | */ | |
f06a754c RK |
1002 | void playlist_window_create(gpointer attribute((unused)) callback_data, |
1003 | guint attribute((unused)) callback_action, | |
1004 | GtkWidget attribute((unused)) *menu_item) { | |
f0bd437a | 1005 | /* If the window already exists, raise it */ |
7fe2807d RK |
1006 | if(playlist_window) { |
1007 | gtk_window_present(GTK_WINDOW(playlist_window)); | |
f0bd437a RK |
1008 | return; |
1009 | } | |
1010 | /* Create the window */ | |
7fe2807d RK |
1011 | playlist_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
1012 | gtk_widget_set_style(playlist_window, tool_style); | |
1013 | g_signal_connect(playlist_window, "destroy", | |
1014 | G_CALLBACK(playlist_window_destroyed), &playlist_window); | |
1015 | gtk_window_set_title(GTK_WINDOW(playlist_window), "Playlists Management"); | |
f0bd437a RK |
1016 | /* TODO loads of this is very similar to (copied from!) users.c - can we |
1017 | * de-dupe? */ | |
1018 | /* Keyboard shortcuts */ | |
7fe2807d RK |
1019 | g_signal_connect(playlist_window, "key-press-event", |
1020 | G_CALLBACK(playlist_window_keypress), 0); | |
f0bd437a | 1021 | /* default size is too small */ |
7fe2807d | 1022 | gtk_window_set_default_size(GTK_WINDOW(playlist_window), 512, 240); |
f0bd437a | 1023 | |
506e02d8 | 1024 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
b5e60f0d | 1025 | gtk_box_pack_start(GTK_BOX(hbox), playlist_picker_create(), |
506e02d8 RK |
1026 | FALSE/*expand*/, FALSE, 0); |
1027 | gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), | |
1028 | FALSE/*expand*/, FALSE, 2); | |
7fe2807d | 1029 | gtk_box_pack_start(GTK_BOX(hbox), playlists_editor_create(), |
506e02d8 | 1030 | TRUE/*expand*/, TRUE/*fill*/, 0); |
f0bd437a | 1031 | |
7fe2807d RK |
1032 | gtk_container_add(GTK_CONTAINER(playlist_window), frame_widget(hbox, NULL)); |
1033 | gtk_widget_show_all(playlist_window); | |
f9b20469 RK |
1034 | } |
1035 | ||
c41b2cac RK |
1036 | /** @brief Keypress handler */ |
1037 | static gboolean playlist_window_keypress(GtkWidget attribute((unused)) *widget, | |
1038 | GdkEventKey *event, | |
1039 | gpointer attribute((unused)) user_data) { | |
1040 | if(event->state) | |
1041 | return FALSE; | |
1042 | switch(event->keyval) { | |
1043 | case GDK_Escape: | |
1044 | gtk_widget_destroy(playlist_window); | |
1045 | return TRUE; | |
1046 | default: | |
1047 | return FALSE; | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | /** @brief Called when the playlist window is destroyed */ | |
1052 | static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget, | |
1053 | GtkWidget **widget_pointer) { | |
1054 | destroy_queuelike(&ql_playlist); | |
1055 | *widget_pointer = NULL; | |
1056 | } | |
1057 | ||
fc36ecb7 RK |
1058 | /** @brief Initialize playlist support */ |
1059 | void playlists_init(void) { | |
1060 | /* We re-get all playlists upon any change... */ | |
b5e60f0d | 1061 | event_register("playlist-created", playlist_list_update, 0); |
b5e60f0d | 1062 | event_register("playlist-deleted", playlist_list_update, 0); |
fc36ecb7 | 1063 | /* ...and on reconnection */ |
b5e60f0d | 1064 | event_register("log-connected", playlist_list_update, 0); |
fc36ecb7 | 1065 | /* ...and from time to time */ |
b5e60f0d | 1066 | event_register("periodic-slow", playlist_list_update, 0); |
fc36ecb7 | 1067 | /* ...and at startup */ |
b5e60f0d | 1068 | playlist_list_update(0, 0, 0); |
7c12e4bd RK |
1069 | |
1070 | /* Update the playlists menu when the set of playlists changes */ | |
b5e60f0d | 1071 | event_register("playlists-updated", playlist_menu_changed, 0); |
7c12e4bd RK |
1072 | /* Update the new-playlist OK button when the set of playlists changes */ |
1073 | event_register("playlists-updated", playlist_new_changed, 0); | |
d9b141cc | 1074 | /* Update the list of playlists in the edit window when the set changes */ |
b5e60f0d | 1075 | event_register("playlists-updated", playlist_picker_fill, 0); |
7e702780 | 1076 | /* Update the displayed playlist when it is modified */ |
7fe2807d | 1077 | event_register("playlist-modified", playlist_editor_fill, 0); |
fc36ecb7 RK |
1078 | } |
1079 | ||
0b0fb26b RK |
1080 | #endif |
1081 | ||
fc36ecb7 RK |
1082 | /* |
1083 | Local Variables: | |
1084 | c-basic-offset:2 | |
1085 | comment-column:40 | |
1086 | fill-column:79 | |
1087 | indent-tabs-mode:nil | |
1088 | End: | |
1089 | */ |