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