-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMatcherWindow.cpp
More file actions
576 lines (501 loc) · 17.1 KB
/
MatcherWindow.cpp
File metadata and controls
576 lines (501 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "MatcherWindow.h"
#include "Debug.h"
#include "Messages.h"
#include "TagSync.h"
#include <Bitmap.h>
#include <Button.h>
#include <GroupLayout.h>
#include <GroupView.h>
#include <LayoutBuilder.h>
#include <ListItem.h>
#include <ListView.h>
#include <Path.h>
#include <ScrollView.h>
#include <SplitView.h>
#include <StringItem.h>
#include <StringView.h>
#include <View.h>
#include <algorithm>
#include <cinttypes>
#include <ctype.h>
#include <stdio.h>
#include <vector>
#include <Catalog.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "MatcherWindow"
/** @name Helper Classes */
///@{
/**
* @class TrackListItem
* @brief A list item representing a MusicBrainz track.
*/
class TrackListItem : public BStringItem {
public:
TrackListItem(const char *text, MatcherTrackInfo *info)
: BStringItem(text), fInfo(info) {}
MatcherTrackInfo *TrackInfo() const { return fInfo; }
private:
MatcherTrackInfo *fInfo;
};
/**
* @class DraggableListView
* @brief A list view that supports dragging items to reorder them.
*/
class DraggableListView : public BListView {
public:
DraggableListView(const char *name)
: BListView(name, B_SINGLE_SELECTION_LIST,
B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS) {}
bool InitiateDrag(BPoint point, int32 index, bool wasSelected) override {
if (index < 0)
return false;
BStringItem *item = dynamic_cast<BStringItem *>(ItemAt(index));
if (!item)
return false;
BMessage msg(MSG_DRAG_ITEM);
msg.AddInt32("from_index", index);
BRect frame = ItemFrame(index);
DragMessage(&msg, frame, this);
return true;
}
void MessageReceived(BMessage *msg) override {
if (msg->what == MSG_DRAG_ITEM && msg->WasDropped()) {
int32 fromIndex;
if (msg->FindInt32("from_index", &fromIndex) == B_OK) {
BPoint dropPoint = msg->DropPoint();
ConvertFromScreen(&dropPoint);
int32 toIndex = IndexOf(dropPoint);
if (toIndex < 0) {
if (dropPoint.y > ItemFrame(CountItems() - 1).bottom)
toIndex = CountItems() - 1;
else
toIndex = 0;
}
if (fromIndex != toIndex) {
MoveItem(fromIndex, toIndex);
Select(toIndex);
SelectionChanged();
}
}
} else {
BListView::MessageReceived(msg);
}
}
};
///@}
// --- MatcherWindow Implementation ---
/**
* @brief Constructs the Matcher Window.
*
* @param files List of local file paths to match.
* @param tracks List of MusicBrainz track metadata to match against.
* @param initialMapping Optional pre-calculated mapping (File Index -> Track
* Index).
* @param target The target messenger to receive the final mapping result.
*/
MatcherWindow::MatcherWindow(const std::vector<BString> &files,
const std::vector<MatcherTrackInfo> &tracks,
const std::vector<int> &initialMapping,
BMessenger target)
: BWindow(BRect(100, 100, 800, 650), B_TRANSLATE("Adjust Album Matching"),
B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS),
fFiles(files), fTracks(tracks), fInitialMapping(initialMapping),
fTarget(target) {
DEBUG_PRINT("MatcherWindow: Files: %lu, Tracks: %lu\n", fFiles.size(),
fTracks.size());
_BuildUI();
CenterOnScreen();
Show();
}
MatcherWindow::~MatcherWindow() {
DEBUG_PRINT("MatcherWindow: Destructor called.\n");
}
/**
* @brief Builds the UI layout.
*
* Creates a split view with:
* - Left: List of local files (static).
* - Right: List of MusicBrainz tracks (reorderable).
*/
void MatcherWindow::_BuildUI() {
// Calculate font-relative sizes for DPI scaling
font_height fh;
be_plain_font->GetHeight(&fh);
float fontHeight = fh.ascent + fh.descent + fh.leading;
fFileListView = new BListView("fileList");
fTrackListView = new DraggableListView("trackList");
// Populate File List
for (const auto &f : fFiles) {
BPath p(f.String());
fFileListView->AddItem(new BStringItem(p.Leaf()));
}
std::vector<MatcherTrackInfo *> orderedTracks(fFiles.size(), nullptr);
std::vector<bool> trackUsed(fTracks.size(), false);
for (size_t i = 0; i < fInitialMapping.size(); i++) {
int trackIdx = fInitialMapping[i];
if (trackIdx >= 0 && trackIdx < (int)fTracks.size()) {
if (i < orderedTracks.size()) {
orderedTracks[i] = const_cast<MatcherTrackInfo *>(&fTracks[trackIdx]);
trackUsed[trackIdx] = true;
}
}
}
// Fill gaps
size_t trackIdx = 0;
for (size_t i = 0; i < orderedTracks.size(); i++) {
if (orderedTracks[i] == nullptr) {
while (trackIdx < fTracks.size() && trackUsed[trackIdx]) {
trackIdx++;
}
if (trackIdx < fTracks.size()) {
orderedTracks[i] = const_cast<MatcherTrackInfo *>(&fTracks[trackIdx]);
trackUsed[trackIdx] = true;
}
}
}
// Add mapped tracks to view
for (size_t i = 0; i < orderedTracks.size(); i++) {
BString label;
if (orderedTracks[i]) {
label.SetToFormat("%" PRId32 ". %s (%s)", orderedTracks[i]->index,
orderedTracks[i]->name.String(),
orderedTracks[i]->duration.String());
fTrackListView->AddItem(
new TrackListItem(label.String(), orderedTracks[i]));
} else {
const char *label = "";
if (fTracks.empty())
label = B_TRANSLATE("Error: No data received");
fTrackListView->AddItem(new TrackListItem(label, nullptr));
}
}
// Add remaining (unused) tracks at the bottom
for (size_t i = 0; i < fTracks.size(); i++) {
if (!trackUsed[i]) {
const auto &t = fTracks[i];
BString label;
label.SetToFormat("%" PRId32 ". %s (%s)", t.index, t.name.String(),
t.duration.String());
fTrackListView->AddItem(new TrackListItem(
label.String(), const_cast<MatcherTrackInfo *>(&t)));
}
}
fTrackListView->SetSelectionMessage(new BMessage(MSG_SELECTION_CHANGED));
// Controls
fBtnMoveUp = new BButton("Up", B_TRANSLATE("Up"), new BMessage(MSG_MOVE_UP));
fBtnMoveDown =
new BButton("Down", B_TRANSLATE("Down"), new BMessage(MSG_MOVE_DOWN));
fBtnMoveUp->SetEnabled(false);
fBtnMoveDown->SetEnabled(false);
fBtnApply =
new BButton("Apply", B_TRANSLATE("Apply"), new BMessage(MSG_MATCH_APPLY));
fBtnCancel = new BButton("Cancel", B_TRANSLATE("Cancel"),
new BMessage(MSG_MATCH_CANCEL));
BScrollView *scrollFiles =
new BScrollView("scrollFiles", fFileListView, 0, false, true);
BScrollView *scrollTracks =
new BScrollView("scrollTracks", fTrackListView, 0, false, true);
scrollFiles->SetExplicitPreferredSize(BSize(fontHeight, fontHeight));
scrollTracks->SetExplicitPreferredSize(BSize(fontHeight, fontHeight));
BStringView *instruction = new BStringView(
"inst", B_TRANSLATE("Match tracks (right) to files (left)."));
BFont font(be_bold_font);
instruction->SetFont(&font);
BSplitView *splitView = new BSplitView(B_HORIZONTAL, 10.0f);
BGroupView *leftPane = new BGroupView(B_VERTICAL, 0);
// Spacer to align headers if needed (though layout builder handles most)
BView *spacer =
new BView(BRect(0, 0, 1, 1), "spacer", B_FOLLOW_NONE, B_WILL_DRAW);
spacer->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
float w, h;
fBtnMoveUp->GetPreferredSize(&w, &h);
spacer->SetExplicitMinSize(BSize(0, h));
BLayoutBuilder::Group<>(leftPane)
.AddGroup(B_HORIZONTAL, 5)
.Add(new BStringView("l1", B_TRANSLATE("Files:")))
.Add(spacer)
.AddGlue()
.End()
.Add(scrollFiles);
BGroupView *rightPane = new BGroupView(B_VERTICAL, 0);
BLayoutBuilder::Group<>(rightPane)
.AddGroup(B_HORIZONTAL, 5)
.Add(new BStringView("l2", B_TRANSLATE("MusicBrainz Tracks:")))
.AddGlue()
.Add(fBtnMoveUp)
.Add(fBtnMoveDown)
.End()
.Add(scrollTracks);
splitView->AddChild(leftPane, 1.0f);
splitView->AddChild(rightPane, 1.0f);
BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
.SetInsets(B_USE_WINDOW_INSETS)
.Add(instruction)
.Add(splitView)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.AddGlue()
.Add(fBtnApply)
.Add(fBtnCancel)
.End();
// If no initial mapping was provided or it looks empty, try smart matching?
// The original code called _SmartMatch() at the end, but that overrides the
// logic above. We will keep the original behavior:
_SmartMatch();
}
void MatcherWindow::MessageReceived(BMessage *msg) {
switch (msg->what) {
case MSG_SELECTION_CHANGED: {
int32 sel = fTrackListView->CurrentSelection();
fBtnMoveUp->SetEnabled(sel > 0);
fBtnMoveDown->SetEnabled(sel >= 0 &&
sel < fTrackListView->CountItems() - 1);
break;
}
case MSG_MOVE_UP: {
int32 sel = fTrackListView->CurrentSelection();
if (sel > 0) {
fTrackListView->SwapItems(sel, sel - 1);
fTrackListView->Select(sel - 1);
fTrackListView->ScrollToSelection();
}
break;
}
case MSG_MOVE_DOWN: {
int32 sel = fTrackListView->CurrentSelection();
if (sel >= 0 && sel < fTrackListView->CountItems() - 1) {
fTrackListView->SwapItems(sel, sel + 1);
fTrackListView->Select(sel + 1);
fTrackListView->ScrollToSelection();
}
break;
}
case MSG_MATCH_APPLY:
_Apply();
break;
case MSG_SMART_MATCH:
_SmartMatch();
break;
case MSG_MATCH_CANCEL:
Quit();
break;
default:
BWindow::MessageReceived(msg);
}
}
/**
* @brief Applies the user's mapping and sends the result back to the target.
*/
void MatcherWindow::_Apply() {
BMessage result(MSG_MATCH_RESULT);
for (int32 i = 0; i < fFileListView->CountItems(); i++) {
if (i >= fTrackListView->CountItems()) {
result.AddInt32("track_idx", -1);
continue;
}
TrackListItem *item =
dynamic_cast<TrackListItem *>(fTrackListView->ItemAt(i));
if (!item || !item->TrackInfo()) {
result.AddInt32("track_idx", -1);
} else {
// Find the index of this track info in the original fTracks vector
int foundArrIdx = -1;
const MatcherTrackInfo *target = item->TrackInfo();
if (target >= &fTracks[0] && target < &fTracks[fTracks.size()]) {
foundArrIdx = target - &fTracks[0];
}
result.AddInt32("track_idx", foundArrIdx);
}
result.AddString("file_path", fFiles[i]);
}
fTarget.SendMessage(&result);
Quit();
}
/**
* @brief Helper to calculate Levenshtein distance for fuzzy string matching.
*/
int _LevenshteinDistance(const char *s1, const char *s2) {
int len1 = strlen(s1);
int len2 = strlen(s2);
std::vector<std::vector<int>> d(len1 + 1, std::vector<int>(len2 + 1));
for (int i = 0; i <= len1; i++)
d[i][0] = i;
for (int j = 0; j <= len2; j++)
d[0][j] = j;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
int cost = (tolower(s1[i - 1]) == tolower(s2[j - 1])) ? 0 : 1;
d[i][j] =
std::min({d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost});
}
}
return d[len1][len2];
}
static int ParseDuration(const BString &durStr) {
if (durStr.IsEmpty())
return 0;
int min = 0, sec = 0;
if (sscanf(durStr.String(), "%d:%d", &min, &sec) >= 2) {
return min * 60 + sec;
}
return 0;
}
/**
* @brief Attempts to automatically match files to tracks.
*
* Uses a weighted scoring system to determine the best match:
* 1. Duration Match: Checks if file length matches track length (within
* tolerance).
* 2. Track Number Match: Checks if metadata track number matches MB index.
* 3. Name Similarity: Checks for substring matches or Levenshtein distance
* between filename and track name.
*
* Populates the track list with the best guesses.
*/
void MatcherWindow::_SmartMatch() {
DEBUG_PRINT("MatcherWindow: _SmartMatch (Weighted Scoring) start\\n");
if (fTrackListView) {
for (int32 i = 0; i < fTrackListView->CountItems(); i++) {
delete fTrackListView->ItemAt(i);
}
fTrackListView->MakeEmpty();
}
struct Score {
int score;
size_t fileIdx;
size_t trackIdx;
};
std::vector<Score> allScores;
allScores.reserve(fFiles.size() * fTracks.size());
struct FileInfo {
int durationSec;
int trackNum;
BString cleanName;
};
// Pre-calculate file info (tags, duration, clean name)
std::vector<FileInfo> fileInfos(fFiles.size());
for (size_t i = 0; i < fFiles.size(); i++) {
TagData td;
TagSync::ReadTags(BPath(fFiles[i].String()), td);
fileInfos[i].durationSec = td.lengthSec;
if (td.track > 0) {
fileInfos[i].trackNum = td.track;
} else {
// Fallback: Try reading track number from filename
BString fn = BPath(fFiles[i].String()).Leaf();
const char *s = fn.String();
while (*s && !isdigit(*s))
s++;
fileInfos[i].trackNum = (*s) ? atoi(s) : 0;
}
// Heuristic: Clean filename for similarity check
BString fn = BPath(fFiles[i].String()).Leaf();
int lastDot = fn.FindLast('.');
if (lastDot > 0)
fn.Truncate(lastDot);
const char *p = fn.String();
while (*p &&
(isdigit(*p) || isspace(*p) || *p == '-' || *p == '.' || *p == '_'))
p++;
fileInfos[i].cleanName = p;
}
// Calculate scores matrix
for (size_t i = 0; i < fFiles.size(); i++) {
for (size_t k = 0; k < fTracks.size(); k++) {
int score = 0;
const MatcherTrackInfo &trk = fTracks[k];
const FileInfo &file = fileInfos[i];
// 1. Duration Match
int trkLen = ParseDuration(trk.duration);
if (trkLen > 0 && file.durationSec > 0) {
int diff = std::abs(trkLen - file.durationSec);
if (diff <= 1)
score += 50;
else if (diff <= 3)
score += 30;
else if (diff <= 10)
score -= 20;
else
score -= 50;
}
// 2. Track Number Match
if (file.trackNum > 0 && file.trackNum == trk.index) {
score += 40;
}
// 3. Name Similarity
if (!file.cleanName.IsEmpty() && !trk.name.IsEmpty()) {
if (file.cleanName.IFindFirst(trk.name) >= 0) {
score += 25;
} else {
int dist =
_LevenshteinDistance(file.cleanName.String(), trk.name.String());
int maxLen = std::max(file.cleanName.Length(), trk.name.Length());
if (maxLen > 0) {
float sim = 1.0f - (float)dist / (float)maxLen;
if (sim > 0.8f)
score += 20;
else if (sim > 0.5f)
score += 10;
}
}
}
allScores.push_back({score, i, k});
}
}
// Greedy Assignment based on score
std::sort(allScores.begin(), allScores.end(),
[](const Score &a, const Score &b) { return a.score > b.score; });
std::vector<MatcherTrackInfo *> assignments(fFiles.size(), nullptr);
std::vector<bool> fileAssigned(fFiles.size(), false);
std::vector<bool> trackUsed(fTracks.size(), false);
for (const auto &s : allScores) {
if (s.score < 0)
break;
if (!fileAssigned[s.fileIdx] && !trackUsed[s.trackIdx]) {
assignments[s.fileIdx] =
const_cast<MatcherTrackInfo *>(&fTracks[s.trackIdx]);
fileAssigned[s.fileIdx] = true;
trackUsed[s.trackIdx] = true;
}
}
// Fill gaps sequentially for unmatched files
size_t trackIdx = 0;
for (size_t i = 0; i < fFiles.size(); i++) {
if (!assignments[i]) {
while (trackIdx < fTracks.size() && trackUsed[trackIdx])
trackIdx++;
if (trackIdx < fTracks.size()) {
assignments[i] = const_cast<MatcherTrackInfo *>(&fTracks[trackIdx]);
trackUsed[trackIdx] = true;
}
}
}
// Populate List View
for (size_t i = 0; i < assignments.size(); i++) {
if (assignments[i]) {
BString label;
label.SetToFormat("%" PRId32 ". %s (%s)", assignments[i]->index,
assignments[i]->name.String(),
assignments[i]->duration.String());
fTrackListView->AddItem(
new TrackListItem(label.String(), assignments[i]));
} else {
const char *label = "";
if (fTracks.empty())
label = "Fehler: Keine Daten empfangen";
else
label = B_TRANSLATE("<-- No Match -->");
fTrackListView->AddItem(new TrackListItem(label, nullptr));
}
}
// Add remaining unused tracks at bottom
for (size_t k = 0; k < fTracks.size(); k++) {
if (!trackUsed[k]) {
const auto &t = fTracks[k];
BString label;
label.SetToFormat("%" PRId32 ". %s (%s)", t.index, t.name.String(),
t.duration.String());
fTrackListView->AddItem(new TrackListItem(
label.String(), const_cast<MatcherTrackInfo *>(&t)));
}
}
}