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 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 server/lookup.c
21 * @brief Server lookups
23 * To improve performance many server lookups are cached.
26 #include "disorder-cgi.h"
28 /** @brief Cached data */
29 static unsigned flags;
31 /** @brief Map of hashes to queud data */
32 static hash *queuemap;
34 struct queue_entry *dcgi_queue;
35 struct queue_entry *dcgi_playing;
36 struct queue_entry *dcgi_recent;
39 int dcgi_volume_right;
44 rights_type dcgi_rights;
47 int dcgi_random_enabled;
49 static void queuemap_add(struct queue_entry *q) {
51 queuemap = hash_new(sizeof (struct queue_entry *));
53 hash_add(queuemap, q->id, &q, HASH_INSERT_OR_REPLACE);
56 /** @brief Fetch cachable data */
57 void dcgi_lookup(unsigned want) {
58 unsigned need = want ^ (flags & want);
59 struct queue_entry *r, *rnext;
65 if(!dcgi_client || !need)
67 if(need & DCGI_QUEUE) {
68 disorder_queue(dcgi_client, &dcgi_queue);
69 queuemap_add(dcgi_queue);
71 if(need & DCGI_PLAYING) {
72 disorder_playing(dcgi_client, &dcgi_playing);
73 queuemap_add(dcgi_playing);
76 disorder_new_tracks(dcgi_client, &dcgi_new, &dcgi_nnew, 0);
77 if(need & DCGI_RECENT) {
78 /* we need to reverse the order of the list */
79 disorder_recent(dcgi_client, &r);
82 r->next = dcgi_recent;
86 queuemap_add(dcgi_recent);
88 if(need & DCGI_VOLUME)
89 disorder_get_volume(dcgi_client,
90 &dcgi_volume_left, &dcgi_volume_right);
92 /* DCGI_FILES and DCGI_DIRS are looking obsolete now */
93 if(need & (DCGI_FILES|DCGI_DIRS)) {
94 if(!(dir = cgi_get("directory")))
96 re = cgi_get("regexp");
98 if(disorder_directories(dcgi_client, dir, re,
101 if(need & DCGI_FILES)
102 if(disorder_files(dcgi_client, dir, re,
107 if(need & DCGI_RIGHTS) {
108 dcgi_rights = RIGHT_READ; /* fail-safe */
109 if(!disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
111 parse_rights(rs, &dcgi_rights, 1);
113 if(need & DCGI_ENABLED)
114 disorder_enabled(dcgi_client, &dcgi_enabled);
115 if(need & DCGI_RANDOM_ENABLED)
116 disorder_random_enabled(dcgi_client, &dcgi_random_enabled);
120 /** @brief Locate a track by ID */
121 struct queue_entry *dcgi_findtrack(const char *id) {
122 struct queue_entry *q, **qq;
124 if(queuemap && (qq = hash_find(id)))
126 dcgi_lookup(DCGI_PLAYING);
127 if(queuemap && (qq = hash_find(id)))
129 dcgi_lookup(DCGI_QUEUE);
130 if(queuemap && (qq = hash_find(id)))
132 dcgi_lookup(DCGI_RECENT);
133 if(queuemap && (qq = hash_find(id)))
138 void dcgi_lookup_reset(void) {
139 /* Forget everything we knew */
149 dcgi_random_enabled = 0;
150 dcgi_volume_left = dcgi_volume_right = 0;