2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "disobedience.h"
20 static int namepart_lookups_outstanding;
21 static const struct cache_type cachetype_string = { 3600 };
22 static const struct cache_type cachetype_integer = { 3600 };
24 /** @brief Called when a namepart lookup has completed or failed
26 * When there are no lookups in flight a redraw is provoked. This might well
27 * provoke further lookups.
29 static 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);
36 /** @brief Called when a namepart lookup has completed */
37 static void namepart_completed(void *v, const char *err, const char *value) {
38 D(("namepart_completed"));
40 gtk_label_set_text(GTK_LABEL(report_label), err);
45 cache_put(&cachetype_string, key, value);
46 namepart_completed_or_failed();
49 /** @brief Called when a length lookup has completed */
50 static void length_completed(void *v, const char *err, long l) {
51 D(("length_completed"));
53 gtk_label_set_text(GTK_LABEL(report_label), err);
59 D(("namepart_completed"));
60 value = xmalloc(sizeof *value);
62 cache_put(&cachetype_integer, key, value);
63 namepart_completed_or_failed();
66 /** @brief Arrange to fill in a namepart cache entry */
67 static void namepart_fill(const char *track,
71 D(("namepart_fill %s %s %s %s", track, context, part, key));
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);
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
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. */
86 const char *namepart(const char *track,
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);
98 namepart_fill(track, context, part, key);
104 /** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
105 void namepart_update(const char *track,
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);
117 /** @brief Look up a track length
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. */
121 long namepart_length(const char *track) {
125 D(("getlength %s", track));
126 byte_xasprintf(&key, "length track=%s", track);
127 value = cache_get(&cachetype_integer, key);
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);
137 /** @brief Resolve a track name
139 * Returns the supplied track name if it doesn't have the answer yet.
141 char *namepart_resolve(const char *track) {
144 byte_xasprintf(&key, "resolve track=%s", track);
145 const char *value = cache_get(&cachetype_string, key);
148 ++namepart_lookups_outstanding;
149 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
150 disorder_eclient_resolve(client, namepart_completed,
154 return xstrdup(value);