2 * This file is part of DisOrder
3 * Copyright (C) 2006-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 /** @file disobedience/queue-generic.c
21 * @brief Queue widgets
23 * This file provides contains code shared between all the queue-like
24 * widgets - the queue, the recent list and the added tracks list.
26 * This code is in the process of being rewritten to use the native list
29 * There are three @ref queuelike objects: @ref ql_queue, @ref
30 * ql_recent and @ref ql_added. Each has an associated queue linked
31 * list and a list store containing the contents.
33 * When new contents turn up we rearrange the list store accordingly.
35 * NB that while in the server the playing track is not in the queue, in
36 * Disobedience, the playing does live in @c ql_queue.q, despite its different
37 * status to everything else found in that list.
39 #include "disobedience.h"
40 #include "queue-generic.h"
42 static struct queuelike *const queuelikes[] = {
43 &ql_queue, &ql_recent, &ql_added
45 #define NQUEUELIKES (sizeof queuelikes / sizeof *queuelikes)
47 /* Track detail lookup ----------------------------------------------------- */
49 static int namepart_lookups_outstanding;
50 static const struct cache_type cachetype_string = { 3600 };
51 static const struct cache_type cachetype_integer = { 3600 };
53 /** @brief Called when a namepart lookup has completed or failed
55 * When there are no lookups in flight a redraw is provoked. This might well
56 * provoke further lookups.
58 static void namepart_completed_or_failed(void) {
59 --namepart_lookups_outstanding;
60 fprintf(stderr, "namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding);
61 if(!namepart_lookups_outstanding) {
62 /* There are no more lookups outstanding, so we update the display */
63 for(unsigned n = 0; n < NQUEUELIKES; ++n)
64 ql_update_list_store(queuelikes[n]);
68 /** @brief Called when a namepart lookup has completed */
69 static void namepart_completed(void *v, const char *error, const char *value) {
70 D(("namepart_completed"));
72 gtk_label_set_text(GTK_LABEL(report_label), error);
77 cache_put(&cachetype_string, key, value);
78 namepart_completed_or_failed();
81 /** @brief Called when a length lookup has completed */
82 static void length_completed(void *v, const char *error, long l) {
83 D(("length_completed"));
85 gtk_label_set_text(GTK_LABEL(report_label), error);
91 D(("namepart_completed"));
92 value = xmalloc(sizeof *value);
94 cache_put(&cachetype_integer, key, value);
95 namepart_completed_or_failed();
98 /** @brief Arrange to fill in a namepart cache entry */
99 static void namepart_fill(const char *track,
103 D(("namepart_fill %s %s %s %s", track, context, part, key));
104 /* We limit the total number of lookups in flight */
105 ++namepart_lookups_outstanding;
106 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
107 disorder_eclient_namepart(client, namepart_completed,
108 track, context, part, (void *)key);
111 /** @brief Look up a namepart
112 * @param track Track name
113 * @param context Context
114 * @param part Name part
115 * @param lookup If nonzero, will schedule a lookup for unknown values
117 * If it is in the cache then just return its value. If not then look it up
118 * and arrange for the queues to be updated when its value is available. */
119 static const char *namepart(const char *track,
125 D(("namepart %s %s %s", track, context, part));
126 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
127 context, part, track);
128 value = cache_get(&cachetype_string, key);
131 namepart_fill(track, context, part, key);
137 /** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
138 void namepart_update(const char *track,
143 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
144 context, part, track);
145 /* Only refetch if it's actually in the cache. */
146 if(cache_get(&cachetype_string, key))
147 namepart_fill(track, context, part, key);
150 /** @brief Look up a track length
152 * If it is in the cache then just return its value. If not then look it up
153 * and arrange for the queues to be updated when its value is available. */
154 static long getlength(const char *track) {
158 D(("getlength %s", track));
159 byte_xasprintf(&key, "length track=%s", track);
160 value = cache_get(&cachetype_integer, key);
163 D(("deferring..."));;
164 ++namepart_lookups_outstanding;
165 D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
166 disorder_eclient_length(client, length_completed, track, key);
170 /* Column formatting -------------------------------------------------------- */
172 /** @brief Format the 'when' column */
173 const char *column_when(const struct queue_entry *q,
174 const char attribute((unused)) *data) {
181 case playing_isscratch:
182 case playing_unplayed:
187 case playing_no_player:
189 case playing_scratched:
190 case playing_started:
192 case playing_quitting:
200 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
203 return xstrdup(when);
206 /** @brief Format the 'who' column */
207 const char *column_who(const struct queue_entry *q,
208 const char attribute((unused)) *data) {
210 return q->submitter ? q->submitter : "";
213 /** @brief Format one of the track name columns */
214 const char *column_namepart(const struct queue_entry *q,
216 D(("column_namepart"));
217 return namepart(q->track, "display", data);
220 /** @brief Format the length column */
221 const char *column_length(const struct queue_entry *q,
222 const char attribute((unused)) *data) {
223 D(("column_length"));
226 char *played = 0, *length = 0;
228 /* Work out what to say for the length */
229 l = getlength(q->track);
231 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
233 byte_xasprintf(&length, "?:??");
234 /* For the currently playing track we want to report how much of the track
236 if(q == playing_track) {
237 /* log_state() arranges that we re-get the playing data whenever the
238 * pause/resume state changes */
239 if(last_state & DISORDER_TRACK_PAUSED)
240 l = playing_track->sofar;
243 l = playing_track->sofar + (now - last_playing);
245 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
251 /* Selection processing ---------------------------------------------------- */
253 /** @brief Stash the selection of @c ql->view
254 * @param ql Queuelike of interest
255 * @return Hash representing current selection
257 static hash *save_selection(struct queuelike *ql) {
258 hash *h = hash_new(1);
260 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
261 for(const struct queue_entry *q = ql->q; q; q = q->next) {
262 if(gtk_tree_selection_iter_is_selected(ql->selection, iter))
263 hash_add(h, q->id, "", HASH_INSERT);
264 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
269 /** @brief Called from restore_selection() */
270 static int restore_selection_callback(const char *key,
271 void attribute((unused)) *value,
273 const struct queuelike *const ql = u;
275 const struct queue_entry *q;
277 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
278 for(q = ql->q; q && strcmp(key, q->id); q = q->next)
279 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
281 gtk_tree_selection_select_iter(ql->selection, iter);
282 /* There might be gaps if things have disappeared */
286 /** @brief Restore selection of @c ql->view
287 * @param ql Queuelike of interest
288 * @param h Hash representing former selection
290 static void restore_selection(struct queuelike *ql, hash *h) {
291 hash_foreach(h, restore_selection_callback, ql);
294 /* List store maintenance -------------------------------------------------- */
296 /** @brief Update one row of a list store
297 * @param q Queue entry
298 * @param iter Iterator referring to row or NULL to work it out
300 void ql_update_row(struct queue_entry *q,
302 const struct queuelike *const ql = q->ql;
304 D(("ql_update_row"));
305 /* If no iter was supplied, work it out */
306 GtkTreeIter my_iter[1];
308 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter);
309 struct queue_entry *qq;
310 for(qq = ql->q; qq && q != qq; qq = qq->next)
311 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter);
316 /* Update all the columns */
317 for(int col = 0; col < ql->ncolumns; ++col)
318 gtk_list_store_set(ql->store, iter,
319 col, ql->columns[col].value(q,
320 ql->columns[col].data),
324 /** @brief Update the list store
325 * @param ql Queuelike to update
327 * Called when new namepart data is available (and initially). Doesn't change
328 * the rows, just updates the cell values.
330 void ql_update_list_store(struct queuelike *ql) {
331 D(("ql_update_list_store"));
333 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
334 for(struct queue_entry *q = ql->q; q; q = q->next) {
335 ql_update_row(q, iter);
336 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
340 /** @brief Reset the list store
341 * @param ql Queuelike to reset
343 * Throws away all rows and starts again. Used when new queue contents arrives
346 void ql_new_queue(struct queuelike *ql,
347 struct queue_entry *newq) {
349 hash *h = save_selection(ql);
350 /* Clear out old contents */
351 gtk_list_store_clear(ql->store);
352 /* Put in new rows */
354 for(struct queue_entry *q = ql->q; q; q = q->next) {
355 /* Tell every queue entry which queue owns it */
359 gtk_list_store_append(ql->store, iter);
360 /* Update the row contents */
361 ql_update_row(q, iter);
363 restore_selection(ql, h);
364 /* Update menu sensitivity */
368 /** @brief Initialize a @ref queuelike */
369 GtkWidget *init_queuelike(struct queuelike *ql) {
370 D(("init_queuelike"));
371 /* Create the list store */
372 GType *types = xcalloc(ql->ncolumns, sizeof (GType));
373 for(int n = 0; n < ql->ncolumns; ++n)
374 types[n] = G_TYPE_STRING;
375 ql->store = gtk_list_store_newv(ql->ncolumns, types);
377 /* Create the view */
378 ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store));
380 /* Create cell renderers and label columns */
381 for(int n = 0; n < ql->ncolumns; ++n) {
382 GtkCellRenderer *r = gtk_cell_renderer_text_new();
383 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
384 (ql->columns[n].name,
388 gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c);
391 /* The selection should support multiple things being selected */
392 ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view));
393 gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE);
395 /* Remember what the view belongs to */
396 //g_object_set_data(G_OBJECT(ql->view), "type", (void *)&tabtype_queue);
398 g_object_set_data(G_OBJECT(ql->view), "queue", ql);
399 /* Catch button presses */
400 g_signal_connect(ql->view, "button-release-event",
401 G_CALLBACK(ql_button_release), ql);
408 return scroll_widget(ql->view);