File indexing completed on 2024-06-16 07:42:24

0001 /*
0002     SPDX-FileCopyrightText: 2008-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bcddb.h"
0009 #include "k3bmedium.h"
0010 #include "k3btoc.h"
0011 #include "k3b_i18n.h"
0012 
0013 #include <QApplication>
0014 #include <QDialog>
0015 #include <QDialogButtonBox>
0016 #include <QVBoxLayout>
0017 #include <QListWidget>
0018 #include <QLabel>
0019 
0020 #include <KCDDB/Client>
0021 
0022 
0023 KCDDB::TrackOffsetList K3b::CDDB::createTrackOffsetList( const K3b::Device::Toc& toc )
0024 {
0025     KCDDB::TrackOffsetList trackOffsets;
0026     foreach( const K3b::Device::Track& track, toc ) {
0027         trackOffsets.append( track.firstSector().lba() + 150 );
0028     }
0029     trackOffsets.append( toc.length().lba() + 150 );
0030     return trackOffsets;
0031 }
0032 
0033 
0034 int K3b::CDDB::MultiEntriesDialog::selectCddbEntry( const KCDDB::CDInfoList& entries, QWidget* parent )
0035 {
0036     QDialog dialog( parent );
0037     dialog.setWindowTitle( i18n("Multiple CDDB Entries Found") );
0038 
0039     QLabel* infoLabel = new QLabel( i18n("K3b found multiple or inexact CDDB entries. Please select one."), &dialog );
0040     infoLabel->setWordWrap( true );
0041 
0042     QListWidget* listBox = new QListWidget( &dialog );
0043     listBox->setSelectionMode( QAbstractItemView::SingleSelection );
0044 
0045     QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dialog );
0046     QObject::connect( buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()) );
0047     QObject::connect( buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()) );
0048 
0049     QVBoxLayout* layout = new QVBoxLayout( &dialog );
0050     layout->setContentsMargins( 0, 0, 0, 0 );
0051     layout->addWidget( infoLabel );
0052     layout->addWidget( listBox );
0053     layout->addWidget( buttonBox );
0054 
0055     dialog.setMinimumSize( 280, 200 );
0056 
0057     int i = 1;
0058     foreach( const KCDDB::CDInfo& info, entries ) {
0059         listBox->addItem( QString::number(i++) + ' ' +
0060                               info.get( KCDDB::Artist ).toString() + " - " +
0061                               info.get( KCDDB::Title ).toString() + " (" +
0062                               info.get( KCDDB::Category ).toString() + ')' );
0063     }
0064 
0065     listBox->setCurrentRow( 0 );
0066 
0067     if( dialog.exec() == QDialog::Accepted )
0068         return listBox->currentRow();
0069     else
0070         return -1;
0071 }
0072 
0073 
0074 class K3b::CDDB::CDDBJob::Private
0075 {
0076 public:
0077     KCDDB::Client cddbClient;
0078     K3b::Medium medium;
0079     K3b::Device::Toc toc;
0080 
0081     KCDDB::CDInfo cddbInfo;
0082 
0083     void _k_cddbQueryFinished( KCDDB::Result );
0084 
0085     CDDBJob* q;
0086 };
0087 
0088 
0089 void K3b::CDDB::CDDBJob::Private::_k_cddbQueryFinished( KCDDB::Result result )
0090 {
0091     if( result == KCDDB::Success ) {
0092         cddbInfo = cddbClient.lookupResponse().first();
0093     }
0094     else if ( result == KCDDB::MultipleRecordFound ) {
0095         KCDDB::CDInfoList results = cddbClient.lookupResponse();
0096         int i = K3b::CDDB::MultiEntriesDialog::selectCddbEntry( results, qApp->activeWindow() );
0097         if ( i >= 0 ) {
0098             cddbInfo = results[i];
0099         }
0100     }
0101     else {
0102         q->setError( KJob::UserDefinedError );
0103         q->setErrorText( KCDDB::resultToString( result ) );
0104     }
0105 
0106     // save the entry locally
0107     if ( cddbInfo.isValid() ) {
0108         cddbClient.store( cddbInfo, K3b::CDDB::createTrackOffsetList( toc ) );
0109     }
0110 
0111     q->emitResult();
0112 }
0113 
0114 
0115 K3b::CDDB::CDDBJob::CDDBJob( QObject* parent )
0116     : KJob( parent ),
0117       d( new Private() )
0118 {
0119     d->q = this;
0120     d->cddbClient.setBlockingMode( false );
0121     connect( &d->cddbClient, SIGNAL(finished(KCDDB::Result)),
0122              this, SLOT(_k_cddbQueryFinished(KCDDB::Result)) );
0123 }
0124 
0125 
0126 K3b::CDDB::CDDBJob::~CDDBJob()
0127 {
0128     delete d;
0129 }
0130 
0131 
0132 K3b::Medium K3b::CDDB::CDDBJob::medium() const
0133 {
0134     return d->medium;
0135 }
0136 
0137 
0138 void K3b::CDDB::CDDBJob::start()
0139 {
0140     qDebug();
0141     d->cddbInfo.clear();
0142     d->cddbClient.lookup( createTrackOffsetList( d->toc ) );
0143 }
0144 
0145 
0146 KCDDB::CDInfo K3b::CDDB::CDDBJob::cddbResult() const
0147 {
0148     return d->cddbInfo;
0149 }
0150 
0151 
0152 K3b::CDDB::CDDBJob* K3b::CDDB::CDDBJob::queryCddb( const K3b::Medium& medium )
0153 {
0154     CDDBJob* job = new CDDBJob();
0155     job->d->medium = medium;
0156     job->d->toc = medium.toc();
0157     // start async so callers can connect to signals
0158     QMetaObject::invokeMethod( job, "start", Qt::QueuedConnection );
0159     return job;
0160 }
0161 
0162 
0163 K3b::CDDB::CDDBJob* K3b::CDDB::CDDBJob::queryCddb( const K3b::Device::Toc& toc )
0164 {
0165     CDDBJob* job = new CDDBJob();
0166     job->d->toc = toc;
0167     // start async so callers can connect to signals
0168     QMetaObject::invokeMethod( job, "start", Qt::QueuedConnection );
0169     return job;
0170 }
0171 
0172 #include "moc_k3bcddb.cpp"