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 | |
b5e60f0d RK |
43 | static void playlist_list_received_playlists(void *v, |
44 | const char *err, | |
45 | int nvec, char **vec); | |
7fe2807d RK |
46 | static void playlist_editor_fill(const char *event, |
47 | void *eventdata, | |
48 | void *callbackdata); | |
b35f7c0f RK |
49 | static int playlist_playall_sensitive(void *extra); |
50 | static void playlist_playall_activate(GtkMenuItem *menuitem, | |
51 | gpointer user_data); | |
dba3eb8e RK |
52 | static int playlist_remove_sensitive(void *extra) ; |
53 | static void playlist_remove_activate(GtkMenuItem *menuitem, | |
54 | gpointer user_data); | |
c41b2cac RK |
55 | static void playlist_new_locked(void *v, const char *err); |
56 | static void playlist_new_retrieved(void *v, const char *err, | |
57 | int nvec, | |
58 | char **vec); | |
59 | static void playlist_new_created(void *v, const char *err); | |
60 | static void playlist_new_unlocked(void *v, const char *err); | |
61 | static void playlist_new_entry_edited(GtkEditable *editable, | |
62 | gpointer user_data); | |
63 | static void playlist_new_button_toggled(GtkToggleButton *tb, | |
64 | gpointer userdata); | |
65 | static void playlist_new_changed(const char *event, | |
66 | void *eventdata, | |
67 | void *callbackdata); | |
68 | static const char *playlist_new_valid(void); | |
69 | static void playlist_new_details(char **namep, | |
70 | char **fullnamep, | |
71 | gboolean *sharedp, | |
72 | gboolean *publicp, | |
73 | gboolean *privatep); | |
74 | static void playlist_new_ok(GtkButton *button, | |
75 | gpointer userdata); | |
76 | static void playlist_new_cancel(GtkButton *button, | |
77 | gpointer userdata); | |
78 | static void playlists_editor_received_tracks(void *v, | |
79 | const char *err, | |
80 | int nvec, char **vec); | |
81 | static void playlist_window_destroyed(GtkWidget *widget, | |
82 | GtkWidget **widget_pointer); | |
83 | static gboolean playlist_window_keypress(GtkWidget *widget, | |
84 | GdkEventKey *event, | |
85 | gpointer user_data); | |
86 | static int playlistcmp(const void *ap, const void *bp); | |
7a3eb8eb RK |
87 | static void playlist_modify_locked(void *v, const char *err); |
88 | void playlist_modify_retrieved(void *v, const char *err, | |
487eb32a RK |
89 | int nvec, |
90 | char **vec); | |
7a3eb8eb RK |
91 | static void playlist_modify_updated(void *v, const char *err); |
92 | static void playlist_modify_unlocked(void *v, const char *err); | |
93 | static void playlist_drop(struct queuelike *ql, | |
94 | int ntracks, | |
95 | char **tracks, char **ids, | |
96 | struct queue_entry *after_me); | |
97 | struct playlist_modify_data; | |
98 | static void playlist_drop_modify(struct playlist_modify_data *mod, | |
99 | int nvec, char **vec); | |
100 | static void playlist_remove_modify(struct playlist_modify_data *mod, | |
101 | int nvec, char **vec); | |
9ad9f8f7 RK |
102 | static gboolean playlist_new_keypress(GtkWidget *widget, |
103 | GdkEventKey *event, | |
104 | gpointer user_data); | |
105 | static gboolean playlist_picker_keypress(GtkWidget *widget, | |
106 | GdkEventKey *event, | |
107 | gpointer user_data); | |
ada57e73 RK |
108 | static void playlist_editor_button_toggled(GtkToggleButton *tb, |
109 | gpointer userdata); | |
110 | static void playlist_editor_set_buttons(const char *event, | |
111 | void *eventdata, | |
112 | void *callbackdata); | |
113 | static void playlist_editor_got_share(void *v, | |
114 | const char *err, | |
115 | const char *value); | |
116 | static void playlist_editor_share_set(void *v, const char *err); | |
018d1a8b RK |
117 | static void playlist_picker_update_section(const char *title, const char *key, |
118 | int start, int end); | |
119 | static gboolean playlist_picker_find(GtkTreeIter *parent, | |
120 | const char *title, const char *key, | |
121 | GtkTreeIter iter[1], | |
122 | gboolean create); | |
123 | static void playlist_picker_delete_obsolete(GtkTreeIter parent[1], | |
124 | char **exists, | |
125 | int nexists); | |
d64a314e RK |
126 | static gboolean playlist_picker_button(GtkWidget *widget, |
127 | GdkEventButton *event, | |
128 | gpointer user_data); | |
1e0a962a RK |
129 | static gboolean playlist_editor_keypress(GtkWidget *widget, |
130 | GdkEventKey *event, | |
131 | gpointer user_data); | |
ad424400 RK |
132 | static void playlist_editor_ok(GtkButton *button, gpointer userdata); |
133 | static void playlist_editor_help(GtkButton *button, gpointer userdata); | |
fc36ecb7 | 134 | |
f0bd437a | 135 | /** @brief Playlist editing window */ |
7fe2807d | 136 | static GtkWidget *playlist_window; |
f0bd437a | 137 | |
6bfaa2a1 RK |
138 | /** @brief Columns for the playlist editor */ |
139 | static const struct queue_column playlist_columns[] = { | |
140 | { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE }, | |
141 | { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE }, | |
142 | { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE }, | |
6bfaa2a1 RK |
143 | }; |
144 | ||
dba3eb8e RK |
145 | /** @brief Pop-up menu for playlist editor |
146 | * | |
147 | * Status: | |
148 | * - track properties works but, bizarrely, raises the main window | |
149 | * - play track works | |
150 | * - play playlist works | |
151 | * - select/deselect all work | |
152 | */ | |
6bfaa2a1 | 153 | static struct menuitem playlist_menuitems[] = { |
ba6f3dd4 RK |
154 | { "Track properties", GTK_STOCK_PROPERTIES, ql_properties_activate, ql_properties_sensitive, 0, 0 }, |
155 | { "Play track", GTK_STOCK_MEDIA_PLAY, ql_play_activate, ql_play_sensitive, 0, 0 }, | |
156 | { "Play playlist", NULL, playlist_playall_activate, playlist_playall_sensitive, 0, 0 }, | |
4750a901 | 157 | { "Remove track from playlist", GTK_STOCK_DELETE, playlist_remove_activate, playlist_remove_sensitive, 0, 0 }, |
ba6f3dd4 RK |
158 | { "Select all tracks", GTK_STOCK_SELECT_ALL, ql_selectall_activate, ql_selectall_sensitive, 0, 0 }, |
159 | { "Deselect all tracks", NULL, ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 }, | |
6bfaa2a1 RK |
160 | }; |
161 | ||
a6712ea8 RK |
162 | static const GtkTargetEntry playlist_targets[] = { |
163 | { | |
164 | PLAYLIST_TRACKS, /* drag type */ | |
165 | GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */ | |
166 | PLAYLIST_TRACKS_ID /* ID value */ | |
167 | }, | |
168 | { | |
169 | PLAYABLE_TRACKS, /* drag type */ | |
170 | GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ | |
171 | PLAYABLE_TRACKS_ID, /* ID value */ | |
172 | }, | |
173 | { | |
174 | .target = NULL | |
175 | } | |
176 | }; | |
177 | ||
6bfaa2a1 RK |
178 | /** @brief Queuelike for editing a playlist */ |
179 | static struct queuelike ql_playlist = { | |
180 | .name = "playlist", | |
181 | .columns = playlist_columns, | |
182 | .ncolumns = sizeof playlist_columns / sizeof *playlist_columns, | |
183 | .menuitems = playlist_menuitems, | |
184 | .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems, | |
a6712ea8 RK |
185 | .drop = playlist_drop, |
186 | .drag_source_targets = playlist_targets, | |
187 | .drag_source_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, | |
188 | .drag_dest_targets = playlist_targets, | |
189 | .drag_dest_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, | |
6bfaa2a1 RK |
190 | }; |
191 | ||
3c1a4e96 | 192 | /* Maintaining the list of playlists ---------------------------------------- */ |
121944d1 | 193 | |
b5e60f0d RK |
194 | /** @brief Current list of playlists or NULL */ |
195 | char **playlists; | |
196 | ||
197 | /** @brief Count of playlists */ | |
198 | int nplaylists; | |
199 | ||
200 | /** @brief Schedule an update to the list of playlists | |
201 | * | |
202 | * Called periodically and when a playlist is created or deleted. | |
203 | */ | |
204 | static void playlist_list_update(const char attribute((unused)) *event, | |
205 | void attribute((unused)) *eventdata, | |
206 | void attribute((unused)) *callbackdata) { | |
207 | disorder_eclient_playlists(client, playlist_list_received_playlists, 0); | |
fc36ecb7 RK |
208 | } |
209 | ||
c41b2cac RK |
210 | /** @brief Called with a new list of playlists */ |
211 | static void playlist_list_received_playlists(void attribute((unused)) *v, | |
212 | const char *err, | |
213 | int nvec, char **vec) { | |
214 | if(err) { | |
215 | playlists = 0; | |
216 | nplaylists = -1; | |
217 | /* Probably means server does not support playlists */ | |
218 | } else { | |
219 | playlists = vec; | |
220 | nplaylists = nvec; | |
221 | qsort(playlists, nplaylists, sizeof (char *), playlistcmp); | |
222 | } | |
223 | /* Tell our consumers */ | |
224 | event_raise("playlists-updated", 0); | |
225 | } | |
226 | ||
fc36ecb7 RK |
227 | /** @brief qsort() callback for playlist name comparison */ |
228 | static int playlistcmp(const void *ap, const void *bp) { | |
229 | const char *a = *(char **)ap, *b = *(char **)bp; | |
230 | const char *ad = strchr(a, '.'), *bd = strchr(b, '.'); | |
231 | int c; | |
232 | ||
233 | /* Group owned playlists by owner */ | |
234 | if(ad && bd) { | |
235 | const int adn = ad - a, bdn = bd - b; | |
236 | if((c = strncmp(a, b, adn < bdn ? adn : bdn))) | |
237 | return c; | |
238 | /* Lexical order within playlists of a single owner */ | |
239 | return strcmp(ad + 1, bd + 1); | |
240 | } | |
241 | ||
242 | /* Owned playlists after shared ones */ | |
243 | if(ad) { | |
244 | return 1; | |
245 | } else if(bd) { | |
246 | return -1; | |
247 | } | |
248 | ||
249 | /* Lexical order of shared playlists */ | |
250 | return strcmp(a, b); | |
251 | } | |
252 | ||
121944d1 RK |
253 | /* Playlists menu ----------------------------------------------------------- */ |
254 | ||
b35f7c0f RK |
255 | static void playlist_menu_playing(void attribute((unused)) *v, |
256 | const char *err) { | |
257 | if(err) | |
2d144dda | 258 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
b35f7c0f RK |
259 | } |
260 | ||
121944d1 RK |
261 | /** @brief Play received playlist contents |
262 | * | |
263 | * Passed as a completion callback by menu_activate_playlist(). | |
264 | */ | |
b5e60f0d RK |
265 | static void playlist_menu_received_content(void attribute((unused)) *v, |
266 | const char *err, | |
267 | int nvec, char **vec) { | |
121944d1 | 268 | if(err) { |
2d144dda | 269 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
121944d1 RK |
270 | return; |
271 | } | |
272 | for(int n = 0; n < nvec; ++n) | |
b35f7c0f | 273 | disorder_eclient_play(client, vec[n], playlist_menu_playing, NULL); |
121944d1 RK |
274 | } |
275 | ||
276 | /** @brief Called to activate a playlist | |
277 | * | |
278 | * Called when the menu item for a playlist is clicked. | |
279 | */ | |
b5e60f0d | 280 | static void playlist_menu_activate(GtkMenuItem *menuitem, |
f9b20469 RK |
281 | gpointer attribute((unused)) user_data) { |
282 | GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child); | |
283 | const char *playlist = gtk_label_get_text(label); | |
284 | ||
b5e60f0d RK |
285 | disorder_eclient_playlist_get(client, playlist_menu_received_content, |
286 | playlist, NULL); | |
f9b20469 RK |
287 | } |
288 | ||
b5e60f0d RK |
289 | /** @brief Called when the playlists change |
290 | * | |
291 | * Naively refills the menu. The results might be unsettling if the menu is | |
292 | * currently open, but this is hopefuly fairly rare. | |
293 | */ | |
294 | static void playlist_menu_changed(const char attribute((unused)) *event, | |
295 | void attribute((unused)) *eventdata, | |
296 | void attribute((unused)) *callbackdata) { | |
f9b20469 RK |
297 | if(!playlists_menu) |
298 | return; /* OMG too soon */ | |
299 | GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu); | |
f9b20469 RK |
300 | while(menu->children) |
301 | gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data)); | |
302 | /* NB nplaylists can be -1 as well as 0 */ | |
303 | for(int n = 0; n < nplaylists; ++n) { | |
304 | GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]); | |
b5e60f0d | 305 | g_signal_connect(w, "activate", G_CALLBACK(playlist_menu_activate), 0); |
f9b20469 RK |
306 | gtk_widget_show(w); |
307 | gtk_menu_shell_append(menu, w); | |
308 | } | |
fdea9f40 | 309 | gtk_widget_set_sensitive(menu_playlists_widget, |
f9b20469 | 310 | nplaylists > 0); |
fdea9f40 | 311 | gtk_widget_set_sensitive(menu_editplaylists_widget, |
6acdbba4 | 312 | nplaylists >= 0); |
f9b20469 RK |
313 | } |
314 | ||
7f7c3819 RK |
315 | /* Popup to create a new playlist ------------------------------------------- */ |
316 | ||
317 | /** @brief New-playlist popup */ | |
318 | static GtkWidget *playlist_new_window; | |
319 | ||
320 | /** @brief Text entry in new-playlist popup */ | |
321 | static GtkWidget *playlist_new_entry; | |
322 | ||
b5e60f0d | 323 | /** @brief Label for displaying feedback on what's wrong */ |
7f7c3819 RK |
324 | static GtkWidget *playlist_new_info; |
325 | ||
b5e60f0d | 326 | /** @brief "Shared" radio button */ |
7f7c3819 | 327 | static GtkWidget *playlist_new_shared; |
b5e60f0d RK |
328 | |
329 | /** @brief "Public" radio button */ | |
7f7c3819 | 330 | static GtkWidget *playlist_new_public; |
b5e60f0d RK |
331 | |
332 | /** @brief "Private" radio button */ | |
7f7c3819 RK |
333 | static GtkWidget *playlist_new_private; |
334 | ||
c41b2cac RK |
335 | /** @brief Buttons for new-playlist popup */ |
336 | static struct button playlist_new_buttons[] = { | |
337 | { | |
338 | .stock = GTK_STOCK_OK, | |
339 | .clicked = playlist_new_ok, | |
340 | .tip = "Create new playlist" | |
341 | }, | |
342 | { | |
343 | .stock = GTK_STOCK_CANCEL, | |
344 | .clicked = playlist_new_cancel, | |
345 | .tip = "Do not create new playlist" | |
7c12e4bd | 346 | } |
c41b2cac RK |
347 | }; |
348 | #define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons) | |
349 | ||
350 | /** @brief Pop up a new window to enter the playlist name and details */ | |
351 | static void playlist_new_playlist(void) { | |
352 | assert(playlist_new_window == NULL); | |
353 | playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
354 | g_signal_connect(playlist_new_window, "destroy", | |
355 | G_CALLBACK(gtk_widget_destroyed), &playlist_new_window); | |
356 | gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist"); | |
357 | /* Window will be modal, suppressing access to other windows */ | |
358 | gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE); | |
359 | gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window), | |
360 | GTK_WINDOW(playlist_window)); | |
361 | ||
362 | /* Window contents will use a table (grid) layout */ | |
363 | GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/); | |
364 | ||
365 | /* First row: playlist name */ | |
366 | gtk_table_attach_defaults(GTK_TABLE(table), | |
367 | gtk_label_new("Playlist name"), | |
368 | 0, 1, 0, 1); | |
369 | playlist_new_entry = gtk_entry_new(); | |
370 | g_signal_connect(playlist_new_entry, "changed", | |
371 | G_CALLBACK(playlist_new_entry_edited), NULL); | |
372 | gtk_table_attach_defaults(GTK_TABLE(table), | |
373 | playlist_new_entry, | |
374 | 1, 3, 0, 1); | |
375 | ||
376 | /* Second row: radio buttons to choose type */ | |
377 | playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared"); | |
378 | playlist_new_public | |
379 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), | |
380 | "public"); | |
381 | playlist_new_private | |
382 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared), | |
383 | "private"); | |
384 | g_signal_connect(playlist_new_shared, "toggled", | |
385 | G_CALLBACK(playlist_new_button_toggled), NULL); | |
386 | g_signal_connect(playlist_new_public, "toggled", | |
387 | G_CALLBACK(playlist_new_button_toggled), NULL); | |
388 | g_signal_connect(playlist_new_private, "toggled", | |
389 | G_CALLBACK(playlist_new_button_toggled), NULL); | |
390 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2); | |
391 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2); | |
392 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2); | |
393 | ||
394 | /* Third row: info bar saying why not */ | |
395 | playlist_new_info = gtk_label_new(""); | |
396 | gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info, | |
397 | 0, 3, 2, 3); | |
398 | ||
399 | /* Fourth row: ok/cancel buttons */ | |
400 | GtkWidget *hbox = create_buttons_box(playlist_new_buttons, | |
401 | NPLAYLIST_NEW_BUTTONS, | |
402 | gtk_hbox_new(FALSE, 0)); | |
403 | gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4); | |
404 | ||
405 | gtk_container_add(GTK_CONTAINER(playlist_new_window), | |
406 | frame_widget(table, NULL)); | |
407 | ||
408 | /* Set initial state of OK button */ | |
409 | playlist_new_changed(0,0,0); | |
410 | ||
9ad9f8f7 RK |
411 | g_signal_connect(playlist_new_window, "key-press-event", |
412 | G_CALLBACK(playlist_new_keypress), 0); | |
c41b2cac RK |
413 | |
414 | /* Display the window */ | |
415 | gtk_widget_show_all(playlist_new_window); | |
7c12e4bd RK |
416 | } |
417 | ||
9ad9f8f7 RK |
418 | /** @brief Keypress handler */ |
419 | static gboolean playlist_new_keypress(GtkWidget attribute((unused)) *widget, | |
420 | GdkEventKey *event, | |
421 | gpointer attribute((unused)) user_data) { | |
422 | if(event->state) | |
423 | return FALSE; | |
424 | switch(event->keyval) { | |
425 | case GDK_Return: | |
426 | playlist_new_ok(NULL, NULL); | |
427 | return TRUE; | |
428 | case GDK_Escape: | |
429 | gtk_widget_destroy(playlist_new_window); | |
430 | return TRUE; | |
431 | default: | |
432 | return FALSE; | |
433 | } | |
434 | } | |
435 | ||
c41b2cac RK |
436 | /** @brief Called when 'ok' is clicked in new-playlist popup */ |
437 | static void playlist_new_ok(GtkButton attribute((unused)) *button, | |
438 | gpointer attribute((unused)) userdata) { | |
9ad9f8f7 RK |
439 | if(playlist_new_valid()) |
440 | return; | |
c41b2cac RK |
441 | gboolean shared, public, private; |
442 | char *name, *fullname; | |
443 | playlist_new_details(&name, &fullname, &shared, &public, &private); | |
444 | ||
445 | /* We need to: | |
446 | * - lock the playlist | |
447 | * - check it doesn't exist | |
448 | * - set sharing (which will create it empty | |
449 | * - unlock it | |
450 | * | |
451 | * TODO we should freeze the window while this is going on to stop a second | |
452 | * click. | |
453 | */ | |
454 | disorder_eclient_playlist_lock(client, playlist_new_locked, fullname, | |
455 | fullname); | |
7c12e4bd RK |
456 | } |
457 | ||
c41b2cac RK |
458 | /** @brief Called when the proposed new playlist has been locked */ |
459 | static void playlist_new_locked(void *v, const char *err) { | |
460 | char *fullname = v; | |
7c12e4bd | 461 | if(err) { |
2d144dda | 462 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
7c12e4bd RK |
463 | return; |
464 | } | |
c41b2cac RK |
465 | disorder_eclient_playlist_get(client, playlist_new_retrieved, |
466 | fullname, fullname); | |
7c12e4bd RK |
467 | } |
468 | ||
469 | /** @brief Called when the proposed new playlist's contents have been retrieved | |
470 | * | |
471 | * ...or rather, normally, when it's been reported that it does not exist. | |
472 | */ | |
473 | static void playlist_new_retrieved(void *v, const char *err, | |
474 | int nvec, | |
475 | char attribute((unused)) **vec) { | |
476 | char *fullname = v; | |
477 | if(!err && nvec != -1) | |
478 | /* A rare case but not in principle impossible */ | |
479 | err = "A playlist with that name already exists."; | |
480 | if(err) { | |
2d144dda | 481 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
7c12e4bd RK |
482 | disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname); |
483 | return; | |
484 | } | |
485 | gboolean shared, public, private; | |
486 | playlist_new_details(0, 0, &shared, &public, &private); | |
487 | disorder_eclient_playlist_set_share(client, playlist_new_created, fullname, | |
488 | public ? "public" | |
489 | : private ? "private" | |
490 | : "shared", | |
491 | fullname); | |
492 | } | |
493 | ||
c41b2cac RK |
494 | /** @brief Called when the new playlist has been created */ |
495 | static void playlist_new_created(void attribute((unused)) *v, const char *err) { | |
7c12e4bd | 496 | if(err) { |
2d144dda | 497 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
7c12e4bd RK |
498 | return; |
499 | } | |
c41b2cac RK |
500 | disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL); |
501 | // TODO arrange for the new playlist to be selected | |
7c12e4bd RK |
502 | } |
503 | ||
c41b2cac RK |
504 | /** @brief Called when the newly created playlist has unlocked */ |
505 | static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) { | |
506 | if(err) | |
2d144dda | 507 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
c41b2cac RK |
508 | /* Pop down the creation window */ |
509 | gtk_widget_destroy(playlist_new_window); | |
7f7c3819 RK |
510 | } |
511 | ||
512 | /** @brief Called when 'cancel' is clicked in new-playlist popup */ | |
513 | static void playlist_new_cancel(GtkButton attribute((unused)) *button, | |
514 | gpointer attribute((unused)) userdata) { | |
515 | gtk_widget_destroy(playlist_new_window); | |
516 | } | |
517 | ||
c41b2cac RK |
518 | /** @brief Called when some radio button in the new-playlist popup changes */ |
519 | static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) *tb, | |
520 | gpointer attribute((unused)) userdata) { | |
521 | playlist_new_changed(0,0,0); | |
522 | } | |
523 | ||
524 | /** @brief Called when the text entry field in the new-playlist popup changes */ | |
525 | static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable, | |
526 | gpointer attribute((unused)) user_data) { | |
527 | playlist_new_changed(0,0,0); | |
528 | } | |
529 | ||
530 | /** @brief Called to update new playlist window state | |
531 | * | |
532 | * This is called whenever one the text entry or radio buttons changed, and | |
533 | * also when the set of known playlists changes. It determines whether the new | |
534 | * playlist would be creatable and sets the sensitivity of the OK button | |
535 | * and info display accordingly. | |
536 | */ | |
537 | static void playlist_new_changed(const char attribute((unused)) *event, | |
538 | void attribute((unused)) *eventdata, | |
539 | void attribute((unused)) *callbackdata) { | |
540 | if(!playlist_new_window) | |
541 | return; | |
542 | const char *reason = playlist_new_valid(); | |
543 | gtk_widget_set_sensitive(playlist_new_buttons[0].widget, | |
544 | !reason); | |
545 | gtk_label_set_text(GTK_LABEL(playlist_new_info), reason); | |
546 | } | |
7f7c3819 | 547 | |
7c12e4bd RK |
548 | /** @brief Test whether the new-playlist window settings are valid |
549 | * @return NULL on success or an error string if not | |
550 | */ | |
7f7c3819 RK |
551 | static const char *playlist_new_valid(void) { |
552 | gboolean shared, public, private; | |
7c12e4bd RK |
553 | char *name, *fullname; |
554 | playlist_new_details(&name, &fullname, &shared, &public, &private); | |
7f7c3819 RK |
555 | if(!(shared || public || private)) |
556 | return "No type set."; | |
7f7c3819 RK |
557 | if(!*name) |
558 | return ""; | |
7f7c3819 | 559 | /* See if the result is valid */ |
7c12e4bd RK |
560 | if(!valid_username(name) |
561 | || playlist_parse_name(fullname, NULL, NULL)) | |
7f7c3819 | 562 | return "Not a valid playlist name."; |
b5e60f0d RK |
563 | /* See if the result clashes with an existing name. This is not a perfect |
564 | * check, the playlist might be created after this point but before we get a | |
565 | * chance to disable the "OK" button. However when we try to create the | |
566 | * playlist we will first try to retrieve it, with a lock held, so we | |
567 | * shouldn't end up overwriting anything. */ | |
7f7c3819 RK |
568 | for(int n = 0; n < nplaylists; ++n) |
569 | if(!strcmp(playlists[n], fullname)) { | |
570 | if(shared) | |
571 | return "A shared playlist with that name already exists."; | |
572 | else | |
573 | return "You already have a playlist with that name."; | |
574 | } | |
575 | /* As far as we can tell creation would work */ | |
576 | return NULL; | |
577 | } | |
578 | ||
c41b2cac RK |
579 | /** @brief Get entered new-playlist details |
580 | * @param namep Where to store entered name (or NULL) | |
581 | * @param fullnamep Where to store computed full name (or NULL) | |
582 | * @param sharep Where to store 'shared' flag (or NULL) | |
583 | * @param publicp Where to store 'public' flag (or NULL) | |
584 | * @param privatep Where to store 'private' flag (or NULL) | |
7f7c3819 | 585 | */ |
c41b2cac RK |
586 | static void playlist_new_details(char **namep, |
587 | char **fullnamep, | |
588 | gboolean *sharedp, | |
589 | gboolean *publicp, | |
590 | gboolean *privatep) { | |
591 | gboolean shared, public, private; | |
592 | g_object_get(playlist_new_shared, "active", &shared, (char *)NULL); | |
593 | g_object_get(playlist_new_public, "active", &public, (char *)NULL); | |
594 | g_object_get(playlist_new_private, "active", &private, (char *)NULL); | |
595 | char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry), | |
596 | 0, -1); /* name owned by calle */ | |
597 | char *name = xstrdup(gname); | |
598 | g_free(gname); | |
599 | if(sharedp) *sharedp = shared; | |
600 | if(publicp) *publicp = public; | |
601 | if(privatep) *privatep = private; | |
602 | if(namep) *namep = name; | |
603 | if(fullnamep) { | |
604 | if(*sharedp) *fullnamep = *namep; | |
605 | else byte_xasprintf(fullnamep, "%s.%s", config->username, name); | |
606 | } | |
7f7c3819 RK |
607 | } |
608 | ||
b5e60f0d | 609 | /* Playlist picker ---------------------------------------------------------- */ |
121944d1 | 610 | |
b5e60f0d RK |
611 | /** @brief Delete button */ |
612 | static GtkWidget *playlist_picker_delete_button; | |
613 | ||
018d1a8b RK |
614 | /** @brief Tree model for list of playlists |
615 | * | |
616 | * This has two columns: | |
617 | * - column 0 will be the display name | |
618 | * - column 1 will be the sort key/playlist name (and will not be displayed) | |
619 | */ | |
620 | static GtkTreeStore *playlist_picker_list; | |
b5e60f0d RK |
621 | |
622 | /** @brief Selection for list of playlists */ | |
623 | static GtkTreeSelection *playlist_picker_selection; | |
624 | ||
625 | /** @brief Currently selected playlist */ | |
626 | static const char *playlist_picker_selected; | |
627 | ||
628 | /** @brief (Re-)populate the playlist picker tree model */ | |
629 | static void playlist_picker_fill(const char attribute((unused)) *event, | |
630 | void attribute((unused)) *eventdata, | |
631 | void attribute((unused)) *callbackdata) { | |
7fe2807d | 632 | if(!playlist_window) |
d571e07b | 633 | return; |
b5e60f0d | 634 | if(!playlist_picker_list) |
018d1a8b RK |
635 | playlist_picker_list = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_STRING); |
636 | /* We will accumulate a list of all the sections that exist */ | |
637 | char **sections = xcalloc(nplaylists, sizeof (char *)); | |
638 | int nsections = 0; | |
639 | /* Make sure shared playlists are there */ | |
640 | int start = 0, end; | |
641 | for(end = start; end < nplaylists && !strchr(playlists[end], '.'); ++end) | |
642 | ; | |
643 | if(start != end) { | |
644 | playlist_picker_update_section("Shared playlists", "", | |
645 | start, end); | |
646 | sections[nsections++] = (char *)""; | |
647 | } | |
648 | /* Make sure owned playlists are there */ | |
649 | while((start = end) < nplaylists) { | |
650 | const int nl = strchr(playlists[start], '.') - playlists[start]; | |
651 | char *name = xstrndup(playlists[start], nl); | |
652 | for(end = start; | |
653 | end < nplaylists | |
654 | && playlists[end][nl] == '.' | |
655 | && !strncmp(playlists[start], playlists[end], nl); | |
656 | ++end) | |
657 | ; | |
658 | playlist_picker_update_section(name, name, start, end); | |
659 | sections[nsections++] = name; | |
660 | } | |
661 | /* Delete obsolete sections */ | |
662 | playlist_picker_delete_obsolete(NULL, sections, nsections); | |
663 | } | |
664 | ||
665 | /** @brief Update a section in the picker tree model | |
666 | * @param section Section name | |
667 | * @param start First entry in @ref playlists | |
668 | * @param end Past last entry in @ref playlists | |
669 | */ | |
670 | static void playlist_picker_update_section(const char *title, const char *key, | |
671 | int start, int end) { | |
672 | /* Find the section, creating it if necessary */ | |
673 | GtkTreeIter section_iter[1]; | |
674 | playlist_picker_find(NULL, title, key, section_iter, TRUE); | |
675 | /* Add missing rows */ | |
676 | for(int n = start; n < end; ++n) { | |
677 | GtkTreeIter child[1]; | |
678 | char *name; | |
679 | if((name = strchr(playlists[n], '.'))) | |
680 | ++name; | |
681 | else | |
682 | name = playlists[n]; | |
683 | playlist_picker_find(section_iter, | |
684 | name, playlists[n], | |
685 | child, | |
686 | TRUE); | |
687 | } | |
688 | /* Delete anything that shouldn't exist. */ | |
689 | playlist_picker_delete_obsolete(section_iter, playlists + start, end - start); | |
690 | } | |
691 | ||
692 | /** @brief Find and maybe create a row in the picker tree model | |
693 | * @param parent Parent iterator (or NULL for top level) | |
694 | * @param title Display name of section | |
695 | * @param key Key to search for | |
696 | * @param iter Iterator to point at key | |
697 | * @param create If TRUE, key will be created if it doesn't exist | |
698 | * @param compare Row comparison function | |
699 | * @return TRUE if key exists else FALSE | |
700 | * | |
701 | * If the @p key exists then @p iter will point to it and TRUE will be | |
702 | * returned. | |
703 | * | |
704 | * If the @p key does not exist and @p create is TRUE then it will be created. | |
705 | * @p iter wil point to it and TRUE will be returned. | |
706 | * | |
707 | * If the @p key does not exist and @p create is FALSE then FALSE will be | |
708 | * returned. | |
709 | */ | |
710 | static gboolean playlist_picker_find(GtkTreeIter *parent, | |
711 | const char *title, | |
712 | const char *key, | |
713 | GtkTreeIter iter[1], | |
714 | gboolean create) { | |
715 | gchar *candidate; | |
716 | GtkTreeIter next[1]; | |
717 | gboolean it; | |
718 | int row = 0; | |
719 | ||
720 | it = gtk_tree_model_iter_children(GTK_TREE_MODEL(playlist_picker_list), | |
721 | next, | |
722 | parent); | |
723 | while(it) { | |
724 | /* Find the value at row 'next' */ | |
725 | gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), | |
726 | next, | |
727 | 1, &candidate, | |
728 | -1); | |
729 | /* See how it compares with @p key */ | |
730 | int c = strcmp(key, candidate); | |
731 | g_free(candidate); | |
732 | if(!c) { | |
733 | *iter = *next; | |
734 | return TRUE; /* we found our key */ | |
735 | } | |
736 | if(c < 0) { | |
737 | /* @p key belongs before row 'next' */ | |
738 | if(create) { | |
739 | gtk_tree_store_insert_with_values(playlist_picker_list, | |
740 | iter, | |
741 | parent, | |
742 | row, /* insert here */ | |
743 | 0, title, 1, key, -1); | |
744 | return TRUE; | |
745 | } else | |
746 | return FALSE; | |
747 | ++row; | |
748 | } | |
749 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(playlist_picker_list), next); | |
750 | } | |
751 | /* We have reached the end and not found a row that should be later than @p | |
752 | * key. */ | |
753 | if(create) { | |
754 | gtk_tree_store_insert_with_values(playlist_picker_list, | |
755 | iter, | |
756 | parent, | |
757 | INT_MAX, /* insert at end */ | |
758 | 0, title, 1, key, -1); | |
759 | return TRUE; | |
760 | } else | |
761 | return FALSE; | |
762 | } | |
763 | ||
764 | /** @brief Delete obsolete rows | |
765 | * @param parent Parent or NULL | |
766 | * @param exists List of rows that should exist (by key) | |
767 | * @param nexists Length of @p exists | |
768 | */ | |
769 | static void playlist_picker_delete_obsolete(GtkTreeIter parent[1], | |
770 | char **exists, | |
771 | int nexists) { | |
772 | /* Delete anything that shouldn't exist. */ | |
773 | GtkTreeIter iter[1]; | |
774 | gboolean it = gtk_tree_model_iter_children(GTK_TREE_MODEL(playlist_picker_list), | |
775 | iter, | |
776 | parent); | |
777 | while(it) { | |
778 | /* Find the value at row 'next' */ | |
779 | gchar *candidate; | |
780 | gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), | |
781 | iter, | |
782 | 1, &candidate, | |
783 | -1); | |
784 | gboolean found = FALSE; | |
785 | for(int n = 0; n < nexists; ++n) | |
786 | if((found = !strcmp(candidate, exists[n]))) | |
787 | break; | |
788 | if(!found) | |
789 | it = gtk_tree_store_remove(playlist_picker_list, iter); | |
790 | else | |
791 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(playlist_picker_list), | |
792 | iter); | |
793 | g_free(candidate); | |
d571e07b | 794 | } |
f0bd437a RK |
795 | } |
796 | ||
797 | /** @brief Called when the selection might have changed */ | |
b5e60f0d RK |
798 | static void playlist_picker_selection_changed(GtkTreeSelection attribute((unused)) *treeselection, |
799 | gpointer attribute((unused)) user_data) { | |
f0bd437a RK |
800 | GtkTreeIter iter; |
801 | char *gselected, *selected; | |
802 | ||
803 | /* Identify the current selection */ | |
018d1a8b RK |
804 | if(gtk_tree_selection_get_selected(playlist_picker_selection, 0, &iter) |
805 | && gtk_tree_store_iter_depth(playlist_picker_list, &iter) > 0) { | |
b5e60f0d | 806 | gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list), &iter, |
018d1a8b | 807 | 1, &gselected, -1); |
f0bd437a RK |
808 | selected = xstrdup(gselected); |
809 | g_free(gselected); | |
810 | } else | |
811 | selected = 0; | |
d9b141cc | 812 | /* Set button sensitivity according to the new state */ |
9c6a77c2 RK |
813 | int deletable = FALSE; |
814 | if(selected) { | |
815 | if(strchr(selected, '.')) { | |
816 | if(!strncmp(selected, config->username, strlen(config->username))) | |
817 | deletable = TRUE; | |
818 | } else | |
819 | deletable = TRUE; | |
820 | } | |
821 | gtk_widget_set_sensitive(playlist_picker_delete_button, deletable); | |
f0bd437a | 822 | /* Eliminate no-change cases */ |
b5e60f0d | 823 | if(!selected && !playlist_picker_selected) |
f0bd437a | 824 | return; |
b5e60f0d RK |
825 | if(selected |
826 | && playlist_picker_selected | |
827 | && !strcmp(selected, playlist_picker_selected)) | |
f0bd437a | 828 | return; |
d9b141cc | 829 | /* Record the new state */ |
b5e60f0d | 830 | playlist_picker_selected = selected; |
7e702780 RK |
831 | /* Re-initalize the queue */ |
832 | ql_new_queue(&ql_playlist, NULL); | |
ada57e73 RK |
833 | /* Synthesize a playlist-modified to re-initialize the editor etc */ |
834 | event_raise("playlist-modified", (void *)playlist_picker_selected); | |
f0bd437a RK |
835 | } |
836 | ||
837 | /** @brief Called when the 'add' button is pressed */ | |
b5e60f0d RK |
838 | static void playlist_picker_add(GtkButton attribute((unused)) *button, |
839 | gpointer attribute((unused)) userdata) { | |
7c12e4bd | 840 | /* Unselect whatever is selected TODO why?? */ |
b5e60f0d RK |
841 | gtk_tree_selection_unselect_all(playlist_picker_selection); |
842 | playlist_new_playlist(); | |
f0bd437a RK |
843 | } |
844 | ||
d9b141cc | 845 | /** @brief Called when playlist deletion completes */ |
b5e60f0d RK |
846 | static void playlists_picker_delete_completed(void attribute((unused)) *v, |
847 | const char *err) { | |
d9b141cc | 848 | if(err) |
2d144dda | 849 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
d9b141cc RK |
850 | } |
851 | ||
f0bd437a | 852 | /** @brief Called when the 'Delete' button is pressed */ |
b5e60f0d RK |
853 | static void playlist_picker_delete(GtkButton attribute((unused)) *button, |
854 | gpointer attribute((unused)) userdata) { | |
f0bd437a RK |
855 | GtkWidget *yesno; |
856 | int res; | |
857 | ||
b5e60f0d | 858 | if(!playlist_picker_selected) |
9ad9f8f7 | 859 | return; |
7fe2807d | 860 | yesno = gtk_message_dialog_new(GTK_WINDOW(playlist_window), |
f0bd437a RK |
861 | GTK_DIALOG_MODAL, |
862 | GTK_MESSAGE_QUESTION, | |
863 | GTK_BUTTONS_YES_NO, | |
c5782050 | 864 | "Do you really want to delete playlist %s?" |
f0bd437a | 865 | " This action cannot be undone.", |
b5e60f0d | 866 | playlist_picker_selected); |
f0bd437a RK |
867 | res = gtk_dialog_run(GTK_DIALOG(yesno)); |
868 | gtk_widget_destroy(yesno); | |
869 | if(res == GTK_RESPONSE_YES) { | |
870 | disorder_eclient_playlist_delete(client, | |
b5e60f0d RK |
871 | playlists_picker_delete_completed, |
872 | playlist_picker_selected, | |
f0bd437a RK |
873 | NULL); |
874 | } | |
875 | } | |
876 | ||
877 | /** @brief Table of buttons below the playlist list */ | |
b5e60f0d | 878 | static struct button playlist_picker_buttons[] = { |
f0bd437a RK |
879 | { |
880 | GTK_STOCK_ADD, | |
b5e60f0d | 881 | playlist_picker_add, |
f0bd437a | 882 | "Create a new playlist", |
ad424400 RK |
883 | 0, |
884 | NULL, | |
f0bd437a RK |
885 | }, |
886 | { | |
887 | GTK_STOCK_REMOVE, | |
b5e60f0d | 888 | playlist_picker_delete, |
f0bd437a | 889 | "Delete a playlist", |
ad424400 RK |
890 | 0, |
891 | NULL, | |
f0bd437a RK |
892 | }, |
893 | }; | |
b5e60f0d | 894 | #define NPLAYLIST_PICKER_BUTTONS (sizeof playlist_picker_buttons / sizeof *playlist_picker_buttons) |
f0bd437a | 895 | |
506e02d8 | 896 | /** @brief Create the list of playlists for the edit playlists window */ |
b5e60f0d | 897 | static GtkWidget *playlist_picker_create(void) { |
506e02d8 | 898 | /* Create the list of playlist and populate it */ |
b5e60f0d | 899 | playlist_picker_fill(NULL, NULL, NULL); |
506e02d8 | 900 | /* Create the tree view */ |
b5e60f0d | 901 | GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlist_picker_list)); |
506e02d8 RK |
902 | /* ...and the renderers for it */ |
903 | GtkCellRenderer *cr = gtk_cell_renderer_text_new(); | |
904 | GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist", | |
905 | cr, | |
906 | "text", 0, | |
907 | NULL); | |
908 | gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col); | |
909 | /* Get the selection for the view; set its mode; arrange for a callback when | |
910 | * it changes */ | |
b5e60f0d RK |
911 | playlist_picker_selected = NULL; |
912 | playlist_picker_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)); | |
913 | gtk_tree_selection_set_mode(playlist_picker_selection, GTK_SELECTION_BROWSE); | |
914 | g_signal_connect(playlist_picker_selection, "changed", | |
915 | G_CALLBACK(playlist_picker_selection_changed), NULL); | |
506e02d8 RK |
916 | |
917 | /* Create the control buttons */ | |
b5e60f0d RK |
918 | GtkWidget *buttons = create_buttons_box(playlist_picker_buttons, |
919 | NPLAYLIST_PICKER_BUTTONS, | |
506e02d8 | 920 | gtk_hbox_new(FALSE, 1)); |
b5e60f0d | 921 | playlist_picker_delete_button = playlist_picker_buttons[1].widget; |
506e02d8 | 922 | |
b5e60f0d | 923 | playlist_picker_selection_changed(NULL, NULL); |
7c12e4bd | 924 | |
506e02d8 RK |
925 | /* Buttons live below the list */ |
926 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); | |
927 | gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0); | |
928 | gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0); | |
929 | ||
9ad9f8f7 RK |
930 | g_signal_connect(tree, "key-press-event", |
931 | G_CALLBACK(playlist_picker_keypress), 0); | |
d64a314e RK |
932 | g_signal_connect(tree, "button-press-event", |
933 | G_CALLBACK(playlist_picker_button), 0); | |
9ad9f8f7 | 934 | |
506e02d8 RK |
935 | return vbox; |
936 | } | |
937 | ||
9ad9f8f7 RK |
938 | static gboolean playlist_picker_keypress(GtkWidget attribute((unused)) *widget, |
939 | GdkEventKey *event, | |
940 | gpointer attribute((unused)) user_data) { | |
941 | if(event->state) | |
942 | return FALSE; | |
943 | switch(event->keyval) { | |
944 | case GDK_BackSpace: | |
945 | case GDK_Delete: | |
946 | playlist_picker_delete(NULL, NULL); | |
947 | return TRUE; | |
948 | default: | |
949 | return FALSE; | |
950 | } | |
951 | } | |
952 | ||
d64a314e RK |
953 | static void playlist_picker_select_activate(GtkMenuItem attribute((unused)) *item, |
954 | gpointer attribute((unused)) userdata) { | |
955 | /* nothing */ | |
956 | } | |
957 | ||
958 | static int playlist_picker_select_sensitive(void *extra) { | |
959 | GtkTreeIter *iter = extra; | |
960 | return gtk_tree_store_iter_depth(playlist_picker_list, iter) > 0; | |
961 | } | |
962 | ||
963 | static void playlist_picker_play_activate(GtkMenuItem attribute((unused)) *item, | |
964 | gpointer attribute((unused)) userdata) { | |
965 | /* Re-use the menu-based activation callback */ | |
966 | disorder_eclient_playlist_get(client, playlist_menu_received_content, | |
967 | playlist_picker_selected, NULL); | |
968 | } | |
969 | ||
970 | static int playlist_picker_play_sensitive(void *extra) { | |
971 | GtkTreeIter *iter = extra; | |
972 | return gtk_tree_store_iter_depth(playlist_picker_list, iter) > 0; | |
973 | } | |
974 | ||
975 | static void playlist_picker_remove_activate(GtkMenuItem attribute((unused)) *item, | |
976 | gpointer attribute((unused)) userdata) { | |
977 | /* Re-use the 'Remove' button' */ | |
978 | playlist_picker_delete(NULL, NULL); | |
979 | } | |
980 | ||
981 | static int playlist_picker_remove_sensitive(void *extra) { | |
982 | GtkTreeIter *iter = extra; | |
983 | if(gtk_tree_store_iter_depth(playlist_picker_list, iter) > 0) { | |
984 | if(strchr(playlist_picker_selected, '.')) { | |
985 | if(!strncmp(playlist_picker_selected, config->username, | |
986 | strlen(config->username))) | |
987 | return TRUE; | |
988 | } else | |
989 | return TRUE; | |
990 | } | |
991 | return FALSE; | |
992 | } | |
993 | ||
994 | /** @brief Pop-up menu for picker */ | |
995 | static struct menuitem playlist_picker_menuitems[] = { | |
996 | { | |
997 | "Select playlist", | |
ba6f3dd4 | 998 | NULL, |
d64a314e RK |
999 | playlist_picker_select_activate, |
1000 | playlist_picker_select_sensitive, | |
1001 | 0, | |
1002 | 0 | |
1003 | }, | |
1004 | { | |
1005 | "Play playlist", | |
ba6f3dd4 | 1006 | GTK_STOCK_MEDIA_PLAY, |
d64a314e RK |
1007 | playlist_picker_play_activate, |
1008 | playlist_picker_play_sensitive, | |
1009 | 0, | |
1010 | 0 | |
1011 | }, | |
1012 | { | |
1013 | "Remove playlist", | |
4750a901 | 1014 | GTK_STOCK_DELETE, |
d64a314e RK |
1015 | playlist_picker_remove_activate, |
1016 | playlist_picker_remove_sensitive, | |
1017 | 0, | |
1018 | 0 | |
1019 | }, | |
1020 | }; | |
1021 | ||
1022 | static gboolean playlist_picker_button(GtkWidget *widget, | |
1023 | GdkEventButton *event, | |
1024 | gpointer attribute((unused)) user_data) { | |
1025 | if(event->type == GDK_BUTTON_PRESS && event->button == 3) { | |
1026 | static GtkWidget *playlist_picker_menu; | |
1027 | ||
1028 | /* Right click press pops up a menu */ | |
1029 | ensure_selected(GTK_TREE_VIEW(widget), event); | |
1030 | /* Find the selected row */ | |
1031 | GtkTreeIter iter[1]; | |
1032 | if(!gtk_tree_selection_get_selected(playlist_picker_selection, 0, iter)) | |
1033 | return TRUE; | |
1034 | popup(&playlist_picker_menu, event, | |
1035 | playlist_picker_menuitems, | |
1036 | sizeof playlist_picker_menuitems / sizeof *playlist_picker_menuitems, | |
1037 | iter); | |
1038 | return TRUE; | |
1039 | } | |
1040 | return FALSE; | |
1041 | } | |
1042 | ||
a544b1cc RK |
1043 | static void playlist_picker_destroy(void) { |
1044 | playlist_picker_delete_button = NULL; | |
1045 | g_object_unref(playlist_picker_list); | |
1046 | playlist_picker_list = NULL; | |
1047 | playlist_picker_selection = NULL; | |
1048 | playlist_picker_selected = NULL; | |
1049 | } | |
1050 | ||
7fe2807d | 1051 | /* Playlist editor ---------------------------------------------------------- */ |
506e02d8 | 1052 | |
ada57e73 RK |
1053 | static GtkWidget *playlist_editor_shared; |
1054 | static GtkWidget *playlist_editor_public; | |
1055 | static GtkWidget *playlist_editor_private; | |
1056 | static int playlist_editor_setting_buttons; | |
1057 | ||
ad424400 RK |
1058 | /** @brief Buttons for the playlist window */ |
1059 | static struct button playlist_editor_buttons[] = { | |
1060 | { | |
1061 | GTK_STOCK_OK, | |
1062 | playlist_editor_ok, | |
1063 | "Close window", | |
1064 | 0, | |
1065 | gtk_box_pack_end, | |
1066 | }, | |
1067 | { | |
1068 | GTK_STOCK_HELP, | |
1069 | playlist_editor_help, | |
1070 | "Go to manual", | |
1071 | 0, | |
1072 | gtk_box_pack_end, | |
1073 | }, | |
1074 | }; | |
1075 | ||
1076 | #define NPLAYLIST_EDITOR_BUTTONS (int)(sizeof playlist_editor_buttons / sizeof *playlist_editor_buttons) | |
1077 | ||
c41b2cac RK |
1078 | static GtkWidget *playlists_editor_create(void) { |
1079 | assert(ql_playlist.view == NULL); /* better not be set up already */ | |
ada57e73 RK |
1080 | |
1081 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); | |
1082 | playlist_editor_shared = gtk_radio_button_new_with_label(NULL, "shared"); | |
1083 | playlist_editor_public | |
1084 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared), | |
1085 | "public"); | |
1086 | playlist_editor_private | |
1087 | = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared), | |
1088 | "private"); | |
1089 | g_signal_connect(playlist_editor_public, "toggled", | |
1090 | G_CALLBACK(playlist_editor_button_toggled), | |
1091 | (void *)"public"); | |
1092 | g_signal_connect(playlist_editor_private, "toggled", | |
1093 | G_CALLBACK(playlist_editor_button_toggled), | |
1094 | (void *)"private"); | |
1095 | gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_shared, | |
1096 | FALSE/*expand*/, FALSE/*fill*/, 0); | |
1097 | gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_public, | |
1098 | FALSE/*expand*/, FALSE/*fill*/, 0); | |
1099 | gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_private, | |
1100 | FALSE/*expand*/, FALSE/*fill*/, 0); | |
1101 | playlist_editor_set_buttons(0,0,0); | |
ad424400 RK |
1102 | create_buttons_box(playlist_editor_buttons, |
1103 | NPLAYLIST_EDITOR_BUTTONS, | |
1104 | hbox); | |
ada57e73 RK |
1105 | |
1106 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); | |
1e0a962a RK |
1107 | GtkWidget *view = init_queuelike(&ql_playlist); |
1108 | gtk_box_pack_start(GTK_BOX(vbox), view, | |
ada57e73 RK |
1109 | TRUE/*expand*/, TRUE/*fill*/, 0); |
1110 | gtk_box_pack_start(GTK_BOX(vbox), hbox, | |
1111 | FALSE/*expand*/, FALSE/*fill*/, 0); | |
1e0a962a RK |
1112 | g_signal_connect(view, "key-press-event", |
1113 | G_CALLBACK(playlist_editor_keypress), 0); | |
ada57e73 RK |
1114 | return vbox; |
1115 | } | |
1116 | ||
1e0a962a RK |
1117 | static gboolean playlist_editor_keypress(GtkWidget attribute((unused)) *widget, |
1118 | GdkEventKey *event, | |
1119 | gpointer attribute((unused)) user_data) { | |
1120 | if(event->state) | |
1121 | return FALSE; | |
1122 | switch(event->keyval) { | |
1123 | case GDK_BackSpace: | |
1124 | case GDK_Delete: | |
1125 | playlist_remove_activate(NULL, NULL); | |
1126 | return TRUE; | |
1127 | default: | |
1128 | return FALSE; | |
1129 | } | |
1130 | } | |
1131 | ||
ada57e73 RK |
1132 | /** @brief Called when the public/private buttons are set */ |
1133 | static void playlist_editor_button_toggled(GtkToggleButton *tb, | |
1134 | gpointer userdata) { | |
1135 | const char *state = userdata; | |
1136 | if(!gtk_toggle_button_get_active(tb) | |
1137 | || !playlist_picker_selected | |
1138 | || playlist_editor_setting_buttons) | |
1139 | return; | |
1140 | disorder_eclient_playlist_set_share(client, playlist_editor_share_set, | |
1141 | playlist_picker_selected, state, NULL); | |
1142 | } | |
1143 | ||
1144 | static void playlist_editor_share_set(void attribute((unused)) *v, | |
1145 | const attribute((unused)) char *err) { | |
1146 | if(err) | |
2d144dda | 1147 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
ada57e73 RK |
1148 | } |
1149 | ||
1150 | /** @brief Set the editor button state and sensitivity */ | |
1151 | static void playlist_editor_set_buttons(const char attribute((unused)) *event, | |
1152 | void *eventdata, | |
1153 | void attribute((unused)) *callbackdata) { | |
1154 | /* If this event is for a non-selected playlist do nothing */ | |
1155 | if(eventdata | |
1156 | && playlist_picker_selected | |
1157 | && strcmp(eventdata, playlist_picker_selected)) | |
1158 | return; | |
1159 | if(playlist_picker_selected) { | |
1160 | if(strchr(playlist_picker_selected, '.')) | |
1161 | disorder_eclient_playlist_get_share(client, | |
1162 | playlist_editor_got_share, | |
1163 | playlist_picker_selected, | |
1164 | (void *)playlist_picker_selected); | |
1165 | else | |
1166 | playlist_editor_got_share((void *)playlist_picker_selected, NULL, | |
1167 | "shared"); | |
1168 | } else | |
1169 | playlist_editor_got_share(NULL, NULL, NULL); | |
1170 | } | |
1171 | ||
1172 | /** @brief Called with playlist sharing details */ | |
1173 | static void playlist_editor_got_share(void *v, | |
1174 | const char *err, | |
1175 | const char *value) { | |
1176 | const char *playlist = v; | |
1177 | if(err) { | |
2d144dda | 1178 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
ada57e73 RK |
1179 | value = NULL; |
1180 | } | |
1181 | /* Set the currently active button */ | |
1182 | ++playlist_editor_setting_buttons; | |
1183 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_shared), | |
1184 | value && !strcmp(value, "shared")); | |
1185 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_public), | |
1186 | value && !strcmp(value, "public")); | |
1187 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_private), | |
1188 | value && !strcmp(value, "private")); | |
1189 | /* Set button sensitivity */ | |
1190 | gtk_widget_set_sensitive(playlist_editor_shared, FALSE); | |
1191 | int sensitive = (playlist | |
1192 | && strchr(playlist, '.') | |
1193 | && !strncmp(playlist, config->username, | |
1194 | strlen(config->username))); | |
1195 | gtk_widget_set_sensitive(playlist_editor_public, sensitive); | |
1196 | gtk_widget_set_sensitive(playlist_editor_private, sensitive); | |
1197 | --playlist_editor_setting_buttons; | |
c41b2cac RK |
1198 | } |
1199 | ||
1200 | /** @brief (Re-)populate the playlist tree model */ | |
1201 | static void playlist_editor_fill(const char attribute((unused)) *event, | |
1202 | void *eventdata, | |
1203 | void attribute((unused)) *callbackdata) { | |
1204 | const char *modified_playlist = eventdata; | |
1205 | if(!playlist_window) | |
1206 | return; | |
1207 | if(!playlist_picker_selected) | |
1208 | return; | |
1209 | if(!strcmp(playlist_picker_selected, modified_playlist)) | |
1210 | disorder_eclient_playlist_get(client, playlists_editor_received_tracks, | |
1211 | playlist_picker_selected, | |
1212 | (void *)playlist_picker_selected); | |
1213 | } | |
1214 | ||
7e702780 | 1215 | /** @brief Called with new tracks for the playlist */ |
f06a754c | 1216 | static void playlists_editor_received_tracks(void *v, |
7fe2807d RK |
1217 | const char *err, |
1218 | int nvec, char **vec) { | |
f06a754c | 1219 | const char *playlist = v; |
7e702780 | 1220 | if(err) { |
2d144dda | 1221 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
7e702780 RK |
1222 | return; |
1223 | } | |
f06a754c RK |
1224 | if(!playlist_picker_selected |
1225 | || strcmp(playlist, playlist_picker_selected)) { | |
1226 | /* The tracks are for the wrong playlist - something must have changed | |
1227 | * while the fetch command was in flight. We just ignore this callback, | |
1228 | * the right answer will be requested and arrive in due course. */ | |
1229 | return; | |
1230 | } | |
7e702780 RK |
1231 | if(nvec == -1) |
1232 | /* No such playlist, presumably we'll get a deleted event shortly */ | |
1233 | return; | |
1234 | /* Translate the list of tracks into queue entries */ | |
eaf4cd2e | 1235 | struct queue_entry *newq, **qq = &newq, *qprev = NULL; |
7e702780 RK |
1236 | hash *h = hash_new(sizeof(int)); |
1237 | for(int n = 0; n < nvec; ++n) { | |
1238 | struct queue_entry *q = xmalloc(sizeof *q); | |
8bbeb28e | 1239 | q->prev = qprev; |
7e702780 RK |
1240 | q->track = vec[n]; |
1241 | /* Synthesize a unique ID so that the selection survives updates. Tracks | |
1242 | * can appear more than once in the queue so we can't use raw track names, | |
1243 | * so we add a serial number to the start. */ | |
7e702780 RK |
1244 | int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0; |
1245 | byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]); | |
f57ff3aa | 1246 | hash_add(h, vec[n], &serial, HASH_INSERT_OR_REPLACE); |
7e702780 RK |
1247 | *qq = q; |
1248 | qq = &q->next; | |
8bbeb28e | 1249 | qprev = q; |
7e702780 RK |
1250 | } |
1251 | *qq = NULL; | |
7e702780 | 1252 | ql_new_queue(&ql_playlist, newq); |
7e702780 RK |
1253 | } |
1254 | ||
ad424400 RK |
1255 | static void playlist_editor_ok(GtkButton attribute((unused)) *button, |
1256 | gpointer attribute((unused)) userdata) { | |
1257 | gtk_widget_destroy(playlist_window); | |
1258 | } | |
1259 | ||
1260 | static void playlist_editor_help(GtkButton attribute((unused)) *button, | |
1261 | gpointer attribute((unused)) userdata) { | |
1262 | popup_help("playlists.html"); | |
1263 | } | |
1264 | ||
7a3eb8eb RK |
1265 | /* Playlist mutation -------------------------------------------------------- */ |
1266 | ||
1267 | /** @brief State structure for guarded playlist modification | |
1268 | * | |
f57ff3aa RK |
1269 | * To safely move, insert or delete rows we must: |
1270 | * - take a lock | |
1271 | * - fetch the playlist | |
1272 | * - verify it's not changed | |
1273 | * - update the playlist contents | |
1274 | * - store the playlist | |
1275 | * - release the lock | |
1276 | * | |
1277 | * The playlist_modify_ functions do just that. | |
1278 | * | |
7a3eb8eb RK |
1279 | * To kick things off create one of these and disorder_eclient_playlist_lock() |
1280 | * with playlist_modify_locked() as its callback. @c modify will be called; it | |
1281 | * should disorder_eclient_playlist_set() to set the new state with | |
1282 | * playlist_modify_updated() as its callback. | |
1283 | */ | |
1284 | struct playlist_modify_data { | |
1285 | /** @brief Affected playlist */ | |
1286 | const char *playlist; | |
1287 | /** @brief Modification function | |
1288 | * @param mod Pointer back to state structure | |
1289 | * @param ntracks Length of playlist | |
1290 | * @param tracks Tracks in playlist | |
1291 | */ | |
1292 | void (*modify)(struct playlist_modify_data *mod, | |
1293 | int ntracks, char **tracks); | |
1294 | ||
1295 | /** @brief Number of tracks dropped */ | |
1296 | int ntracks; | |
1297 | /** @brief Track names dropped */ | |
1298 | char **tracks; | |
1299 | /** @brief Track IDs dropped */ | |
1300 | char **ids; | |
1301 | /** @brief Drop after this point */ | |
1302 | struct queue_entry *after_me; | |
1303 | }; | |
1304 | ||
1305 | /** @brief Called with playlist locked | |
1306 | * | |
1307 | * This is the entry point for guarded modification ising @ref | |
1308 | * playlist_modify_data. | |
1309 | */ | |
1310 | static void playlist_modify_locked(void *v, const char *err) { | |
1311 | struct playlist_modify_data *mod = v; | |
1312 | if(err) { | |
2d144dda | 1313 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
7a3eb8eb RK |
1314 | return; |
1315 | } | |
1316 | disorder_eclient_playlist_get(client, playlist_modify_retrieved, | |
1317 | mod->playlist, mod); | |
1318 | } | |
1319 | ||
1320 | /** @brief Called with current playlist contents | |
1321 | * Checks that the playlist is still current and has not changed. | |
1322 | */ | |
1323 | void playlist_modify_retrieved(void *v, const char *err, | |
1324 | int nvec, | |
1325 | char **vec) { | |
1326 | struct playlist_modify_data *mod = v; | |
1327 | if(err) { | |
2d144dda | 1328 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
7a3eb8eb RK |
1329 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); |
1330 | return; | |
1331 | } | |
1332 | if(nvec < 0 | |
1333 | || !playlist_picker_selected | |
1334 | || strcmp(mod->playlist, playlist_picker_selected)) { | |
1335 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); | |
1336 | return; | |
1337 | } | |
1338 | /* We check that the contents haven't changed. If they have we just abandon | |
1339 | * the operation. The user will have to try again. */ | |
1340 | struct queue_entry *q; | |
1341 | int n; | |
1342 | for(n = 0, q = ql_playlist.q; q && n < nvec; ++n, q = q->next) | |
1343 | if(strcmp(q->track, vec[n])) | |
1344 | break; | |
1345 | if(n != nvec || q != NULL) { | |
1346 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); | |
1347 | return; | |
1348 | } | |
1349 | mod->modify(mod, nvec, vec); | |
1350 | } | |
1351 | ||
1352 | /** @brief Called when the playlist has been updated */ | |
1353 | static void playlist_modify_updated(void attribute((unused)) *v, | |
1354 | const char *err) { | |
1355 | if(err) | |
2d144dda | 1356 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
7a3eb8eb RK |
1357 | disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL); |
1358 | } | |
1359 | ||
1360 | /** @brief Called when the playlist has been unlocked */ | |
1361 | static void playlist_modify_unlocked(void attribute((unused)) *v, | |
1362 | const char *err) { | |
1363 | if(err) | |
2d144dda | 1364 | popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err); |
7a3eb8eb RK |
1365 | } |
1366 | ||
1367 | /* Drop tracks into a playlist ---------------------------------------------- */ | |
1368 | ||
1369 | static void playlist_drop(struct queuelike attribute((unused)) *ql, | |
1370 | int ntracks, | |
1371 | char **tracks, char **ids, | |
1372 | struct queue_entry *after_me) { | |
1373 | struct playlist_modify_data *mod = xmalloc(sizeof *mod); | |
1374 | ||
1375 | mod->playlist = playlist_picker_selected; | |
1376 | mod->modify = playlist_drop_modify; | |
1377 | mod->ntracks = ntracks; | |
1378 | mod->tracks = tracks; | |
1379 | mod->ids = ids; | |
1380 | mod->after_me = after_me; | |
7a3eb8eb RK |
1381 | disorder_eclient_playlist_lock(client, playlist_modify_locked, |
1382 | mod->playlist, mod); | |
1383 | } | |
1384 | ||
a6712ea8 RK |
1385 | /** @brief Return true if track @p i is in the moved set */ |
1386 | static int playlist_drop_is_moved(struct playlist_modify_data *mod, | |
1387 | int i) { | |
1388 | struct queue_entry *q; | |
1389 | ||
a6712ea8 RK |
1390 | /* Find the q corresponding to i, so we can get the ID */ |
1391 | for(q = ql_playlist.q; i; q = q->next, --i) | |
1392 | ; | |
a6712ea8 RK |
1393 | /* See if track i matches any of the moved set by ID */ |
1394 | for(int n = 0; n < mod->ntracks; ++n) | |
8bbeb28e | 1395 | if(!strcmp(q->id, mod->ids[n])) |
a6712ea8 | 1396 | return 1; |
a6712ea8 RK |
1397 | return 0; |
1398 | } | |
1399 | ||
7a3eb8eb RK |
1400 | static void playlist_drop_modify(struct playlist_modify_data *mod, |
1401 | int nvec, char **vec) { | |
1402 | char **newvec; | |
1403 | int nnewvec; | |
f57ff3aa | 1404 | |
53ce677c | 1405 | //fprintf(stderr, "\nplaylist_drop_modify\n"); |
a6712ea8 RK |
1406 | /* after_me is the queue_entry to insert after, or NULL to insert at the |
1407 | * beginning (including the case when the playlist is empty) */ | |
53ce677c RK |
1408 | //fprintf(stderr, "after_me = %s\n", |
1409 | // mod->after_me ? mod->after_me->track : "NULL"); | |
a6712ea8 RK |
1410 | struct queue_entry *q = ql_playlist.q; |
1411 | int ins = 0; | |
1412 | if(mod->after_me) { | |
1413 | ++ins; | |
1414 | while(q && q != mod->after_me) { | |
1415 | q = q->next; | |
1416 | ++ins; | |
1417 | } | |
1418 | } | |
1419 | /* Now ins is the index to insert at; equivalently, the row to insert before, | |
1420 | * and so equal to nvec to append. */ | |
8bbeb28e | 1421 | #if 0 |
a6712ea8 RK |
1422 | fprintf(stderr, "ins = %d = %s\n", |
1423 | ins, ins < nvec ? vec[ins] : "NULL"); | |
d8efe0a3 RK |
1424 | for(int n = 0; n < nvec; ++n) |
1425 | fprintf(stderr, "%d: %s %s\n", n, n == ins ? "->" : " ", vec[n]); | |
a6712ea8 | 1426 | fprintf(stderr, "nvec = %d\n", nvec); |
8bbeb28e | 1427 | #endif |
7a3eb8eb RK |
1428 | if(mod->ids) { |
1429 | /* This is a rearrangement */ | |
d8efe0a3 RK |
1430 | /* We have: |
1431 | * - vec[], the current layout | |
1432 | * - ins, pointing into vec | |
1433 | * - mod->tracks[], a subset of vec[] which is to be moved | |
1434 | * | |
1435 | * ins is the insertion point BUT it is in terms of the whole | |
1436 | * array, i.e. before mod->tracks[] have been removed. The first | |
1437 | * step then is to remove everything in mod->tracks[] and adjust | |
1438 | * ins downwards as necessary. | |
1439 | */ | |
1440 | /* First zero out anything that's moved */ | |
1441 | int before_ins = 0; | |
1442 | for(int n = 0; n < nvec; ++n) { | |
1443 | if(playlist_drop_is_moved(mod, n)) { | |
1444 | vec[n] = NULL; | |
1445 | if(n < ins) | |
1446 | ++before_ins; | |
7a3eb8eb RK |
1447 | } |
1448 | } | |
d8efe0a3 RK |
1449 | /* Now collapse down the array */ |
1450 | int i = 0; | |
1451 | for(int n = 0; n < nvec; ++n) { | |
1452 | if(vec[n]) | |
1453 | vec[i++] = vec[n]; | |
1454 | } | |
1455 | assert(i + mod->ntracks == nvec); | |
1456 | nvec = i; | |
1457 | /* Adjust the insertion point to take account of things moved from before | |
1458 | * it */ | |
1459 | ins -= before_ins; | |
1460 | /* The effect is now the same as an insertion */ | |
7a3eb8eb | 1461 | } |
d8efe0a3 RK |
1462 | /* This is (now) an insertion */ |
1463 | nnewvec = nvec + mod->ntracks; | |
1464 | newvec = xcalloc(nnewvec, sizeof (char *)); | |
1465 | memcpy(newvec, vec, | |
1466 | ins * sizeof (char *)); | |
1467 | memcpy(newvec + ins, mod->tracks, | |
1468 | mod->ntracks * sizeof (char *)); | |
1469 | memcpy(newvec + ins + mod->ntracks, vec + ins, | |
1470 | (nvec - ins) * sizeof (char *)); | |
7a3eb8eb RK |
1471 | disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist, |
1472 | newvec, nnewvec, mod); | |
1473 | } | |
1474 | ||
b35f7c0f RK |
1475 | /* Playlist editor right-click menu ---------------------------------------- */ |
1476 | ||
1477 | /** @brief Called to determine whether the playlist is playable */ | |
1478 | static int playlist_playall_sensitive(void attribute((unused)) *extra) { | |
1479 | /* If there's no playlist obviously we can't play it */ | |
1480 | if(!playlist_picker_selected) | |
1481 | return FALSE; | |
1482 | /* If it's empty we can't play it */ | |
1483 | if(!ql_playlist.q) | |
1484 | return FALSE; | |
1485 | /* Otherwise we can */ | |
1486 | return TRUE; | |
1487 | } | |
1488 | ||
1489 | /** @brief Called to play the selected playlist */ | |
1490 | static void playlist_playall_activate(GtkMenuItem attribute((unused)) *menuitem, | |
1491 | gpointer attribute((unused)) user_data) { | |
1492 | if(!playlist_picker_selected) | |
1493 | return; | |
1494 | /* Re-use the menu-based activation callback */ | |
1495 | disorder_eclient_playlist_get(client, playlist_menu_received_content, | |
1496 | playlist_picker_selected, NULL); | |
1497 | } | |
1498 | ||
dba3eb8e RK |
1499 | /** @brief Called to determine whether the playlist is playable */ |
1500 | static int playlist_remove_sensitive(void attribute((unused)) *extra) { | |
1501 | /* If there's no playlist obviously we can't remove from it */ | |
1502 | if(!playlist_picker_selected) | |
1503 | return FALSE; | |
1504 | /* If no tracks are selected we cannot remove them */ | |
1505 | if(!gtk_tree_selection_count_selected_rows(ql_playlist.selection)) | |
1506 | return FALSE; | |
1507 | /* We're good to go */ | |
1508 | return TRUE; | |
1509 | } | |
1510 | ||
9ad9f8f7 | 1511 | /** @brief Called to remove the selected playlist */ |
dba3eb8e RK |
1512 | static void playlist_remove_activate(GtkMenuItem attribute((unused)) *menuitem, |
1513 | gpointer attribute((unused)) user_data) { | |
1514 | if(!playlist_picker_selected) | |
1515 | return; | |
7a3eb8eb | 1516 | struct playlist_modify_data *mod = xmalloc(sizeof *mod); |
487eb32a | 1517 | |
7a3eb8eb RK |
1518 | mod->playlist = playlist_picker_selected; |
1519 | mod->modify = playlist_remove_modify; | |
1520 | disorder_eclient_playlist_lock(client, playlist_modify_locked, | |
1521 | mod->playlist, mod); | |
487eb32a RK |
1522 | } |
1523 | ||
7a3eb8eb RK |
1524 | static void playlist_remove_modify(struct playlist_modify_data *mod, |
1525 | int attribute((unused)) nvec, char **vec) { | |
487eb32a RK |
1526 | GtkTreeIter iter[1]; |
1527 | gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql_playlist.store), | |
1528 | iter); | |
7a3eb8eb | 1529 | int n = 0, m = 0; |
487eb32a RK |
1530 | while(it) { |
1531 | if(!gtk_tree_selection_iter_is_selected(ql_playlist.selection, iter)) | |
1532 | vec[m++] = vec[n++]; | |
1533 | else | |
1534 | n++; | |
1535 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_playlist.store), iter); | |
1536 | } | |
7a3eb8eb RK |
1537 | disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist, |
1538 | vec, m, mod); | |
dba3eb8e RK |
1539 | } |
1540 | ||
506e02d8 RK |
1541 | /* Playlists window --------------------------------------------------------- */ |
1542 | ||
7de5fbb9 | 1543 | /** @brief Pop up the playlists window |
121944d1 RK |
1544 | * |
1545 | * Called when the playlists menu item is selected | |
1546 | */ | |
f06a754c RK |
1547 | void playlist_window_create(gpointer attribute((unused)) callback_data, |
1548 | guint attribute((unused)) callback_action, | |
1549 | GtkWidget attribute((unused)) *menu_item) { | |
f0bd437a | 1550 | /* If the window already exists, raise it */ |
7fe2807d RK |
1551 | if(playlist_window) { |
1552 | gtk_window_present(GTK_WINDOW(playlist_window)); | |
f0bd437a RK |
1553 | return; |
1554 | } | |
1555 | /* Create the window */ | |
7fe2807d RK |
1556 | playlist_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
1557 | gtk_widget_set_style(playlist_window, tool_style); | |
1558 | g_signal_connect(playlist_window, "destroy", | |
1559 | G_CALLBACK(playlist_window_destroyed), &playlist_window); | |
1560 | gtk_window_set_title(GTK_WINDOW(playlist_window), "Playlists Management"); | |
f0bd437a RK |
1561 | /* TODO loads of this is very similar to (copied from!) users.c - can we |
1562 | * de-dupe? */ | |
1563 | /* Keyboard shortcuts */ | |
7fe2807d RK |
1564 | g_signal_connect(playlist_window, "key-press-event", |
1565 | G_CALLBACK(playlist_window_keypress), 0); | |
f0bd437a | 1566 | /* default size is too small */ |
9e239dff | 1567 | gtk_window_set_default_size(GTK_WINDOW(playlist_window), 640, 320); |
f0bd437a | 1568 | |
506e02d8 | 1569 | GtkWidget *hbox = gtk_hbox_new(FALSE, 0); |
b5e60f0d | 1570 | gtk_box_pack_start(GTK_BOX(hbox), playlist_picker_create(), |
506e02d8 RK |
1571 | FALSE/*expand*/, FALSE, 0); |
1572 | gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), | |
1573 | FALSE/*expand*/, FALSE, 2); | |
7fe2807d | 1574 | gtk_box_pack_start(GTK_BOX(hbox), playlists_editor_create(), |
506e02d8 | 1575 | TRUE/*expand*/, TRUE/*fill*/, 0); |
f0bd437a | 1576 | |
7fe2807d RK |
1577 | gtk_container_add(GTK_CONTAINER(playlist_window), frame_widget(hbox, NULL)); |
1578 | gtk_widget_show_all(playlist_window); | |
f9b20469 RK |
1579 | } |
1580 | ||
c41b2cac RK |
1581 | /** @brief Keypress handler */ |
1582 | static gboolean playlist_window_keypress(GtkWidget attribute((unused)) *widget, | |
1583 | GdkEventKey *event, | |
1584 | gpointer attribute((unused)) user_data) { | |
1585 | if(event->state) | |
1586 | return FALSE; | |
1587 | switch(event->keyval) { | |
1588 | case GDK_Escape: | |
1589 | gtk_widget_destroy(playlist_window); | |
1590 | return TRUE; | |
1591 | default: | |
1592 | return FALSE; | |
1593 | } | |
1594 | } | |
1595 | ||
1596 | /** @brief Called when the playlist window is destroyed */ | |
1597 | static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget, | |
1598 | GtkWidget **widget_pointer) { | |
1599 | destroy_queuelike(&ql_playlist); | |
a544b1cc | 1600 | playlist_picker_destroy(); |
c41b2cac RK |
1601 | *widget_pointer = NULL; |
1602 | } | |
1603 | ||
fc36ecb7 RK |
1604 | /** @brief Initialize playlist support */ |
1605 | void playlists_init(void) { | |
1606 | /* We re-get all playlists upon any change... */ | |
b5e60f0d | 1607 | event_register("playlist-created", playlist_list_update, 0); |
b5e60f0d | 1608 | event_register("playlist-deleted", playlist_list_update, 0); |
fc36ecb7 | 1609 | /* ...and on reconnection */ |
b5e60f0d | 1610 | event_register("log-connected", playlist_list_update, 0); |
fc36ecb7 | 1611 | /* ...and from time to time */ |
b5e60f0d | 1612 | event_register("periodic-slow", playlist_list_update, 0); |
fc36ecb7 | 1613 | /* ...and at startup */ |
b5e60f0d | 1614 | playlist_list_update(0, 0, 0); |
7c12e4bd RK |
1615 | |
1616 | /* Update the playlists menu when the set of playlists changes */ | |
b5e60f0d | 1617 | event_register("playlists-updated", playlist_menu_changed, 0); |
7c12e4bd RK |
1618 | /* Update the new-playlist OK button when the set of playlists changes */ |
1619 | event_register("playlists-updated", playlist_new_changed, 0); | |
d9b141cc | 1620 | /* Update the list of playlists in the edit window when the set changes */ |
b5e60f0d | 1621 | event_register("playlists-updated", playlist_picker_fill, 0); |
7e702780 | 1622 | /* Update the displayed playlist when it is modified */ |
7fe2807d | 1623 | event_register("playlist-modified", playlist_editor_fill, 0); |
ada57e73 RK |
1624 | /* Update the shared/public/etc buttons when a playlist is modified */ |
1625 | event_register("playlist-modified", playlist_editor_set_buttons, 0); | |
fc36ecb7 RK |
1626 | } |
1627 | ||
1628 | /* | |
1629 | Local Variables: | |
1630 | c-basic-offset:2 | |
1631 | comment-column:40 | |
1632 | fill-column:79 | |
1633 | indent-tabs-mode:nil | |
1634 | End: | |
1635 | */ |