diff --git a/dbserver/menuitem.go b/dbserver/menuitem.go index f0315c9..adf408a 100644 --- a/dbserver/menuitem.go +++ b/dbserver/menuitem.go @@ -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() } } diff --git a/dbserver/render.go b/dbserver/render.go index b9bcebe..ed437a5 100644 --- a/dbserver/render.go +++ b/dbserver/render.go @@ -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 } } diff --git a/dbserver/render_override.go b/dbserver/render_override.go index a10a084..0d380dd 100644 --- a/dbserver/render_override.go +++ b/dbserver/render_override.go @@ -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. @@ -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: diff --git a/dbserver/render_override_test.go b/dbserver/render_override_test.go index 329a694..195c896 100644 --- a/dbserver/render_override_test.go +++ b/dbserver/render_override_test.go @@ -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) + } + } +} diff --git a/dbserver/songinfo_title.go b/dbserver/songinfo_title.go index e4f2482..9948c1e 100644 --- a/dbserver/songinfo_title.go +++ b/dbserver/songinfo_title.go @@ -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}, diff --git a/dbserver/sortmenu.go b/dbserver/sortmenu.go index 0015c19..528a179 100644 --- a/dbserver/sortmenu.go +++ b/dbserver/sortmenu.go @@ -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). @@ -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 } diff --git a/dbserver/track.go b/dbserver/track.go index 8a53fef..2316d0d 100644 --- a/dbserver/track.go +++ b/dbserver/track.go @@ -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 } diff --git a/library/import_masterdb.go b/library/import_masterdb.go index c85acae..7e81cee 100644 --- a/library/import_masterdb.go +++ b/library/import_masterdb.go @@ -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 @@ -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, diff --git a/library/rbdate.go b/library/rbdate.go index 638f71a..63b4cdb 100644 --- a/library/rbdate.go +++ b/library/rbdate.go @@ -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 +} diff --git a/library/rbdate_test.go b/library/rbdate_test.go index 7360415..602c400 100644 --- a/library/rbdate_test.go +++ b/library/rbdate_test.go @@ -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) + } +} diff --git a/library/track.go b/library/track.go index ff298d8..f6fa6fe 100644 --- a/library/track.go +++ b/library/track.go @@ -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"` @@ -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") +}