Rewrite queue/recent/added to use native list widget.
[disorder] / disobedience / queue-generic.c
1 /*
2 * This file is part of DisOrder
3 * Copyright (C) 2006-2008 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20 /** @file disobedience/queue-generic.c
21 * @brief Queue widgets
22 *
23 * This file provides contains code shared between all the queue-like
24 * widgets - the queue, the recent list and the added tracks list.
25 *
26 * This code is in the process of being rewritten to use the native list
27 * widget.
28 *
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.
32 *
33 * When new contents turn up we rearrange the list store accordingly.
34 *
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.
38 */
39 #include "disobedience.h"
40 #include "queue-generic.h"
41
42 static struct queuelike *const queuelikes[] = {
43 &ql_queue, &ql_recent, &ql_added
44 };
45 #define NQUEUELIKES (sizeof queuelikes / sizeof *queuelikes)
46
47 /* Track detail lookup ----------------------------------------------------- */
48
49 static int namepart_lookups_outstanding;
50 static const struct cache_type cachetype_string = { 3600 };
51 static const struct cache_type cachetype_integer = { 3600 };
52
53 /** @brief Called when a namepart lookup has completed or failed
54 *
55 * When there are no lookups in flight a redraw is provoked. This might well
56 * provoke further lookups.
57 */
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]);
65 }
66 }
67
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"));
71 if(error) {
72 gtk_label_set_text(GTK_LABEL(report_label), error);
73 value = "?";
74 }
75 const char *key = v;
76
77 cache_put(&cachetype_string, key, value);
78 namepart_completed_or_failed();
79 }
80
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"));
84 if(error) {
85 gtk_label_set_text(GTK_LABEL(report_label), error);
86 l = -1;
87 }
88 const char *key = v;
89 long *value;
90
91 D(("namepart_completed"));
92 value = xmalloc(sizeof *value);
93 *value = l;
94 cache_put(&cachetype_integer, key, value);
95 namepart_completed_or_failed();
96 }
97
98 /** @brief Arrange to fill in a namepart cache entry */
99 static void namepart_fill(const char *track,
100 const char *context,
101 const char *part,
102 const char *key) {
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);
109 }
110
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
116 *
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,
120 const char *context,
121 const char *part) {
122 char *key;
123 const char *value;
124
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);
129 if(!value) {
130 D(("deferring..."));
131 namepart_fill(track, context, part, key);
132 value = "?";
133 }
134 return value;
135 }
136
137 /** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
138 void namepart_update(const char *track,
139 const char *context,
140 const char *part) {
141 char *key;
142
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);
148 }
149
150 /** @brief Look up a track length
151 *
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) {
155 char *key;
156 const long *value;
157
158 D(("getlength %s", track));
159 byte_xasprintf(&key, "length track=%s", track);
160 value = cache_get(&cachetype_integer, key);
161 if(value)
162 return *value;
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);
167 return -1;
168 }
169
170 /* Column formatting -------------------------------------------------------- */
171
172 /** @brief Format the 'when' column */
173 const char *column_when(const struct queue_entry *q,
174 const char attribute((unused)) *data) {
175 char when[64];
176 struct tm tm;
177 time_t t;
178
179 D(("column_when"));
180 switch(q->state) {
181 case playing_isscratch:
182 case playing_unplayed:
183 case playing_random:
184 t = q->expected;
185 break;
186 case playing_failed:
187 case playing_no_player:
188 case playing_ok:
189 case playing_scratched:
190 case playing_started:
191 case playing_paused:
192 case playing_quitting:
193 t = q->played;
194 break;
195 default:
196 t = 0;
197 break;
198 }
199 if(t)
200 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
201 else
202 when[0] = 0;
203 return xstrdup(when);
204 }
205
206 /** @brief Format the 'who' column */
207 const char *column_who(const struct queue_entry *q,
208 const char attribute((unused)) *data) {
209 D(("column_who"));
210 return q->submitter ? q->submitter : "";
211 }
212
213 /** @brief Format one of the track name columns */
214 const char *column_namepart(const struct queue_entry *q,
215 const char *data) {
216 D(("column_namepart"));
217 return namepart(q->track, "display", data);
218 }
219
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"));
224 long l;
225 time_t now;
226 char *played = 0, *length = 0;
227
228 /* Work out what to say for the length */
229 l = getlength(q->track);
230 if(l > 0)
231 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
232 else
233 byte_xasprintf(&length, "?:??");
234 /* For the currently playing track we want to report how much of the track
235 * has been played */
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;
241 else {
242 time(&now);
243 l = playing_track->sofar + (now - last_playing);
244 }
245 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
246 return played;
247 } else
248 return length;
249 }
250
251 /* Selection processing ---------------------------------------------------- */
252
253 /** @brief Stash the selection of @c ql->view
254 * @param ql Queuelike of interest
255 * @return Hash representing current selection
256 */
257 static hash *save_selection(struct queuelike *ql) {
258 hash *h = hash_new(1);
259 GtkTreeIter iter[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);
265 }
266 return h;
267 }
268
269 /** @brief Called from restore_selection() */
270 static int restore_selection_callback(const char *key,
271 void attribute((unused)) *value,
272 void *u) {
273 const struct queuelike *const ql = u;
274 GtkTreeIter iter[1];
275 const struct queue_entry *q;
276
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);
280 if(q)
281 gtk_tree_selection_select_iter(ql->selection, iter);
282 /* There might be gaps if things have disappeared */
283 return 0;
284 }
285
286 /** @brief Restore selection of @c ql->view
287 * @param ql Queuelike of interest
288 * @param h Hash representing former selection
289 */
290 static void restore_selection(struct queuelike *ql, hash *h) {
291 hash_foreach(h, restore_selection_callback, ql);
292 }
293
294 /* List store maintenance -------------------------------------------------- */
295
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
299 */
300 void ql_update_row(struct queue_entry *q,
301 GtkTreeIter *iter) {
302 const struct queuelike *const ql = q->ql;
303
304 D(("ql_update_row"));
305 /* If no iter was supplied, work it out */
306 GtkTreeIter my_iter[1];
307 if(!iter) {
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);
312 if(!qq)
313 return;
314 iter = my_iter;
315 }
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),
321 -1);
322 }
323
324 /** @brief Update the list store
325 * @param ql Queuelike to update
326 *
327 * Called when new namepart data is available (and initially). Doesn't change
328 * the rows, just updates the cell values.
329 */
330 void ql_update_list_store(struct queuelike *ql) {
331 D(("ql_update_list_store"));
332 GtkTreeIter iter[1];
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);
337 }
338 }
339
340 /** @brief Reset the list store
341 * @param ql Queuelike to reset
342 *
343 * Throws away all rows and starts again. Used when new queue contents arrives
344 * from the server.
345 */
346 void ql_new_queue(struct queuelike *ql,
347 struct queue_entry *newq) {
348 D(("ql_new_queue"));
349 hash *h = save_selection(ql);
350 /* Clear out old contents */
351 gtk_list_store_clear(ql->store);
352 /* Put in new rows */
353 ql->q = newq;
354 for(struct queue_entry *q = ql->q; q; q = q->next) {
355 /* Tell every queue entry which queue owns it */
356 q->ql = ql;
357 /* Add a row */
358 GtkTreeIter iter[1];
359 gtk_list_store_append(ql->store, iter);
360 /* Update the row contents */
361 ql_update_row(q, iter);
362 }
363 restore_selection(ql, h);
364 /* Update menu sensitivity */
365 menu_update(-1);
366 }
367
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);
376
377 /* Create the view */
378 ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store));
379
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,
385 r,
386 "text", n,
387 (char *)0);
388 gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c);
389 }
390
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);
394
395 /* Remember what the view belongs to */
396 //g_object_set_data(G_OBJECT(ql->view), "type", (void *)&tabtype_queue);
397 /* TODO tabtype */
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);
402
403 /* TODO style? */
404 /* TODO drag+drop */
405
406 ql->init();
407
408 return scroll_widget(ql->view);
409 }
410
411 /*
412 Local Variables:
413 c-basic-offset:2
414 comment-column:40
415 fill-column:79
416 indent-tabs-mode:nil
417 End:
418 */