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