- 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(truncate_for_display(namepart(q->track, "display", data),
- config->short_display));
-}
-
-/** @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
- * @param label Label to contain
- * @param style Pointer to style to use
- * @param wp Updated with maximum width (or NULL)
- * @return New widget
- */
-static GtkWidget *wrap_queue_cell(GtkWidget *label,
- GtkStyle *style,
- 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 colors */
- gtk_widget_set_style(bg, style);
- gtk_widget_set_style(label, style);
- 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,
- GtkStyle *style,
- int *wp) {
- GtkWidget *label;
- D(("get_queue_cell %d %d", row, col));
- label = ql->columns[col].widget(ql, q, ql->columns[col].data);
- gtk_misc_set_alignment(GTK_MISC(label), ql->columns[col].xalign, 0);
- return wrap_queue_cell(label, style, wp);
-}
-
-/** @brief Add a padding cell to the end of a row */
-static GtkWidget *get_padding_cell(GtkStyle *style) {
- D(("get_padding_cell"));
- NW(label);
- return wrap_queue_cell(gtk_label_new(""), style, 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 < ql->ncolumns + 1; ++col)
- gtk_widget_set_state(ql->cells[row * (ql->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, (const char **)v.vec);
-}
-
-/* Drag and drop rearrangement --------------------------------------------- */
-
-/** @brief Return nonzero if @p is a draggable row
- *
- * Only tracks in the main queue are draggable (and the currently playing track
- * is not draggable).
- */
-static int draggable_row(const struct queue_entry *q) {
- return q->ql == &ql_queue && q != playing_track;
-}