chiark / gitweb /
e1bd34616c6d091cbd63f5737e339603135c0b41
[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 for Disobedience
22  *
23  * The playlists management window contains:
24  * - a list of all playlists
25  * - an add button
26  * - a delete button
27  * - a drag+drop capable view of the playlist
28  * - a close button
29  */
30 #include "disobedience.h"
31 #include "queue-generic.h"
32 #include "popup.h"
33 #include "validity.h"
34
35 #if PLAYLISTS
36
37 static void playlists_updated(void *v,
38                               const char *err,
39                               int nvec, char **vec);
40 static void playlists_fill_tracks(const char *event,
41                                   void *eventdata,
42                                   void *callbackdata);
43
44 /** @brief Playlist editing window */
45 static GtkWidget *playlists_window;
46
47 /** @brief Tree model for list of playlists */
48 static GtkListStore *playlists_list;
49
50 /** @brief Selection for list of playlists */
51 static GtkTreeSelection *playlists_selection;
52
53 /** @brief Currently selected playlist */
54 static const char *playlists_selected;
55
56 /** @brief Delete button */
57 static GtkWidget *playlists_delete_button;
58
59 /** @brief Current list of playlists or NULL */
60 char **playlists;
61
62 /** @brief Count of playlists */
63 int nplaylists;
64
65 /** @brief Columns for the playlist editor */
66 static const struct queue_column playlist_columns[] = {
67   { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
68   { "Album",  column_namepart, "album",  COL_EXPAND|COL_ELLIPSIZE },
69   { "Title",  column_namepart, "title",  COL_EXPAND|COL_ELLIPSIZE },
70   { "Length", column_length,   0,        COL_RIGHT }
71 };
72
73 /** @brief Pop-up menu for playlist editor */
74 // TODO some of these may not be generic enough yet - check!
75 static struct menuitem playlist_menuitems[] = {
76   { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
77   { "Play track", ql_play_activate, ql_play_sensitive, 0, 0 },
78   //{ "Play playlist", ql_playall_activate, ql_playall_sensitive, 0, 0 },
79   { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
80   { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
81   { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
82 };
83
84 /** @brief Queuelike for editing a playlist */
85 static struct queuelike ql_playlist = {
86   .name = "playlist",
87   .columns = playlist_columns,
88   .ncolumns = sizeof playlist_columns / sizeof *playlist_columns,
89   .menuitems = playlist_menuitems,
90   .nmenuitems = sizeof playlist_menuitems / sizeof *playlist_menuitems,
91   //.drop = playlist_drop  //TODO
92 };
93
94 /* Maintaining the list of playlists ---------------------------------------- */
95
96 /** @brief Schedule an update to the list of playlists */
97 static void playlists_update(const char attribute((unused)) *event,
98                              void attribute((unused)) *eventdata,
99                              void attribute((unused)) *callbackdata) {
100   disorder_eclient_playlists(client, playlists_updated, 0);
101 }
102
103 /** @brief qsort() callback for playlist name comparison */
104 static int playlistcmp(const void *ap, const void *bp) {
105   const char *a = *(char **)ap, *b = *(char **)bp;
106   const char *ad = strchr(a, '.'), *bd = strchr(b, '.');
107   int c;
108
109   /* Group owned playlists by owner */
110   if(ad && bd) {
111     const int adn = ad - a, bdn = bd - b;
112     if((c = strncmp(a, b, adn < bdn ? adn : bdn)))
113       return c;
114     /* Lexical order within playlists of a single owner */
115     return strcmp(ad + 1, bd + 1);
116   }
117
118   /* Owned playlists after shared ones */
119   if(ad) {
120     return 1;
121   } else if(bd) {
122     return -1;
123   }
124
125   /* Lexical order of shared playlists */
126   return strcmp(a, b);
127 }
128
129 /** @brief Called with a new list of playlists */
130 static void playlists_updated(void attribute((unused)) *v,
131                               const char *err,
132                               int nvec, char **vec) {
133   if(err) {
134     playlists = 0;
135     nplaylists = -1;
136     /* Probably means server does not support playlists */
137   } else {
138     playlists = vec;
139     nplaylists = nvec;
140     qsort(playlists, nplaylists, sizeof (char *), playlistcmp);
141   }
142   /* Tell our consumers */
143   event_raise("playlists-updated", 0);
144 }
145
146 /* Playlists menu ----------------------------------------------------------- */
147
148 /** @brief Play received playlist contents
149  *
150  * Passed as a completion callback by menu_activate_playlist().
151  */
152 static void playlist_play_content(void attribute((unused)) *v,
153                                   const char *err,
154                                   int nvec, char **vec) {
155   if(err) {
156     popup_protocol_error(0, err);
157     return;
158   }
159   for(int n = 0; n < nvec; ++n)
160     disorder_eclient_play(client, vec[n], NULL, NULL);
161 }
162
163 /** @brief Called to activate a playlist
164  *
165  * Called when the menu item for a playlist is clicked.
166  */
167 static void menu_activate_playlist(GtkMenuItem *menuitem,
168                                    gpointer attribute((unused)) user_data) {
169   GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
170   const char *playlist = gtk_label_get_text(label);
171
172   disorder_eclient_playlist_get(client, playlist_play_content, playlist, NULL);
173 }
174
175 /** @brief Called when the playlists change */
176 static void menu_playlists_changed(const char attribute((unused)) *event,
177                                    void attribute((unused)) *eventdata,
178                                    void attribute((unused)) *callbackdata) {
179   if(!playlists_menu)
180     return;                             /* OMG too soon */
181   GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
182   /* TODO: we could be more sophisticated and only insert/remove widgets as
183    * needed.  The current logic trashes the selection which is not acceptable
184    * and interacts badly with one playlist being currently locked and
185    * edited. */
186   while(menu->children)
187     gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
188   /* NB nplaylists can be -1 as well as 0 */
189   for(int n = 0; n < nplaylists; ++n) {
190     GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]);
191     g_signal_connect(w, "activate", G_CALLBACK(menu_activate_playlist), 0);
192     gtk_widget_show(w);
193     gtk_menu_shell_append(menu, w);
194   }
195   gtk_widget_set_sensitive(menu_playlists_widget,
196                            nplaylists > 0);
197   gtk_widget_set_sensitive(menu_editplaylists_widget,
198                            nplaylists >= 0);
199 }
200
201 /* Popup to create a new playlist ------------------------------------------- */
202
203 /** @brief New-playlist popup */
204 static GtkWidget *playlist_new_window;
205
206 /** @brief Text entry in new-playlist popup */
207 static GtkWidget *playlist_new_entry;
208
209 static GtkWidget *playlist_new_info;
210
211 static GtkWidget *playlist_new_shared;
212 static GtkWidget *playlist_new_public;
213 static GtkWidget *playlist_new_private;
214
215 /** @brief Get entered new-playlist details */
216 static void playlist_new_details(char **namep,
217                                  char **fullnamep,
218                                  gboolean *sharedp,
219                                  gboolean *publicp,
220                                  gboolean *privatep) {
221   gboolean shared, public, private;
222   g_object_get(playlist_new_shared, "active", &shared, (char *)NULL);
223   g_object_get(playlist_new_public, "active", &public, (char *)NULL);
224   g_object_get(playlist_new_private, "active", &private, (char *)NULL);
225   char *gname = gtk_editable_get_chars(GTK_EDITABLE(playlist_new_entry),
226                                        0, -1); /* name owned by calle */
227   char *name = xstrdup(gname);
228   g_free(gname);
229   if(sharedp) *sharedp = shared;
230   if(publicp) *publicp = public;
231   if(privatep) *privatep = private;
232   if(namep) *namep = name;
233   if(fullnamep) {
234     if(*sharedp) *fullnamep = *namep;
235     else byte_xasprintf(fullnamep, "%s.%s", config->username, name);
236   }
237 }
238
239 /** @brief Called when the newly created playlist has unlocked */
240 static void playlist_new_unlocked(void attribute((unused)) *v, const char *err) {
241   if(err)
242     popup_protocol_error(0, err);
243   gtk_widget_destroy(playlist_new_window);
244 }
245
246 /** @brief Called when the new playlist has been created */
247 static void playlist_new_created(void attribute((unused)) *v, const char *err) {
248   if(err) {
249     popup_protocol_error(0, err);
250     return;
251   }
252   disorder_eclient_playlist_unlock(client, playlist_new_unlocked, NULL);
253   // TODO arrange for the new playlist to be selected
254 }
255
256 /** @brief Called when the proposed new playlist's contents have been retrieved
257  *
258  * ...or rather, normally, when it's been reported that it does not exist.
259  */
260 static void playlist_new_retrieved(void *v, const char *err,
261                                    int nvec,
262                                    char attribute((unused)) **vec) {
263   char *fullname = v;
264   if(!err && nvec != -1)
265     /* A rare case but not in principle impossible */
266     err = "A playlist with that name already exists.";
267   if(err) {
268     popup_protocol_error(0, err);
269     disorder_eclient_playlist_unlock(client, playlist_new_unlocked, fullname);
270     return;
271   }
272   gboolean shared, public, private;
273   playlist_new_details(0, 0, &shared, &public, &private);
274   disorder_eclient_playlist_set_share(client, playlist_new_created, fullname,
275                                       public ? "public"
276                                       : private ? "private"
277                                       : "shared",
278                                       fullname);
279 }
280
281 /** @brief Called when the proposed new playlist has been locked */
282 static void playlist_new_locked(void *v, const char *err) {
283   char *fullname = v;
284   if(err) {
285     popup_protocol_error(0, err);
286     return;
287   }
288   disorder_eclient_playlist_get(client, playlist_new_retrieved,
289                                 fullname, fullname);
290 }
291
292 /** @brief Called when 'ok' is clicked in new-playlist popup */
293 static void playlist_new_ok(GtkButton attribute((unused)) *button,
294                             gpointer attribute((unused)) userdata) {
295   gboolean shared, public, private;
296   char *name, *fullname;
297   playlist_new_details(&name, &fullname, &shared, &public, &private);
298
299   /* We need to:
300    * - lock the playlist
301    * - check it doesn't exist
302    * - set sharing (which will create it empty
303    * - unlock it
304    *
305    * TODO we should freeze the window while this is going on
306    */
307   disorder_eclient_playlist_lock(client, playlist_new_locked, fullname,
308                                  fullname);
309 }
310
311 /** @brief Called when 'cancel' is clicked in new-playlist popup */
312 static void playlist_new_cancel(GtkButton attribute((unused)) *button,
313                                 gpointer attribute((unused)) userdata) {
314   gtk_widget_destroy(playlist_new_window);
315 }
316
317 /** @brief Buttons for new-playlist popup */
318 static struct button playlist_new_buttons[] = {
319   {
320     .stock = GTK_STOCK_OK,
321     .clicked = playlist_new_ok,
322     .tip = "Create new playlist"
323   },
324   {
325     .stock = GTK_STOCK_CANCEL,
326     .clicked = playlist_new_cancel,
327     .tip = "Do not create new playlist"
328   }
329 };
330 #define NPLAYLIST_NEW_BUTTONS (sizeof playlist_new_buttons / sizeof *playlist_new_buttons)
331
332 /** @brief Test whether the new-playlist window settings are valid
333  * @return NULL on success or an error string if not
334  */
335 static const char *playlist_new_valid(void) {
336   gboolean shared, public, private;
337   char *name, *fullname;
338   playlist_new_details(&name, &fullname, &shared, &public, &private);
339   if(!(shared || public || private))
340     return "No type set.";
341   if(!*name)
342     return "";
343   /* See if the result is valid */
344   if(!valid_username(name)
345      || playlist_parse_name(fullname, NULL, NULL))
346     return "Not a valid playlist name.";
347   /* See if the result clashes with an existing name */
348   for(int n = 0; n < nplaylists; ++n)
349     if(!strcmp(playlists[n], fullname)) {
350       if(shared)
351         return "A shared playlist with that name already exists.";
352       else
353         return "You already have a playlist with that name.";
354     }
355   /* As far as we can tell creation would work */
356   return NULL;
357 }
358
359 /** @brief Called to update new playlist window state
360  *
361  * This is called whenever one the text entry or radio buttons changed, and
362  * also when the set of known playlists changes.  It determines whether the new
363  * playlist would be creatable and sets the sensitivity of the OK button
364  * and info display accordingly.
365  */
366 static void playlist_new_changed(const char attribute((unused)) *event,
367                                  void attribute((unused)) *eventdata,
368                                  void attribute((unused)) *callbackdata) {
369   if(!playlist_new_window)
370     return;
371   const char *reason = playlist_new_valid();
372   gtk_widget_set_sensitive(playlist_new_buttons[0].widget,
373                            !reason);
374   gtk_label_set_text(GTK_LABEL(playlist_new_info), reason);
375 }
376
377 /** @brief Called when some radio button in the new-playlist popup changes */
378 static void playlist_new_button_toggled(GtkToggleButton attribute((unused)) tb,
379                                         gpointer attribute((unused)) userdata) {
380   playlist_new_changed(0,0,0);
381 }
382
383 /** @brief Called when the text entry field in the new-playlist popup changes */
384 static void playlist_new_entry_edited(GtkEditable attribute((unused)) *editable,
385                                       gpointer attribute((unused)) user_data) {
386   playlist_new_changed(0,0,0);
387 }
388
389 /** @brief Pop up a new window to enter the playlist name and details */
390 static void playlist_new(void) {
391   assert(playlist_new_window == NULL);
392   playlist_new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
393   g_signal_connect(playlist_new_window, "destroy",
394                    G_CALLBACK(gtk_widget_destroyed), &playlist_new_window);
395   gtk_window_set_title(GTK_WINDOW(playlist_new_window), "Create new playlist");
396   /* Window will be modal, suppressing access to other windows */
397   gtk_window_set_modal(GTK_WINDOW(playlist_new_window), TRUE);
398   gtk_window_set_transient_for(GTK_WINDOW(playlist_new_window),
399                                GTK_WINDOW(playlists_window));
400
401   /* Window contents will use a table (grid) layout */
402   GtkWidget *table = gtk_table_new(3, 3, FALSE/*!homogeneous*/);
403
404   /* First row: playlist name */
405   gtk_table_attach_defaults(GTK_TABLE(table),
406                             gtk_label_new("Playlist name"),
407                             0, 1, 0, 1);
408   playlist_new_entry = gtk_entry_new();
409   g_signal_connect(playlist_new_entry, "changed",
410                    G_CALLBACK(playlist_new_entry_edited), NULL);
411   gtk_table_attach_defaults(GTK_TABLE(table),
412                             playlist_new_entry,
413                             1, 3, 0, 1);
414
415   /* Second row: radio buttons to choose type */
416   playlist_new_shared = gtk_radio_button_new_with_label(NULL, "shared");
417   playlist_new_public
418     = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared),
419                                                   "public");
420   playlist_new_private
421     = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(playlist_new_shared),
422                                                   "private");
423   g_signal_connect(playlist_new_shared, "toggled",
424                    G_CALLBACK(playlist_new_button_toggled), NULL);
425   g_signal_connect(playlist_new_public, "toggled",
426                    G_CALLBACK(playlist_new_button_toggled), NULL);
427   g_signal_connect(playlist_new_private, "toggled",
428                    G_CALLBACK(playlist_new_button_toggled), NULL);
429   gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_shared, 0, 1, 1, 2);
430   gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_public, 1, 2, 1, 2);
431   gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_private, 2, 3, 1, 2);
432
433   /* Third row: info bar saying why not */
434   playlist_new_info = gtk_label_new("");
435   gtk_table_attach_defaults(GTK_TABLE(table), playlist_new_info,
436                             0, 3, 2, 3);
437
438   /* Fourth row: ok/cancel buttons */
439   GtkWidget *hbox = create_buttons_box(playlist_new_buttons,
440                                        NPLAYLIST_NEW_BUTTONS,
441                                        gtk_hbox_new(FALSE, 0));
442   gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 3, 3, 4);
443
444   gtk_container_add(GTK_CONTAINER(playlist_new_window),
445                     frame_widget(table, NULL));
446
447   /* Set initial state of OK button */
448   playlist_new_changed(0,0,0);
449
450   /* TODO: return should = OK, escape should = cancel */
451   
452   /* Display the window */
453   gtk_widget_show_all(playlist_new_window);
454 }
455
456 /* Playlists window (list of playlists) ------------------------------------- */
457
458 /** @brief (Re-)populate the playlist tree model */
459 static void playlists_fill(const char attribute((unused)) *event,
460                            void attribute((unused)) *eventdata,
461                            void attribute((unused)) *callbackdata) {
462   GtkTreeIter iter[1];
463
464   if(!playlists_window)
465     return;
466   if(!playlists_list)
467     playlists_list = gtk_list_store_new(1, G_TYPE_STRING);
468   const char *was_selected = playlists_selected;
469   gtk_list_store_clear(playlists_list); /* clears playlists_selected */
470   for(int n = 0; n < nplaylists; ++n) {
471     gtk_list_store_insert_with_values(playlists_list, iter, n/*position*/,
472                                       0, playlists[n],        /* column 0 */
473                                       -1);                    /* no more cols */
474     /* Reselect the selected playlist */
475     if(was_selected && !strcmp(was_selected, playlists[n]))
476       gtk_tree_selection_select_iter(playlists_selection, iter);
477   }
478 }
479
480 /** @brief Called when the selection might have changed */
481 static void playlists_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
482                                         gpointer attribute((unused)) user_data) {
483   GtkTreeIter iter;
484   char *gselected, *selected;
485   
486   /* Identify the current selection */
487   if(gtk_tree_selection_get_selected(playlists_selection, 0, &iter)) {
488     gtk_tree_model_get(GTK_TREE_MODEL(playlists_list), &iter,
489                        0, &gselected, -1);
490     selected = xstrdup(gselected);
491     g_free(gselected);
492   } else
493     selected = 0;
494   /* Set button sensitivity according to the new state */
495   if(selected)
496     gtk_widget_set_sensitive(playlists_delete_button, 1);
497   else
498     gtk_widget_set_sensitive(playlists_delete_button, 0);
499   /* Eliminate no-change cases */
500   if(!selected && !playlists_selected)
501     return;
502   if(selected && playlists_selected && !strcmp(selected, playlists_selected))
503     return;
504   /* Record the new state */
505   playlists_selected = selected;
506   /* Re-initalize the queue */
507   ql_new_queue(&ql_playlist, NULL);
508   playlists_fill_tracks(NULL, (void *)playlists_selected, NULL);
509 }
510
511 /** @brief Called when the 'add' button is pressed */
512 static void playlists_add(GtkButton attribute((unused)) *button,
513                           gpointer attribute((unused)) userdata) {
514   /* Unselect whatever is selected TODO why?? */
515   gtk_tree_selection_unselect_all(playlists_selection);
516   playlist_new();
517 }
518
519 /** @brief Called when playlist deletion completes */
520 static void playlists_delete_completed(void attribute((unused)) *v,
521                                        const char *err) {
522   if(err)
523     popup_protocol_error(0, err);
524 }
525
526 /** @brief Called when the 'Delete' button is pressed */
527 static void playlists_delete(GtkButton attribute((unused)) *button,
528                              gpointer attribute((unused)) userdata) {
529   GtkWidget *yesno;
530   int res;
531
532   fprintf(stderr, "playlists_delete\n");
533   if(!playlists_selected)
534     return;                             /* shouldn't happen */
535   yesno = gtk_message_dialog_new(GTK_WINDOW(playlists_window),
536                                  GTK_DIALOG_MODAL,
537                                  GTK_MESSAGE_QUESTION,
538                                  GTK_BUTTONS_YES_NO,
539                                  "Do you really want to delete playlist %s?"
540                                  " This action cannot be undone.",
541                                  playlists_selected);
542   res = gtk_dialog_run(GTK_DIALOG(yesno));
543   gtk_widget_destroy(yesno);
544   if(res == GTK_RESPONSE_YES) {
545     disorder_eclient_playlist_delete(client,
546                                      playlists_delete_completed,
547                                      playlists_selected,
548                                      NULL);
549   }
550 }
551
552 /** @brief Table of buttons below the playlist list */
553 static struct button playlists_buttons[] = {
554   {
555     GTK_STOCK_ADD,
556     playlists_add,
557     "Create a new playlist",
558     0
559   },
560   {
561     GTK_STOCK_REMOVE,
562     playlists_delete,
563     "Delete a playlist",
564     0
565   },
566 };
567 #define NPLAYLISTS_BUTTONS (sizeof playlists_buttons / sizeof *playlists_buttons)
568
569 /** @brief Create the list of playlists for the edit playlists window */
570 static GtkWidget *playlists_window_list(void) {
571   /* Create the list of playlist and populate it */
572   playlists_fill(NULL, NULL, NULL);
573   /* Create the tree view */
574   GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlists_list));
575   /* ...and the renderers for it */
576   GtkCellRenderer *cr = gtk_cell_renderer_text_new();
577   GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("Playlist",
578                                                                     cr,
579                                                                     "text", 0,
580                                                                     NULL);
581   gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
582   /* Get the selection for the view; set its mode; arrange for a callback when
583    * it changes */
584   playlists_selected = NULL;
585   playlists_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
586   gtk_tree_selection_set_mode(playlists_selection, GTK_SELECTION_BROWSE);
587   g_signal_connect(playlists_selection, "changed",
588                    G_CALLBACK(playlists_selection_changed), NULL);
589
590   /* Create the control buttons */
591   GtkWidget *buttons = create_buttons_box(playlists_buttons,
592                                           NPLAYLISTS_BUTTONS,
593                                           gtk_hbox_new(FALSE, 1));
594   playlists_delete_button = playlists_buttons[1].widget;
595
596   playlists_selection_changed(NULL, NULL);
597
598   /* Buttons live below the list */
599   GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
600   gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
601   gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
602
603   return vbox;
604 }
605
606 /* Playlists window (edit current playlist) --------------------------------- */
607
608 /** @brief Called with new tracks for the playlist */
609 static void playlists_got_new_tracks(void attribute((unused)) *v,
610                                      const char *err,
611                                      int nvec, char **vec) {
612   fprintf(stderr, "playlists_got_new_tracks\n");
613   if(err) {
614     popup_protocol_error(0, err);
615     return;
616   }
617   if(nvec == -1)
618     /* No such playlist, presumably we'll get a deleted event shortly */
619     return;
620   /* Translate the list of tracks into queue entries */
621   struct queue_entry *newq, **qq = &newq;
622   hash *h = hash_new(sizeof(int));
623   for(int n = 0; n < nvec; ++n) {
624     struct queue_entry *q = xmalloc(sizeof *q);
625     q->track = vec[n];
626     /* Synthesize a unique ID so that the selection survives updates.  Tracks
627      * can appear more than once in the queue so we can't use raw track names,
628      * so we add a serial number to the start. */
629     /* TODO but this doesn't work for some reason */
630     int *serialp = hash_find(h, vec[n]), serial = serialp ? *serialp : 0;
631     byte_xasprintf((char **)&q->id, "%d-%s", serial++, vec[n]);
632     fprintf(stderr, "%s\n", q->id);
633     hash_add(h, vec[0], &serial, HASH_INSERT_OR_REPLACE);
634     *qq = q;
635     qq = &q->next;
636   }
637   *qq = NULL;
638   fprintf(stderr, "calling ql_new_queue\n");
639   ql_new_queue(&ql_playlist, newq);
640   fprintf(stderr, "back form ql_new_queue\n");
641 }
642
643 /** @brief (Re-)populate the playlist tree model */
644 static void playlists_fill_tracks(const char attribute((unused)) *event,
645                                   void *eventdata,
646                                   void attribute((unused)) *callbackdata) {
647   const char *modified_playlist = eventdata;
648   fprintf(stderr, "playlists_fill_tracks: %s\n", modified_playlist);
649   if(!playlists_window)
650     return;
651   if(!playlists_selected)
652     return;
653   if(!strcmp(playlists_selected, modified_playlist))
654     disorder_eclient_playlist_get(client, playlists_got_new_tracks,
655                                   playlists_selected, NULL);
656 }
657
658 static GtkWidget *playlists_window_edit(void) {
659   assert(ql_playlist.view == NULL);     /* better not be set up already */
660   GtkWidget *w = init_queuelike(&ql_playlist);
661   /* Initially empty */
662   return w;
663 }
664
665 /* Playlists window --------------------------------------------------------- */
666
667 /** @brief Keypress handler */
668 static gboolean playlists_keypress(GtkWidget attribute((unused)) *widget,
669                                    GdkEventKey *event,
670                                    gpointer attribute((unused)) user_data) {
671   if(event->state)
672     return FALSE;
673   switch(event->keyval) {
674   case GDK_Escape:
675     gtk_widget_destroy(playlists_window);
676     return TRUE;
677   default:
678     return FALSE;
679   }
680 }
681
682 /** @brief Called when the playlist window is destroyed */
683 static void playlists_window_destroyed(GtkWidget attribute((unused)) *widget,
684                                        GtkWidget **widget_pointer) {
685   destroy_queuelike(&ql_playlist);
686   *widget_pointer = NULL;
687 }
688
689 /** @brief Pop up the playlists window
690  *
691  * Called when the playlists menu item is selected
692  */
693 void edit_playlists(gpointer attribute((unused)) callback_data,
694                     guint attribute((unused)) callback_action,
695                     GtkWidget attribute((unused)) *menu_item) {
696   /* If the window already exists, raise it */
697   if(playlists_window) {
698     gtk_window_present(GTK_WINDOW(playlists_window));
699     return;
700   }
701   /* Create the window */
702   playlists_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
703   gtk_widget_set_style(playlists_window, tool_style);
704   g_signal_connect(playlists_window, "destroy",
705                    G_CALLBACK(playlists_window_destroyed), &playlists_window);
706   gtk_window_set_title(GTK_WINDOW(playlists_window), "Playlists Management");
707   /* TODO loads of this is very similar to (copied from!) users.c - can we
708    * de-dupe? */
709   /* Keyboard shortcuts */
710   g_signal_connect(playlists_window, "key-press-event",
711                    G_CALLBACK(playlists_keypress), 0);
712   /* default size is too small */
713   gtk_window_set_default_size(GTK_WINDOW(playlists_window), 512, 240);
714
715   GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
716   gtk_box_pack_start(GTK_BOX(hbox), playlists_window_list(),
717                      FALSE/*expand*/, FALSE, 0);
718   gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(),
719                      FALSE/*expand*/, FALSE, 2);
720   gtk_box_pack_start(GTK_BOX(hbox), playlists_window_edit(),
721                      TRUE/*expand*/, TRUE/*fill*/, 0);
722
723   gtk_container_add(GTK_CONTAINER(playlists_window), frame_widget(hbox, NULL));
724   gtk_widget_show_all(playlists_window);
725 }
726
727 /** @brief Initialize playlist support */
728 void playlists_init(void) {
729   /* We re-get all playlists upon any change... */
730   event_register("playlist-created", playlists_update, 0);
731   event_register("playlist-modified", playlists_update, 0);
732   event_register("playlist-deleted", playlists_update, 0);
733   /* ...and on reconnection */
734   event_register("log-connected", playlists_update, 0);
735   /* ...and from time to time */
736   event_register("periodic-slow", playlists_update, 0);
737   /* ...and at startup */
738
739   /* Update the playlists menu when the set of playlists changes */
740   event_register("playlists-updated", menu_playlists_changed, 0);
741   /* Update the new-playlist OK button when the set of playlists changes */
742   event_register("playlists-updated", playlist_new_changed, 0);
743   /* Update the list of playlists in the edit window when the set changes */
744   event_register("playlists-updated", playlists_fill, 0);
745   /* Update the displayed playlist when it is modified */
746   event_register("playlist-modified", playlists_fill_tracks, 0);
747   playlists_update(0, 0, 0);
748 }
749
750 #endif
751
752 /*
753 Local Variables:
754 c-basic-offset:2
755 comment-column:40
756 fill-column:79
757 indent-tabs-mode:nil
758 End:
759 */