- int (*sensitive)(struct queuelike *ql,
- struct menuitem *m,
- struct queue_entry *q);
- /* Called to determine whether the menu item is usable. Returns TRUE if it
- * should be sensitive and FALSE otherwise. Q points to the queue entry the
- * pointer is over. */
-
- /* State */
- gulong handlerid; /* signal handler ID */
- GtkWidget *w; /* menu item widget */
-};
-
-struct queuelike {
- /* Parameters */
- const char *name; /* queue or recent */
-
- /* Callbacks */
- void (*notify)(void);
- /* Called when an update completes. */
-
- struct queue_entry *(*fixup)(struct queue_entry *q);
- /* Fix up the queue after update, or 0. Q is the list passed back from the
- * server, the return value is assigned to ql->q. */
-
- /* Widgets */
- GtkWidget *mainlayout; /* main layout */
- GtkWidget *mainscroll; /* scroller for main layout */
- GtkWidget *titlelayout; /* title layout */
- GtkWidget *titlecells[NCOLUMNS + 1]; /* title cells */
- GtkWidget **cells; /* all the cells */
- GtkWidget *menu; /* popup menu */
- struct menuitem *menuitems; /* menu items */
- GtkWidget *dragmark; /* drag destination marker */
- GtkWidget **dropzones; /* drag targets */
-
- /* State */
- struct queue_entry *q; /* head of queue */
- struct queue_entry *last_click; /* last click */
- int nrows; /* number of rows */
- int mainrowheight; /* height of one row */
- hash *selection; /* currently selected items */
- int swallow_release; /* swallow button release from drag */
-};
-
-static struct queuelike ql_queue, ql_recent; /* queue and recently played */
-static struct queue_entry *actual_queue; /* actual queue */
-static struct queue_entry *playing_track; /* currenty playing */
-static time_t last_playing = (time_t)-1; /* when last got playing */
-static int namepart_lookups_outstanding;
-static int namepart_completions_deferred; /* # of completions not processed */
-static const struct cache_type cachetype_string = { 3600 };
-static const struct cache_type cachetype_integer = { 3600 };
-static GtkWidget *playing_length_label;
-
-/* Debugging --------------------------------------------------------------- */
-
-#if 0
-static void describe_widget(const char *name, GtkWidget *w, int indent) {
- int ww, wh, wx, wy;
-
- if(name)
- fprintf(stderr, "%*s[%s]: '%s'\n", indent, "",
- name, gtk_widget_get_name(w));
- gdk_window_get_position(w->window, &wx, &wy);
- gdk_drawable_get_size(GDK_DRAWABLE(w->window), &ww, &wh);
- fprintf(stderr, "%*s window %p: %dx%d at %dx%d\n",
- indent, "", w->window, ww, wh, wx, wy);
-}
-
-static void dump_layout(const struct queuelike *ql) {
- GtkWidget *w;
- char s[20];
- int row, col;
- const struct queue_entry *q;
-
- describe_widget("mainscroll", ql->mainscroll, 0);
- describe_widget("mainlayout", ql->mainlayout, 1);
- for(q = ql->q, row = 0; q; q = q->next, ++row)
- for(col = 0; col < NCOLUMNS + 1; ++col)
- if((w = ql->cells[row * (NCOLUMNS + 1) + col])) {
- sprintf(s, "%dx%d", row, col);
- describe_widget(s, w, 2);
- if(GTK_BIN(w)->child)
- describe_widget(0, w, 3);
- }
-}
-#endif
-
-/* Track detail lookup ----------------------------------------------------- */
-
-/* A namepart lookup has completed or failed. */
-static void namepart_completed_or_failed(void) {
- D(("namepart_completed_or_failed"));
- --namepart_lookups_outstanding;
- if(!namepart_lookups_outstanding || namepart_completions_deferred > 24) {
- redisplay_queue(&ql_queue);
- redisplay_queue(&ql_recent);
- namepart_completions_deferred = 0;
- }
-}
-
-/* A namepart lookup has completed. */
-static void namepart_completed(void *v, const char *value) {
- struct callbackdata *cbd = v;
-
- D(("namepart_completed"));
- cache_put(&cachetype_string, cbd->u.key, value);
- ++namepart_completions_deferred;
- namepart_completed_or_failed();
-}
-
-/* A length lookup has completed. */
-static void length_completed(void *v, long l) {
- struct callbackdata *cbd = v;
- long *value;
-
- D(("namepart_completed"));
- value = xmalloc(sizeof *value);
- *value = l;
- cache_put(&cachetype_integer, cbd->u.key, value);
- ++namepart_completions_deferred;
- namepart_completed_or_failed();
-}
-
-/* A length or namepart lookup has failed. */
-static void namepart_protocol_error(
- struct callbackdata attribute((unused)) *cbd,
- int attribute((unused)) code,
- const char *msg) {
- D(("namepart_protocol_error"));
- gtk_label_set_text(GTK_LABEL(report_label), msg);
- namepart_completed_or_failed();
-}
-
-/* Arrange to fill in a namepart cache entry */
-static void namepart_fill(const char *track,
- const char *context,
- const char *part,
- const char *key) {
- struct callbackdata *cbd;
-
- ++namepart_lookups_outstanding;
- cbd = xmalloc(sizeof *cbd);
- cbd->onerror = namepart_protocol_error;
- cbd->u.key = key;
- disorder_eclient_namepart(client, namepart_completed,
- track, context, part, cbd);
-}
-
-/* Look up a namepart. If it is in the cache then just return its value. If
- * not then look it up and arrange for the queues to be updated when its value
- * is available. */
-static const char *namepart(const char *track,
- const char *context,
- const char *part) {
- char *key;
- const char *value;
-
- D(("namepart %s %s %s", track, context, part));
- byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
- context, part, track);
- value = cache_get(&cachetype_string, key);
- if(!value) {
- D(("deferring..."));
- /* stick a value in the cache so we don't issue another lookup if we
- * revisit */
- cache_put(&cachetype_string, key, value = "?");
- namepart_fill(track, context, part, key);
- }
- return value;
-}
-
-/* Called from properties.c when we know a name part has changed */
-void namepart_update(const char *track,
- const char *context,
- const char *part) {
- char *key;
-
- byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
- context, part, track);
- /* Only refetch if it's actually in the cache */
- if(cache_get(&cachetype_string, key))
- namepart_fill(track, context, part, key);
-}
-
-/* Look up a track length. If it is in the cache then just return its value.
- * If not then look it up and arrange for the queues to be updated when its
- * value is available. */
-static long getlength(const char *track) {
- char *key;
- const long *value;
- struct callbackdata *cbd;
- static const long bogus = -1;
-
- D(("getlength %s", track));
- byte_xasprintf(&key, "length track=%s", track);
- value = cache_get(&cachetype_integer, key);
- if(!value) {
- D(("deferring..."));;
- cache_put(&cachetype_integer, key, value = &bogus);
- ++namepart_lookups_outstanding;
- cbd = xmalloc(sizeof *cbd);
- cbd->onerror = namepart_protocol_error;
- cbd->u.key = key;
- disorder_eclient_length(client, length_completed, track, cbd);
- }
- return *value;
-}
-
-/* Column constructors ----------------------------------------------------- */
-
-/* Format the 'when' column */
-static GtkWidget *column_when(const struct queuelike attribute((unused)) *ql,
- const struct queue_entry *q,
- const char attribute((unused)) *data) {
- char when[64];
- struct tm tm;
- time_t t;
-
- D(("column_when"));
- switch(q->state) {
- case playing_isscratch:
- case playing_unplayed:
- case playing_random:
- t = q->expected;
- break;
- case playing_failed:
- case playing_no_player:
- case playing_ok:
- case playing_scratched:
- case playing_started:
- case playing_paused:
- case playing_quitting:
- t = q->played;
- break;
- default:
- t = 0;
- break;
- }
- if(t)
- strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
- else
- when[0] = 0;
- NW(label);
- return gtk_label_new(when);
-}
-
-/* Format the 'who' column */
-static GtkWidget *column_who(const struct queuelike attribute((unused)) *ql,
- const struct queue_entry *q,
- const char attribute((unused)) *data) {
- D(("column_who"));
- NW(label);
- return gtk_label_new(q->submitter ? q->submitter : "");
-}
-
-/* Format one of the track name columns */
-static GtkWidget *column_namepart(const struct queuelike
- attribute((unused)) *ql,
- const struct queue_entry *q,
- const char *data) {
- D(("column_namepart"));
- NW(label);
- return gtk_label_new(namepart(q->track, "display", data));
-}
-
-/* Compute the length field */
-static const char *text_length(const struct queue_entry *q) {
- long l;
- time_t now;
- char *played = 0, *length = 0;
-
- /* Work out what to say for the length */
- l = getlength(q->track);
- if(l > 0)
- byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
- else
- byte_xasprintf(&length, "?:??");
- /* For the currently playing track we want to report how much of the track
- * has been played */
- if(q == playing_track) {
- /* log_state() arranges that we re-get the playing data whenever the
- * pause/resume state changes */
- if(last_state & DISORDER_TRACK_PAUSED)
- l = playing_track->sofar;
- else {
- time(&now);
- l = playing_track->sofar + (now - last_playing);
- }
- byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
- return played;
- } else
- return length;
-}
-
-/* Format the length column */
-static GtkWidget *column_length(const struct queuelike attribute((unused)) *ql,
- const struct queue_entry *q,
- const char attribute((unused)) *data) {
- D(("column_length"));
- if(q == playing_track) {
- assert(!playing_length_label);
- NW(label);
- playing_length_label = gtk_label_new(text_length(q));
- /* Zot playing_length_label when it is destroyed */
- g_signal_connect(playing_length_label, "destroy",
- G_CALLBACK(gtk_widget_destroyed), &playing_length_label);
- return playing_length_label;
- } else {
- NW(label);
- return gtk_label_new(text_length(q));
- }
-
-}
-
-/* Apply a new queue contents, transferring the selection from the old value */
-static void update_queue(struct queuelike *ql, struct queue_entry *newq) {
- struct queue_entry *q;
-
- D(("update_queue"));
- /* Propagate last_click across the change */
- if(ql->last_click) {
- for(q = newq; q; q = q->next) {
- if(!strcmp(q->id, ql->last_click->id))