chiark / gitweb /
Typos, comments, etc.
[disorder] / disobedience / playlists.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2008 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
32 #if PLAYLISTS
33
34 static void playlists_updated(void *v,
35                               const char *err,
36                               int nvec, char **vec);
37
38 /** @brief Playlist editing window */
39 static GtkWidget *playlists_window;
40
41 /** @brief Tree model for list of playlists */
42 static GtkListStore *playlists_list;
43
44 /** @brief Selection for list of playlists */
45 static GtkTreeSelection *playlists_selection;
46
47 /** @brief Currently selected playlist */
48 static const char *playlists_selected;
49
50 /** @brief Delete button */
51 static GtkWidget *playlists_delete_button;
52
53 /** @brief Current list of playlists or NULL */
54 char **playlists;
55
56 /** @brief Count of playlists */
57 int nplaylists;
58
59 /** @brief Schedule an update to the list of playlists */
60 static void playlists_update(const char attribute((unused)) *event,
61                              void attribute((unused)) *eventdata,
62                              void attribute((unused)) *callbackdata) {
63   disorder_eclient_playlists(client, playlists_updated, 0);
64 }
65
66 /** @brief qsort() callback for playlist name comparison */
67 static int playlistcmp(const void *ap, const void *bp) {
68   const char *a = *(char **)ap, *b = *(char **)bp;
69   const char *ad = strchr(a, '.'), *bd = strchr(b, '.');
70   int c;
71
72   /* Group owned playlists by owner */
73   if(ad && bd) {
74     const int adn = ad - a, bdn = bd - b;
75     if((c = strncmp(a, b, adn < bdn ? adn : bdn)))
76       return c;
77     /* Lexical order within playlists of a single owner */
78     return strcmp(ad + 1, bd + 1);
79   }
80
81   /* Owned playlists after shared ones */
82   if(ad) {
83     return 1;
84   } else if(bd) {
85     return -1;
86   }
87
88   /* Lexical order of shared playlists */
89   return strcmp(a, b);
90 }
91
92 /** @brief Called with a new list of playlists */
93 static void playlists_updated(void attribute((unused)) *v,
94                               const char *err,
95                               int nvec, char **vec) {
96   if(err) {
97     playlists = 0;
98     nplaylists = -1;
99     /* Probably means server does not support playlists */
100   } else {
101     playlists = vec;
102     nplaylists = nvec;
103     qsort(playlists, nplaylists, sizeof (char *), playlistcmp);
104   }
105   /* Tell our consumers */
106   event_raise("playlists-updated", 0);
107 }
108
109 /** @brief Called to activate a playlist */
110 static void menu_activate_playlist(GtkMenuItem *menuitem,
111                                    gpointer attribute((unused)) user_data) {
112   GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
113   const char *playlist = gtk_label_get_text(label);
114
115   fprintf(stderr, "activate playlist %s\n", playlist); /* TODO */
116   /* We need to:
117    * - unlock any currently locked playlist
118    * - throw away the current editing widget
119    * - create a new editing widget
120    * - populate it
121    */
122 }
123
124 /** @brief Called when the playlists change */
125 static void menu_playlists_changed(const char attribute((unused)) *event,
126                                    void attribute((unused)) *eventdata,
127                                    void attribute((unused)) *callbackdata) {
128   if(!playlists_menu)
129     return;                             /* OMG too soon */
130   GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
131   /* TODO: we could be more sophisticated and only insert/remove widgets as
132    * needed.  The current logic trashes the selection which is not acceptable
133    * and interacts badly with one playlist being currently locked and
134    * edited. */
135   while(menu->children)
136     gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
137   /* NB nplaylists can be -1 as well as 0 */
138   for(int n = 0; n < nplaylists; ++n) {
139     GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]);
140     g_signal_connect(w, "activate", G_CALLBACK(menu_activate_playlist), 0);
141     gtk_widget_show(w);
142     gtk_menu_shell_append(menu, w);
143   }
144   gtk_widget_set_sensitive(playlists_widget,
145                            nplaylists > 0);
146   gtk_widget_set_sensitive(editplaylists_widget,
147                            nplaylists >= 0);
148 }
149
150 /** @brief (Re-)populate the playlist tree model */
151 static void playlists_fill(void) {
152   GtkTreeIter iter[1];
153
154   if(!playlists_list)
155     playlists_list = gtk_list_store_new(1, G_TYPE_STRING);
156   gtk_list_store_clear(playlists_list);
157   for(int n = 0; n < nplaylists; ++n)
158     gtk_list_store_insert_with_values(playlists_list, iter, n/*position*/,
159                                       0, playlists[n],        /* column 0 */
160                                       -1);                    /* no more cols */
161   // TODO reselect whatever was formerly selected if possible, if not then
162   // zap the contents view
163 }
164
165 /** @brief Called when the selection might have changed */
166 static void playlists_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
167                                         gpointer attribute((unused)) user_data) {
168   GtkTreeIter iter;
169   char *gselected, *selected;
170   
171   /* Identify the current selection */
172   if(gtk_tree_selection_get_selected(playlists_selection, 0, &iter)) {
173     gtk_tree_model_get(GTK_TREE_MODEL(playlists_list), &iter,
174                        0, &gselected, -1);
175     selected = xstrdup(gselected);
176     g_free(gselected);
177   } else
178     selected = 0;
179   /* Eliminate no-change cases */
180   if(!selected && !playlists_selected)
181     return;
182   if(selected && playlists_selected && !strcmp(selected, playlists_selected))
183     return;
184   /* There's been a change */
185   playlists_selected = selected;
186   if(playlists_selected) {
187     fprintf(stderr, "playlists selection changed\n'"); /* TODO */
188     gtk_widget_set_sensitive(playlists_delete_button, 1);
189   } else
190     gtk_widget_set_sensitive(playlists_delete_button, 0);
191 }
192
193 /** @brief Called when the 'add' button is pressed */
194 static void playlists_add(GtkButton attribute((unused)) *button,
195                           gpointer attribute((unused)) userdata) {
196   /* Unselect whatever is selected */
197   gtk_tree_selection_unselect_all(playlists_selection);
198   fprintf(stderr, "playlists_add\n");/* TODO */
199   /* We need to pop up a window asking for:
200    * - the name for the playlist
201    * - whether it is to be a public, private or shared playlist
202    * Moreover we should keep track of the known playlists and grey out OK
203    * if the name is a clash (as well as if it's actually invalid).
204    */
205 }
206
207 /** @brief Called when the 'Delete' button is pressed */
208 static void playlists_delete(GtkButton attribute((unused)) *button,
209                              gpointer attribute((unused)) userdata) {
210   GtkWidget *yesno;
211   int res;
212
213   if(!playlists_selected)
214     return;                             /* shouldn't happen */
215   yesno = gtk_message_dialog_new(GTK_WINDOW(playlists_window),
216                                  GTK_DIALOG_MODAL,
217                                  GTK_MESSAGE_QUESTION,
218                                  GTK_BUTTONS_YES_NO,
219                                  "Do you really want to delete playlist %s?"
220                                  " This action cannot be undone.",
221                                  playlists_selected);
222   res = gtk_dialog_run(GTK_DIALOG(yesno));
223   gtk_widget_destroy(yesno);
224   if(res == GTK_RESPONSE_YES) {
225     disorder_eclient_playlist_delete(client,
226                                      NULL/*playlists_delete_completed*/,
227                                      playlists_selected,
228                                      NULL);
229   }
230 }
231
232 /** @brief Table of buttons below the playlist list */
233 static struct button playlists_buttons[] = {
234   {
235     GTK_STOCK_ADD,
236     playlists_add,
237     "Create a new playlist",
238     0
239   },
240   {
241     GTK_STOCK_REMOVE,
242     playlists_delete,
243     "Delete a playlist",
244     0
245   },
246 };
247 #define NPLAYLISTS_BUTTONS (sizeof playlists_buttons / sizeof *playlists_buttons)
248
249 /** @brief Keypress handler */
250 static gboolean playlists_keypress(GtkWidget attribute((unused)) *widget,
251                                    GdkEventKey *event,
252                                    gpointer attribute((unused)) user_data) {
253   if(event->state)
254     return FALSE;
255   switch(event->keyval) {
256   case GDK_Escape:
257     gtk_widget_destroy(playlists_window);
258     return TRUE;
259   default:
260     return FALSE;
261   }
262 }
263
264 void edit_playlists(gpointer attribute((unused)) callback_data,
265                      guint attribute((unused)) callback_action,
266                      GtkWidget attribute((unused)) *menu_item) {
267   GtkWidget *tree, *hbox, *vbox, *buttons;
268   GtkCellRenderer *cr;
269   GtkTreeViewColumn *col;
270
271   /* If the window already exists, raise it */
272   if(playlists_window) {
273     gtk_window_present(GTK_WINDOW(playlists_window));
274     return;
275   }
276   /* Create the window */
277   playlists_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
278   gtk_widget_set_style(playlists_window, tool_style);
279   g_signal_connect(playlists_window, "destroy",
280                    G_CALLBACK(gtk_widget_destroyed), &playlists_window);
281   gtk_window_set_title(GTK_WINDOW(playlists_window), "Playlists Management");
282   /* TODO loads of this is very similar to (copied from!) users.c - can we
283    * de-dupe? */
284   /* Keyboard shortcuts */
285   g_signal_connect(playlists_window, "key-press-event",
286                    G_CALLBACK(playlists_keypress), 0);
287   /* default size is too small */
288   gtk_window_set_default_size(GTK_WINDOW(playlists_window), 240, 240);
289   /* Create the list of playlist and populate it */
290   playlists_fill();
291   /* Create the tree view */
292   tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(playlists_list));
293   /* ...and the renderers for it */
294   cr = gtk_cell_renderer_text_new();
295   col = gtk_tree_view_column_new_with_attributes("Playlist",
296                                                  cr,
297                                                  "text", 0,
298                                                  NULL);
299   gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
300   /* Get the selection for the view; set its mode; arrange for a callback when
301    * it changes */
302   playlists_selected = NULL;
303   playlists_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
304   gtk_tree_selection_set_mode(playlists_selection, GTK_SELECTION_BROWSE);
305   g_signal_connect(playlists_selection, "changed",
306                    G_CALLBACK(playlists_selection_changed), NULL);
307
308   /* Create the control buttons */
309   buttons = create_buttons_box(playlists_buttons,
310                                NPLAYLISTS_BUTTONS,
311                                gtk_hbox_new(FALSE, 1));
312   playlists_delete_button = playlists_buttons[1].widget;
313
314   /* Buttons live below the list */
315   vbox = gtk_vbox_new(FALSE, 0);
316   gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
317   gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
318
319   hbox = gtk_hbox_new(FALSE, 0);
320   gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0);
321   gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), FALSE/*expand*/, FALSE, 2);
322   // TODO something to edit the playlist in
323   //gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0);
324   gtk_container_add(GTK_CONTAINER(playlists_window), frame_widget(hbox, NULL));
325   gtk_widget_show_all(playlists_window);
326 }
327
328 /** @brief Initialize playlist support */
329 void playlists_init(void) {
330   /* We re-get all playlists upon any change... */
331   event_register("playlist-created", playlists_update, 0);
332   event_register("playlist-modified", playlists_update, 0);
333   event_register("playlist-deleted", playlists_update, 0);
334   /* ...and on reconnection */
335   event_register("log-connected", playlists_update, 0);
336   /* ...and from time to time */
337   event_register("periodic-slow", playlists_update, 0);
338   /* ...and at startup */
339   event_register("playlists-updated", menu_playlists_changed, 0);
340   playlists_update(0, 0, 0);
341 }
342
343 #endif
344
345 /*
346 Local Variables:
347 c-basic-offset:2
348 comment-column:40
349 fill-column:79
350 indent-tabs-mode:nil
351 End:
352 */