2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/playlists.c
21 * @brief Playlist for Disobedience
24 #include "disobedience.h"
26 static void playlists_updated(void *v
,
28 int nvec
, char **vec
);
30 /** @brief Current list of playlists or NULL */
33 /** @brief Count of playlists */
36 /** @brief Schedule an update to the list of playlists */
37 static void playlists_update(const char attribute((unused
)) *event
,
38 void attribute((unused
)) *eventdata
,
39 void attribute((unused
)) *callbackdata
) {
40 disorder_eclient_playlists(client
, playlists_updated
, 0);
43 /** @brief qsort() callback for playlist name comparison */
44 static int playlistcmp(const void *ap
, const void *bp
) {
45 const char *a
= *(char **)ap
, *b
= *(char **)bp
;
46 const char *ad
= strchr(a
, '.'), *bd
= strchr(b
, '.');
49 /* Group owned playlists by owner */
51 const int adn
= ad
- a
, bdn
= bd
- b
;
52 if((c
= strncmp(a
, b
, adn
< bdn ? adn
: bdn
)))
54 /* Lexical order within playlists of a single owner */
55 return strcmp(ad
+ 1, bd
+ 1);
58 /* Owned playlists after shared ones */
65 /* Lexical order of shared playlists */
69 /** @brief Called with a new list of playlists */
70 static void playlists_updated(void attribute((unused
)) *v
,
72 int nvec
, char **vec
) {
76 /* Probably means server does not support playlists */
80 qsort(playlists
, nplaylists
, sizeof (char *), playlistcmp
);
82 /* Tell our consumers */
83 event_raise("playlists-updated", 0);
86 /** @brief Called to activate a playlist */
87 static void menu_activate_playlist(GtkMenuItem
*menuitem
,
88 gpointer
attribute((unused
)) user_data
) {
89 GtkLabel
*label
= GTK_LABEL(GTK_BIN(menuitem
)->child
);
90 const char *playlist
= gtk_label_get_text(label
);
92 fprintf(stderr
, "activate playlist %s\n", playlist
); /* TODO */
95 /** @brief Called when the playlists change */
96 static void menu_playlists_changed(const char attribute((unused
)) *event
,
97 void attribute((unused
)) *eventdata
,
98 void attribute((unused
)) *callbackdata
) {
100 return; /* OMG too soon */
101 GtkMenuShell
*menu
= GTK_MENU_SHELL(playlists_menu
);
102 /* TODO: we could be more sophisticated and only insert/remove widgets as
103 * needed. For now that's too much effort. */
104 while(menu
->children
)
105 gtk_container_remove(GTK_CONTAINER(menu
), GTK_WIDGET(menu
->children
->data
));
106 /* NB nplaylists can be -1 as well as 0 */
107 for(int n
= 0; n
< nplaylists
; ++n
) {
108 GtkWidget
*w
= gtk_menu_item_new_with_label(playlists
[n
]);
109 g_signal_connect(w
, "activate", G_CALLBACK(menu_activate_playlist
), 0);
111 gtk_menu_shell_append(menu
, w
);
113 gtk_widget_set_sensitive(playlists_widget
,
117 void edit_playlists(gpointer
attribute((unused
)) callback_data
,
118 guint
attribute((unused
)) callback_action
,
119 GtkWidget
attribute((unused
)) *menu_item
) {
120 fprintf(stderr
, "edit playlists\n"); /* TODO */
123 /** @brief Initialize playlist support */
124 void playlists_init(void) {
125 /* We re-get all playlists upon any change... */
126 event_register("playlist-created", playlists_update
, 0);
127 event_register("playlist-modified", playlists_update
, 0);
128 event_register("playlist-deleted", playlists_update
, 0);
129 /* ...and on reconnection */
130 event_register("log-connected", playlists_update
, 0);
131 /* ...and from time to time */
132 event_register("periodic-slow", playlists_update
, 0);
133 /* ...and at startup */
134 event_register("playlists-updated", menu_playlists_changed
, 0);
135 playlists_update(0, 0, 0);