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