Switch to GPL v3
[disorder] / disobedience / lookup.c
CommitLineData
ad47bd4c
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
ad47bd4c 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
ad47bd4c
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
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 *
ad47bd4c 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ad47bd4c
RK
17 */
18#include "disobedience.h"
19
20static int namepart_lookups_outstanding;
21static const struct cache_type cachetype_string = { 3600 };
22static const struct cache_type cachetype_integer = { 3600 };
23
24/** @brief Called when a namepart lookup has completed or failed
25 *
26 * When there are no lookups in flight a redraw is provoked. This might well
27 * provoke further lookups.
28 */
29static void namepart_completed_or_failed(void) {
30 --namepart_lookups_outstanding;
31 if(!namepart_lookups_outstanding)
32 /* When all lookups complete, we update any displays that care */
33 event_raise("lookups-completed", 0);
34}
35
36/** @brief Called when a namepart lookup has completed */
abf99697 37static void namepart_completed(void *v, const char *err, const char *value) {
ad47bd4c 38 D(("namepart_completed"));
abf99697
RK
39 if(err) {
40 gtk_label_set_text(GTK_LABEL(report_label), err);
ad47bd4c
RK
41 value = "?";
42 }
43 const char *key = v;
44
45 cache_put(&cachetype_string, key, value);
46 namepart_completed_or_failed();
47}
48
49/** @brief Called when a length lookup has completed */
abf99697 50static void length_completed(void *v, const char *err, long l) {
ad47bd4c 51 D(("length_completed"));
abf99697
RK
52 if(err) {
53 gtk_label_set_text(GTK_LABEL(report_label), err);
ad47bd4c
RK
54 l = -1;
55 }
56 const char *key = v;
57 long *value;
58
59 D(("namepart_completed"));
60 value = xmalloc(sizeof *value);
61 *value = l;
62 cache_put(&cachetype_integer, key, value);
63 namepart_completed_or_failed();
64}
65
66/** @brief Arrange to fill in a namepart cache entry */
67static void namepart_fill(const char *track,
68 const char *context,
69 const char *part,
70 const char *key) {
71 D(("namepart_fill %s %s %s %s", track, context, part, key));
ad47bd4c
RK
72 ++namepart_lookups_outstanding;
73 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
74 disorder_eclient_namepart(client, namepart_completed,
75 track, context, part, (void *)key);
76}
77
78/** @brief Look up a namepart
79 * @param track Track name
80 * @param context Context
81 * @param part Name part
82 * @param lookup If nonzero, will schedule a lookup for unknown values
83 *
84 * If it is in the cache then just return its value. If not then look it up
85 * and arrange for the queues to be updated when its value is available. */
86const char *namepart(const char *track,
87 const char *context,
88 const char *part) {
89 char *key;
90 const char *value;
91
92 D(("namepart %s %s %s", track, context, part));
93 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
94 context, part, track);
95 value = cache_get(&cachetype_string, key);
96 if(!value) {
97 D(("deferring..."));
98 namepart_fill(track, context, part, key);
99 value = "?";
100 }
101 return value;
102}
103
104/** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
105void namepart_update(const char *track,
106 const char *context,
107 const char *part) {
108 char *key;
109
110 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
111 context, part, track);
112 /* Only refetch if it's actually in the cache. */
113 if(cache_get(&cachetype_string, key))
114 namepart_fill(track, context, part, key);
115}
116
117/** @brief Look up a track length
118 *
119 * If it is in the cache then just return its value. If not then look it up
120 * and arrange for the queues to be updated when its value is available. */
121long namepart_length(const char *track) {
122 char *key;
123 const long *value;
124
125 D(("getlength %s", track));
126 byte_xasprintf(&key, "length track=%s", track);
127 value = cache_get(&cachetype_integer, key);
128 if(value)
129 return *value;
130 D(("deferring..."));;
131 ++namepart_lookups_outstanding;
132 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
133 disorder_eclient_length(client, length_completed, track, key);
134 return -1;
135}
136
68110302
RK
137/** @brief Resolve a track name
138 *
139 * Returns the supplied track name if it doesn't have the answer yet.
140 */
141char *namepart_resolve(const char *track) {
142 char *key;
143
144 byte_xasprintf(&key, "resolve track=%s", track);
145 const char *value = cache_get(&cachetype_string, key);
146 if(!value) {
147 D(("deferring..."));
148 ++namepart_lookups_outstanding;
149 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
150 disorder_eclient_resolve(client, namepart_completed,
151 track, (void *)key);
152 value = track;
153 }
154 return xstrdup(value);
155}
ad47bd4c
RK
156
157/*
158Local Variables:
159c-basic-offset:2
160comment-column:40
161fill-column:79
162indent-tabs-mode:nil
163End:
164*/