4005238323f27d01ddb93afc9bffb274620ba8ec
[disorder] / server / 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 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 */
20 /** @file server/macros-disorder.c
21 * @brief DisOrder-specific expansions
22 */
23
24 #include <config.h>
25 #include "types.h"
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "queue.h"
31 #include "sink.h"
32 #include "client.h"
33 #include "rights.h"
34 #include "lookup.h"
35 #include "cgi.h"
36
37 /** @brief Client used by CGI
38 *
39 * The caller should arrange for this to be created before any of
40 * these expansions are used (if it cannot connect then it's safe to
41 * leave it as NULL).
42 */
43 disorder_client *client;
44
45 /** @brief Cached data */
46 static unsigned flags;
47
48 struct queue_entry *queue;
49 struct queue_entry *playing;
50 struct queue_entry *recent;
51
52 int volume_left;
53 int volume_right;
54
55 char **files;
56 int nfiles;
57
58 char **dirs;
59 int ndirs;
60
61 char **new;
62 int nnew;
63
64 rights_type rights;
65
66 int enabled;
67 int random_enabled;
68
69 /** @brief Fetch cachable data */
70 void lookup(unsigned want) {
71 unsigned need = want ^ (flags & want);
72 struct queue_entry *r, *rnext;
73 const char *dir, *re;
74 char *rights_string;
75
76 if(!client || !need)
77 return;
78 if(need & DC_QUEUE)
79 disorder_queue(client, &queue);
80 if(need & DC_PLAYING)
81 disorder_playing(client, &playing);
82 if(need & DC_NEW)
83 disorder_new_tracks(client, &new, &nnew, 0);
84 if(need & DC_RECENT) {
85 /* we need to reverse the order of the list */
86 disorder_recent(client, &r);
87 while(r) {
88 rnext = r->next;
89 r->next = recent;
90 recent = r;
91 r = rnext;
92 }
93 }
94 if(need & DC_VOLUME)
95 disorder_get_volume(client,
96 &volume_left, &volume_right);
97 /* DC_FILES and DC_DIRS are looking obsolete now */
98 if(need & (DC_FILES|DC_DIRS)) {
99 if(!(dir = cgi_get("directory")))
100 dir = "";
101 re = cgi_get("regexp");
102 if(need & DC_DIRS)
103 if(disorder_directories(client, dir, re,
104 &dirs, &ndirs))
105 ndirs = 0;
106 if(need & DC_FILES)
107 if(disorder_files(client, dir, re,
108 &files, &nfiles))
109 nfiles = 0;
110 }
111 if(need & DC_RIGHTS) {
112 rights = RIGHT_READ; /* fail-safe */
113 if(!disorder_userinfo(client, disorder_user(client),
114 "rights", &rights_string))
115 parse_rights(rights_string, &rights, 1);
116 }
117 if(need & DC_ENABLED)
118 disorder_enabled(client, &enabled);
119 if(need & DC_RANDOM_ENABLED)
120 disorder_random_enabled(client, &random_enabled);
121 if(need & DC_RANDOM_ENABLED)
122 flags |= need;
123 }
124
125 void lookup_reset(void) {
126 /* Junk the old connection if there is one */
127 if(client)
128 disorder_close(client);
129 /* Create a new connection */
130 client = disorder_new(0);
131 /* Forget everything we knew */
132 flags = 0;
133 }
134
135
136 /*
137 Local Variables:
138 c-basic-offset:2
139 comment-column:40
140 fill-column:79
141 indent-tabs-mode:nil
142 End:
143 */