2 * This file is part of DisOrder
3 * Copyright (C) 2008, 2009 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/playlists.c
21 * @brief Playlist support for Disobedience
23 * The playlists management window contains:
24 * - the playlist picker (a list of all playlists) TODO should be a tree!
27 * - the playlist editor (a d+d-capable view of the currently picked playlist)
28 * - a close button TODO
30 * This file also maintains the playlist menu, allowing playlists to be
31 * activated from the main window's menu.
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
38 #include "disobedience.h"
39 #include "queue-generic.h"
43 static void playlist_list_received_playlists(void *v
,
45 int nvec
, char **vec
);
46 static void playlist_editor_fill(const char *event
,
49 static int playlist_playall_sensitive(void *extra
);
50 static void playlist_playall_activate(GtkMenuItem
*menuitem
,
52 static int playlist_remove_sensitive(void *extra
) ;
53 static void playlist_remove_activate(GtkMenuItem
*menuitem
,
55 static void playlist_new_locked(void *v
, const char *err
);
56 static void playlist_new_retrieved(void *v
, const char *err
,
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
,
63 static void playlist_new_button_toggled(GtkToggleButton
*tb
,
65 static void playlist_new_changed(const char *event
,
68 static const char *playlist_new_valid(void);
69 static void playlist_new_details(char **namep
,
74 static void playlist_new_ok(GtkButton
*button
,
76 static void playlist_new_cancel(GtkButton
*button
,
78 static void playlists_editor_received_tracks(void *v
,
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
,
86 static int playlistcmp(const void *ap
, const void *bp
);
87 static void playlist_modify_locked(void *v
, const char *err
);
88 void playlist_modify_retrieved(void *v
, const char *err
,
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
,
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
);
102 static gboolean
playlist_new_keypress(GtkWidget
*widget
,
105 static gboolean
playlist_picker_keypress(GtkWidget
*widget
,
108 static void playlist_editor_button_toggled(GtkToggleButton
*tb
,
110 static void playlist_editor_set_buttons(const char *event
,
113 static void playlist_editor_got_share(void *v
,
116 static void playlist_editor_share_set(void *v
, const char *err
);
117 static void playlist_picker_update_section(const char *title
, const char *key
,
119 static gboolean
playlist_picker_find(GtkTreeIter
*parent
,
120 const char *title
, const char *key
,
123 static void playlist_picker_delete_obsolete(GtkTreeIter parent
[1],
126 static gboolean
playlist_picker_button(GtkWidget
*widget
,
127 GdkEventButton
*event
,
130 /** @brief Playlist editing window */
131 static GtkWidget
*playlist_window
;
133 /** @brief Columns for the playlist editor */
134 static const struct queue_column playlist_columns
[] = {
135 { "Artist", column_namepart
, "artist", COL_EXPAND
|COL_ELLIPSIZE
},
136 { "Album", column_namepart
, "album", COL_EXPAND
|COL_ELLIPSIZE
},
137 { "Title", column_namepart
, "title", COL_EXPAND
|COL_ELLIPSIZE
},
140 /** @brief Pop-up menu for playlist editor
143 * - track properties works but, bizarrely, raises the main window
145 * - play playlist works
146 * - select/deselect all work
148 static struct menuitem playlist_menuitems
[] = {
149 { "Track properties", ql_properties_activate
, ql_properties_sensitive
, 0, 0 },
150 { "Play track", ql_play_activate
, ql_play_sensitive
, 0, 0 },
151 { "Play playlist", playlist_playall_activate
, playlist_playall_sensitive
, 0, 0 },
152 { "Remove track from playlist", playlist_remove_activate
, playlist_remove_sensitive
, 0, 0 },
153 { "Select all tracks", ql_selectall_activate
, ql_selectall_sensitive
, 0, 0 },
154 { "Deselect all tracks", ql_selectnone_activate
, ql_selectnone_sensitive
, 0, 0 },
157 static const GtkTargetEntry playlist_targets
[] = {
159 PLAYLIST_TRACKS
, /* drag type */
160 GTK_TARGET_SAME_WIDGET
, /* rearrangement within a widget */
161 PLAYLIST_TRACKS_ID
/* ID value */
164 PLAYABLE_TRACKS
, /* drag type */
165 GTK_TARGET_SAME_APP
|GTK_TARGET_OTHER_WIDGET
, /* copying between widgets */
166 PLAYABLE_TRACKS_ID
, /* ID value */
173 /** @brief Queuelike for editing a playlist */
174 static struct queuelike ql_playlist
= {
176 .columns
= playlist_columns
,
177 .ncolumns
= sizeof playlist_columns
/ sizeof *playlist_columns
,
178 .menuitems
= playlist_menuitems
,
179 .nmenuitems
= sizeof playlist_menuitems
/ sizeof *playlist_menuitems
,
180 .drop
= playlist_drop
,
181 .drag_source_targets
= playlist_targets
,
182 .drag_source_actions
= GDK_ACTION_MOVE
|GDK_ACTION_COPY
,
183 .drag_dest_targets
= playlist_targets
,
184 .drag_dest_actions
= GDK_ACTION_MOVE
|GDK_ACTION_COPY
,
187 /* Maintaining the list of playlists ---------------------------------------- */
189 /** @brief Current list of playlists or NULL */
192 /** @brief Count of playlists */
195 /** @brief Schedule an update to the list of playlists
197 * Called periodically and when a playlist is created or deleted.
199 static void playlist_list_update(const char attribute((unused
)) *event
,
200 void attribute((unused
)) *eventdata
,
201 void attribute((unused
)) *callbackdata
) {
202 disorder_eclient_playlists(client
, playlist_list_received_playlists
, 0);
205 /** @brief Called with a new list of playlists */
206 static void playlist_list_received_playlists(void attribute((unused
)) *v
,
208 int nvec
, char **vec
) {
212 /* Probably means server does not support playlists */
216 qsort(playlists
, nplaylists
, sizeof (char *), playlistcmp
);
218 /* Tell our consumers */
219 event_raise("playlists-updated", 0);
222 /** @brief qsort() callback for playlist name comparison */
223 static int playlistcmp(const void *ap
, const void *bp
) {
224 const char *a
= *(char **)ap
, *b
= *(char **)bp
;
225 const char *ad
= strchr(a
, '.'), *bd
= strchr(b
, '.');
228 /* Group owned playlists by owner */
230 const int adn
= ad
- a
, bdn
= bd
- b
;
231 if((c
= strncmp(a
, b
, adn
< bdn ? adn
: bdn
)))
233 /* Lexical order within playlists of a single owner */
234 return strcmp(ad
+ 1, bd
+ 1);
237 /* Owned playlists after shared ones */
244 /* Lexical order of shared playlists */
248 /* Playlists menu ----------------------------------------------------------- */
250 static void playlist_menu_playing(void attribute((unused
)) *v
,
253 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
256 /** @brief Play received playlist contents
258 * Passed as a completion callback by menu_activate_playlist().
260 static void playlist_menu_received_content(void attribute((unused
)) *v
,
262 int nvec
, char **vec
) {
264 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
267 for(int n
= 0; n
< nvec
; ++n
)
268 disorder_eclient_play(client
, vec
[n
], playlist_menu_playing
, NULL
);
271 /** @brief Called to activate a playlist
273 * Called when the menu item for a playlist is clicked.
275 static void playlist_menu_activate(GtkMenuItem
*menuitem
,
276 gpointer
attribute((unused
)) user_data
) {
277 GtkLabel
*label
= GTK_LABEL(GTK_BIN(menuitem
)->child
);
278 const char *playlist
= gtk_label_get_text(label
);
280 disorder_eclient_playlist_get(client
, playlist_menu_received_content
,
284 /** @brief Called when the playlists change
286 * Naively refills the menu. The results might be unsettling if the menu is
287 * currently open, but this is hopefuly fairly rare.
289 static void playlist_menu_changed(const char attribute((unused
)) *event
,
290 void attribute((unused
)) *eventdata
,
291 void attribute((unused
)) *callbackdata
) {
293 return; /* OMG too soon */
294 GtkMenuShell
*menu
= GTK_MENU_SHELL(playlists_menu
);
295 while(menu
->children
)
296 gtk_container_remove(GTK_CONTAINER(menu
), GTK_WIDGET(menu
->children
->data
));
297 /* NB nplaylists can be -1 as well as 0 */
298 for(int n
= 0; n
< nplaylists
; ++n
) {
299 GtkWidget
*w
= gtk_menu_item_new_with_label(playlists
[n
]);
300 g_signal_connect(w
, "activate", G_CALLBACK(playlist_menu_activate
), 0);
302 gtk_menu_shell_append(menu
, w
);
304 gtk_widget_set_sensitive(menu_playlists_widget
,
306 gtk_widget_set_sensitive(menu_editplaylists_widget
,
310 /* Popup to create a new playlist ------------------------------------------- */
312 /** @brief New-playlist popup */
313 static GtkWidget
*playlist_new_window
;
315 /** @brief Text entry in new-playlist popup */
316 static GtkWidget
*playlist_new_entry
;
318 /** @brief Label for displaying feedback on what's wrong */
319 static GtkWidget
*playlist_new_info
;
321 /** @brief "Shared" radio button */
322 static GtkWidget
*playlist_new_shared
;
324 /** @brief "Public" radio button */
325 static GtkWidget
*playlist_new_public
;
327 /** @brief "Private" radio button */
328 static GtkWidget
*playlist_new_private
;
330 /** @brief Buttons for new-playlist popup */
331 static struct button playlist_new_buttons
[] = {
333 .stock
= GTK_STOCK_OK
,
334 .clicked
= playlist_new_ok
,
335 .tip
= "Create new playlist"
338 .stock
= GTK_STOCK_CANCEL
,
339 .clicked
= playlist_new_cancel
,
340 .tip
= "Do not create new playlist"
343 #define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons)
345 /** @brief Pop up a new window to enter the playlist name and details */
346 static void playlist_new_playlist(void) {
347 assert(playlist_new_window
== NULL
);
348 playlist_new_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
349 g_signal_connect(playlist_new_window
, "destroy",
350 G_CALLBACK(gtk_widget_destroyed
), &playlist_new_window
);
351 gtk_window_set_title(GTK_WINDOW(playlist_new_window
), "Create new playlist");
352 /* Window will be modal, suppressing access to other windows */
353 gtk_window_set_modal(GTK_WINDOW(playlist_new_window
), TRUE
);
354 gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window
),
355 GTK_WINDOW(playlist_window
));
357 /* Window contents will use a table (grid) layout */
358 GtkWidget
*table
= gtk_table_new(3, 3, FALSE
/*!homogeneous*/);
360 /* First row: playlist name */
361 gtk_table_attach_defaults(GTK_TABLE(table
),
362 gtk_label_new("Playlist name"),
364 playlist_new_entry
= gtk_entry_new();
365 g_signal_connect(playlist_new_entry
, "changed",
366 G_CALLBACK(playlist_new_entry_edited
), NULL
);
367 gtk_table_attach_defaults(GTK_TABLE(table
),
371 /* Second row: radio buttons to choose type */
372 playlist_new_shared
= gtk_radio_button_new_with_label(NULL
, "shared");
374 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared
),
377 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared
),
379 g_signal_connect(playlist_new_shared
, "toggled",
380 G_CALLBACK(playlist_new_button_toggled
), NULL
);
381 g_signal_connect(playlist_new_public
, "toggled",
382 G_CALLBACK(playlist_new_button_toggled
), NULL
);
383 g_signal_connect(playlist_new_private
, "toggled",
384 G_CALLBACK(playlist_new_button_toggled
), NULL
);
385 gtk_table_attach_defaults(GTK_TABLE(table
), playlist_new_shared
, 0, 1, 1, 2);
386 gtk_table_attach_defaults(GTK_TABLE(table
), playlist_new_public
, 1, 2, 1, 2);
387 gtk_table_attach_defaults(GTK_TABLE(table
), playlist_new_private
, 2, 3, 1, 2);
389 /* Third row: info bar saying why not */
390 playlist_new_info
= gtk_label_new("");
391 gtk_table_attach_defaults(GTK_TABLE(table
), playlist_new_info
,
394 /* Fourth row: ok/cancel buttons */
395 GtkWidget
*hbox
= create_buttons_box(playlist_new_buttons
,
396 NPLAYLIST_NEW_BUTTONS
,
397 gtk_hbox_new(FALSE
, 0));
398 gtk_table_attach_defaults(GTK_TABLE(table
), hbox
, 0, 3, 3, 4);
400 gtk_container_add(GTK_CONTAINER(playlist_new_window
),
401 frame_widget(table
, NULL
));
403 /* Set initial state of OK button */
404 playlist_new_changed(0,0,0);
406 g_signal_connect(playlist_new_window
, "key-press-event",
407 G_CALLBACK(playlist_new_keypress
), 0);
409 /* Display the window */
410 gtk_widget_show_all(playlist_new_window
);
413 /** @brief Keypress handler */
414 static gboolean
playlist_new_keypress(GtkWidget
attribute((unused
)) *widget
,
416 gpointer
attribute((unused
)) user_data
) {
419 switch(event
->keyval
) {
421 playlist_new_ok(NULL
, NULL
);
424 gtk_widget_destroy(playlist_new_window
);
431 /** @brief Called when 'ok' is clicked in new-playlist popup */
432 static void playlist_new_ok(GtkButton
attribute((unused
)) *button
,
433 gpointer
attribute((unused
)) userdata
) {
434 if(playlist_new_valid())
436 gboolean shared
, public, private;
437 char *name
, *fullname
;
438 playlist_new_details(&name
, &fullname
, &shared
, &public, &private);
441 * - lock the playlist
442 * - check it doesn't exist
443 * - set sharing (which will create it empty
446 * TODO we should freeze the window while this is going on to stop a second
449 disorder_eclient_playlist_lock(client
, playlist_new_locked
, fullname
,
453 /** @brief Called when the proposed new playlist has been locked */
454 static void playlist_new_locked(void *v
, const char *err
) {
457 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
460 disorder_eclient_playlist_get(client
, playlist_new_retrieved
,
464 /** @brief Called when the proposed new playlist's contents have been retrieved
466 * ...or rather, normally, when it's been reported that it does not exist.
468 static void playlist_new_retrieved(void *v
, const char *err
,
470 char attribute((unused
)) **vec
) {
472 if(!err
&& nvec
!= -1)
473 /* A rare case but not in principle impossible */
474 err
= "A playlist with that name already exists.";
476 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
477 disorder_eclient_playlist_unlock(client
, playlist_new_unlocked
, fullname
);
480 gboolean shared
, public, private;
481 playlist_new_details(0, 0, &shared
, &public, &private);
482 disorder_eclient_playlist_set_share(client
, playlist_new_created
, fullname
,
484 : private ?
"private"
489 /** @brief Called when the new playlist has been created */
490 static void playlist_new_created(void attribute((unused
)) *v
, const char *err
) {
492 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
495 disorder_eclient_playlist_unlock(client
, playlist_new_unlocked
, NULL
);
496 // TODO arrange for the new playlist to be selected
499 /** @brief Called when the newly created playlist has unlocked */
500 static void playlist_new_unlocked(void attribute((unused
)) *v
, const char *err
) {
502 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
503 /* Pop down the creation window */
504 gtk_widget_destroy(playlist_new_window
);
507 /** @brief Called when 'cancel' is clicked in new-playlist popup */
508 static void playlist_new_cancel(GtkButton
attribute((unused
)) *button
,
509 gpointer
attribute((unused
)) userdata
) {
510 gtk_widget_destroy(playlist_new_window
);
513 /** @brief Called when some radio button in the new-playlist popup changes */
514 static void playlist_new_button_toggled(GtkToggleButton
attribute((unused
)) *tb
,
515 gpointer
attribute((unused
)) userdata
) {
516 playlist_new_changed(0,0,0);
519 /** @brief Called when the text entry field in the new-playlist popup changes */
520 static void playlist_new_entry_edited(GtkEditable
attribute((unused
)) *editable
,
521 gpointer
attribute((unused
)) user_data
) {
522 playlist_new_changed(0,0,0);
525 /** @brief Called to update new playlist window state
527 * This is called whenever one the text entry or radio buttons changed, and
528 * also when the set of known playlists changes. It determines whether the new
529 * playlist would be creatable and sets the sensitivity of the OK button
530 * and info display accordingly.
532 static void playlist_new_changed(const char attribute((unused
)) *event
,
533 void attribute((unused
)) *eventdata
,
534 void attribute((unused
)) *callbackdata
) {
535 if(!playlist_new_window
)
537 const char *reason
= playlist_new_valid();
538 gtk_widget_set_sensitive(playlist_new_buttons
[0].widget
,
540 gtk_label_set_text(GTK_LABEL(playlist_new_info
), reason
);
543 /** @brief Test whether the new-playlist window settings are valid
544 * @return NULL on success or an error string if not
546 static const char *playlist_new_valid(void) {
547 gboolean shared
, public, private;
548 char *name
, *fullname
;
549 playlist_new_details(&name
, &fullname
, &shared
, &public, &private);
550 if(!(shared
|| public || private))
551 return "No type set.";
554 /* See if the result is valid */
555 if(!valid_username(name
)
556 || playlist_parse_name(fullname
, NULL
, NULL
))
557 return "Not a valid playlist name.";
558 /* See if the result clashes with an existing name. This is not a perfect
559 * check, the playlist might be created after this point but before we get a
560 * chance to disable the "OK" button. However when we try to create the
561 * playlist we will first try to retrieve it, with a lock held, so we
562 * shouldn't end up overwriting anything. */
563 for(int n
= 0; n
< nplaylists
; ++n
)
564 if(!strcmp(playlists
[n
], fullname
)) {
566 return "A shared playlist with that name already exists.";
568 return "You already have a playlist with that name.";
570 /* As far as we can tell creation would work */
574 /** @brief Get entered new-playlist details
575 * @param namep Where to store entered name (or NULL)
576 * @param fullnamep Where to store computed full name (or NULL)
577 * @param sharep Where to store 'shared' flag (or NULL)
578 * @param publicp Where to store 'public' flag (or NULL)
579 * @param privatep Where to store 'private' flag (or NULL)
581 static void playlist_new_details(char **namep
,
585 gboolean
*privatep
) {
586 gboolean shared
, public, private;
587 g_object_get(playlist_new_shared
, "active", &shared
, (char *)NULL
);
588 g_object_get(playlist_new_public
, "active", &public, (char *)NULL
);
589 g_object_get(playlist_new_private
, "active", &private, (char *)NULL
);
590 char *gname
= gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry
),
591 0, -1); /* name owned by calle */
592 char *name
= xstrdup(gname
);
594 if(sharedp
) *sharedp
= shared
;
595 if(publicp
) *publicp
= public;
596 if(privatep
) *privatep
= private;
597 if(namep
) *namep
= name
;
599 if(*sharedp
) *fullnamep
= *namep
;
600 else byte_xasprintf(fullnamep
, "%s.%s", config
->username
, name
);
604 /* Playlist picker ---------------------------------------------------------- */
606 /** @brief Delete button */
607 static GtkWidget
*playlist_picker_delete_button
;
609 /** @brief Tree model for list of playlists
611 * This has two columns:
612 * - column 0 will be the display name
613 * - column 1 will be the sort key/playlist name (and will not be displayed)
615 static GtkTreeStore
*playlist_picker_list
;
617 /** @brief Selection for list of playlists */
618 static GtkTreeSelection
*playlist_picker_selection
;
620 /** @brief Currently selected playlist */
621 static const char *playlist_picker_selected
;
623 /** @brief (Re-)populate the playlist picker tree model */
624 static void playlist_picker_fill(const char attribute((unused
)) *event
,
625 void attribute((unused
)) *eventdata
,
626 void attribute((unused
)) *callbackdata
) {
629 if(!playlist_picker_list
)
630 playlist_picker_list
= gtk_tree_store_new(2, G_TYPE_STRING
, G_TYPE_STRING
);
631 /* We will accumulate a list of all the sections that exist */
632 char **sections
= xcalloc(nplaylists
, sizeof (char *));
634 /* Make sure shared playlists are there */
636 for(end
= start
; end
< nplaylists
&& !strchr(playlists
[end
], '.'); ++end
)
639 playlist_picker_update_section("Shared playlists", "",
641 sections
[nsections
++] = (char *)"";
643 /* Make sure owned playlists are there */
644 while((start
= end
) < nplaylists
) {
645 const int nl
= strchr(playlists
[start
], '.') - playlists
[start
];
646 char *name
= xstrndup(playlists
[start
], nl
);
649 && playlists
[end
][nl
] == '.'
650 && !strncmp(playlists
[start
], playlists
[end
], nl
);
653 playlist_picker_update_section(name
, name
, start
, end
);
654 sections
[nsections
++] = name
;
656 /* Delete obsolete sections */
657 playlist_picker_delete_obsolete(NULL
, sections
, nsections
);
660 /** @brief Update a section in the picker tree model
661 * @param section Section name
662 * @param start First entry in @ref playlists
663 * @param end Past last entry in @ref playlists
665 static void playlist_picker_update_section(const char *title
, const char *key
,
666 int start
, int end
) {
667 /* Find the section, creating it if necessary */
668 GtkTreeIter section_iter
[1];
669 playlist_picker_find(NULL
, title
, key
, section_iter
, TRUE
);
670 /* Add missing rows */
671 for(int n
= start
; n
< end
; ++n
) {
672 GtkTreeIter child
[1];
674 if((name
= strchr(playlists
[n
], '.')))
678 playlist_picker_find(section_iter
,
683 /* Delete anything that shouldn't exist. */
684 playlist_picker_delete_obsolete(section_iter
, playlists
+ start
, end
- start
);
687 /** @brief Find and maybe create a row in the picker tree model
688 * @param parent Parent iterator (or NULL for top level)
689 * @param title Display name of section
690 * @param key Key to search for
691 * @param iter Iterator to point at key
692 * @param create If TRUE, key will be created if it doesn't exist
693 * @param compare Row comparison function
694 * @return TRUE if key exists else FALSE
696 * If the @p key exists then @p iter will point to it and TRUE will be
699 * If the @p key does not exist and @p create is TRUE then it will be created.
700 * @p iter wil point to it and TRUE will be returned.
702 * If the @p key does not exist and @p create is FALSE then FALSE will be
705 static gboolean
playlist_picker_find(GtkTreeIter
*parent
,
715 it
= gtk_tree_model_iter_children(GTK_TREE_MODEL(playlist_picker_list
),
719 /* Find the value at row 'next' */
720 gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list
),
724 /* See how it compares with @p key */
725 int c
= strcmp(key
, candidate
);
729 return TRUE
; /* we found our key */
732 /* @p key belongs before row 'next' */
734 gtk_tree_store_insert_with_values(playlist_picker_list
,
737 row
, /* insert here */
738 0, title
, 1, key
, -1);
744 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(playlist_picker_list
), next
);
746 /* We have reached the end and not found a row that should be later than @p
749 gtk_tree_store_insert_with_values(playlist_picker_list
,
752 INT_MAX
, /* insert at end */
753 0, title
, 1, key
, -1);
759 /** @brief Delete obsolete rows
760 * @param parent Parent or NULL
761 * @param exists List of rows that should exist (by key)
762 * @param nexists Length of @p exists
764 static void playlist_picker_delete_obsolete(GtkTreeIter parent
[1],
767 /* Delete anything that shouldn't exist. */
769 gboolean it
= gtk_tree_model_iter_children(GTK_TREE_MODEL(playlist_picker_list
),
773 /* Find the value at row 'next' */
775 gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list
),
779 gboolean found
= FALSE
;
780 for(int n
= 0; n
< nexists
; ++n
)
781 if((found
= !strcmp(candidate
, exists
[n
])))
784 it
= gtk_tree_store_remove(playlist_picker_list
, iter
);
786 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(playlist_picker_list
),
792 /** @brief Called when the selection might have changed */
793 static void playlist_picker_selection_changed(GtkTreeSelection
attribute((unused
)) *treeselection
,
794 gpointer
attribute((unused
)) user_data
) {
796 char *gselected
, *selected
;
798 /* Identify the current selection */
799 if(gtk_tree_selection_get_selected(playlist_picker_selection
, 0, &iter
)
800 && gtk_tree_store_iter_depth(playlist_picker_list
, &iter
) > 0) {
801 gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list
), &iter
,
803 selected
= xstrdup(gselected
);
807 /* Set button sensitivity according to the new state */
808 int deletable
= FALSE
;
810 if(strchr(selected
, '.')) {
811 if(!strncmp(selected
, config
->username
, strlen(config
->username
)))
816 gtk_widget_set_sensitive(playlist_picker_delete_button
, deletable
);
817 /* Eliminate no-change cases */
818 if(!selected
&& !playlist_picker_selected
)
821 && playlist_picker_selected
822 && !strcmp(selected
, playlist_picker_selected
))
824 /* Record the new state */
825 playlist_picker_selected
= selected
;
826 /* Re-initalize the queue */
827 ql_new_queue(&ql_playlist
, NULL
);
828 /* Synthesize a playlist-modified to re-initialize the editor etc */
829 event_raise("playlist-modified", (void *)playlist_picker_selected
);
832 /** @brief Called when the 'add' button is pressed */
833 static void playlist_picker_add(GtkButton
attribute((unused
)) *button
,
834 gpointer
attribute((unused
)) userdata
) {
835 /* Unselect whatever is selected TODO why?? */
836 gtk_tree_selection_unselect_all(playlist_picker_selection
);
837 playlist_new_playlist();
840 /** @brief Called when playlist deletion completes */
841 static void playlists_picker_delete_completed(void attribute((unused
)) *v
,
844 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
847 /** @brief Called when the 'Delete' button is pressed */
848 static void playlist_picker_delete(GtkButton
attribute((unused
)) *button
,
849 gpointer
attribute((unused
)) userdata
) {
853 if(!playlist_picker_selected
)
855 yesno
= gtk_message_dialog_new(GTK_WINDOW(playlist_window
),
857 GTK_MESSAGE_QUESTION
,
859 "Do you really want to delete playlist %s?"
860 " This action cannot be undone.",
861 playlist_picker_selected
);
862 res
= gtk_dialog_run(GTK_DIALOG(yesno
));
863 gtk_widget_destroy(yesno
);
864 if(res
== GTK_RESPONSE_YES
) {
865 disorder_eclient_playlist_delete(client
,
866 playlists_picker_delete_completed
,
867 playlist_picker_selected
,
872 /** @brief Table of buttons below the playlist list */
873 static struct button playlist_picker_buttons
[] = {
877 "Create a new playlist",
882 playlist_picker_delete
,
887 #define NPLAYLIST_PICKER_BUTTONS (sizeof playlist_picker_buttons / sizeof *playlist_picker_buttons)
889 /** @brief Create the list of playlists for the edit playlists window */
890 static GtkWidget
*playlist_picker_create(void) {
891 /* Create the list of playlist and populate it */
892 playlist_picker_fill(NULL
, NULL
, NULL
);
893 /* Create the tree view */
894 GtkWidget
*tree
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlist_picker_list
));
895 /* ...and the renderers for it */
896 GtkCellRenderer
*cr
= gtk_cell_renderer_text_new();
897 GtkTreeViewColumn
*col
= gtk_tree_view_column_new_with_attributes("Playlist",
901 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), col
);
902 /* Get the selection for the view; set its mode; arrange for a callback when
904 playlist_picker_selected
= NULL
;
905 playlist_picker_selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
906 gtk_tree_selection_set_mode(playlist_picker_selection
, GTK_SELECTION_BROWSE
);
907 g_signal_connect(playlist_picker_selection
, "changed",
908 G_CALLBACK(playlist_picker_selection_changed
), NULL
);
910 /* Create the control buttons */
911 GtkWidget
*buttons
= create_buttons_box(playlist_picker_buttons
,
912 NPLAYLIST_PICKER_BUTTONS
,
913 gtk_hbox_new(FALSE
, 1));
914 playlist_picker_delete_button
= playlist_picker_buttons
[1].widget
;
916 playlist_picker_selection_changed(NULL
, NULL
);
918 /* Buttons live below the list */
919 GtkWidget
*vbox
= gtk_vbox_new(FALSE
, 0);
920 gtk_box_pack_start(GTK_BOX(vbox
), scroll_widget(tree
), TRUE
/*expand*/, TRUE
/*fill*/, 0);
921 gtk_box_pack_start(GTK_BOX(vbox
), buttons
, FALSE
/*expand*/, FALSE
, 0);
923 g_signal_connect(tree
, "key-press-event",
924 G_CALLBACK(playlist_picker_keypress
), 0);
925 g_signal_connect(tree
, "button-press-event",
926 G_CALLBACK(playlist_picker_button
), 0);
931 static gboolean
playlist_picker_keypress(GtkWidget
attribute((unused
)) *widget
,
933 gpointer
attribute((unused
)) user_data
) {
936 switch(event
->keyval
) {
939 playlist_picker_delete(NULL
, NULL
);
946 static void playlist_picker_select_activate(GtkMenuItem
attribute((unused
)) *item
,
947 gpointer
attribute((unused
)) userdata
) {
951 static int playlist_picker_select_sensitive(void *extra
) {
952 GtkTreeIter
*iter
= extra
;
953 return gtk_tree_store_iter_depth(playlist_picker_list
, iter
) > 0;
956 static void playlist_picker_play_activate(GtkMenuItem
attribute((unused
)) *item
,
957 gpointer
attribute((unused
)) userdata
) {
958 /* Re-use the menu-based activation callback */
959 disorder_eclient_playlist_get(client
, playlist_menu_received_content
,
960 playlist_picker_selected
, NULL
);
963 static int playlist_picker_play_sensitive(void *extra
) {
964 GtkTreeIter
*iter
= extra
;
965 return gtk_tree_store_iter_depth(playlist_picker_list
, iter
) > 0;
968 static void playlist_picker_remove_activate(GtkMenuItem
attribute((unused
)) *item
,
969 gpointer
attribute((unused
)) userdata
) {
970 /* Re-use the 'Remove' button' */
971 playlist_picker_delete(NULL
, NULL
);
974 static int playlist_picker_remove_sensitive(void *extra
) {
975 GtkTreeIter
*iter
= extra
;
976 if(gtk_tree_store_iter_depth(playlist_picker_list
, iter
) > 0) {
977 if(strchr(playlist_picker_selected
, '.')) {
978 if(!strncmp(playlist_picker_selected
, config
->username
,
979 strlen(config
->username
)))
987 /** @brief Pop-up menu for picker */
988 static struct menuitem playlist_picker_menuitems
[] = {
991 playlist_picker_select_activate
,
992 playlist_picker_select_sensitive
,
998 playlist_picker_play_activate
,
999 playlist_picker_play_sensitive
,
1005 playlist_picker_remove_activate
,
1006 playlist_picker_remove_sensitive
,
1012 static gboolean
playlist_picker_button(GtkWidget
*widget
,
1013 GdkEventButton
*event
,
1014 gpointer
attribute((unused
)) user_data
) {
1015 if(event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
1016 static GtkWidget
*playlist_picker_menu
;
1018 /* Right click press pops up a menu */
1019 ensure_selected(GTK_TREE_VIEW(widget
), event
);
1020 /* Find the selected row */
1021 GtkTreeIter iter
[1];
1022 if(!gtk_tree_selection_get_selected(playlist_picker_selection
, 0, iter
))
1024 popup(&playlist_picker_menu
, event
,
1025 playlist_picker_menuitems
,
1026 sizeof playlist_picker_menuitems
/ sizeof *playlist_picker_menuitems
,
1033 static void playlist_picker_destroy(void) {
1034 playlist_picker_delete_button
= NULL
;
1035 g_object_unref(playlist_picker_list
);
1036 playlist_picker_list
= NULL
;
1037 playlist_picker_selection
= NULL
;
1038 playlist_picker_selected
= NULL
;
1041 /* Playlist editor ---------------------------------------------------------- */
1043 static GtkWidget
*playlist_editor_shared
;
1044 static GtkWidget
*playlist_editor_public
;
1045 static GtkWidget
*playlist_editor_private
;
1046 static int playlist_editor_setting_buttons
;
1048 static GtkWidget
*playlists_editor_create(void) {
1049 assert(ql_playlist
.view
== NULL
); /* better not be set up already */
1051 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 0);
1052 playlist_editor_shared
= gtk_radio_button_new_with_label(NULL
, "shared");
1053 playlist_editor_public
1054 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared
),
1056 playlist_editor_private
1057 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared
),
1059 g_signal_connect(playlist_editor_public
, "toggled",
1060 G_CALLBACK(playlist_editor_button_toggled
),
1062 g_signal_connect(playlist_editor_private
, "toggled",
1063 G_CALLBACK(playlist_editor_button_toggled
),
1065 gtk_box_pack_start(GTK_BOX(hbox
), playlist_editor_shared
,
1066 FALSE
/*expand*/, FALSE
/*fill*/, 0);
1067 gtk_box_pack_start(GTK_BOX(hbox
), playlist_editor_public
,
1068 FALSE
/*expand*/, FALSE
/*fill*/, 0);
1069 gtk_box_pack_start(GTK_BOX(hbox
), playlist_editor_private
,
1070 FALSE
/*expand*/, FALSE
/*fill*/, 0);
1071 playlist_editor_set_buttons(0,0,0);
1073 GtkWidget
*vbox
= gtk_vbox_new(FALSE
, 0);
1074 gtk_box_pack_start(GTK_BOX(vbox
), init_queuelike(&ql_playlist
),
1075 TRUE
/*expand*/, TRUE
/*fill*/, 0);
1076 gtk_box_pack_start(GTK_BOX(vbox
), hbox
,
1077 FALSE
/*expand*/, FALSE
/*fill*/, 0);
1081 /** @brief Called when the public/private buttons are set */
1082 static void playlist_editor_button_toggled(GtkToggleButton
*tb
,
1083 gpointer userdata
) {
1084 const char *state
= userdata
;
1085 if(!gtk_toggle_button_get_active(tb
)
1086 || !playlist_picker_selected
1087 || playlist_editor_setting_buttons
)
1089 disorder_eclient_playlist_set_share(client
, playlist_editor_share_set
,
1090 playlist_picker_selected
, state
, NULL
);
1093 static void playlist_editor_share_set(void attribute((unused
)) *v
,
1094 const attribute((unused
)) char *err
) {
1096 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1099 /** @brief Set the editor button state and sensitivity */
1100 static void playlist_editor_set_buttons(const char attribute((unused
)) *event
,
1102 void attribute((unused
)) *callbackdata
) {
1103 /* If this event is for a non-selected playlist do nothing */
1105 && playlist_picker_selected
1106 && strcmp(eventdata
, playlist_picker_selected
))
1108 if(playlist_picker_selected
) {
1109 if(strchr(playlist_picker_selected
, '.'))
1110 disorder_eclient_playlist_get_share(client
,
1111 playlist_editor_got_share
,
1112 playlist_picker_selected
,
1113 (void *)playlist_picker_selected
);
1115 playlist_editor_got_share((void *)playlist_picker_selected
, NULL
,
1118 playlist_editor_got_share(NULL
, NULL
, NULL
);
1121 /** @brief Called with playlist sharing details */
1122 static void playlist_editor_got_share(void *v
,
1124 const char *value
) {
1125 const char *playlist
= v
;
1127 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1130 /* Set the currently active button */
1131 ++playlist_editor_setting_buttons
;
1132 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_shared
),
1133 value
&& !strcmp(value
, "shared"));
1134 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_public
),
1135 value
&& !strcmp(value
, "public"));
1136 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_private
),
1137 value
&& !strcmp(value
, "private"));
1138 /* Set button sensitivity */
1139 gtk_widget_set_sensitive(playlist_editor_shared
, FALSE
);
1140 int sensitive
= (playlist
1141 && strchr(playlist
, '.')
1142 && !strncmp(playlist
, config
->username
,
1143 strlen(config
->username
)));
1144 gtk_widget_set_sensitive(playlist_editor_public
, sensitive
);
1145 gtk_widget_set_sensitive(playlist_editor_private
, sensitive
);
1146 --playlist_editor_setting_buttons
;
1149 /** @brief (Re-)populate the playlist tree model */
1150 static void playlist_editor_fill(const char attribute((unused
)) *event
,
1152 void attribute((unused
)) *callbackdata
) {
1153 const char *modified_playlist
= eventdata
;
1154 if(!playlist_window
)
1156 if(!playlist_picker_selected
)
1158 if(!strcmp(playlist_picker_selected
, modified_playlist
))
1159 disorder_eclient_playlist_get(client
, playlists_editor_received_tracks
,
1160 playlist_picker_selected
,
1161 (void *)playlist_picker_selected
);
1164 /** @brief Called with new tracks for the playlist */
1165 static void playlists_editor_received_tracks(void *v
,
1167 int nvec
, char **vec
) {
1168 const char *playlist
= v
;
1170 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1173 if(!playlist_picker_selected
1174 || strcmp(playlist
, playlist_picker_selected
)) {
1175 /* The tracks are for the wrong playlist - something must have changed
1176 * while the fetch command was in flight. We just ignore this callback,
1177 * the right answer will be requested and arrive in due course. */
1181 /* No such playlist, presumably we'll get a deleted event shortly */
1183 /* Translate the list of tracks into queue entries */
1184 struct queue_entry
*newq
, **qq
= &newq
, *qprev
= NULL
;
1185 hash
*h
= hash_new(sizeof(int));
1186 for(int n
= 0; n
< nvec
; ++n
) {
1187 struct queue_entry
*q
= xmalloc(sizeof *q
);
1190 /* Synthesize a unique ID so that the selection survives updates. Tracks
1191 * can appear more than once in the queue so we can't use raw track names,
1192 * so we add a serial number to the start. */
1193 int *serialp
= hash_find(h
, vec
[n
]), serial
= serialp ?
*serialp
: 0;
1194 byte_xasprintf((char **)&q
->id
, "%d-%s", serial
++, vec
[n
]);
1195 hash_add(h
, vec
[n
], &serial
, HASH_INSERT_OR_REPLACE
);
1201 ql_new_queue(&ql_playlist
, newq
);
1204 /* Playlist mutation -------------------------------------------------------- */
1206 /** @brief State structure for guarded playlist modification
1208 * To safely move, insert or delete rows we must:
1210 * - fetch the playlist
1211 * - verify it's not changed
1212 * - update the playlist contents
1213 * - store the playlist
1214 * - release the lock
1216 * The playlist_modify_ functions do just that.
1218 * To kick things off create one of these and disorder_eclient_playlist_lock()
1219 * with playlist_modify_locked() as its callback. @c modify will be called; it
1220 * should disorder_eclient_playlist_set() to set the new state with
1221 * playlist_modify_updated() as its callback.
1223 struct playlist_modify_data
{
1224 /** @brief Affected playlist */
1225 const char *playlist
;
1226 /** @brief Modification function
1227 * @param mod Pointer back to state structure
1228 * @param ntracks Length of playlist
1229 * @param tracks Tracks in playlist
1231 void (*modify
)(struct playlist_modify_data
*mod
,
1232 int ntracks
, char **tracks
);
1234 /** @brief Number of tracks dropped */
1236 /** @brief Track names dropped */
1238 /** @brief Track IDs dropped */
1240 /** @brief Drop after this point */
1241 struct queue_entry
*after_me
;
1244 /** @brief Called with playlist locked
1246 * This is the entry point for guarded modification ising @ref
1247 * playlist_modify_data.
1249 static void playlist_modify_locked(void *v
, const char *err
) {
1250 struct playlist_modify_data
*mod
= v
;
1252 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1255 disorder_eclient_playlist_get(client
, playlist_modify_retrieved
,
1256 mod
->playlist
, mod
);
1259 /** @brief Called with current playlist contents
1260 * Checks that the playlist is still current and has not changed.
1262 void playlist_modify_retrieved(void *v
, const char *err
,
1265 struct playlist_modify_data
*mod
= v
;
1267 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1268 disorder_eclient_playlist_unlock(client
, playlist_modify_unlocked
, NULL
);
1272 || !playlist_picker_selected
1273 || strcmp(mod
->playlist
, playlist_picker_selected
)) {
1274 disorder_eclient_playlist_unlock(client
, playlist_modify_unlocked
, NULL
);
1277 /* We check that the contents haven't changed. If they have we just abandon
1278 * the operation. The user will have to try again. */
1279 struct queue_entry
*q
;
1281 for(n
= 0, q
= ql_playlist
.q
; q
&& n
< nvec
; ++n
, q
= q
->next
)
1282 if(strcmp(q
->track
, vec
[n
]))
1284 if(n
!= nvec
|| q
!= NULL
) {
1285 disorder_eclient_playlist_unlock(client
, playlist_modify_unlocked
, NULL
);
1288 mod
->modify(mod
, nvec
, vec
);
1291 /** @brief Called when the playlist has been updated */
1292 static void playlist_modify_updated(void attribute((unused
)) *v
,
1295 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1296 disorder_eclient_playlist_unlock(client
, playlist_modify_unlocked
, NULL
);
1299 /** @brief Called when the playlist has been unlocked */
1300 static void playlist_modify_unlocked(void attribute((unused
)) *v
,
1303 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1306 /* Drop tracks into a playlist ---------------------------------------------- */
1308 static void playlist_drop(struct queuelike
attribute((unused
)) *ql
,
1310 char **tracks
, char **ids
,
1311 struct queue_entry
*after_me
) {
1312 struct playlist_modify_data
*mod
= xmalloc(sizeof *mod
);
1314 mod
->playlist
= playlist_picker_selected
;
1315 mod
->modify
= playlist_drop_modify
;
1316 mod
->ntracks
= ntracks
;
1317 mod
->tracks
= tracks
;
1319 mod
->after_me
= after_me
;
1320 disorder_eclient_playlist_lock(client
, playlist_modify_locked
,
1321 mod
->playlist
, mod
);
1324 /** @brief Return true if track @p i is in the moved set */
1325 static int playlist_drop_is_moved(struct playlist_modify_data
*mod
,
1327 struct queue_entry
*q
;
1329 /* Find the q corresponding to i, so we can get the ID */
1330 for(q
= ql_playlist
.q
; i
; q
= q
->next
, --i
)
1332 /* See if track i matches any of the moved set by ID */
1333 for(int n
= 0; n
< mod
->ntracks
; ++n
)
1334 if(!strcmp(q
->id
, mod
->ids
[n
]))
1339 static void playlist_drop_modify(struct playlist_modify_data
*mod
,
1340 int nvec
, char **vec
) {
1344 //fprintf(stderr, "\nplaylist_drop_modify\n");
1345 /* after_me is the queue_entry to insert after, or NULL to insert at the
1346 * beginning (including the case when the playlist is empty) */
1347 //fprintf(stderr, "after_me = %s\n",
1348 // mod->after_me ? mod->after_me->track : "NULL");
1349 struct queue_entry
*q
= ql_playlist
.q
;
1353 while(q
&& q
!= mod
->after_me
) {
1358 /* Now ins is the index to insert at; equivalently, the row to insert before,
1359 * and so equal to nvec to append. */
1361 fprintf(stderr
, "ins = %d = %s\n",
1362 ins
, ins
< nvec ? vec
[ins
] : "NULL");
1363 for(int n
= 0; n
< nvec
; ++n
)
1364 fprintf(stderr
, "%d: %s %s\n", n
, n
== ins ?
"->" : " ", vec
[n
]);
1365 fprintf(stderr
, "nvec = %d\n", nvec
);
1368 /* This is a rearrangement */
1370 * - vec[], the current layout
1371 * - ins, pointing into vec
1372 * - mod->tracks[], a subset of vec[] which is to be moved
1374 * ins is the insertion point BUT it is in terms of the whole
1375 * array, i.e. before mod->tracks[] have been removed. The first
1376 * step then is to remove everything in mod->tracks[] and adjust
1377 * ins downwards as necessary.
1379 /* First zero out anything that's moved */
1381 for(int n
= 0; n
< nvec
; ++n
) {
1382 if(playlist_drop_is_moved(mod
, n
)) {
1388 /* Now collapse down the array */
1390 for(int n
= 0; n
< nvec
; ++n
) {
1394 assert(i
+ mod
->ntracks
== nvec
);
1396 /* Adjust the insertion point to take account of things moved from before
1399 /* The effect is now the same as an insertion */
1401 /* This is (now) an insertion */
1402 nnewvec
= nvec
+ mod
->ntracks
;
1403 newvec
= xcalloc(nnewvec
, sizeof (char *));
1405 ins
* sizeof (char *));
1406 memcpy(newvec
+ ins
, mod
->tracks
,
1407 mod
->ntracks
* sizeof (char *));
1408 memcpy(newvec
+ ins
+ mod
->ntracks
, vec
+ ins
,
1409 (nvec
- ins
) * sizeof (char *));
1410 disorder_eclient_playlist_set(client
, playlist_modify_updated
, mod
->playlist
,
1411 newvec
, nnewvec
, mod
);
1414 /* Playlist editor right-click menu ---------------------------------------- */
1416 /** @brief Called to determine whether the playlist is playable */
1417 static int playlist_playall_sensitive(void attribute((unused
)) *extra
) {
1418 /* If there's no playlist obviously we can't play it */
1419 if(!playlist_picker_selected
)
1421 /* If it's empty we can't play it */
1424 /* Otherwise we can */
1428 /** @brief Called to play the selected playlist */
1429 static void playlist_playall_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1430 gpointer
attribute((unused
)) user_data
) {
1431 if(!playlist_picker_selected
)
1433 /* Re-use the menu-based activation callback */
1434 disorder_eclient_playlist_get(client
, playlist_menu_received_content
,
1435 playlist_picker_selected
, NULL
);
1438 /** @brief Called to determine whether the playlist is playable */
1439 static int playlist_remove_sensitive(void attribute((unused
)) *extra
) {
1440 /* If there's no playlist obviously we can't remove from it */
1441 if(!playlist_picker_selected
)
1443 /* If no tracks are selected we cannot remove them */
1444 if(!gtk_tree_selection_count_selected_rows(ql_playlist
.selection
))
1446 /* We're good to go */
1450 /** @brief Called to remove the selected playlist */
1451 static void playlist_remove_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1452 gpointer
attribute((unused
)) user_data
) {
1453 if(!playlist_picker_selected
)
1455 struct playlist_modify_data
*mod
= xmalloc(sizeof *mod
);
1457 mod
->playlist
= playlist_picker_selected
;
1458 mod
->modify
= playlist_remove_modify
;
1459 disorder_eclient_playlist_lock(client
, playlist_modify_locked
,
1460 mod
->playlist
, mod
);
1463 static void playlist_remove_modify(struct playlist_modify_data
*mod
,
1464 int attribute((unused
)) nvec
, char **vec
) {
1465 GtkTreeIter iter
[1];
1466 gboolean it
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql_playlist
.store
),
1470 if(!gtk_tree_selection_iter_is_selected(ql_playlist
.selection
, iter
))
1471 vec
[m
++] = vec
[n
++];
1474 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_playlist
.store
), iter
);
1476 disorder_eclient_playlist_set(client
, playlist_modify_updated
, mod
->playlist
,
1480 /* Playlists window --------------------------------------------------------- */
1482 /** @brief Pop up the playlists window
1484 * Called when the playlists menu item is selected
1486 void playlist_window_create(gpointer
attribute((unused
)) callback_data
,
1487 guint
attribute((unused
)) callback_action
,
1488 GtkWidget
attribute((unused
)) *menu_item
) {
1489 /* If the window already exists, raise it */
1490 if(playlist_window
) {
1491 gtk_window_present(GTK_WINDOW(playlist_window
));
1494 /* Create the window */
1495 playlist_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1496 gtk_widget_set_style(playlist_window
, tool_style
);
1497 g_signal_connect(playlist_window
, "destroy",
1498 G_CALLBACK(playlist_window_destroyed
), &playlist_window
);
1499 gtk_window_set_title(GTK_WINDOW(playlist_window
), "Playlists Management");
1500 /* TODO loads of this is very similar to (copied from!) users.c - can we
1502 /* Keyboard shortcuts */
1503 g_signal_connect(playlist_window
, "key-press-event",
1504 G_CALLBACK(playlist_window_keypress
), 0);
1505 /* default size is too small */
1506 gtk_window_set_default_size(GTK_WINDOW(playlist_window
), 640, 320);
1508 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 0);
1509 gtk_box_pack_start(GTK_BOX(hbox
), playlist_picker_create(),
1510 FALSE
/*expand*/, FALSE
, 0);
1511 gtk_box_pack_start(GTK_BOX(hbox
), gtk_event_box_new(),
1512 FALSE
/*expand*/, FALSE
, 2);
1513 gtk_box_pack_start(GTK_BOX(hbox
), playlists_editor_create(),
1514 TRUE
/*expand*/, TRUE
/*fill*/, 0);
1516 gtk_container_add(GTK_CONTAINER(playlist_window
), frame_widget(hbox
, NULL
));
1517 gtk_widget_show_all(playlist_window
);
1520 /** @brief Keypress handler */
1521 static gboolean
playlist_window_keypress(GtkWidget
attribute((unused
)) *widget
,
1523 gpointer
attribute((unused
)) user_data
) {
1526 switch(event
->keyval
) {
1528 gtk_widget_destroy(playlist_window
);
1535 /** @brief Called when the playlist window is destroyed */
1536 static void playlist_window_destroyed(GtkWidget
attribute((unused
)) *widget
,
1537 GtkWidget
**widget_pointer
) {
1538 destroy_queuelike(&ql_playlist
);
1539 playlist_picker_destroy();
1540 *widget_pointer
= NULL
;
1543 /** @brief Initialize playlist support */
1544 void playlists_init(void) {
1545 /* We re-get all playlists upon any change... */
1546 event_register("playlist-created", playlist_list_update
, 0);
1547 event_register("playlist-deleted", playlist_list_update
, 0);
1548 /* ...and on reconnection */
1549 event_register("log-connected", playlist_list_update
, 0);
1550 /* ...and from time to time */
1551 event_register("periodic-slow", playlist_list_update
, 0);
1552 /* ...and at startup */
1553 playlist_list_update(0, 0, 0);
1555 /* Update the playlists menu when the set of playlists changes */
1556 event_register("playlists-updated", playlist_menu_changed
, 0);
1557 /* Update the new-playlist OK button when the set of playlists changes */
1558 event_register("playlists-updated", playlist_new_changed
, 0);
1559 /* Update the list of playlists in the edit window when the set changes */
1560 event_register("playlists-updated", playlist_picker_fill
, 0);
1561 /* Update the displayed playlist when it is modified */
1562 event_register("playlist-modified", playlist_editor_fill
, 0);
1563 /* Update the shared/public/etc buttons when a playlist is modified */
1564 event_register("playlist-modified", playlist_editor_set_buttons
, 0);
1572 indent-tabs-mode:nil