-/** @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
- *
- * Need this in the middle of the types for NCOLUMNS
- */
-static const struct column columns[] = {
- { "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 */
-#define NCOLUMNS (int)(sizeof columns / sizeof *columns)
-
-/** @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) two of these: @ref ql_queue and @ref ql_recent.
- */
-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[NCOLUMNS + 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 */
-};
-
-static struct queuelike ql_queue; /**< @brief The main queue */
-static struct queuelike ql_recent; /*< @brief Recently-played 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 < 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 ----------------------------------------------------- */
-
-/** @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);
- 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))
- break;
- ql->last_click = q;
- }
- }
- /* Tell every queue entry which queue owns it */
- for(q = newq; q; q = q->next)
- q->ql = ql;
- /* Switch to the new queue */
- ql->q = newq;
- /* Clean up any selected items that have fallen off */
- for(q = ql->q; q; q = q->next)
- selection_live(ql->selection, q->id);
- selection_cleanup(ql->selection);
-}
-
-/** @brief Wrap up a widget for putting into the queue or title */
-static GtkWidget *wrap_queue_cell(GtkWidget *label,
- const char *name,
- int *wp) {
- GtkRequisition req;
- GtkWidget *bg;
-
- D(("wrap_queue_cell"));
- /* Padding should be in the label so there are no gaps in the
- * background */
- gtk_misc_set_padding(GTK_MISC(label), HCELLPADDING, VCELLPADDING);
- /* Event box is just to hold a background color */
- NW(event_box);
- bg = gtk_event_box_new();
- gtk_container_add(GTK_CONTAINER(bg), label);
- if(wp) {
- /* Update maximum width */
- gtk_widget_size_request(label, &req);
- if(req.width > *wp) *wp = req.width;
- }
- /* Set widget names */
- gtk_widget_set_name(bg, name);
- gtk_widget_set_name(label, name);
- return bg;
-}
-
-/** @brief Create the wrapped widget for a cell in the queue display */
-static GtkWidget *get_queue_cell(struct queuelike *ql,
- const struct queue_entry *q,
- int row,
- int col,
- const char *name,
- int *wp) {
- GtkWidget *label;
- D(("get_queue_cell %d %d", row, col));
- label = columns[col].widget(ql, q, columns[col].data);
- gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
- return wrap_queue_cell(label, name, wp);
-}
-
-/** @brief Add a padding cell to the end of a row */
-static GtkWidget *get_padding_cell(const char *name) {
- D(("get_padding_cell"));
- NW(label);
- return wrap_queue_cell(gtk_label_new(""), name, 0);
-}
-
-/* User button press and menu ---------------------------------------------- */
-
-/** @brief Update widget states in order to reflect the selection status */
-static void set_widget_states(struct queuelike *ql) {
- struct queue_entry *q;
- int row, col;
-
- for(q = ql->q, row = 0; q; q = q->next, ++row) {
- for(col = 0; col < NCOLUMNS + 1; ++col)
- gtk_widget_set_state(ql->cells[row * (NCOLUMNS + 1) + col],
- selection_selected(ql->selection, q->id) ?
- GTK_STATE_SELECTED : GTK_STATE_NORMAL);
- }
- /* Might need to change sensitivity of 'Properties' in main menu */
- menu_update(-1);
-}
-
-/** @brief Ordering function for queue entries */
-static int queue_before(const struct queue_entry *a,
- const struct queue_entry *b) {
- while(a && a != b)
- a = a->next;
- return !!a;
-}
-
-/** @brief A button was pressed and released */
-static gboolean queuelike_button_released(GtkWidget attribute((unused)) *widget,
- GdkEventButton *event,
- gpointer user_data) {
- struct queue_entry *q = user_data, *qq;
- struct queuelike *ql = q->ql;
- struct menuiteminfo *mii;
- int n;
-
- /* Might be a release left over from a drag */
- if(ql->swallow_release) {
- ql->swallow_release = 0;
- return FALSE; /* propagate */
- }
-
- if(event->type == GDK_BUTTON_PRESS
- && event->button == 3) {
- /* Right button click.
- * If the current item is not selected then switch the selection to just
- * this item */
- if(q && !selection_selected(ql->selection, q->id)) {
- selection_empty(ql->selection);
- selection_set(ql->selection, q->id, 1);
- ql->last_click = q;
- set_widget_states(ql);
- }
- /* Set the sensitivity of each menu item and (re-)establish the signal
- * handlers */
- for(n = 0; ql->menuitems[n].name; ++n) {
- if(ql->menuitems[n].handlerid)
- g_signal_handler_disconnect(ql->menuitems[n].w,
- ql->menuitems[n].handlerid);
- gtk_widget_set_sensitive(ql->menuitems[n].w,
- ql->menuitems[n].sensitive(ql,
- &ql->menuitems[n],
- q));
- mii = xmalloc(sizeof *mii);
- mii->ql = ql;
- mii->q = q;
- ql->menuitems[n].handlerid = g_signal_connect
- (ql->menuitems[n].w, "activate",
- G_CALLBACK(ql->menuitems[n].activate), mii);
- }
- /* Update the menu according to context */
- gtk_widget_show_all(ql->menu);
- gtk_menu_popup(GTK_MENU(ql->menu), 0, 0, 0, 0,
- event->button, event->time);
- return TRUE; /* hide the click from other widgets */
- }
- if(event->type == GDK_BUTTON_RELEASE
- && event->button == 1) {
- /* no modifiers: select this, unselect everything else, set last click
- * +ctrl: flip selection of this, set last click
- * +shift: select from last click to here, don't set last click
- * +ctrl+shift: select from last click to here, set last click
- */
- switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
- case 0:
- selection_empty(ql->selection);
- selection_set(ql->selection, q->id, 1);
- ql->last_click = q;
- break;
- case GDK_CONTROL_MASK:
- selection_flip(ql->selection, q->id);
- ql->last_click = q;
- break;
- case GDK_SHIFT_MASK:
- case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
- if(ql->last_click) {
- if(!(event->state & GDK_CONTROL_MASK))
- selection_empty(ql->selection);
- selection_set(ql->selection, q->id, 1);
- qq = q;
- if(queue_before(ql->last_click, q))
- while(qq != ql->last_click) {
- qq = qq->prev;
- selection_set(ql->selection, qq->id, 1);
- }
- else
- while(qq != ql->last_click) {
- qq = qq->next;
- selection_set(ql->selection, qq->id, 1);
- }
- if(event->state & GDK_CONTROL_MASK)
- ql->last_click = q;
- }
- break;
- }
- set_widget_states(ql);
- gtk_widget_queue_draw(ql->mainlayout);
- }
- return FALSE; /* propagate */
-}
-
-/** @brief A button was pressed or released on the mainlayout
- *
- * For debugging only at the moment. */
-static gboolean mainlayout_button(GtkWidget attribute((unused)) *widget,
- GdkEventButton attribute((unused)) *event,
- gpointer attribute((unused)) user_data) {
- return FALSE; /* propagate */
-}
-
-/** @brief Select all entries in a queue */
-void queue_select_all(struct queuelike *ql) {
- struct queue_entry *qq;
-
- for(qq = ql->q; qq; qq = qq->next)
- selection_set(ql->selection, qq->id, 1);
- ql->last_click = 0;
- set_widget_states(ql);
-}
-
-/** @brief Pop up properties for selected tracks */
-void queue_properties(struct queuelike *ql) {
- struct vector v;
- const struct queue_entry *qq;
-
- vector_init(&v);
- for(qq = ql->q; qq; qq = qq->next)
- if(selection_selected(ql->selection, qq->id))
- vector_append(&v, (char *)qq->track);
- if(v.nvec)
- properties(v.nvec, v.vec);
-}