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 2 of the License, or
8 * (at your option) any later version.
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.
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
20 #include "disobedience.h"
22 static int namepart_lookups_outstanding
;
23 static const struct cache_type cachetype_string
= { 3600 };
24 static const struct cache_type cachetype_integer
= { 3600 };
26 /** @brief Called when a namepart lookup has completed or failed
28 * When there are no lookups in flight a redraw is provoked. This might well
29 * provoke further lookups.
31 static 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);
38 /** @brief Called when a namepart lookup has completed */
39 static void namepart_completed(void *v
, const char *error
, const char *value
) {
40 D(("namepart_completed"));
42 gtk_label_set_text(GTK_LABEL(report_label
), error
);
47 cache_put(&cachetype_string
, key
, value
);
48 namepart_completed_or_failed();
51 /** @brief Called when a length lookup has completed */
52 static void length_completed(void *v
, const char *error
, long l
) {
53 D(("length_completed"));
55 gtk_label_set_text(GTK_LABEL(report_label
), error
);
61 D(("namepart_completed"));
62 value
= xmalloc(sizeof *value
);
64 cache_put(&cachetype_integer
, key
, value
);
65 namepart_completed_or_failed();
68 /** @brief Arrange to fill in a namepart cache entry */
69 static void namepart_fill(const char *track
,
73 D(("namepart_fill %s %s %s %s", track
, context
, part
, key
));
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
);
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
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. */
88 const char *namepart(const char *track
,
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
);
100 namepart_fill(track
, context
, part
, key
);
106 /** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
107 void namepart_update(const char *track
,
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
);
119 /** @brief Look up a track length
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. */
123 long namepart_length(const char *track
) {
127 D(("getlength %s", track
));
128 byte_xasprintf(&key
, "length track=%s", track
);
129 value
= cache_get(&cachetype_integer
, key
);
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
);
139 /** @brief Resolve a track name
141 * Returns the supplied track name if it doesn't have the answer yet.
143 char *namepart_resolve(const char *track
) {
146 byte_xasprintf(&key
, "resolve track=%s", track
);
147 const char *value
= cache_get(&cachetype_string
, key
);
150 ++namepart_lookups_outstanding
;
151 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding
));
152 disorder_eclient_resolve(client
, namepart_completed
,
156 return xstrdup(value
);