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