-
-/** @brief Horizontal padding for queue cells */
-#define HCELLPADDING 4
-
-/** @brief Vertical padding for queue cells */
-#define VCELLPADDING 2
-
-/* Queue management -------------------------------------------------------- */
-
-WT(label);
-WT(event_box);
-WT(menu);
-WT(menu_item);
-WT(layout);
-WT(vbox);
-
-struct queuelike;
-
-static void add_drag_targets(struct queuelike *ql);
-static void remove_drag_targets(struct queuelike *ql);
-static void redisplay_queue(struct queuelike *ql);
-static GtkWidget *column_when(const struct queuelike *ql,
- const struct queue_entry *q,
- const char *data);
-static GtkWidget *column_who(const struct queuelike *ql,
- const struct queue_entry *q,
- const char *data);
-static GtkWidget *column_namepart(const struct queuelike *ql,
- const struct queue_entry *q,
- const char *data);
-static GtkWidget *column_length(const struct queuelike *ql,
- const struct queue_entry *q,
- const char *data);
-static int draggable_row(const struct queue_entry *q);
-
-static const struct tabtype tabtype_queue; /* forward */
-
-static const GtkTargetEntry dragtargets[] = {
- { (char *)"disobedience-queue", GTK_TARGET_SAME_APP, 0 }
-};
-#define NDRAGTARGETS (int)(sizeof dragtargets / sizeof *dragtargets)
-
-/** @brief Definition of a column */
-struct column {
- const char *name; /* Column name */
- GtkWidget *(*widget)(const struct queuelike *ql,
- const struct queue_entry *q,
- const char *data); /* Make a label for this column */
- const char *data; /* Data to pass to widget() */
- gfloat xalign; /* Alignment of the label */
-};
-
-/** @brief Table of columns for queue and recently played list */
-static const struct column maincolumns[] = {
- { "When", column_when, 0, 1 },
- { "Who", column_who, 0, 0 },
- { "Artist", column_namepart, "artist", 0 },
- { "Album", column_namepart, "album", 0 },
- { "Title", column_namepart, "title", 0 },
- { "Length", column_length, 0, 1 }
-};
-
-/** @brief Number of columns in queue and recnetly played list */
-#define NMAINCOLUMNS (int)(sizeof maincolumns / sizeof *maincolumns)
-
-/** @brief Table of columns for recently added tracks */
-static const struct column addedcolumns[] = {
- { "Artist", column_namepart, "artist", 0 },
- { "Album", column_namepart, "album", 0 },
- { "Title", column_namepart, "title", 0 },
- { "Length", column_length, 0, 1 }
-};
-
-/** @brief Number of columns in recently added list */
-#define NADDEDCOLUMNS (int)(sizeof addedcolumns / sizeof *addedcolumns)
-
-/** @brief Maximum number of column in any @ref queuelike */
-#define MAXCOLUMNS (NMAINCOLUMNS > NADDEDCOLUMNS ? NMAINCOLUMNS : NADDEDCOLUMNS)
-
-/** @brief Data passed to menu item activation handlers */
-struct menuiteminfo {
- struct queuelike *ql; /**< @brief which queue we're dealing with */
- struct queue_entry *q; /**< @brief hovered entry or 0 */
-};
-
-/** @brief An item in the queue's popup menu */
-struct queue_menuitem {
- /** @brief Menu item name */
- const char *name;
-
- /** @brief Called to activate the menu item
- *
- * The user data is the queue entry that the pointer was over when the menu
- * popped up. */
- void (*activate)(GtkMenuItem *menuitem,
- gpointer user_data);
-
- /** @brief Called to determine whether the menu item is usable.
- *
- * Returns @c TRUE if it should be sensitive and @c FALSE otherwise. @p q
- * points to the queue entry the pointer is over.
- */
- int (*sensitive)(struct queuelike *ql,
- struct queue_menuitem *m,
- struct queue_entry *q);
-
- /** @brief Signal handler ID */
- gulong handlerid;
-
- /** @brief Widget for menu item */
- GtkWidget *w;
-};
-
-/** @brief A queue-like object
- *
- * There are (currently) three of these: @ref ql_queue, @ref ql_recent and @ref
- * ql_added.
- */
-struct queuelike {
- /** @brief Name of this queue */
- const char *name;
-
- /** @brief Called when an update completes */
- void (*notify)(void);
-
- /** @brief Called to fix up the queue after update
- * @param q The list passed back from the server
- * @return Assigned to @c ql->q
- */
- struct queue_entry *(*fixup)(struct queue_entry *q);
-
- /* Widgets */
- GtkWidget *mainlayout; /**< @brief main layout */
- GtkWidget *mainscroll; /**< @brief scroller for main layout */
- GtkWidget *titlelayout; /**< @brief title layout */
- GtkWidget *titlecells[MAXCOLUMNS + 1]; /**< @brief title cells */
- GtkWidget **cells; /**< @brief all the cells */
- GtkWidget *menu; /**< @brief popup menu */
- struct queue_menuitem *menuitems; /**< @brief menu items */
- GtkWidget *dragmark; /**< @brief drag destination marker */
- GtkWidget **dropzones; /**< @brief drag targets */
-
- /* State */
- struct queue_entry *q; /**< @brief head of queue */
- struct queue_entry *last_click; /**< @brief last click */
- int nrows; /**< @brief number of rows */
- int mainrowheight; /**< @brief height of one row */
- hash *selection; /**< @brief currently selected items */
- int swallow_release; /**< @brief swallow button release from drag */
-
- const struct column *columns; /**< @brief Table of columns */
- int ncolumns; /**< @brief Number of columns */
-};
-
-static struct queuelike ql_queue; /**< @brief The main queue */
-static struct queuelike ql_recent; /*< @brief Recently-played tracks */
-static struct queuelike ql_added; /*< @brief Newly added tracks */
-static struct queue_entry *actual_queue; /**< @brief actual queue */
-static struct queue_entry *playing_track; /**< @brief currenty playing */
-static time_t last_playing = (time_t)-1; /**< @brief 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 < ql->ncolumns + 1; ++col)
- if((w = ql->cells[row * (ql->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 ----------------------------------------------------- */
-
-/** @brief Called when 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);
- redisplay_queue(&ql_added);
- namepart_completions_deferred = 0;
- }
-}
-
-/** @brief Called when 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();
-}
-
-/** @brief Called when 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();
-}
-
-/** @brief Called when 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();
-}
-
-/** @brief 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);
-}
-
-/** @brief 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;
-}
-
-/** @brief Called from @ref disobedience/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);
-}
-
-/** @brief 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 ----------------------------------------------------- */
-
-/** @brief 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);
-}
-
-/** @brief 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 : "");
-}
-
-/** @brief 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));
-}
-
-/** @brief 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;
-}
-
-/** @brief 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));
- }
-
-}
-
-/** @brief 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))