File indexing completed on 2024-10-06 04:26:01

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include <config-k3b.h>
0007 
0008 #include "k3bmusicbrainz.h"
0009 
0010 #include <musicbrainz/mb_c.h>
0011 
0012 #include <KProtocolManager>
0013 #include <QDebug>
0014 #include <QUrl>
0015 
0016 
0017 class K3b::MusicBrainz::Private
0018 {
0019 public:
0020     musicbrainz_t mb;
0021 
0022     QStringList titles;
0023     QStringList artists;
0024 };
0025 
0026 
0027 K3b::MusicBrainz::MusicBrainz()
0028 {
0029     d = new Private;
0030     d->mb = mb_New();
0031     mb_UseUTF8( d->mb, 1 );
0032 }
0033 
0034 
0035 K3b::MusicBrainz::~MusicBrainz()
0036 {
0037     mb_Delete( d->mb );
0038     delete d;
0039 }
0040 
0041 
0042 int K3b::MusicBrainz::query( const QByteArray& trm )
0043 {
0044     d->titles.clear();
0045     d->artists.clear();
0046 
0047     if( KProtocolManager::useProxy() ) {
0048         QUrl proxy( KProtocolManager::proxyFor("http") );
0049         mb_SetProxy( d->mb, const_cast<char*>(proxy.host().toLatin1().constData()), short(proxy.port()) );
0050     }
0051 
0052     const char* args[2];
0053     args[0] = trm.data();
0054     args[1] = 0;
0055 
0056     if( mb_QueryWithArgs( d->mb, (char*)MBQ_TrackInfoFromTRMId, (char**)args ) ) {
0057 
0058         unsigned int i = 1;
0059         while( mb_Select(d->mb, (char*)MBS_Rewind) && mb_Select1( d->mb, (char*)MBS_SelectTrack, i ) ) {
0060             QByteArray data( 256, 0 );
0061             mb_GetResultData( d->mb, (char*)MBE_TrackGetArtistName, data.data(), 256 );
0062             d->artists.append( QString::fromUtf8( data ).trimmed() );
0063             mb_GetResultData( d->mb, (char*)MBE_TrackGetTrackName, data.data(), 256 );
0064             d->titles.append( QString::fromUtf8( data ).trimmed() );
0065 
0066             ++i;
0067         }
0068 
0069         return i-1;
0070     }
0071     else {
0072         char buffer[256];
0073         mb_GetQueryError( d->mb, buffer, 256 );
0074         qDebug() << "(K3b::MusicBrainz) query error: " << buffer;
0075         return 0;
0076     }
0077 }
0078 
0079 
0080 QString K3b::MusicBrainz::title( unsigned int i ) const
0081 {
0082     return d->titles[i];
0083 }
0084 
0085 
0086 QString K3b::MusicBrainz::artist( unsigned int i ) const
0087 {
0088     return d->artists[i];
0089 }