Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions dbserver/menuitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,7 @@ func (h *Handler) applyTrackDetail(m *menuItem, t *library.Track, ids *catIDs) {
m.Label2 = t.Comment
case "date_added":
m.ParentID = t.ID
if !t.DateAdded.IsZero() {
m.Label2 = t.DateAdded.Format("2006-01-02")
}
m.Label2 = t.DateAddedString()
}
}

Expand Down
4 changes: 2 additions & 2 deletions dbserver/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ func (h *Handler) handleRenderMenu(msg *proto.DBMessage) []*proto.DBMessage {
}
if override {
item = h.overrideColumn(rows, item, column)
if column == 0x23 && utf16Units(item.Label2) > commentTextMax {
dlog.Debugf("dbserver: render of a comment longer than rekordbox's limit left unanswered, as rekordbox does")
if (column == 0x23 || column == 0x2e) && utf16Units(item.Label2) > commentTextMax {
dlog.Debugf("dbserver: render of a comment or date added longer than rekordbox's limit left unanswered, as rekordbox does")
return nil
}
}
Expand Down
10 changes: 5 additions & 5 deletions dbserver/render_override.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (h *Handler) renderColumn(msg *proto.DBMessage) (byte, bool) {
// rekordbox's limits on a track row's strings (link-spec
// BOUNDARY_INVALID_LIFECYCLE_ORACLE.md "Secondary string limits"): a lookup
// value (artist, album, genre, label, remixer, original artist, key) keeps 127
// characters and a comment 255. A comment that still takes more than 255
// UTF-16 units (characters beyond the BMP take two) leaves the whole render
// unanswered.
// characters, and a comment or date added 255. One of those that still takes
// more than 255 UTF-16 units (characters beyond the BMP take two) leaves the
// whole render unanswered.
const (
lookupTextMax = 127
commentTextMax = 255
commentTextMax = 255 // comments and dates added
)

// capText cuts s to its first n characters.
Expand Down Expand Up @@ -125,7 +125,7 @@ func (h *Handler) overrideColumn(c *trackRows, m *menuItem, col byte) *menuItem
case 0x23: // comments and dates carry the track's ID
r.ParentID, r.Label2 = m.ID, capText(v.comment, commentTextMax)
case 0x2e:
r.ParentID, r.Label2 = m.ID, v.dateAdded
r.ParentID, r.Label2 = m.ID, capText(v.dateAdded, commentTextMax)
case 0x0d:
r.ParentID = uint32(v.tempo)
case 0x0a:
Expand Down
34 changes: 34 additions & 0 deletions dbserver/render_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,37 @@ func TestRenderOverrideStringLimits(t *testing.T) {
t.Errorf("the six-argument render went unanswered")
}
}

// A date added stored as text that isn't a date is served as stored, as
// rekordbox serves its StockDate: in the Date Added column, by the render
// override (cut to 255 characters, and a render whose text still takes more
// than 255 UTF-16 units goes unanswered, as for a comment), and in TRACK
// INFO (link-spec secondary-*-thresholds and secondary-date-added-boundaries).
func TestDateAddedStoredText(t *testing.T) {
lib := library.New()
text := strings.Repeat("d", 126)
lib.AddTrack(&library.Track{ID: 1, Title: "One", DateAddedText: text})
lib.AddTrack(&library.Track{ID: 2, Title: "Two", DateAddedText: strings.Repeat("e", 300)})
col := "date_added"
h := &Handler{lib: lib, menu: detailMenu{detail: &col}}

if got := rowColumn(listRows(t, h, 0), 1).text; got != text {
t.Errorf("Date Added column %q, want the stored text", got)
}
// The override from another column (KEY) extracts the date added.
hk := &Handler{lib: lib, menu: keyColumnMenu{}}
if got := rowColumn(listRows(t, hk, 0, 1, 17), 2).text; got != strings.Repeat("e", 255) {
t.Errorf("override: %d characters, want 255", len(got))
}
lib.AddTrack(&library.Track{ID: 3, Title: "Three", DateAddedText: strings.Repeat("🙂", 128)})
if rows := listRows(t, hk, 0, 1, 17); rows != nil {
t.Errorf("a 256-unit date added was rendered, want no reply")
}

h.Handle(&proto.DBMessage{TxID: 1, Type: proto.DBMsgGetMetadata, Args: []proto.DBArg{proto.ArgI32(0x01020401), proto.ArgI32(1)}})
for _, r := range h.pendingItems {
if r.ItemType == 0x2e && r.Label1 != text {
t.Errorf("TRACK INFO date added %q, want the stored text", r.Label1)
}
}
}
5 changes: 1 addition & 4 deletions dbserver/songinfo_title.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func (h *Handler) newSongInfoTitle(t *library.Track, plainArg0 uint32) *songInfo
}
return h.catID(kind, name)
}
date := ""
if !t.DateAdded.IsZero() {
date = t.DateAdded.Format("2006-01-02")
}
date := t.DateAddedString()
color := uint32(t.ColorID)
return &songInfoTitle{plainArg0: plainArg0, columns: map[uint32]songInfoColumn{
2: {id("artist", t.Artist), t.Artist, 0x07},
Expand Down
6 changes: 2 additions & 4 deletions dbserver/sortmenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ type sortValues struct {
tempo int64 // BPM x 100
rating int64
seconds, bitrate, color, plays int64
dateAdded string // YYYY-MM-DD, "" when undated
dateAdded string // Track.DateAddedString: "" when undated

// pos is the values' index in the library's prebuilt rows, when they
// hold them (trackRows.holds).
Expand All @@ -154,9 +154,7 @@ func libSortValues(t *library.Track) sortValues {
seconds: int64(t.Duration.Seconds()), bitrate: int64(t.Bitrate),
color: int64(t.ColorID), plays: int64(t.PlayCount),
}
if !t.DateAdded.IsZero() {
v.dateAdded = t.DateAdded.Format("2006-01-02")
}
v.dateAdded = t.DateAddedString()
return v
}

Expand Down
4 changes: 1 addition & 3 deletions dbserver/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func (h *Handler) handleGetMetadata(msg *proto.DBMessage) []*proto.DBMessage {
comment = track.Comment
label = track.Label
artworkID = track.ArtID
if !track.DateAdded.IsZero() {
dateAdded = track.DateAdded.Format("2006-01-02")
}
dateAdded = track.DateAddedString()
if track.Duration > 0 {
duration = uint32(track.Duration.Seconds()) * 1000
}
Expand Down
2 changes: 2 additions & 0 deletions library/import_masterdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ func masterDBToLibrary(t *masterDBTrack, path string) *Track {
// rekordbox reads its dates leniently by position and treats a blank or
// malformed one as no date (smart playlists skip such tracks).
dateAdded := ParseRekordboxDate(t.DateAdded)
dateAddedText := storedDateText(t.DateAdded, dateAdded)
dateCreated := ParseRekordboxDate(t.DateCreated)
releaseDate := ParseRekordboxDate(t.ReleaseDate)
// Preserve the rekordbox djmdContent.ID as the library track ID so served
Expand All @@ -639,6 +640,7 @@ func masterDBToLibrary(t *masterDBTrack, path string) *Track {
Comment: t.Comment,
Label: t.Label,
DateAdded: dateAdded,
DateAddedText: dateAddedText,
DateCreated: dateCreated,
ReleaseDate: releaseDate,
Bitrate: t.Bitrate,
Expand Down
14 changes: 14 additions & 0 deletions library/rbdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ func ParseRekordboxDate(s string) time.Time {
}
return t
}

// storedDateText is a stored date's text when it differs from the date it
// parsed to, written as YYYY-MM-DD (cut at a NUL, as rekordbox's strings
// are), or "" when that text is exactly the date or empty: what
// Track.DateAddedText keeps.
func storedDateText(stored string, parsed time.Time) string {
if i := strings.IndexByte(stored, 0); i >= 0 {
stored = stored[:i]
}
if stored == "" || !parsed.IsZero() && parsed.Format("2006-01-02") == stored {
return ""
}
return stored
}
29 changes: 29 additions & 0 deletions library/rbdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,32 @@ func TestParseRekordboxDate(t *testing.T) {
}
}
}

// An import keeps a stored date's text only when it isn't the date it parses
// to, as YYYY-MM-DD: rekordbox serves StockDate as stored, and every
// canonical date needs nothing extra.
func TestStoredDateText(t *testing.T) {
for _, c := range []struct{ stored, want string }{
{"2021-02-02", ""},
{"", ""},
{"2021/02/02", "2021/02/02"}, // parses (separators are ignored) but reads differently
{"dddddddddd", "dddddddddd"}, // no date at all
{"2021-02-02\x00tail", ""}, // cut at the NUL
{"bad\x00tail", "bad"},
} {
if got := storedDateText(c.stored, ParseRekordboxDate(c.stored)); got != c.want {
t.Errorf("storedDateText(%q) = %q, want %q", c.stored, got, c.want)
}
}
tr := &Track{DateAdded: ParseRekordboxDate("2021-02-02")}
if got := tr.DateAddedString(); got != "2021-02-02" {
t.Errorf("canonical date: %q", got)
}
tr.DateAddedText = "2021/02/02"
if got := tr.DateAddedString(); got != "2021/02/02" {
t.Errorf("stored text: %q, want it as stored", got)
}
if got := (&Track{}).DateAddedString(); got != "" {
t.Errorf("undated: %q", got)
}
}
17 changes: 17 additions & 0 deletions library/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type Track struct {
// track has no key relation). It tells an empty-named key from no key.
KeyID uint32 `json:"key_id,omitempty"`
DateAdded time.Time `json:"date_added"`
// DateAddedText is the date added as an imported library stored it, kept
// only when it isn't DateAdded's YYYY-MM-DD: rekordbox serves its
// StockDate as stored, whatever text it holds. See DateAddedString.
DateAddedText string `json:"date_added_text,omitempty"`
// DateCreated is rekordbox's separate DateCreated, from an import; smart
// playlist rules on dateCreated test it (zero when unknown).
DateCreated time.Time `json:"date_created,omitempty"`
Expand Down Expand Up @@ -117,3 +121,16 @@ func TrackColorName(id uint8) string {
}
return ""
}

// DateAddedString is the track's date added as rekordbox serves it: the
// imported text when one was kept, else YYYY-MM-DD, or "" for an undated
// track.
func (t *Track) DateAddedString() string {
switch {
case t.DateAddedText != "":
return t.DateAddedText
case t.DateAdded.IsZero():
return ""
}
return t.DateAdded.Format("2006-01-02")
}
Loading