chiark / gitweb /
51969957c1b6c4bf91d802211eb0a2794e52adc1
[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  * To do:
40  * - drag and drop queue rearrangement
41  * - display playing row in a different color?
42  */
43 #include "disobedience.h"
44 #include "queue-generic.h"
45
46 static struct queuelike *const queuelikes[] = {
47   &ql_queue, &ql_recent, &ql_added
48 };
49 #define NQUEUELIKES (sizeof queuelikes / sizeof *queuelikes)
50
51 /* Track detail lookup ----------------------------------------------------- */
52
53 static int namepart_lookups_outstanding;
54 static const struct cache_type cachetype_string = { 3600 };
55 static const struct cache_type cachetype_integer = { 3600 };
56
57 /** @brief Called when a namepart lookup has completed or failed
58  *
59  * When there are no lookups in flight a redraw is provoked.  This might well
60  * provoke further lookups.
61  */
62 static void namepart_completed_or_failed(void) {
63   --namepart_lookups_outstanding;
64   if(!namepart_lookups_outstanding) {
65     /* There are no more lookups outstanding, so we update the display */
66     for(unsigned n = 0; n < NQUEUELIKES; ++n)
67       ql_update_list_store(queuelikes[n]);
68   }
69 }
70
71 /** @brief Called when a namepart lookup has completed */
72 static void namepart_completed(void *v, const char *error, const char *value) {
73   D(("namepart_completed"));
74   if(error) {
75     gtk_label_set_text(GTK_LABEL(report_label), error);
76     value = "?";
77   }
78   const char *key = v;
79   
80   cache_put(&cachetype_string, key, value);
81   namepart_completed_or_failed();
82 }
83
84 /** @brief Called when a length lookup has completed */
85 static void length_completed(void *v, const char *error, long l) {
86   D(("length_completed"));
87   if(error) {
88     gtk_label_set_text(GTK_LABEL(report_label), error);
89     l = -1;
90   }
91   const char *key = v;
92   long *value;
93   
94   D(("namepart_completed"));
95   value = xmalloc(sizeof *value);
96   *value = l;
97   cache_put(&cachetype_integer, key, value);
98   namepart_completed_or_failed();
99 }
100
101 /** @brief Arrange to fill in a namepart cache entry */
102 static void namepart_fill(const char *track,
103                           const char *context,
104                           const char *part,
105                           const char *key) {
106   D(("namepart_fill %s %s %s %s", track, context, part, key));
107   /* We limit the total number of lookups in flight */
108   ++namepart_lookups_outstanding;
109   D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
110   disorder_eclient_namepart(client, namepart_completed,
111                             track, context, part, (void *)key);
112 }
113
114 /** @brief Look up a namepart
115  * @param track Track name
116  * @param context Context
117  * @param part Name part
118  * @param lookup If nonzero, will schedule a lookup for unknown values
119  *
120  * If it is in the cache then just return its value.  If not then look it up
121  * and arrange for the queues to be updated when its value is available. */
122 static const char *namepart(const char *track,
123                             const char *context,
124                             const char *part) {
125   char *key;
126   const char *value;
127
128   D(("namepart %s %s %s", track, context, part));
129   byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
130                  context, part, track);
131   value = cache_get(&cachetype_string, key);
132   if(!value) {
133     D(("deferring..."));
134     namepart_fill(track, context, part, key);
135     value = "?";
136   }
137   return value;
138 }
139
140 /** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
141 void namepart_update(const char *track,
142                      const char *context,
143                      const char *part) {
144   char *key;
145
146   byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
147                  context, part, track);
148   /* Only refetch if it's actually in the cache. */
149   if(cache_get(&cachetype_string, key))
150     namepart_fill(track, context, part, key);
151 }
152
153 /** @brief Look up a track length
154  *
155  * If it is in the cache then just return its value.  If not then look it up
156  * and arrange for the queues to be updated when its value is available. */
157 static long getlength(const char *track) {
158   char *key;
159   const long *value;
160
161   D(("getlength %s", track));
162   byte_xasprintf(&key, "length track=%s", track);
163   value = cache_get(&cachetype_integer, key);
164   if(value)
165     return *value;
166   D(("deferring..."));;
167   ++namepart_lookups_outstanding;
168   D(("namepart_lookups_outstanding -> %d\n", namepart_lookups_outstanding));
169   disorder_eclient_length(client, length_completed, track, key);
170   return -1;
171 }
172
173 /* Column formatting -------------------------------------------------------- */
174
175 /** @brief Format the 'when' column */
176 const char *column_when(const struct queue_entry *q,
177                         const char attribute((unused)) *data) {
178   char when[64];
179   struct tm tm;
180   time_t t;
181
182   D(("column_when"));
183   switch(q->state) {
184   case playing_isscratch:
185   case playing_unplayed:
186   case playing_random:
187     t = q->expected;
188     break;
189   case playing_failed:
190   case playing_no_player:
191   case playing_ok:
192   case playing_scratched:
193   case playing_started:
194   case playing_paused:
195   case playing_quitting:
196     t = q->played;
197     break;
198   default:
199     t = 0;
200     break;
201   }
202   if(t)
203     strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
204   else
205     when[0] = 0;
206   return xstrdup(when);
207 }
208
209 /** @brief Format the 'who' column */
210 const char *column_who(const struct queue_entry *q,
211                        const char attribute((unused)) *data) {
212   D(("column_who"));
213   return q->submitter ? q->submitter : "";
214 }
215
216 /** @brief Format one of the track name columns */
217 const char *column_namepart(const struct queue_entry *q,
218                       const char *data) {
219   D(("column_namepart"));
220   return namepart(q->track, "display", data);
221 }
222
223 /** @brief Format the length column */
224 const char *column_length(const struct queue_entry *q,
225                           const char attribute((unused)) *data) {
226   D(("column_length"));
227   long l;
228   time_t now;
229   char *played = 0, *length = 0;
230
231   /* Work out what to say for the length */
232   l = getlength(q->track);
233   if(l > 0)
234     byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
235   else
236     byte_xasprintf(&length, "?:??");
237   /* For the currently playing track we want to report how much of the track
238    * has been played */
239   if(q == playing_track) {
240     /* log_state() arranges that we re-get the playing data whenever the
241      * pause/resume state changes */
242     if(last_state & DISORDER_TRACK_PAUSED)
243       l = playing_track->sofar;
244     else {
245       time(&now);
246       l = playing_track->sofar + (now - last_playing);
247     }
248     byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
249     return played;
250   } else
251     return length;
252 }
253
254 /* Selection processing ---------------------------------------------------- */
255
256 /** @brief Stash the selection of @c ql->view
257  * @param ql Queuelike of interest
258  * @return Hash representing current selection
259  */
260 static hash *save_selection(struct queuelike *ql) {
261   hash *h = hash_new(1);
262   GtkTreeIter iter[1];
263   gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
264   for(const struct queue_entry *q = ql->q; q; q = q->next) {
265     if(gtk_tree_selection_iter_is_selected(ql->selection, iter))
266       hash_add(h, q->id, "", HASH_INSERT);
267     gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
268   }
269   return h;
270 }
271
272 /** @brief Called from restore_selection() */
273 static int restore_selection_callback(const char *key,
274                                       void attribute((unused)) *value,
275                                       void *u) {
276   const struct queuelike *const ql = u;
277   GtkTreeIter iter[1];
278   const struct queue_entry *q;
279   
280   gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
281   for(q = ql->q; q && strcmp(key, q->id); q = q->next)
282     gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
283   if(q) 
284     gtk_tree_selection_select_iter(ql->selection, iter);
285   /* There might be gaps if things have disappeared */
286   return 0;
287 }
288
289 /** @brief Restore selection of @c ql->view
290  * @param ql Queuelike of interest
291  * @param h Hash representing former selection
292  */
293 static void restore_selection(struct queuelike *ql, hash *h) {
294   hash_foreach(h, restore_selection_callback, ql);
295 }
296
297 /* List store maintenance -------------------------------------------------- */
298
299 /** @brief Update one row of a list store
300  * @param q Queue entry
301  * @param iter Iterator referring to row or NULL to work it out
302  */
303 void ql_update_row(struct queue_entry *q,
304                    GtkTreeIter *iter) { 
305   const struct queuelike *const ql = q->ql; 
306
307   D(("ql_update_row"));
308   /* If no iter was supplied, work it out */
309   GtkTreeIter my_iter[1];
310   if(!iter) {
311     gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter);
312     struct queue_entry *qq;
313     for(qq = ql->q; qq && q != qq; qq = qq->next)
314       gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter);
315     if(!qq)
316       return;
317     iter = my_iter;
318   }
319   /* Update all the columns */
320   for(int col = 0; col < ql->ncolumns; ++col)
321     gtk_list_store_set(ql->store, iter,
322                        col, ql->columns[col].value(q,
323                                                    ql->columns[col].data),
324                        -1);
325 }
326
327 /** @brief Update the list store
328  * @param ql Queuelike to update
329  *
330  * Called when new namepart data is available (and initially).  Doesn't change
331  * the rows, just updates the cell values.
332  */
333 void ql_update_list_store(struct queuelike *ql) {
334   D(("ql_update_list_store"));
335   GtkTreeIter iter[1];
336   gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter);
337   for(struct queue_entry *q = ql->q; q; q = q->next) {
338     ql_update_row(q, iter);
339     gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter);
340   }
341 }
342
343 /** @brief Reset the list store
344  * @param ql Queuelike to reset
345  *
346  * Throws away all rows and starts again.  Used when new queue contents arrives
347  * from the server.
348  */
349 void ql_new_queue(struct queuelike *ql,
350                   struct queue_entry *newq) {
351   D(("ql_new_queue"));
352   hash *h = save_selection(ql);
353   /* Clear out old contents */
354   gtk_list_store_clear(ql->store);
355   /* Put in new rows */
356   ql->q = newq;
357   for(struct queue_entry *q = ql->q; q; q = q->next) {
358     /* Tell every queue entry which queue owns it */
359     q->ql = ql;
360     /* Add a row */
361     GtkTreeIter iter[1];
362     gtk_list_store_append(ql->store, iter);
363     /* Update the row contents */
364     ql_update_row(q, iter);
365   }
366   restore_selection(ql, h);
367   /* Update menu sensitivity */
368   menu_update(-1);
369 }
370
371 /** @brief Initialize a @ref queuelike */
372 GtkWidget *init_queuelike(struct queuelike *ql) {
373   D(("init_queuelike"));
374   /* Create the list store */
375   GType *types = xcalloc(ql->ncolumns, sizeof (GType));
376   for(int n = 0; n < ql->ncolumns; ++n)
377     types[n] = G_TYPE_STRING;
378   ql->store = gtk_list_store_newv(ql->ncolumns, types);
379
380   /* Create the view */
381   ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store));
382   gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql->view), TRUE);
383
384   /* Create cell renderers and label columns */
385   for(int n = 0; n < ql->ncolumns; ++n) {
386     GtkCellRenderer *r = gtk_cell_renderer_text_new();
387     if(ql->columns[n].flags & COL_ELLIPSIZE)
388       g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_END, (char *)0);
389     if(ql->columns[n].flags & COL_RIGHT)
390       g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
391     GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
392       (ql->columns[n].name,
393        r,
394        "text", n,
395        (char *)0);
396     g_object_set(c, "resizable", TRUE, (char *)0);
397     if(ql->columns[n].flags & COL_EXPAND)
398       g_object_set(c, "expand", TRUE, (char *)0);
399     gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c);
400   }
401
402   /* The selection should support multiple things being selected */
403   ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view));
404   gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE);
405
406   /* Catch button presses */
407   g_signal_connect(ql->view, "button-press-event",
408                    G_CALLBACK(ql_button_release), ql);
409
410   /* TODO style? */
411   /* TODO drag+drop */
412
413   ql->init();
414
415   GtkWidget *scrolled = scroll_widget(ql->view);
416   g_object_set_data(G_OBJECT(scrolled), "type", (void *)ql_tabtype(ql));
417   return scrolled;
418 }
419
420 /*
421 Local Variables:
422 c-basic-offset:2
423 comment-column:40
424 fill-column:79
425 indent-tabs-mode:nil
426 End:
427 */