chiark / gitweb /
Disobedience now keeps track of known playlists and has a (not yet
[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 *
23 */
24#include "disobedience.h"
25
26static void playlists_updated(void *v,
27 const char *err,
28 int nvec, char **vec);
29
30/** @brief Current list of playlists or NULL */
31char **playlists;
32
33/** @brief Count of playlists */
34int nplaylists;
35
36/** @brief Schedule an update to the list of playlists */
37static 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);
41}
42
43/** @brief qsort() callback for playlist name comparison */
44static 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, '.');
47 int c;
48
49 /* Group owned playlists by owner */
50 if(ad && bd) {
51 const int adn = ad - a, bdn = bd - b;
52 if((c = strncmp(a, b, adn < bdn ? adn : bdn)))
53 return c;
54 /* Lexical order within playlists of a single owner */
55 return strcmp(ad + 1, bd + 1);
56 }
57
58 /* Owned playlists after shared ones */
59 if(ad) {
60 return 1;
61 } else if(bd) {
62 return -1;
63 }
64
65 /* Lexical order of shared playlists */
66 return strcmp(a, b);
67}
68
69/** @brief Called with a new list of playlists */
70static void playlists_updated(void attribute((unused)) *v,
71 const char *err,
72 int nvec, char **vec) {
73 if(err) {
74 playlists = 0;
75 nplaylists = -1;
76 /* Probably means server does not support playlists */
77 } else {
78 playlists = vec;
79 nplaylists = nvec;
80 qsort(playlists, nplaylists, sizeof (char *), playlistcmp);
81 }
82 /* Tell our consumers */
83 event_raise("playlists-updated", 0);
84}
85
86/** @brief Initialize playlist support */
87void playlists_init(void) {
88 /* We re-get all playlists upon any change... */
89 event_register("playlist-created", playlists_update, 0);
90 event_register("playlist-modified", playlists_update, 0);
91 event_register("playlist-deleted", playlists_update, 0);
92 /* ...and on reconnection */
93 event_register("log-connected", playlists_update, 0);
94 /* ...and from time to time */
95 event_register("periodic-slow", playlists_update, 0);
96 /* ...and at startup */
97 playlists_update(0, 0, 0);
98}
99
100/*
101Local Variables:
102c-basic-offset:2
103comment-column:40
104fill-column:79
105indent-tabs-mode:nil
106End:
107*/