chiark / gitweb /
Doxygen-clean
[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
ad47bd4c
RK
82 *
83 * If it is in the cache then just return its value. If not then look it up
84 * and arrange for the queues to be updated when its value is available. */
85const char *namepart(const char *track,
86 const char *context,
87 const char *part) {
88 char *key;
89 const char *value;
90
91 D(("namepart %s %s %s", track, context, part));
92 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
93 context, part, track);
94 value = cache_get(&cachetype_string, key);
95 if(!value) {
96 D(("deferring..."));
97 namepart_fill(track, context, part, key);
98 value = "?";
99 }
100 return value;
101}
102
103/** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
104void namepart_update(const char *track,
105 const char *context,
106 const char *part) {
107 char *key;
108
109 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
110 context, part, track);
111 /* Only refetch if it's actually in the cache. */
112 if(cache_get(&cachetype_string, key))
113 namepart_fill(track, context, part, key);
114}
115
116/** @brief Look up a track length
117 *
118 * If it is in the cache then just return its value. If not then look it up
119 * and arrange for the queues to be updated when its value is available. */
120long namepart_length(const char *track) {
121 char *key;
122 const long *value;
123
124 D(("getlength %s", track));
125 byte_xasprintf(&key, "length track=%s", track);
126 value = cache_get(&cachetype_integer, key);
127 if(value)
128 return *value;
129 D(("deferring..."));;
130 ++namepart_lookups_outstanding;
131 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
132 disorder_eclient_length(client, length_completed, track, key);
133 return -1;
134}
135
68110302
RK
136/** @brief Resolve a track name
137 *
138 * Returns the supplied track name if it doesn't have the answer yet.
139 */
140char *namepart_resolve(const char *track) {
141 char *key;
142
143 byte_xasprintf(&key, "resolve track=%s", track);
144 const char *value = cache_get(&cachetype_string, key);
145 if(!value) {
146 D(("deferring..."));
147 ++namepart_lookups_outstanding;
148 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
149 disorder_eclient_resolve(client, namepart_completed,
150 track, (void *)key);
151 value = track;
152 }
153 return xstrdup(value);
154}
ad47bd4c
RK
155
156/*
157Local Variables:
158c-basic-offset:2
159comment-column:40
160fill-column:79
161indent-tabs-mode:nil
162End:
163*/