chiark / gitweb /
examples/disorder.init.in: Read settings from `/etc/default/disorder'.
[disorder] / cgi / lookup.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004-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 3 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,
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  * 
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/>.
17  */
18 /** @file cgi/lookup.c
19  * @brief Server lookups
20  *
21  * To improve performance many server lookups are cached.
22  */
23
24 #include "disorder-cgi.h"
25
26 /** @brief Cached data */
27 static unsigned flags;
28
29 /** @brief Map of hashes to queud data */
30 static hash *queuemap;
31
32 struct queue_entry *dcgi_queue;
33 struct queue_entry *dcgi_playing;
34 struct queue_entry *dcgi_recent;
35
36 long dcgi_volume_left;
37 long dcgi_volume_right;
38
39 char **dcgi_new;
40 int dcgi_nnew;
41
42 rights_type dcgi_rights;
43
44 int dcgi_enabled;
45 int dcgi_random_enabled;
46
47 static 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
54 /** @brief Fetch cachable data */
55 void dcgi_lookup(unsigned want) {
56   unsigned need = want ^ (flags & want);
57   struct queue_entry *r, *rnext;
58   char *rs;
59
60   if(!dcgi_client || !need)
61     return;
62   if(need & DCGI_QUEUE) {
63     disorder_queue(dcgi_client, &dcgi_queue);
64     queuemap_add(dcgi_queue);
65   }
66   if(need & DCGI_PLAYING) {
67     disorder_playing(dcgi_client, &dcgi_playing);
68     queuemap_add(dcgi_playing);
69   }
70   if(need & DCGI_NEW)
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);
75     while(r) {
76       rnext = r->next;
77       r->next = dcgi_recent;
78       dcgi_recent = r;
79       r = rnext;
80     }
81     queuemap_add(dcgi_recent);
82   }
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),
89                           "rights", &rs))
90       parse_rights(rs, &dcgi_rights, 1);
91   }
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);
96   flags |= need;
97 }
98
99 /** @brief Locate a track by ID */
100 struct queue_entry *dcgi_findtrack(const char *id) {
101   struct queue_entry **qq;
102
103   if(queuemap && (qq = hash_find(queuemap, id)))
104     return *qq;
105   dcgi_lookup(DCGI_PLAYING);
106   if(queuemap && (qq = hash_find(queuemap, id)))
107     return *qq;
108   dcgi_lookup(DCGI_QUEUE);
109   if(queuemap && (qq = hash_find(queuemap, id)))
110     return *qq;
111   dcgi_lookup(DCGI_RECENT);
112   if(queuemap && (qq = hash_find(queuemap, id)))
113     return *qq;
114   return NULL;
115 }
116
117 void dcgi_lookup_reset(void) {
118   /* Forget everything we knew */
119   flags = 0;
120   queuemap = 0;
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;
130 }
131
132
133 /*
134 Local Variables:
135 c-basic-offset:2
136 comment-column:40
137 fill-column:79
138 indent-tabs-mode:nil
139 End:
140 */