File indexing completed on 2024-05-05 04:51:47
0001 /* 0002 SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "k3bmusicbrainzjob.h" 0007 #include "k3bmusicbrainztrackloopupjob.h" 0008 0009 #include "k3baudiotrack.h" 0010 #include "k3baudiodatasource.h" 0011 #include "k3bsimplejobhandler.h" 0012 0013 #include <KLocalizedString> 0014 #include <KMessageBox> 0015 0016 #include <QInputDialog> 0017 0018 0019 class K3b::MusicBrainzJob::Private 0020 { 0021 public: 0022 QList<K3b::AudioTrack*> tracks; 0023 int currentTrackIndex; 0024 bool canceled; 0025 K3b::MusicBrainzTrackLookupJob* mbTrackLookupJob; 0026 }; 0027 0028 0029 // cannot use this as parent for the K3b::SimpleJobHandler since this has not been constructed yet 0030 K3b::MusicBrainzJob::MusicBrainzJob( QWidget* parent ) 0031 : K3b::Job( new K3b::SimpleJobHandler( 0 ), parent ), 0032 d( new Private() ) 0033 { 0034 d->canceled = false; 0035 d->mbTrackLookupJob = new K3b::MusicBrainzTrackLookupJob( this, this ); 0036 0037 connect( d->mbTrackLookupJob, SIGNAL(percent(int)), this, SIGNAL(subPercent(int)), Qt::QueuedConnection ); 0038 connect( d->mbTrackLookupJob, SIGNAL(percent(int)), this, SLOT(slotTrmPercent(int)), Qt::QueuedConnection ); 0039 connect( d->mbTrackLookupJob, SIGNAL(finished(bool)), this, SLOT(slotMbJobFinished(bool)), Qt::QueuedConnection ); 0040 connect( d->mbTrackLookupJob, SIGNAL(infoMessage(QString,int)), this, SIGNAL(infoMessage(QString,int)), Qt::QueuedConnection ); 0041 } 0042 0043 0044 K3b::MusicBrainzJob::~MusicBrainzJob() 0045 { 0046 delete jobHandler(); 0047 delete d; 0048 } 0049 0050 0051 bool K3b::MusicBrainzJob::hasBeenCanceled() const 0052 { 0053 return d->canceled; 0054 } 0055 0056 0057 void K3b::MusicBrainzJob::setTracks( const QList<K3b::AudioTrack*>& tracks ) 0058 { 0059 d->tracks = tracks; 0060 } 0061 0062 0063 void K3b::MusicBrainzJob::start() 0064 { 0065 jobStarted(); 0066 0067 d->canceled = false; 0068 d->currentTrackIndex = 0; 0069 0070 d->mbTrackLookupJob->setAudioTrack( d->tracks.first() ); 0071 d->mbTrackLookupJob->start(); 0072 } 0073 0074 0075 void K3b::MusicBrainzJob::cancel() 0076 { 0077 d->canceled = true; 0078 d->mbTrackLookupJob->cancel(); 0079 } 0080 0081 0082 void K3b::MusicBrainzJob::slotTrmPercent( int p ) 0083 { 0084 // the easy way (inaccurate) 0085 emit percent( (100*d->currentTrackIndex + p) / d->tracks.count() ); 0086 } 0087 0088 0089 void K3b::MusicBrainzJob::slotMbJobFinished( bool success ) 0090 { 0091 if( hasBeenCanceled() ) { 0092 emit canceled(); 0093 jobFinished(false); 0094 } 0095 else { 0096 K3b::AudioTrack* currentTrack = d->tracks.at( d->currentTrackIndex ); 0097 0098 if( success ) { 0099 // found entries 0100 QStringList resultStrings, resultStringsUnique; 0101 for( int i = 0; i < d->mbTrackLookupJob->results(); ++i ) 0102 resultStrings.append( d->mbTrackLookupJob->artist(i) + " - " + d->mbTrackLookupJob->title(i) ); 0103 0104 // since we are only using the title and the artist a lot of entries are alike to us 0105 // so to not let the user have to choose between two equal entries we trim the list down 0106 for( QStringList::const_iterator it = resultStrings.constBegin(); 0107 it != resultStrings.constEnd(); ++it ) 0108 if( !resultStringsUnique.contains( *it ) ) 0109 resultStringsUnique.append( *it ); 0110 0111 QString s; 0112 bool ok = true; 0113 if( resultStringsUnique.count() > 1 ) 0114 s = QInputDialog::getItem( dynamic_cast<QWidget*>(parent()), 0115 i18n("MusicBrainz Query"), 0116 i18n("Found multiple matches for track %1 (%2). Please select one.", 0117 currentTrack->trackNumber(), 0118 currentTrack->firstSource()->sourceComment()), 0119 resultStringsUnique, 0120 0, 0121 false, 0122 &ok ); 0123 else 0124 s = resultStringsUnique.first(); 0125 0126 if( ok ) { 0127 int i = resultStrings.lastIndexOf( s ); 0128 currentTrack->setTitle( d->mbTrackLookupJob->title(i) ); 0129 currentTrack->setArtist( d->mbTrackLookupJob->artist(i) ); 0130 } 0131 } 0132 0133 emit trackFinished( currentTrack, success ); 0134 0135 // query next track 0136 ++d->currentTrackIndex; 0137 if( d->currentTrackIndex < d->tracks.count() ) { 0138 d->mbTrackLookupJob->setAudioTrack( d->tracks.at( d->currentTrackIndex ) ); 0139 d->mbTrackLookupJob->start(); 0140 } 0141 else { 0142 jobFinished( true ); 0143 } 0144 } 0145 } 0146 0147 #include "moc_k3bmusicbrainzjob.cpp"