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", GTK_STOCK_PROPERTIES, ql_properties_activate, ql_properties_sensitive, 0, 0 },
155 { "Play track", GTK_STOCK_MEDIA_PLAY, ql_play_activate, ql_play_sensitive, 0, 0 },
156 { "Play playlist", NULL, playlist_playall_activate, playlist_playall_sensitive, 0, 0 },
157 { "Remove track from playlist", GTK_STOCK_DELETE, playlist_remove_activate, playlist_remove_sensitive, 0, 0 },
158 { "Select all tracks", GTK_STOCK_SELECT_ALL, ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
159 { "Deselect all tracks", NULL, ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
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(shared) *fullnamep = name;
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[] = {
999 playlist_picker_select_activate,
1000 playlist_picker_select_sensitive,
1006 GTK_STOCK_MEDIA_PLAY,
1007 playlist_picker_play_activate,
1008 playlist_picker_play_sensitive,
1015 playlist_picker_remove_activate,
1016 playlist_picker_remove_sensitive,
1022 static gboolean playlist_picker_button(GtkWidget *widget,
1023 GdkEventButton *event,
1024 gpointer attribute((unused)) user_data) {
1025 if(event->type == GDK_BUTTON_PRESS && event->button == 3) {
1026 static GtkWidget *playlist_picker_menu;
1028 /* Right click press pops up a menu */
1029 ensure_selected(GTK_TREE_VIEW(widget), event);
1030 /* Find the selected row */
1031 GtkTreeIter iter[1];
1032 if(!gtk_tree_selection_get_selected(playlist_picker_selection, 0, iter))
1034 popup(&playlist_picker_menu, event,
1035 playlist_picker_menuitems,
1036 sizeof playlist_picker_menuitems / sizeof *playlist_picker_menuitems,
1043 static void playlist_picker_destroy(void) {
1044 playlist_picker_delete_button = NULL;
1045 g_object_unref(playlist_picker_list);
1046 playlist_picker_list = NULL;
1047 playlist_picker_selection = NULL;
1048 playlist_picker_selected = NULL;
1051 /* Playlist editor ---------------------------------------------------------- */
1053 static GtkWidget *playlist_editor_shared;
1054 static GtkWidget *playlist_editor_public;
1055 static GtkWidget *playlist_editor_private;
1056 static int playlist_editor_setting_buttons;
1058 /** @brief Buttons for the playlist window */
1059 static struct button playlist_editor_buttons[] = {
1069 playlist_editor_help,
1076 #define NPLAYLIST_EDITOR_BUTTONS (int)(sizeof playlist_editor_buttons / sizeof *playlist_editor_buttons)
1078 static GtkWidget *playlists_editor_create(void) {
1079 assert(ql_playlist.view == NULL); /* better not be set up already */
1081 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
1082 playlist_editor_shared = gtk_radio_button_new_with_label(NULL, "shared");
1083 playlist_editor_public
1084 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared),
1086 playlist_editor_private
1087 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_editor_shared),
1089 g_signal_connect(playlist_editor_public, "toggled",
1090 G_CALLBACK(playlist_editor_button_toggled),
1092 g_signal_connect(playlist_editor_private, "toggled",
1093 G_CALLBACK(playlist_editor_button_toggled),
1095 gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_shared,
1096 FALSE/*expand*/, FALSE/*fill*/, 0);
1097 gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_public,
1098 FALSE/*expand*/, FALSE/*fill*/, 0);
1099 gtk_box_pack_start(GTK_BOX(hbox), playlist_editor_private,
1100 FALSE/*expand*/, FALSE/*fill*/, 0);
1101 playlist_editor_set_buttons(0,0,0);
1102 create_buttons_box(playlist_editor_buttons,
1103 NPLAYLIST_EDITOR_BUTTONS,
1106 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
1107 GtkWidget *view = init_queuelike(&ql_playlist);
1108 gtk_box_pack_start(GTK_BOX(vbox), view,
1109 TRUE/*expand*/, TRUE/*fill*/, 0);
1110 gtk_box_pack_start(GTK_BOX(vbox), hbox,
1111 FALSE/*expand*/, FALSE/*fill*/, 0);
1112 g_signal_connect(view, "key-press-event",
1113 G_CALLBACK(playlist_editor_keypress), 0);
1117 static gboolean playlist_editor_keypress(GtkWidget attribute((unused)) *widget,
1119 gpointer attribute((unused)) user_data) {
1122 switch(event->keyval) {
1125 playlist_remove_activate(NULL, NULL);
1132 /** @brief Called when the public/private buttons are set */
1133 static void playlist_editor_button_toggled(GtkToggleButton *tb,
1134 gpointer userdata) {
1135 const char *state = userdata;
1136 if(!gtk_toggle_button_get_active(tb)
1137 || !playlist_picker_selected
1138 || playlist_editor_setting_buttons)
1140 disorder_eclient_playlist_set_share(client, playlist_editor_share_set,
1141 playlist_picker_selected, state, NULL);
1144 static void playlist_editor_share_set(void attribute((unused)) *v,
1145 const attribute((unused)) char *err) {
1147 popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
1150 /** @brief Set the editor button state and sensitivity */
1151 static void playlist_editor_set_buttons(const char attribute((unused)) *event,
1153 void attribute((unused)) *callbackdata) {
1154 /* If this event is for a non-selected playlist do nothing */
1156 && playlist_picker_selected
1157 && strcmp(eventdata, playlist_picker_selected))
1159 if(playlist_picker_selected) {
1160 if(strchr(playlist_picker_selected, '.'))
1161 disorder_eclient_playlist_get_share(client,
1162 playlist_editor_got_share,
1163 playlist_picker_selected,
1164 (void *)playlist_picker_selected);
1166 playlist_editor_got_share((void *)playlist_picker_selected, NULL,
1169 playlist_editor_got_share(NULL, NULL, NULL);
1172 /** @brief Called with playlist sharing details */
1173 static void playlist_editor_got_share(void *v,
1175 const char *value) {
1176 const char *playlist = v;
1178 popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
1181 /* Set the currently active button */
1182 ++playlist_editor_setting_buttons;
1183 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_shared),
1184 value && !strcmp(value, "shared"));
1185 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_public),
1186 value && !strcmp(value, "public"));
1187 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(playlist_editor_private),
1188 value && !strcmp(value, "private"));
1189 /* Set button sensitivity */
1190 gtk_widget_set_sensitive(playlist_editor_shared, FALSE);
1191 int sensitive = (playlist
1192 && strchr(playlist, '.')
1193 && !strncmp(playlist, config->username,
1194 strlen(config->username)));
1195 gtk_widget_set_sensitive(playlist_editor_public, sensitive);
1196 gtk_widget_set_sensitive(playlist_editor_private, sensitive);
1197 --playlist_editor_setting_buttons;
1200 /** @brief (Re-)populate the playlist tree model */
1201 static void playlist_editor_fill(const char attribute((unused)) *event,
1203 void attribute((unused)) *callbackdata) {
1204 const char *modified_playlist = eventdata;
1205 if(!playlist_window)
1207 if(!playlist_picker_selected)
1209 if(!strcmp(playlist_picker_selected, modified_playlist))
1210 disorder_eclient_playlist_get(client, playlists_editor_received_tracks,
1211 playlist_picker_selected,
1212 (void *)playlist_picker_selected);
1215 /** @brief Called with new tracks for the playlist */
1216 static void playlists_editor_received_tracks(void *v,
1218 int nvec, char **vec) {
1219 const char *playlist = v;
1221 popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
1224 if(!playlist_picker_selected
1225 || strcmp(playlist, playlist_picker_selected)) {
1226 /* The tracks are for the wrong playlist - something must have changed
1227 * while the fetch command was in flight. We just ignore this callback,
1228 * the right answer will be requested and arrive in due course. */
1232 /* No such playlist, presumably we'll get a deleted event shortly */
1234 /* Translate the list of tracks into queue entries */
1235 struct queue_entry *newq, **qq = &newq, *qprev = NULL;
1236 hash *h = hash_new(sizeof(int));
1237 for(int n = 0; n < nvec; ++n) {
1238 struct queue_entry *q = xmalloc(sizeof *q);
1241 /* Synthesize a unique ID so that the selection survives updates. Tracks
1242 * can appear more than once in the queue so we can't use raw track names,
1243 * so we add a serial number to the start. */
1244 int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0;
1245 byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]);
1246 hash_add(h, vec[n], &serial, HASH_INSERT_OR_REPLACE);
1252 ql_new_queue(&ql_playlist, newq);
1255 static void playlist_editor_ok(GtkButton attribute((unused)) *button,
1256 gpointer attribute((unused)) userdata) {
1257 gtk_widget_destroy(playlist_window);
1260 static void playlist_editor_help(GtkButton attribute((unused)) *button,
1261 gpointer attribute((unused)) userdata) {
1262 popup_help("playlists.html");
1265 /* Playlist mutation -------------------------------------------------------- */
1267 /** @brief State structure for guarded playlist modification
1269 * To safely move, insert or delete rows we must:
1271 * - fetch the playlist
1272 * - verify it's not changed
1273 * - update the playlist contents
1274 * - store the playlist
1275 * - release the lock
1277 * The playlist_modify_ functions do just that.
1279 * To kick things off create one of these and disorder_eclient_playlist_lock()
1280 * with playlist_modify_locked() as its callback. @c modify will be called; it
1281 * should disorder_eclient_playlist_set() to set the new state with
1282 * playlist_modify_updated() as its callback.
1284 struct playlist_modify_data {
1285 /** @brief Affected playlist */
1286 const char *playlist;
1287 /** @brief Modification function
1288 * @param mod Pointer back to state structure
1289 * @param ntracks Length of playlist
1290 * @param tracks Tracks in playlist
1292 void (*modify)(struct playlist_modify_data *mod,
1293 int ntracks, char **tracks);
1295 /** @brief Number of tracks dropped */
1297 /** @brief Track names dropped */
1299 /** @brief Track IDs dropped */
1301 /** @brief Drop after this point */
1302 struct queue_entry *after_me;
1305 /** @brief Called with playlist locked
1307 * This is the entry point for guarded modification ising @ref
1308 * playlist_modify_data.
1310 static void playlist_modify_locked(void *v, const char *err) {
1311 struct playlist_modify_data *mod = v;
1313 popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
1316 disorder_eclient_playlist_get(client, playlist_modify_retrieved,
1317 mod->playlist, mod);
1320 /** @brief Called with current playlist contents
1321 * Checks that the playlist is still current and has not changed.
1323 void playlist_modify_retrieved(void *v, const char *err,
1326 struct playlist_modify_data *mod = v;
1328 popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
1329 disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
1333 || !playlist_picker_selected
1334 || strcmp(mod->playlist, playlist_picker_selected)) {
1335 disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
1338 /* We check that the contents haven't changed. If they have we just abandon
1339 * the operation. The user will have to try again. */
1340 struct queue_entry *q;
1342 for(n = 0, q = ql_playlist.q; q && n < nvec; ++n, q = q->next)
1343 if(strcmp(q->track, vec[n]))
1345 if(n != nvec || q != NULL) {
1346 disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
1349 mod->modify(mod, nvec, vec);
1352 /** @brief Called when the playlist has been updated */
1353 static void playlist_modify_updated(void attribute((unused)) *v,
1356 popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
1357 disorder_eclient_playlist_unlock(client, playlist_modify_unlocked, NULL);
1360 /** @brief Called when the playlist has been unlocked */
1361 static void playlist_modify_unlocked(void attribute((unused)) *v,
1364 popup_submsg(playlist_window, GTK_MESSAGE_ERROR, err);
1367 /* Drop tracks into a playlist ---------------------------------------------- */
1369 static void playlist_drop(struct queuelike attribute((unused)) *ql,
1371 char **tracks, char **ids,
1372 struct queue_entry *after_me) {
1373 struct playlist_modify_data *mod = xmalloc(sizeof *mod);
1375 mod->playlist = playlist_picker_selected;
1376 mod->modify = playlist_drop_modify;
1377 mod->ntracks = ntracks;
1378 mod->tracks = tracks;
1380 mod->after_me = after_me;
1381 disorder_eclient_playlist_lock(client, playlist_modify_locked,
1382 mod->playlist, mod);
1385 /** @brief Return true if track @p i is in the moved set */
1386 static int playlist_drop_is_moved(struct playlist_modify_data *mod,
1388 struct queue_entry *q;
1390 /* Find the q corresponding to i, so we can get the ID */
1391 for(q = ql_playlist.q; i; q = q->next, --i)
1393 /* See if track i matches any of the moved set by ID */
1394 for(int n = 0; n < mod->ntracks; ++n)
1395 if(!strcmp(q->id, mod->ids[n]))
1400 static void playlist_drop_modify(struct playlist_modify_data *mod,
1401 int nvec, char **vec) {
1405 //fprintf(stderr, "\nplaylist_drop_modify\n");
1406 /* after_me is the queue_entry to insert after, or NULL to insert at the
1407 * beginning (including the case when the playlist is empty) */
1408 //fprintf(stderr, "after_me = %s\n",
1409 // mod->after_me ? mod->after_me->track : "NULL");
1410 struct queue_entry *q = ql_playlist.q;
1414 while(q && q != mod->after_me) {
1419 /* Now ins is the index to insert at; equivalently, the row to insert before,
1420 * and so equal to nvec to append. */
1422 fprintf(stderr, "ins = %d = %s\n",
1423 ins, ins < nvec ? vec[ins] : "NULL");
1424 for(int n = 0; n < nvec; ++n)
1425 fprintf(stderr, "%d: %s %s\n", n, n == ins ? "->" : " ", vec[n]);
1426 fprintf(stderr, "nvec = %d\n", nvec);
1429 /* This is a rearrangement */
1431 * - vec[], the current layout
1432 * - ins, pointing into vec
1433 * - mod->tracks[], a subset of vec[] which is to be moved
1435 * ins is the insertion point BUT it is in terms of the whole
1436 * array, i.e. before mod->tracks[] have been removed. The first
1437 * step then is to remove everything in mod->tracks[] and adjust
1438 * ins downwards as necessary.
1440 /* First zero out anything that's moved */
1442 for(int n = 0; n < nvec; ++n) {
1443 if(playlist_drop_is_moved(mod, n)) {
1449 /* Now collapse down the array */
1451 for(int n = 0; n < nvec; ++n) {
1455 assert(i + mod->ntracks == nvec);
1457 /* Adjust the insertion point to take account of things moved from before
1460 /* The effect is now the same as an insertion */
1462 /* This is (now) an insertion */
1463 nnewvec = nvec + mod->ntracks;
1464 newvec = xcalloc(nnewvec, sizeof (char *));
1466 ins * sizeof (char *));
1467 memcpy(newvec + ins, mod->tracks,
1468 mod->ntracks * sizeof (char *));
1469 memcpy(newvec + ins + mod->ntracks, vec + ins,
1470 (nvec - ins) * sizeof (char *));
1471 disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist,
1472 newvec, nnewvec, mod);
1475 /* Playlist editor right-click menu ---------------------------------------- */
1477 /** @brief Called to determine whether the playlist is playable */
1478 static int playlist_playall_sensitive(void attribute((unused)) *extra) {
1479 /* If there's no playlist obviously we can't play it */
1480 if(!playlist_picker_selected)
1482 /* If it's empty we can't play it */
1485 /* Otherwise we can */
1489 /** @brief Called to play the selected playlist */
1490 static void playlist_playall_activate(GtkMenuItem attribute((unused)) *menuitem,
1491 gpointer attribute((unused)) user_data) {
1492 if(!playlist_picker_selected)
1494 /* Re-use the menu-based activation callback */
1495 disorder_eclient_playlist_get(client, playlist_menu_received_content,
1496 playlist_picker_selected, NULL);
1499 /** @brief Called to determine whether the playlist is playable */
1500 static int playlist_remove_sensitive(void attribute((unused)) *extra) {
1501 /* If there's no playlist obviously we can't remove from it */
1502 if(!playlist_picker_selected)
1504 /* If no tracks are selected we cannot remove them */
1505 if(!gtk_tree_selection_count_selected_rows(ql_playlist.selection))
1507 /* We're good to go */
1511 /** @brief Called to remove the selected playlist */
1512 static void playlist_remove_activate(GtkMenuItem attribute((unused)) *menuitem,
1513 gpointer attribute((unused)) user_data) {
1514 if(!playlist_picker_selected)
1516 struct playlist_modify_data *mod = xmalloc(sizeof *mod);
1518 mod->playlist = playlist_picker_selected;
1519 mod->modify = playlist_remove_modify;
1520 disorder_eclient_playlist_lock(client, playlist_modify_locked,
1521 mod->playlist, mod);
1524 static void playlist_remove_modify(struct playlist_modify_data *mod,
1525 int attribute((unused)) nvec, char **vec) {
1526 GtkTreeIter iter[1];
1527 gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql_playlist.store),
1531 if(!gtk_tree_selection_iter_is_selected(ql_playlist.selection, iter))
1532 vec[m++] = vec[n++];
1535 it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_playlist.store), iter);
1537 disorder_eclient_playlist_set(client, playlist_modify_updated, mod->playlist,
1541 /* Playlists window --------------------------------------------------------- */
1543 /** @brief Pop up the playlists window
1545 * Called when the playlists menu item is selected
1547 void playlist_window_create(gpointer attribute((unused)) callback_data,
1548 guint attribute((unused)) callback_action,
1549 GtkWidget attribute((unused)) *menu_item) {
1550 /* If the window already exists, raise it */
1551 if(playlist_window) {
1552 gtk_window_present(GTK_WINDOW(playlist_window));
1555 /* Create the window */
1556 playlist_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1557 gtk_widget_set_style(playlist_window, tool_style);
1558 g_signal_connect(playlist_window, "destroy",
1559 G_CALLBACK(playlist_window_destroyed), &playlist_window);
1560 gtk_window_set_title(GTK_WINDOW(playlist_window), "Playlists Management");
1561 /* TODO loads of this is very similar to (copied from!) users.c - can we
1563 /* Keyboard shortcuts */
1564 g_signal_connect(playlist_window, "key-press-event",
1565 G_CALLBACK(playlist_window_keypress), 0);
1566 /* default size is too small */
1567 gtk_window_set_default_size(GTK_WINDOW(playlist_window), 640, 320);
1569 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
1570 gtk_box_pack_start(GTK_BOX(hbox), playlist_picker_create(),
1571 FALSE/*expand*/, FALSE, 0);
1572 gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(),
1573 FALSE/*expand*/, FALSE, 2);
1574 gtk_box_pack_start(GTK_BOX(hbox), playlists_editor_create(),
1575 TRUE/*expand*/, TRUE/*fill*/, 0);
1577 gtk_container_add(GTK_CONTAINER(playlist_window), frame_widget(hbox, NULL));
1578 gtk_widget_show_all(playlist_window);
1581 /** @brief Keypress handler */
1582 static gboolean playlist_window_keypress(GtkWidget attribute((unused)) *widget,
1584 gpointer attribute((unused)) user_data) {
1587 switch(event->keyval) {
1589 gtk_widget_destroy(playlist_window);
1596 /** @brief Called when the playlist window is destroyed */
1597 static void playlist_window_destroyed(GtkWidget attribute((unused)) *widget,
1598 GtkWidget **widget_pointer) {
1599 destroy_queuelike(&ql_playlist);
1600 playlist_picker_destroy();
1601 *widget_pointer = NULL;
1604 /** @brief Initialize playlist support */
1605 void playlists_init(void) {
1606 /* We re-get all playlists upon any change... */
1607 event_register("playlist-created", playlist_list_update, 0);
1608 event_register("playlist-deleted", playlist_list_update, 0);
1609 /* ...and on reconnection */
1610 event_register("log-connected", playlist_list_update, 0);
1611 /* ...and from time to time */
1612 event_register("periodic-slow", playlist_list_update, 0);
1613 /* ...and at startup */
1614 playlist_list_update(0, 0, 0);
1616 /* Update the playlists menu when the set of playlists changes */
1617 event_register("playlists-updated", playlist_menu_changed, 0);
1618 /* Update the new-playlist OK button when the set of playlists changes */
1619 event_register("playlists-updated", playlist_new_changed, 0);
1620 /* Update the list of playlists in the edit window when the set changes */
1621 event_register("playlists-updated", playlist_picker_fill, 0);
1622 /* Update the displayed playlist when it is modified */
1623 event_register("playlist-modified", playlist_editor_fill, 0);
1624 /* Update the shared/public/etc buttons when a playlist is modified */
1625 event_register("playlist-modified", playlist_editor_set_buttons, 0);
1633 indent-tabs-mode:nil