Commit | Line | Data |
---|---|---|
448d3570 RK |
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 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 | */ | |
1e97629d RK |
20 | /** @file server/lookup.c |
21 | * @brief Server lookups | |
448d3570 | 22 | * |
1e97629d | 23 | * To improve performance many server lookups are cached. |
448d3570 | 24 | */ |
1e97629d RK |
25 | |
26 | #include "disorder-cgi.h" | |
448d3570 RK |
27 | |
28 | /** @brief Cached data */ | |
29 | static unsigned flags; | |
30 | ||
a2c4ad5f RK |
31 | /** @brief Map of hashes to queud data */ |
32 | static hash *queuemap; | |
33 | ||
1e97629d RK |
34 | struct queue_entry *dcgi_queue; |
35 | struct queue_entry *dcgi_playing; | |
36 | struct queue_entry *dcgi_recent; | |
448d3570 | 37 | |
1e97629d RK |
38 | int dcgi_volume_left; |
39 | int dcgi_volume_right; | |
448d3570 | 40 | |
1e97629d RK |
41 | char **dcgi_new; |
42 | int dcgi_nnew; | |
448d3570 | 43 | |
1e97629d | 44 | rights_type dcgi_rights; |
448d3570 | 45 | |
1e97629d RK |
46 | int dcgi_enabled; |
47 | int dcgi_random_enabled; | |
448d3570 | 48 | |
a2c4ad5f RK |
49 | static void queuemap_add(struct queue_entry *q) { |
50 | if(!queuemap) | |
51 | queuemap = hash_new(sizeof (struct queue_entry *)); | |
52 | for(; q; q = q->next) | |
53 | hash_add(queuemap, q->id, &q, HASH_INSERT_OR_REPLACE); | |
54 | } | |
55 | ||
448d3570 | 56 | /** @brief Fetch cachable data */ |
1e97629d | 57 | void dcgi_lookup(unsigned want) { |
448d3570 RK |
58 | unsigned need = want ^ (flags & want); |
59 | struct queue_entry *r, *rnext; | |
1e97629d | 60 | #if 0 |
448d3570 | 61 | const char *dir, *re; |
1e97629d RK |
62 | #endif |
63 | char *rs; | |
448d3570 | 64 | |
1e97629d | 65 | if(!dcgi_client || !need) |
448d3570 | 66 | return; |
a2c4ad5f | 67 | if(need & DCGI_QUEUE) { |
1e97629d | 68 | disorder_queue(dcgi_client, &dcgi_queue); |
a2c4ad5f RK |
69 | queuemap_add(dcgi_queue); |
70 | } | |
71 | if(need & DCGI_PLAYING) { | |
1e97629d | 72 | disorder_playing(dcgi_client, &dcgi_playing); |
a2c4ad5f RK |
73 | queuemap_add(dcgi_playing); |
74 | } | |
1e97629d RK |
75 | if(need & DCGI_NEW) |
76 | disorder_new_tracks(dcgi_client, &dcgi_new, &dcgi_nnew, 0); | |
77 | if(need & DCGI_RECENT) { | |
448d3570 | 78 | /* we need to reverse the order of the list */ |
1e97629d | 79 | disorder_recent(dcgi_client, &r); |
448d3570 RK |
80 | while(r) { |
81 | rnext = r->next; | |
1e97629d RK |
82 | r->next = dcgi_recent; |
83 | dcgi_recent = r; | |
448d3570 RK |
84 | r = rnext; |
85 | } | |
a2c4ad5f | 86 | queuemap_add(dcgi_recent); |
448d3570 | 87 | } |
1e97629d RK |
88 | if(need & DCGI_VOLUME) |
89 | disorder_get_volume(dcgi_client, | |
90 | &dcgi_volume_left, &dcgi_volume_right); | |
91 | #if 0 | |
92 | /* DCGI_FILES and DCGI_DIRS are looking obsolete now */ | |
93 | if(need & (DCGI_FILES|DCGI_DIRS)) { | |
448d3570 RK |
94 | if(!(dir = cgi_get("directory"))) |
95 | dir = ""; | |
96 | re = cgi_get("regexp"); | |
1e97629d RK |
97 | if(need & DCGI_DIRS) |
98 | if(disorder_directories(dcgi_client, dir, re, | |
448d3570 RK |
99 | &dirs, &ndirs)) |
100 | ndirs = 0; | |
1e97629d RK |
101 | if(need & DCGI_FILES) |
102 | if(disorder_files(dcgi_client, dir, re, | |
448d3570 RK |
103 | &files, &nfiles)) |
104 | nfiles = 0; | |
105 | } | |
1e97629d RK |
106 | #endif |
107 | if(need & DCGI_RIGHTS) { | |
108 | dcgi_rights = RIGHT_READ; /* fail-safe */ | |
109 | if(!disorder_userinfo(dcgi_client, disorder_user(dcgi_client), | |
110 | "rights", &rs)) | |
111 | parse_rights(rs, &dcgi_rights, 1); | |
448d3570 | 112 | } |
1e97629d RK |
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); | |
448d3570 RK |
117 | flags |= need; |
118 | } | |
119 | ||
a2c4ad5f RK |
120 | /** @brief Locate a track by ID */ |
121 | struct queue_entry *dcgi_findtrack(const char *id) { | |
30d85588 | 122 | struct queue_entry **qq; |
a2c4ad5f | 123 | |
30d85588 RK |
124 | if(queuemap && (qq = hash_find(queuemap, id))) |
125 | return *qq; | |
a2c4ad5f | 126 | dcgi_lookup(DCGI_PLAYING); |
30d85588 RK |
127 | if(queuemap && (qq = hash_find(queuemap, id))) |
128 | return *qq; | |
a2c4ad5f | 129 | dcgi_lookup(DCGI_QUEUE); |
30d85588 RK |
130 | if(queuemap && (qq = hash_find(queuemap, id))) |
131 | return *qq; | |
a2c4ad5f | 132 | dcgi_lookup(DCGI_RECENT); |
30d85588 RK |
133 | if(queuemap && (qq = hash_find(queuemap, id))) |
134 | return *qq; | |
a2c4ad5f RK |
135 | return NULL; |
136 | } | |
137 | ||
1e97629d | 138 | void dcgi_lookup_reset(void) { |
71634563 RK |
139 | /* Forget everything we knew */ |
140 | flags = 0; | |
a2c4ad5f | 141 | queuemap = 0; |
1e97629d RK |
142 | dcgi_recent = 0; |
143 | dcgi_queue = 0; | |
144 | dcgi_playing = 0; | |
145 | dcgi_rights = 0; | |
146 | dcgi_new = 0; | |
147 | dcgi_nnew = 0; | |
148 | dcgi_enabled = 0; | |
149 | dcgi_random_enabled = 0; | |
150 | dcgi_volume_left = dcgi_volume_right = 0; | |
71634563 RK |
151 | } |
152 | ||
153 | ||
448d3570 RK |
154 | /* |
155 | Local Variables: | |
156 | c-basic-offset:2 | |
157 | comment-column:40 | |
158 | fill-column:79 | |
159 | indent-tabs-mode:nil | |
160 | End: | |
161 | */ |