forked from flacon/flacon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisc.cpp
1005 lines (810 loc) · 26.4 KB
/
disc.cpp
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* Flacon - audio File Encoder
* https://github.com/flacon/flacon
*
* Copyright: 2012-2013
* Alexander Sokoloff <sokoloff.a@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "disc.h"
#include "track.h"
#include "project.h"
#include "settings.h"
#include "inputaudiofile.h"
#include "formats_in/informat.h"
#include "formats_out/outformat.h"
#include "audiofilematcher.h"
#include "assert.h"
#include <QTextCodec>
#include <QFileInfo>
#include <QStringList>
#include <QDir>
#include <QQueue>
#include <QtAlgorithms>
#include <QDebug>
#include <QLoggingCategory>
#include <QBuffer>
namespace {
Q_LOGGING_CATEGORY(LOG, "Disk")
Q_LOGGING_CATEGORY(LOG_SEARCH_AUDIO_FILES, "SearchAudioFiles")
}
#define COVER_PREVIEW_SIZE 500
/************************************************
************************************************/
Disc::Disc(QObject *parent) :
QObject(parent)
{
}
/************************************************
************************************************/
Disc::Disc(InputAudioFile &audioFile, QObject *parent) :
QObject(parent),
mAudioFile(audioFile)
{
}
/************************************************
************************************************/
Disc::Disc(Cue &cue, QObject *parent) :
QObject(parent)
{
mTracks.reserve(cue.count());
for (const Track &cueTrack : cue) {
Track *track = new Track(cueTrack);
mTracks << track;
connect(track, &Track::tagChanged, this, &Disc::trackChanged);
}
mCueFilePath = cue.fileName();
mCurrentTagsUri = mCueFilePath;
syncTagsFromTracks();
mTagSets[mCurrentTagsUri].setTitle(cue.title());
}
/************************************************
************************************************/
Disc::~Disc()
{
qDeleteAll(mTracks);
}
/************************************************
************************************************/
void Disc::searchCueFile(bool replaceExisting)
{
if (!replaceExisting && !mCueFilePath.isEmpty()) {
return;
}
QStringList audioFiles = audioFilePaths();
if (audioFiles.isEmpty()) {
return;
}
// Embedded CUE ........................
try {
QByteArray embeddedCue = this->audioFiles().first().format()->readEmbeddedCue(this->audioFilePaths().first());
if (!embeddedCue.isEmpty()) {
QBuffer buf(&embeddedCue);
buf.open(QBuffer::ReadOnly);
setCueFile(Cue(&buf, audioFilePaths().first()));
qDebug() << this->isEmpty();
return;
}
}
catch (const FlaconError &err) {
qCWarning(LOG) << "Can't parse embedded cue:" << err.what();
}
// Serarch CUE files ...................
QFileInfo audioFile = QFileInfo(audioFiles.first());
QList<Cue> cues;
QFileInfoList files = audioFile.dir().entryInfoList(QStringList("*.cue"), QDir::Files | QDir::Readable);
foreach (QFileInfo f, files) {
try {
cues << Cue(f.absoluteFilePath());
}
catch (FlaconError &) {
continue; // Just skipping the incorrect files.
}
}
unsigned int bestWeight = 99999;
Cue bestDisc;
foreach (const Cue &cue, cues) {
AudioFileMatcher matcher(cue.fileName(), cue);
if (matcher.containsAudioFile(audioFile.filePath())) {
unsigned int weight = levenshteinDistance(QFileInfo(cue.fileName()).baseName(), audioFile.baseName());
if (weight < bestWeight) {
bestWeight = weight;
bestDisc = cue;
}
}
}
if (!bestDisc.uri().isEmpty()) {
setCueFile(bestDisc);
}
}
/************************************************
************************************************/
void Disc::searchAudioFiles(bool replaceExisting)
{
QString fullPath = QFileInfo(cueFilePath()).filePath();
qCDebug(LOG_SEARCH_AUDIO_FILES) << "Search audio for" << cueFilePath();
qCDebug(LOG_SEARCH_AUDIO_FILES) << "fullPath=" << fullPath;
qCDebug(LOG_SEARCH_AUDIO_FILES) << "Audio files=" << audioFileNames();
AudioFileMatcher matcher(fullPath, Tracks(mTracks));
for (int i = 0; i < audioFiles().count(); ++i) {
if (audioFiles()[i].isNull() || replaceExisting) {
QStringList foundAudio = matcher.audioFiles(i);
for (const QString &file : foundAudio) {
qCDebug(LOG_SEARCH_AUDIO_FILES) << " *" << i << " test audio file " << file;
InputAudioFile newAudio(file);
if (newAudio.isValid()) {
qCDebug(LOG_SEARCH_AUDIO_FILES) << "set audio" << newAudio.filePath();
setAudioFile(newAudio, i);
break;
}
}
}
}
}
/************************************************
************************************************/
void Disc::searchCoverImage(bool replaceExisting)
{
if (!replaceExisting && !mCoverImageFile.isEmpty()) {
return;
}
if (mCueFilePath.isEmpty()) {
return;
}
// Search cover ...................
QString dir = QFileInfo(mCueFilePath).dir().absolutePath();
mCoverImagePreview = QImage();
mCoverImageFile = searchCoverImage(dir);
}
/************************************************
************************************************/
Track *Disc::track(int index) const
{
return mTracks.at(index);
}
/************************************************
*
************************************************/
const Track *Disc::preGapTrack() const
{
if (!mTracks.isEmpty()) {
mPreGapTrack = *mTracks.first();
mPreGapTrack.setCueFileName(mTracks.first()->cueFileName());
}
mPreGapTrack.setTag(TagId::TrackNum, QByteArray("0"));
mPreGapTrack.setTitle("(HTOA)");
return &mPreGapTrack;
}
/************************************************
************************************************/
bool Disc::canConvert() const
{
return this->errors().isEmpty();
}
/************************************************
************************************************/
bool Disc::canDownloadInfo() const
{
return !discId().isEmpty();
}
/************************************************
************************************************/
void Disc::setCueFile(const Cue &cueDisc)
{
InputAudioFileList audioFiles = this->audioFiles();
// If the tracks contain tags from the Internet and the
// tags have been changed, we must save the changes.
syncTagsFromTracks();
mTagSets.remove(mCueFilePath);
int count = cueDisc.count();
mCueFilePath = cueDisc.fileName();
// Remove all tags if number of tracks differ from loaded CUE.
for (auto it = mTagSets.begin(); it != mTagSets.end();) {
if (it.value().count() != count)
it = mTagSets.erase(it);
else
++it;
}
// Sync count of tracks
for (int i = mTracks.count(); i < count; ++i) {
Track *track = new Track();
connect(track, &Track::tagChanged, this, &Disc::trackChanged);
mTracks.append(track);
}
while (mTracks.count() > count)
delete mTracks.takeLast();
for (int t = 0; t < count; ++t) {
*mTracks[t] = cueDisc.at(t);
}
mCurrentTagsUri = mCueFilePath;
syncTagsFromTracks();
mTagSets[mCurrentTagsUri].setTitle(cueDisc.title());
for (int i = 0; i < qMin(audioFiles.count(), mTracks.count()); ++i) {
if (!audioFiles[i].isNull()) {
setAudioFile(InputAudioFile(audioFiles[i]), i);
}
}
project->emitLayoutChanged();
}
/************************************************
*
************************************************/
void Disc::updateDurations(TrackPtrList &tracks)
{
for (int i = 0; i < tracks.count() - 1; ++i) {
Track *track = tracks[i];
uint start = track->cueIndex(1).milliseconds();
uint end = tracks.at(i + 1)->cueIndex(1).milliseconds();
track->mDuration = end > start ? end - start : 0;
}
Track *track = tracks.last();
uint start = track->cueIndex(1).milliseconds();
uint end = track->audioFile().duration();
track->mDuration = end > start ? end - start : 0;
}
/************************************************
*
************************************************/
void Disc::syncTagsFromTracks()
{
if (mTagSets.isEmpty())
return;
Tracks &tags = mTagSets[mCurrentTagsUri];
if (tags.isEmpty()) {
tags.resize(mTracks.count());
tags.setUri(mCurrentTagsUri);
}
for (int i = 0; i < qMin(tags.count(), mTracks.count()); ++i) {
tags[i] = *(mTracks.at(i));
}
}
/************************************************
*
************************************************/
void Disc::syncTagsToTracks()
{
if (mTagSets.isEmpty())
return;
Tracks &tags = mTagSets[mCurrentTagsUri];
assert(tags.count() == mTracks.count());
for (int i = 0; i < mTracks.count(); ++i) {
Track *track = mTracks[i];
const Track &tgs = tags.at(i);
track->blockSignals(true);
track->setTag(TagId::Album, tgs.tagValue(TagId::Album));
track->setTag(TagId::Date, tgs.tagValue(TagId::Date));
track->setTag(TagId::Genre, tgs.tagValue(TagId::Genre));
track->setTag(TagId::Artist, tgs.tagValue(TagId::Artist));
track->setTag(TagId::SongWriter, tgs.tagValue(TagId::SongWriter));
track->setTag(TagId::Title, tgs.tagValue(TagId::Title));
track->setTag(TagId::AlbumArtist, tgs.tagValue(TagId::AlbumArtist));
track->blockSignals(false);
}
}
/************************************************
*
************************************************/
int Disc::distance(const Tracks &other)
{
if (mTracks.isEmpty() || other.empty())
return std::numeric_limits<int>::max();
int res = 0;
QString str1 = mTracks.first()->artist().toUpper().replace("THE ", "");
QString str2 = other.first().artist().toUpper().replace("THE ", "");
res += levenshteinDistance(str1, str2) * 3;
str1 = mTracks.first()->album().toUpper().replace("THE ", "");
str2 = other.first().album().toUpper().replace("THE ", "");
res += levenshteinDistance(str1, str2);
return res;
}
/************************************************
*
************************************************/
bool Disc::isSameTagValue(TagId tagId)
{
if (mTracks.isEmpty())
return false;
auto it = mTracks.constBegin();
QByteArray value = (*it)->tagData(tagId);
++it;
for (; it != mTracks.constEnd(); ++it) {
if ((*it)->tagData(tagId) != value)
return false;
}
return true;
}
/************************************************
************************************************/
QList<TrackPtrList> Disc::tracksByFileTag() const
{
QList<TrackPtrList> res;
if (mTracks.isEmpty()) {
return res;
}
int n = 0;
int b = 0;
while (b < mTracks.count()) {
int e = b;
res.append(TrackPtrList());
TrackPtrList &list = res.last();
QString prev = mTracks[b]->tag(TagId::File);
for (; e < count() && mTracks[e]->tag(TagId::File) == prev; ++e) {
list << mTracks[e];
}
b = e;
++n;
}
return res;
}
/************************************************
************************************************/
InputAudioFileList Disc::audioFiles() const
{
InputAudioFileList res;
if (mTracks.isEmpty() && !mAudioFile.isNull()) {
res << mAudioFile;
return res;
}
for (const TrackPtrList &l : tracksByFileTag()) {
res << l.first()->audioFile();
}
return res;
}
/************************************************
************************************************/
QStringList Disc::audioFileNames() const
{
QStringList res;
for (const InputAudioFile &a : audioFiles()) {
res << a.fileName();
}
return res;
}
/************************************************
************************************************/
QStringList Disc::audioFilePaths() const
{
QStringList res;
for (const InputAudioFile &a : audioFiles()) {
res << a.filePath();
}
return res;
}
/************************************************
************************************************/
void Disc::setAudioFile(const InputAudioFile &file, int fileNum)
{
if (mTracks.isEmpty()) {
assert(fileNum == 0);
mAudioFile = file;
return;
}
QList<TrackPtrList> tracksList = tracksByFileTag();
if (fileNum >= tracksList.count()) {
return;
}
TrackPtrList &tracks = tracksList[fileNum];
for (Track *track : tracks) {
track->setAudioFile(file);
}
updateDurations(tracks);
}
/************************************************
************************************************/
bool Disc::isMultiAudio() const
{
return audioFiles().count() > 0;
}
/************************************************
*
************************************************/
int Disc::startTrackNum() const
{
if (mTracks.isEmpty())
return 0;
return mTracks.first()->trackNum();
}
/************************************************
************************************************/
void Disc::setStartTrackNum(TrackNum value)
{
foreach (auto track, mTracks) {
track->setTrackNum(value++);
}
project->emitDiscChanged(this);
}
/************************************************
************************************************/
QString Disc::codecName() const
{
if (!mTracks.isEmpty())
return mTracks.first()->codecName();
return "";
}
/************************************************
************************************************/
void Disc::setCodecName(const QString &codecName)
{
QString codec = codecName;
if (codecName == CODEC_AUTODETECT) {
UcharDet charDet;
foreach (auto track, mTracks)
charDet << *track;
codec = charDet.textCodecName();
}
foreach (auto track, mTracks) {
track->setCodecName(codec);
}
project->emitDiscChanged(this);
}
/************************************************
************************************************/
QString Disc::tagSetTitle() const
{
if (mCurrentTagsUri.isEmpty())
return "";
const_cast<Disc *>(this)->syncTagsFromTracks();
return mTagSets[mCurrentTagsUri].title();
}
/************************************************
************************************************/
QString Disc::tagsUri() const
{
return mCurrentTagsUri;
}
/************************************************
*
************************************************/
QString Disc::discId() const
{
if (!mTracks.isEmpty())
return mTracks.first()->tag(TagId::DiscId);
return "";
}
/************************************************
*
************************************************/
QString Disc::fileTag() const
{
if (!mTracks.isEmpty())
return mTracks.first()->tag(TagId::File);
return "";
}
/************************************************
*
************************************************/
DiscNum Disc::discNum() const
{
if (!mTracks.isEmpty())
return mTracks.first()->discNum();
return 0;
}
/************************************************
*
************************************************/
DiscNum Disc::discCount() const
{
if (!mTracks.isEmpty())
return mTracks.first()->discCount();
return 0;
}
/************************************************
*
************************************************/
QStringList Disc::warnings() const
{
QStringList res;
for (const InputAudioFile &audioFile : audioFiles()) {
int bps = audioFile.bitsPerSample();
if (Settings::i()->currentProfile().bitsPerSample() != BitsPerSample::AsSourcee) {
bps = qMin(audioFile.bitsPerSample(), int(Settings::i()->currentProfile().bitsPerSample()));
}
if (bps > int(Settings::i()->currentProfile().maxBitPerSample())) {
res << tr("A maximum of %1-bit per sample is supported by this format. This value will be used for encoding.", "Warning message")
.arg(int(Settings::i()->currentProfile().maxBitPerSample()));
}
int sr = audioFile.sampleRate();
if (Settings::i()->currentProfile().sampleRate() != SampleRate::AsSource) {
sr = qMin(sr, int(Settings::i()->currentProfile().sampleRate()));
}
if (sr > int(Settings::i()->currentProfile().maxSampleRate())) {
res << tr("A maximum sample rate of %1 is supported by this format. This value will be used for encoding.", "Warning message")
.arg(int(Settings::i()->currentProfile().maxSampleRate()));
}
if (Settings::i()->currentProfile().gainType() != GainType::Disable && audioFile.channelsCount() > 2) {
res << tr("ReplayGain calculation is not supported for multi-channel audio. The ReplayGain will be disabled for this disk.", "Warning message");
}
}
return res;
}
/************************************************
*
************************************************/
QStringList Disc::errors() const
{
QStringList res;
if (count() < 1) {
res << tr("Cue file not set.");
return res;
}
const QList<TrackPtrList> &audioFileTracks = tracksByFileTag();
for (const TrackPtrList &tracks : audioFileTracks) {
const InputAudioFile &audio = tracks.first()->audioFile();
if (audio.isNull()) {
if (audioFileTracks.count() == 1) {
res << tr("Audio file not set.", "Warning message");
continue;
}
if (tracks.count() == 1) {
res << tr("Audio file not set for track %1.", "Warning message, Placeholders is a track number")
.arg(tracks.first()->trackNum());
continue;
}
res << tr("Audio file not set for tracks %1 to %2.", "Warning message, Placeholders is a track numbers")
.arg(tracks.first()->trackNum())
.arg(tracks.last()->trackNum());
continue;
}
if (!audio.isValid()) {
if (audioFileTracks.count() == 1) {
res << audio.errorString();
continue;
}
}
}
for (const TrackPtrList &tracks : audioFileTracks) {
InputAudioFile audio = tracks.first()->audioFile();
if (audio.isNull() || !audio.isValid()) {
continue;
}
uint duration = 0;
for (int i = 0; i < tracks.count() - 1; ++i) {
duration += tracks[i]->duration();
}
if (audio.duration() <= duration) {
res << tr("Audio file shorter than expected from CUE sheet.");
break;
}
}
return res;
}
/************************************************
*
************************************************/
QVector<Disc::TagSet> Disc::tagSets() const
{
if (mTagSets.isEmpty())
return QVector<TagSet>();
QStringList keys = mTagSets.keys();
keys.removeAll(mCueFilePath);
std::sort(keys.begin(), keys.end());
keys.prepend(mCueFilePath);
QVector<TagSet> res;
foreach (const QString &key, keys) {
const Tracks &tags = mTagSets[key];
TagSet ts;
ts.uri = tags.uri();
ts.name = tags.title();
res << ts;
}
return res;
}
/************************************************
*
************************************************/
void Disc::addTagSet(const Tracks &tags, bool activate)
{
mTagSets[tags.uri()] = tags;
// Sometimes CDDB response contains an additional
// DATA track. For example
// http://freedb.freedb.org/~cddb/cddb.cgi?cmd=CDDB+READ+rock+A20FA70C&hello=a+127.0.0.1+f+0&proto=5
mTagSets[tags.uri()].resize(mTracks.count());
if (activate)
activateTagSet(tags.uri());
}
/************************************************
*
************************************************/
void Disc::activateTagSet(const QString &uri)
{
if (!mTagSets.contains(uri))
return;
syncTagsFromTracks();
mCurrentTagsUri = uri;
syncTagsToTracks();
project->emitLayoutChanged();
}
/************************************************
*
************************************************/
void Disc::addTagSets(const QVector<Tracks> &discs)
{
if (discs.isEmpty())
return;
int minDist = std::numeric_limits<int>::max();
int bestDisc = 0;
for (int i = 0; i < discs.count(); ++i) {
const Tracks &disc = discs.at(i);
if (disc.count() < mTracks.count())
continue;
addTagSet(disc, false);
int n = distance(disc);
if (n < minDist) {
minDist = n;
bestDisc = i;
}
}
activateTagSet(discs.at(bestDisc).uri());
}
/************************************************
************************************************/
void Disc::setCoverImageFile(const QString &fileName)
{
mCoverImageFile = fileName;
mCoverImagePreview = QImage();
}
/************************************************
************************************************/
QImage Disc::coverImagePreview() const
{
if (!mCoverImageFile.isEmpty() && mCoverImagePreview.isNull()) {
mCoverImagePreview = coverImage();
if (!mCoverImagePreview.isNull()) {
mCoverImagePreview.scaled(COVER_PREVIEW_SIZE, COVER_PREVIEW_SIZE, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
}
return mCoverImagePreview;
}
/************************************************
************************************************/
QImage Disc::coverImage() const
{
if (mCoverImageFile.isEmpty())
return QImage();
return QImage(mCoverImageFile);
}
/************************************************
*
************************************************/
QString Disc::discTag(TagId tagId) const
{
if (isEmpty())
return "";
return mTracks.first()->tag(tagId);
}
/************************************************
*
************************************************/
QByteArray Disc::discTagData(TagId tagId) const
{
if (isEmpty())
return QByteArray();
return mTracks.first()->tagData(tagId);
}
/************************************************
*
************************************************/
void Disc::setDiscTag(TagId tagId, const QString &value)
{
foreach (auto track, mTracks)
track->setTag(tagId, value);
}
/************************************************
*
************************************************/
void Disc::setDiscTag(TagId tagId, const QByteArray &value)
{
foreach (auto track, mTracks)
track->setTag(tagId, value);
}
/************************************************
************************************************/
bool compareCoverImages(const QFileInfo &f1, const QFileInfo &f2)
{
const static QStringList order(QStringList()
<< "COVER"
<< "FRONT"
<< "FOLDER");
QString f1up = f1.baseName().toUpper();
QString f2up = f2.baseName().toUpper();
int n1 = 9999;
int n2 = 9999;
for (int i = 0; i < order.count(); ++i) {
const QString &pattern = order.at(i);
// complete match ..................
if (f1up == pattern) {
n1 = i;
}
if (f2up == pattern) {
n2 = i;
}
// filename contains pattern .......
if (f1up.contains(pattern)) {
n1 = i + order.count();
}
if (f2up.contains(pattern)) {
n2 = i + order.count();
}
}
if (n1 != n2)
return n1 < n2;
// If we have 2 files with same name but in different directories,
// we choose the nearest (with the shorter path).
int l1 = f1.absoluteFilePath().length();
int l2 = f2.absoluteFilePath().length();
if (l1 != l2)
return l1 < l2;
return f1.absoluteFilePath() < f2.absoluteFilePath();
}
/************************************************
************************************************/
QStringList Disc::searchCoverImages(const QString &startDir)
{
QFileInfoList files;
QStringList exts;
exts << "*.jpg";
exts << "*.jpeg";
exts << "*.png";
exts << "*.bmp";
exts << "*.tiff";
QQueue<QString> query;
query << startDir;
QSet<QString> processed;
while (!query.isEmpty()) {
QDir dir(query.dequeue());
QFileInfoList dirs = dir.entryInfoList(QDir::Dirs | QDir::Readable | QDir::NoDotAndDotDot);
foreach (QFileInfo d, dirs) {
if (d.isSymLink())
d = QFileInfo(d.symLinkTarget());
if (!processed.contains(d.absoluteFilePath())) {
processed << d.absoluteFilePath();
query << d.absoluteFilePath();
}
}
files << dir.entryInfoList(exts, QDir::Files | QDir::Readable);
}
std::stable_sort(files.begin(), files.end(), compareCoverImages);
QStringList res;
foreach (QFileInfo f, files)
res << f.absoluteFilePath();
return res;
}
/************************************************
************************************************/
QString Disc::searchCoverImage(const QString &startDir)
{
QStringList l = searchCoverImages(startDir);
foreach (QString file, l) {
QImage img(file);
if (!img.isNull())
return file;
}
return "";
}
/************************************************
*
************************************************/
void Disc::trackChanged(TagId tagId)
{
if (mTracks.isEmpty())
return;
if (tagId == TagId::Artist && isSameTagValue(TagId::Artist)) {