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
,
129 static gboolean
playlist_editor_keypress(GtkWidget
*widget
,
132 static void playlist_editor_ok(GtkButton
*button
, gpointer userdata
);
133 static void playlist_editor_help(GtkButton
*button
, gpointer userdata
);
135 /** @brief Playlist editing window */
136 static GtkWidget
*playlist_window
;
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
},
145 /** @brief Pop-up menu for playlist editor
148 * - track properties works but, bizarrely, raises the main window
150 * - play playlist works
151 * - select/deselect all work
153 static struct menuitem playlist_menuitems
[] = {
154 { "Track properties", ql_properties_activate
, ql_properties_sensitive
, 0, 0 },
155 { "Play track", ql_play_activate
, ql_play_sensitive
, 0, 0 },
156 { "Play playlist", playlist_playall_activate
, playlist_playall_sensitive
, 0, 0 },
157 { "Remove track from playlist", playlist_remove_activate
, playlist_remove_sensitive
, 0, 0 },
158 { "Select all tracks", ql_selectall_activate
, ql_selectall_sensitive
, 0, 0 },
159 { "Deselect all tracks", ql_selectnone_activate
, ql_selectnone_sensitive
, 0, 0 },
162 static const GtkTargetEntry playlist_targets
[] = {
164 PLAYLIST_TRACKS
, /* drag type */
165 GTK_TARGET_SAME_WIDGET
, /* rearrangement within a widget */
166 PLAYLIST_TRACKS_ID
/* ID value */
169 PLAYABLE_TRACKS
, /* drag type */
170 GTK_TARGET_SAME_APP
|GTK_TARGET_OTHER_WIDGET
, /* copying between widgets */
171 PLAYABLE_TRACKS_ID
, /* ID value */
178 /** @brief Queuelike for editing a playlist */
179 static struct queuelike ql_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
,
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
,
192 /* Maintaining the list of playlists ---------------------------------------- */
194 /** @brief Current list of playlists or NULL */
197 /** @brief Count of playlists */
200 /** @brief Schedule an update to the list of playlists
202 * Called periodically and when a playlist is created or deleted.
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);
210 /** @brief Called with a new list of playlists */
211 static void playlist_list_received_playlists(void attribute((unused
)) *v
,
213 int nvec
, char **vec
) {
217 /* Probably means server does not support playlists */
221 qsort(playlists
, nplaylists
, sizeof (char *), playlistcmp
);
223 /* Tell our consumers */
224 event_raise("playlists-updated", 0);
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
, '.');
233 /* Group owned playlists by owner */
235 const int adn
= ad
- a
, bdn
= bd
- b
;
236 if((c
= strncmp(a
, b
, adn
< bdn ? adn
: bdn
)))
238 /* Lexical order within playlists of a single owner */
239 return strcmp(ad
+ 1, bd
+ 1);
242 /* Owned playlists after shared ones */
249 /* Lexical order of shared playlists */
253 /* Playlists menu ----------------------------------------------------------- */
255 static void playlist_menu_playing(void attribute((unused
)) *v
,
258 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
261 /** @brief Play received playlist contents
263 * Passed as a completion callback by menu_activate_playlist().
265 static void playlist_menu_received_content(void attribute((unused
)) *v
,
267 int nvec
, char **vec
) {
269 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
272 for(int n
= 0; n
< nvec
; ++n
)
273 disorder_eclient_play(client
, vec
[n
], playlist_menu_playing
, NULL
);
276 /** @brief Called to activate a playlist
278 * Called when the menu item for a playlist is clicked.
280 static void playlist_menu_activate(GtkMenuItem
*menuitem
,
281 gpointer
attribute((unused
)) user_data
) {
282 GtkLabel
*label
= GTK_LABEL(GTK_BIN(menuitem
)->child
);
283 const char *playlist
= gtk_label_get_text(label
);
285 disorder_eclient_playlist_get(client
, playlist_menu_received_content
,
289 /** @brief Called when the playlists change
291 * Naively refills the menu. The results might be unsettling if the menu is
292 * currently open, but this is hopefuly fairly rare.
294 static void playlist_menu_changed(const char attribute((unused
)) *event
,
295 void attribute((unused
)) *eventdata
,
296 void attribute((unused
)) *callbackdata
) {
298 return; /* OMG too soon */
299 GtkMenuShell
*menu
= GTK_MENU_SHELL(playlists_menu
);
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
]);
305 g_signal_connect(w
, "activate", G_CALLBACK(playlist_menu_activate
), 0);
307 gtk_menu_shell_append(menu
, w
);
309 gtk_widget_set_sensitive(menu_playlists_widget
,
311 gtk_widget_set_sensitive(menu_editplaylists_widget
,
315 /* Popup to create a new playlist ------------------------------------------- */
317 /** @brief New-playlist popup */
318 static GtkWidget
*playlist_new_window
;
320 /** @brief Text entry in new-playlist popup */
321 static GtkWidget
*playlist_new_entry
;
323 /** @brief Label for displaying feedback on what's wrong */
324 static GtkWidget
*playlist_new_info
;
326 /** @brief "Shared" radio button */
327 static GtkWidget
*playlist_new_shared
;
329 /** @brief "Public" radio button */
330 static GtkWidget
*playlist_new_public
;
332 /** @brief "Private" radio button */
333 static GtkWidget
*playlist_new_private
;
335 /** @brief Buttons for new-playlist popup */
336 static struct button playlist_new_buttons
[] = {
338 .stock
= GTK_STOCK_OK
,
339 .clicked
= playlist_new_ok
,
340 .tip
= "Create new playlist"
343 .stock
= GTK_STOCK_CANCEL
,
344 .clicked
= playlist_new_cancel
,
345 .tip
= "Do not create new playlist"
348 #define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons)
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
));
362 /* Window contents will use a table (grid) layout */
363 GtkWidget
*table
= gtk_table_new(3, 3, FALSE
/*!homogeneous*/);
365 /* First row: playlist name */
366 gtk_table_attach_defaults(GTK_TABLE(table
),
367 gtk_label_new("Playlist name"),
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
),
376 /* Second row: radio buttons to choose type */
377 playlist_new_shared
= gtk_radio_button_new_with_label(NULL
, "shared");
379 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared
),
382 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared
),
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);
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
,
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);
405 gtk_container_add(GTK_CONTAINER(playlist_new_window
),
406 frame_widget(table
, NULL
));
408 /* Set initial state of OK button */
409 playlist_new_changed(0,0,0);
411 g_signal_connect(playlist_new_window
, "key-press-event",
412 G_CALLBACK(playlist_new_keypress
), 0);
414 /* Display the window */
415 gtk_widget_show_all(playlist_new_window
);
418 /** @brief Keypress handler */
419 static gboolean
playlist_new_keypress(GtkWidget
attribute((unused
)) *widget
,
421 gpointer
attribute((unused
)) user_data
) {
424 switch(event
->keyval
) {
426 playlist_new_ok(NULL
, NULL
);
429 gtk_widget_destroy(playlist_new_window
);
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
) {
439 if(playlist_new_valid())
441 gboolean shared
, public, private;
442 char *name
, *fullname
;
443 playlist_new_details(&name
, &fullname
, &shared
, &public, &private);
446 * - lock the playlist
447 * - check it doesn't exist
448 * - set sharing (which will create it empty
451 * TODO we should freeze the window while this is going on to stop a second
454 disorder_eclient_playlist_lock(client
, playlist_new_locked
, fullname
,
458 /** @brief Called when the proposed new playlist has been locked */
459 static void playlist_new_locked(void *v
, const char *err
) {
462 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
465 disorder_eclient_playlist_get(client
, playlist_new_retrieved
,
469 /** @brief Called when the proposed new playlist's contents have been retrieved
471 * ...or rather, normally, when it's been reported that it does not exist.
473 static void playlist_new_retrieved(void *v
, const char *err
,
475 char attribute((unused
)) **vec
) {
477 if(!err
&& nvec
!= -1)
478 /* A rare case but not in principle impossible */
479 err
= "A playlist with that name already exists.";
481 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
482 disorder_eclient_playlist_unlock(client
, playlist_new_unlocked
, fullname
);
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
,
489 : private ?
"private"
494 /** @brief Called when the new playlist has been created */
495 static void playlist_new_created(void attribute((unused
)) *v
, const char *err
) {
497 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
500 disorder_eclient_playlist_unlock(client
, playlist_new_unlocked
, NULL
);
501 // TODO arrange for the new playlist to be selected
504 /** @brief Called when the newly created playlist has unlocked */
505 static void playlist_new_unlocked(void attribute((unused
)) *v
, const char *err
) {
507 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
508 /* Pop down the creation window */
509 gtk_widget_destroy(playlist_new_window
);
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
);
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);
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);
530 /** @brief Called to update new playlist window state
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.
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
)
542 const char *reason
= playlist_new_valid();
543 gtk_widget_set_sensitive(playlist_new_buttons
[0].widget
,
545 gtk_label_set_text(GTK_LABEL(playlist_new_info
), reason
);
548 /** @brief Test whether the new-playlist window settings are valid
549 * @return NULL on success or an error string if not
551 static const char *playlist_new_valid(void) {
552 gboolean shared
, public, private;
553 char *name
, *fullname
;
554 playlist_new_details(&name
, &fullname
, &shared
, &public, &private);
555 if(!(shared
|| public || private))
556 return "No type set.";
559 /* See if the result is valid */
560 if(!valid_username(name
)
561 || playlist_parse_name(fullname
, NULL
, NULL
))
562 return "Not a valid playlist name.";
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. */
568 for(int n
= 0; n
< nplaylists
; ++n
)
569 if(!strcmp(playlists
[n
], fullname
)) {
571 return "A shared playlist with that name already exists.";
573 return "You already have a playlist with that name.";
575 /* As far as we can tell creation would work */
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)
586 static void playlist_new_details(char **namep
,
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
);
599 if(sharedp
) *sharedp
= shared
;
600 if(publicp
) *publicp
= public;
601 if(privatep
) *privatep
= private;
602 if(namep
) *namep
= name
;
604 if(*sharedp
) *fullnamep
= *namep
;
605 else byte_xasprintf(fullnamep
, "%s.%s", config
->username
, name
);
609 /* Playlist picker ---------------------------------------------------------- */
611 /** @brief Delete button */
612 static GtkWidget
*playlist_picker_delete_button
;
614 /** @brief Tree model for list of playlists
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)
620 static GtkTreeStore
*playlist_picker_list
;
622 /** @brief Selection for list of playlists */
623 static GtkTreeSelection
*playlist_picker_selection
;
625 /** @brief Currently selected playlist */
626 static const char *playlist_picker_selected
;
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
) {
634 if(!playlist_picker_list
)
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 *));
639 /* Make sure shared playlists are there */
641 for(end
= start
; end
< nplaylists
&& !strchr(playlists
[end
], '.'); ++end
)
644 playlist_picker_update_section("Shared playlists", "",
646 sections
[nsections
++] = (char *)"";
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
);
654 && playlists
[end
][nl
] == '.'
655 && !strncmp(playlists
[start
], playlists
[end
], nl
);
658 playlist_picker_update_section(name
, name
, start
, end
);
659 sections
[nsections
++] = name
;
661 /* Delete obsolete sections */
662 playlist_picker_delete_obsolete(NULL
, sections
, nsections
);
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
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];
679 if((name
= strchr(playlists
[n
], '.')))
683 playlist_picker_find(section_iter
,
688 /* Delete anything that shouldn't exist. */
689 playlist_picker_delete_obsolete(section_iter
, playlists
+ start
, end
- start
);
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
701 * If the @p key exists then @p iter will point to it and TRUE will be
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.
707 * If the @p key does not exist and @p create is FALSE then FALSE will be
710 static gboolean
playlist_picker_find(GtkTreeIter
*parent
,
720 it
= gtk_tree_model_iter_children(GTK_TREE_MODEL(playlist_picker_list
),
724 /* Find the value at row 'next' */
725 gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list
),
729 /* See how it compares with @p key */
730 int c
= strcmp(key
, candidate
);
734 return TRUE
; /* we found our key */
737 /* @p key belongs before row 'next' */
739 gtk_tree_store_insert_with_values(playlist_picker_list
,
742 row
, /* insert here */
743 0, title
, 1, key
, -1);
749 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(playlist_picker_list
), next
);
751 /* We have reached the end and not found a row that should be later than @p
754 gtk_tree_store_insert_with_values(playlist_picker_list
,
757 INT_MAX
, /* insert at end */
758 0, title
, 1, key
, -1);
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
769 static void playlist_picker_delete_obsolete(GtkTreeIter parent
[1],
772 /* Delete anything that shouldn't exist. */
774 gboolean it
= gtk_tree_model_iter_children(GTK_TREE_MODEL(playlist_picker_list
),
778 /* Find the value at row 'next' */
780 gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list
),
784 gboolean found
= FALSE
;
785 for(int n
= 0; n
< nexists
; ++n
)
786 if((found
= !strcmp(candidate
, exists
[n
])))
789 it
= gtk_tree_store_remove(playlist_picker_list
, iter
);
791 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(playlist_picker_list
),
797 /** @brief Called when the selection might have changed */
798 static void playlist_picker_selection_changed(GtkTreeSelection
attribute((unused
)) *treeselection
,
799 gpointer
attribute((unused
)) user_data
) {
801 char *gselected
, *selected
;
803 /* Identify the current selection */
804 if(gtk_tree_selection_get_selected(playlist_picker_selection
, 0, &iter
)
805 && gtk_tree_store_iter_depth(playlist_picker_list
, &iter
) > 0) {
806 gtk_tree_model_get(GTK_TREE_MODEL(playlist_picker_list
), &iter
,
808 selected
= xstrdup(gselected
);
812 /* Set button sensitivity according to the new state */
813 int deletable
= FALSE
;
815 if(strchr(selected
, '.')) {
816 if(!strncmp(selected
, config
->username
, strlen(config
->username
)))
821 gtk_widget_set_sensitive(playlist_picker_delete_button
, deletable
);
822 /* Eliminate no-change cases */
823 if(!selected
&& !playlist_picker_selected
)
826 && playlist_picker_selected
827 && !strcmp(selected
, playlist_picker_selected
))
829 /* Record the new state */
830 playlist_picker_selected
= selected
;
831 /* Re-initalize the queue */
832 ql_new_queue(&ql_playlist
, NULL
);
833 /* Synthesize a playlist-modified to re-initialize the editor etc */
834 event_raise("playlist-modified", (void *)playlist_picker_selected
);
837 /** @brief Called when the 'add' button is pressed */
838 static void playlist_picker_add(GtkButton
attribute((unused
)) *button
,
839 gpointer
attribute((unused
)) userdata
) {
840 /* Unselect whatever is selected TODO why?? */
841 gtk_tree_selection_unselect_all(playlist_picker_selection
);
842 playlist_new_playlist();
845 /** @brief Called when playlist deletion completes */
846 static void playlists_picker_delete_completed(void attribute((unused
)) *v
,
849 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
852 /** @brief Called when the 'Delete' button is pressed */
853 static void playlist_picker_delete(GtkButton
attribute((unused
)) *button
,
854 gpointer
attribute((unused
)) userdata
) {
858 if(!playlist_picker_selected
)
860 yesno
= gtk_message_dialog_new(GTK_WINDOW(playlist_window
),
862 GTK_MESSAGE_QUESTION
,
864 "Do you really want to delete playlist %s?"
865 " This action cannot be undone.",
866 playlist_picker_selected
);
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
,
871 playlists_picker_delete_completed
,
872 playlist_picker_selected
,
877 /** @brief Table of buttons below the playlist list */
878 static struct button playlist_picker_buttons
[] = {
882 "Create a new playlist",
888 playlist_picker_delete
,
894 #define NPLAYLIST_PICKER_BUTTONS (sizeof playlist_picker_buttons / sizeof *playlist_picker_buttons)
896 /** @brief Create the list of playlists for the edit playlists window */
897 static GtkWidget
*playlist_picker_create(void) {
898 /* Create the list of playlist and populate it */
899 playlist_picker_fill(NULL
, NULL
, NULL
);
900 /* Create the tree view */
901 GtkWidget
*tree
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlist_picker_list
));
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",
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
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
);
917 /* Create the control buttons */
918 GtkWidget
*buttons
= create_buttons_box(playlist_picker_buttons
,
919 NPLAYLIST_PICKER_BUTTONS
,
920 gtk_hbox_new(FALSE
, 1));
921 playlist_picker_delete_button
= playlist_picker_buttons
[1].widget
;
923 playlist_picker_selection_changed(NULL
, NULL
);
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);
930 g_signal_connect(tree
, "key-press-event",
931 G_CALLBACK(playlist_picker_keypress
), 0);
932 g_signal_connect(tree
, "button-press-event",
933 G_CALLBACK(playlist_picker_button
), 0);
938 static gboolean
playlist_picker_keypress(GtkWidget
attribute((unused
)) *widget
,
940 gpointer
attribute((unused
)) user_data
) {
943 switch(event
->keyval
) {
946 playlist_picker_delete(NULL
, NULL
);
953 static void playlist_picker_select_activate(GtkMenuItem
attribute((unused
)) *item
,
954 gpointer
attribute((unused
)) userdata
) {
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;
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
);
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;
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
);
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
)))
994 /** @brief Pop-up menu for picker */
995 static struct menuitem playlist_picker_menuitems
[] = {
998 playlist_picker_select_activate
,
999 playlist_picker_select_sensitive
,
1005 playlist_picker_play_activate
,
1006 playlist_picker_play_sensitive
,
1012 playlist_picker_remove_activate
,
1013 playlist_picker_remove_sensitive
,
1019 static gboolean
playlist_picker_button(GtkWidget
*widget
,
1020 GdkEventButton
*event
,
1021 gpointer
attribute((unused
)) user_data
) {
1022 if(event
->type
== GDK_BUTTON_PRESS
&& event
->button
== 3) {
1023 static GtkWidget
*playlist_picker_menu
;
1025 /* Right click press pops up a menu */
1026 ensure_selected(GTK_TREE_VIEW(widget
), event
);
1027 /* Find the selected row */
1028 GtkTreeIter iter
[1];
1029 if(!gtk_tree_selection_get_selected(playlist_picker_selection
, 0, iter
))
1031 popup(&playlist_picker_menu
, event
,
1032 playlist_picker_menuitems
,
1033 sizeof playlist_picker_menuitems
/ sizeof *playlist_picker_menuitems
,
1040 static void playlist_picker_destroy(void) {
1041 playlist_picker_delete_button
= NULL
;
1042 g_object_unref(playlist_picker_list
);
1043 playlist_picker_list
= NULL
;
1044 playlist_picker_selection
= NULL
;
1045 playlist_picker_selected
= NULL
;
1048 /* Playlist editor ---------------------------------------------------------- */
1050 static GtkWidget
*playlist_editor_shared
;
1051 static GtkWidget
*playlist_editor_public
;
1052 static GtkWidget
*playlist_editor_private
;
1053 static int playlist_editor_setting_buttons
;
1055 /** @brief Buttons for the playlist window */
1056 static struct button playlist_editor_buttons
[] = {
1066 playlist_editor_help
,
1073 #define NPLAYLIST_EDITOR_BUTTONS (int)(sizeof playlist_editor_buttons / sizeof *playlist_editor_buttons)
1075 static GtkWidget
*playlists_editor_create(void) {
1076 assert(ql_playlist
.view
== NULL
); /* better not be set up already */
1078 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 0);
1079 playlist_editor_shared
= gtk_radio_button_new_with_label(NULL
, "shared");
1080 playlist_editor_public
1081 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared
),
1083 playlist_editor_private
1084 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared
),
1086 g_signal_connect(playlist_editor_public
, "toggled",
1087 G_CALLBACK(playlist_editor_button_toggled
),
1089 g_signal_connect(playlist_editor_private
, "toggled",
1090 G_CALLBACK(playlist_editor_button_toggled
),
1092 gtk_box_pack_start(GTK_BOX(hbox
), playlist_editor_shared
,
1093 FALSE
/*expand*/, FALSE
/*fill*/, 0);
1094 gtk_box_pack_start(GTK_BOX(hbox
), playlist_editor_public
,
1095 FALSE
/*expand*/, FALSE
/*fill*/, 0);
1096 gtk_box_pack_start(GTK_BOX(hbox
), playlist_editor_private
,
1097 FALSE
/*expand*/, FALSE
/*fill*/, 0);
1098 playlist_editor_set_buttons(0,0,0);
1099 create_buttons_box(playlist_editor_buttons
,
1100 NPLAYLIST_EDITOR_BUTTONS
,
1103 GtkWidget
*vbox
= gtk_vbox_new(FALSE
, 0);
1104 GtkWidget
*view
= init_queuelike(&ql_playlist
);
1105 gtk_box_pack_start(GTK_BOX(vbox
), view
,
1106 TRUE
/*expand*/, TRUE
/*fill*/, 0);
1107 gtk_box_pack_start(GTK_BOX(vbox
), hbox
,
1108 FALSE
/*expand*/, FALSE
/*fill*/, 0);
1109 g_signal_connect(view
, "key-press-event",
1110 G_CALLBACK(playlist_editor_keypress
), 0);
1114 static gboolean
playlist_editor_keypress(GtkWidget
attribute((unused
)) *widget
,
1116 gpointer
attribute((unused
)) user_data
) {
1119 switch(event
->keyval
) {
1122 playlist_remove_activate(NULL
, NULL
);
1129 /** @brief Called when the public/private buttons are set */
1130 static void playlist_editor_button_toggled(GtkToggleButton
*tb
,
1131 gpointer userdata
) {
1132 const char *state
= userdata
;
1133 if(!gtk_toggle_button_get_active(tb
)
1134 || !playlist_picker_selected
1135 || playlist_editor_setting_buttons
)
1137 disorder_eclient_playlist_set_share(client
, playlist_editor_share_set
,
1138 playlist_picker_selected
, state
, NULL
);
1141 static void playlist_editor_share_set(void attribute((unused
)) *v
,
1142 const attribute((unused
)) char *err
) {
1144 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1147 /** @brief Set the editor button state and sensitivity */
1148 static void playlist_editor_set_buttons(const char attribute((unused
)) *event
,
1150 void attribute((unused
)) *callbackdata
) {
1151 /* If this event is for a non-selected playlist do nothing */
1153 && playlist_picker_selected
1154 && strcmp(eventdata
, playlist_picker_selected
))
1156 if(playlist_picker_selected
) {
1157 if(strchr(playlist_picker_selected
, '.'))
1158 disorder_eclient_playlist_get_share(client
,
1159 playlist_editor_got_share
,
1160 playlist_picker_selected
,
1161 (void *)playlist_picker_selected
);
1163 playlist_editor_got_share((void *)playlist_picker_selected
, NULL
,
1166 playlist_editor_got_share(NULL
, NULL
, NULL
);
1169 /** @brief Called with playlist sharing details */
1170 static void playlist_editor_got_share(void *v
,
1172 const char *value
) {
1173 const char *playlist
= v
;
1175 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1178 /* Set the currently active button */
1179 ++playlist_editor_setting_buttons
;
1180 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_shared
),
1181 value
&& !strcmp(value
, "shared"));
1182 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_public
),
1183 value
&& !strcmp(value
, "public"));
1184 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_private
),
1185 value
&& !strcmp(value
, "private"));
1186 /* Set button sensitivity */
1187 gtk_widget_set_sensitive(playlist_editor_shared
, FALSE
);
1188 int sensitive
= (playlist
1189 && strchr(playlist
, '.')
1190 && !strncmp(playlist
, config
->username
,
1191 strlen(config
->username
)));
1192 gtk_widget_set_sensitive(playlist_editor_public
, sensitive
);
1193 gtk_widget_set_sensitive(playlist_editor_private
, sensitive
);
1194 --playlist_editor_setting_buttons
;
1197 /** @brief (Re-)populate the playlist tree model */
1198 static void playlist_editor_fill(const char attribute((unused
)) *event
,
1200 void attribute((unused
)) *callbackdata
) {
1201 const char *modified_playlist
= eventdata
;
1202 if(!playlist_window
)
1204 if(!playlist_picker_selected
)
1206 if(!strcmp(playlist_picker_selected
, modified_playlist
))
1207 disorder_eclient_playlist_get(client
, playlists_editor_received_tracks
,
1208 playlist_picker_selected
,
1209 (void *)playlist_picker_selected
);
1212 /** @brief Called with new tracks for the playlist */
1213 static void playlists_editor_received_tracks(void *v
,
1215 int nvec
, char **vec
) {
1216 const char *playlist
= v
;
1218 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1221 if(!playlist_picker_selected
1222 || strcmp(playlist
, playlist_picker_selected
)) {
1223 /* The tracks are for the wrong playlist - something must have changed
1224 * while the fetch command was in flight. We just ignore this callback,
1225 * the right answer will be requested and arrive in due course. */
1229 /* No such playlist, presumably we'll get a deleted event shortly */
1231 /* Translate the list of tracks into queue entries */
1232 struct queue_entry
*newq
, **qq
= &newq
, *qprev
= NULL
;
1233 hash
*h
= hash_new(sizeof(int));
1234 for(int n
= 0; n
< nvec
; ++n
) {
1235 struct queue_entry
*q
= xmalloc(sizeof *q
);
1238 /* Synthesize a unique ID so that the selection survives updates. Tracks
1239 * can appear more than once in the queue so we can't use raw track names,
1240 * so we add a serial number to the start. */
1241 int *serialp
= hash_find(h
, vec
[n
]), serial
= serialp ?
*serialp
: 0;
1242 byte_xasprintf((char **)&q
->id
, "%d-%s", serial
++, vec
[n
]);
1243 hash_add(h
, vec
[n
], &serial
, HASH_INSERT_OR_REPLACE
);
1249 ql_new_queue(&ql_playlist
, newq
);
1252 static void playlist_editor_ok(GtkButton
attribute((unused
)) *button
,
1253 gpointer
attribute((unused
)) userdata
) {
1254 gtk_widget_destroy(playlist_window
);
1257 static void playlist_editor_help(GtkButton
attribute((unused
)) *button
,
1258 gpointer
attribute((unused
)) userdata
) {
1259 popup_help("playlists.html");
1262 /* Playlist mutation -------------------------------------------------------- */
1264 /** @brief State structure for guarded playlist modification
1266 * To safely move, insert or delete rows we must:
1268 * - fetch the playlist
1269 * - verify it's not changed
1270 * - update the playlist contents
1271 * - store the playlist
1272 * - release the lock
1274 * The playlist_modify_ functions do just that.
1276 * To kick things off create one of these and disorder_eclient_playlist_lock()
1277 * with playlist_modify_locked() as its callback. @c modify will be called; it
1278 * should disorder_eclient_playlist_set() to set the new state with
1279 * playlist_modify_updated() as its callback.
1281 struct playlist_modify_data
{
1282 /** @brief Affected playlist */
1283 const char *playlist
;
1284 /** @brief Modification function
1285 * @param mod Pointer back to state structure
1286 * @param ntracks Length of playlist
1287 * @param tracks Tracks in playlist
1289 void (*modify
)(struct playlist_modify_data
*mod
,
1290 int ntracks
, char **tracks
);
1292 /** @brief Number of tracks dropped */
1294 /** @brief Track names dropped */
1296 /** @brief Track IDs dropped */
1298 /** @brief Drop after this point */
1299 struct queue_entry
*after_me
;
1302 /** @brief Called with playlist locked
1304 * This is the entry point for guarded modification ising @ref
1305 * playlist_modify_data.
1307 static void playlist_modify_locked(void *v
, const char *err
) {
1308 struct playlist_modify_data
*mod
= v
;
1310 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1313 disorder_eclient_playlist_get(client
, playlist_modify_retrieved
,
1314 mod
->playlist
, mod
);
1317 /** @brief Called with current playlist contents
1318 * Checks that the playlist is still current and has not changed.
1320 void playlist_modify_retrieved(void *v
, const char *err
,
1323 struct playlist_modify_data
*mod
= v
;
1325 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1326 disorder_eclient_playlist_unlock(client
, playlist_modify_unlocked
, NULL
);
1330 || !playlist_picker_selected
1331 || strcmp(mod
->playlist
, playlist_picker_selected
)) {
1332 disorder_eclient_playlist_unlock(client
, playlist_modify_unlocked
, NULL
);
1335 /* We check that the contents haven't changed. If they have we just abandon
1336 * the operation. The user will have to try again. */
1337 struct queue_entry
*q
;
1339 for(n
= 0, q
= ql_playlist
.q
; q
&& n
< nvec
; ++n
, q
= q
->next
)
1340 if(strcmp(q
->track
, vec
[n
]))
1342 if(n
!= nvec
|| q
!= NULL
) {
1343 disorder_eclient_playlist_unlock(client
, playlist_modify_unlocked
, NULL
);
1346 mod
->modify(mod
, nvec
, vec
);
1349 /** @brief Called when the playlist has been updated */
1350 static void playlist_modify_updated(void attribute((unused
)) *v
,
1353 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1354 disorder_eclient_playlist_unlock(client
, playlist_modify_unlocked
, NULL
);
1357 /** @brief Called when the playlist has been unlocked */
1358 static void playlist_modify_unlocked(void attribute((unused
)) *v
,
1361 popup_submsg(playlist_window
, GTK_MESSAGE_ERROR
, err
);
1364 /* Drop tracks into a playlist ---------------------------------------------- */
1366 static void playlist_drop(struct queuelike
attribute((unused
)) *ql
,
1368 char **tracks
, char **ids
,
1369 struct queue_entry
*after_me
) {
1370 struct playlist_modify_data
*mod
= xmalloc(sizeof *mod
);
1372 mod
->playlist
= playlist_picker_selected
;
1373 mod
->modify
= playlist_drop_modify
;
1374 mod
->ntracks
= ntracks
;
1375 mod
->tracks
= tracks
;
1377 mod
->after_me
= after_me
;
1378 disorder_eclient_playlist_lock(client
, playlist_modify_locked
,
1379 mod
->playlist
, mod
);
1382 /** @brief Return true if track @p i is in the moved set */
1383 static int playlist_drop_is_moved(struct playlist_modify_data
*mod
,
1385 struct queue_entry
*q
;
1387 /* Find the q corresponding to i, so we can get the ID */
1388 for(q
= ql_playlist
.q
; i
; q
= q
->next
, --i
)
1390 /* See if track i matches any of the moved set by ID */
1391 for(int n
= 0; n
< mod
->ntracks
; ++n
)
1392 if(!strcmp(q
->id
, mod
->ids
[n
]))
1397 static void playlist_drop_modify(struct playlist_modify_data
*mod
,
1398 int nvec
, char **vec
) {
1402 //fprintf(stderr, "\nplaylist_drop_modify\n");
1403 /* after_me is the queue_entry to insert after, or NULL to insert at the
1404 * beginning (including the case when the playlist is empty) */
1405 //fprintf(stderr, "after_me = %s\n",
1406 // mod->after_me ? mod->after_me->track : "NULL");
1407 struct queue_entry
*q
= ql_playlist
.q
;
1411 while(q
&& q
!= mod
->after_me
) {
1416 /* Now ins is the index to insert at; equivalently, the row to insert before,
1417 * and so equal to nvec to append. */
1419 fprintf(stderr
, "ins = %d = %s\n",
1420 ins
, ins
< nvec ? vec
[ins
] : "NULL");
1421 for(int n
= 0; n
< nvec
; ++n
)
1422 fprintf(stderr
, "%d: %s %s\n", n
, n
== ins ?
"->" : " ", vec
[n
]);
1423 fprintf(stderr
, "nvec = %d\n", nvec
);
1426 /* This is a rearrangement */
1428 * - vec[], the current layout
1429 * - ins, pointing into vec
1430 * - mod->tracks[], a subset of vec[] which is to be moved
1432 * ins is the insertion point BUT it is in terms of the whole
1433 * array, i.e. before mod->tracks[] have been removed. The first
1434 * step then is to remove everything in mod->tracks[] and adjust
1435 * ins downwards as necessary.
1437 /* First zero out anything that's moved */
1439 for(int n
= 0; n
< nvec
; ++n
) {
1440 if(playlist_drop_is_moved(mod
, n
)) {
1446 /* Now collapse down the array */
1448 for(int n
= 0; n
< nvec
; ++n
) {
1452 assert(i
+ mod
->ntracks
== nvec
);
1454 /* Adjust the insertion point to take account of things moved from before
1457 /* The effect is now the same as an insertion */
1459 /* This is (now) an insertion */
1460 nnewvec
= nvec
+ mod
->ntracks
;
1461 newvec
= xcalloc(nnewvec
, sizeof (char *));
1463 ins
* sizeof (char *));
1464 memcpy(newvec
+ ins
, mod
->tracks
,
1465 mod
->ntracks
* sizeof (char *));
1466 memcpy(newvec
+ ins
+ mod
->ntracks
, vec
+ ins
,
1467 (nvec
- ins
) * sizeof (char *));
1468 disorder_eclient_playlist_set(client
, playlist_modify_updated
, mod
->playlist
,
1469 newvec
, nnewvec
, mod
);
1472 /* Playlist editor right-click menu ---------------------------------------- */
1474 /** @brief Called to determine whether the playlist is playable */
1475 static int playlist_playall_sensitive(void attribute((unused
)) *extra
) {
1476 /* If there's no playlist obviously we can't play it */
1477 if(!playlist_picker_selected
)
1479 /* If it's empty we can't play it */
1482 /* Otherwise we can */
1486 /** @brief Called to play the selected playlist */
1487 static void playlist_playall_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1488 gpointer
attribute((unused
)) user_data
) {
1489 if(!playlist_picker_selected
)
1491 /* Re-use the menu-based activation callback */
1492 disorder_eclient_playlist_get(client
, playlist_menu_received_content
,
1493 playlist_picker_selected
, NULL
);
1496 /** @brief Called to determine whether the playlist is playable */
1497 static int playlist_remove_sensitive(void attribute((unused
)) *extra
) {
1498 /* If there's no playlist obviously we can't remove from it */
1499 if(!playlist_picker_selected
)
1501 /* If no tracks are selected we cannot remove them */
1502 if(!gtk_tree_selection_count_selected_rows(ql_playlist
.selection
))
1504 /* We're good to go */
1508 /** @brief Called to remove the selected playlist */
1509 static void playlist_remove_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1510 gpointer
attribute((unused
)) user_data
) {
1511 if(!playlist_picker_selected
)
1513 struct playlist_modify_data
*mod
= xmalloc(sizeof *mod
);
1515 mod
->playlist
= playlist_picker_selected
;
1516 mod
->modify
= playlist_remove_modify
;
1517 disorder_eclient_playlist_lock(client
, playlist_modify_locked
,
1518 mod
->playlist
, mod
);
1521 static void playlist_remove_modify(struct playlist_modify_data
*mod
,
1522 int attribute((unused
)) nvec
, char **vec
) {
1523 GtkTreeIter iter
[1];
1524 gboolean it
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql_playlist
.store
),
1528 if(!gtk_tree_selection_iter_is_selected(ql_playlist
.selection
, iter
))
1529 vec
[m
++] = vec
[n
++];
1532 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_playlist
.store
), iter
);
1534 disorder_eclient_playlist_set(client
, playlist_modify_updated
, mod
->playlist
,
1538 /* Playlists window --------------------------------------------------------- */
1540 /** @brief Pop up the playlists window
1542 * Called when the playlists menu item is selected
1544 void playlist_window_create(gpointer
attribute((unused
)) callback_data
,
1545 guint
attribute((unused
)) callback_action
,
1546 GtkWidget
attribute((unused
)) *menu_item
) {
1547 /* If the window already exists, raise it */
1548 if(playlist_window
) {
1549 gtk_window_present(GTK_WINDOW(playlist_window
));
1552 /* Create the window */
1553 playlist_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1554 gtk_widget_set_style(playlist_window
, tool_style
);
1555 g_signal_connect(playlist_window
, "destroy",
1556 G_CALLBACK(playlist_window_destroyed
), &playlist_window
);
1557 gtk_window_set_title(GTK_WINDOW(playlist_window
), "Playlists Management");
1558 /* TODO loads of this is very similar to (copied from!) users.c - can we
1560 /* Keyboard shortcuts */
1561 g_signal_connect(playlist_window
, "key-press-event",
1562 G_CALLBACK(playlist_window_keypress
), 0);
1563 /* default size is too small */
1564 gtk_window_set_default_size(GTK_WINDOW(playlist_window
), 640, 320);
1566 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 0);
1567 gtk_box_pack_start(GTK_BOX(hbox
), playlist_picker_create(),
1568 FALSE
/*expand*/, FALSE
, 0);
1569 gtk_box_pack_start(GTK_BOX(hbox
), gtk_event_box_new(),
1570 FALSE
/*expand*/, FALSE
, 2);
1571 gtk_box_pack_start(GTK_BOX(hbox
), playlists_editor_create(),
1572 TRUE
/*expand*/, TRUE
/*fill*/, 0);
1574 gtk_container_add(GTK_CONTAINER(playlist_window
), frame_widget(hbox
, NULL
));
1575 gtk_widget_show_all(playlist_window
);
1578 /** @brief Keypress handler */
1579 static gboolean
playlist_window_keypress(GtkWidget
attribute((unused
)) *widget
,
1581 gpointer
attribute((unused
)) user_data
) {
1584 switch(event
->keyval
) {
1586 gtk_widget_destroy(playlist_window
);
1593 /** @brief Called when the playlist window is destroyed */
1594 static void playlist_window_destroyed(GtkWidget
attribute((unused
)) *widget
,
1595 GtkWidget
**widget_pointer
) {
1596 destroy_queuelike(&ql_playlist
);
1597 playlist_picker_destroy();
1598 *widget_pointer
= NULL
;
1601 /** @brief Initialize playlist support */
1602 void playlists_init(void) {
1603 /* We re-get all playlists upon any change... */
1604 event_register("playlist-created", playlist_list_update
, 0);
1605 event_register("playlist-deleted", playlist_list_update
, 0);
1606 /* ...and on reconnection */
1607 event_register("log-connected", playlist_list_update
, 0);
1608 /* ...and from time to time */
1609 event_register("periodic-slow", playlist_list_update
, 0);
1610 /* ...and at startup */
1611 playlist_list_update(0, 0, 0);
1613 /* Update the playlists menu when the set of playlists changes */
1614 event_register("playlists-updated", playlist_menu_changed
, 0);
1615 /* Update the new-playlist OK button when the set of playlists changes */
1616 event_register("playlists-updated", playlist_new_changed
, 0);
1617 /* Update the list of playlists in the edit window when the set changes */
1618 event_register("playlists-updated", playlist_picker_fill
, 0);
1619 /* Update the displayed playlist when it is modified */
1620 event_register("playlist-modified", playlist_editor_fill
, 0);
1621 /* Update the shared/public/etc buttons when a playlist is modified */
1622 event_register("playlist-modified", playlist_editor_set_buttons
, 0);
1630 indent-tabs-mode:nil