chiark / gitweb /
Obsolete playing_isscratch. The constant still exists as a transition
[disorder] / cgi / lookup.c
CommitLineData
448d3570
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
448d3570 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
448d3570
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
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.
14 *
448d3570 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
448d3570 17 */
59cf25c4 18/** @file cgi/lookup.c
1e97629d 19 * @brief Server lookups
448d3570 20 *
1e97629d 21 * To improve performance many server lookups are cached.
448d3570 22 */
1e97629d
RK
23
24#include "disorder-cgi.h"
448d3570
RK
25
26/** @brief Cached data */
27static unsigned flags;
28
a2c4ad5f
RK
29/** @brief Map of hashes to queud data */
30static hash *queuemap;
31
1e97629d
RK
32struct queue_entry *dcgi_queue;
33struct queue_entry *dcgi_playing;
34struct queue_entry *dcgi_recent;
448d3570 35
1e97629d
RK
36int dcgi_volume_left;
37int dcgi_volume_right;
448d3570 38
1e97629d
RK
39char **dcgi_new;
40int dcgi_nnew;
448d3570 41
1e97629d 42rights_type dcgi_rights;
448d3570 43
1e97629d
RK
44int dcgi_enabled;
45int dcgi_random_enabled;
448d3570 46
a2c4ad5f
RK
47static void queuemap_add(struct queue_entry *q) {
48 if(!queuemap)
49 queuemap = hash_new(sizeof (struct queue_entry *));
50 for(; q; q = q->next)
51 hash_add(queuemap, q->id, &q, HASH_INSERT_OR_REPLACE);
52}
53
448d3570 54/** @brief Fetch cachable data */
1e97629d 55void dcgi_lookup(unsigned want) {
448d3570
RK
56 unsigned need = want ^ (flags & want);
57 struct queue_entry *r, *rnext;
1e97629d 58 char *rs;
448d3570 59
1e97629d 60 if(!dcgi_client || !need)
448d3570 61 return;
a2c4ad5f 62 if(need & DCGI_QUEUE) {
1e97629d 63 disorder_queue(dcgi_client, &dcgi_queue);
a2c4ad5f
RK
64 queuemap_add(dcgi_queue);
65 }
66 if(need & DCGI_PLAYING) {
1e97629d 67 disorder_playing(dcgi_client, &dcgi_playing);
a2c4ad5f
RK
68 queuemap_add(dcgi_playing);
69 }
1e97629d
RK
70 if(need & DCGI_NEW)
71 disorder_new_tracks(dcgi_client, &dcgi_new, &dcgi_nnew, 0);
72 if(need & DCGI_RECENT) {
448d3570 73 /* we need to reverse the order of the list */
1e97629d 74 disorder_recent(dcgi_client, &r);
448d3570
RK
75 while(r) {
76 rnext = r->next;
1e97629d
RK
77 r->next = dcgi_recent;
78 dcgi_recent = r;
448d3570
RK
79 r = rnext;
80 }
a2c4ad5f 81 queuemap_add(dcgi_recent);
448d3570 82 }
1e97629d
RK
83 if(need & DCGI_VOLUME)
84 disorder_get_volume(dcgi_client,
85 &dcgi_volume_left, &dcgi_volume_right);
1e97629d
RK
86 if(need & DCGI_RIGHTS) {
87 dcgi_rights = RIGHT_READ; /* fail-safe */
88 if(!disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
89 "rights", &rs))
90 parse_rights(rs, &dcgi_rights, 1);
448d3570 91 }
1e97629d
RK
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);
448d3570
RK
96 flags |= need;
97}
98
a2c4ad5f
RK
99/** @brief Locate a track by ID */
100struct queue_entry *dcgi_findtrack(const char *id) {
30d85588 101 struct queue_entry **qq;
a2c4ad5f 102
30d85588
RK
103 if(queuemap && (qq = hash_find(queuemap, id)))
104 return *qq;
a2c4ad5f 105 dcgi_lookup(DCGI_PLAYING);
30d85588
RK
106 if(queuemap && (qq = hash_find(queuemap, id)))
107 return *qq;
a2c4ad5f 108 dcgi_lookup(DCGI_QUEUE);
30d85588
RK
109 if(queuemap && (qq = hash_find(queuemap, id)))
110 return *qq;
a2c4ad5f 111 dcgi_lookup(DCGI_RECENT);
30d85588
RK
112 if(queuemap && (qq = hash_find(queuemap, id)))
113 return *qq;
a2c4ad5f
RK
114 return NULL;
115}
116
1e97629d 117void dcgi_lookup_reset(void) {
71634563
RK
118 /* Forget everything we knew */
119 flags = 0;
a2c4ad5f 120 queuemap = 0;
1e97629d
RK
121 dcgi_recent = 0;
122 dcgi_queue = 0;
123 dcgi_playing = 0;
124 dcgi_rights = 0;
125 dcgi_new = 0;
126 dcgi_nnew = 0;
127 dcgi_enabled = 0;
128 dcgi_random_enabled = 0;
129 dcgi_volume_left = dcgi_volume_right = 0;
71634563
RK
130}
131
132
448d3570
RK
133/*
134Local Variables:
135c-basic-offset:2
136comment-column:40
137fill-column:79
138indent-tabs-mode:nil
139End:
140*/