chiark / gitweb /
Almost compiles, will not work though
[disorder] / server / lookup.c
CommitLineData
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 */
20/** @file server/macros-disorder.c
21 * @brief DisOrder-specific expansions
22 */
23
24#include <config.h>
25#include "types.h"
26
71634563
RK
27#include <stdio.h>
28#include <string.h>
29
30#include "queue.h"
448d3570
RK
31#include "sink.h"
32#include "client.h"
71634563 33#include "rights.h"
448d3570
RK
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 */
43disorder_client *client;
44
45/** @brief Cached data */
46static unsigned flags;
47
48struct queue_entry *queue;
49struct queue_entry *playing;
50struct queue_entry *recent;
51
52int volume_left;
53int volume_right;
54
55char **files;
56int nfiles;
57
58char **dirs;
59int ndirs;
60
61char **new;
62int nnew;
63
64rights_type rights;
65
66int enabled;
67int random_enabled;
68
69/** @brief Fetch cachable data */
71634563 70void lookup(unsigned want) {
448d3570
RK
71 unsigned need = want ^ (flags & want);
72 struct queue_entry *r, *rnext;
73 const char *dir, *re;
71634563 74 char *rights_string;
448d3570
RK
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),
71634563
RK
114 "rights", &rights_string))
115 parse_rights(rights_string, &rights, 1);
448d3570
RK
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
71634563
RK
125void 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
448d3570
RK
136/*
137Local Variables:
138c-basic-offset:2
139comment-column:40
140fill-column:79
141indent-tabs-mode:nil
142End:
143*/