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