Debian Codegraph @

components/extension/tagedit/chooser/chooser_albums.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2023 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include "chooser_albums.h"
14
15 #include "../config.h"
16
17 BoCA::ChooserAlbums::ChooserAlbums() : Chooser("Albums")
18 {
19 dontUpdateAlbumList = False;
20
21 tracks.EnableLocking();
22 albums.EnableLocking();
23
24 list_albums = new ListBox(Point(7, 7), Size(100, 150));
25 list_albums->onSelectEntry.Connect(&ChooserAlbums::OnSelectAlbum, this);
26
27 list_albums->EnableLocking();
28
29 droparea_albums = new DropArea(Point(7, 7), Size(100, 150));
30 droparea_albums->onDropFiles.Connect(&ChooserAlbums::OnDropFiles, this);
31
32 shortcut_previous = new Shortcut(0, Input::Keyboard::KeyUp, list_albums);
33 shortcut_previous->onKeyDown.Connect(&ChooserAlbums::OnShortcutPrevious, this);
34
35 shortcut_next = new Shortcut(0, Input::Keyboard::KeyDown, list_albums);
36 shortcut_next->onKeyDown.Connect(&ChooserAlbums::OnShortcutNext, this);
37
38 shortcut_first = new Shortcut(0, Input::Keyboard::KeyHome, list_albums);
39 shortcut_first->onKeyDown.Connect(&ChooserAlbums::OnShortcutFirst, this);
40
41 shortcut_last = new Shortcut(0, Input::Keyboard::KeyEnd, list_albums);
42 shortcut_last->onKeyDown.Connect(&ChooserAlbums::OnShortcutLast, this);
43
44 Add(list_albums);
45 Add(droparea_albums);
46
47 Add(shortcut_previous);
48 Add(shortcut_next);
49 Add(shortcut_first);
50 Add(shortcut_last);
51
52 onChangeSize.Connect(&ChooserAlbums::OnChangeSize, this);
53
54 Settings *settings = Settings::Get();
55 JobList *joblist = JobList::Get();
56
57 settings->onChangeLanguageSettings.Connect(&ChooserAlbums::OnChangeLanguageSettings, this);
58
59 joblist->onApplicationAddTrack.Connect(&ChooserAlbums::OnApplicationAddTrack, this);
60 joblist->onApplicationModifyTrack.Connect(&ChooserAlbums::OnApplicationModifyTrack, this);
61 joblist->onApplicationRemoveTrack.Connect(&ChooserAlbums::OnApplicationRemoveTrack, this);
62 joblist->onApplicationSelectTrack.Connect(&ChooserAlbums::OnApplicationSelectTrack, this);
63
64 joblist->onApplicationRemoveAllTracks.Connect(&ChooserAlbums::OnApplicationRemoveAllTracks, this);
65 }
66
67 BoCA::ChooserAlbums::~ChooserAlbums()
68 {
69 Settings *settings = Settings::Get();
70 JobList *joblist = JobList::Get();
71
72 settings->onChangeLanguageSettings.Disconnect(&ChooserAlbums::OnChangeLanguageSettings, this);
73
74 joblist->onApplicationAddTrack.Disconnect(&ChooserAlbums::OnApplicationAddTrack, this);
75 joblist->onApplicationModifyTrack.Disconnect(&ChooserAlbums::OnApplicationModifyTrack, this);
76 joblist->onApplicationRemoveTrack.Disconnect(&ChooserAlbums::OnApplicationRemoveTrack, this);
77 joblist->onApplicationSelectTrack.Disconnect(&ChooserAlbums::OnApplicationSelectTrack, this);
78
79 joblist->onApplicationRemoveAllTracks.Disconnect(&ChooserAlbums::OnApplicationRemoveAllTracks, this);
80
81 DeleteObject(list_albums);
82 DeleteObject(droparea_albums);
83
84 DeleteObject(shortcut_previous);
85 DeleteObject(shortcut_next);
86 DeleteObject(shortcut_first);
87 DeleteObject(shortcut_last);
88 }
89
90 /* Called when component canvas size changes.
91 * ----
92 */
93 Void BoCA::ChooserAlbums::OnChangeSize(const Size &nSize)
94 {
95 Rect clientRect = Rect(GetPosition(), GetSize());
96 Size clientSize = Size(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);
97
98 list_albums->SetSize(clientSize - Size(15, 15));
99 droparea_albums->SetSize(clientSize - Size(15, 15));
100 }
101
102 /* Called when application language is changed.
103 * ----
104 */
105 Void BoCA::ChooserAlbums::OnChangeLanguageSettings()
106 {
107 I18n *i18n = I18n::Get();
108
109 i18n->SetContext("Extensions::Tag Editor");
110
111 SetText(i18n->TranslateString("Albums"));
112
113 /* Hide all affected widgets prior to changing
114 * labels to avoid flickering.
115 */
116 Bool prevVisible = IsVisible();
117
118 if (prevVisible) Hide();
119
120 list_albums->RemoveAllTabs();
121
122 list_albums->AddTab(i18n->TranslateString("Artist"), 120);
123 list_albums->AddTab(i18n->TranslateString("Album"));
124 list_albums->AddTab(i18n->TranslateString("Disc"), 50);
125
126 /* Show all widgets again.
127 */
128 if (prevVisible) Show();
129 }
130
131 /* Called when files are dragged and dropped on the album list.
132 * ----
133 * Adds the files to the main program's joblist.
134 */
135 Void BoCA::ChooserAlbums::OnDropFiles(const Array<String> &files)
136 {
137 JobList *joblist = JobList::Get();
138
139 joblist->doAddFiles.Call(files);
140 }
141
142 /* Called when an album entry is selected.
143 * ----
144 * Finds the corresponding album and emits onSelectAlbum.
145 */
146 Void BoCA::ChooserAlbums::OnSelectAlbum()
147 {
148 const Track &album = albums.GetNth(list_albums->GetSelectedEntryNumber());
149
150 if (IsActiveChooser()) onSelectAlbum.Emit(album);
151 }
152
153 /* Called when the up arrow key is pressed.
154 * ----
155 * Selects the previous album.
156 */
157 Void BoCA::ChooserAlbums::OnShortcutPrevious()
158 {
159 if (!IsVisible() || !allowTrackChangeByArrowKey.Call()) return;
160
161 if (list_albums->GetSelectedEntryNumber() == -1) list_albums->SelectNthEntry(list_albums->Length() - 1);
162 else list_albums->SelectNthEntry(list_albums->GetSelectedEntryNumber() - 1);
163 }
164
165 /* Called when the down arrow key is pressed.
166 * ----
167 * Selects the next album.
168 */
169 Void BoCA::ChooserAlbums::OnShortcutNext()
170 {
171 if (!IsVisible() || !allowTrackChangeByArrowKey.Call()) return;
172
173 list_albums->SelectNthEntry(list_albums->GetSelectedEntryNumber() + 1);
174 }
175
176 /* Called when the home key is pressed.
177 * ----
178 * Selects the first album.
179 */
180 Void BoCA::ChooserAlbums::OnShortcutFirst()
181 {
182 if (!IsVisible() || !allowTrackRemoveByDeleteKey.Call()) return;
183
184 list_albums->SelectNthEntry(0);
185 }
186
187 /* Called when the end key is pressed.
188 * ----
189 * Selects the last album.
190 */
191 Void BoCA::ChooserAlbums::OnShortcutLast()
192 {
193 if (!IsVisible() || !allowTrackRemoveByDeleteKey.Call()) return;
194
195 list_albums->SelectNthEntry(list_albums->Length() - 1);
196 }
197
198 /* Called when an album is modified.
199 * ----
200 * Modifies our corresponding entry accordingly.
201 */
202 Void BoCA::ChooserAlbums::OnModifyTrack(const Track &track)
203 {
204 const Config *config = Config::Get();
205
206 Bool clearAlbumArtist = config->GetIntValue(ConfigureTagEdit::ConfigID, "ClearAlbumArtist", False);
207
208 for (Int i = 0; i < list_albums->Length(); i++)
209 {
210 if (albums.Get(list_albums->GetNthEntry(i)->GetHandle()).GetTrackID() != track.GetTrackID()) continue;
211
212 Info info = track.GetInfo();
213
214 /* Consolidate artist information.
215 */
216 Track &origAlbum = albums.GetReference(list_albums->GetNthEntry(i)->GetHandle());
217 const Info &origInfo = origAlbum.GetInfo();
218
219 info.SetOtherInfo(INFO_ALBUMARTIST, info.artist);
220
221 /* Update joblist entry.
222 */
223 I18n *i18n = I18n::Get();
224 String jlEntry = String(info.artist != NIL ? info.artist : i18n->TranslateString("unknown artist")).Append(ListEntry::tabDelimiter)
225 .Append(info.album != NIL ? info.album : i18n->TranslateString("unknown album")).Append(ListEntry::tabDelimiter);
226
227 if (info.disc > 0) jlEntry.Append((info.disc < 10 ? String("0").Append(String::FromInt(info.disc)) : String::FromInt(info.disc)).Append(info.numDiscs > 0 ? String("/").Append(info.numDiscs < 10 ? String("0").Append(String::FromInt(info.numDiscs)) : String::FromInt(info.numDiscs)) : String()));
228
229 list_albums->GetNthEntry(i)->SetText(jlEntry);
230
231 /* Check for changed album art.
232 */
233 Bool picturesChanged = (track.pictures.Length() != origAlbum.pictures.Length());
234
235 if (!picturesChanged)
236 {
237 for (Int i = 0; i < track.pictures.Length(); i++)
238 {
239 if (track.pictures.GetNth(i) == origAlbum.pictures.GetNth(i)) continue;
240
241 picturesChanged = True;
242
243 break;
244 }
245 }
246
247 /* Modify tracks and emit onComponentModifyTrack for each affected track.
248 */
249 dontUpdateAlbumList = True;
250
251 for (Int j = 0; j < tracks.Length(); j++)
252 {
253 Track &mTrack = tracks.GetNthReference(j);
254
255 if (!IsAlbumIdentical(mTrack, origAlbum)) continue;
256
257 Info mTrackInfo = mTrack.GetInfo();
258
259 /* Update basic info.
260 */
261 if (!clearAlbumArtist) mTrackInfo.SetOtherInfo(INFO_ALBUMARTIST, info.artist);
262
263 if (info.artist != origInfo.artist)
264 {
265 if (mTrackInfo.artist == mTrackInfo.GetOtherInfo(INFO_ALBUMARTIST) && clearAlbumArtist) mTrackInfo.SetOtherInfo(INFO_ALBUMARTIST, NIL);
266
267 if (mTrackInfo.artist == origInfo.artist) mTrackInfo.artist = info.artist;
268 else mTrackInfo.SetOtherInfo(INFO_ALBUMARTIST, info.artist);
269
270 if (mTrackInfo.artist == mTrackInfo.GetOtherInfo(INFO_ALBUMARTIST) && clearAlbumArtist) mTrackInfo.SetOtherInfo(INFO_ALBUMARTIST, NIL);
271 }
272
273 if (info.album != origInfo.album) mTrackInfo.album = info.album;
274 if (info.genre != origInfo.genre) mTrackInfo.genre = info.genre;
275 if (info.year != origInfo.year) mTrackInfo.year = info.year;
276 if (info.comment != origInfo.comment) mTrackInfo.comment = info.comment;
277
278 if (info.numTracks != origInfo.numTracks) mTrackInfo.numTracks = info.numTracks;
279
280 if (info.disc != origInfo.disc) mTrackInfo.disc = info.disc;
281 if (info.numDiscs != origInfo.numDiscs) mTrackInfo.numDiscs = info.numDiscs;
282
283 if (info.label != origInfo.label) mTrackInfo.label = info.label;
284
285 /* Update other text info.
286 */
287 Int index = 0;
288 static String keys[] = { INFO_BAND, INFO_CONDUCTOR, INFO_COMPOSER, INFO_LYRICIST, INFO_REMIXER,
289 INFO_ORIG_ARTIST, INFO_ORIG_LYRICIST, INFO_ORIG_YEAR, INFO_MEDIATYPE,
290 INFO_CATALOGNUMBER, INFO_BARCODE, INFO_WEB_ARTIST, INFO_WEB_PUBLISHER,
291 INFO_WEB_RADIO, INFO_WEB_SOURCE, INFO_WEB_COPYRIGHT, INFO_WEB_COMMERCIAL, NIL };
292
293 while (keys[index] != NIL)
294 {
295 String key = keys[index++];
296
297 if (info.GetOtherInfo(key) == origInfo.GetOtherInfo(key)) continue;
298
299 mTrackInfo.SetOtherInfo(key, info.GetOtherInfo(key));
300 }
301
302 mTrack.SetInfo(mTrackInfo);
303
304 /* Update album art.
305 */
306 if (picturesChanged)
307 {
308 mTrack.pictures.RemoveAll();
309
310 foreach (const Picture &picture, track.pictures) mTrack.pictures.Add(picture);
311 }
312
313 JobList::Get()->onComponentModifyTrack.Emit(mTrack);
314 }
315
316 origAlbum = track;
317 origAlbum.SetInfo(info);
318
319 dontUpdateAlbumList = False;
320
321 /* Reselect album to let non-active editors know about changes.
322 */
323 OnSelectAlbum();
324
325 break;
326 }
327 }
328
329 /* Called when a track is added to the application joblist.
330 * ----
331 * Adds entries to tracks and list_tracks.
332 */
333 Void BoCA::ChooserAlbums::OnApplicationAddTrack(const Track &track)
334 {
335 tracks.Add(track);
336
337 AddToAlbumList(track);
338 }
339
340 /* Called when a track is modified by the application.
341 * ----
342 * Modifies our corresponding entry accordingly.
343 */
344 Void BoCA::ChooserAlbums::OnApplicationModifyTrack(const Track &track)
345 {
346 Bool first = True;
347
348 for (Int i = 0; i < tracks.Length(); i++)
349 {
350 if (tracks.GetNth(i).GetTrackID() != track.GetTrackID())
351 {
352 if (IsAlbumIdentical(tracks.GetNth(i), track)) first = False;
353
354 continue;
355 }
356
357 Track trackAlbum = tracks.GetNth(i);
358
359 tracks.GetNthReference(i) = track;
360
361 AddToAlbumList(track);
362
363 foreach (const Track &album, albums)
364 {
365 if (!IsAlbumIdentical(album, trackAlbum)) continue;
366
367 RemoveFromAlbumList(album);
368 }
369
370 if (first && !dontUpdateAlbumList)
371 {
372 UpdateAlbum(track);
373 ReselectEntry();
374 }
375
376 break;
377 }
378
379 if (IsActiveChooser() && list_albums->GetSelectedEntry() == NIL) onSelectNone.Emit();
380 }
381
382 /* Called when a track is removed from the application joblist.
383 * ----
384 * Removes our corresponding entry from tracks and list_tracks.
385 */
386 Void BoCA::ChooserAlbums::OnApplicationRemoveTrack(const Track &track)
387 {
388 for (Int i = 0; i < tracks.Length(); i++)
389 {
390 if (tracks.GetNth(i).GetTrackID() != track.GetTrackID()) continue;
391
392 tracks.RemoveNth(i);
393
394 break;
395 }
396
397 RemoveFromAlbumList(track);
398
399 if (IsActiveChooser() && (list_albums->GetSelectedEntry() == NIL || list_albums->Length() == 0)) onSelectNone.Emit();
400 }
401
402 /* Called when a track is selected in the application joblist.
403 * ----
404 * Finds and selects the corresponding entry in our album list.
405 */
406 Void BoCA::ChooserAlbums::OnApplicationSelectTrack(const Track &track)
407 {
408 for (Int i = 0; i < list_albums->Length(); i++)
409 {
410 if (!IsAlbumIdentical(albums.Get(list_albums->GetNthEntry(i)->GetHandle()), track)) continue;
411
412 if (list_albums->GetSelectedEntryNumber() != i) list_albums->SelectNthEntry(i);
413
414 break;
415 }
416 }
417
418 /* Called when all tracks are removed from the application joblist at once.
419 * ----
420 * Clears tracks and list_tracks.
421 */
422 Void BoCA::ChooserAlbums::OnApplicationRemoveAllTracks()
423 {
424 tracks.RemoveAll();
425
426 if (IsActiveChooser()) onSelectNone.Emit();
427
428 UpdateAlbumList();
429 }
430
431 /* Called when the currently selected entry needs to be selected again.
432 * ----
433 */
434 Void BoCA::ChooserAlbums::ReselectEntry()
435 {
436 if (list_albums->GetSelectedEntry() == NIL) return;
437
438 OnSelectAlbum();
439 }
440
441 /* Checks if the album of two tracks is the same.
442 * ----
443 */
444 Bool BoCA::ChooserAlbums::IsAlbumIdentical(const Track &track1, const Track &track2)
445 {
446 const Info &info1 = track1.GetInfo();
447 const Info &info2 = track2.GetInfo();
448
449 String artist1 = info1.artist;
450 String artist2 = info2.artist;
451
452 if (info1.HasOtherInfo(INFO_ALBUMARTIST)) artist1 = info1.GetOtherInfo(INFO_ALBUMARTIST);
453 if (info2.HasOtherInfo(INFO_ALBUMARTIST)) artist2 = info2.GetOtherInfo(INFO_ALBUMARTIST);
454
455 if ((artist1 == NIL || artist2 == NIL || artist1 == artist2) && info1.album == info2.album && ((info1.disc <= 0 && info2.disc <= 0) || info1.disc == info2.disc)) return True;
456
457 return False;
458 }
459
460 Void BoCA::ChooserAlbums::AddToAlbumList(const Track &track)
461 {
462 if (dontUpdateAlbumList) return;
463
464 Bool found = False;
465
466 foreach (const Track &album, albums)
467 {
468 if (IsAlbumIdentical(album, track)) { found = True; break; }
469 }
470
471 if (found) return;
472
473 Track album;
474 Info albumInfo = album.GetInfo();
475 const Info &trackInfo = track.GetInfo();
476
477 /* Copy basic info.
478 */
479 albumInfo.artist = trackInfo.GetOtherInfo(INFO_ALBUMARTIST) == NIL ? trackInfo.artist : trackInfo.GetOtherInfo(INFO_ALBUMARTIST);
480 albumInfo.album = trackInfo.album;
481 albumInfo.disc = trackInfo.disc;
482 albumInfo.numDiscs = trackInfo.numDiscs;
483
484 /* Copy album artist.
485 */
486 foreach (const String &pair, trackInfo.other)
487 {
488 String key = pair.Head(pair.Find(":"));
489
490 if (key == INFO_ALBUMARTIST) albumInfo.other.Add(pair);
491 }
492
493 album.SetInfo(albumInfo);
494
495 /* Add to album list.
496 */
497 I18n *i18n = I18n::Get();
498 const Info &info = album.GetInfo();
499 String jlEntry = String(info.artist != NIL ? info.artist : i18n->TranslateString("unknown artist")).Append(ListEntry::tabDelimiter)
500 .Append(info.album != NIL ? info.album : i18n->TranslateString("unknown album")).Append(ListEntry::tabDelimiter);
501
502 if (info.disc > 0) jlEntry.Append((info.disc < 10 ? String("0").Append(String::FromInt(info.disc)) : String::FromInt(info.disc)).Append(info.numDiscs > 0 ? String("/").Append(info.numDiscs < 10 ? String("0").Append(String::FromInt(info.numDiscs)) : String::FromInt(info.numDiscs)) : String()));
503
504 albums.Add(album, list_albums->AddEntry(jlEntry)->GetHandle());
505
506 UpdateAlbum(track);
507 }
508
509 Void BoCA::ChooserAlbums::RemoveFromAlbumList(const Track &album)
510 {
511 if (dontUpdateAlbumList) return;
512
513 Bool found = False;
514
515 foreach (const Track &track, tracks)
516 {
517 if (IsAlbumIdentical(album, track)) { found = True; break; }
518 }
519
520 if (found) return;
521
522 for (Int i = 0; i < albums.Length(); i++)
523 {
524 if (!IsAlbumIdentical(album, albums.GetNth(i))) continue;
525
526 albums.RemoveNth(i);
527 list_albums->Remove(list_albums->GetNthEntry(i));
528
529 break;
530 }
531 }
532
533 Void BoCA::ChooserAlbums::UpdateAlbumList()
534 {
535 if (dontUpdateAlbumList) return;
536
537 foreach (const Track &track, tracks) AddToAlbumList(track);
538 foreachreverse (const Track &album, albums) RemoveFromAlbumList(album);
539 }
540
541 Void BoCA::ChooserAlbums::UpdateAlbum(const Track &track)
542 {
543 if (dontUpdateAlbumList) return;
544
545 foreach (Track &album, albums)
546 {
547 if (!IsAlbumIdentical(album, track)) continue;
548
549 Info albumInfo = album.GetInfo();
550 const Info &trackInfo = track.GetInfo();
551
552 /* Copy basic info.
553 */
554 albumInfo.artist = trackInfo.GetOtherInfo(INFO_ALBUMARTIST) == NIL ? trackInfo.artist : trackInfo.GetOtherInfo(INFO_ALBUMARTIST);
555 albumInfo.album = trackInfo.album;
556 albumInfo.genre = trackInfo.genre;
557 albumInfo.year = trackInfo.year;
558 albumInfo.comment = trackInfo.comment;
559
560 albumInfo.numTracks = trackInfo.numTracks;
561
562 albumInfo.disc = trackInfo.disc;
563 albumInfo.numDiscs = trackInfo.numDiscs;
564
565 albumInfo.label = trackInfo.label;
566
567 /* Copy other text info.
568 */
569 foreach (const String &pair, trackInfo.other)
570 {
571 String key = pair.Head(pair.Find(":"));
572
573 if (key == INFO_ALBUMARTIST || key == INFO_BAND ||
574 key == INFO_CONDUCTOR || key == INFO_COMPOSER ||
575 key == INFO_LYRICIST || key == INFO_REMIXER ||
576
577 key == INFO_MEDIATYPE || key == INFO_CATALOGNUMBER ||
578 key == INFO_BARCODE ||
579
580 key == INFO_ORIG_ARTIST || key == INFO_ORIG_ALBUM ||
581 key == INFO_ORIG_LYRICIST || key == INFO_ORIG_YEAR ||
582
583 key == INFO_WEB_ARTIST || key == INFO_WEB_PUBLISHER ||
584 key == INFO_WEB_RADIO || key == INFO_WEB_SOURCE ||
585 key == INFO_WEB_COPYRIGHT || key == INFO_WEB_COMMERCIAL) albumInfo.other.Add(pair);
586 }
587
588 album.SetInfo(albumInfo);
589
590 /* Copy album art.
591 */
592 album.pictures.RemoveAll();
593
594 foreach (const Picture &picture, track.pictures) album.pictures.Add(picture);
595
596 break;
597 }
598 }