2 * This file is part of DisOrder.
3 * Copyright (C) 2004-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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file cgi/lookup.c
19 * @brief Server lookups
21 * To improve performance many server lookups are cached.
24 #include "disorder-cgi.h"
26 /** @brief Cached data */
27 static unsigned flags
;
29 /** @brief Map of hashes to queud data */
30 static hash
*queuemap
;
32 struct queue_entry
*dcgi_queue
;
33 struct queue_entry
*dcgi_playing
;
34 struct queue_entry
*dcgi_recent
;
36 long dcgi_volume_left
;
37 long dcgi_volume_right
;
42 rights_type dcgi_rights
;
45 int dcgi_random_enabled
;
47 static void queuemap_add(struct queue_entry
*q
) {
49 queuemap
= hash_new(sizeof (struct queue_entry
*));
51 hash_add(queuemap
, q
->id
, &q
, HASH_INSERT_OR_REPLACE
);
54 /** @brief Fetch cachable data */
55 void dcgi_lookup(unsigned want
) {
56 unsigned need
= want
^ (flags
& want
);
57 struct queue_entry
*r
, *rnext
;
60 if(!dcgi_client
|| !need
)
62 if(need
& DCGI_QUEUE
) {
63 disorder_queue(dcgi_client
, &dcgi_queue
);
64 queuemap_add(dcgi_queue
);
66 if(need
& DCGI_PLAYING
) {
67 disorder_playing(dcgi_client
, &dcgi_playing
);
68 queuemap_add(dcgi_playing
);
71 disorder_new_tracks(dcgi_client
, 0, &dcgi_new
, &dcgi_nnew
);
72 if(need
& DCGI_RECENT
) {
73 /* we need to reverse the order of the list */
74 disorder_recent(dcgi_client
, &r
);
77 r
->next
= dcgi_recent
;
81 queuemap_add(dcgi_recent
);
83 if(need
& DCGI_VOLUME
)
84 disorder_get_volume(dcgi_client
,
85 &dcgi_volume_left
, &dcgi_volume_right
);
86 if(need
& DCGI_RIGHTS
) {
87 dcgi_rights
= RIGHT_READ
; /* fail-safe */
88 if(!disorder_userinfo(dcgi_client
, disorder_user(dcgi_client
),
90 parse_rights(rs
, &dcgi_rights
, 1);
92 if(need
& DCGI_ENABLED
)
93 disorder_enabled(dcgi_client
, &dcgi_enabled
);
94 if(need
& DCGI_RANDOM_ENABLED
)
95 disorder_random_enabled(dcgi_client
, &dcgi_random_enabled
);
99 /** @brief Locate a track by ID */
100 struct queue_entry
*dcgi_findtrack(const char *id
) {
101 struct queue_entry
**qq
;
103 if(queuemap
&& (qq
= hash_find(queuemap
, id
)))
105 dcgi_lookup(DCGI_PLAYING
);
106 if(queuemap
&& (qq
= hash_find(queuemap
, id
)))
108 dcgi_lookup(DCGI_QUEUE
);
109 if(queuemap
&& (qq
= hash_find(queuemap
, id
)))
111 dcgi_lookup(DCGI_RECENT
);
112 if(queuemap
&& (qq
= hash_find(queuemap
, id
)))
117 void dcgi_lookup_reset(void) {
118 /* Forget everything we knew */
128 dcgi_random_enabled
= 0;
129 dcgi_volume_left
= dcgi_volume_right
= 0;