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