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