-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLibraryViewManager.cpp
More file actions
500 lines (424 loc) · 14.5 KB
/
LibraryViewManager.cpp
File metadata and controls
500 lines (424 loc) · 14.5 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
#include "LibraryViewManager.h"
#include "ContentColumnView.h"
#include "Debug.h"
#include "MediaItem.h"
#include "Messages.h"
#include "SimpleColumnView.h"
#include <ColumnListView.h>
#include <ColumnTypes.h>
#include <Entry.h>
#include <Path.h>
#include <ScrollView.h>
#include <String.h>
#include <Window.h>
#include <algorithm>
#include <cstdio>
#include <set>
#include <Catalog.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "LibraryViewManager"
static const BString kLabelAllGenre = B_TRANSLATE("Show All Genre");
static const BString kLabelAllArtist = B_TRANSLATE("Show All Artist");
static const BString kLabelAllAlbum = B_TRANSLATE("Show All Album");
static const BString kLabelNoGenre = B_TRANSLATE("No Genre");
static const BString kLabelNoArtist = B_TRANSLATE("No Artist");
static const BString kLabelNoAlbum = B_TRANSLATE("No Album");
/**
* @brief Constructs the LibraryViewManager.
*
* Initializes the four main column views:
* - Genre
* - Artist
* - Album
* - Content (Tracks)
*
* Sets up message targets for selection changes.
*
* @param target The messenger (typically MainWindow) to receive selection
* messages.
*/
LibraryViewManager::LibraryViewManager(BMessenger target) : fTarget(target) {
fGenreView = new SimpleColumnView("genre");
fGenreView->SetSelectionMessage(MSG_SELECTION_CHANGED_GENRE);
fGenreView->SetTarget(fTarget);
fArtistView = new SimpleColumnView("artist");
fArtistView->SetSelectionMessage(MSG_SELECTION_CHANGED_ARTIST);
fArtistView->SetTarget(fTarget);
fAlbumView = new SimpleColumnView("album");
fAlbumView->SetSelectionMessage(MSG_SELECTION_CHANGED_ALBUM);
fAlbumView->SetTarget(fTarget);
fContentView = new ContentColumnView("content");
}
LibraryViewManager::~LibraryViewManager() {
// Views are usually owned by the window's view hierarchy,
// so we don't strictly need to delete them if they are attached.
}
SimpleColumnView *LibraryViewManager::GenreView() const { return fGenreView; }
SimpleColumnView *LibraryViewManager::ArtistView() const { return fArtistView; }
SimpleColumnView *LibraryViewManager::AlbumView() const { return fAlbumView; }
ContentColumnView *LibraryViewManager::ContentView() const {
return fContentView;
}
const std::vector<BString> &LibraryViewManager::ActivePaths() const {
return fActivePaths;
}
void LibraryViewManager::SetActivePaths(const std::vector<BString> &paths) {
fActivePaths = paths;
}
BString LibraryViewManager::SelectedText(SimpleColumnView *v) {
if (!v)
return "";
int32 sel = v->CurrentSelection();
if (sel >= 0) {
return v->ItemAt(sel);
}
return "";
}
BString LibraryViewManager::SelectedData(SimpleColumnView *v) {
if (!v)
return "";
int32 sel = v->CurrentSelection();
if (sel >= 0) {
return v->PathAt(sel);
}
return "";
}
/**
* @brief Resets all filters and clears the content view.
*/
void LibraryViewManager::ResetFilters() {
fGenreView->Clear();
fArtistView->Clear();
fAlbumView->Clear();
fContentView->ClearEntries();
fActivePaths.clear();
}
/**
* @brief Checks if a file path is allowed based on the current mode (Library vs
* Playlist).
*/
bool LibraryViewManager::IsPathAllowed(const BString &filePath,
bool isLibraryMode) const {
return _PathAllowedByMode(filePath, isLibraryMode, fActivePaths);
}
bool LibraryViewManager::_PathAllowedByMode(
const BString &filePath, bool isLibraryMode,
const std::vector<BString> &activePaths) const {
if (isLibraryMode)
return true;
for (const auto &p : activePaths) {
if (p == filePath)
return true;
}
return false;
}
/**
* @brief The core filtering logic.
*
* Updates Genre -> Artist -> Album -> Content views based on the current
* selection. Also handles "smart updates" to avoid flicker if list contents
* haven't changed.
*
* Filtering Process:
* 1. Filter Source Items based on Library/Playlist Mode.
* 2. Build Filter Sets (Genre, Artist, Album -> Years).
* 3. Populate Filter Lists (Genre, Artist, Album).
* 4. Build Final Content List.
* 5. Notify Target (Main Window) about totals.
* 6. Update Content View.
* 7. Prepare Display Items (handling "Alles anzeigen", "Kein..." and
* Disambiguation).
* 8. Smart Update of List Views.
*
* @param allItems The full database of media items.
* @param isLibraryMode True if showing full library, False if showing a
* specific playlist (ActivePaths).
* @param currentPlaylist Name of the current playlist (for UI if needed).
* @param filterText Search filter text.
*/
void LibraryViewManager::UpdateFilteredViews(
const std::vector<MediaItem> &allItems, bool isLibraryMode,
const BString ¤tPlaylist, const BString &filterText) {
BString selGenre = SelectedText(fGenreView);
BString selArtist = SelectedText(fArtistView);
BString selAlbum = SelectedText(fAlbumView);
// Reset downstream selections if upstream selection changed
if (selGenre != fLastSelectedGenre) {
selArtist = "";
selAlbum = "";
} else if (selArtist != fLastSelectedArtist) {
selAlbum = "";
}
fLastSelectedGenre = selGenre;
fLastSelectedArtist = selArtist;
// 1. Filter Source Items based on Library/Playlist Mode
std::vector<MediaItem> sourceItems;
if (isLibraryMode) {
sourceItems = allItems;
} else {
sourceItems.reserve(fActivePaths.size());
for (const auto &p : fActivePaths) {
auto it = std::find_if(allItems.begin(), allItems.end(),
[&](const MediaItem &mi) { return mi.path == p; });
if (it != allItems.end()) {
sourceItems.push_back(*it);
} else {
// Create dummy item for missing files in playlist
MediaItem mi;
mi.path = p;
BPath bp(p.String());
mi.title = bp.Leaf() ? bp.Leaf() : p.String();
BEntry e(bp.Path());
mi.missing = !e.Exists();
sourceItems.push_back(mi);
}
}
}
fContentView->ClearEntries();
// 2. Build Filter Sets
std::set<BString> allGenres;
bool hasUntaggedGenreSrc = false;
std::set<BString> artistsForGenre;
bool hasUntaggedArtistForGenre = false;
// Map AlbumName -> Set of Years (for disambiguation)
std::map<BString, std::set<int32>> albumsForGA;
bool hasUntaggedAlbumForGA = false;
// -- Filter Lambdas --
auto genreOK = [&](const MediaItem &i) {
if (selGenre.IsEmpty() || selGenre == kLabelAllGenre)
return true;
if (selGenre == kLabelNoGenre)
return i.genre.IsEmpty();
return i.genre == selGenre;
};
auto artistOK = [&](const MediaItem &i) {
if (selArtist.IsEmpty() || selArtist == kLabelAllArtist)
return true;
if (selArtist == kLabelNoArtist)
return i.artist.IsEmpty();
return i.artist == selArtist;
};
BString selAlbumData = SelectedData(fAlbumView);
auto albumOK = [&](const MediaItem &i) {
if (selAlbum.IsEmpty() || selAlbum == kLabelAllAlbum)
return true;
if (selAlbum == kLabelNoAlbum)
return i.album.IsEmpty();
// Check for Year disambiguation in hidden data column
if (!selAlbumData.IsEmpty()) {
int32 sep = selAlbumData.FindLast("|");
if (sep > 0) {
BString yearStr = selAlbumData.String() + sep + 1;
int32 targetYear = atoi(yearStr.String());
BString targetName;
selAlbumData.CopyInto(targetName, 0, sep);
if (i.album != targetName)
return false;
if (i.year != targetYear)
return false;
return true;
}
}
// Standard album name match
if (i.album != selAlbum)
return false;
return true;
};
auto textOK = [&](const MediaItem &i) {
if (filterText.IsEmpty())
return true;
if (i.title.IFindFirst(filterText) >= 0)
return true;
if (i.artist.IFindFirst(filterText) >= 0)
return true;
if (i.album.IFindFirst(filterText) >= 0)
return true;
return false;
};
// 3. Populate Filter Lists (Genre, Artist, Album)
for (const auto &it : sourceItems) {
if (!textOK(it))
continue;
if (it.genre.IsEmpty())
hasUntaggedGenreSrc = true;
else
allGenres.insert(it.genre);
if (genreOK(it)) {
if (it.artist.IsEmpty())
hasUntaggedArtistForGenre = true;
else
artistsForGenre.insert(it.artist);
if (artistOK(it)) {
if (it.album.IsEmpty())
hasUntaggedAlbumForGA = true;
else {
albumsForGA[it.album].insert(it.year);
}
}
}
}
// 4. Build Final Content List
std::vector<MediaItem> finalItems;
finalItems.reserve(sourceItems.size());
for (const auto &it : sourceItems) {
if (!(genreOK(it) && artistOK(it) && albumOK(it)))
continue;
if (!textOK(it))
continue;
finalItems.push_back(it);
}
// 5. Notify Target (Main Window) about totals
int32 totalCount = finalItems.size();
int64 totalDuration = 0;
for (const auto &it : finalItems) {
totalDuration += it.duration;
}
if (fTarget.IsValid()) {
BMessage previewMsg(MSG_LIBRARY_PREVIEW);
previewMsg.AddInt32("count", totalCount);
previewMsg.AddInt64("duration", totalDuration);
fTarget.SendMessage(&previewMsg);
}
// 6. Update Content View
fContentView->AddEntries(finalItems);
// 7. Prepare Display Items (handling "All", "No...", and
// Disambiguation)
struct DisplayItem {
BString text;
BString data; // Hidden data (e.g. "AlbumName|2023")
};
std::vector<BString> genreItems;
genreItems.push_back(kLabelAllGenre);
if (hasUntaggedGenreSrc)
genreItems.push_back(kLabelNoGenre);
for (const auto &g : allGenres)
genreItems.push_back(g);
std::vector<BString> artistItems;
artistItems.push_back(kLabelAllArtist);
if (hasUntaggedArtistForGenre)
artistItems.push_back(kLabelNoArtist);
for (const auto &a : artistsForGenre)
artistItems.push_back(a);
std::vector<DisplayItem> albumDisplayItems;
albumDisplayItems.push_back({kLabelAllAlbum, ""});
if (hasUntaggedAlbumForGA)
albumDisplayItems.push_back({kLabelNoAlbum, ""});
for (auto &[name, years] : albumsForGA) {
if (years.empty())
continue;
std::vector<int32> sortedYears(years.begin(), years.end());
std::sort(sortedYears.begin(),
sortedYears.end()); // Ensure years are sorted
if (sortedYears.size() == 1) {
// Single year, no visual disambiguation needed, but store data just in
// case
int32 y = sortedYears[0];
BString data = name;
data << "|" << y;
albumDisplayItems.push_back({name, data});
} else {
// Multiple years for same album name -> Disambiguate
for (int32 y : sortedYears) {
BString displayName = name;
if (y > 0) {
displayName << " [" << y << "]";
} else {
displayName << " [?]";
}
BString data = name;
data << "|" << y;
albumDisplayItems.push_back({displayName, data});
}
}
}
// 8. Smart Update of List Views (Prevent flickering/scrolling reset if
// unchanged)
auto smartUpdateWithData =
[&](SimpleColumnView *view, const std::vector<DisplayItem> &newItems,
const BString ¤tSelText, const BString ¤tSelData) {
bool changed = false;
if (view->CountItems() != (int32)newItems.size()) {
changed = true;
} else {
for (int32 i = 0; i < (int32)newItems.size(); i++) {
if (view->ItemAt(i) != newItems[i].text ||
view->PathAt(i) != newItems[i].data) {
changed = true;
break;
}
}
}
if (!changed)
return;
view->Clear();
for (const auto &item : newItems) {
view->AddItem(item.text, item.data);
}
// Restore Selection
if (!currentSelText.IsEmpty()) {
bool found = false;
// Try matching by data first (more precise)
if (!currentSelData.IsEmpty()) {
for (int32 i = 0; i < view->CountItems(); i++) {
if (view->PathAt(i) == currentSelData) {
view->Select(i);
view->ScrollToSelection();
found = true;
break;
}
}
}
// Fallback to text match
if (!found) {
for (int32 i = 0; i < view->CountItems(); i++) {
if (view->ItemAt(i) == currentSelText) {
view->Select(i);
view->ScrollToSelection();
break;
}
}
}
}
};
auto toDisplay = [](const std::vector<BString> &strs) {
std::vector<DisplayItem> out;
for (const auto &s : strs)
out.push_back({s, ""});
return out;
};
smartUpdateWithData(fGenreView, toDisplay(genreItems), selGenre, "");
smartUpdateWithData(fArtistView, toDisplay(artistItems), selArtist, "");
smartUpdateWithData(fAlbumView, albumDisplayItems, selAlbum, selAlbumData);
}
/**
* @brief Adds a single item to the views incrementally.
* Note: Only used for real-time updates (e.g. during scan).
*/
void LibraryViewManager::AddMediaItem(const MediaItem &item) {
fContentView->AddEntry(item);
auto addUnique = [](SimpleColumnView *v, const BString &val,
const char *emptyLabel) {
BString text = val.IsEmpty() ? BString(emptyLabel) : val;
for (int32 i = 0; i < v->CountItems(); i++) {
if (v->ItemAt(i) == text)
return;
}
v->AddItem(text);
};
addUnique(fGenreView, item.genre, kLabelNoGenre.String());
BString selGenre = SelectedText(fGenreView);
bool genreMatch = (selGenre.IsEmpty() || selGenre == kLabelAllGenre ||
(selGenre == kLabelNoGenre && item.genre.IsEmpty()) ||
selGenre == item.genre);
if (genreMatch) {
addUnique(fArtistView, item.artist, kLabelNoArtist.String());
BString selArtist = SelectedText(fArtistView);
bool artistMatch =
(selArtist.IsEmpty() || selArtist == kLabelAllArtist ||
(selArtist == kLabelNoArtist && item.artist.IsEmpty()) ||
selArtist == item.artist);
if (artistMatch) {
addUnique(fAlbumView, item.album, kLabelNoAlbum.String());
}
}
}