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
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. */
85 const char *namepart(const char *track
,
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
);
97 namepart_fill(track
, context
, part
, key
);
103 /** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
104 void namepart_update(const char *track
,
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
);
116 /** @brief Look up a track length
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. */
120 long namepart_length(const char *track
) {
124 D(("getlength %s", track
));
125 byte_xasprintf(&key
, "length track=%s", track
);
126 value
= cache_get(&cachetype_integer
, key
);
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
);
136 /** @brief Resolve a track name
138 * Returns the supplied track name if it doesn't have the answer yet.
140 char *namepart_resolve(const char *track
) {
143 byte_xasprintf(&key
, "resolve track=%s", track
);
144 const char *value
= cache_get(&cachetype_string
, key
);
147 ++namepart_lookups_outstanding
;
148 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding
));
149 disorder_eclient_resolve(client
, namepart_completed
,
153 return xstrdup(value
);