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